public static async Task <Document> RefactorAsync( Document document, InitializerExpressionSyntax initializer, CancellationToken cancellationToken) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; ImplicitElementAccessSyntax implicitElementAccess = ImplicitElementAccess( BracketedArgumentList( OpenBracketToken().WithTriviaFrom(initializer.OpenBraceToken), SingletonSeparatedList(Argument(expressions[0]).WithFormatterAnnotation()), CloseBracketToken())); AssignmentExpressionSyntax assignment = SimpleAssignmentExpression( implicitElementAccess, EqualsToken().WithTriviaFrom(initializer.ChildTokens().FirstOrDefault()), expressions[1] .AppendTrailingTrivia(initializer.CloseBraceToken.GetLeadingAndTrailingTrivia()) .WithFormatterAnnotation()); SyntaxNode newRoot = root.ReplaceNode(initializer, assignment); return(document.WithSyntaxRoot(newRoot)); }
private static AssignmentExpressionSyntax CreateNewExpression(InitializerExpressionSyntax initializer) { SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; SyntaxToken openBracket = Token( initializer.OpenBraceToken.LeadingTrivia, SyntaxKind.OpenBracketToken, initializer.OpenBraceToken.TrailingTrivia.EmptyIfWhitespace()); ImplicitElementAccessSyntax implicitElementAccess = ImplicitElementAccess( BracketedArgumentList( openBracket, SingletonSeparatedList(Argument(expressions[0].TrimTrivia())), CloseBracketToken())); SyntaxToken comma = initializer.ChildTokens().FirstOrDefault(f => f.IsKind(SyntaxKind.CommaToken)); SyntaxTriviaList commaLeading = comma.LeadingTrivia; SyntaxToken equalsToken = Token( (commaLeading.Any()) ? commaLeading : TriviaList(Space), SyntaxKind.EqualsToken, comma.TrailingTrivia); ExpressionSyntax valueExpression = expressions[1]; valueExpression = valueExpression.AppendToTrailingTrivia(initializer.CloseBraceToken.LeadingTrivia.EmptyIfWhitespace()); return(SimpleAssignmentExpression(implicitElementAccess, equalsToken, valueExpression) .WithTriviaFrom(initializer)); }
public static Task <Document> RefactorAsync( Document document, InitializerExpressionSyntax initializer, CancellationToken cancellationToken) { SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; SyntaxTriviaList openBraceTrailing = initializer.OpenBraceToken.TrailingTrivia; SyntaxTriviaList closeBraceLeading = initializer.CloseBraceToken.LeadingTrivia; SyntaxToken openBracket = Token( initializer.OpenBraceToken.LeadingTrivia, SyntaxKind.OpenBracketToken, (openBraceTrailing.All(f => f.IsWhitespaceTrivia())) ? default(SyntaxTriviaList) : openBraceTrailing); ImplicitElementAccessSyntax implicitElementAccess = ImplicitElementAccess( BracketedArgumentList( openBracket, SingletonSeparatedList(Argument(expressions[0])), CloseBracketToken())); SyntaxToken comma = initializer.ChildTokens().FirstOrDefault(f => f.IsKind(SyntaxKind.CommaToken)); SyntaxTriviaList commaLeading = comma.LeadingTrivia; SyntaxToken equalsToken = Token( (commaLeading.Any()) ? commaLeading : TriviaList(Space), SyntaxKind.EqualsToken, comma.TrailingTrivia); ExpressionSyntax valueExpression = expressions[1]; if (closeBraceLeading.Any(f => !f.IsWhitespaceTrivia())) { valueExpression = valueExpression.AppendToTrailingTrivia(closeBraceLeading); } AssignmentExpressionSyntax assignment = SimpleAssignmentExpression(implicitElementAccess, equalsToken, valueExpression) .WithTriviaFrom(initializer); return(document.ReplaceNodeAsync(initializer, assignment, cancellationToken)); }