コード例 #1
0
 public static ImmutableArray <IfRefactoring> Analyze(
     SelectedStatementCollection selectedStatements,
     SemanticModel semanticModel,
     CancellationToken cancellationToken)
 {
     return(Analyze(selectedStatements, DefaultOptions, semanticModel, cancellationToken));
 }
コード例 #2
0
        private static async Task ComputeRefactoringAsync(
            RefactoringContext context,
            ExpressionStatementSyntax expressionStatement,
            SelectedStatementCollection selectedStatements)
        {
            ExpressionSyntax expression = expressionStatement.Expression;

            if (expression?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true)
            {
                var assignment        = (AssignmentExpressionSyntax)expression;
                ExpressionSyntax left = assignment.Left;

                if (left != null)
                {
                    ExpressionSyntax right = assignment.Right;

                    if (!right.IsKind(SyntaxKind.NullLiteralExpression) &&
                        CanBeEqualToNull(right) &&
                        !NullCheckExists(left, expressionStatement))
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        if (semanticModel
                            .GetTypeSymbol(left, context.CancellationToken)?
                            .IsReferenceType == true)
                        {
                            RegisterRefactoring(context, left, expressionStatement, selectedStatements.Count - 1);
                        }
                    }
                }
            }
        }
コード例 #3
0
        public static async Task <Document> RefactorAsync(
            Document document,
            ObjectCreationExpressionSyntax objectCreation,
            SelectedStatementCollection selectedStatements,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            StatementContainer container = selectedStatements.Container;

            ExpressionStatementSyntax[] expressionStatements = selectedStatements
                                                               .Skip(1)
                                                               .Cast <ExpressionStatementSyntax>()
                                                               .ToArray();

            StatementSyntax firstNode = selectedStatements.First;

            SyntaxList <StatementSyntax> newStatements = container.Statements.Replace(
                firstNode,
                firstNode.ReplaceNode(
                    objectCreation,
                    objectCreation.WithInitializer(CreateInitializer(objectCreation, expressionStatements))));

            int count = expressionStatements.Length;
            int index = selectedStatements.FirstIndex + 1;

            while (count > 0)
            {
                newStatements = newStatements.RemoveAt(index);
                count--;
            }

            return(await document.ReplaceNodeAsync(
                       container.Node,
                       container.NodeWithStatements(newStatements),
                       cancellationToken).ConfigureAwait(false));
        }
コード例 #4
0
        public async Task ComputeRefactoringAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            StatementSyntax statement = selectedStatements.First;

            if (statement?.IsKind(SyntaxKind.LocalDeclarationStatement) == true)
            {
                var localDeclaration = (LocalDeclarationStatementSyntax)statement;
                VariableDeclarationSyntax declaration = localDeclaration.Declaration;

                if (declaration != null)
                {
                    VariableDeclaratorSyntax variable = declaration.SingleVariableOrDefault();

                    if (variable != null &&
                        variable.Initializer?.Value?.IsKind(SyntaxKind.ObjectCreationExpression) == true &&
                        declaration.Type != null)
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(declaration.Type, context.CancellationToken);

                        if (typeSymbol?.IsNamedType() == true &&
                            ((INamedTypeSymbol)typeSymbol).Implements(SpecialType.System_IDisposable))
                        {
                            context.RegisterRefactoring(
                                $"Using '{variable.Identifier.ValueText}'",
                                cancellationToken => RefactorAsync(context.Document, selectedStatements, cancellationToken));
                        }
                    }
                }
            }
        }
コード例 #5
0
 public static async Task ComputeRefactoringAsync(RefactoringContext context, BlockSyntax block)
 {
     if (SelectedStatementsRefactoring.IsAnyRefactoringEnabled(context))
     {
         SelectedStatementCollection selectedStatements = SelectedStatementCollection.Create(block, context.Span);
         await SelectedStatementsRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
     }
 }
