Пример #1
0
        public static async Task ComputeRefactorings(RefactoringContext context, ClassDeclarationSyntax classDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, classDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, classDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateBaseConstructors))
            {
                await GenerateBaseConstructorsRefactoring.ComputeRefactoringAsync(context, classDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ImplementIEquatableOfT))
            {
                await ImplementIEquatableOfTRefactoring.ComputeRefactoringAsync(context, classDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortMemberDeclarations) &&
                classDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, classDeclaration);
            }
        }
Пример #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            ClassDeclarationSyntax classDeclaration,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            List <IMethodSymbol> constructors = GenerateBaseConstructorsAnalysis.GetMissingBaseConstructors(classDeclaration, semanticModel, cancellationToken);

            return(await GenerateBaseConstructorsRefactoring.RefactorAsync(document, classDeclaration, constructors.ToArray(), semanticModel, cancellationToken).ConfigureAwait(false));
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, RecordDeclarationSyntax recordDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.AddGenericParameterToDeclaration))
            {
                AddGenericParameterToDeclarationRefactoring.ComputeRefactoring(context, recordDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, recordDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.GenerateBaseConstructors) &&
                recordDeclaration.Identifier.Span.Contains(context.Span))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                List <IMethodSymbol> constructors = GenerateBaseConstructorsAnalysis.GetMissingBaseConstructors(recordDeclaration, semanticModel, context.CancellationToken);

                if (constructors?.Count > 0)
                {
                    context.RegisterRefactoring(
                        (constructors.Count == 1) ? "Generate base constructor" : "Generate base constructors",
                        ct => GenerateBaseConstructorsRefactoring.RefactorAsync(context.Document, recordDeclaration, constructors.ToArray(), semanticModel, ct),
                        RefactoringDescriptors.GenerateBaseConstructors);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.ImplementCustomEnumerator) &&
                context.Span.IsEmptyAndContainedInSpan(recordDeclaration.Identifier))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ImplementCustomEnumeratorRefactoring.ComputeRefactoring(context, recordDeclaration, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.ExpandPositionalConstructor) &&
                recordDeclaration.ParameterList != null &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(recordDeclaration.ParameterList.Parameters))
            {
                ExpandPositionalConstructorRefactoring.ComputeRefactoring(context, recordDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.SortMemberDeclarations) &&
                recordDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, recordDeclaration);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ClassDeclarationSyntax classDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, classDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, classDeclaration);
            }

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

                List <IMethodSymbol> constructors = GenerateBaseConstructorsAnalysis.GetMissingBaseConstructors(classDeclaration, semanticModel, context.CancellationToken);

                if (constructors?.Count > 0)
                {
                    context.RegisterRefactoring(
                        (constructors.Count == 1) ? "Generate base constructor" : "Generate base constructors",
                        cancellationToken => GenerateBaseConstructorsRefactoring.RefactorAsync(context.Document, classDeclaration, constructors.ToArray(), semanticModel, cancellationToken),
                        RefactoringIdentifiers.GenerateBaseConstructors);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ImplementIEquatableOfT))
            {
                await ImplementIEquatableOfTRefactoring.ComputeRefactoringAsync(context, classDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ImplementCustomEnumerator) &&
                context.Span.IsEmptyAndContainedInSpan(classDeclaration.Identifier))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ImplementCustomEnumeratorRefactoring.ComputeRefactoring(context, classDeclaration, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortMemberDeclarations) &&
                classDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, classDeclaration);
            }
        }
Пример #5
0
        public static async Task ComputeRefactorings(RefactoringContext context, ClassDeclarationSyntax classDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, classDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, classDeclaration);
            }

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

                ImmutableArray <IMethodSymbol> constructors = GenerateBaseConstructorsRefactoring.GetMissingBaseConstructors(classDeclaration, semanticModel, context.CancellationToken);

                if (!constructors.IsDefaultOrEmpty)
                {
                    context.RegisterRefactoring(
                        (constructors.Length == 1) ? "Generate base constructor" : "Generate base constructors",
                        cancellationToken => GenerateBaseConstructorsRefactoring.RefactorAsync(context.Document, classDeclaration, constructors, semanticModel, context.CancellationToken));
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ImplementIEquatableOfT))
            {
                await ImplementIEquatableOfTRefactoring.ComputeRefactoringAsync(context, classDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortMemberDeclarations) &&
                classDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, classDeclaration);
            }
        }
