private static async Task <Document> RefactorAsync(
            Document document,
            InterpolatedStringExpressionSyntax interpolatedString,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            int position = interpolatedString.SpanStart;

            bool isVerbatim = interpolatedString.IsVerbatim();

            ExpressionSyntax newNode = null;

            SyntaxList <InterpolatedStringContentSyntax> contents = interpolatedString.Contents;

            InterpolatedStringContentSyntax content1 = contents[0];
            InterpolatedStringContentSyntax content2 = contents[1];

            if (content1.Kind() == SyntaxKind.InterpolatedStringText)
            {
                ExpressionSyntax expression1 = GetExpression((InterpolatedStringTextSyntax)content1, isVerbatim);
                ExpressionSyntax expression2 = ((InterpolationSyntax)content2).Expression;

                newNode = CreateAddExpression(expression1, expression2, position, semanticModel, cancellationToken, isLeft: false);
            }
            else if (content2.Kind() == SyntaxKind.InterpolatedStringText)
            {
                ExpressionSyntax expression1 = ((InterpolationSyntax)content1).Expression;
                ExpressionSyntax expression2 = GetExpression((InterpolatedStringTextSyntax)content2, isVerbatim);

                newNode = CreateAddExpression(expression1, expression2, position, semanticModel, cancellationToken, isLeft: true);
            }
            else
            {
                ExpressionSyntax expression1 = ((InterpolationSyntax)content1).Expression;
                ExpressionSyntax expression2 = ((InterpolationSyntax)content2).Expression;

                bool isLiteral = expression1 is LiteralExpressionSyntax;

                BinaryExpressionSyntax addExpression = CreateAddExpression(expression1, expression2, position, semanticModel, cancellationToken, isLeft: !isLiteral);

                newNode = CreateAddExpression(addExpression.Left, addExpression.Right, position, semanticModel, cancellationToken, isLeft: isLiteral);
            }

            for (int i = 2; i < contents.Count; i++)
            {
                InterpolatedStringContentSyntax content = contents[i];

                ExpressionSyntax expression = (content.Kind() == SyntaxKind.InterpolatedStringText)
                    ? GetExpression((InterpolatedStringTextSyntax)content, isVerbatim)
                    : ((InterpolationSyntax)content).Expression;

                newNode = CreateAddExpression(newNode, expression, position, semanticModel, cancellationToken, isLeft: false);
            }

            newNode = newNode.Parenthesize().WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(interpolatedString, newNode, cancellationToken).ConfigureAwait(false));
        }
        public static InterpolatedStringContentConversion Create(InterpolatedStringContentSyntax content, bool isVerbatim)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            SyntaxKind kind = content.Kind();

            switch (kind)
            {
            case SyntaxKind.Interpolation:
            {
                var interpolation = (InterpolationSyntax)content;

                InterpolationAlignmentClauseSyntax alignmentClause = interpolation.AlignmentClause;
                InterpolationFormatClauseSyntax    formatClause    = interpolation.FormatClause;

                if (alignmentClause != null ||
                    formatClause != null)
                {
                    var sb = new StringBuilder();
                    sb.Append("\"{0");

                    if (alignmentClause != null)
                    {
                        sb.Append(',');
                        sb.Append(alignmentClause.Value.ToString());
                    }

                    if (formatClause != null)
                    {
                        sb.Append(':');
                        sb.Append(formatClause.FormatStringToken.Text);
                    }

                    sb.Append("}\"");

                    return(new InterpolatedStringContentConversion(kind, "AppendFormat", SeparatedList(new ArgumentSyntax[] { Argument(ParseExpression(sb.ToString())), Argument(interpolation.Expression) })));
                }
                else
                {
                    return(new InterpolatedStringContentConversion(kind, "Append", SingletonSeparatedList(Argument(interpolation.Expression))));
                }
            }

            case SyntaxKind.InterpolatedStringText:
            {
                var interpolatedStringText = (InterpolatedStringTextSyntax)content;

                string text = interpolatedStringText.TextToken.Text;

                text = (isVerbatim)
                            ? "@\"" + text + "\""
                            : "\"" + text + "\"";

                ExpressionSyntax stringLiteral = ParseExpression(text);

                return(new InterpolatedStringContentConversion(kind, "Append", SingletonSeparatedList(Argument(stringLiteral))));
            }

            default:
            {
                throw new ArgumentException("", nameof(content));
            }
            }
        }
        Refactor(InterpolatedStringContentSyntax content, bool isVerbatim)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            SyntaxKind kind = content.Kind();

            switch (kind)
            {
            case SyntaxKind.Interpolation:
            {
                var interpolation = (InterpolationSyntax)content;

                InterpolationAlignmentClauseSyntax alignmentClause = interpolation.AlignmentClause;
                InterpolationFormatClauseSyntax    formatClause    = interpolation.FormatClause;

                if (alignmentClause != null ||
                    formatClause != null)
                {
                    StringBuilder sb = StringBuilderCache.GetInstance();

                    sb.Append("\"{0");

                    if (alignmentClause != null)
                    {
                        sb.Append(',');
                        sb.Append(alignmentClause.Value.ToString());
                    }

                    if (formatClause != null)
                    {
                        sb.Append(':');
                        sb.Append(formatClause.FormatStringToken.Text);
                    }

                    sb.Append("}\"");

                    ExpressionSyntax expression = ParseExpression(StringBuilderCache.GetStringAndFree(sb));

                    return(kind, "AppendFormat", ImmutableArray.Create(Argument(expression), Argument(interpolation.Expression)));
                }
                else
                {
                    return(kind, "Append", ImmutableArray.Create(Argument(interpolation.Expression)));
                }
            }

            case SyntaxKind.InterpolatedStringText:
            {
                var interpolatedStringText = (InterpolatedStringTextSyntax)content;

                string text = interpolatedStringText.TextToken.Text;

                text = StringUtility.ReplaceDoubleBracesWithSingleBrace(text);

                text = (isVerbatim)
                            ? "@\"" + text + "\""
                            : "\"" + text + "\"";

                ExpressionSyntax stringLiteral = ParseExpression(text);

                return(kind, "Append", ImmutableArray.Create(Argument(stringLiteral)));
            }

            default:
            {
                throw new ArgumentException("", nameof(content));
            }
            }
        }
