Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ private static int GetCommentRunIndentationOffset(
int firstCommentIndex = tokenIndex;
int currentLine = tokens[tokenIndex].Extent.StartLineNumber;
int precedingIndex = tokenIndex - 1;
while (precedingIndex >= 0 &&
tokens[precedingIndex].Kind == TokenKind.Comment &&
tokens[precedingIndex].Extent.StartLineNumber == currentLine)
{
firstCommentIndex = precedingIndex;
precedingIndex--;
Comment thread
Copilot marked this conversation as resolved.
}

while (precedingIndex >= 0 &&
(tokens[precedingIndex].Kind == TokenKind.NewLine ||
tokens[precedingIndex].Kind == TokenKind.LineContinuation))
Expand All @@ -395,37 +403,67 @@ private static int GetCommentRunIndentationOffset(
if (precedingIndex < 0 ||
tokens[precedingIndex].Kind != TokenKind.Comment ||
HasCodeAfterCommentOnSameLine(tokens, precedingIndex) ||
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1 ||
(precedingIndex > 0 &&
tokens[precedingIndex - 1].Kind != TokenKind.NewLine &&
tokens[precedingIndex - 1].Kind != TokenKind.LineContinuation))
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1)
{
break;
}

firstCommentIndex = precedingIndex;
currentLine = tokens[precedingIndex].Extent.StartLineNumber;
precedingIndex--;
int previousCommentLine = tokens[precedingIndex].Extent.StartLineNumber;
int firstCommentOnPreviousLine = precedingIndex;
int precedingCommentIndex = precedingIndex - 1;
while (precedingCommentIndex >= 0 &&
tokens[precedingCommentIndex].Kind == TokenKind.Comment &&
tokens[precedingCommentIndex].Extent.StartLineNumber == previousCommentLine)
{
firstCommentOnPreviousLine = precedingCommentIndex;
precedingCommentIndex--;
}
Comment thread
Copilot marked this conversation as resolved.

if (precedingCommentIndex >= 0 &&
tokens[precedingCommentIndex].Kind != TokenKind.NewLine &&
tokens[precedingCommentIndex].Kind != TokenKind.LineContinuation)
{
break;
}

firstCommentIndex = firstCommentOnPreviousLine;
currentLine = previousCommentLine;
precedingIndex = precedingCommentIndex;
}

commentRunStartsAfterLineContinuation =
firstCommentIndex > 0 &&
tokens[firstCommentIndex - 1].Kind == TokenKind.LineContinuation;
commentRunStartsAfterPipeline =
PreviousLineEndedWithPipe(tokens, firstCommentIndex, tokens[firstCommentIndex]);
int leastIndentedColumn = tokens[firstCommentIndex].Extent.StartColumnNumber;
for (int nextCommentIndex = firstCommentIndex + 2;
nextCommentIndex < tokens.Length &&
(tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine ||
tokens[nextCommentIndex - 1].Kind == TokenKind.LineContinuation) &&
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
!HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex) &&
tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1;
nextCommentIndex += 2)
int leastIndentedColumn = int.MaxValue;
int nextCommentIndex = firstCommentIndex;
while (nextCommentIndex < tokens.Length &&
tokens[nextCommentIndex].Kind == TokenKind.Comment)
{
leastIndentedColumn = Math.Min(
leastIndentedColumn,
tokens[nextCommentIndex].Extent.StartColumnNumber);
int currentCommentLine = tokens[nextCommentIndex].Extent.StartLineNumber;
while (nextCommentIndex < tokens.Length &&
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
tokens[nextCommentIndex].Extent.StartLineNumber == currentCommentLine)
{
leastIndentedColumn = Math.Min(
leastIndentedColumn,
tokens[nextCommentIndex].Extent.StartColumnNumber);
nextCommentIndex++;
}

if (nextCommentIndex + 1 >= tokens.Length ||
(tokens[nextCommentIndex].Kind != TokenKind.NewLine &&
tokens[nextCommentIndex].Kind != TokenKind.LineContinuation) ||
tokens[nextCommentIndex + 1].Kind != TokenKind.Comment ||
HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex + 1) ||
tokens[nextCommentIndex + 1].Extent.StartLineNumber !=
tokens[nextCommentIndex - 1].Extent.EndLineNumber + 1)
{
break;
}

nextCommentIndex++;
}

return tokens[tokenIndex].Extent.StartColumnNumber -
Expand Down
39 changes: 39 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ Write-Output `
Should -Be $scriptDefinition
}

It 'Should preserve indentation when a comment-only line contains multiple block comments' {
$scriptDefinition = @'
if ($a) {
<# first #> <# second #>
# nested
}
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'

Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
Should -Be $scriptDefinition
}

It 'Should preserve continuation indentation before a block-comment-prefixed argument' {
$scriptDefinition = @'
Write-Output `
Expand All @@ -280,6 +293,32 @@ Write-Output `
Should -Be $scriptDefinition
}

It 'Should preserve continuation indentation after multiple inline block comments' {
$scriptDefinition = @'
Write-Output `
<# first #> <# second #>
-InputObject 'value'
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'

Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
Should -Be $scriptDefinition
}

It 'Should preserve indentation with many adjacent block comments' {
$comments = (1..16 | ForEach-Object { "<# comment $_ #>" }) -join ' '
$scriptDefinition = @(
'if ($a) {'
" $comments"
' # nested'
'}'
) -join "`n"
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'

Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
Should -Be $scriptDefinition
}

It 'Should preserve indentation when the first comment is not the least-indented with <Kind> indentation' -TestCases @(
@{ Kind = 'space'; Indentation = ' ' }
@{ Kind = 'tab'; Indentation = "`t" }
Expand Down
Loading