public override void Initialize(AnalysisContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } base.Initialize(context); context.RegisterCompilationStartAction(startContext => { INamedTypeSymbol attributeSymbol = startContext.Compilation.GetTypeByMetadataName(MetadataNames.System_Attribute); INamedTypeSymbol attributeUsageAttributeSymbol = startContext.Compilation.GetTypeByMetadataName(MetadataNames.System_AttributeUsageAttribute); if (attributeSymbol != null && attributeUsageAttributeSymbol != null) { startContext.RegisterSymbolAction( nodeContext => UseAttributeUsageAttributeRefactoring.AnalyzerNamedTypeSymbol(nodeContext, attributeSymbol, attributeUsageAttributeSymbol), SymbolKind.NamedType); } }); }
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: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ISymbol symbol = semanticModel.GetDeclaredSymbol(classDeclaration, context.CancellationToken); ImmutableArray <SyntaxReference> syntaxReferences = symbol.DeclaringSyntaxReferences; if (!syntaxReferences.Any()) { break; } ModifiersCodeFixRegistrator.AddModifier( context, diagnostic, ImmutableArray.CreateRange(syntaxReferences, f => f.GetSyntax()), SyntaxKind.StaticKeyword, title: "Make class static"); break; } case DiagnosticIdentifiers.AddStaticModifierToAllPartialClassDeclarations: { ModifiersCodeFixRegistrator.AddModifier(context, diagnostic, classDeclaration, SyntaxKind.StaticKeyword); 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); 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; } } } }