public static async Task <Document> RefactorAsync(
            Document document,
            ConditionalExpressionSyntax conditionalExpression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax condition = conditionalExpression.Condition;

            ExpressionSyntax newNode = (conditionalExpression.WhenTrue.WalkDownParentheses().IsKind(SyntaxKind.TrueLiteralExpression))
                ? condition
                : LogicalNegationHelper.LogicallyNegate(condition, semanticModel, cancellationToken);

            SyntaxTriviaList trailingTrivia = conditionalExpression
                                              .DescendantTrivia(TextSpan.FromBounds(condition.Span.End, conditionalExpression.Span.End))
                                              .ToSyntaxTriviaList()
                                              .EmptyIfWhitespace()
                                              .AddRange(conditionalExpression.GetTrailingTrivia());

            newNode = newNode
                      .WithLeadingTrivia(conditionalExpression.GetLeadingTrivia())
                      .WithTrailingTrivia(trailingTrivia);

            return(await document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken).ConfigureAwait(false));
        }
コード例 #2
0
        private static ExpressionSyntax CreateNewNode(
            ConditionalExpressionSyntax conditionalExpression,
            ExpressionSyntax newNode)
        {
            SyntaxTriviaList trailingTrivia = conditionalExpression
                                              .DescendantTrivia(TextSpan.FromBounds(conditionalExpression.Condition.Span.End, conditionalExpression.Span.End))
                                              .ToSyntaxTriviaList()
                                              .EmptyIfWhitespace()
                                              .AddRange(conditionalExpression.GetTrailingTrivia());

            return(newNode
                   .WithLeadingTrivia(conditionalExpression.GetLeadingTrivia())
                   .WithTrailingTrivia(trailingTrivia)
                   .WithSimplifierAnnotation());
        }