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

Preserve formatting when converting array-expressions to collection-expressions. #69278

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.UseCollectionExpression;
Expand Down Expand Up @@ -70,7 +71,7 @@ protected override async Task FixAllAsync(

return;

bool IsOnSingleLine(SyntaxNode node)
static bool IsOnSingleLine(SourceText sourceText, SyntaxNode node)
=> sourceText.AreOnSameLine(node.GetFirstToken(), node.GetLastToken());

void RewriteInitializerExpression(InitializerExpressionSyntax initializer)
Expand All @@ -79,40 +80,103 @@ void RewriteInitializerExpression(InitializerExpressionSyntax initializer)
initializer,
(current, _) => ConvertInitializerToCollectionExpression(
(InitializerExpressionSyntax)current,
IsOnSingleLine(initializer)));
IsOnSingleLine(sourceText, initializer)));
}

bool ShouldReplaceExistingExpressionEntirely(ExpressionSyntax explicitOrImplicitArray, InitializerExpressionSyntax initializer)
{
// Any time we have `{ x, y, z }` in any form, then always just replace the whole original expression
// with `[x, y, z]`.
if (IsOnSingleLine(sourceText, initializer))
return true;

// initializer was on multiple lines, but started on the same line as the 'new' keyword. e.g.:
//
// var v = new[] {
// 1, 2, 3
// };
//
// Just remove the `new...` section entirely, but otherwise keep the initialize multiline:
//
// var v = [
// 1, 2, 3
// ];
var newKeyword = explicitOrImplicitArray.GetFirstToken();
if (sourceText.AreOnSameLine(newKeyword, initializer.OpenBraceToken))
return true;

// Initializer was on multiple lines, and was not on the same line as the 'new' keyword, and the 'new' is on a newline:
//
// var v2 =
// new[]
// {
// 1, 2, 3
// };
//
// For this latter, we want to just remove the new portion and move the collection to subsume it.
var previousToken = newKeyword.GetPreviousToken();
if (previousToken == default)
return true;

if (!sourceText.AreOnSameLine(previousToken, newKeyword))
return true;

// All that is left is:
//
// var v2 = new[]
// {
// 1, 2, 3
// };
//
// For this we want to remove the 'new' portion, but keep the collection on its own line.
return false;
}

void RewriteArrayCreationExpression(ArrayCreationExpressionSyntax arrayCreation)
{
Contract.ThrowIfNull(arrayCreation.Initializer);
var shouldReplaceExpressionEntirely = ShouldReplaceExistingExpressionEntirely(arrayCreation, arrayCreation.Initializer);

editor.ReplaceNode(
arrayCreation,
(current, _) =>
{
var currentArrayCreation = (ArrayCreationExpressionSyntax)current;
Contract.ThrowIfNull(currentArrayCreation.Initializer);

var collectionExpression = ConvertInitializerToCollectionExpression(
currentArrayCreation.Initializer,
IsOnSingleLine(arrayCreation));
IsOnSingleLine(sourceText, arrayCreation.Initializer));

collectionExpression = collectionExpression.WithLeadingTrivia(currentArrayCreation.GetLeadingTrivia());
return collectionExpression;
return shouldReplaceExpressionEntirely
? collectionExpression.WithTriviaFrom(currentArrayCreation)
: collectionExpression
.WithPrependedLeadingTrivia(currentArrayCreation.Type.GetTrailingTrivia())
.WithPrependedLeadingTrivia(ElasticMarker);
});
}

void RewriteImplicitArrayCreationExpression(ImplicitArrayCreationExpressionSyntax implicitArrayCreation)
{
Contract.ThrowIfNull(implicitArrayCreation.Initializer);
var shouldReplaceExpressionEntirely = ShouldReplaceExistingExpressionEntirely(implicitArrayCreation, implicitArrayCreation.Initializer);

editor.ReplaceNode(
implicitArrayCreation,
(current, _) =>
{
var currentArrayCreation = (ImplicitArrayCreationExpressionSyntax)current;
Contract.ThrowIfNull(currentArrayCreation.Initializer);

var collectionExpression = ConvertInitializerToCollectionExpression(
currentArrayCreation.Initializer,
IsOnSingleLine(implicitArrayCreation));
IsOnSingleLine(sourceText, implicitArrayCreation));

collectionExpression = collectionExpression.WithLeadingTrivia(currentArrayCreation.GetLeadingTrivia());
return collectionExpression;
return shouldReplaceExpressionEntirely
? collectionExpression.WithTriviaFrom(currentArrayCreation)
: collectionExpression
.WithPrependedLeadingTrivia(currentArrayCreation.CloseBracketToken.TrailingTrivia)
.WithPrependedLeadingTrivia(ElasticMarker);
});
}
}
Expand Down
Loading