Ejemplo n.º 1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison))
            {
                await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractExpressionFromCondition) &&
                context.Span.IsBetweenSpans(expression))
            {
                ExtractConditionRefactoring.ComputeRefactoring(context, expression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ParenthesizeExpression) &&
                context.Span.IsBetweenSpans(expression) &&
                ParenthesizeExpressionRefactoring.CanRefactor(context, expression))
            {
                context.RegisterRefactoring(
                    $"Parenthesize '{expression}'",
                    cancellationToken => ParenthesizeExpressionRefactoring.RefactorAsync(context.Document, expression, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceNullLiteralExpressionWithDefaultExpression))
            {
                await ReplaceNullLiteralExpressionWithDefaultExpressionRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithExpression))
            {
                ReplaceConditionalExpressionWithExpressionRefactoring.ComputeRefactoring(context, expression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckExpressionForNull) &&
                context.Span.IsContainedInSpanOrBetweenSpans(expression))
            {
                await CheckExpressionForNullRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceExpressionWithConstantValue) &&
                context.Span.IsBetweenSpans(expression))
            {
                await ReplaceExpressionWithConstantValueRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, WhileStatementSyntax whileStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison) &&
                whileStatement.Condition != null &&
                whileStatement.Condition.Span.Contains(context.Span) &&
                context.SupportsSemanticModel)
            {
                await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, whileStatement.Condition).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceWhileStatementWithDoStatement) &&
                (whileStatement.WhileKeyword.Span.Contains(context.Span)))
            {
                context.RegisterRefactoring(
                    "Replace while with do",
                    cancellationToken =>
                {
                    return(ReplaceWhileStatementWithDoStatementRefactoring.RefactorAsync(
                               context.Document,
                               whileStatement,
                               cancellationToken));
                });
            }
        }
Ejemplo n.º 3
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SwapStatementsInIfElse,
                    RefactoringIdentifiers.ReplaceIfElseWithConditionalExpression) &&
                IfElseAnalysis.IsTopmostIf(ifStatement) &&
                context.Span.IsBetweenSpans(ifStatement))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithConditionalExpression))
                {
                    ReplaceIfElseWithConditionalExpressionRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse) &&
                    SwapStatementInIfElseRefactoring.CanRefactor(ifStatement))
                {
                    context.RegisterRefactoring(
                        "Swap statements in if-else",
                        cancellationToken =>
                    {
                        return(SwapStatementInIfElseRefactoring.RefactorAsync(
                                   context.Document,
                                   ifStatement,
                                   cancellationToken));
                    });
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBooleanComparison) &&
                ifStatement.Condition != null &&
                ifStatement.Condition.Span.Contains(context.Span) &&
                context.SupportsSemanticModel)
            {
                await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, ifStatement.Condition).ConfigureAwait(false);
            }
        }
Ejemplo n.º 4
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.AddBooleanComparison) &&
                binaryExpression.IsKind(SyntaxKind.LogicalAndExpression, SyntaxKind.LogicalOrExpression) &&
                binaryExpression.Left?.IsMissing == false &&
                binaryExpression.Right?.IsMissing == false &&
                context.SupportsSemanticModel)
            {
                if (binaryExpression.Left.Span.Contains(context.Span))
                {
                    await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, binaryExpression.Left).ConfigureAwait(false);
                }
                else if (binaryExpression.Right.Span.Contains(context.Span))
                {
                    await AddBooleanComparisonRefactoring.ComputeRefactoringAsync(context, binaryExpression.Right).ConfigureAwait(false);
                }
            }

            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.CanRefactor(binaryExpression))
            {
                context.RegisterRefactoring(
                    "Expand ??",
                    cancellationToken =>
                {
                    return(ExpandCoalesceExpressionRefactoring.RefactorAsync(
                               context.Document,
                               binaryExpression,
                               cancellationToken));
                });
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.MergeStringLiterals,
                    RefactoringIdentifiers.MergeStringLiteralsIntoMultilineStringLiteral) &&
                MergeStringLiteralsRefactoring.CanRefactor(context, binaryExpression))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeStringLiterals))
                {
                    context.RegisterRefactoring(
                        "Merge string literals",
                        cancellationToken => MergeStringLiteralsRefactoring.RefactorAsync(context.Document, binaryExpression, cancellationToken: cancellationToken));
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeStringLiteralsIntoMultilineStringLiteral) &&
                    binaryExpression
                    .DescendantTrivia(binaryExpression.Span)
                    .Any(f => f.IsKind(SyntaxKind.EndOfLineTrivia)))
                {
                    context.RegisterRefactoring(
                        "Merge string literals into multiline string literal",
                        cancellationToken => MergeStringLiteralsRefactoring.RefactorAsync(context.Document, binaryExpression, multiline: true, cancellationToken: cancellationToken));
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInBinaryExpression) &&
                SwapExpressionsRefactoring.CanRefactor(binaryExpression) &&
                context.Span.IsBetweenSpans(binaryExpression))
            {
                context.RegisterRefactoring(
                    "Swap expressions",
                    cancellationToken =>
                {
                    return(SwapExpressionsRefactoring.RefactorAsync(
                               context.Document,
                               binaryExpression,
                               cancellationToken));
                });
            }

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

            if (context.Span.IsContainedInSpanOrBetweenSpans(binaryExpression.OperatorToken) &&
                context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ReplaceEqualsExpressionWithStringEquals,
                    RefactoringIdentifiers.ReplaceEqualsExpressionWithStringIsNullOrWhiteSpace))
            {
                await ReplaceEqualsExpressionRefactoring.ComputeRefactoringsAsync(context, binaryExpression).ConfigureAwait(false);
            }
        }