コード例 #6
0
        public static void ComputeRefactoring(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.Count == 2)
            {
                StatementSyntax[] statements = selectedStatements.ToArray();

                if (statements[0].IsKind(SyntaxKind.IfStatement) &&
                    statements[1].IsKind(SyntaxKind.ReturnStatement))
                {
                    var ifStatement = (IfStatementSyntax)statements[0];

                    if (!IfElseChain.IsPartOfChain(ifStatement))
                    {
                        ExpressionSyntax returnExpression = ReplaceIfWithStatementRefactoring.GetReturnExpression(ifStatement);

                        if (returnExpression != null)
                        {
                            var returnStatement = (ReturnStatementSyntax)statements[1];
                            ExpressionSyntax returnExpression2 = returnStatement.Expression;

                            if (returnExpression2 != null)
                            {
                                const string title = "Replace if-return with return";

                                if (returnExpression.IsBooleanLiteralExpression() ||
                                    returnExpression2.IsBooleanLiteralExpression())
                                {
                                    context.RegisterRefactoring(
                                        title,
                                        cancellationToken => RefactorAsync(context.Document, selectedStatements.Container, ifStatement, returnStatement, cancellationToken));
                                }
                                else
                                {
                                    context.RegisterRefactoring(
                                        title,
                                        cancellationToken =>
                                    {
                                        return(RefactorAsync(
                                                   context.Document,
                                                   selectedStatements.Container,
                                                   ifStatement,
                                                   returnStatement,
                                                   returnExpression,
                                                   returnExpression2,
                                                   cancellationToken));
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #7
0
        public static ImmutableArray <IfRefactoring> Analyze(
            SelectedStatementCollection selectedStatements,
            IfAnalysisOptions options,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            if (selectedStatements.Count == 2)
            {
                StatementSyntax[] statements = selectedStatements.ToArray();

                StatementSyntax statement1 = statements[0];
                StatementSyntax statement2 = statements[1];

                SyntaxKind kind1 = statement1.Kind();
                SyntaxKind kind2 = statement2.Kind();

                if (kind1 == SyntaxKind.IfStatement)
                {
                    if (kind2 == SyntaxKind.ReturnStatement)
                    {
                        var ifStatement = (IfStatementSyntax)statement1;

                        if (ifStatement.IsSimpleIf())
                        {
                            return(Analyze(ifStatement, (ReturnStatementSyntax)statement2, options, semanticModel, cancellationToken));
                        }
                    }
                }
                else if (options.UseConditionalExpression)
                {
                    if (kind1 == SyntaxKind.LocalDeclarationStatement)
                    {
                        if (kind2 == SyntaxKind.IfStatement)
                        {
                            return(Analyze((LocalDeclarationStatementSyntax)statement1, (IfStatementSyntax)statement2, options));
                        }
                    }
                    else if (kind1 == SyntaxKind.ExpressionStatement)
                    {
                        if (kind2 == SyntaxKind.IfStatement)
                        {
                            return(Analyze((ExpressionStatementSyntax)statement1, (IfStatementSyntax)statement2, options));
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
コード例 #8
0
        public static void ComputeRefactorings(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            List <IfStatementSyntax> ifStatements = GetIfStatements(selectedStatements);

            if (ifStatements?.Count > 1)
            {
                context.RegisterRefactoring(
                    "Merge if statements",
                    cancellationToken =>
                {
                    return(RefactorAsync(
                               context.Document,
                               selectedStatements.Container,
                               ifStatements.ToImmutableArray(),
                               cancellationToken));
                });
            }
        }
コード例 #9
0
        public static void ComputeRefactorings(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            using (IEnumerator <StatementSyntax> en = selectedStatements.GetEnumerator())
            {
                if (en.MoveNext() &&
                    en.Current.IsKind(SyntaxKind.ExpressionStatement))
                {
                    var statement = (ExpressionStatementSyntax)en.Current;

                    if (statement.Expression?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true &&
                        en.MoveNext() &&
                        en.Current.IsKind(SyntaxKind.ReturnStatement))
                    {
                        var returnStatement = (ReturnStatementSyntax)en.Current;

                        if (returnStatement.Expression != null &&
                            !en.MoveNext())
                        {
                            var assignment = (AssignmentExpressionSyntax)statement.Expression;

                            if (assignment.Left?.IsMissing == false &&
                                assignment.Right?.IsMissing == false &&
                                assignment.Left.IsEquivalentTo(returnStatement.Expression, topLevel: false))
                            {
                                context.RegisterRefactoring(
                                    "Merge statements",
                                    cancellationToken =>
                                {
                                    return(RefactorAsync(
                                               context.Document,
                                               statement,
                                               returnStatement,
                                               cancellationToken));
                                });
                            }
                        }
                    }
                }
            }
        }
コード例 #10
0
        private static async Task ComputeRefactoringAsync(
            RefactoringContext context,
            LocalDeclarationStatementSyntax localDeclaration,
            SelectedStatementCollection selectedStatements)
        {
            VariableDeclarationSyntax variableDeclaration = localDeclaration.Declaration;

            if (variableDeclaration != null)
            {
                TypeSyntax type = variableDeclaration.Type;

                if (type != null)
                {
                    VariableDeclaratorSyntax variableDeclarator = variableDeclaration.SingleVariableOrDefault();

                    ExpressionSyntax value = variableDeclarator?.Initializer?.Value;

                    if (!value.IsKind(SyntaxKind.NullLiteralExpression) &&
                        CanBeEqualToNull(value))
                    {
                        SyntaxToken identifier = variableDeclarator.Identifier;

                        IdentifierNameSyntax identifierName = IdentifierName(identifier);

                        if (!NullCheckExists(identifierName, localDeclaration))
                        {
                            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                            if (semanticModel
                                .GetTypeSymbol(type, context.CancellationToken)?
                                .IsReferenceType == true)
                            {
                                RegisterRefactoring(context, identifierName, localDeclaration, selectedStatements.Count - 1);
                            }
                        }
                    }
                }
            }
        }
コード例 #11
0
        public Task <Document> RefactorAsync(
            Document document,
            SelectedStatementCollection selectedStatements,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            IStatementContainer container = selectedStatements.Container;

            StatementSyntax[] statements = selectedStatements.ToArray();

            int index = selectedStatements.FirstIndex;

            SyntaxTriviaList leadingTrivia  = statements[0].GetLeadingTrivia();
            SyntaxTriviaList trailingTrivia = statements[statements.Length - 1].GetTrailingTrivia();

            statements[0] = statements[0].WithLeadingTrivia();
            statements[statements.Length - 1] = statements[statements.Length - 1].WithTrailingTrivia();

            SyntaxList <StatementSyntax> newStatements = container.Statements;

            int cnt = statements.Length;

            while (cnt > 0)
            {
                newStatements = newStatements.RemoveAt(index);
                cnt--;
            }

            TStatement statement = CreateStatement(statements.ToImmutableArray());

            statement = statement
                        .WithLeadingTrivia(leadingTrivia)
                        .WithTrailingTrivia(trailingTrivia)
                        .WithFormatterAnnotation();

            newStatements = newStatements.Insert(index, statement);

            return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken));
        }
コード例 #12
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (SelectedStatementsRefactoring.IsAnyRefactoringEnabled(context))
            {
                SelectedStatementCollection selectedStatements = SelectedStatementCollection.Create(switchSection, context.Span);
                await SelectedStatementsRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitSwitchLabels))
            {
                SplitSwitchLabelsRefactoring.ComputeRefactoring(context, switchSection);
            }

            if (context.Span.IsEmpty &&
                context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddBracesToSwitchSection,
                    RefactoringIdentifiers.AddBracesToSwitchSections,
                    RefactoringIdentifiers.RemoveBracesFromSwitchSection,
                    RefactoringIdentifiers.RemoveBracesFromSwitchSections))
            {
                var switchStatement = (SwitchStatementSyntax)switchSection.Parent;

                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                switch (CSharpAnalysis.AnalyzeBraces(switchSection))
                {
                case BracesAnalysisResult.AddBraces:
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSection))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken));
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSections) &&
                        sections.Any(f => f != switchSection && AddBracesToSwitchSectionRefactoring.CanAddBraces(f)))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionsRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken));
                    }

                    break;
                }

                case BracesAnalysisResult.RemoveBraces:
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSection))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken));
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSections) &&
                        sections.Any(f => f != switchSection && RemoveBracesFromSwitchSectionRefactoring.CanRemoveBraces(f)))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionsRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken));
                    }

                    break;
                }
                }
            }
        }
