Ejemplo n.º 1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConstructorDeclarationSyntax constructorDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic) &&
                constructorDeclaration.Span.Contains(context.Span) &&
                MarkMemberAsStaticRefactoring.CanRefactor(constructorDeclaration))
            {
                context.RegisterRefactoring(
                    "Mark constructor as static",
                    cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, constructorDeclaration, cancellationToken));
            }

            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);
            }
        }
Ejemplo n.º 2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IndexerDeclarationSyntax indexerDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                indexerDeclaration.AccessorList?.Span.Contains(context.Span) == true &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(indexerDeclaration))
            {
                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, indexerDeclaration, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span) &&
                MakeMemberAbstractRefactoring.CanRefactor(indexerDeclaration))
            {
                context.RegisterRefactoring(
                    "Make indexer abstract",
                    cancellationToken => MakeMemberAbstractRefactoring.RefactorAsync(context.Document, indexerDeclaration, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, indexerDeclaration).ConfigureAwait(false);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, LocalFunctionStatementSyntax localFunctionStatement)
        {
            if (localFunctionStatement.IsParentKind(SyntaxKind.Block))
            {
                BlockSyntax body = localFunctionStatement.Body;

                if (body != null)
                {
                    if (body.OpenBraceToken.Span.Contains(context.Span) ||
                        body.CloseBraceToken.Span.Contains(context.Span))
                    {
                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveMember))
                        {
                            context.RegisterRefactoring(
                                "Remove local function",
                                cancellationToken => context.Document.RemoveStatementAsync(localFunctionStatement, cancellationToken),
                                RefactoringIdentifiers.RemoveMember);
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember))
                        {
                            context.RegisterRefactoring(
                                "Duplicate local function",
                                cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken),
                                RefactoringIdentifiers.DuplicateMember);
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.CommentOutMember))
                        {
                            CommentOutRefactoring.RegisterRefactoring(context, localFunctionStatement);
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMethodReturnTypeToVoid) &&
                context.Span.IsEmptyAndContainedInSpan(localFunctionStatement))
            {
                await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, localFunctionStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(localFunctionStatement, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MoveUnsafeContextToContainingDeclaration))
            {
                MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, AccessorDeclarationSyntax accessor)
        {
            BlockSyntax body = accessor.Body;

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatAccessorBraces) &&
                body?.Span.Contains(context.Span) == true &&
                !body.OpenBraceToken.IsMissing &&
                !body.CloseBraceToken.IsMissing)
            {
                if (body.IsSingleLine())
                {
                    context.RegisterRefactoring(
                        "Format braces on multiple lines",
                        cancellationToken => CSharpFormatter.ToMultiLineAsync(context.Document, accessor, cancellationToken));
                }
                else
                {
                    SyntaxList <StatementSyntax> statements = body.Statements;

                    if (statements.Count == 1 &&
                        statements[0].IsSingleLine())
                    {
                        context.RegisterRefactoring(
                            "Format braces on a single line",
                            cancellationToken => CSharpFormatter.ToSingleLineAsync(context.Document, accessor, cancellationToken));
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(accessor) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(accessor))
            {
                SyntaxNode node = accessor;

                var accessorList = accessor.Parent as AccessorListSyntax;

                if (accessorList != null)
                {
                    SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

                    if (accessors.Count == 1 &&
                        accessors.First().IsKind(SyntaxKind.GetAccessorDeclaration))
                    {
                        var parent = accessorList.Parent as MemberDeclarationSyntax;

                        if (parent != null)
                        {
                            node = parent;
                        }
                    }
                }

                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, node, cancellationToken));
            }
        }
Ejemplo n.º 5
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, MethodDeclarationSyntax methodDeclaration)
        {
            if (methodDeclaration.Span.Contains(context.Span))
            {
                await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, methodDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, methodDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodWithProperty) &&
                methodDeclaration.HeaderSpan().Contains(context.Span) &&
                ReplaceMethodWithPropertyRefactoring.CanRefactor(methodDeclaration))
            {
                context.RegisterRefactoring(
                    $"Replace '{methodDeclaration.Identifier.ValueText}' with property",
                    cancellationToken => ReplaceMethodWithPropertyRefactoring.RefactorAsync(context.Document, methodDeclaration, cancellationToken));
            }

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

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                methodDeclaration.HeaderSpan().Contains(context.Span) &&
                MakeMemberAbstractRefactoring.CanRefactor(methodDeclaration))
            {
                context.RegisterRefactoring(
                    "Make method abstract",
                    cancellationToken => MakeMemberAbstractRefactoring.RefactorAsync(context.Document, methodDeclaration, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) &&
                methodDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakeMemberVirtualRefactoring.ComputeRefactoring(context, methodDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                methodDeclaration.HeaderSpan().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, methodDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameMethodAccordingToTypeName))
            {
                await RenameMethodAccoringToTypeNameAsync(context, methodDeclaration).ConfigureAwait(false);
            }
        }
Ejemplo n.º 6
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, LocalFunctionStatementSyntax localFunctionStatement)
        {
            if (localFunctionStatement.IsParentKind(SyntaxKind.Block))
            {
                BlockSyntax body = localFunctionStatement.Body;

                if (body != null)
                {
                    if (body.OpenBraceToken.Span.Contains(context.Span) ||
                        body.CloseBraceToken.Span.Contains(context.Span))
                    {
                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveMember))
                        {
                            context.RegisterRefactoring(
                                "Remove local function",
                                cancellationToken => context.Document.RemoveStatementAsync(localFunctionStatement, cancellationToken));
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember))
                        {
                            context.RegisterRefactoring(
                                "Duplicate local function",
                                cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken));
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.CommentOutMember))
                        {
                            CommentOutRefactoring.RegisterRefactoring(context, localFunctionStatement);
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }

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

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield) &&
                localFunctionStatement.Identifier.Span.Contains(context.Span))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, localFunctionStatement, semanticModel);
            }
        }
Ejemplo n.º 7
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IndexerDeclarationSyntax indexerDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(indexerDeclaration, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, indexerDeclaration, ct),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                context.Span.IsEmptyAndContainedInSpan(indexerDeclaration.ThisKeyword))
            {
                MakeIndexerAbstractRefactoring.ComputeRefactoring(context, indexerDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakeIndexerVirtualRefactoring.ComputeRefactoring(context, indexerDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span) &&
                !indexerDeclaration.HasDocumentationComment())
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoring(context, indexerDeclaration, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddParameterToInterfaceMember) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(indexerDeclaration.ThisKeyword))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                foreach (CodeAction codeAction in AddParameterToInterfaceMemberRefactoring.ComputeRefactoringForImplicitImplementation(
                             new CommonFixContext(context.Document, RefactoringIdentifiers.AddParameterToInterfaceMember, semanticModel, context.CancellationToken),
                             indexerDeclaration))
                {
                    context.RegisterRefactoring(codeAction);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(indexerDeclaration.ThisKeyword))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddMemberToInterfaceRefactoring.ComputeRefactoring(context, indexerDeclaration, semanticModel);
            }
        }
Ejemplo n.º 8
0
 public static void ComputeRefactorings(RefactoringContext context, AccessorDeclarationSyntax accessor)
 {
     if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
         context.SupportsCSharp6 &&
         UseExpressionBodiedMemberRefactoring.CanRefactor(accessor, context.Span))
     {
         context.RegisterRefactoring(
             UseExpressionBodiedMemberRefactoring.Title,
             ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, accessor, ct),
             RefactoringIdentifiers.UseExpressionBodiedMember);
     }
 }
