Skip to content

Fix PlaceOpenBrace/PlaceCloseBrace corrections leaving stray whitespace and misindented braces - #2262

Open
Christoph Bergmeister (bergmeister) with Copilot wants to merge 5 commits into
mainfrom
copilot/ignore-onelineblock-correction
Open

Christoph Bergmeister (bergmeister) with Copilot wants to merge 5 commits into
mainfrom
copilot/ignore-onelineblock-correction

Conversation

Copilot AI commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

With IgnoreOneLineBlock = $false, splitting a one-line if/else produced stray leading and trailing spaces, a } indented by 3 spaces, and a else with a leading space:

Invoke-Formatter -ScriptDefinition "if (`$RegEx) { '-match' } else { '-like' }" -Settings @{
    Rules = @{
        PSPlaceOpenBrace  = @{ Enable = $true; IgnoreOneLineBlock = $false }
        PSPlaceCloseBrace = @{ Enable = $true; IgnoreOneLineBlock = $false }
    }
}
Before After
if ($RegEx) {
'-match'
}
else {
'-like'
}
if ($RegEx) {
'-match'
}
else {
'-like'
}

About the issue's expected output: these settings run only the two brace rules. PSPlaceCloseBrace.NewLineAfter defaults to $true, so it puts else on its own line, and none of the enabled rules indents the body (that's PSUseConsistentIndentation). The "After" column is therefore the correct result. The CodeFormattingOTBS preset with IgnoreOneLineBlock = $false now produces exactly the output the issue expected:

if ($RegEx) {
    '-match'
} else {
    '-like'
}

Changes

  • PlaceOpenBrace, NewLineAfter correction: the correction extent now runs from { to the next token on the same line, so it also removes the whitespace after the brace.
  • PlaceCloseBrace, BraceShouldBeOnNewLine correction: when the preceding token is on the same line, the extent now starts at that token's end, so it also removes the whitespace before }.
  • PlaceCloseBrace.GetIndentation: the loop condition was k > 0, so it never checked the first token in the script. When the open brace was on the first line, } took its indentation from the 2nd token (( → 3 spaces). Changed to k >= 0. This also affects the NoEmptyLineBefore correction, which uses the same helper.
  • PlaceCloseBrace, NewLineAfter correction: the extent now runs from } up to the branch keyword (else/elseif/catch/finally). The replacement is } + newline + the brace's indentation, so the keyword lines up with } and no leading space remains.

Tests

  • Three existing correction-extent tests now expect ViolationText to be '} ' (brace plus the following space) because the extent now includes that space.
  • Added regression tests:
    • the scenario from the issue;
    • a nested block, to check that } and else keep their indentation;
    • the OTBS preset with IgnoreOneLineBlock = $false;
    • the open-brace correction extent.

…ce and wrong indentation

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix IgnoreOneLineBlock=false correction issue in PSPlaceOpenBrace and PSPlaceCloseBrace Fix PlaceOpenBrace/PlaceCloseBrace corrections leaving stray whitespace and misindented braces 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

The correction can misalign else after a multiline condition by using the opening-brace line’s indentation instead of preserving the closing brace’s leading whitespace, and regression coverage is needed.

1 open issue
What changed in this PR

This PR addresses #2218 by refining brace-formatting corrections so splitting one-line blocks removes stray whitespace and aligns closing braces and branch keywords.

Changes:

  • Extend correction ranges to consume whitespace adjacent to braces.
  • Include the script’s first token when calculating closing-brace indentation.
  • Add regression tests for one-line statements, nested branches, OTBS formatting, and correction ranges.
File Description
Tests/​Rules/​PlaceOpenBrace.tests.ps1 Verify that opening-brace corrections consume following whitespace.
Tests/​Rules/​PlaceCloseBrace.tests.ps1 Update correction expectations and add formatting regressions.
Rules/​PlaceOpenBrace.cs Remove whitespace after { when inserting a newline.
Rules/​PlaceCloseBrace.cs Adjust correction ranges and indentation for closing braces and branches.

Review effort: Balanced

Comment thread Rules/PlaceCloseBrace.cs Outdated
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.

🔵 Needs a closer look

The formatter uses continuation-line indentation for multiline conditions, causing correctly aligned } else { blocks to gain an extra-indented else that brace-only formatting does not repair.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Use closing brace indentation for split-condition else formatting

Rules/​PlaceCloseBrace.cs:288

For a condition split as if ($a -and followed by an indented $b) {, GetIndentation() returns the continuation line's indentation rather than the closing brace's indentation. An already correctly aligned } else { therefore becomes } followed by an extra-indented else, which brace-only formatting does not repair. Use the closing brace's actual leading whitespace when it already starts its own line, falling back to the helper only for an inline brace.

Review effort: Balanced

* Initial plan

* Preserve close brace indentation for branches

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
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

The closing-brace rule replaces tab-based indentation with spaces when splitting a branch, misaligning else; it needs to preserve the original leading whitespace and include a tab-indented regression test.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread Rules/PlaceCloseBrace.cs Outdated
* Initial plan

* Preserve tab indentation when formatting branches

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
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 were identified for the brace-formatting corrections.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@bergmeister
Christoph Bergmeister (bergmeister) marked this pull request as ready for review October 28, 2026 13:20
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.

IgnoreOneLineBlock=$false Correction Issue in PSPlaceOpenBrace and PSPlaceCloseBrace

3 participants