Skip to content

Commit 075fdbb

Browse files
CopilotbergmeisterCopilot
authored
Exclude inline-code block comments from comment indentation runs (#2243)
* Initial plan * Exclude inline code comments from comment runs Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> * Refactor HasCodeAfterCommentOnSameLine method Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix comment helper compilation error Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> * Preserve pipeline context through comment continuations Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> * Avoid inheriting pipeline context from trailing comments 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> Co-authored-by: Christoph Bergmeister <c.bergmeister@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent b028aaf commit 075fdbb

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎Rules/UseConsistentIndentation.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,30 @@ private static int GetCommentRunIndentationOffset(
359359
out bool commentRunStartsAfterLineContinuation,
360360
out bool commentRunStartsAfterPipeline)
361361
{
362+
commentRunStartsAfterLineContinuation = false;
363+
commentRunStartsAfterPipeline = false;
364+
if (HasCodeAfterCommentOnSameLine(tokens, tokenIndex))
365+
{
366+
commentRunStartsAfterLineContinuation =
367+
tokenIndex > 0 &&
368+
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))
375+
{
376+
GetCommentRunIndentationOffset(
377+
tokens,
378+
tokenIndex - 2,
379+
out _,
380+
out commentRunStartsAfterPipeline);
381+
}
382+
383+
return 0;
384+
}
385+
362386
int firstCommentIndex = tokenIndex;
363387
int currentLine = tokens[tokenIndex].Extent.StartLineNumber;
364388
int precedingIndex = tokenIndex - 1;
@@ -369,6 +393,7 @@ private static int GetCommentRunIndentationOffset(
369393
precedingIndex--;
370394
if (precedingIndex < 0 ||
371395
tokens[precedingIndex].Kind != TokenKind.Comment ||
396+
HasCodeAfterCommentOnSameLine(tokens, precedingIndex) ||
372397
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1 ||
373398
(precedingIndex > 0 &&
374399
tokens[precedingIndex - 1].Kind != TokenKind.NewLine &&
@@ -393,6 +418,7 @@ private static int GetCommentRunIndentationOffset(
393418
(tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine ||
394419
tokens[nextCommentIndex - 1].Kind == TokenKind.LineContinuation) &&
395420
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
421+
!HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex) &&
396422
tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1;
397423
nextCommentIndex += 2)
398424
{
@@ -405,6 +431,16 @@ private static int GetCommentRunIndentationOffset(
405431
leastIndentedColumn;
406432
}
407433

434+
private static bool HasCodeAfterCommentOnSameLine(Token[] tokens, int commentIndex)
435+
{
436+
Token nextToken = NextTokenIgnoringComments(tokens, commentIndex);
437+
return nextToken != null &&
438+
nextToken.Kind != TokenKind.NewLine &&
439+
nextToken.Kind != TokenKind.LineContinuation &&
440+
nextToken.Kind != TokenKind.EndOfInput &&
441+
nextToken.Extent.StartLineNumber <= tokens[commentIndex].Extent.EndLineNumber;
442+
}
443+
408444
/// <summary>
409445
/// Scans forward from the current opener to the end of the line.
410446
/// Returns true if there is at least one unclosed opener when

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,25 @@ Write-Output `
284284
Should -Be $scriptDefinition
285285
}
286286

287+
It 'Should not include a block comment followed by code in a comment run' {
288+
$scriptDefinition = @'
289+
if ($a) {
290+
# comment
291+
<# note #> Write-Output 'value'
292+
}
293+
'@
294+
$expectedScriptDefinition = @'
295+
if ($a) {
296+
# comment
297+
<# note #> Write-Output 'value'
298+
}
299+
'@
300+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
301+
302+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
303+
Should -Be $expectedScriptDefinition
304+
}
305+
287306
It 'Should preserve a comment run containing an internal line continuation with <Kind> indentation' -TestCases @(
288307
@{ Kind = 'space'; Indentation = ' ' }
289308
@{ Kind = 'tab'; Indentation = "`t" }
@@ -674,6 +693,36 @@ bar
674693
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def
675694
}
676695

696+
It "Should preserve pipeline indentation across a comment line continuation with PipelineIndentation None" {
697+
$def = @'
698+
foo |
699+
<# comment #> `
700+
<# note #> bar
701+
'@
702+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None'
703+
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def
704+
}
705+
706+
It "Should not inherit pipeline indentation from a trailing comment with PipelineIndentation None" {
707+
$def = @'
708+
function test {
709+
foo | bar # trailing
710+
<# note #> baz
711+
}
712+
'@
713+
$expected = @'
714+
function test {
715+
foo | bar # trailing
716+
<# note #> baz
717+
}
718+
'@
719+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None'
720+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
721+
$violations.Count | Should -Be 1
722+
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected
723+
Invoke-Formatter -ScriptDefinition $expected -Settings $settings | Should -Be $expected
724+
}
725+
677726
It "Should indent properly after line continuation (backtick) character with pipeline" {
678727
$def = @'
679728
foo |

0 commit comments

Comments
 (0)