|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CodeActions; |
| 8 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 9 | +using Microsoft.CodeAnalysis.CSharp; |
| 10 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 11 | + |
| 12 | +namespace EntityFrameworkCore.Triggered.Analyzers.CodeFixes; |
| 13 | + |
| 14 | +[ExportCodeFixProvider(LanguageNames.CSharp), Shared] |
| 15 | +public sealed class MigrateToAsyncTriggerCodeFixProvider : CodeFixProvider |
| 16 | +{ |
| 17 | + public override ImmutableArray<string> FixableDiagnosticIds { get; } = |
| 18 | + ImmutableArray.Create("EFCT001"); |
| 19 | + |
| 20 | + public override FixAllProvider? GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| 21 | + |
| 22 | + public override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 23 | + { |
| 24 | + var diagnostic = context.Diagnostics.First(); |
| 25 | + var properties = diagnostic.Properties; |
| 26 | + |
| 27 | + if (!properties.TryGetValue("AsyncInterfaceShortName", out var asyncInterfaceName) || |
| 28 | + !properties.TryGetValue("SyncInterfaceShortName", out var syncInterfaceName) || |
| 29 | + !properties.TryGetValue("SyncMethodName", out var syncMethodName) || |
| 30 | + !properties.TryGetValue("AsyncMethodName", out var asyncMethodName)) |
| 31 | + return; |
| 32 | + |
| 33 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 34 | + if (root == null) return; |
| 35 | + |
| 36 | + var node = root.FindNode(diagnostic.Location.SourceSpan); |
| 37 | + var methodDeclaration = node.FirstAncestorOrSelf<MethodDeclarationSyntax>(); |
| 38 | + if (methodDeclaration == null) return; |
| 39 | + |
| 40 | + var classDeclaration = methodDeclaration.FirstAncestorOrSelf<ClassDeclarationSyntax>(); |
| 41 | + if (classDeclaration == null) return; |
| 42 | + |
| 43 | + context.RegisterCodeFix( |
| 44 | + CodeAction.Create( |
| 45 | + title: $"Migrate to {asyncInterfaceName}", |
| 46 | + createChangedDocument: ct => MigrateToAsync(context.Document, root, classDeclaration, methodDeclaration, syncInterfaceName!, asyncInterfaceName!, syncMethodName!, asyncMethodName!, ct), |
| 47 | + equivalenceKey: "MigrateToAsyncTrigger"), |
| 48 | + diagnostic); |
| 49 | + } |
| 50 | + |
| 51 | + private static Task<Document> MigrateToAsync( |
| 52 | + Document document, |
| 53 | + SyntaxNode root, |
| 54 | + ClassDeclarationSyntax classDeclaration, |
| 55 | + MethodDeclarationSyntax methodDeclaration, |
| 56 | + string syncInterfaceName, |
| 57 | + string asyncInterfaceName, |
| 58 | + string syncMethodName, |
| 59 | + string asyncMethodName, |
| 60 | + CancellationToken cancellationToken) |
| 61 | + { |
| 62 | + var newRoot = root; |
| 63 | + |
| 64 | + // Replace interface name in base list |
| 65 | + if (classDeclaration.BaseList != null) |
| 66 | + { |
| 67 | + var newBaseList = classDeclaration.BaseList; |
| 68 | + foreach (var baseType in classDeclaration.BaseList.Types) |
| 69 | + { |
| 70 | + var typeName = GetBaseTypeName(baseType); |
| 71 | + if (typeName == syncInterfaceName) |
| 72 | + { |
| 73 | + var newBaseType = ReplaceInterfaceName(baseType, syncInterfaceName, asyncInterfaceName); |
| 74 | + newBaseList = newBaseList.ReplaceNode(baseType, newBaseType); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (newBaseList != classDeclaration.BaseList) |
| 79 | + { |
| 80 | + newRoot = newRoot.ReplaceNode(classDeclaration.BaseList, newBaseList); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Find the method again in the updated tree |
| 85 | + var updatedMethod = newRoot.FindNode(methodDeclaration.Identifier.Span) |
| 86 | + .FirstAncestorOrSelf<MethodDeclarationSyntax>(); |
| 87 | + |
| 88 | + if (updatedMethod != null && updatedMethod.Identifier.Text == syncMethodName) |
| 89 | + { |
| 90 | + var newMethod = updatedMethod.WithIdentifier( |
| 91 | + SyntaxFactory.Identifier(asyncMethodName) |
| 92 | + .WithTriviaFrom(updatedMethod.Identifier)); |
| 93 | + newRoot = newRoot.ReplaceNode(updatedMethod, newMethod); |
| 94 | + } |
| 95 | + |
| 96 | + return Task.FromResult(document.WithSyntaxRoot(newRoot)); |
| 97 | + } |
| 98 | + |
| 99 | + private static string? GetBaseTypeName(BaseTypeSyntax baseType) |
| 100 | + { |
| 101 | + return baseType.Type switch |
| 102 | + { |
| 103 | + SimpleNameSyntax simple => simple.Identifier.Text, |
| 104 | + QualifiedNameSyntax qualified => qualified.Right.Identifier.Text, |
| 105 | + _ => null |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + private static BaseTypeSyntax ReplaceInterfaceName(BaseTypeSyntax baseType, string oldName, string newName) |
| 110 | + { |
| 111 | + switch (baseType.Type) |
| 112 | + { |
| 113 | + case GenericNameSyntax generic when generic.Identifier.Text == oldName: |
| 114 | + var newGeneric = generic.WithIdentifier( |
| 115 | + SyntaxFactory.Identifier(newName).WithTriviaFrom(generic.Identifier)); |
| 116 | + return baseType.WithType(newGeneric); |
| 117 | + |
| 118 | + case SimpleNameSyntax simple when simple.Identifier.Text == oldName: |
| 119 | + var newSimple = SyntaxFactory.IdentifierName( |
| 120 | + SyntaxFactory.Identifier(newName).WithTriviaFrom(simple.Identifier)); |
| 121 | + return baseType.WithType(newSimple); |
| 122 | + |
| 123 | + case QualifiedNameSyntax qualified when qualified.Right.Identifier.Text == oldName: |
| 124 | + SimpleNameSyntax newRight; |
| 125 | + if (qualified.Right is GenericNameSyntax rightGeneric) |
| 126 | + { |
| 127 | + newRight = rightGeneric.WithIdentifier( |
| 128 | + SyntaxFactory.Identifier(newName).WithTriviaFrom(rightGeneric.Identifier)); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + newRight = SyntaxFactory.IdentifierName( |
| 133 | + SyntaxFactory.Identifier(newName).WithTriviaFrom(qualified.Right.Identifier)); |
| 134 | + } |
| 135 | + return baseType.WithType(qualified.WithRight(newRight)); |
| 136 | + |
| 137 | + default: |
| 138 | + return baseType; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments