Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix switch indentation for list patterns #75953

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5605,6 +5605,34 @@ public object Method(int i)
await AssertFormatAsync(expectedCode, code);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72196")]
public async Task FormatSwitchExpression_ListPatternAligned()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please use 'raw string literals' for test cases. There is a code refactoring to fix this up btw.

var code = @"class C
{
void M()
{
_ = Array.Empty<string>() switch
{
[] => 0,
_ => 1,
};
}
}";
var expectedCode = @"class C
{
void M()
{
_ = Array.Empty<string>() switch
{
[] => 0,
_ => 1,
};
}
}";
await AssertFormatAsync(expectedCode, code);
}

[Fact]
public async Task FormatSwitchWithPropertyPattern()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ private static void AddBracketIndentationOperation(List<IndentBlockOperation> li
// Brackets in list patterns are formatted like blocks, so align close bracket with open bracket
AddIndentBlockOperation(list, bracketPair.openBracket.GetNextToken(includeZeroWidth: true), bracketPair.closeBracket.GetPreviousToken(includeZeroWidth: true));

if (IsSwitchExpressionPattern(node))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a 'why' explanation as to why we don't want to execute teh rest of the code if we hit such a thing.

{
return;
}

// If we have:
//
// return Goo([ //<-- determining indentation here.
Expand All @@ -279,6 +284,11 @@ private static void AddBracketIndentationOperation(List<IndentBlockOperation> li
}
}

private static bool IsSwitchExpressionPattern(SyntaxNode node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i would personally inline this. It's only called in one place, and has very simple logic. If you do want to keep this as a helper method, that's ok.

--

"nits" are ideas/gentle-suggestions. But i'm deferring to your judgement here on what you prefer.

{
return node.Parent is SwitchExpressionArmSyntax arm && arm.Pattern == node;
}

private static void AddAlignmentBlockOperationRelativeToFirstTokenOnBaseTokenLine(List<IndentBlockOperation> list, (SyntaxToken openBrace, SyntaxToken closeBrace) bracePair)
{
var option = IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine;
Expand Down
Loading