Skip to content

Preserve relative indentation in commented-out code - #2231

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

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

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

Confidence in all multiline comment-group boundaries remains limited, and the regression tests have not been run.

No open issues 🎉

Review effort: Balanced

* Initial plan

* Fix multiline comment group detection

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.

🔵 Needs a closer look

Stateful interactions between comment grouping, continuations, and pipelines warrant human validation of formatter correctness and repeated-formatting stability.

No open issues 🎉

Review effort: Balanced

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>

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

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

Medium severity 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

@bergmeister

Copy link
Copy Markdown
Collaborator

🔵 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

Medium severity 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

Copilot AI commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister) I've opened a new pull request, #2257, to work on those changes. Once the pull request is ready, I'll request review from you.

* Initial plan

* Exclude trailing pipe comments from 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>

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

The implementation and regression coverage address the reported indentation loss, with no unresolved blocking findings.

No open issues 🎉

Review effort: Balanced

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@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

Recursive comment scanning interacts with continuation and pipeline indentation, warranting final human validation despite the extensive regression coverage.

No open issues 🎉

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PSUseConsistentIndentation flattens the indentation of commented-out code

3 participants