コード例 #13
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            StatementSyntax firstStatement = selectedStatements.First;
            SemanticModel   semanticModel  = null;
            ISymbol         symbol         = null;
            ObjectCreationExpressionSyntax objectCreation = null;

            if (selectedStatements.IsMultiple)
            {
                SyntaxKind kind = firstStatement.Kind();

                if (kind == SyntaxKind.LocalDeclarationStatement)
                {
                    var localDeclaration = (LocalDeclarationStatementSyntax)firstStatement;

                    VariableDeclaratorSyntax variable = localDeclaration.Declaration?.SingleVariableOrDefault();

                    objectCreation = variable?.Initializer?.Value as ObjectCreationExpressionSyntax;

                    if (objectCreation != null)
                    {
                        semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        symbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken);
                    }
                }
                else if (kind == SyntaxKind.ExpressionStatement)
                {
                    var expressionStatement = (ExpressionStatementSyntax)firstStatement;

                    var assignment = expressionStatement.Expression as AssignmentExpressionSyntax;

                    if (assignment != null)
                    {
                        objectCreation = assignment.Right as ObjectCreationExpressionSyntax;

                        if (objectCreation != null)
                        {
                            semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                            symbol = semanticModel.GetSymbol(assignment.Left, context.CancellationToken);
                        }
                    }
                }

                if (objectCreation != null &&
                    symbol?.IsErrorType() == false &&
                    selectedStatements
                    .Skip(1)
                    .All(f => IsValidAssignmentStatement(f, symbol, semanticModel, context.CancellationToken)))
                {
                    context.RegisterRefactoring(
                        "Collapse to initializer",
                        cancellationToken =>
                    {
                        return(RefactorAsync(
                                   context.Document,
                                   objectCreation,
                                   selectedStatements,
                                   cancellationToken));
                    });
                }
            }
        }