Ejemplo n.º 9
0
 private static void ComputeRefactorings(RefactoringContext context, ConversionOperatorDeclarationSyntax operatorDeclaration)
 {
     if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
         context.SupportsCSharp6 &&
         UseExpressionBodiedMemberRefactoring.CanRefactor(operatorDeclaration, context.Span))
     {
         context.RegisterRefactoring(
             UseExpressionBodiedMemberRefactoring.Title,
             cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, operatorDeclaration, cancellationToken),
             RefactoringIdentifiers.UseExpressionBodiedMember);
     }
 }
 private static void ComputeRefactorings(RefactoringContext context, OperatorDeclarationSyntax operatorDeclaration)
 {
     if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
         operatorDeclaration.Body?.Span.Contains(context.Span) == true &&
         context.SupportsCSharp6 &&
         UseExpressionBodiedMemberRefactoring.CanRefactor(operatorDeclaration))
     {
         context.RegisterRefactoring(
             "Use expression-bodied member",
             cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, operatorDeclaration, cancellationToken));
     }
 }
        public static void ComputeRefactorings(RefactoringContext context, LocalFunctionStatementSyntax localFunctionStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                localFunctionStatement.Body?.Span.Contains(context.Span) == true &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(localFunctionStatement))
            {
                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken));
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, AccessorDeclarationSyntax accessor)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatAccessorBraces))
            {
                BlockSyntax body = accessor.Body;

                if (body?.Span.Contains(context.Span) == true &&
                    !body.OpenBraceToken.IsMissing &&
                    !body.CloseBraceToken.IsMissing)
                {
                    if (body.IsSingleLine())
                    {
                        if (accessor.Parent?.IsMultiLine() == true)
                        {
                            context.RegisterRefactoring(
                                "Format braces on separate lines",
                                ct => SyntaxFormatter.ToMultiLineAsync(context.Document, accessor, ct),
                                RefactoringIdentifiers.FormatAccessorBraces);
                        }
                    }
                    else if (body.Statements.SingleOrDefault(shouldThrow: false)?.IsSingleLine() == true &&
                             accessor.DescendantTrivia(accessor.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.RegisterRefactoring(
                            "Format braces on a single line",
                            ct => SyntaxFormatter.ToSingleLineAsync(context.Document, accessor, ct),
                            RefactoringIdentifiers.FormatAccessorBraces);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(accessor, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, accessor, ct),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConstructorDeclarationSyntax constructorDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(constructorDeclaration, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, constructorDeclaration, cancellationToken),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                constructorDeclaration.HeaderSpan().Contains(context.Span) &&
                !constructorDeclaration.HasDocumentationComment())
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoring(context, constructorDeclaration, semanticModel);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, LocalFunctionStatementSyntax localFunctionStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }

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

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield) &&
                localFunctionStatement.Identifier.Span.Contains(context.Span))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, localFunctionStatement, semanticModel);
            }
        }
        private static string GetRefactoringId(MemberDeclarationSyntax memberDeclaration)
        {
            switch (memberDeclaration)
            {
            case MethodDeclarationSyntax methodDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = methodDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(methodDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case PropertyDeclarationSyntax propertyDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(propertyDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case IndexerDeclarationSyntax indexerDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = indexerDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(indexerDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case OperatorDeclarationSyntax operatorDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = operatorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(operatorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case ConversionOperatorDeclarationSyntax conversionOperatorDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = conversionOperatorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(conversionOperatorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case ConstructorDeclarationSyntax constructorDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = constructorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(constructorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case DestructorDeclarationSyntax destructorDeclaration:
            {
                ArrowExpressionClauseSyntax expressionBody = destructorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(destructorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }
            }

            return(null);
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, PropertyDeclarationSyntax propertyDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplacePropertyWithMethod) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                ReplacePropertyWithMethodRefactoring.ComputeRefactoring(context, propertyDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemovePropertyInitializer) &&
                RemovePropertyInitializerRefactoring.CanRefactor(context, propertyDeclaration))
            {
                context.RegisterRefactoring(
                    "Remove property initializer",
                    cancellationToken => RemovePropertyInitializerRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken),
                    RefactoringIdentifiers.RemovePropertyInitializer);
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ExpandProperty,
                    RefactoringIdentifiers.ExpandPropertyAndAddBackingField) &&
                propertyDeclaration.Span.Contains(context.Span) &&
                ExpandPropertyRefactoring.CanRefactor(propertyDeclaration))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandProperty))
                {
                    context.RegisterRefactoring(
                        "Expand property",
                        cancellationToken => ExpandPropertyRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken),
                        RefactoringIdentifiers.ExpandProperty);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandPropertyAndAddBackingField))
                {
                    context.RegisterRefactoring(
                        "Expand property and add backing field",
                        cancellationToken => ExpandPropertyAndAddBackingFieldRefactoring.RefactorAsync(context.Document, propertyDeclaration, context.Settings.PrefixFieldIdentifierWithUnderscore, cancellationToken),
                        RefactoringIdentifiers.ExpandPropertyAndAddBackingField);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(propertyDeclaration, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, propertyDeclaration, ct),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.NotifyWhenPropertyChange))
            {
                await NotifyWhenPropertyChangeRefactoring.ComputeRefactoringAsync(context, propertyDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakePropertyAbstractRefactoring.ComputeRefactoring(context, propertyDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) &&
                context.Span.IsEmptyAndContainedInSpan(propertyDeclaration.Identifier))
            {
                MakePropertyVirtualRefactoring.ComputeRefactoring(context, propertyDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span) &&
                !propertyDeclaration.HasDocumentationComment())
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoring(context, propertyDeclaration, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenamePropertyAccordingToTypeName))
            {
                await RenamePropertyAccodingToTypeName(context, propertyDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(propertyDeclaration.Identifier))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddMemberToInterfaceRefactoring.ComputeRefactoring(context, propertyDeclaration, semanticModel);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, PropertyDeclarationSyntax propertyDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic) &&
                propertyDeclaration.Span.Contains(context.Span) &&
                MarkMemberAsStaticRefactoring.CanRefactor(propertyDeclaration))
            {
                context.RegisterRefactoring(
                    "Mark property as static",
                    cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));

                MarkAllMembersAsStaticRefactoring.RegisterRefactoring(context, (ClassDeclarationSyntax)propertyDeclaration.Parent);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplacePropertyWithMethod) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span) &&
                ReplacePropertyWithMethodRefactoring.CanRefactor(context, propertyDeclaration))
            {
                string propertyName = propertyDeclaration.Identifier.ValueText;

                string title = $"Replace '{propertyName}' with method";

                if (propertyDeclaration.AccessorList.Accessors.Count > 1)
                {
                    title += "s";
                }

                context.RegisterRefactoring(
                    title,
                    cancellationToken => ReplacePropertyWithMethodRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));
            }

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

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemovePropertyInitializer) &&
                RemovePropertyInitializerRefactoring.CanRefactor(context, propertyDeclaration))
            {
                context.RegisterRefactoring(
                    "Remove property initializer",
                    cancellationToken => RemovePropertyInitializerRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));
            }

            if (context.SupportsSemanticModel)
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.ExpandProperty,
                        RefactoringIdentifiers.ExpandPropertyAndAddBackingField) &&
                    propertyDeclaration.Span.Contains(context.Span) &&
                    ExpandPropertyRefactoring.CanRefactor(propertyDeclaration))
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandProperty))
                    {
                        context.RegisterRefactoring(
                            "Expand property",
                            cancellationToken => ExpandPropertyRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandPropertyAndAddBackingField))
                    {
                        context.RegisterRefactoring(
                            "Expand property and add backing field",
                            cancellationToken => ExpandPropertyAndAddBackingFieldRefactoring.RefactorAsync(context.Document, propertyDeclaration, context.Settings.PrefixFieldIdentifierWithUnderscore, cancellationToken));
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.NotifyPropertyChanged) &&
                    await NotifyPropertyChangedRefactoring.CanRefactorAsync(context, propertyDeclaration).ConfigureAwait(false))
                {
                    context.RegisterRefactoring(
                        "Notify property changed",
                        cancellationToken =>
                    {
                        return(NotifyPropertyChangedRefactoring.RefactorAsync(
                                   context.Document,
                                   propertyDeclaration,
                                   context.SupportsCSharp6,
                                   cancellationToken));
                    });
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span) &&
                MakeMemberAbstractRefactoring.CanRefactor(propertyDeclaration))
            {
                context.RegisterRefactoring(
                    "Make property abstract",
                    cancellationToken => MakeMemberAbstractRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, propertyDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenamePropertyAccordingToTypeName) &&
                context.SupportsSemanticModel &&
                propertyDeclaration.Type != null &&
                propertyDeclaration.Identifier.Span.Contains(context.Span))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ITypeSymbol typeSymbol = semanticModel
                                         .GetTypeInfo(propertyDeclaration.Type, context.CancellationToken)
                                         .Type;

                if (typeSymbol?.IsErrorType() == false)
                {
                    string newName = SyntaxUtility.CreateIdentifier(typeSymbol);

                    if (!string.IsNullOrEmpty(newName))
                    {
                        newName = TextUtility.FirstCharToUpper(newName);

                        if (!string.Equals(newName, propertyDeclaration.Identifier.ValueText, StringComparison.Ordinal))
                        {
                            ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken);

                            context.RegisterRefactoring(
                                $"Rename property to '{newName}'",
                                cancellationToken => SymbolRenamer.RenameAsync(context.Document, symbol, newName, cancellationToken));
                        }
                    }
                }
            }
        }
Ejemplo n.º 18
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, MethodDeclarationSyntax methodDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMethodReturnTypeToVoid) &&
                context.Span.IsEmptyAndContainedInSpan(methodDeclaration))
            {
                await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, methodDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, methodDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceMethodWithProperty) &&
                methodDeclaration.HeaderSpan().Contains(context.Span) &&
                ReplaceMethodWithPropertyRefactoring.CanRefactor(methodDeclaration))
            {
                context.RegisterRefactoring(
                    $"Replace '{methodDeclaration.Identifier.ValueText}' with property",
                    cancellationToken => ReplaceMethodWithPropertyRefactoring.RefactorAsync(context.Document, methodDeclaration, cancellationToken),
                    RefactoringIdentifiers.ReplaceMethodWithProperty);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(methodDeclaration, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, methodDeclaration, cancellationToken),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                context.Span.IsEmptyAndContainedInSpan(methodDeclaration.Identifier))
            {
                MakeMethodAbstractRefactoring.ComputeRefactoring(context, methodDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) &&
                methodDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakeMethodVirtualRefactoring.ComputeRefactoring(context, methodDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                methodDeclaration.HeaderSpan().Contains(context.Span) &&
                !methodDeclaration.HasDocumentationComment())
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoring(context, methodDeclaration, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameMethodAccordingToTypeName))
            {
                await RenameMethodAccoringToTypeNameAsync(context, methodDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddParameterToInterfaceMember) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(methodDeclaration.Identifier))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                foreach (CodeAction codeAction in AddParameterToInterfaceMemberRefactoring.ComputeRefactoringForImplicitImplementation(
                             new CommonFixContext(context.Document, RefactoringIdentifiers.AddParameterToInterfaceMember, semanticModel, context.CancellationToken),
                             methodDeclaration))
                {
                    context.RegisterRefactoring(codeAction);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(methodDeclaration.Identifier))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddMemberToInterfaceRefactoring.ComputeRefactoring(context, methodDeclaration, semanticModel);
            }
        }
Ejemplo n.º 19
0
        private static string GetRefactoringId(MemberDeclarationSyntax memberDeclaration)
        {
            switch (memberDeclaration.Kind())
            {
            case SyntaxKind.MethodDeclaration:
            {
                var methodDeclaration = (MethodDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = methodDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(methodDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case SyntaxKind.PropertyDeclaration:
            {
                var propertyDeclaration = (PropertyDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(propertyDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case SyntaxKind.IndexerDeclaration:
            {
                var indexerDeclaration = (IndexerDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = indexerDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(indexerDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case SyntaxKind.OperatorDeclaration:
            {
                var operatorDeclaration = (OperatorDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = operatorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(operatorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case SyntaxKind.ConversionOperatorDeclaration:
            {
                var conversionOperatorDeclaration = (ConversionOperatorDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = conversionOperatorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(conversionOperatorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case SyntaxKind.ConstructorDeclaration:
            {
                var constructorDeclaration = (ConstructorDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = constructorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(constructorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }

            case SyntaxKind.DestructorDeclaration:
            {
                var destructorDeclaration = (DestructorDeclarationSyntax)memberDeclaration;

                ArrowExpressionClauseSyntax expressionBody = destructorDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    if (ExpandExpressionBodyAnalysis.IsFixable(expressionBody))
                    {
                        return(RefactoringIdentifiers.ExpandExpressionBody);
                    }
                }
                else if (UseExpressionBodiedMemberRefactoring.CanRefactor(destructorDeclaration))
                {
                    return(RefactoringIdentifiers.UseExpressionBodiedMember);
                }

                return(null);
            }
            }

            return(null);
        }