예제 #1
0
            internal static SyntaxNode Update(MethodDeclarationSyntax method, string before, string after)
            {
                if (!Cache.TryDequeue(out var rewriter))
                {
                    rewriter = new ReplaceRewriter();
                }

                rewriter.before = before;
                rewriter.after  = after;
                var updated = rewriter.Visit(method);

                Cache.Enqueue(rewriter);
                return(updated);
            }
예제 #2
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
                             .ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (syntaxRoot.TryFindNodeOrAncestor(diagnostic, out MethodDeclarationSyntax? methodDeclaration) &&
                    diagnostic.Properties.TryGetValue("before", out var before) &&
                    diagnostic.Properties.TryGetValue("after", out var after))
                {
                    context.RegisterCodeFix(
                        CodeAction.Create(
                            $"Replace {before} with {after}",
                            _ => Task.FromResult(
                                context.Document.WithSyntaxRoot(
                                    syntaxRoot.ReplaceNode(
                                        methodDeclaration,
                                        ReplaceRewriter.Update(methodDeclaration, before, after)))),
                            nameof(StandardNamesFix)),
                        diagnostic);
                }
            }
        }