-
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
Update "use collection initializer" to use a collection-expression if the user prefers that form. #69219
Update "use collection initializer" to use a collection-expression if the user prefers that form. #69219
Conversation
{ | ||
var elements = CreateElements<CollectionElementSyntax>( | ||
objectCreation, matches, | ||
static (match, expression) => match?.UseSpread is true ? SpreadElement(expression) : ExpressionElement(expression)); |
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.
similar behavior to normal collection initializers. however, here we may produce spread elements, not just normal expressions. the 'Match' value tells us if we want that. 'match' is nullable as some of the expressions provided here came from the original object creation expression, and thus have no match data.
…jmabadi/roslyn into useCollectionExpression2
@@ -253,7 +253,7 @@ private static void AddBracketIndentationOperation(List<IndentBlockOperation> li | |||
return; | |||
} | |||
|
|||
if (node.IsKind(SyntaxKind.ListPattern) && node.Parent != null) | |||
if (node.Parent != null && node.Kind() is SyntaxKind.ListPattern or SyntaxKind.CollectionExpression) |
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.
generally following the correspondance that list patterns and collection expressions should be formatted similarly.
=> ImmutableArray.Create(IDEDiagnosticIds.UseCollectionInitializerDiagnosticId); | ||
|
||
protected override bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic) | ||
protected abstract TStatementSyntax GetNewStatement( |
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.
moved thsi abstract method from below to here.
BaseObjectCreationExpressionSyntax objectCreation, | ||
ImmutableArray<Match<StatementSyntax>> matches, | ||
Func<Match<StatementSyntax>?, ExpressionSyntax, TElement> createElement) | ||
where TElement : SyntaxNode |
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 similar cod eto what we had before. Except:
- newline handling is pulled out of this to the callers.
- this is generified so it can make expression lists (for collection initializers) or element lists (for collection expressions).
else | ||
nodesAndTokens.Add(createElement(null, (ExpressionSyntax)nodeOrToken.AsNode()!)); | ||
} | ||
} |
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.
updated as the different features have to be able to customer what sort of lists they get (either expressions or elements)
var list = new List<int> | ||
{ | ||
1 | ||
}; |
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 was actually a bug from before that got fixed. top level statemnets weren't properly wrapping (like what happens in a normal statement).
|
||
return; | ||
|
||
(ImmutableArray<Match<TStatementSyntax>> matches, bool shouldUseCollectionExpression) GetMatches() |
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.
new logic that first tries to see if we can convert to a collection expr. otherwise, falls back to original approach of trying a collection-initializer.
allows us to share the majorty of code and concepts.
Part of #69132
Also expands on the prior form. So if the user has:
This can now be updated to: