Skip to content

Commit 9941989

Browse files
Copilotbergmeister
andauthored
Refine comment-run handling
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 7092b4c commit 9941989

2 files changed

Lines changed: 9 additions & 28 deletions

File tree

‎Rules/UseConsistentIndentation.cs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ private static int GetCommentRunIndentationOffset(Token[] tokens, int tokenIndex
349349
precedingIndex--;
350350
if (precedingIndex < 0 ||
351351
tokens[precedingIndex].Kind != TokenKind.Comment ||
352-
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1)
352+
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1 ||
353+
(precedingIndex > 0 && tokens[precedingIndex - 1].Kind != TokenKind.NewLine))
353354
{
354355
break;
355356
}
@@ -706,9 +707,5 @@ private int GetIndentation(int indentationLevel)
706707
return indentationLevel * indentationLevelMultiplier;
707708
}
708709

709-
private string GetIndentationString(int indentationLevel)
710-
{
711-
return new string(indentationChar, GetIndentation(indentationLevel));
712-
}
713710
}
714711
}

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function foo {
204204
}
205205
}
206206

207-
Context "When a multi-line command is given" {
207+
Context "When a commented-out code block is provided" {
208208

209209
It 'Should preserve relative indentation in commented-out code with <Kind> indentation' -TestCases @(
210210
@{ Kind = 'space'; Indentation = ' ' }
@@ -219,17 +219,8 @@ function foo {
219219
"$($Indentation)Write-Output 'after'"
220220
'}'
221221
) -join "`n"
222-
$settings = @{
223-
IncludeRules = @('PSUseConsistentIndentation')
224-
Rules = @{
225-
PSUseConsistentIndentation = @{
226-
Enable = $true
227-
IndentationSize = 4
228-
PipelineIndentation = 'NoIndentation'
229-
Kind = $Kind
230-
}
231-
}
232-
}
222+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
223+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
233224

234225
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
235226
Should -Be $scriptDefinition
@@ -256,22 +247,15 @@ function foo {
256247
"$($Indentation)Write-Output 'after'"
257248
'}'
258249
) -join "`n"
259-
$settings = @{
260-
IncludeRules = @('PSUseConsistentIndentation')
261-
Rules = @{
262-
PSUseConsistentIndentation = @{
263-
Enable = $true
264-
IndentationSize = 4
265-
PipelineIndentation = 'NoIndentation'
266-
Kind = $Kind
267-
}
268-
}
269-
}
250+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
251+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
270252

271253
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
272254
Should -Be $expectedScriptDefinition
273255
}
256+
}
274257

258+
Context "When a multi-line command is given" {
275259
It "When a comment is in the middle of a multi-line statement with preceding and succeeding line continuations" {
276260
$scriptDefinition = @'
277261
foo `

0 commit comments

Comments
 (0)