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));
        }
Example #2
0
        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));
        }