コード例 #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, VariableDeclarationSyntax variableDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameIdentifierAccordingToTypeName))
            {
                await RenameVariableAccordingToTypeNameAsync(context, variableDeclaration).ConfigureAwait(false);
            }

            await ChangeVariableDeclarationTypeRefactoring.ComputeRefactoringsAsync(context, variableDeclaration).ConfigureAwait(false);

            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod))
            {
                await AddCastExpressionAsync(context, variableDeclaration).ConfigureAwait(false);
            }

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

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitVariableDeclaration) &&
                SplitVariableDeclarationRefactoring.CanRefactor(variableDeclaration))
            {
                context.RegisterRefactoring(
                    SplitVariableDeclarationRefactoring.GetTitle(variableDeclaration),
                    cancellationToken => SplitVariableDeclarationRefactoring.RefactorAsync(context.Document, variableDeclaration, cancellationToken));
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.ExtractExpressionFromCondition) &&
                context.Span.IsBetweenSpans(expression))
            {
                ExtractConditionRefactoring.ComputeRefactoring(context, expression);
            }

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

            if (context.IsRefactoringEnabled(RefactoringDescriptors.ReplaceNullLiteralWithDefaultExpression))
            {
                await ReplaceNullLiteralWithDefaultExpressionRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.ReplaceConditionalExpressionWithTrueOrFalseBranch))
            {
                ReplaceConditionalExpressionWithTrueOrFalseBranchRefactoring.ComputeRefactoring(context, expression);
            }

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

            if (context.IsRefactoringEnabled(RefactoringDescriptors.InlineConstantValue) &&
                context.Span.IsBetweenSpans(expression))
            {
                await InlineConstantValueRefactoring.ComputeRefactoringAsync(context, expression).ConfigureAwait(false);
            }
        }
コード例 #4
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, VariableDeclarationSyntax variableDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.RenameIdentifierAccordingToTypeName))
            {
                await RenameVariableAccordingToTypeNameAsync(context, variableDeclaration).ConfigureAwait(false);
            }

            await ChangeVariableDeclarationTypeRefactoring.ComputeRefactoringsAsync(context, variableDeclaration).ConfigureAwait(false);

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

            if (context.IsRefactoringEnabled(RefactoringDescriptors.SplitVariableDeclaration) &&
                SplitVariableDeclarationAnalysis.IsFixable(variableDeclaration))
            {
                context.RegisterRefactoring(
                    SplitVariableDeclarationRefactoring.GetTitle(variableDeclaration),
                    ct => SplitVariableDeclarationRefactoring.RefactorAsync(context.Document, variableDeclaration, ct),
                    RefactoringDescriptors.SplitVariableDeclaration);
            }
        }
コード例 #5
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInUsingStatement))
            {
                var refactoring = new WrapStatements.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.ConvertStatementsToIfElse))
            {
                ConvertStatementsToIfElseRefactoring.ComputeRefactorings(context, selectedStatements);
            }

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

                IfAnalysisOptions options = IfStatementRefactoring.GetIfAnalysisOptions(context);

                foreach (IfAnalysis analysis in IfAnalysis.Analyze(selectedStatements, options, semanticModel, context.CancellationToken))
                {
                    string refactoringId = IfStatementRefactoring.GetRefactoringIdentifier(analysis);

                    if (context.IsRefactoringEnabled(refactoringId))
                    {
                        context.RegisterRefactoring(
                            analysis.Title,
                            cancellationToken => IfRefactoring.RefactorAsync(context.Document, analysis, cancellationToken),
                            equivalenceKey: refactoringId);
                    }
                }
            }

            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.ConvertWhileToFor))
            {
                await ConvertWhileToForRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInCondition))
            {
                context.RegisterRefactoring(
                    WrapInIfStatementRefactoring.Title,
                    ct => WrapInIfStatementRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct),
                    RefactoringIdentifiers.WrapInCondition);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInTryCatch))
            {
                context.RegisterRefactoring(
                    WrapInTryCatchRefactoring.Title,
                    ct => WrapInTryCatchRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct),
                    RefactoringIdentifiers.WrapInTryCatch);
            }
        }
コード例 #6
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInUsingStatement))
            {
                var refactoring = new WrapStatements.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),
                    useExpression: false);

                foreach (IfAnalysis analysis in IfAnalysis.Analyze(selectedStatements, options, semanticModel, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        analysis.Title,
                        cancellationToken => IfRefactoring.RefactorAsync(context.Document, analysis, 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.WrapInElseClause))
            {
                WrapInElseClauseRefactoring.ComputeRefactoring(context, selectedStatements);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInCondition))
            {
                context.RegisterRefactoring(
                    WrapInIfStatementRefactoring.Title,
                    ct => WrapInIfStatementRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInTryCatch))
            {
                context.RegisterRefactoring(
                    WrapInTryCatchRefactoring.Title,
                    ct => WrapInTryCatchRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct));
            }
        }
コード例 #7
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));
                    });
                }
            }
        }