CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
IsCompleteCalledInAsynchronousBusinessRuleRemoveCallCodeFix.cs
Go to the documentation of this file.
1using System.Collections.Immutable;
2using System.Composition;
3using System.Linq;
4using System.Threading.Tasks;
6using Microsoft.CodeAnalysis;
7using Microsoft.CodeAnalysis.CodeActions;
8using Microsoft.CodeAnalysis.CodeFixes;
9using Microsoft.CodeAnalysis.CSharp;
10using Microsoft.CodeAnalysis.CSharp.Syntax;
11using Microsoft.CodeAnalysis.Formatting;
12
13namespace Csla.Analyzers
14{
15 [ExportCodeFixProvider(LanguageNames.CSharp)]
16 [Shared]
18 : CodeFixProvider
19 {
20 public override ImmutableArray<string> FixableDiagnosticIds =>
21 ImmutableArray.Create(Constants.AnalyzerIdentifiers.CompleteInExecuteAsync);
22
23 public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
24
25 public override async Task RegisterCodeFixesAsync(CodeFixContext context)
26 {
27 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
28
29 context.CancellationToken.ThrowIfCancellationRequested();
30
31 var diagnostic = context.Diagnostics.First();
32 var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
33
34 context.CancellationToken.ThrowIfCancellationRequested();
35 await AddCodeFixAsync(context, root, diagnostic, methodNode);
36 }
37
38 private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root,
39 Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
40 {
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];
45
46 var newRoot = root;
47
48 var completeInvocations = methodNode.DescendantNodes(_ => true).OfType<InvocationExpressionSyntax>()
49 .Where(invocation =>
50 {
51 return model.GetSymbolInfo(invocation.Expression).Symbol is IMethodSymbol invocationSymbol &&
52 invocationSymbol.Name == "Complete" && Equals(invocationSymbol.ContainingType, contextParameter.Type);
53 })
54 .Select(invocation => invocation.FindParent<ExpressionStatementSyntax>());
55
56 newRoot = newRoot.RemoveNodes(completeInvocations, SyntaxRemoveOptions.KeepExteriorTrivia | SyntaxRemoveOptions.KeepDirectives)
57 .WithAdditionalAnnotations(Formatter.Annotation);
58
59 context.RegisterCodeFix(
60 CodeAction.Create(
61 IsCompleteCalledInAsynchronousBusinessRuleCodeFixConstants.RemoveCompleteCalls,
62 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
63 IsCompleteCalledInAsynchronousBusinessRuleCodeFixConstants.RemoveCompleteCalls), diagnostic);
64 }
65 }
66}