Beispiel #1
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, EnumDeclarationSyntax enumDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, enumDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortMemberDeclarations) &&
                enumDeclaration.BracesSpan().Contains(context.Span))
            {
                await SortEnumMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, enumDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateCombinedEnumMember) &&
                enumDeclaration.BracesSpan().Contains(context.Span))
            {
                await GenerateCombinedEnumMemberRefactoring.ComputeRefactoringAsync(context, enumDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateEnumMember) &&
                context.Span.IsEmpty &&
                enumDeclaration.BracesSpan().Contains(context.Span))
            {
                await GenerateEnumMemberRefactoring.ComputeRefactoringAsync(context, enumDeclaration).ConfigureAwait(false);
            }
        }
Beispiel #2
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);
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, StructDeclarationSyntax structDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.AddGenericParameterToDeclaration))
            {
                AddGenericParameterToDeclarationRefactoring.ComputeRefactoring(context, structDeclaration);
            }

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

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

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

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

            if (context.IsRefactoringEnabled(RefactoringDescriptors.SortMemberDeclarations) &&
                structDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, structDeclaration);
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, DelegateDeclarationSyntax delegateDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.AddGenericParameterToDeclaration))
            {
                AddGenericParameterToDeclarationRefactoring.ComputeRefactoring(context, delegateDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, delegateDeclaration);
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, DelegateDeclarationSyntax delegateDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, delegateDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, delegateDeclaration);
            }
        }
Beispiel #6
0
        public static void ComputeRefactorings(RefactoringContext context, InterfaceDeclarationSyntax interfaceDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, interfaceDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortMemberDeclarations) &&
                interfaceDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, interfaceDeclaration);
            }
        }
        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);
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, InterfaceDeclarationSyntax interfaceDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.AddGenericParameterToDeclaration))
            {
                AddGenericParameterToDeclarationRefactoring.ComputeRefactoring(context, interfaceDeclaration);
            }

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

            if (context.IsRefactoringEnabled(RefactoringDescriptors.SortMemberDeclarations) &&
                interfaceDeclaration.BracesSpan().Contains(context.Span))
            {
                SortMemberDeclarationsRefactoring.ComputeRefactoring(context, interfaceDeclaration);
            }
        }
Beispiel #10
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, EnumDeclarationSyntax enumDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.ExtractTypeDeclarationToNewFile))
            {
                ExtractTypeDeclarationToNewFileRefactoring.ComputeRefactorings(context, enumDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveEnumMemberValue) &&
                context.Span.IsEmptyAndContainedInSpan(enumDeclaration.Identifier))
            {
                RemoveEnumMemberValueRefactoring.ComputeRefactoring(context, enumDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.GenerateEnumValues) &&
                context.Span.IsEmpty)
            {
                if (enumDeclaration.BracesSpan().Contains(context.Span))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    GenerateEnumValuesRefactoring.ComputeRefactoring(context, enumDeclaration, semanticModel);
                }

                if (enumDeclaration.Identifier.Span.Contains(context.Span))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    GenerateAllEnumValuesRefactoring.ComputeRefactoring(context, enumDeclaration, semanticModel);
                }
            }

            await SelectedEnumMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, enumDeclaration).ConfigureAwait(false);

            if (context.IsRefactoringEnabled(RefactoringDescriptors.GenerateEnumMember) &&
                context.Span.IsEmpty &&
                enumDeclaration.BracesSpan().Contains(context.Span))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                GenerateEnumMemberRefactoring.ComputeRefactoring(context, enumDeclaration, semanticModel);
            }
        }
Beispiel #11
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);
            }
        }
        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 " + GetMemberName(member),
                                cancellationToken => Remover.RemoveMemberAsync(context.Document, member, cancellationToken));
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember))
                        {
                            context.RegisterRefactoring(
                                "Duplicate " + GetMemberName(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.NamespaceDeclaration:
            {
                NamespaceDeclarationRefactoring.ComputeRefactorings(context, (NamespaceDeclarationSyntax)member);
                break;
            }

            case SyntaxKind.ClassDeclaration:
            {
                await ClassDeclarationRefactoring.ComputeRefactorings(context, (ClassDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

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

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

            case SyntaxKind.EnumDeclaration:
            {
                await EnumDeclarationRefactoring.ComputeRefactoringAsync(context, (EnumDeclarationSyntax)member).ConfigureAwait(false);

                break;
            }

            case SyntaxKind.EnumMemberDeclaration:
            {
                await EnumMemberDeclarationRefactoring.ComputeRefactoringAsync(context, (EnumMemberDeclarationSyntax)member).ConfigureAwait(false);

                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.DestructorDeclaration:
            {
                DestructorDeclarationRefactoring.ComputeRefactorings(context, (DestructorDeclarationSyntax)member);
                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;
            }
            }
        }