Example #4
0
        public static bool TryCreate(InterpolatedStringContentSyntax content, bool isVerbatim, out InterpolatedStringContentConversion conversion)
        {
            switch (content?.Kind())
            {
            case SyntaxKind.Interpolation:
            {
                var interpolation = (InterpolationSyntax)content;

                InterpolationAlignmentClauseSyntax alignmentClause = interpolation.AlignmentClause;
                InterpolationFormatClauseSyntax    formatClause    = interpolation.FormatClause;

                if (alignmentClause != null ||
                    formatClause != null)
                {
                    var sb = new StringBuilder();
                    sb.Append("\"{0");

                    if (alignmentClause != null)
                    {
                        sb.Append(',');
                        sb.Append(alignmentClause.Value.ToString());
                    }

                    if (formatClause != null)
                    {
                        sb.Append(':');
                        sb.Append(formatClause.FormatStringToken.Text);
                    }

                    sb.Append("}\"");

                    conversion = new InterpolatedStringContentConversion("AppendFormat", SeparatedList(new ArgumentSyntax[] { Argument(ParseExpression(sb.ToString())), Argument(interpolation.Expression) }));
                    return(true);
                }
                else
                {
                    conversion = new InterpolatedStringContentConversion("Append", SingletonSeparatedList(Argument(interpolation.Expression)));
                    return(true);
                }
            }

            case SyntaxKind.InterpolatedStringText:
            {
                var interpolatedStringText = (InterpolatedStringTextSyntax)content;

                string text = interpolatedStringText.TextToken.Text;

                text = (isVerbatim)
                            ? "@\"" + text + "\""
                            : "\"" + text + "\"";

                ExpressionSyntax stringLiteral = ParseExpression(text);

                conversion = new InterpolatedStringContentConversion("Append", SingletonSeparatedList(Argument(stringLiteral)));
                return(true);
            }
            }

            conversion = default(InterpolatedStringContentConversion);
            return(false);
        }