Example #1
0
        private static Task <Document> AddNewLineAfterConditionalOperatorInsteadOfBeforeItAsync(
            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;

            ExpressionSyntax newCondition     = condition;
            ExpressionSyntax newWhenTrue      = whenTrue;
            ExpressionSyntax newWhenFalse     = whenFalse;
            SyntaxToken      newQuestionToken = questionToken;
            SyntaxToken      newColonToken    = colonToken;

            if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(condition, questionToken, whenTrue))
            {
                var(left, token, right) = CodeFixHelpers.AddNewLineAfterTokenInsteadOfBeforeIt(condition, questionToken, whenTrue);

                newCondition     = left;
                newQuestionToken = token;
                newWhenTrue      = right;
            }

            if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(whenTrue, colonToken, whenFalse))
            {
                var(left, token, right) = CodeFixHelpers.AddNewLineAfterTokenInsteadOfBeforeIt(newWhenTrue, colonToken, whenFalse);

                newWhenTrue   = left;
                newColonToken = token;
                newWhenFalse  = right;
            }

            ConditionalExpressionSyntax newConditionalExpression = ConditionalExpression(
                newCondition,
                newQuestionToken,
                newWhenTrue,
                newColonToken,
                newWhenFalse);

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