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.
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
| } | ||
|
|
||
| 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.
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.
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.
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.
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.
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.
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: