public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document .GetSyntaxRootAsync(context.CancellationToken) .ConfigureAwait(false); NamespaceDeclarationSyntax namespaceDeclaration = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <NamespaceDeclarationSyntax>(); Debug.Assert(namespaceDeclaration != null, $"{nameof(namespaceDeclaration)} is null"); if (namespaceDeclaration == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.RemoveEmptyNamespaceDeclaration: { CodeAction codeAction = CodeAction.Create( "Remove empty namespace declaration", cancellationToken => RemoveEmptyNamespaceDeclarationRefactoring.RefactorAsync(context.Document, namespaceDeclaration, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.DeclareUsingDirectiveOnTopLevel: { string title = (namespaceDeclaration.Usings.Count == 1) ? "Move using to top level" : "Move usings to top level"; CodeAction codeAction = CodeAction.Create( title, cancellationToken => DeclareUsingDirectiveOnTopLevelRefactoring.RefactorAsync(context.Document, namespaceDeclaration, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
public override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out NamespaceDeclarationSyntax namespaceDeclaration)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.RemoveEmptyNamespaceDeclaration: { CodeAction codeAction = CodeAction.Create( "Remove empty namespace declaration", ct => RemoveEmptyNamespaceDeclarationRefactoring.RefactorAsync(context.Document, namespaceDeclaration, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.DeclareUsingDirectiveOnTopLevel: { string title = (namespaceDeclaration.Usings.Count == 1) ? "Move using to top level" : "Move usings to top level"; CodeAction codeAction = CodeAction.Create( title, ct => DeclareUsingDirectiveOnTopLevelRefactoring.RefactorAsync(context.Document, namespaceDeclaration, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }