Exemple #1
0
        private void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext context)
        {
            var constructor = (ConstructorDeclarationSyntax)context.Node;

            RemoveRedundantBaseConstructorCallRefactoring.Analyze(context, constructor);

            RemoveRedundantConstructorRefactoring.Analyze(context, constructor);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document
                              .GetSyntaxRootAsync(context.CancellationToken)
                              .ConfigureAwait(false);

            ConstructorDeclarationSyntax constructor = root
                                                       .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                       .FirstAncestorOrSelf <ConstructorDeclarationSyntax>();

            if (constructor == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantBaseConstructorCall:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant base constructor call",
                        cancellationToken => RemoveRedundantBaseConstructorCallRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.RemoveRedundantConstructor:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant constructor",
                        cancellationToken => RemoveRedundantConstructorRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AbstractTypeShouldNotHavePublicConstructors:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Change accessibility to 'protected'",
                        cancellationToken => AbstractTypeShouldNotHavePublicConstructorsRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Exemple #3
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out ConstructorDeclarationSyntax constructor))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantBaseConstructorCall:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant base constructor call",
                        cancellationToken => RemoveRedundantBaseConstructorCallRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.RemoveRedundantConstructor:
                {
                    CodeAction codeAction = CodeActionFactory.RemoveMemberDeclaration(
                        context.Document,
                        constructor,
                        title: "Remove redundant constructor",
                        equivalenceKey: GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AbstractTypeShouldNotHavePublicConstructors:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Change accessibility to 'protected'",
                        cancellationToken => AbstractTypeShouldNotHavePublicConstructorsRefactoring.RefactorAsync(context.Document, constructor, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }