1using System.Collections.Immutable;
2using System.Composition;
4using System.Threading.Tasks;
10using Microsoft.CodeAnalysis.CSharp.Syntax;
15 [ExportCodeFixProvider(LanguageNames.CSharp)]
21 ImmutableArray.Create(Constants.AnalyzerIdentifiers.CompleteInExecuteAsync);
23 public sealed
override FixAllProvider
GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
27 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(
false);
29 context.CancellationToken.ThrowIfCancellationRequested();
31 var diagnostic = context.Diagnostics.First();
32 var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
34 context.CancellationToken.ThrowIfCancellationRequested();
35 await AddCodeFixAsync(context, root, diagnostic, methodNode);
38 private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root,
39 Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
41 var model = await context.Document.GetSemanticModelAsync(context.CancellationToken);
42 var methodSymbol = model.GetDeclaredSymbol(methodNode);
43 var typeSymbol = methodSymbol.ContainingType;
44 var contextParameter = methodSymbol.Parameters[0];
48 var completeInvocations = methodNode.DescendantNodes(_ =>
true).OfType<InvocationExpressionSyntax>()
51 return model.GetSymbolInfo(invocation.Expression).Symbol is IMethodSymbol invocationSymbol &&
52 invocationSymbol.Name ==
"Complete" && Equals(invocationSymbol.ContainingType, contextParameter.Type);
54 .Select(invocation => invocation.FindParent<ExpressionStatementSyntax>());
56 newRoot = newRoot.RemoveNodes(completeInvocations, SyntaxRemoveOptions.KeepExteriorTrivia | SyntaxRemoveOptions.KeepDirectives)
57 .WithAdditionalAnnotations(Formatter.Annotation);
59 context.RegisterCodeFix(
61 IsCompleteCalledInAsynchronousBusinessRuleCodeFixConstants.RemoveCompleteCalls,
62 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
63 IsCompleteCalledInAsynchronousBusinessRuleCodeFixConstants.RemoveCompleteCalls), diagnostic);
override async Task RegisterCodeFixesAsync(CodeFixContext context)
override ImmutableArray< string > FixableDiagnosticIds
sealed override FixAllProvider GetFixAllProvider()