Skip to content

Commit 0c2d630

Browse files
Copilotbergmeister
andauthored
Preserve relative indentation in comment runs (#2236)
* Initial plan * Preserve indentation across comment runs Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 9941989 commit 0c2d630

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎Rules/UseConsistentIndentation.cs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,21 @@ private static int GetCommentRunIndentationOffset(Token[] tokens, int tokenIndex
360360
precedingIndex--;
361361
}
362362

363+
int leastIndentedColumn = tokens[firstCommentIndex].Extent.StartColumnNumber;
364+
for (int nextCommentIndex = firstCommentIndex + 2;
365+
nextCommentIndex < tokens.Length &&
366+
tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine &&
367+
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
368+
tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1;
369+
nextCommentIndex += 2)
370+
{
371+
leastIndentedColumn = Math.Min(
372+
leastIndentedColumn,
373+
tokens[nextCommentIndex].Extent.StartColumnNumber);
374+
}
375+
363376
return tokens[tokenIndex].Extent.StartColumnNumber -
364-
tokens[firstCommentIndex].Extent.StartColumnNumber;
377+
leastIndentedColumn;
365378
}
366379

367380
/// <summary>

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ function foo {
253253
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
254254
Should -Be $expectedScriptDefinition
255255
}
256+
257+
It 'Should preserve indentation when the first comment is not the least-indented with <Kind> indentation' -TestCases @(
258+
@{ Kind = 'space'; Indentation = ' ' }
259+
@{ Kind = 'tab'; Indentation = "`t" }
260+
) {
261+
param([string] $Kind, [string] $Indentation)
262+
$scriptDefinition = @(
263+
"$Indentation# inner"
264+
'# outer'
265+
) -join "`n"
266+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
267+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
268+
269+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
270+
Should -Be $scriptDefinition
271+
}
256272
}
257273

258274
Context "When a multi-line command is given" {

0 commit comments

Comments
 (0)