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(AddModifierRefactoring.RefactorAsync(
                                       context.Document,
                                       classDeclaration,
                                       SyntaxKind.StaticKeyword,
                                       cancellationToken));
                        },
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Ejemplo n.º 2
0
        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;
                }
                }
            }
        }