-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
List patterns: main IDE scenarios and AddRequiredParens #53850
Conversation
@@ -27,6 +27,7 @@ internal enum OperatorPrecedence | |||
Multiplicative, | |||
Switch, | |||
Range, | |||
Slice, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slice,
📝 I don't know what is the correct precedence order in this list. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
{ | ||
void M() | ||
{ | ||
_ = this is [ 1, 2, >= 3 ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ = this is [1, 2, >= 3]
#Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
{ | ||
// is [ > 0$$] | ||
return CreateAdjustSpacesOperation(1, AdjustSpacesOption.ForceSpacesIfOnSingleLine); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
definitely not sure about this. #Resolved
@CyrusNajmabadi I've removed the AddParens change from this PR. |
@@ -520,6 +520,39 @@ private static bool NeedsSeparatorForPositionalPattern(SyntaxToken token, Syntax | |||
return false; | |||
} | |||
|
|||
private static bool NeedsSeparatorForListPattern(SyntaxToken token, SyntaxToken next) | |||
{ | |||
ListPatternSyntax? listPattern; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListPatternSyntax? listPattern; | |
ListPatternSyntax listPattern; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var nextIsOpenBracket = next.IsKind(SyntaxKind.OpenBracketToken); | |
if (nextIsOpenBracket) | |
if (next.IsKind(SyntaxKind.OpenBracketToken)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var tokenIsOpenBracket = token.IsKind(SyntaxKind.OpenBracketToken); | |
if (tokenIsOpenBracket) | |
if (token.IsKind(SyntaxKind.OpenBracketToken)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems odd. what sort of expressions are legal here? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants for example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, you can ignore this if you want.
} | ||
if (NeedsSeparatorForListPattern(token, next)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | |
if (NeedsSeparatorForListPattern(token, next)) | |
} | |
if (NeedsSeparatorForListPattern(token, next)) |
i'm surpirsed there was no warning on this.
@@ -436,6 +443,13 @@ public override AbstractFormattingRule WithOptions(AnalyzerConfigOptions options | |||
return CreateAdjustSpacesOperation(1, AdjustSpacesOption.ForceSpacesIfOnSingleLine); | |||
} | |||
|
|||
// Slice pattern: | |||
// ..var x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// ..var x | |
// .. var x |
Basic scenarios (first commit):
More advanced scenarios (second commit):- AddRequiredParens- RemoveUnecessaryParensTest plan #51289
FYI @alrz