예제 #1
0
        private static void Write(
            ExpressionSyntax expression,
            ExpressionSyntax nextExpression,
            SyntaxToken token,
            string newText,
            SyntaxNodeTextBuilder builder)
        {
            if (FormatConditionalExpressionAnalyzer.IsFixable(expression, token))
            {
                if (!expression.GetTrailingTrivia().IsEmptyOrWhitespace() ||
                    !token.LeadingTrivia.IsEmptyOrWhitespace())
                {
                    builder.AppendTrailingTrivia(expression);
                    builder.AppendLeadingTrivia(token);
                }

                builder.AppendTrailingTrivia(token);
                builder.AppendLeadingTrivia(nextExpression);
                builder.Append(newText);
                builder.AppendSpan(nextExpression);
            }
            else
            {
                builder.AppendTrailingTrivia(expression);
                builder.AppendFullSpan(token);
                builder.AppendLeadingTriviaAndSpan(nextExpression);
            }
        }
        private static void Write(
            ExpressionSyntax expression,
            ExpressionSyntax nextExpression,
            SyntaxToken token,
            string newText,
            SyntaxNodeTextBuilder builder)
        {
            if (IsFixable(expression, token))
            {
                if (expression.GetTrailingTrivia().Any(f => !f.IsWhitespaceOrEndOfLineTrivia()) ||
                    token.LeadingTrivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    builder.AppendTrailingTrivia(expression);
                    builder.AppendLeadingTrivia(token);
                }

                builder.AppendTrailingTrivia(token);
                builder.AppendLeadingTrivia(nextExpression);
                builder.Append(newText);
                builder.AppendSpan(nextExpression);
            }
            else
            {
                builder.AppendTrailingTrivia(expression);
                builder.AppendFullSpan(token);
                builder.AppendLeadingTriviaAndSpan(nextExpression);
            }
        }
예제 #3
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;

            StringBuilder sb = StringBuilderCache.GetInstance();

            var builder = new SyntaxNodeTextBuilder(conditionalExpression, sb);

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

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

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

            builder.AppendTrailingTrivia();

            ExpressionSyntax newNode = SyntaxFactory.ParseExpression(StringBuilderCache.GetStringAndFree(sb));

            return(document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken));
        }