public static void ComputeRefactoring(RefactoringContext context, SelectedStatementsInfo info)
        {
            if (info.IsSingleSelected)
            {
                StatementSyntax[] statements = info.SelectedNodes().ToArray();

                StatementSyntax first = statements[0];

                if (first.IsKind(SyntaxKind.IfStatement))
                {
                    var ifStatement = (IfStatementSyntax)first;

                    if (IfElseAnalysis.IsTopmostIf(ifStatement) &&
                        CanBeReplacedWithSwitch(ifStatement))
                    {
                        string title = (IfElseAnalysis.IsIsolatedIf(ifStatement))
                            ? "Replace if with switch"
                            : "Replace if-else with switch";

                        context.RegisterRefactoring(
                            title,
                            cancellationToken =>
                        {
                            return(RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        });
                    }
                }
            }
        }
Пример #2
0
        private static bool CheckContainingNode(SyntaxNode node)
        {
            switch (node.Kind())
            {
            case SyntaxKind.ForEachStatement:
            case SyntaxKind.ForStatement:
            case SyntaxKind.UsingStatement:
            case SyntaxKind.WhileStatement:
            case SyntaxKind.DoStatement:
            case SyntaxKind.LockStatement:
            case SyntaxKind.FixedStatement:
            case SyntaxKind.UnsafeStatement:
            case SyntaxKind.TryStatement:
            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
                return(true);

            case SyntaxKind.IfStatement:
                return(IfElseAnalysis.IsTopmostIf((IfStatementSyntax)node));

            case SyntaxKind.ElseClause:
                return(IfElseAnalysis.IsEndOfChain((ElseClauseSyntax)node));
            }

            return(false);
        }
Пример #3
0
        public static bool CanRefactor(
            IfStatementSyntax ifStatement,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (IfElseAnalysis.IsTopmostIf(ifStatement))
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause != null)
                {
                    ExpressionSyntax condition = ifStatement.Condition;

                    if (condition != null)
                    {
                        ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(condition, cancellationToken);

                        if (typeSymbol?.IsBoolean() == true)
                        {
                            AssignmentExpressionSyntax trueExpression = GetSimpleAssignmentExpression(ifStatement.Statement);

                            ExpressionSyntax trueRight = trueExpression?.Right;

                            if (trueRight?.IsBooleanLiteralExpression() == true)
                            {
                                AssignmentExpressionSyntax falseExpression = GetSimpleAssignmentExpression(elseClause.Statement);

                                ExpressionSyntax falseRight = falseExpression?.Right;

                                if (falseRight?.IsBooleanLiteralExpression() == true)
                                {
                                    var trueBooleanLiteral  = (LiteralExpressionSyntax)trueRight;
                                    var falseBooleanLiteral = (LiteralExpressionSyntax)falseRight;

                                    if (trueBooleanLiteral.IsKind(SyntaxKind.TrueLiteralExpression) != falseBooleanLiteral.IsKind(SyntaxKind.TrueLiteralExpression) &&
                                        trueExpression.Left?.IsEquivalentTo(falseExpression.Left, topLevel: false) == true)
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
        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);
            }
        }
        public static void ComputeRefactoring(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (IfElseAnalysis.IsTopmostIf(ifStatement))
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause != null)
                {
                    StatementSyntax statement2 = elseClause.Statement;

                    if (statement2?.IsKind(SyntaxKind.IfStatement) == false)
                    {
                        StatementSyntax statement1 = GetStatementOrDefault(ifStatement.Statement);

                        if (statement1 != null)
                        {
                            statement2 = GetStatementOrDefault(statement2);

                            if (statement2 != null)
                            {
                                SyntaxKind kind1 = statement1.Kind();
                                SyntaxKind kind2 = statement2.Kind();

                                if (kind1 == SyntaxKind.ReturnStatement)
                                {
                                    if (kind2 == SyntaxKind.ReturnStatement)
                                    {
                                        ComputeRefactoring(
                                            context,
                                            ifStatement,
                                            (ReturnStatementSyntax)statement1,
                                            (ReturnStatementSyntax)statement2);
                                    }
                                }
                                else if (kind1 == SyntaxKind.YieldReturnStatement)
                                {
                                    if (kind2 == SyntaxKind.YieldReturnStatement)
                                    {
                                        ComputeRefactoring(
                                            context,
                                            ifStatement,
                                            (YieldStatementSyntax)statement1,
                                            (YieldStatementSyntax)statement2);
                                    }
                                }
                                else if (kind1 == SyntaxKind.ExpressionStatement)
                                {
                                    if (kind2 == SyntaxKind.ExpressionStatement)
                                    {
                                        ComputeRefactoring(
                                            context,
                                            ifStatement,
                                            (ExpressionStatementSyntax)statement1,
                                            (ExpressionStatementSyntax)statement2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #6
0
        private static StatementSyntax GetStatement(
            RefactoringContext context,
            BlockSyntax block,
            SyntaxNode parent)
        {
            switch (parent?.Kind())
            {
            case SyntaxKind.WhileStatement:
            case SyntaxKind.DoStatement:
            case SyntaxKind.ForStatement:
            case SyntaxKind.ForEachStatement:
            case SyntaxKind.FixedStatement:
            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
            case SyntaxKind.UnsafeStatement:
            case SyntaxKind.UsingStatement:
            case SyntaxKind.LockStatement:
            {
                if (block.OpenBraceToken.Span.Contains(context.Span) ||
                    block.CloseBraceToken.Span.Contains(context.Span))
                {
                    if (parent.IsKind(SyntaxKind.UsingStatement))
                    {
                        var usingStatement = (UsingStatementSyntax)parent;

                        while (usingStatement.Parent?.IsKind(SyntaxKind.UsingStatement) == true)
                        {
                            usingStatement = (UsingStatementSyntax)usingStatement.Parent;
                        }

                        return(usingStatement);
                    }

                    return((StatementSyntax)parent);
                }

                break;
            }

            case SyntaxKind.TryStatement:
            {
                var tryStatement = (TryStatementSyntax)parent;

                if (tryStatement.Block?.OpenBraceToken.Span.Contains(context.Span) == true)
                {
                    return((StatementSyntax)parent);
                }

                break;
            }

            case SyntaxKind.IfStatement:
            {
                var ifStatement = (IfStatementSyntax)parent;

                if (IfElseAnalysis.IsTopmostIf(ifStatement) &&
                    block.OpenBraceToken.Span.Contains(context.Span))
                {
                    return(ifStatement);
                }

                if (ifStatement.Else == null &&
                    block.CloseBraceToken.Span.Contains(context.Span))
                {
                    return(ifStatement);
                }

                break;
            }

            case SyntaxKind.ElseClause:
            {
                var elseClause = (ElseClauseSyntax)parent;

                if (block.CloseBraceToken.Span.Contains(context.Span))
                {
                    return(IfElseAnalysis.GetTopmostIf(elseClause));
                }

                break;
            }

            case SyntaxKind.CatchClause:
            {
                var catchClause = (CatchClauseSyntax)parent;

                if (catchClause.Parent?.IsKind(SyntaxKind.TryStatement) == true)
                {
                    var tryStatement = (TryStatementSyntax)catchClause.Parent;

                    if (tryStatement.Finally == null &&
                        catchClause.Block?.CloseBraceToken.Span.Contains(context.Span) == true)
                    {
                        return(tryStatement);
                    }
                }

                break;
            }

            case SyntaxKind.FinallyClause:
            {
                var finallyClause = (FinallyClauseSyntax)parent;

                if (finallyClause.Parent?.IsKind(SyntaxKind.TryStatement) == true)
                {
                    var tryStatement = (TryStatementSyntax)finallyClause.Parent;

                    if (finallyClause.Block?.CloseBraceToken.Span.Contains(context.Span) == true)
                    {
                        return(tryStatement);
                    }
                }

                break;
            }
            }

            return(null);
        }