From 546bc9f336b3a16f466487c08011b3518a311302 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:05:04 +0000 Subject: [PATCH 1/2] Initial plan From 58b9a065cfe80e0a14bb631a3e8c366b31501f63 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:09:22 +0000 Subject: [PATCH 2/2] Preserve indentation across comment runs Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --- Rules/UseConsistentIndentation.cs | 15 ++++++++++++++- Tests/Rules/UseConsistentIndentation.tests.ps1 | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 97c913db5..7c51b98f6 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -360,8 +360,21 @@ private static int GetCommentRunIndentationOffset(Token[] tokens, int tokenIndex precedingIndex--; } + int leastIndentedColumn = tokens[firstCommentIndex].Extent.StartColumnNumber; + for (int nextCommentIndex = firstCommentIndex + 2; + nextCommentIndex < tokens.Length && + tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine && + tokens[nextCommentIndex].Kind == TokenKind.Comment && + tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1; + nextCommentIndex += 2) + { + leastIndentedColumn = Math.Min( + leastIndentedColumn, + tokens[nextCommentIndex].Extent.StartColumnNumber); + } + return tokens[tokenIndex].Extent.StartColumnNumber - - tokens[firstCommentIndex].Extent.StartColumnNumber; + leastIndentedColumn; } /// diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 3a0f325e2..c6418ed49 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -253,6 +253,22 @@ function foo { Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expectedScriptDefinition } + + It 'Should preserve indentation when the first comment is not the least-indented with indentation' -TestCases @( + @{ Kind = 'space'; Indentation = ' ' } + @{ Kind = 'tab'; Indentation = "`t" } + ) { + param([string] $Kind, [string] $Indentation) + $scriptDefinition = @( + "$Indentation# inner" + '# outer' + ) -join "`n" + $settings.Rules.PSUseConsistentIndentation.Kind = $Kind + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | + Should -Be $scriptDefinition + } } Context "When a multi-line command is given" {