Skip to content

Preserve relative indentation in commented-out code - #2231

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

Christoph Bergmeister (bergmeister) with Copilot wants to merge 5 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 thread Rules/UseConsistentIndentation.cs Outdated
Comment thread Rules/UseConsistentIndentation.cs Outdated
Comment thread Rules/UseConsistentIndentation.cs
* Initial plan

* Preserve indentation across comment runs

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>
@bergmeister
Christoph Bergmeister (bergmeister) removed this pull request from stack #2239 October 26, 2026 11:41
@bergmeister
Christoph Bergmeister (bergmeister) added this pull request to stack #2240 October 26, 2026 11:47
@bergmeister
Christoph Bergmeister (bergmeister) removed this pull request from stack #2240 October 26, 2026 20:40
* Initial plan

* Fix comment runs after line continuations

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

* Fix continuation indent after comment runs

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

* Include line continuations in comment scan

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

* Apply last suggestion 'Prevent double-counting indentation in comment runs' #2238 (comment)

* Fix Pester discovery by removing extra BOM

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>
Co-authored-by: Christoph Bergmeister <c.bergmeister@gmail.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

Inline block comments and comment runs after pipes still have unresolved indentation errors.

2 open issues
3 resolved since last review

Review effort: Balanced

Comment on lines +306 to +307
tempIndentationLevel = indentationLevel +
(commentRunStartsAfterLineContinuation ? 1 : 0);
Comment on lines +382 to +385
(tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine ||
tokens[nextCommentIndex - 1].Kind == TokenKind.LineContinuation) &&
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1;
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