Exemplo n.º 1
0
        static InvocationExpressionSyntax AddArgument(InvocationExpressionSyntax invocationExpression, IParameterSymbol parameterToAdd, bool isNextInSequence)
        {
            ExpressionSyntax defaultValue;

            if (parameterToAdd.HasExplicitDefaultValue)
            {
                defaultValue = ComputeConstantValueCodeRefactoringProvider.GetLiteralExpression(parameterToAdd.ExplicitDefaultValue);
            }
            else
            {
                return(invocationExpression);
            }
            ArgumentSyntax newArgument = SyntaxFactory.Argument(defaultValue);

            if (invocationExpression.ArgumentList.Arguments.Any(argument => argument.NameColon != null) || !isNextInSequence)
            {
                newArgument = newArgument.WithNameColon(SyntaxFactory.NameColon(parameterToAdd.Name));
            }

            var newArguments = invocationExpression.ArgumentList.AddArguments(newArgument);

            return(invocationExpression.WithArgumentList(newArguments));
        }
Exemplo n.º 2
0
        bool ExtractExpression(StatementSyntax statement, out ExpressionSyntax leftSide, out ExpressionSyntax rightSide)
        {
            leftSide  = null;
            rightSide = null;

            if (statement == null || statement is EmptyStatementSyntax)
            {
                return(true);
            }

            switch (statement.Kind())
            {
            case SyntaxKind.ExpressionStatement:
                var expression = ((ExpressionStatementSyntax)statement).Expression;
                switch (expression?.Kind())
                {
                case SyntaxKind.AddAssignmentExpression:
                    leftSide  = ((AssignmentExpressionSyntax)expression).Left;
                    rightSide = ((AssignmentExpressionSyntax)expression).Right;
                    return(true);

                case SyntaxKind.SubtractAssignmentExpression:
                    leftSide  = ((AssignmentExpressionSyntax)expression).Left;
                    rightSide = SyntaxFactory.PrefixUnaryExpression(SyntaxKind.UnaryMinusExpression, ((AssignmentExpressionSyntax)expression).Right);
                    return(true);

                case SyntaxKind.PostDecrementExpression:
                case SyntaxKind.PreDecrementExpression:
                    leftSide  = expression.ExtractUnaryOperand();
                    rightSide = ComputeConstantValueCodeRefactoringProvider.GetLiteralExpression(-1);
                    return(true);

                case SyntaxKind.PostIncrementExpression:
                case SyntaxKind.PreIncrementExpression:
                    leftSide  = expression.ExtractUnaryOperand();
                    rightSide = ComputeConstantValueCodeRefactoringProvider.GetLiteralExpression(1);
                    return(true);
                }
                return(false);

            case SyntaxKind.Block:
                var block = (BlockSyntax)statement;
                foreach (StatementSyntax child in block.Statements)
                {
                    ExpressionSyntax newLeft, newRight;
                    if (!ExtractExpression(child, out newLeft, out newRight))
                    {
                        return(false);
                    }

                    if (newLeft == null)
                    {
                        continue;
                    }

                    if (leftSide == null)
                    {
                        leftSide  = newLeft;
                        rightSide = newRight;
                    }
                    else if (leftSide.IsEquivalentTo(newLeft))
                    {
                        rightSide = SyntaxFactory.BinaryExpression(
                            SyntaxKind.AddExpression,
                            CSharpUtil.AddParensIfRequired(rightSide),
                            CSharpUtil.AddParensIfRequired(newRight)
                            );
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(true);

            case SyntaxKind.IfStatement:
                var condition = (IfStatementSyntax)statement;
                ExpressionSyntax ifLeft, ifRight;
                if (!ExtractExpression(condition.Statement, out ifLeft, out ifRight))
                {
                    return(false);
                }

                ExpressionSyntax elseLeft, elseRight;
                if (!ExtractExpression(condition.Else?.Statement, out elseLeft, out elseRight))
                {
                    return(false);
                }

                if (ifLeft == null && elseLeft == null)
                {
                    return(true);
                }
                if (ifLeft != null && elseLeft != null && !ifLeft.IsEquivalentTo(elseLeft))
                {
                    return(false);
                }

                ifRight   = ifRight ?? ComputeConstantValueCodeRefactoringProvider.GetLiteralExpression(0);
                elseRight = elseRight ?? ComputeConstantValueCodeRefactoringProvider.GetLiteralExpression(0);

                leftSide  = ifLeft ?? elseLeft;
                rightSide = SyntaxFactory.ConditionalExpression(condition.Condition, ifRight, elseRight);
                return(true);
            }
            return(false);
        }