Example #1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax condition,
            ExpressionSyntax expression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var ifStatement = (IfStatementSyntax)condition.Parent;

            IfStatementSyntax newIfStatement = ifStatement.ReplaceNode(
                expression.Parent,
                ExtractExpressionFromConditionRefactoring.GetNewCondition(condition, expression));

            newIfStatement = newIfStatement.WithFormatterAnnotation();

            if (condition.IsKind(SyntaxKind.LogicalAndExpression))
            {
                root = root.ReplaceNode(
                    ifStatement,
                    ExtractExpressionToNestedIf(expression, ifStatement, newIfStatement));
            }
            else if (condition.IsKind(SyntaxKind.LogicalOrExpression))
            {
                root = root.ReplaceNode(
                    ifStatement.Parent,
                    ExtractExpressionToIf(expression, ifStatement, newIfStatement));
            }

            return(document.WithSyntaxRoot(root));
        }
Example #2
0
        private static BinaryExpressionSyntax GetCondition(BinaryExpressionSyntax parent)
        {
            BinaryExpressionSyntax binaryExpression = ExtractExpressionFromConditionRefactoring.GetCondition(parent, SyntaxKind.IfStatement);

            if (binaryExpression != null)
            {
                if (binaryExpression.IsKind(SyntaxKind.LogicalAndExpression) ||
                    binaryExpression.Parent.Parent?.IsKind(SyntaxKind.Block) == true)
                {
                    return(binaryExpression);
                }
            }

            return(null);
        }
Example #3
0
        public static void ComputeRefactoring(RefactoringContext context, ExpressionSyntax expression)
        {
            if (context.Span.IsBetweenSpans(expression))
            {
                SyntaxNode parent = expression.Parent;

                if (parent?.IsKind(SyntaxKind.LogicalAndExpression) == true)
                {
                    BinaryExpressionSyntax binaryExpression = ExtractExpressionFromConditionRefactoring.GetCondition((BinaryExpressionSyntax)parent, SyntaxKind.WhileStatement);

                    if (binaryExpression?.IsKind(SyntaxKind.LogicalAndExpression) == true)
                    {
                        context.RegisterRefactoring(
                            $"Extract '{expression}'",
                            cancellationToken => RefactorAsync(context.Document, binaryExpression, expression, cancellationToken));
                    }
                }
            }
        }
Example #4
0
        public static async Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax condition,
            ExpressionSyntax expression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var whileStatement = (WhileStatementSyntax)condition.Parent;

            WhileStatementSyntax newWhileStatement = whileStatement.ReplaceNode(
                expression.Parent,
                ExtractExpressionFromConditionRefactoring.GetNewCondition(condition, expression));

            newWhileStatement = newWhileStatement.WithFormatterAnnotation();

            root = root.ReplaceNode(
                whileStatement,
                ExtractExpressionToNestedIf(expression, whileStatement, newWhileStatement));

            return(document.WithSyntaxRoot(root));
        }