Add analyzer/fixer to suggest changing code like ImmutableArray.Create(1, 2, 3) to [1, 2, 3]#69473
Merged
CyrusNajmabadi merged 28 commits intodotnet:mainfrom Aug 11, 2023
Conversation
Closed
14 tasks
CyrusNajmabadi
commented
Aug 10, 2023
src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs
Outdated
Show resolved
Hide resolved
…ollectionExpressionRewriter.cs
CyrusNajmabadi
commented
Aug 10, 2023
| } | ||
|
|
||
| public static ImmutableArray<CollectionExpressionMatch> TryGetMatches( | ||
| public static ImmutableArray<CollectionExpressionMatch<StatementSyntax>> TryGetMatches( |
Contributor
Author
There was a problem hiding this comment.
this type became generic as we don't have statements when converting expressions (like ImmutableArray.Create(1, 2, 3)) over to collection literals.
CyrusNajmabadi
commented
Aug 10, 2023
| /// name="expressionToReplace"/> expression. | ||
| /// </summary> | ||
| public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionAsync<TParentExpression>( | ||
| public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionAsync<TParentExpression, TMatchNode>( |
Contributor
Author
There was a problem hiding this comment.
fallout of making the match type generic.
CyrusNajmabadi
commented
Aug 10, 2023
| var lineContainingPosition = document.Text.Lines.GetLineFromPosition(position); | ||
| var lineText = lineContainingPosition.ToString(); | ||
| var indentation = lineText.ConvertTabToSpace(formattingOptions.TabSize, initialColumn: 0, endPosition: position - lineContainingPosition.Start); | ||
| return indentation.CreateIndentationString(formattingOptions.UseTabs, formattingOptions.TabSize); |
Contributor
Author
There was a problem hiding this comment.
wasn't handling tabs properly before. Will be adding tests on this as well.
CyrusNajmabadi
commented
Aug 10, 2023
| // expression). We then call into our helper which replaces expressions with collection expressions. The reason | ||
| // for the dummy object creation expression is that it serves as an actual node the rewriting code can attach an | ||
| // initializer to, by which it can figure out appropriate wrapping and indentation for the collection expression | ||
| // elements. |
Contributor
Author
There was a problem hiding this comment.
a really funky way to do things. but it allows us to leverage as much rewriting code as possible.
CyrusNajmabadi
commented
Aug 10, 2023
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/FormattingExtensions.cs
Outdated
Show resolved
Hide resolved
…atting/FormattingExtensions.cs
…/CyrusNajmabadi/roslyn into useCollectionExpressionForCreate
Contributor
Author
|
@akhera99 this is ready for review. thanks! |
akhera99
approved these changes
Aug 11, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #69132
Followup to #69452