Ejemplo n.º 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));
        }
Ejemplo n.º 2
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));
        }