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

Conversation

obonn1
Copy link
Contributor

@obonn1 obonn1 commented Nov 18, 2024

The indentation of a list pattern or collection expression is (according to pre-existing comment in the adjusted method) "relative to the start of the line the prior token is on if we're on the same line as it." However, the code was missing a check for the two tokens being on the same line.

@obonn1 obonn1 requested a review from a team as a code owner November 18, 2024 06:34
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 18, 2024
@dotnet-policy-service dotnet-policy-service bot added Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode labels Nov 18, 2024
@obonn1 obonn1 marked this pull request as draft November 18, 2024 09:08
@obonn1 obonn1 force-pushed the feature/fix_switch_indentation_for_list branch from d575e61 to 33c85a2 Compare November 20, 2024 09:03
@obonn1 obonn1 force-pushed the feature/fix_switch_indentation_for_list branch from 33c85a2 to cd5a93b Compare November 20, 2024 09:05
@obonn1 obonn1 marked this pull request as ready for review November 20, 2024 15:15
@@ -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.

@@ -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.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

List pattern is not correctly formatted in the first case of a switch expression
2 participants