diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index c2dcb8688..f60a05d64 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -466,14 +466,18 @@ private static int GetCommentRunIndentationOffset( while (nextCommentIndex < tokens.Length && tokens[nextCommentIndex].Kind == TokenKind.Comment) { + int firstCommentInGroup = nextCommentIndex; int currentCommentEndLine = tokens[nextCommentIndex].Extent.EndLineNumber; while (nextCommentIndex < tokens.Length && tokens[nextCommentIndex].Kind == TokenKind.Comment && tokens[nextCommentIndex].Extent.StartLineNumber <= currentCommentEndLine) { - leastIndentedColumn = Math.Min( - leastIndentedColumn, - tokens[nextCommentIndex].Extent.StartColumnNumber); + if (nextCommentIndex == firstCommentInGroup) + { + leastIndentedColumn = Math.Min( + leastIndentedColumn, + tokens[nextCommentIndex].Extent.StartColumnNumber); + } currentCommentEndLine = Math.Max( currentCommentEndLine, tokens[nextCommentIndex].Extent.EndLineNumber); diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 75b0e099c..39a8b1ecc 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -343,6 +343,22 @@ if ($a) { Should -Be $scriptDefinition } + It 'Should not increase indentation across a multiline block comment when repeatedly formatted' { + $scriptDefinition = @' +if ($a) { + <# first +#> <# second #> + # nested +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + + $formatted = Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings + $formatted | Should -Be $scriptDefinition + Invoke-Formatter -ScriptDefinition $formatted -Settings $settings | + Should -Be $scriptDefinition + } + It 'Should preserve indentation with many adjacent block comments' { $comments = (1..16 | ForEach-Object { "<# comment $_ #>" }) -join ' ' $scriptDefinition = @(