Repository navigation
Preserve relative indentation in commented-out code - #2231
Draft
Christoph Bergmeister (bergmeister) with Copilot wants to merge 3 commits into
Draft
Christoph Bergmeister (bergmeister) with Copilot wants to merge 3 commits into
Christoph Bergmeister (bergmeister) with Copilot wants to merge 3 commits into
Conversation
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix PSUseConsistentIndentation flattening indented comments
Preserve relative indentation in commented-out code
Oct 22, 2026
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Some comment runs still lose relative indentation, and repeated backward scans introduce quadratic processing.
3 open issues
What changed in this PR
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. |
Review effort: Balanced
Comment on lines
+347
to
+349
| while (precedingIndex >= 0 && tokens[precedingIndex].Kind == TokenKind.NewLine) | ||
| { | ||
| precedingIndex--; |
| if (precedingIndex < 0 || | ||
| tokens[precedingIndex].Kind != TokenKind.Comment || | ||
| tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1 || | ||
| (precedingIndex > 0 && tokens[precedingIndex - 1].Kind != TokenKind.NewLine)) |
| { | ||
| onNewLine = false; | ||
| if (token.Extent.StartColumnNumber - 1 != GetIndentation(expectedIndentationLevel)) | ||
| int expectedIndentation = Math.Max(0, GetIndentation(expectedIndentationLevel) + indentationOffset); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: