public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document .GetSyntaxRootAsync(context.CancellationToken) .ConfigureAwait(false); ClassDeclarationSyntax classDeclaration = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <ClassDeclarationSyntax>(); Debug.Assert(classDeclaration != null, $"{nameof(classDeclaration)} is null"); if (classDeclaration == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.MarkClassAsStatic: { CodeAction codeAction = CodeAction.Create( "Mark class as static", cancellationToken => { return(MarkClassAsStaticRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddStaticModifierToAllPartialClassDeclarations: { CodeAction codeAction = CodeAction.Create( "Add static modifier", cancellationToken => { return(AddStaticModifierToAllPartialClassDeclarationsRefactoring.RefactorAsync( context.Document, classDeclaration, 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 ClassDeclarationSyntax classDeclaration)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.MakeClassStatic: { CodeAction codeAction = null; SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol symbol = semanticModel.GetDeclaredSymbol(classDeclaration, context.CancellationToken); ImmutableArray <SyntaxReference> syntaxReferences = symbol.DeclaringSyntaxReferences; if (syntaxReferences.Length == 1) { codeAction = CodeAction.Create( $"Make '{classDeclaration.Identifier.ValueText}' static", cancellationToken => { return(MakeClassStaticRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, GetEquivalenceKey(diagnostic)); } else { ImmutableArray <ClassDeclarationSyntax> classDeclarations = syntaxReferences .Select(f => (ClassDeclarationSyntax)f.GetSyntax(context.CancellationToken)) .ToImmutableArray(); codeAction = CodeAction.Create( $"Make '{classDeclaration.Identifier.ValueText}' static", cancellationToken => { return(MakeClassStaticRefactoring.RefactorAsync( context.Solution(), classDeclarations, cancellationToken)); }, GetEquivalenceKey(diagnostic)); } context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddStaticModifierToAllPartialClassDeclarations: { CodeAction codeAction = CodeAction.Create( "Add 'static' modifier", cancellationToken => { return(AddStaticModifierToAllPartialClassDeclarationsRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.ImplementExceptionConstructors: { CodeAction codeAction = CodeAction.Create( "Generate exception constructors", cancellationToken => { return(ImplementExceptionConstructorsRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UseAttributeUsageAttribute: { CodeAction codeAction = CodeAction.Create( "Use AttributeUsageAttribute", cancellationToken => { return(UseAttributeUsageAttributeRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); ClassDeclarationSyntax classDeclaration = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <ClassDeclarationSyntax>(); Debug.Assert(classDeclaration != null, $"{nameof(classDeclaration)} is null"); if (classDeclaration == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.MarkClassAsStatic: { CodeAction codeAction = null; SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol symbol = semanticModel.GetDeclaredSymbol(classDeclaration, context.CancellationToken); ImmutableArray <SyntaxReference> syntaxReferences = symbol.DeclaringSyntaxReferences; if (syntaxReferences.Length == 1) { codeAction = CodeAction.Create( $"Mark '{classDeclaration.Identifier.ValueText}' as static", cancellationToken => { return(MarkClassAsStaticRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); } else { ImmutableArray <ClassDeclarationSyntax> classDeclarations = syntaxReferences .Select(f => (ClassDeclarationSyntax)f.GetSyntax(context.CancellationToken)) .ToImmutableArray(); codeAction = CodeAction.Create( $"Mark '{classDeclaration.Identifier.ValueText}' as static", cancellationToken => { return(MarkClassAsStaticRefactoring.RefactorAsync( context.Solution(), classDeclarations, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); } context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddStaticModifierToAllPartialClassDeclarations: { CodeAction codeAction = CodeAction.Create( "Add static modifier", cancellationToken => { return(AddStaticModifierToAllPartialClassDeclarationsRefactoring.RefactorAsync( context.Document, classDeclaration, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }