Skip to content

Commit 2e1c903

Browse files
Copilotbergmeister
andauthored
Handle block comment boundaries in indentation runs
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 0e5eb9f commit 2e1c903

2 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎Rules/UseConsistentIndentation.cs‎

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,18 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
279279
if (tokenIndex > 2 && tokens[tokenIndex - 1].Kind == TokenKind.NewLine
280280
&& tokens[tokenIndex - 2].Kind == TokenKind.Comment)
281281
{
282+
int precedingCommentIndex = tokenIndex - 2;
283+
while (precedingCommentIndex > 0 &&
284+
tokens[precedingCommentIndex - 1].Kind == TokenKind.Comment &&
285+
tokens[precedingCommentIndex - 1].Extent.EndLineNumber >=
286+
tokens[precedingCommentIndex].Extent.StartLineNumber)
287+
{
288+
precedingCommentIndex--;
289+
}
290+
282291
GetCommentRunIndentationOffset(
283292
tokens,
284-
tokenIndex - 2,
293+
precedingCommentIndex,
285294
out bool commentRunStartsAfterLineContinuation,
286295
out _);
287296
if (commentRunStartsAfterLineContinuation)
@@ -366,19 +375,30 @@ private static int GetCommentRunIndentationOffset(
366375
commentRunStartsAfterLineContinuation =
367376
tokenIndex > 0 &&
368377
tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation;
369-
if (tokenIndex > 2 &&
370-
(tokens[tokenIndex - 1].Kind == TokenKind.NewLine ||
371-
tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation) &&
372-
tokens[tokenIndex - 2].Kind == TokenKind.Comment &&
373-
(tokens[tokenIndex - 3].Kind == TokenKind.NewLine ||
374-
tokens[tokenIndex - 3].Kind == TokenKind.LineContinuation))
378+
int precedingCommentIndex = tokenIndex - 1;
379+
if (precedingCommentIndex >= 0 &&
380+
(tokens[precedingCommentIndex].Kind == TokenKind.NewLine ||
381+
tokens[precedingCommentIndex].Kind == TokenKind.LineContinuation))
375382
{
376-
GetCommentRunIndentationOffset(
377-
tokens,
378-
tokenIndex - 2,
379-
out bool precedingCommentRunStartsAfterLineContinuation,
380-
out commentRunStartsAfterPipeline);
381-
commentRunStartsAfterLineContinuation |= precedingCommentRunStartsAfterLineContinuation;
383+
precedingCommentIndex--;
384+
if (precedingCommentIndex >= 0 &&
385+
tokens[precedingCommentIndex].Kind == TokenKind.Comment)
386+
{
387+
while (precedingCommentIndex > 0 &&
388+
tokens[precedingCommentIndex - 1].Kind == TokenKind.Comment &&
389+
tokens[precedingCommentIndex - 1].Extent.EndLineNumber >=
390+
tokens[precedingCommentIndex].Extent.StartLineNumber)
391+
{
392+
precedingCommentIndex--;
393+
}
394+
395+
GetCommentRunIndentationOffset(
396+
tokens,
397+
precedingCommentIndex,
398+
out bool precedingCommentRunStartsAfterLineContinuation,
399+
out commentRunStartsAfterPipeline);
400+
commentRunStartsAfterLineContinuation |= precedingCommentRunStartsAfterLineContinuation;
401+
}
382402
}
383403

384404
return 0;
@@ -389,12 +409,15 @@ private static int GetCommentRunIndentationOffset(
389409
int precedingIndex = tokenIndex - 1;
390410
while (precedingIndex >= 0 &&
391411
tokens[precedingIndex].Kind == TokenKind.Comment &&
392-
tokens[precedingIndex].Extent.StartLineNumber == currentLine)
412+
(tokens[precedingIndex].Extent.StartLineNumber == currentLine ||
413+
tokens[precedingIndex].Extent.EndLineNumber >=
414+
tokens[firstCommentIndex].Extent.StartLineNumber))
393415
{
394416
firstCommentIndex = precedingIndex;
395417
precedingIndex--;
396418
}
397419

420+
currentLine = tokens[firstCommentIndex].Extent.StartLineNumber;
398421
while (precedingIndex >= 0 &&
399422
(tokens[precedingIndex].Kind == TokenKind.NewLine ||
400423
tokens[precedingIndex].Kind == TokenKind.LineContinuation))
@@ -413,7 +436,9 @@ private static int GetCommentRunIndentationOffset(
413436
int precedingCommentIndex = precedingIndex - 1;
414437
while (precedingCommentIndex >= 0 &&
415438
tokens[precedingCommentIndex].Kind == TokenKind.Comment &&
416-
tokens[precedingCommentIndex].Extent.StartLineNumber == previousCommentLine)
439+
(tokens[precedingCommentIndex].Extent.StartLineNumber == previousCommentLine ||
440+
tokens[precedingCommentIndex].Extent.EndLineNumber >=
441+
tokens[firstCommentOnPreviousLine].Extent.StartLineNumber))
417442
{
418443
firstCommentOnPreviousLine = precedingCommentIndex;
419444
precedingCommentIndex--;
@@ -427,7 +452,7 @@ private static int GetCommentRunIndentationOffset(
427452
}
428453

429454
firstCommentIndex = firstCommentOnPreviousLine;
430-
currentLine = previousCommentLine;
455+
currentLine = tokens[firstCommentOnPreviousLine].Extent.StartLineNumber;
431456
precedingIndex = precedingCommentIndex;
432457
}
433458

@@ -441,14 +466,17 @@ private static int GetCommentRunIndentationOffset(
441466
while (nextCommentIndex < tokens.Length &&
442467
tokens[nextCommentIndex].Kind == TokenKind.Comment)
443468
{
444-
int currentCommentLine = tokens[nextCommentIndex].Extent.StartLineNumber;
469+
int currentCommentEndLine = tokens[nextCommentIndex].Extent.EndLineNumber;
445470
while (nextCommentIndex < tokens.Length &&
446471
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
447-
tokens[nextCommentIndex].Extent.StartLineNumber == currentCommentLine)
472+
tokens[nextCommentIndex].Extent.StartLineNumber <= currentCommentEndLine)
448473
{
449474
leastIndentedColumn = Math.Min(
450475
leastIndentedColumn,
451476
tokens[nextCommentIndex].Extent.StartColumnNumber);
477+
currentCommentEndLine = Math.Max(
478+
currentCommentEndLine,
479+
tokens[nextCommentIndex].Extent.EndLineNumber);
452480
nextCommentIndex++;
453481
}
454482

@@ -458,7 +486,7 @@ private static int GetCommentRunIndentationOffset(
458486
tokens[nextCommentIndex + 1].Kind != TokenKind.Comment ||
459487
HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex + 1) ||
460488
tokens[nextCommentIndex + 1].Extent.StartLineNumber !=
461-
tokens[nextCommentIndex - 1].Extent.EndLineNumber + 1)
489+
currentCommentEndLine + 1)
462490
{
463491
break;
464492
}

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,44 @@ Write-Output `
305305
Should -Be $scriptDefinition
306306
}
307307

308+
It 'Should preserve continuation indentation after multiple block comments before an argument' {
309+
$scriptDefinition = @'
310+
Write-Output `
311+
<# first #> <# second #>
312+
<# note #> -InputObject 'value'
313+
'@
314+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
315+
316+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
317+
Should -Be $scriptDefinition
318+
}
319+
320+
It 'Should preserve pipeline context after multiple block comments before an argument' {
321+
$scriptDefinition = @'
322+
Write-Output |
323+
<# first #> <# second #>
324+
<# note #> -InputObject 'value'
325+
'@
326+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
327+
328+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
329+
Should -Be $scriptDefinition
330+
}
331+
332+
It 'Should preserve nested indentation across a multiline block comment' {
333+
$scriptDefinition = @'
334+
if ($a) {
335+
<# first
336+
#> <# second #>
337+
# nested
338+
}
339+
'@
340+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
341+
342+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
343+
Should -Be $scriptDefinition
344+
}
345+
308346
It 'Should preserve indentation with many adjacent block comments' {
309347
$comments = (1..16 | ForEach-Object { "<# comment $_ #>" }) -join ' '
310348
$scriptDefinition = @(

0 commit comments

Comments
 (0)