Skip to content

fix impl unwrap block #2207 - #3115

Closed
asukaminato0721 wants to merge 6 commits into
meta:mainfrom
asukaminato0721:24552-ty
Closed

asukaminato0721 wants to merge 6 commits into
meta:mainfrom
asukaminato0721:24552-ty

Conversation

@asukaminato0721

@asukaminato0721 asukaminato0721 commented Apr 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

part of #2207

impl unwrap block

astral-sh/ruff#24552

Test Plan

add test

@meta-cla meta-cla Bot added the cla signed label Apr 12, 2026
@asukaminato0721 asukaminato0721 changed the title fix impl unwrap scope #2207 fix impl unwrap block #2207 Apr 12, 2026
@asukaminato0721
asukaminato0721 marked this pull request as ready for review April 12, 2026 17:51
Copilot AI review requested due to automatic review settings April 12, 2026 17:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an “Unwrap block” refactor code action to the Pyrefly LSP, enabling users to remove an enclosing block statement (e.g., if, else, for/while without else, with) and reindent the block body into the parent scope.

Changes:

  • Implemented unwrap_block quick-fix/refactor logic, including AST traversal and text edit construction.
  • Wired the new refactor into Transaction and the non-wasm LSP server’s code-action collection.
  • Added initial LSP code-action tests for unwrap-block (basic if, else, and loop-else rejection).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pyrefly/lib/test/lsp/code_actions.rs Adds unwrap-block helpers and a few targeted tests.
pyrefly/lib/state/lsp/quick_fixes/unwrap_block.rs New unwrap-block refactor implementation (target detection + edit generation).
pyrefly/lib/state/lsp/quick_fixes/mod.rs Exposes the new unwrap_block module.
pyrefly/lib/state/lsp.rs Adds Transaction::unwrap_block_code_actions API entrypoint.
pyrefly/lib/lsp/non_wasm/server.rs Includes unwrap-block in refactor code-action computation/telemetry.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pyrefly/lib/test/lsp/code_actions.rs Resolved
@github-actions

This comment has been minimized.

@connernilsen connernilsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @asukaminato0721, this is looking good, but a few things (mostly requests for more tests). Let me know if you have questions

Comment thread pyrefly/lib/test/lsp/code_actions.rs Resolved
Comment thread pyrefly/lib/test/lsp/code_actions.rs Resolved
@github-actions

This comment has been minimized.

@connernilsen connernilsen left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more things I found

Comment thread pyrefly/lib/test/lsp/code_actions.rs Resolved
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@codspeed

codspeed Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 35 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing asukaminato0721:24552-ty (d5ea5b6) with main (4788884)

Open in CodSpeed

Footnotes

  1. 9 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@github-actions

This comment has been minimized.

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@stroxler

Copy link
Copy Markdown
Contributor

I'm closing this PR; it's too old and attached to a stale diff internally, which makes it hard to review.

I think we can turn around review faster on a fresh PR, if you choose to make one.

If you do, there are at least four correctness issues AI review flagged that would be good to look into:

  • Dangling clause headers: unwrapping an except before else or finally retains that next header but deletes its body, producing invalid Python.
  • Multiline-string corruption: reindentation changes whitespace inside string literals—even when unwrapping an always-true if.
  • Incorrect suite-colon detection: a multiline header such as if g(lambda: 1,\n 2): print("x") offers an action that retains part of the header and produces invalid Python.
  • Loop-control escape: unwrapping a loop containing break or continue can leave invalid code or redirect control to an enclosing loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants