コード例 #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForEachStatementSyntax forEachStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringDescriptors.UseImplicitType,
                    RefactoringDescriptors.UseExplicitType,
                    RefactoringDescriptors.ChangeTypeAccordingToExpression))
            {
                await ChangeTypeAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.RenameIdentifierAccordingToTypeName))
            {
                await RenameIdentifierAccordingToTypeNameAsync(context, forEachStatement).ConfigureAwait(false);
            }

            SemanticModel semanticModel = null;

            if (context.IsAnyRefactoringEnabled(RefactoringDescriptors.ConvertForEachToFor, RefactoringDescriptors.ConvertForEachToForAndReverseLoop) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forEachStatement))
            {
                semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(forEachStatement.Expression, context.CancellationToken);

                if (SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, forEachStatement.SpanStart))
                {
                    if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertForEachToFor))
                    {
                        context.RegisterRefactoring(
                            "Convert to 'for'",
                            ct => ConvertForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: false, cancellationToken: ct),
                            RefactoringDescriptors.ConvertForEachToFor);
                    }

                    if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertForEachToForAndReverseLoop))
                    {
                        context.RegisterRefactoring(
                            "Convert to 'for' and reverse loop",
                            ct => ConvertForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: true, cancellationToken: ct),
                            RefactoringDescriptors.ConvertForEachToForAndReverseLoop);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.DeconstructForeachVariable) &&
                TextSpan.FromBounds(forEachStatement.Type.SpanStart, forEachStatement.Identifier.Span.End).Contains(context.Span))
            {
                semanticModel ??= await context.GetSemanticModelAsync().ConfigureAwait(false);

                DeconstructForeachVariableRefactoring.ComputeRefactoring(context, forEachStatement, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.UseEnumeratorExplicitly) &&
                context.Span.IsEmptyAndContainedInSpan(forEachStatement.ForEachKeyword))
            {
                UseEnumeratorExplicitlyRefactoring.ComputeRefactoring(context, forEachStatement);
            }
        }
コード例 #2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForEachStatementSyntax forEachStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeExplicitTypeToVar,
                    RefactoringIdentifiers.ChangeVarToExplicitType,
                    RefactoringIdentifiers.ChangeTypeAccordingToExpression))
            {
                await ChangeTypeAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameIdentifierAccordingToTypeName))
            {
                await RenameIdentifierAccordingToTypeNameAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor, RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forEachStatement))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(forEachStatement.Expression, context.CancellationToken);

                if (SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, forEachStatement.SpanStart))
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor))
                    {
                        context.RegisterRefactoring(
                            "Replace foreach with for",
                            cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: false, cancellationToken: cancellationToken),
                            RefactoringIdentifiers.ReplaceForEachWithFor);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop))
                    {
                        context.RegisterRefactoring(
                            "Replace foreach with for and reverse loop",
                            cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: true, cancellationToken: cancellationToken),
                            RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithEnumerator) &&
                context.Span.IsEmptyAndContainedInSpan(forEachStatement.ForEachKeyword))
            {
                ReplaceForEachWithEnumeratorRefactoring.ComputeRefactoring(context, forEachStatement);
            }
        }
コード例 #3
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf,
                    RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf,
                    RefactoringIdentifiers.SimplifyIf,
                    RefactoringIdentifiers.SwapStatementsInIfElse,
                    RefactoringIdentifiers.ReplaceIfElseWithSwitch) &&
                ifStatement.IsTopmostIf() &&
                context.Span.IsBetweenSpans(ifStatement))
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf,
                        RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf,
                        RefactoringIdentifiers.SimplifyIf,
                        RefactoringIdentifiers.SplitIfStatement))
                {
                    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(ifStatement, options, semanticModel, context.CancellationToken))
                    {
                        context.RegisterRefactoring(
                            refactoring.Title,
                            cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken));
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse))
                {
                    SwapStatementInIfElseRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithSwitch))
                {
                    await ReplaceIfElseWithSwitchRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitIfStatement))
                {
                    SplitIfStatementRefactoring.ComputeRefactoring(context, ifStatement);
                }
            }
        }
コード例 #4
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConstructorDeclarationSyntax constructorDeclaration)
        {
            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic, RefactoringIdentifiers.MarkAllMembersAsStatic) &&
                constructorDeclaration.Span.Contains(context.Span) &&
                MarkMemberAsStaticRefactoring.CanRefactor(constructorDeclaration))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic))
                {
                    context.RegisterRefactoring(
                        "Mark constructor as static",
                        cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, constructorDeclaration, cancellationToken));
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkAllMembersAsStatic))
                {
                    MarkAllMembersAsStaticRefactoring.RegisterRefactoring(context, (ClassDeclarationSyntax)constructorDeclaration.Parent);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                constructorDeclaration.Body?.Span.Contains(context.Span) == true &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(constructorDeclaration))
            {
                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, constructorDeclaration, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                constructorDeclaration.HeaderSpanIncludingInitializer().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, constructorDeclaration).ConfigureAwait(false);
            }
        }
コード例 #5
0
        private static void ComputeRefactoring(RefactoringContext context, BlockSyntax block)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.RemoveBraces,
                    RefactoringIdentifiers.RemoveBracesFromIfElse) &&
                CanRefactor(context, block))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBraces))
                {
                    context.RegisterRefactoring(
                        "Remove braces",
                        cancellationToken => RefactorAsync(context.Document, block, cancellationToken));
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromIfElse))
                {
                    IfStatementSyntax topmostIf = GetTopmostIf(block);

                    if (topmostIf?.Else != null &&
                        CanRefactorIfElse(block, topmostIf))
                    {
                        context.RegisterRefactoring(
                            "Remove braces from if-else",
                            cancellationToken =>
                        {
                            return(RemoveBracesFromIfElseElseRefactoring.RefactorAsync(
                                       context.Document,
                                       topmostIf,
                                       cancellationToken));
                        });
                    }
                }
            }
        }
コード例 #6
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ReturnStatementSyntax returnStatement)
        {
            if (context.SupportsSemanticModel)
            {
                if (returnStatement.Expression != null)
                {
                    if (context.IsAnyRefactoringEnabled(
                            RefactoringIdentifiers.AddBooleanComparison,
                            RefactoringIdentifiers.ChangeMemberTypeAccordingToReturnExpression,
                            RefactoringIdentifiers.AddCastExpression,
                            RefactoringIdentifiers.CallToMethod))
                    {
                        await ReturnExpressionRefactoring.ComputeRefactoringsAsync(context, returnStatement.Expression).ConfigureAwait(false);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.CreateConditionFromBooleanExpression))
                    {
                        await CreateConditionFromBooleanExpressionRefactoring.ComputeRefactoringAsync(context, returnStatement.Expression).ConfigureAwait(false);
                    }
                }
                else if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddDefaultValueToReturnStatement))
                {
                    await AddDefaultValueToReturnStatementRefactoring.ComputeRefactoringsAsync(context, returnStatement).ConfigureAwait(false);
                }
            }
        }
コード例 #7
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ReturnStatementSyntax returnStatement)
        {
            ExpressionSyntax expression = returnStatement.Expression;

            if (expression != null)
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.AddBooleanComparison,
                        RefactoringIdentifiers.ChangeMemberTypeAccordingToReturnExpression,
                        RefactoringIdentifiers.AddCastExpression,
                        RefactoringIdentifiers.CallToMethod))
                {
                    await ReturnExpressionRefactoring.ComputeRefactoringsAsync(context, expression).ConfigureAwait(false);
                }

                if (context.Span.IsBetweenSpans(returnStatement))
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfStatement))
                    {
                        var refactoring = new ReplaceReturnStatementWithIfStatementRefactoring();
                        await refactoring.ComputeRefactoringAsync(context, returnStatement).ConfigureAwait(false);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInElseClause))
                    {
                        WrapInElseClauseRefactoring.ComputeRefactoring(context, returnStatement);
                    }
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddDefaultValueToReturnStatement))
            {
                await AddDefaultValueToReturnStatementRefactoring.ComputeRefactoringsAsync(context, returnStatement).ConfigureAwait(false);
            }
        }
コード例 #8
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SwapStatementsInIfElse,
                    RefactoringIdentifiers.ReplaceIfElseWithAssignment,
                    RefactoringIdentifiers.ReplaceIfStatementWithReturnStatement,
                    RefactoringIdentifiers.ReplaceIfElseWithSwitch) &&
                IfElseChain.IsTopmostIf(ifStatement) &&
                context.Span.IsBetweenSpans(ifStatement))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfStatementWithReturnStatement))
                {
                    await ReplaceIfWithStatementRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithAssignment))
                {
                    ReplaceIfElseWithAssignmentRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse))
                {
                    SwapStatementInIfElseRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithSwitch))
                {
                    await ReplaceIfElseWithSwitchRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }
            }
        }
コード例 #9
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, CaseSwitchLabelSyntax caseLabel)
        {
            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod))
            {
                ExpressionSyntax value = caseLabel.Value;

                if (value?.Span.Contains(context.Span) == true)
                {
                    var switchStatement = caseLabel.Parent?.Parent as SwitchStatementSyntax;

                    if (switchStatement != null)
                    {
                        ExpressionSyntax expression = switchStatement.Expression;

                        if (expression?.IsMissing == false)
                        {
                            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

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

                            if (typeSymbol?.IsErrorType() == false)
                            {
                                ModifyExpressionRefactoring.ComputeRefactoring(context, value, typeSymbol, semanticModel);
                            }
                        }
                    }
                }
            }
        }
コード例 #10
0
        public static async Task ComputeRefactoringsAsync(
            RefactoringContext context,
            DeclarationExpressionSyntax declarationExpression)
        {
            if (declarationExpression.Type?.Span.Contains(context.Span) == true &&
                context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeExplicitTypeToVar,
                    RefactoringIdentifiers.ChangeVarToExplicitType))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                TypeAnalysis analysis = CSharpTypeAnalysis.AnalyzeType(declarationExpression, semanticModel, context.CancellationToken);

                if (analysis.IsExplicit)
                {
                    if (analysis.SupportsImplicit &&
                        context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar))
                    {
                        context.RegisterRefactoring(CodeActionFactory.ChangeTypeToVar(context.Document, declarationExpression.Type, equivalenceKey: RefactoringIdentifiers.ChangeExplicitTypeToVar));
                    }
                }
                else if (analysis.SupportsExplicit &&
                         context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType))
                {
                    TypeSyntax type = declarationExpression.Type;

                    var localSymbol = (ILocalSymbol)semanticModel.GetDeclaredSymbol(declarationExpression.Designation, context.CancellationToken);

                    ITypeSymbol typeSymbol = localSymbol.Type;

                    context.RegisterRefactoring(CodeActionFactory.ChangeType(context.Document, type, typeSymbol, semanticModel, equivalenceKey: RefactoringIdentifiers.ChangeVarToExplicitType));
                }
            }
        }
コード例 #11
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, AttributeArgumentListSyntax argumentList)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddParameterNameToArgument,
                    RefactoringIdentifiers.RemoveParameterNameFromArgument) &&
                !context.Span.IsEmpty &&
                context.SupportsSemanticModel)
            {
                List <AttributeArgumentSyntax> arguments = null;

                foreach (AttributeArgumentSyntax argument in argumentList.Arguments)
                {
                    if (argument.Expression != null && context.Span.Contains(argument.Expression.Span))
                    {
                        if (arguments == null)
                        {
                            arguments = new List <AttributeArgumentSyntax>();
                        }

                        arguments.Add(argument);
                    }
                }

                if (arguments?.Count > 0)
                {
                    await AddOrRemoveParameterNameAsync(context, argumentList, arguments.ToArray()).ConfigureAwait(false);
                }
            }
        }
コード例 #12
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterSyntax parameter)
        {
            if (!context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddIdentifierToParameter,
                    RefactoringIdentifiers.RenameParameterAccordingToTypeName))
            {
                return;
            }

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

            IParameterSymbol parameterSymbol = semanticModel.GetDeclaredSymbol(parameter, context.CancellationToken);

            if (parameterSymbol?.Type == null)
            {
                return;
            }

            if (parameter.Identifier.IsMissing)
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddIdentifierToParameter))
                {
                    TextSpan span = (parameter.Type != null)
                        ? TextSpan.FromBounds(parameter.Type.Span.End, parameter.Span.End)
                        : parameter.Span;

                    if (span.Contains(context.Span))
                    {
                        string name = NameGenerator.CreateName(parameterSymbol.Type, firstCharToLower: true);

                        if (!string.IsNullOrEmpty(name))
                        {
                            context.RegisterRefactoring(
                                $"Add identifier '{name}'",
                                cancellationToken => AddParameterNameToParameterAsync(context.Document, parameter, name, cancellationToken),
                                RefactoringIdentifiers.AddIdentifierToParameter);
                        }
                    }
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameParameterAccordingToTypeName) &&
                     parameter.Identifier.Span.Contains(context.Span))
            {
                string oldName = parameter.Identifier.ValueText;

                string newName = NameGenerator.Default.CreateUniqueParameterName(
                    oldName,
                    parameterSymbol,
                    semanticModel,
                    cancellationToken: context.CancellationToken);

                if (newName != null)
                {
                    context.RegisterRefactoring(
                        $"Rename '{oldName}' to '{newName}'",
                        cancellationToken => Renamer.RenameSymbolAsync(context.Solution, parameterSymbol, newName, default(OptionSet), cancellationToken),
                        RefactoringIdentifiers.RenameParameterAccordingToTypeName);
                }
            }
        }
コード例 #13
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ReturnStatementSyntax returnStatement)
        {
            ExpressionSyntax expression = returnStatement.Expression;

            if (expression != null)
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.ChangeMemberTypeAccordingToReturnExpression,
                        RefactoringIdentifiers.AddCastExpression,
                        RefactoringIdentifiers.CallToMethod))
                {
                    await ReturnExpressionRefactoring.ComputeRefactoringsAsync(context, expression).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfElse) &&
                    (context.Span.IsEmptyAndContainedInSpan(returnStatement.ReturnKeyword) ||
                     context.Span.IsBetweenSpans(returnStatement)))
                {
                    await ReplaceStatementWithIfStatementRefactoring.ReplaceReturnWithIfElse.ComputeRefactoringAsync(context, returnStatement).ConfigureAwait(false);
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddDefaultValueToReturnStatement))
            {
                await AddDefaultValueToReturnStatementRefactoring.ComputeRefactoringsAsync(context, returnStatement).ConfigureAwait(false);
            }
        }
コード例 #14
0
        public static void ComputeRefactorings(RefactoringContext context, MemberDeclarationSyntax member)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SplitAttributes,
                    RefactoringIdentifiers.MergeAttributes) &&
                !member.IsKind(SyntaxKind.NamespaceDeclaration) &&
                SyntaxListSelection <AttributeListSyntax> .TryCreate(member.GetAttributeLists(), context.Span, out SyntaxListSelection <AttributeListSyntax> selectedAttributeLists))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitAttributes) &&
                    selectedAttributeLists.Any(f => f.Attributes.Count > 1))
                {
                    context.RegisterRefactoring(
                        "Split attributes",
                        ct => SplitAsync(context.Document, member, selectedAttributeLists.ToArray(), ct),
                        RefactoringIdentifiers.SplitAttributes);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAttributes) &&
                    selectedAttributeLists.Count > 1)
                {
                    context.RegisterRefactoring(
                        "Merge attributes",
                        ct => MergeAsync(context.Document, member, selectedAttributeLists.ToArray(), ct),
                        RefactoringIdentifiers.MergeAttributes);
                }
            }
        }
コード例 #15
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForEachStatementSyntax forEachStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeTypeAccordingToExpression))
            {
                await ChangeTypeAccordingToExpressionAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeExplicitTypeToVar,
                    RefactoringIdentifiers.ChangeVarToExplicitType))
            {
                await ChangeTypeAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameIdentifierAccordingToTypeName))
            {
                await RenameIdentifierAccordingToTypeNameAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor) &&
                context.Span.IsEmpty &&
                ReplaceForEachWithForRefactoring.CanRefactor(forEachStatement, await context.GetSemanticModelAsync().ConfigureAwait(false), context.CancellationToken))
            {
                context.RegisterRefactoring(
                    "Replace foreach with for",
                    cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, cancellationToken));
            }
        }
コード例 #16
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ArgumentSyntax argument)
        {
            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod))
            {
                ExpressionSyntax expression = argument.Expression;

                if (expression?.IsMissing == false)
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(expression).ConvertedType;

                    if (typeSymbol?.IsErrorType() == false)
                    {
                        IEnumerable <ITypeSymbol> newTypes = DetermineParameterTypes(argument, semanticModel, context.CancellationToken)
                                                             .Where(f => !typeSymbol.Equals(f));

                        ModifyExpressionRefactoring.ComputeRefactoring(context, expression, newTypes, semanticModel);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodGroupWithLambda))
            {
                await ReplaceMethodGroupWithLambdaRefactoring.ComputeRefactoringAsync(context, argument).ConfigureAwait(false);
            }
        }
コード例 #17
0
        public static void ComputeRefactoring(RefactoringContext context, StatementSyntax statement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddBraces,
                    RefactoringIdentifiers.AddBracesToIfElse) &&
                CanRefactor(context, statement))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBraces))
                {
                    RegisterRefactoring(context, statement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToIfElse))
                {
                    IfStatementSyntax topmostIf = GetTopmostIf(statement);

                    if (topmostIf?.Else != null &&
                        GetEmbeddedStatements(topmostIf).Any(f => f != statement))
                    {
                        context.RegisterRefactoring(
                            "Add braces to if-else",
                            cancellationToken =>
                        {
                            return(AddBracesToIfElseRefactoring.RefactorAsync(
                                       context.Document,
                                       topmostIf,
                                       cancellationToken));
                        });
                    }
                }
            }
        }
コード例 #18
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxNode node)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.WrapInRegion,
                    RefactoringIdentifiers.WrapInIfDirective))
            {
                //XPERF:
                TextLineCollectionSelection selectedLines = await SelectedLinesRefactoring.GetSelectedLinesAsync(context).ConfigureAwait(false);

                if (selectedLines != null)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInRegion))
                    {
                        context.RegisterRefactoring(
                            "Wrap in region",
                            ct => WrapInRegionRefactoring.Instance.RefactorAsync(context.Document, selectedLines, ct));
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInIfDirective))
                    {
                        context.RegisterRefactoring(
                            "Wrap in #if",
                            ct => WrapInIfDirectiveRefactoring.Instance.RefactorAsync(context.Document, selectedLines, ct));
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveEmptyLines) &&
                await RemoveEmptyLinesRefactoring.CanRefactorAsync(context, node).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Remove empty lines",
                    ct => RemoveEmptyLinesRefactoring.RefactorAsync(context.Document, context.Span, ct));
            }
        }
コード例 #19
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));
            }
        }
コード例 #20
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxToken token)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringDescriptors.JoinStringExpressions,
                    RefactoringDescriptors.UseStringBuilderInsteadOfConcatenation) &&
                context.Span.IsEmptyAndContainedInSpan(token) &&
                token.IsParentKind(SyntaxKind.AddExpression))
            {
                var addExpression = (BinaryExpressionSyntax)token.Parent;

                while (addExpression.IsParentKind(SyntaxKind.AddExpression))
                {
                    addExpression = (BinaryExpressionSyntax)addExpression.Parent;
                }

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

                StringConcatenationExpressionInfo concatenationInfo = SyntaxInfo.StringConcatenationExpressionInfo(addExpression, semanticModel, context.CancellationToken);

                if (concatenationInfo.Success)
                {
                    if (context.IsRefactoringEnabled(RefactoringDescriptors.JoinStringExpressions))
                    {
                        JoinStringExpressionsRefactoring.ComputeRefactoring(context, concatenationInfo);
                    }

                    if (context.IsRefactoringEnabled(RefactoringDescriptors.UseStringBuilderInsteadOfConcatenation))
                    {
                        UseStringBuilderInsteadOfConcatenationRefactoring.ComputeRefactoring(context, concatenationInfo);
                    }
                }
            }
        }
コード例 #21
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, EventDeclarationSyntax eventDeclaration)
        {
            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic, RefactoringIdentifiers.MarkAllMembersAsStatic) &&
                eventDeclaration.Span.Contains(context.Span) &&
                MarkMemberAsStaticRefactoring.CanRefactor(eventDeclaration))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic))
                {
                    context.RegisterRefactoring(
                        "Mark event as static",
                        cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, eventDeclaration, cancellationToken));
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkAllMembersAsStatic))
                {
                    MarkAllMembersAsStaticRefactoring.RegisterRefactoring(context, (ClassDeclarationSyntax)eventDeclaration.Parent);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                eventDeclaration.HeaderSpan().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, eventDeclaration).ConfigureAwait(false);
            }
        }
コード例 #22
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (ifStatement.IsTopmostIf() &&
                (context.Span.IsEmptyAndContainedInSpan(ifStatement.IfKeyword) || context.Span.IsBetweenSpans(ifStatement)))
            {
                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(ifStatement, options, semanticModel, context.CancellationToken))
                    {
                        context.RegisterRefactoring(
                            refactoring.Title,
                            cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken));
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse))
                {
                    SwapStatementInIfElseRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithSwitch))
                {
                    await ReplaceIfElseWithSwitchRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitIfStatement))
                {
                    SplitIfStatementRefactoring.ComputeRefactoring(context, ifStatement);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReduceIfNesting) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(ifStatement.IfKeyword))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (ReduceIfNestingRefactoring.IsFixable(
                        ifStatement,
                        semanticModel,
                        semanticModel.GetTypeByMetadataName(MetadataNames.System_Threading_Tasks_Task),
                        context.CancellationToken,
                        topLevelOnly: false))
                {
                    context.RegisterRefactoring(
                        "Reduce if nesting",
                        cancellationToken => ReduceIfNestingRefactoring.RefactorAsync(context.Document, ifStatement, context.CancellationToken));
                }
            }
        }
コード例 #23
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocationExpression)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ReplaceMethodInvocationWithElementAccess,
                    RefactoringIdentifiers.ReplaceAnyWithAllOrAllWithAny,
                    RefactoringIdentifiers.CallConfigureAwait) &&
                invocationExpression.Expression != null &&
                invocationExpression.ArgumentList != null &&
                context.SupportsSemanticModel)
            {
                ExpressionSyntax expression = invocationExpression.Expression;

                if (expression.IsKind(SyntaxKind.SimpleMemberAccessExpression) &&
                    ((MemberAccessExpressionSyntax)expression).Name?.Span.Contains(context.Span) == true)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodInvocationWithElementAccess))
                    {
                        await ReplaceMethodInvocationWithElementAccessRefactoring.ComputeRefactoringsAsync(context, invocationExpression).ConfigureAwait(false);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceAnyWithAllOrAllWithAny))
                    {
                        await ReplaceAnyWithAllOrAllWithAnyRefactoring.ComputeRefactoringAsync(context, invocationExpression).ConfigureAwait(false);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStringFormatWithInterpolatedString) &&
                context.SupportsCSharp6)
            {
                await ReplaceStringFormatWithInterpolatedStringRefactoring.ComputeRefactoringsAsync(context, invocationExpression).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceHasFlagWithBitwiseOperation) &&
                context.SupportsSemanticModel)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (ReplaceHasFlagWithBitwiseOperationRefactoring.CanRefactor(invocationExpression, semanticModel, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        ReplaceHasFlagWithBitwiseOperationRefactoring.Title,
                        cancellationToken =>
                    {
                        return(ReplaceHasFlagWithBitwiseOperationRefactoring.RefactorAsync(
                                   context.Document,
                                   invocationExpression,
                                   cancellationToken));
                    });
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InlineMethod) &&
                context.SupportsSemanticModel)
            {
                await InlineMethodRefactoring.ComputeRefactoringsAsync(context, invocationExpression).ConfigureAwait(false);
            }
        }
コード例 #24
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterSyntax parameter)
        {
            if (!context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddParameterNameToParameter,
                    RefactoringIdentifiers.RenameParameterAccordingToTypeName))
            {
                return;
            }

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

            IParameterSymbol parameterSymbol = semanticModel.GetDeclaredSymbol(parameter, context.CancellationToken);

            if (parameterSymbol?.Type == null)
            {
                return;
            }

            if (parameter.Identifier.IsMissing)
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddParameterNameToParameter))
                {
                    TextSpan span = (parameter.Type != null)
                        ? TextSpan.FromBounds(parameter.Type.Span.End, parameter.Span.End)
                        : parameter.Span;

                    if (span.Contains(context.Span))
                    {
                        string name = SyntaxUtility.CreateIdentifier(parameterSymbol.Type, firstCharToLower: true);

                        if (!string.IsNullOrEmpty(name))
                        {
                            context.RegisterRefactoring(
                                $"Add parameter name '{name}'",
                                cancellationToken => AddParameterNameToParameterAsync(context.Document, parameter, name, cancellationToken));
                        }
                    }
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameParameterAccordingToTypeName) &&
                     parameter.Identifier.Span.Contains(context.Span))
            {
                string name    = parameter.Identifier.ValueText;
                string newName = SyntaxUtility.CreateIdentifier(parameterSymbol.Type, firstCharToLower: true);

                if (!string.IsNullOrEmpty(newName) &&
                    !string.Equals(name, newName, StringComparison.Ordinal))
                {
                    ISymbol symbol = semanticModel.GetDeclaredSymbol(parameter, context.CancellationToken);

                    context.RegisterRefactoring(
                        $"Rename parameter to '{newName}'",
                        cancellationToken => SymbolRenamer.RenameAsync(context.Document, symbol, newName, cancellationToken));
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(
            RefactoringContext context,
            DeclarationExpressionSyntax declarationExpression)
        {
            if (declarationExpression.Type?.Span.Contains(context.Span) == true &&
                context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeExplicitTypeToVar,
                    RefactoringIdentifiers.ChangeVarToExplicitType))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                switch (CSharpAnalysis.AnalyzeType(
                            declarationExpression,
                            semanticModel,
                            context.CancellationToken))
                {
                case TypeAnalysisResult.Explicit:
                case TypeAnalysisResult.ExplicitButShouldBeImplicit:
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar))
                    {
                        context.RegisterRefactoring(
                            "Change type to 'var'",
                            cancellationToken =>
                            {
                                return(ChangeTypeRefactoring.ChangeTypeToVarAsync(
                                           context.Document,
                                           declarationExpression.Type,
                                           cancellationToken));
                            });
                    }

                    break;
                }

                case TypeAnalysisResult.Implicit:
                case TypeAnalysisResult.ImplicitButShouldBeExplicit:
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType))
                    {
                        TypeSyntax type = declarationExpression.Type;

                        var localSymbol = semanticModel.GetDeclaredSymbol(declarationExpression.Designation, context.CancellationToken) as ILocalSymbol;

                        ITypeSymbol typeSymbol = localSymbol.Type;

                        context.RegisterRefactoring(
                            $"Change type to '{SymbolDisplay.GetMinimalDisplayString(typeSymbol, type.Span.Start, semanticModel)}'",
                            cancellationToken => ChangeTypeRefactoring.ChangeTypeAsync(context.Document, type, typeSymbol, cancellationToken));
                    }

                    break;
                }
                }
            }
        }
コード例 #26
0
 public static async Task ComputeRefactoringsAsync(RefactoringContext context, VariableDeclarationSyntax variableDeclaration)
 {
     if (variableDeclaration.Type?.Span.Contains(context.Span) == true &&
         context.IsAnyRefactoringEnabled(
             RefactoringIdentifiers.ChangeExplicitTypeToVar,
             RefactoringIdentifiers.ChangeVarToExplicitType))
     {
         await ChangeTypeAsync(context, variableDeclaration).ConfigureAwait(false);
     }
 }
コード例 #27
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForEachStatementSyntax forEachStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeTypeAccordingToExpression))
            {
                await ChangeTypeAccordingToExpressionAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeExplicitTypeToVar,
                    RefactoringIdentifiers.ChangeVarToExplicitType))
            {
                await ChangeTypeAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameIdentifierAccordingToTypeName))
            {
                await RenameIdentifierAccordingToTypeNameAsync(context, forEachStatement).ConfigureAwait(false);
            }

            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor, RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forEachStatement))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (ReplaceForEachWithForRefactoring.CanRefactor(forEachStatement, semanticModel, context.CancellationToken))
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor))
                    {
                        context.RegisterRefactoring(
                            "Replace foreach with for",
                            cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, reverseLoop: false, semanticModel: semanticModel, cancellationToken: cancellationToken));
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop))
                    {
                        context.RegisterRefactoring(
                            "Replace foreach with for and reverse loop",
                            cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, reverseLoop: true, semanticModel: semanticModel, cancellationToken: cancellationToken));
                    }
                }
            }
        }
コード例 #28
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, EventFieldDeclarationSyntax eventFieldDeclaration)
        {
            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic, RefactoringIdentifiers.MarkAllMembersAsStatic) &&
                eventFieldDeclaration.Span.Contains(context.Span) &&
                MarkMemberAsStaticRefactoring.CanRefactor(eventFieldDeclaration))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic))
                {
                    context.RegisterRefactoring(
                        "Mark event as static",
                        cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, eventFieldDeclaration, cancellationToken));
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkAllMembersAsStatic))
                {
                    MarkAllMembersAsStaticRefactoring.RegisterRefactoring(context, (ClassDeclarationSyntax)eventFieldDeclaration.Parent);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkContainingClassAsAbstract) &&
                eventFieldDeclaration.Span.Contains(context.Span))
            {
                MarkContainingClassAsAbstractRefactoring.ComputeRefactoring(context, eventFieldDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateEventInvokingMethod))
            {
                await GenerateOnEventMethodRefactoring.ComputeRefactoringAsync(context, eventFieldDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandEvent) &&
                eventFieldDeclaration.Span.Contains(context.Span) &&
                ExpandEventRefactoring.CanRefactor(eventFieldDeclaration))
            {
                context.RegisterRefactoring(
                    "Expand event",
                    cancellationToken =>
                {
                    return(ExpandEventRefactoring.RefactorAsync(
                               context.Document,
                               eventFieldDeclaration,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                eventFieldDeclaration.Span.Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, eventFieldDeclaration).ConfigureAwait(false);
            }
        }
コード例 #29
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxNode node)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.WrapInRegion,
                    RefactoringIdentifiers.WrapInIfDirective))
            {
                SelectedTextLineCollection lines = await SelectedLinesRefactoring.GetSelectedLinesAsync(context, node).ConfigureAwait(false);

                if (lines?.Any() == true)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInRegion))
                    {
                        context.RegisterRefactoring(
                            "Wrap in region",
                            cancellationToken =>
                        {
                            var refactoring = new WrapInRegionRefactoring();

                            return(refactoring.RefactorAsync(context.Document, lines, cancellationToken));
                        });
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInIfDirective))
                    {
                        context.RegisterRefactoring(
                            "Wrap in #if",
                            cancellationToken =>
                        {
                            var refactoring = new WrapInIfDirectiveRefactoring();

                            return(refactoring.RefactorAsync(context.Document, lines, cancellationToken));
                        });
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveEmptyLines) &&
                await RemoveEmptyLinesRefactoring.CanRefactorAsync(context, node).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Remove empty lines",
                    cancellationToken =>
                {
                    return(RemoveEmptyLinesRefactoring.RefactorAsync(
                               context.Document,
                               node,
                               context.Span,
                               cancellationToken));
                });
            }
        }
コード例 #30
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterListSyntax parameterList)
        {
            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            if (parameters.Count == 0)
            {
                return;
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateParameter))
            {
                var refactoring = new DuplicateParameterRefactoring(parameterList);
                refactoring.ComputeRefactoring(context);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckParameterForNull))
            {
                await CheckParameterForNullRefactoring.ComputeRefactoringAsync(context, parameterList).ConfigureAwait(false);
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.IntroduceAndInitializeField,
                    RefactoringIdentifiers.IntroduceAndInitializeProperty))
            {
                IntroduceAndInitializeRefactoring.ComputeRefactoring(context, parameterList);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatParameterList) &&
                (context.Span.IsEmpty || context.Span.IsBetweenSpans(parameterList)))
            {
                if (parameterList.IsSingleLine())
                {
                    if (parameters.Count > 1)
                    {
                        context.RegisterRefactoring(
                            "Format each parameter on a separate line",
                            cancellationToken => FormatParameterListRefactoring.FormatEachParameterOnSeparateLineAsync(context.Document, parameterList, cancellationToken));
                    }
                }
                else
                {
                    string title = parameters.Count == 1
                        ? "Format parameter on a single line"
                        : "Format all parameters on a single line";

                    context.RegisterRefactoring(
                        title,
                        cancellationToken => FormatParameterListRefactoring.FormatAllParametersOnSingleLineAsync(context.Document, parameterList, cancellationToken));
                }
            }
        }