Пример #6
0
        public static void AnalyzeNamedType(SymbolAnalysisContext context)
        {
            var symbol = (INamedTypeSymbol)context.Symbol;

            if (symbol.IsClass() &&
                !symbol.IsStatic &&
                !symbol.IsImplicitClass &&
                !symbol.IsImplicitlyDeclared)
            {
                INamedTypeSymbol baseType = symbol.BaseType;

                if (baseType?.IsObject() == false &&
                    baseType.EqualsOrInheritsFrom(context.Compilation.GetTypeByMetadataName(MetadataNames.System_Exception)) &&
                    GenerateBaseConstructorsRefactoring.IsAnyBaseConstructorMissing(symbol, baseType))
                {
                    var classDeclaration = symbol.GetSyntax(context.CancellationToken) as ClassDeclarationSyntax;

                    context.ReportDiagnostic(
                        DiagnosticDescriptors.ImplementExceptionConstructors,
                        classDeclaration.Identifier);
                }
            }
        }
Пример #7
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, MemberDeclarationSyntax member)
        {
            switch (member.Kind())
            {
            case SyntaxKind.MethodDeclaration:
            case SyntaxKind.IndexerDeclaration:
            case SyntaxKind.PropertyDeclaration:
            case SyntaxKind.OperatorDeclaration:
            case SyntaxKind.ConversionOperatorDeclaration:
            case SyntaxKind.ConstructorDeclaration:
            case SyntaxKind.EventDeclaration:
            case SyntaxKind.NamespaceDeclaration:
            case SyntaxKind.ClassDeclaration:
            case SyntaxKind.StructDeclaration:
            case SyntaxKind.InterfaceDeclaration:
            case SyntaxKind.EnumDeclaration:
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.RemoveMember,
                        RefactoringIdentifiers.DuplicateMember,
                        RefactoringIdentifiers.CommentOutMember) &&
                    BraceContainsSpan(context, member))
                {
                    if (member.Parent?.IsKind(
                            SyntaxKind.NamespaceDeclaration,
                            SyntaxKind.ClassDeclaration,
                            SyntaxKind.StructDeclaration,
                            SyntaxKind.InterfaceDeclaration,
                            SyntaxKind.CompilationUnit) == true)
                    {
                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveMember))
                        {
                            context.RegisterRefactoring(
                                "Remove " + SyntaxHelper.GetSyntaxNodeTitle(member),
                                cancellationToken => SyntaxRemover.RemoveMemberAsync(context.Document, member, cancellationToken));
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember))
                        {
                            context.RegisterRefactoring(
                                "Duplicate " + SyntaxHelper.GetSyntaxNodeTitle(member),
                                cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, member, cancellationToken));
                        }
                    }

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

                break;
            }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveAllStatements))
            {
                RemoveAllStatementsRefactoring.ComputeRefactoring(context, member);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveAllMemberDeclarations))
            {
                RemoveAllMemberDeclarationsRefactoring.ComputeRefactoring(context, member);
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SwapMemberDeclarations,
                    RefactoringIdentifiers.RemoveMemberDeclarations) &&
                !member.Span.IntersectsWith(context.Span))
            {
                MemberDeclarationsRefactoring.ComputeRefactoring(context, member);
            }

            switch (member.Kind())
            {
            case SyntaxKind.ClassDeclaration:
            {
                var classDeclaration = (ClassDeclarationSyntax)member;

                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, classDeclaration);

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateBaseConstructors))
                {
                    await GenerateBaseConstructorsRefactoring.ComputeRefactoringAsync(context, classDeclaration).ConfigureAwait(false);
                }

                break;
            }

            case SyntaxKind.StructDeclaration:
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, (StructDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.InterfaceDeclaration:
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, (InterfaceDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.EnumDeclaration:
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, (EnumDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.DelegateDeclaration:
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, (DelegateDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.MethodDeclaration:
            {
                await MethodDeclarationRefactoring.ComputeRefactoringsAsync(context, (MethodDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.ConstructorDeclaration:
            {
                await ConstructorDeclarationRefactoring.ComputeRefactoringsAsync(context, (ConstructorDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.IndexerDeclaration:
            {
                await IndexerDeclarationRefactoring.ComputeRefactoringsAsync(context, (IndexerDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.PropertyDeclaration:
            {
                await PropertyDeclarationRefactoring.ComputeRefactoringsAsync(context, (PropertyDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.OperatorDeclaration:
            {
                ComputeRefactorings(context, (OperatorDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.ConversionOperatorDeclaration:
            {
                ComputeRefactorings(context, (ConversionOperatorDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.FieldDeclaration:
            {
                await FieldDeclarationRefactoring.ComputeRefactoringsAsync(context, (FieldDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.EventDeclaration:
            {
                await EventDeclarationRefactoring.ComputeRefactoringsAsync(context, (EventDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.EventFieldDeclaration:
            {
                await EventFieldDeclarationRefactoring.ComputeRefactoringsAsync(context, (EventFieldDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }
            }
        }