Skip to content

Commit 810f339

Browse files
Copilotbergmeister
andauthored
Preserve branch indentation after multiline conditions (#2271)
* Initial plan * Preserve close brace indentation for branches 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>
1 parent 17e46ca commit 810f339

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎Rules/PlaceCloseBrace.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldHaveNewLineAfter(
277277
var corrections = new List<CorrectionExtent>();
278278
var nextToken = tokens[closeBracePos + 1];
279279
var closeBraceToken = tokens[closeBracePos];
280+
var indentation = tokens[closeBracePos - 1].Kind == TokenKind.NewLine
281+
? new String(' ', closeBraceToken.Extent.StartColumnNumber - 1)
282+
: GetIndentation(tokens, closeBracePos, openBracePos);
280283

281284
// Also remove the whitespace between the close brace and the next token
282285
// and indent the next token to the same level as the close brace
@@ -285,7 +288,7 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldHaveNewLineAfter(
285288
nextToken.Extent.StartLineNumber,
286289
closeBraceToken.Extent.StartColumnNumber,
287290
nextToken.Extent.StartColumnNumber,
288-
closeBraceToken.Text + Environment.NewLine + GetIndentation(tokens, closeBracePos, openBracePos),
291+
closeBraceToken.Text + Environment.NewLine + indentation,
289292
fileName));
290293
return corrections;
291294
}

‎Tests/Rules/PlaceCloseBrace.tests.ps1‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,52 @@ if (Test-Path "blah") {
180180
Test-CorrectionExtentFromContent @params
181181
}
182182

183+
It "Should preserve close brace indentation after a multiline if condition" {
184+
$def = @'
185+
if (
186+
$true) {
187+
$true
188+
} else {
189+
$false
190+
}
191+
'@
192+
$expected = @'
193+
if (
194+
$true) {
195+
$true
196+
}
197+
else {
198+
$false
199+
}
200+
'@
201+
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected
202+
}
203+
204+
It "Should preserve nested close brace indentation after a multiline if condition" {
205+
$def = @'
206+
function Test {
207+
if (
208+
$true) {
209+
$true
210+
} else {
211+
$false
212+
}
213+
}
214+
'@
215+
$expected = @'
216+
function Test {
217+
if (
218+
$true) {
219+
$true
220+
}
221+
else {
222+
$false
223+
}
224+
}
225+
'@
226+
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected
227+
}
228+
183229
It "Should find a violation for a close brace followed by elseif" {
184230
$def = @'
185231
if (Test-Path "blah") {

0 commit comments

Comments
 (0)