List patterns: main IDE scenarios and AddRequiredParens#53850
List patterns: main IDE scenarios and AddRequiredParens#53850jcouv merged 15 commits intodotnet:features/list-patternsfrom
Conversation
There was a problem hiding this comment.
Slice,
📝 I don't know what is the correct precedence order in this list. #Resolved
There was a problem hiding this comment.
it feels like this shoudl be higher up right? i.e. if i have .. y or z then it's parsed a .. (y or z) not (..y) or z. So .. has lower precedence than than all the binaries. so i would expect this to at least be above ConditionalOr. We should probably note that .. as a range and .. as a slice are very different wrt precedence.
...paces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/SpacingFormattingRule.cs
Outdated
Show resolved
Hide resolved
...rp/Analyzers/AddRequiredParentheses/CSharpAddRequiredPatternParenthesesDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
f05a6c5 to
dc68306
Compare
There was a problem hiding this comment.
_ = this is [1, 2, >= 3] #Resolved
There was a problem hiding this comment.
i think this will be like parens. and we'll want to have no spaces around them. similar to foo[1, 2, 3] as well.
...UtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
definitely not sure about this. #Resolved
|
@CyrusNajmabadi I've removed the AddParens change from this PR. |
|
|
||
| private static bool NeedsSeparatorForListPattern(SyntaxToken token, SyntaxToken next) | ||
| { | ||
| ListPatternSyntax? listPattern; |
There was a problem hiding this comment.
| ListPatternSyntax? listPattern; | |
| ListPatternSyntax listPattern; |
There was a problem hiding this comment.
@jcouv your edit to this comment was breaking markdown syntax so I reverted it
| var nextIsOpenBracket = next.IsKind(SyntaxKind.OpenBracketToken); | ||
| if (nextIsOpenBracket) |
There was a problem hiding this comment.
| var nextIsOpenBracket = next.IsKind(SyntaxKind.OpenBracketToken); | |
| if (nextIsOpenBracket) | |
| if (next.IsKind(SyntaxKind.OpenBracketToken)) |
There was a problem hiding this comment.
@jcouv your edit to this comment was breaking markdown syntax so I reverted it
| var tokenIsOpenBracket = token.IsKind(SyntaxKind.OpenBracketToken); | ||
| if (tokenIsOpenBracket) |
There was a problem hiding this comment.
| var tokenIsOpenBracket = token.IsKind(SyntaxKind.OpenBracketToken); | |
| if (tokenIsOpenBracket) | |
| if (token.IsKind(SyntaxKind.OpenBracketToken)) |
There was a problem hiding this comment.
@jcouv your edit to this comment was breaking markdown syntax so I reverted it
| { | ||
| void M() | ||
| { | ||
| _ = this is [0, .. var rest]; |
There was a problem hiding this comment.
add test where ..var is the starting state, to ensure a space is added. #Resolved
| if (token.IsKind(SyntaxKind.DotDotToken) | ||
| && token.Parent.IsKind(SyntaxKind.SlicePattern)) | ||
| { | ||
| return true; |
There was a problem hiding this comment.
this seems odd. what sort of expressions are legal here? #Resolved
There was a problem hiding this comment.
how woudl that work? .. SomeConstant. How would that work? you've got a list there, so how would that list match against a constant?
Put another way, this is not a question if this is legal. It's a question of if the user has a genuine need to be able to write this. If not, then i would say: this is not an expr context as we don't want expr completion items to clutter things up here.
There was a problem hiding this comment.
The slice method can return any type. The type could be int (then you can apply a constant pattern to it) or int[] (then you can apply a constant null pattern to it).
Those are technically correct, but not really useful... I'm okay to remove if you think that's better.
There was a problem hiding this comment.
yes. please remove for now. i'm highly doubtful this will be an issue in practice. we can add back in the future if someone has a genuinely reasonable case they're running into (and in that case, i think we'd likely do a semantic check for them, rather than muddy teh completion space for everyone else).
It's not crashing ;-P In reply to: 947125569 |
Spent some time trying to figure out indentation when a newline is introduced. Couldn't figure it out. In reply to: 947146285 |
|
Yup. t hat's fine. In reply to: 947151263 |
|
Never mind. I was testing stale bits... |
| ListPatternSyntax listPattern; | ||
| if (token.Parent.IsKind(SyntaxKind.ListPattern)) | ||
| { | ||
| listPattern = (ListPatternSyntax)token.Parent; | ||
| } | ||
| else if (next.Parent.IsKind(SyntaxKind.ListPattern)) | ||
| { | ||
| listPattern = (ListPatternSyntax)next.Parent; | ||
| } | ||
| else | ||
| { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
| ListPatternSyntax listPattern; | |
| if (token.Parent.IsKind(SyntaxKind.ListPattern)) | |
| { | |
| listPattern = (ListPatternSyntax)token.Parent; | |
| } | |
| else if (next.Parent.IsKind(SyntaxKind.ListPattern)) | |
| { | |
| listPattern = (ListPatternSyntax)next.Parent; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| var listPattern = token.Parent as ListPatternSyntax ?? next.Parent as ListPatternSyntax; | |
| if (listPattern == null) | |
| return false; |
There was a problem hiding this comment.
nit, you can ignore this if you want.
| } | ||
| if (NeedsSeparatorForListPattern(token, next)) |
There was a problem hiding this comment.
| } | |
| if (NeedsSeparatorForListPattern(token, next)) | |
| } | |
| if (NeedsSeparatorForListPattern(token, next)) |
i'm surpirsed there was no warning on this.
| } | ||
|
|
||
| // Slice pattern: | ||
| // ..var x |
There was a problem hiding this comment.
| // ..var x | |
| // .. var x |
Basic scenarios (first commit):
More advanced scenarios (second commit):- AddRequiredParens- RemoveUnecessaryParensTest plan #51289
FYI @alrz