From dae8e4c9a2c6c6b7ffab6bc7c83749aec9327fb9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:48:50 +0000 Subject: [PATCH 1/3] Initial plan From 6a517b7d7e9b4532f8c91188cc3545281fcbd922 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:54:18 +0000 Subject: [PATCH 2/3] Handle multiple comments per line in comment runs Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 80 ++++++++++++++----- .../Rules/UseConsistentIndentation.tests.ps1 | 13 +++ 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 4dc2ed6ca..65a0f2982 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -387,6 +387,13 @@ private static int GetCommentRunIndentationOffset( int firstCommentIndex = tokenIndex; int currentLine = tokens[tokenIndex].Extent.StartLineNumber; int precedingIndex = tokenIndex - 1; + while (precedingIndex >= 0 && + tokens[precedingIndex].Kind == TokenKind.Comment && + tokens[precedingIndex].Extent.StartLineNumber == currentLine) + { + precedingIndex--; + } + while (precedingIndex >= 0 && (tokens[precedingIndex].Kind == TokenKind.NewLine || tokens[precedingIndex].Kind == TokenKind.LineContinuation)) @@ -395,17 +402,37 @@ private static int GetCommentRunIndentationOffset( if (precedingIndex < 0 || tokens[precedingIndex].Kind != TokenKind.Comment || HasCodeAfterCommentOnSameLine(tokens, precedingIndex) || - tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1 || - (precedingIndex > 0 && - tokens[precedingIndex - 1].Kind != TokenKind.NewLine && - tokens[precedingIndex - 1].Kind != TokenKind.LineContinuation)) + tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1) { break; } - firstCommentIndex = precedingIndex; - currentLine = tokens[precedingIndex].Extent.StartLineNumber; - precedingIndex--; + int previousCommentLine = tokens[precedingIndex].Extent.StartLineNumber; + int firstCommentOnPreviousLine = precedingIndex; + int precedingCommentIndex = precedingIndex - 1; + while (precedingCommentIndex >= 0 && + tokens[precedingCommentIndex].Kind == TokenKind.Comment && + tokens[precedingCommentIndex].Extent.StartLineNumber == previousCommentLine) + { + if (HasCodeAfterCommentOnSameLine(tokens, precedingCommentIndex)) + { + break; + } + + firstCommentOnPreviousLine = precedingCommentIndex; + precedingCommentIndex--; + } + + if (precedingCommentIndex >= 0 && + tokens[precedingCommentIndex].Kind != TokenKind.NewLine && + tokens[precedingCommentIndex].Kind != TokenKind.LineContinuation) + { + break; + } + + firstCommentIndex = firstCommentOnPreviousLine; + currentLine = previousCommentLine; + precedingIndex = precedingCommentIndex; } commentRunStartsAfterLineContinuation = @@ -413,19 +440,34 @@ private static int GetCommentRunIndentationOffset( tokens[firstCommentIndex - 1].Kind == TokenKind.LineContinuation; commentRunStartsAfterPipeline = PreviousLineEndedWithPipe(tokens, firstCommentIndex, tokens[firstCommentIndex]); - int leastIndentedColumn = tokens[firstCommentIndex].Extent.StartColumnNumber; - for (int nextCommentIndex = firstCommentIndex + 2; - nextCommentIndex < tokens.Length && - (tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine || - tokens[nextCommentIndex - 1].Kind == TokenKind.LineContinuation) && - tokens[nextCommentIndex].Kind == TokenKind.Comment && - !HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex) && - tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1; - nextCommentIndex += 2) + int leastIndentedColumn = int.MaxValue; + int nextCommentIndex = firstCommentIndex; + while (nextCommentIndex < tokens.Length && + tokens[nextCommentIndex].Kind == TokenKind.Comment) { - leastIndentedColumn = Math.Min( - leastIndentedColumn, - tokens[nextCommentIndex].Extent.StartColumnNumber); + int currentCommentLine = tokens[nextCommentIndex].Extent.StartLineNumber; + while (nextCommentIndex < tokens.Length && + tokens[nextCommentIndex].Kind == TokenKind.Comment && + tokens[nextCommentIndex].Extent.StartLineNumber == currentCommentLine) + { + leastIndentedColumn = Math.Min( + leastIndentedColumn, + tokens[nextCommentIndex].Extent.StartColumnNumber); + nextCommentIndex++; + } + + if (nextCommentIndex + 1 >= tokens.Length || + (tokens[nextCommentIndex].Kind != TokenKind.NewLine && + tokens[nextCommentIndex].Kind != TokenKind.LineContinuation) || + tokens[nextCommentIndex + 1].Kind != TokenKind.Comment || + HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex + 1) || + tokens[nextCommentIndex + 1].Extent.StartLineNumber != + tokens[nextCommentIndex - 1].Extent.EndLineNumber + 1) + { + break; + } + + nextCommentIndex++; } return tokens[tokenIndex].Extent.StartColumnNumber - diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index cd295dca8..119a64a6f 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -268,6 +268,19 @@ Write-Output ` Should -Be $scriptDefinition } + It 'Should preserve indentation when a comment-only line contains multiple block comments' { + $scriptDefinition = @' +if ($a) { + <# first #> <# second #> + # nested +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | + Should -Be $scriptDefinition + } + It 'Should preserve continuation indentation before a block-comment-prefixed argument' { $scriptDefinition = @' Write-Output ` From 014b773f7c890e5a8277e0db4751f0cd98c5b589 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 Oct 2026 10:19:52 +0000 Subject: [PATCH 3/3] Fix adjacent comment continuation indentation Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 6 +---- .../Rules/UseConsistentIndentation.tests.ps1 | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 65a0f2982..b36e2c91f 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -391,6 +391,7 @@ private static int GetCommentRunIndentationOffset( tokens[precedingIndex].Kind == TokenKind.Comment && tokens[precedingIndex].Extent.StartLineNumber == currentLine) { + firstCommentIndex = precedingIndex; precedingIndex--; } @@ -414,11 +415,6 @@ private static int GetCommentRunIndentationOffset( tokens[precedingCommentIndex].Kind == TokenKind.Comment && tokens[precedingCommentIndex].Extent.StartLineNumber == previousCommentLine) { - if (HasCodeAfterCommentOnSameLine(tokens, precedingCommentIndex)) - { - break; - } - firstCommentOnPreviousLine = precedingCommentIndex; precedingCommentIndex--; } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 119a64a6f..18439de07 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -293,6 +293,32 @@ Write-Output ` Should -Be $scriptDefinition } + It 'Should preserve continuation indentation after multiple inline block comments' { + $scriptDefinition = @' +Write-Output ` + <# first #> <# second #> + -InputObject 'value' +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | + Should -Be $scriptDefinition + } + + It 'Should preserve indentation with many adjacent block comments' { + $comments = (1..16 | ForEach-Object { "<# comment $_ #>" }) -join ' ' + $scriptDefinition = @( + 'if ($a) {' + " $comments" + ' # nested' + '}' + ) -join "`n" + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | + Should -Be $scriptDefinition + } + It 'Should preserve indentation when the first comment is not the least-indented with indentation' -TestCases @( @{ Kind = 'space'; Indentation = ' ' } @{ Kind = 'tab'; Indentation = "`t" }