From 56aa35d21a9ce0ef0703c78126a7db86f20a94e0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 20:53:03 +0000 Subject: [PATCH 1/6] Initial plan From 1e7b679c5ce332fe8df46fc1d256863bb4a3a6af Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 20:58:48 +0000 Subject: [PATCH 2/6] Exclude inline code comments from comment runs Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 24 +++++++++++++++++++ .../Rules/UseConsistentIndentation.tests.ps1 | 19 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 5fd573cc5..b2411c953 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -350,6 +350,12 @@ private static int GetCommentRunIndentationOffset( int tokenIndex, out bool commentRunStartsAfterLineContinuation) { + commentRunStartsAfterLineContinuation = false; + if (HasCodeAfterCommentOnSameLine(tokens, tokenIndex)) + { + return 0; + } + int firstCommentIndex = tokenIndex; int currentLine = tokens[tokenIndex].Extent.StartLineNumber; int precedingIndex = tokenIndex - 1; @@ -360,6 +366,7 @@ private static int GetCommentRunIndentationOffset( precedingIndex--; 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 && @@ -382,6 +389,7 @@ private static int GetCommentRunIndentationOffset( (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) { @@ -394,6 +402,22 @@ private static int GetCommentRunIndentationOffset( leastIndentedColumn; } + private static bool HasCodeAfterCommentOnSameLine(Token[] tokens, int commentIndex) + { + int nextTokenIndex = commentIndex + 1; + if (nextTokenIndex >= tokens.Length) + { + return false; + } + + Token nextToken = tokens[nextTokenIndex]; + return nextToken.Kind != TokenKind.NewLine && + nextToken.Kind != TokenKind.LineContinuation && + nextToken.Kind != TokenKind.EndOfInput && + nextToken.Kind != TokenKind.Comment && + nextToken.Extent.StartLineNumber <= tokens[commentIndex].Extent.EndLineNumber; + } + /// /// Scans forward from the current opener to the end of the line. /// Returns true if there is at least one unclosed opener when diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 6febc02c7..b42f2bfbd 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -284,6 +284,25 @@ Write-Output ` Should -Be $scriptDefinition } + It 'Should not include a block comment followed by code in a comment run' { + $scriptDefinition = @' +if ($a) { + # comment + <# note #> Write-Output 'value' +} +'@ + $expectedScriptDefinition = @' +if ($a) { + # comment + <# note #> Write-Output 'value' +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | + Should -Be $expectedScriptDefinition + } + It 'Should preserve a comment run containing an internal line continuation with indentation' -TestCases @( @{ Kind = 'space'; Indentation = ' ' } @{ Kind = 'tab'; Indentation = "`t" } From 57f8ae07e98ac265f91231b687a034252222c1dc Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Oct 2026 21:11:17 +0000 Subject: [PATCH 3/6] Refactor HasCodeAfterCommentOnSameLine method Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index b2411c953..eeb72c4bc 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -353,6 +353,19 @@ private static int GetCommentRunIndentationOffset( commentRunStartsAfterLineContinuation = false; if (HasCodeAfterCommentOnSameLine(tokens, tokenIndex)) { + commentRunStartsAfterLineContinuation = + tokenIndex > 0 && + tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation; + if (tokenIndex > 2 && + tokens[tokenIndex - 1].Kind == TokenKind.NewLine && + tokens[tokenIndex - 2].Kind == TokenKind.Comment) + { + GetCommentRunIndentationOffset( + tokens, + tokenIndex - 2, + out commentRunStartsAfterLineContinuation); + } + return 0; } @@ -404,17 +417,11 @@ private static int GetCommentRunIndentationOffset( private static bool HasCodeAfterCommentOnSameLine(Token[] tokens, int commentIndex) { - int nextTokenIndex = commentIndex + 1; - if (nextTokenIndex >= tokens.Length) - { - return false; - } - - Token nextToken = tokens[nextTokenIndex]; - return nextToken.Kind != TokenKind.NewLine && + Token nextToken = NextTokenIgnoringComments(tokens, commentIndex); + return nextToken != null && + nextToken.Kind != TokenKind.NewLine && nextToken.Kind != TokenKind.LineContinuation && nextToken.Kind != TokenKind.EndOfInput && - nextToken.Kind != TokenKind.Comment && nextToken.Extent.StartLineNumber <= tokens[commentIndex].Extent.EndLineNumber; } From 024ff36a34a130900c625afdce767ff8a17ffb76 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 22:02:09 +0000 Subject: [PATCH 4/6] Fix comment helper compilation error Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index e3c716d84..a3149de84 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -360,6 +360,7 @@ private static int GetCommentRunIndentationOffset( out bool commentRunStartsAfterPipeline) { commentRunStartsAfterLineContinuation = false; + commentRunStartsAfterPipeline = false; if (HasCodeAfterCommentOnSameLine(tokens, tokenIndex)) { commentRunStartsAfterLineContinuation = @@ -372,7 +373,8 @@ private static int GetCommentRunIndentationOffset( GetCommentRunIndentationOffset( tokens, tokenIndex - 2, - out commentRunStartsAfterLineContinuation); + out commentRunStartsAfterLineContinuation, + out commentRunStartsAfterPipeline); } return 0; From 37e73d89fd18dfce5c2d32a46ca5570880d563ff Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 22:23:10 +0000 Subject: [PATCH 5/6] Preserve pipeline context through comment continuations Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 5 +++-- Tests/Rules/UseConsistentIndentation.tests.ps1 | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index a3149de84..b40a396f1 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -367,13 +367,14 @@ private static int GetCommentRunIndentationOffset( tokenIndex > 0 && tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation; if (tokenIndex > 2 && - tokens[tokenIndex - 1].Kind == TokenKind.NewLine && + (tokens[tokenIndex - 1].Kind == TokenKind.NewLine || + tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation) && tokens[tokenIndex - 2].Kind == TokenKind.Comment) { GetCommentRunIndentationOffset( tokens, tokenIndex - 2, - out commentRunStartsAfterLineContinuation, + out _, out commentRunStartsAfterPipeline); } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 8bc366658..bb19acd08 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -693,6 +693,16 @@ bar Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def } + It "Should preserve pipeline indentation across a comment line continuation with PipelineIndentation None" { + $def = @' +foo | + <# comment #> ` + <# note #> bar +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None' + Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def + } + It "Should indent properly after line continuation (backtick) character with pipeline" { $def = @' foo | From 80a3db723f2782fcb012c6263503590b1733fa4b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 22:47:47 +0000 Subject: [PATCH 6/6] Avoid inheriting pipeline context from trailing comments Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 4 +++- .../Rules/UseConsistentIndentation.tests.ps1 | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index b40a396f1..2dedf530a 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -369,7 +369,9 @@ private static int GetCommentRunIndentationOffset( if (tokenIndex > 2 && (tokens[tokenIndex - 1].Kind == TokenKind.NewLine || tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation) && - tokens[tokenIndex - 2].Kind == TokenKind.Comment) + tokens[tokenIndex - 2].Kind == TokenKind.Comment && + (tokens[tokenIndex - 3].Kind == TokenKind.NewLine || + tokens[tokenIndex - 3].Kind == TokenKind.LineContinuation)) { GetCommentRunIndentationOffset( tokens, diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index bb19acd08..a48cf33c5 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -703,6 +703,26 @@ foo | Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def } + It "Should not inherit pipeline indentation from a trailing comment with PipelineIndentation None" { + $def = @' +function test { + foo | bar # trailing + <# note #> baz +} +'@ + $expected = @' +function test { + foo | bar # trailing + <# note #> baz +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None' + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations.Count | Should -Be 1 + Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected + Invoke-Formatter -ScriptDefinition $expected -Settings $settings | Should -Be $expected + } + It "Should indent properly after line continuation (backtick) character with pipeline" { $def = @' foo |