Skip to content

Commit 01054b2

Browse files
Copilotbergmeister
andauthored
Fix repeated comment-run indentation
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 6515c54 commit 01054b2

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎Rules/UseConsistentIndentation.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,18 @@ private static int GetCommentRunIndentationOffset(
466466
while (nextCommentIndex < tokens.Length &&
467467
tokens[nextCommentIndex].Kind == TokenKind.Comment)
468468
{
469+
int firstCommentInGroup = nextCommentIndex;
469470
int currentCommentEndLine = tokens[nextCommentIndex].Extent.EndLineNumber;
470471
while (nextCommentIndex < tokens.Length &&
471472
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
472473
tokens[nextCommentIndex].Extent.StartLineNumber <= currentCommentEndLine)
473474
{
474-
leastIndentedColumn = Math.Min(
475-
leastIndentedColumn,
476-
tokens[nextCommentIndex].Extent.StartColumnNumber);
475+
if (nextCommentIndex == firstCommentInGroup)
476+
{
477+
leastIndentedColumn = Math.Min(
478+
leastIndentedColumn,
479+
tokens[nextCommentIndex].Extent.StartColumnNumber);
480+
}
477481
currentCommentEndLine = Math.Max(
478482
currentCommentEndLine,
479483
tokens[nextCommentIndex].Extent.EndLineNumber);

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,22 @@ if ($a) {
343343
Should -Be $scriptDefinition
344344
}
345345

346+
It 'Should not increase indentation across a multiline block comment when repeatedly formatted' {
347+
$scriptDefinition = @'
348+
if ($a) {
349+
<# first
350+
#> <# second #>
351+
# nested
352+
}
353+
'@
354+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
355+
356+
$formatted = Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
357+
$formatted | Should -Be $scriptDefinition
358+
Invoke-Formatter -ScriptDefinition $formatted -Settings $settings |
359+
Should -Be $scriptDefinition
360+
}
361+
346362
It 'Should preserve indentation with many adjacent block comments' {
347363
$comments = (1..16 | ForEach-Object { "<# comment $_ #>" }) -join ' '
348364
$scriptDefinition = @(

0 commit comments

Comments
 (0)