Skip to content

Commit cd5a93b

Browse files
author
Omar Bonnet
committed
Fix switch indentation for list patterns
- fixes #72196
1 parent 0a1a7c6 commit cd5a93b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Workspaces/CSharpTest/Formatting/FormattingTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5605,6 +5605,34 @@ public object Method(int i)
56055605
await AssertFormatAsync(expectedCode, code);
56065606
}
56075607

5608+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72196")]
5609+
public async Task FormatSwitchExpression_ListPatternAligned()
5610+
{
5611+
var code = @"class C
5612+
{
5613+
void M()
5614+
{
5615+
_ = Array.Empty<string>() switch
5616+
{
5617+
[] => 0,
5618+
_ => 1,
5619+
};
5620+
}
5621+
}";
5622+
var expectedCode = @"class C
5623+
{
5624+
void M()
5625+
{
5626+
_ = Array.Empty<string>() switch
5627+
{
5628+
[] => 0,
5629+
_ => 1,
5630+
};
5631+
}
5632+
}";
5633+
await AssertFormatAsync(expectedCode, code);
5634+
}
5635+
56085636
[Fact]
56095637
public async Task FormatSwitchWithPropertyPattern()
56105638
{

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/IndentBlockFormattingRule.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ private static void AddBracketIndentationOperation(List<IndentBlockOperation> li
262262
// Brackets in list patterns are formatted like blocks, so align close bracket with open bracket
263263
AddIndentBlockOperation(list, bracketPair.openBracket.GetNextToken(includeZeroWidth: true), bracketPair.closeBracket.GetPreviousToken(includeZeroWidth: true));
264264

265+
if (IsSwitchExpressionPattern(node))
266+
{
267+
return;
268+
}
269+
265270
// If we have:
266271
//
267272
// return Goo([ //<-- determining indentation here.
@@ -279,6 +284,11 @@ private static void AddBracketIndentationOperation(List<IndentBlockOperation> li
279284
}
280285
}
281286

287+
private static bool IsSwitchExpressionPattern(SyntaxNode node)
288+
{
289+
return node.Parent is SwitchExpressionArmSyntax arm && arm.Pattern == node;
290+
}
291+
282292
private static void AddAlignmentBlockOperationRelativeToFirstTokenOnBaseTokenLine(List<IndentBlockOperation> list, (SyntaxToken openBrace, SyntaxToken closeBrace) bracePair)
283293
{
284294
var option = IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine;

0 commit comments

Comments
 (0)