Skip to content

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
CyrusNajmabadi:useCollectionExpressionForCreate
Aug 11, 2023
Merged

Add analyzer/fixer to suggest changing code like ImmutableArray.Create(1, 2, 3) to [1, 2, 3]#69473
CyrusNajmabadi merged 28 commits intodotnet:mainfrom
CyrusNajmabadi:useCollectionExpressionForCreate

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@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
@ghost ghost added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 10, 2023
}

public static ImmutableArray<CollectionExpressionMatch> TryGetMatches(
public static ImmutableArray<CollectionExpressionMatch<StatementSyntax>> TryGetMatches(
Copy link
Copy Markdown
Contributor 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.

/// name="expressionToReplace"/> expression.
/// </summary>
public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionAsync<TParentExpression>(
public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionAsync<TParentExpression, TMatchNode>(
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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