コード例 #14
0
        internal static async Task ComputeRefactoringAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.IsMultiple)
            {
                StatementSyntax statement = selectedStatements.First;

                SyntaxKind kind = statement.Kind();

                if (kind == SyntaxKind.LocalDeclarationStatement)
                {
                    await ComputeRefactoringAsync(context, (LocalDeclarationStatementSyntax)statement, selectedStatements).ConfigureAwait(false);
                }
                else if (kind == SyntaxKind.ExpressionStatement)
                {
                    await ComputeRefactoringAsync(context, (ExpressionStatementSyntax)statement, selectedStatements).ConfigureAwait(false);
                }
            }
        }
コード例 #15
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.Any())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInUsingStatement))
                {
                    var refactoring = new WrapInUsingStatementRefactoring();
                    await refactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CollapseToInitializer))
                {
                    await CollapseToInitializerRefactoring.ComputeRefactoringsAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeIfStatements))
                {
                    MergeIfStatementsRefactoring.ComputeRefactorings(context, selectedStatements);
                }

                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf,
                        RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf,
                        RefactoringIdentifiers.SimplifyIf))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    var options = new IfAnalysisOptions(
                        useCoalesceExpression: context.IsRefactoringEnabled(RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf),
                        useConditionalExpression: context.IsRefactoringEnabled(RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf),
                        useBooleanExpression: context.IsRefactoringEnabled(RefactoringIdentifiers.SimplifyIf));

                    foreach (IfRefactoring refactoring in IfRefactoring.Analyze(selectedStatements, options, semanticModel, context.CancellationToken))
                    {
                        context.RegisterRefactoring(
                            refactoring.Title,
                            cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken));
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeLocalDeclarations))
                {
                    await MergeLocalDeclarationsRefactoring.ComputeRefactoringsAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAssignmentExpressionWithReturnStatement))
                {
                    MergeAssignmentExpressionWithReturnStatementRefactoring.ComputeRefactorings(context, selectedStatements);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckExpressionForNull))
                {
                    await CheckExpressionForNullRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceWhileWithFor))
                {
                    await ReplaceWhileWithForRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInCondition))
                {
                    context.RegisterRefactoring(
                        "Wrap in condition",
                        cancellationToken =>
                    {
                        var refactoring = new WrapInIfStatementRefactoring();
                        return(refactoring.RefactorAsync(context.Document, selectedStatements, cancellationToken));
                    });
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInTryCatch))
                {
                    context.RegisterRefactoring(
                        "Wrap in try-catch",
                        cancellationToken =>
                    {
                        var refactoring = new WrapInTryCatchRefactoring();
                        return(refactoring.RefactorAsync(context.Document, selectedStatements, cancellationToken));
                    });
                }
            }
        }
コード例 #16
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.IsMultiple)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (AreLocalDeclarations(selectedStatements, semanticModel, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        "Merge local declarations",
                        cancellationToken =>
                    {
                        return(RefactorAsync(
                                   context.Document,
                                   selectedStatements.Container,
                                   selectedStatements.Cast <LocalDeclarationStatementSyntax>().ToArray(),
                                   cancellationToken));
                    });
                }
            }
        }
コード例 #17
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.IsMultiple)
            {
                StatementSyntax lastStatement = selectedStatements.Last;

                if (lastStatement.IsKind(SyntaxKind.WhileStatement))
                {
                    IEnumerable <StatementSyntax> statements = selectedStatements
                                                               .Take(selectedStatements.LastIndex - selectedStatements.FirstIndex);

                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    List <LocalDeclarationStatementSyntax> localDeclarations = null;
                    List <ExpressionSyntax> expressions = null;

                    foreach (StatementSyntax statement in statements)
                    {
                        SyntaxKind kind = statement.Kind();

                        if (kind == SyntaxKind.LocalDeclarationStatement)
                        {
                            var localDeclaration = (LocalDeclarationStatementSyntax)statement;

                            if (!IsAnySymbolReferenced(localDeclaration, selectedStatements.ToImmutableArray(), selectedStatements.LastIndex + 1, semanticModel, context.CancellationToken))
                            {
                                if (localDeclarations == null)
                                {
                                    localDeclarations = new List <LocalDeclarationStatementSyntax>();
                                }

                                localDeclarations.Add(localDeclaration);
                            }
                            else
                            {
                                return;
                            }
                        }
                        else if (kind == SyntaxKind.ExpressionStatement)
                        {
                            var expressionStatement = (ExpressionStatementSyntax)statement;

                            ExpressionSyntax expression = expressionStatement.Expression;

                            if (expression != null)
                            {
                                if (CanBeInitializer(expression))
                                {
                                    if (expressions == null)
                                    {
                                        expressions = new List <ExpressionSyntax>();
                                    }

                                    expressions.Add(expression);
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                        else
                        {
                            return;
                        }
                    }

                    if (localDeclarations == null ||
                        expressions == null)
                    {
                        if (localDeclarations == null ||
                            localDeclarations
                            .Select(f => f.Declaration)
                            .Where(f => f != null)
                            .Select(f => semanticModel.GetTypeSymbol(f.Type, context.CancellationToken))
                            .Distinct()
                            .Count() == 1)
                        {
                            context.RegisterRefactoring(
                                Title,
                                cancellationToken => RefactorAsync(context.Document, localDeclarations, expressions, (WhileStatementSyntax)lastStatement, cancellationToken));
                        }
                    }
                }
            }
        }
コード例 #18
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.Any())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInUsingStatement))
                {
                    var refactoring = new WrapInUsingStatementRefactoring();
                    await refactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CollapseToInitializer))
                {
                    await CollapseToInitializerRefactoring.ComputeRefactoringsAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeIfStatements))
                {
                    MergeIfStatementsRefactoring.ComputeRefactorings(context, selectedStatements);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfStatementWithReturnStatement))
                {
                    ReplaceIfAndReturnWithReturnRefactoring.ComputeRefactoring(context, selectedStatements);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeLocalDeclarations))
                {
                    await MergeLocalDeclarationsRefactoring.ComputeRefactoringsAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAssignmentExpressionWithReturnStatement))
                {
                    MergeAssignmentExpressionWithReturnStatementRefactoring.ComputeRefactorings(context, selectedStatements);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckExpressionForNull))
                {
                    await CheckExpressionForNullRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceWhileWithFor))
                {
                    await ReplaceWhileWithForRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInCondition))
                {
                    context.RegisterRefactoring(
                        "Wrap in condition",
                        cancellationToken =>
                    {
                        var refactoring = new WrapInIfStatementRefactoring();
                        return(refactoring.RefactorAsync(context.Document, selectedStatements, cancellationToken));
                    });
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInTryCatch))
                {
                    context.RegisterRefactoring(
                        "Wrap in try-catch",
                        cancellationToken =>
                    {
                        var refactoring = new WrapInTryCatchRefactoring();
                        return(refactoring.RefactorAsync(context.Document, selectedStatements, cancellationToken));
                    });
                }
            }
        }