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 => RemoveBaseConstructorCallAsync(context.Document, constructor, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantConstructor: { CodeAction codeAction = CodeAction.Create( "Remove redundant constructor", cancellationToken => MemberDeclarationRefactoring.RemoveMemberAsync(context.Document, constructor, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken); MemberDeclarationSyntax memberDeclaration = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <MemberDeclarationSyntax>(); if (memberDeclaration == null) { return; } SplitAttributesRefactoring.Refactor(context, memberDeclaration); MergeAttributesRefactoring.Refactor(context, memberDeclaration); switch (memberDeclaration.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: { if (MemberDeclarationRefactoring.CanBeRemoved(context, memberDeclaration)) { context.RegisterRefactoring( "Remove " + SyntaxHelper.GetSyntaxNodeName(memberDeclaration), cancellationToken => MemberDeclarationRefactoring.RemoveMemberAsync(context.Document, memberDeclaration, cancellationToken)); } if (MemberDeclarationRefactoring.CanBeDuplicated(context, memberDeclaration)) { context.RegisterRefactoring( "Duplicate " + SyntaxHelper.GetSyntaxNodeName(memberDeclaration), cancellationToken => MemberDeclarationRefactoring.DuplicateMemberAsync(context.Document, memberDeclaration, cancellationToken)); } break; } } if (root.FindTrivia(context.Span.Start).IsWhitespaceOrEndOfLine()) { SwapMembersRefactoring.Refactor(context, memberDeclaration); } switch (memberDeclaration.Kind()) { case SyntaxKind.MethodDeclaration: { await ComputeRefactoringsAsync(context, (MethodDeclarationSyntax)memberDeclaration); break; } case SyntaxKind.IndexerDeclaration: { ComputeRefactorings(context, (IndexerDeclarationSyntax)memberDeclaration); break; } case SyntaxKind.PropertyDeclaration: { await ComputeRefactoringsAsync(context, (PropertyDeclarationSyntax)memberDeclaration); break; } case SyntaxKind.EventFieldDeclaration: { await ComputeRefactoringsAsync(context, (EventFieldDeclarationSyntax)memberDeclaration); break; } } }