-
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
Add analyzer/fixer to suggest changing code like ImmutableArray.Create(1, 2, 3)
to [1, 2, 3]
#69473
Add analyzer/fixer to suggest changing code like ImmutableArray.Create(1, 2, 3)
to [1, 2, 3]
#69473
Conversation
src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs
Outdated
Show resolved
Hide resolved
…ollectionExpressionRewriter.cs
@@ -91,7 +91,7 @@ private static void AnalyzeArrayCreationExpression(SyntaxNodeAnalysisContext con | |||
ReportArrayCreationDiagnostics(context, syntaxTree, option, arrayCreationExpression); | |||
} | |||
|
|||
public static ImmutableArray<CollectionExpressionMatch> TryGetMatches( | |||
public static ImmutableArray<CollectionExpressionMatch<StatementSyntax>> TryGetMatches( |
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 type became generic as we don't have statements when converting expressions (like ImmutableArray.Create(1, 2, 3)
) over to collection literals.
@@ -27,15 +27,16 @@ internal static class CSharpCollectionExpressionRewriter | |||
/// Creates the final collection-expression <c>[...]</c> that will replace the given <paramref | |||
/// name="expressionToReplace"/> expression. | |||
/// </summary> | |||
public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionAsync<TParentExpression>( | |||
public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionAsync<TParentExpression, TMatchNode>( |
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.
fallout of making the match type generic.
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); |
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.
wasn't handling tabs properly before. Will be adding tests on this as well.
// 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. |
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.
a really funky way to do things. but it allows us to leverage as much rewriting code as possible.
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/FormattingExtensions.cs
Outdated
Show resolved
Hide resolved
…atting/FormattingExtensions.cs
…/CyrusNajmabadi/roslyn into useCollectionExpressionForCreate
@akhera99 this is ready for review. thanks! |
Part of #69132
Followup to #69452