You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Preserve relative indentation in commented-out code - #2231
PSUseConsistentIndentation flattened commented-out code by aligning each comment marker independently, erasing indentation that represented nested structure. Comment runs should retain their relative indentation, while still allowing the run to align with its surrounding code.
Comment-run indentation: Preserve column offsets across adjacent standalone comment lines when applying indentation corrections. Inline comments are not grouped into a run.
Regression coverage: Cover space and tab indentation, shifting a run as a unit, and code following the run.
For example, the nested comment remains nested after formatting:
if ($a) {
# if ($b) {# doThing# }Write-Output'after'
}
CopilotAI
changed the title
[WIP] Fix PSUseConsistentIndentation flattening indented comments
Preserve relative indentation in commented-out code
Oct 22, 2026
This PR addresses #2217 by updating PSUseConsistentIndentation to preserve relative indentation in adjacent standalone comments instead of flattening commented-out code.
Changes:
Calculates comment-run offsets and applies them to indentation corrections.
Adds space- and tab-based regression tests for preserving runs, shifting runs together, and retaining following code.
File
Description
Tests/Rules/UseConsistentIndentation.tests.ps1
Adds comment-run formatting regression tests.
Rules/UseConsistentIndentation.cs
Incorporates relative comment offsets into indentation corrections.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Stateful interactions between comment grouping, continuations, and pipelines warrant human validation of formatter correctness and repeated-formatting stability.
Add a regression test for standalone comments following trailing inline comments.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Inline comments after line-leading pipes can still join standalone comment runs and incorrectly shift pipeline indentation.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Exclude trailing comments from pipeline indentation offset calculation
Rules/UseConsistentIndentation.cs:373
With PipelineIndentation = 'NoIndentation', a line-leading pipe's trailing comment reaches this scan and is incorrectly grouped with following standalone comments. For a function containing a four-space-indented, backtick-continued foo, then | # trailing, # standalone, and bar, formatting moves the pipe from column 5 to column 11. Exclude comments with code before them as well as code after them from run-offset calculation.
Inline comments after line-leading pipes can still join standalone comment runs and incorrectly shift pipeline indentation.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Exclude trailing comments from pipeline indentation offset calculation Rules/UseConsistentIndentation.cs:373
With PipelineIndentation = 'NoIndentation', a line-leading pipe's trailing comment reaches this scan and is incorrectly grouped with following standalone comments. For a function containing a four-space-indented, backtick-continued foo, then | # trailing, # standalone, and bar, formatting moves the pipe from column 5 to column 11. Exclude comments with code before them as well as code after them from run-offset calculation.
Review effort: Balanced
Copilot fix and add regression test. create new pr instead of pushing to this branch
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Recursive comment scanning interacts with continuation and pipeline indentation, warranting final human validation despite the extensive regression coverage.
No open issues 🎉
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PSUseConsistentIndentationflattened commented-out code by aligning each comment marker independently, erasing indentation that represented nested structure. Comment runs should retain their relative indentation, while still allowing the run to align with its surrounding code.For example, the nested comment remains nested after formatting: