public static async Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            IfStatementSyntax nestedIf = GetNestedIfStatement(ifStatement);

            BinaryExpressionSyntax newCondition = CSharpFactory.LogicalAndExpression(
                ifStatement.Condition,
                nestedIf.Condition,
                addParenthesesIfNecessary: true);

            IfStatementSyntax newNode = GetNewIfStatement(ifStatement, nestedIf)
                                        .WithCondition(newCondition)
                                        .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(ifStatement, newNode, cancellationToken).ConfigureAwait(false));
        }
Exemple #2
0
        public static Task <Document> RefactorAsync(
            Document document,
            InvocationExpressionSyntax invocationExpression,
            CancellationToken cancellationToken)
        {
            MemberInvocationExpressionWithSingleParameter invocation  = MemberInvocationExpressionWithSingleParameter.Create(invocationExpression);
            MemberInvocationExpressionWithSingleParameter invocation2 = MemberInvocationExpressionWithSingleParameter.Create((InvocationExpressionSyntax)invocation.Expression);

            LambdaExpressionWithSingleParameter lambda  = LambdaExpressionWithSingleParameter.Create((LambdaExpressionSyntax)invocation.Argument.Expression);
            LambdaExpressionWithSingleParameter lambda2 = LambdaExpressionWithSingleParameter.Create((LambdaExpressionSyntax)invocation2.Argument.Expression);

            BinaryExpressionSyntax logicalAnd = CSharpFactory.LogicalAndExpression(
                ((ExpressionSyntax)lambda2.Body).Parenthesize(moveTrivia: true).WithSimplifierAnnotation(),
                ((ExpressionSyntax)lambda.Body).Parenthesize(moveTrivia: true).WithSimplifierAnnotation());

            InvocationExpressionSyntax newNode = invocation2.InvocationExpression
                                                 .ReplaceNode(invocation2.Name, invocation.Name.WithTriviaFrom(invocation2.Name))
                                                 .WithArgumentList(invocation2.ArgumentList.ReplaceNode((ExpressionSyntax)lambda2.Body, logicalAnd));

            return(document.ReplaceNodeAsync(invocationExpression, newNode, cancellationToken));
        }
Exemple #3
0
        public static Task <Document> RefactorAsync(
            Document document,
            InvocationExpressionSyntax invocationExpression,
            CancellationToken cancellationToken)
        {
            MemberInvocationExpressionInfo invocationInfo  = SyntaxInfo.MemberInvocationExpressionInfo(invocationExpression);
            MemberInvocationExpressionInfo invocationInfo2 = SyntaxInfo.MemberInvocationExpressionInfo(invocationInfo.Expression);

            SingleParameterLambdaExpressionInfo lambda  = SyntaxInfo.SingleParameterLambdaExpressionInfo((LambdaExpressionSyntax)invocationInfo.Arguments.First().Expression);
            SingleParameterLambdaExpressionInfo lambda2 = SyntaxInfo.SingleParameterLambdaExpressionInfo((LambdaExpressionSyntax)invocationInfo2.Arguments.First().Expression);

            BinaryExpressionSyntax logicalAnd = CSharpFactory.LogicalAndExpression(
                ((ExpressionSyntax)lambda2.Body).Parenthesize(),
                ((ExpressionSyntax)lambda.Body).Parenthesize());

            InvocationExpressionSyntax newNode = invocationInfo2.InvocationExpression
                                                 .ReplaceNode(invocationInfo2.Name, invocationInfo.Name.WithTriviaFrom(invocationInfo2.Name))
                                                 .WithArgumentList(invocationInfo2.ArgumentList.ReplaceNode((ExpressionSyntax)lambda2.Body, logicalAnd));

            return(document.ReplaceNodeAsync(invocationExpression, newNode, cancellationToken));
        }
        private static async Task <Document> MergeIfStatementWithNestedIfStatementAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            IfStatementSyntax nestedIf = GetNestedIfStatement(ifStatement);

            BinaryExpressionSyntax newCondition = CSharpFactory.LogicalAndExpression(
                ifStatement.Condition,
                nestedIf.Condition,
                addParenthesesIfNecessary: true);

            IfStatementSyntax newNode = GetNewIfStatement(ifStatement, nestedIf)
                                        .WithCondition(newCondition)
                                        .WithFormatterAnnotation();

            SyntaxNode newRoot = oldRoot.ReplaceNode(ifStatement, newNode);

            return(document.WithSyntaxRoot(newRoot));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            IfStatementSyntax nestedIf = GetNestedIfStatement(ifStatement);

            ExpressionSyntax left  = ifStatement.Condition.Parenthesize();
            ExpressionSyntax right = nestedIf.Condition;

            if (!right.IsKind(SyntaxKind.LogicalAndExpression))
            {
                right = right.Parenthesize();
            }

            BinaryExpressionSyntax newCondition = CSharpFactory.LogicalAndExpression(left, right);

            IfStatementSyntax newNode = GetNewIfStatement(ifStatement, nestedIf)
                                        .WithCondition(newCondition)
                                        .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(ifStatement, newNode, cancellationToken));
        }
 private static ExpressionSyntax LogicalAndExpression(ExpressionSyntax left, ExpressionSyntax right)
 {
     return(CSharpFactory.LogicalAndExpression(
                left.Parenthesize(),
                right.Parenthesize()));
 }
Exemple #7
0
        private static ExpressionSyntax GetBooleanExpression(
            ExpressionSyntax condition,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            switch (expression1.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(expression2);

                case SyntaxKind.FalseLiteralExpression:
                    return(condition);

                default:
                    return(LogicalOrExpression(condition, expression2));
                }
            }

            case SyntaxKind.FalseLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(Negator.LogicallyNegate(condition, semanticModel, cancellationToken));

                case SyntaxKind.FalseLiteralExpression:
                    return(expression2);

                default:
                    return(LogicalAndExpression(Negator.LogicallyNegate(condition, semanticModel, cancellationToken), expression2));
                }
            }

            default:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(LogicalOrExpression(Negator.LogicallyNegate(condition, semanticModel, cancellationToken), expression1));

                case SyntaxKind.FalseLiteralExpression:
                    return(LogicalAndExpression(condition, expression1));

                default:
                    throw new InvalidOperationException();
                }
            }
            }

            BinaryExpressionSyntax LogicalAndExpression(ExpressionSyntax left, ExpressionSyntax right)
            {
                return(CSharpFactory.LogicalAndExpression(
                           left.Parenthesize(),
                           right.Parenthesize()));
            }

            BinaryExpressionSyntax LogicalOrExpression(ExpressionSyntax left, ExpressionSyntax right)
            {
                return(CSharpFactory.LogicalOrExpression(
                           left.Parenthesize(),
                           right.Parenthesize()));
            }
        }
Exemple #8
0
 private static ExpressionSyntax LogicalAndExpression(ExpressionSyntax left, ExpressionSyntax right)
 {
     return(CSharpFactory.LogicalAndExpression(
                left.Parenthesize(moveTrivia: true).WithSimplifierAnnotation(),
                right.Parenthesize(moveTrivia: true).WithSimplifierAnnotation()));
 }