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
15 changes: 14 additions & 1 deletion Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,21 @@ private static int GetCommentRunIndentationOffset(Token[] tokens, int tokenIndex
precedingIndex--;
}

int leastIndentedColumn = tokens[firstCommentIndex].Extent.StartColumnNumber;
for (int nextCommentIndex = firstCommentIndex + 2;
nextCommentIndex < tokens.Length &&
tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine &&
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1;
nextCommentIndex += 2)
{
leastIndentedColumn = Math.Min(
leastIndentedColumn,
tokens[nextCommentIndex].Extent.StartColumnNumber);
}

return tokens[tokenIndex].Extent.StartColumnNumber -
tokens[firstCommentIndex].Extent.StartColumnNumber;
leastIndentedColumn;
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ function foo {
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
Should -Be $expectedScriptDefinition
}

It 'Should preserve indentation when the first comment is not the least-indented with <Kind> indentation' -TestCases @(
@{ Kind = 'space'; Indentation = ' ' }
@{ Kind = 'tab'; Indentation = "`t" }
) {
param([string] $Kind, [string] $Indentation)
$scriptDefinition = @(
"$Indentation# inner"
'# outer'
) -join "`n"
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'

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

Context "When a multi-line command is given" {
Expand Down
Loading