Skip to content

Track formatter ranges through iterative corrections - #2264

Open
Christoph Bergmeister (bergmeister) with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-invoke-formatter-range
Open

Christoph Bergmeister (bergmeister) with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-invoke-formatter-range

Conversation

Copilot AI commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Invoke-Formatter -Range could silently drop later corrections when earlier edits moved them beyond the original range end. Shortening a selected line could also leave invalid range coordinates.

  • Range tracking: Update the range end after each applied correction, accounting for both line and column changes across iterations and rules.
  • Regression coverage: Cover expansion, contraction, multiline selections, and line insertion/removal while preserving adjacent unselected lines.
Invoke-Formatter -ScriptDefinition "if(`$a){'x'}`nif(`$b){'y'}" -Range @(2, 1, 2, 12)

Produces:

if($a){'x'}
if ($b) { 'y' }

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with Invoke-Formatter -Range dropping corrections Track formatter ranges through iterative corrections Oct 27, 2026

Copilot AI 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.

🟡 Changes recommended

Empty replacements can cause range-endpoint mapping to throw after deleting selected lines, so zero-line replacements need explicit handling, including when no selected lines remain.

1 open issue
What changed in this PR

This PR addresses #2211 by tracking formatter selection boundaries as corrections change the text, so later fixes remain within the intended range.

Changes:

  • Update the range endpoint after each correction, accounting for line and column changes.
  • Add regression coverage for expanding and shortening selections, inserting and removing lines, and preserving adjacent unselected lines.
File Description
Tests/​Engine/​InvokeFormatter.tests.ps1 Adds range-formatting regression cases.
Engine/​ScriptAnalyzer.cs Replaces per-pass line-count adjustment with per-correction endpoint tracking.

Review effort: Balanced

Comment thread Engine/ScriptAnalyzer.cs Resolved
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

Copilot AI 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.

🟡 Changes recommended

Zero-line replacements mishandle selection endpoints, potentially causing negative-column exceptions or allowing subsequent formatting to modify originally unselected text.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread Engine/ScriptAnalyzer.cs Resolved
Comment thread Engine/ScriptAnalyzer.cs Resolved
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

Copilot AI 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.

🟢 Approval recommended

No active review issues block the changes to keep formatter corrections within the intended selection.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@bergmeister
Christoph Bergmeister (bergmeister) marked this pull request as ready for review October 28, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-Formatter -Range silently drops corrections after earlier fixes shift text

3 participants