public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); InitializerExpressionSyntax initializer = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <InitializerExpressionSyntax>(); Debug.Assert(initializer != null, $"{nameof(initializer)} is null"); if (initializer == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.RemoveRedundantCommaInInitializer: { CodeAction codeAction = CodeAction.Create( "Remove redundant comma", cancellationToken => RemoveRedundantCommaInInitializerRefactoring.RefactorAsync(context.Document, initializer, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UseCSharp6DictionaryInitializer: { CodeAction codeAction = CodeAction.Create( "Use C# 6.0 dictionary initializer", cancellationToken => UseCSharp6DictionaryInitializerRefactoring.RefactorAsync(context.Document, initializer, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out InitializerExpressionSyntax initializer)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.RemoveRedundantCommaInInitializer: { CodeAction codeAction = CodeAction.Create( "Remove redundant comma", cancellationToken => RemoveRedundantCommaInInitializerRefactoring.RefactorAsync(context.Document, initializer, cancellationToken), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.FormatInitializerWithSingleExpressionOnSingleLine: { CodeAction codeAction = CodeAction.Create( "Format initializer on a single line", cancellationToken => FormatInitializerWithSingleExpressionOnSingleLineRefactoring.RefactorAsync(context.Document, initializer, cancellationToken), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }