Skip to content

Preserve relative indentation in commented-out code - #2231

Draft
Christoph Bergmeister (bergmeister) with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-psuseconsistentindentation-issue
Draft

Christoph Bergmeister (bergmeister) with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-psuseconsistentindentation-issue

Conversation

Copilot AI commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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'
}

Copilot AI and others added 2 commits October 22, 2026 22:04
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

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

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);
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.

PSUseConsistentIndentation flattens the indentation of commented-out code

3 participants