public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            SyntaxNode node = root
                              .FindNode(context.Span, getInnermostNodeForTie: true)?
                              .FirstAncestorOrSelf(f => f.IsKind(
                                                       SyntaxKind.MethodDeclaration,
                                                       SyntaxKind.LocalFunctionStatement,
                                                       SyntaxKind.SimpleLambdaExpression,
                                                       SyntaxKind.ParenthesizedLambdaExpression,
                                                       SyntaxKind.AnonymousMethodExpression));

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

            if (node == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove redundant async/await",
                cancellationToken => RemoveRedundantAsyncAwaitRefactoring.RefactorAsync(context.Document, node, cancellationToken),
                DiagnosticIdentifiers.RemoveRedundantAsyncAwait + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Ejemplo n.º 2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out SyntaxNode node, predicate: f => f.IsKind(
                                                SyntaxKind.MethodDeclaration,
                                                SyntaxKind.LocalFunctionStatement,
                                                SyntaxKind.SimpleLambdaExpression,
                                                SyntaxKind.ParenthesizedLambdaExpression,
                                                SyntaxKind.AnonymousMethodExpression)))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove redundant async/await",
                cancellationToken => RemoveRedundantAsyncAwaitRefactoring.RefactorAsync(context.Document, node, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.RemoveRedundantAsyncAwait));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }