コード例 #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out DestructorDeclarationSyntax destructor))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveEmptyDestructor:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove empty destructor",
                        cancellationToken => RemoveEmptyDestructorRefactoring.RefactorAsync(context.Document, destructor, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
コード例 #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            DestructorDeclarationSyntax destructor = root
                                                     .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                     .FirstAncestorOrSelf <DestructorDeclarationSyntax>();

            Debug.Assert(destructor != null, $"{nameof(destructor)} is null");

            if (destructor == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveEmptyDestructor:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove empty destructor",
                        cancellationToken => RemoveEmptyDestructorRefactoring.RefactorAsync(context.Document, destructor, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }