Esempio n. 1
0
        public static Task <Document> RefactorAsync(
            Document document,
            ConditionalExpressionSyntax conditionalExpression,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax condition     = conditionalExpression.Condition;
            ExpressionSyntax whenTrue      = conditionalExpression.WhenTrue;
            ExpressionSyntax whenFalse     = conditionalExpression.WhenFalse;
            SyntaxToken      questionToken = conditionalExpression.QuestionToken;
            SyntaxToken      colonToken    = conditionalExpression.ColonToken;

            var builder = new SyntaxNodeTextBuilder(conditionalExpression);

            builder.AppendLeadingTrivia();
            builder.AppendSpan(condition);

            Write(condition, whenTrue, questionToken, "? ", builder);

            Write(whenTrue, whenFalse, colonToken, ": ", builder);

            builder.AppendTrailingTrivia();

            ExpressionSyntax newNode = SyntaxFactory.ParseExpression(builder.ToString());

            return(document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken));
        }
Esempio n. 2
0
        private static Task <Document> RefactorAsync(
            Document document,
            InterpolatedStringExpressionSyntax interpolatedString,
            CancellationToken cancellationToken)
        {
            //TODO: StringBuilderCache
            var b = new SyntaxNodeTextBuilder(interpolatedString);

            var arguments = new List <ArgumentSyntax>()
            {
                null
            };

            if (interpolatedString.IsVerbatim())
            {
                b.Append("@");
            }

            b.Append("\"");

            int index = 0;

            foreach (InterpolatedStringContentSyntax content in interpolatedString.Contents)
            {
                switch (content.Kind())
                {
                case SyntaxKind.Interpolation:
                {
                    var interpolation = (InterpolationSyntax)content;

                    b.Append("{");
                    b.Append(index.ToString(CultureInfo.InvariantCulture));
                    index++;

                    InterpolationAlignmentClauseSyntax alignmentClause = interpolation.AlignmentClause;
                    if (alignmentClause != null)
                    {
                        b.Append(",");
                        b.AppendSpan(alignmentClause.Value);
                    }

                    InterpolationFormatClauseSyntax formatClause = interpolation.FormatClause;
                    if (formatClause != null)
                    {
                        b.Append(":");
                        b.AppendSpan(formatClause.FormatStringToken);
                    }

                    b.Append("}");

                    arguments.Add(Argument(interpolation.Expression));
                    break;
                }

                case SyntaxKind.InterpolatedStringText:
                {
                    b.AppendSpan(content);
                    break;
                }
                }
            }

            b.Append("\"");

            arguments[0] = Argument(ParseExpression(b.ToString()));

            InvocationExpressionSyntax invocation = SimpleMemberInvocationExpression(
                CSharpTypeFactory.StringType(),
                IdentifierName("Format"),
                ArgumentList(SeparatedList(arguments)));

            invocation = invocation.WithTriviaFrom(interpolatedString).WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(interpolatedString, invocation, cancellationToken));
        }