public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            Diagnostic?diagnostic = context.Diagnostics.First();

            SyntaxNode?root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            if (TryFindNodeAtSource(diagnostic, root, out _, out _))
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        Strings.VSTHRD002_CodeFix_Await_Title,
                        async ct =>
                {
                    Document?document = context.Document;
                    if (TryFindNodeAtSource(diagnostic, root, out ExpressionSyntax? node, out Func <ExpressionSyntax, CancellationToken, ExpressionSyntax>?transform))
                    {
                        (document, node, _) = await FixUtils.UpdateDocumentAsync(
                            document,
                            node,
                            n => SyntaxFactory.AwaitExpression(transform(n, ct)),
                            ct).ConfigureAwait(false);
                        MethodDeclarationSyntax?method = node.FirstAncestorOrSelf <MethodDeclarationSyntax>();
                        if (method is object)
                        {
                            (document, method) = await FixUtils.MakeMethodAsync(method, document, ct).ConfigureAwait(false);
                        }
                    }

                    return(document.Project.Solution);
                },
                        "only action"),
                    diagnostic);
            }
        }
Пример #2
0
        public override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var diagnostic = context.Diagnostics.First();

            context.RegisterCodeFix(
                CodeAction.Create(
                    Strings.VSTHRD107_CodeFix_Title,
                    async ct =>
            {
                var document = context.Document;
                var root     = await document.GetSyntaxRootAsync(ct).ConfigureAwait(false);
                var method   = root.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf <MethodDeclarationSyntax>();

                (document, method, _) = await FixUtils.UpdateDocumentAsync(
                    document,
                    method,
                    m =>
                {
                    root = m.SyntaxTree.GetRoot(ct);
                    var usingStatement  = root.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf <UsingStatementSyntax>();
                    var awaitExpression = SyntaxFactory.AwaitExpression(
                        SyntaxFactory.ParenthesizedExpression(usingStatement.Expression));
                    var modifiedUsingStatement = usingStatement.WithExpression(awaitExpression)
                                                 .WithAdditionalAnnotations(Simplifier.Annotation);
                    return(m.ReplaceNode(usingStatement, modifiedUsingStatement));
                },
                    ct).ConfigureAwait(false);
                (document, method) = await method.MakeMethodAsync(document, ct).ConfigureAwait(false);

                return(document.Project.Solution);
            },
                    "only action"),
                diagnostic);

            return(Task.FromResult <object>(null));
        }