コード例 #1
0
        protected TStatement AddNestedIf(
            TStatement statement,
            SelectedExpressions selectedExpressions)
        {
            ExpressionSyntax expression = ParseExpression(selectedExpressions.ExpressionsText);

            return(AddNestedIf(statement, expression));
        }
コード例 #2
0
        internal static void ComputeRefactoring(RefactoringContext context, SelectedExpressions selectedExpressions)
        {
            BinaryExpressionSyntax binaryExpression = selectedExpressions.BinaryExpression;

            SyntaxKind kind = binaryExpression.Kind();

            if (kind == SyntaxKind.LogicalAndExpression ||
                kind == SyntaxKind.LogicalOrExpression)
            {
                BinaryExpressionSyntax condition = GetCondition(binaryExpression);

                if (condition != null)
                {
                    SyntaxNode parent = condition.Parent;

                    switch (parent?.Kind())
                    {
                    case SyntaxKind.IfStatement:
                    {
                        if (kind == SyntaxKind.LogicalAndExpression)
                        {
                            var refactoring = new ExtractConditionFromIfToNestedIfRefactoring();
                            context.RegisterRefactoring(
                                refactoring.Title,
                                cancellationToken => refactoring.RefactorAsync(context.Document, (IfStatementSyntax)parent, condition, selectedExpressions, cancellationToken));
                        }
                        else if (kind == SyntaxKind.LogicalOrExpression)
                        {
                            StatementContainer container = GetStatementContainer(parent);
                            if (container != null)
                            {
                                var refactoring = new ExtractConditionFromIfToIfRefactoring();
                                context.RegisterRefactoring(
                                    refactoring.Title,
                                    cancellationToken => refactoring.RefactorAsync(context.Document, container, condition, selectedExpressions, cancellationToken));
                            }
                        }

                        break;
                    }

                    case SyntaxKind.WhileStatement:
                    {
                        if (kind == SyntaxKind.LogicalAndExpression)
                        {
                            var refactoring = new ExtractConditionFromWhileToNestedIfRefactoring();
                            context.RegisterRefactoring(
                                refactoring.Title,
                                cancellationToken => refactoring.RefactorAsync(context.Document, (WhileStatementSyntax)parent, condition, selectedExpressions, cancellationToken));
                        }

                        break;
                    }
                    }
                }
            }
        }
コード例 #3
0
        protected TStatement RemoveExpressionsFromCondition(
            TStatement statement,
            BinaryExpressionSyntax condition,
            SelectedExpressions selectedExpressions)
        {
            var binaryExpression = (BinaryExpressionSyntax)selectedExpressions.Expressions.First().Parent;

            return(statement.ReplaceNode(
                       condition,
                       binaryExpression.Left));
        }
コード例 #4
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, SelectedExpressions selectedExpressions)
        {
            if (selectedExpressions.BinaryExpression.IsKind(SyntaxKind.AddExpression))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                StringExpressionChain chain = StringExpressionChain.TryCreate(selectedExpressions, semanticModel, context.CancellationToken);

                if (chain != null)
                {
                    ComputeRefactoring(context, chain);
                }
            }
        }
コード例 #5
0
        public async Task <Document> RefactorAsync(
            Document document,
            WhileStatementSyntax whileStatement,
            BinaryExpressionSyntax condition,
            SelectedExpressions selectedExpressions,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            WhileStatementSyntax newNode = RemoveExpressionsFromCondition(whileStatement, condition, selectedExpressions);

            newNode = AddNestedIf(newNode, selectedExpressions)
                      .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(whileStatement, newNode, cancellationToken).ConfigureAwait(false));
        }
コード例 #6
0
        public async Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            BinaryExpressionSyntax condition,
            SelectedExpressions selectedExpressions,
            CancellationToken cancellationToken)
        {
            IfStatementSyntax newNode = RemoveExpressionsFromCondition(ifStatement, condition, selectedExpressions)
                                        .WithFormatterAnnotation();

            ExpressionSyntax expression = SyntaxFactory.ParseExpression(selectedExpressions.ExpressionsText);

            newNode = AddNestedIf(newNode, expression);

            return(await document.ReplaceNodeAsync(ifStatement, newNode, cancellationToken).ConfigureAwait(false));
        }
コード例 #7
0
        public async Task <Document> RefactorAsync(
            Document document,
            StatementContainer container,
            BinaryExpressionSyntax condition,
            SelectedExpressions selectedExpressions,
            CancellationToken cancellationToken)
        {
            var ifStatement = (IfStatementSyntax)condition.Parent;

            IfStatementSyntax newIfStatement = RemoveExpressionsFromCondition(ifStatement, condition, selectedExpressions)
                                               .WithFormatterAnnotation();

            ExpressionSyntax expression = SyntaxFactory.ParseExpression(selectedExpressions.ExpressionsText);

            SyntaxNode newNode = AddNextIf(container, ifStatement, newIfStatement, expression);

            return(await document.ReplaceNodeAsync(container.Node, newNode, cancellationToken).ConfigureAwait(false));
        }
コード例 #8
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, BinaryExpressionSyntax binaryExpression)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.NegateOperator))
            {
                SyntaxToken operatorToken = binaryExpression.OperatorToken;

                if (operatorToken.Span.Contains(context.Span) &&
                    NegateOperatorRefactoring.CanBeNegated(operatorToken))
                {
                    context.RegisterRefactoring(
                        "Negate operator",
                        cancellationToken => NegateOperatorRefactoring.RefactorAsync(context.Document, operatorToken, cancellationToken));
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatBinaryExpression))
            {
                FormatBinaryExpressionRefactoring.ComputeRefactorings(context, binaryExpression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.NegateBinaryExpression))
            {
                NegateBinaryExpressionRefactoring.ComputeRefactoring(context, binaryExpression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandCoalesceExpression) &&
                binaryExpression.OperatorToken.Span.Contains(context.Span))
            {
                ExpandCoalesceExpressionRefactoring.ComputeRefactoring(context, binaryExpression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeStringExpressions) &&
                context.Span.IsBetweenSpans(binaryExpression))
            {
                await MergeStringExpressionsRefactoring.ComputeRefactoringAsync(context, binaryExpression).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInBinaryExpression) &&
                context.Span.IsBetweenSpans(binaryExpression))
            {
                SwapExpressionsInBinaryExpressionRefactoring.ComputeRefactoring(context, binaryExpression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceAsWithCast) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(binaryExpression))
            {
                ReplaceAsWithCastRefactoring.ComputeRefactoring(context, binaryExpression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.NegateIsExpression))
            {
                NegateIsExpressionRefactoring.ComputeRefactoring(context, binaryExpression);
            }

            if (context.Span.IsContainedInSpanOrBetweenSpans(binaryExpression.OperatorToken))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceEqualsExpressionWithStringEquals))
                {
                    await ReplaceEqualsExpressionWithStringEqualsRefactoring.ComputeRefactoringAsync(context, binaryExpression).ConfigureAwait(false);
                }

                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.ReplaceEqualsExpressionWithStringIsNullOrEmpty,
                        RefactoringIdentifiers.ReplaceEqualsExpressionWithStringIsNullOrWhiteSpace))
                {
                    await ReplaceEqualsExpressionRefactoring.ComputeRefactoringsAsync(context, binaryExpression).ConfigureAwait(false);
                }
            }

            if (!context.Span.IsBetweenSpans(binaryExpression) &&
                context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ExtractExpressionFromCondition,
                    RefactoringIdentifiers.MergeStringExpressions))
            {
                SelectedExpressions selectedExpressions = SelectedExpressions.TryCreate(binaryExpression, context.Span);

                if (selectedExpressions?.Expressions.Length > 1)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractExpressionFromCondition))
                    {
                        ExtractConditionRefactoring.ComputeRefactoring(context, selectedExpressions);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeStringExpressions))
                    {
                        await MergeStringExpressionsRefactoring.ComputeRefactoringAsync(context, selectedExpressions).ConfigureAwait(false);
                    }
                }
            }
        }