Skip to content
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

Merged

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Aug 10, 2023

Part of #69132

Followup to #69452

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner August 10, 2023 23:02
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 10, 2023
@@ -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(
Copy link
Member Author

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>(
Copy link
Member Author

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);
Copy link
Member Author

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.
Copy link
Member Author

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.

@CyrusNajmabadi
Copy link
Member Author

@akhera99 this is ready for review. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants