| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Composition; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | using Microsoft.CodeAnalysis; |
| | | 8 | | using Microsoft.CodeAnalysis.CodeActions; |
| | | 9 | | using Microsoft.CodeAnalysis.CodeFixes; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 11 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 12 | | |
| | | 13 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 14 | | |
| | | 15 | | [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AgentTopologyCodeFixProvider))] |
| | | 16 | | [Shared] |
| | | 17 | | public sealed class AgentTopologyCodeFixProvider : CodeFixProvider |
| | | 18 | | { |
| | | 19 | | private const string Title = "Add [NeedlrAiAgent]"; |
| | | 20 | | |
| | | 21 | | public override ImmutableArray<string> FixableDiagnosticIds => |
| | 862 | 22 | | ImmutableArray.Create( |
| | 862 | 23 | | MafDiagnosticIds.HandoffsToTargetNotNeedlrAgent, |
| | 862 | 24 | | MafDiagnosticIds.HandoffsToSourceNotNeedlrAgent); |
| | | 25 | | |
| | 8 | 26 | | public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| | | 27 | | |
| | | 28 | | public override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| | | 29 | | { |
| | 14 | 30 | | var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| | 14 | 31 | | if (root is null) |
| | 0 | 32 | | return; |
| | | 33 | | |
| | 56 | 34 | | foreach (var diagnostic in context.Diagnostics) |
| | | 35 | | { |
| | 14 | 36 | | if (diagnostic.Id == MafDiagnosticIds.HandoffsToSourceNotNeedlrAgent) |
| | 7 | 37 | | RegisterSourceFix(context, root, diagnostic); |
| | 7 | 38 | | else if (diagnostic.Id == MafDiagnosticIds.HandoffsToTargetNotNeedlrAgent) |
| | 7 | 39 | | await RegisterTargetFixAsync(context, root, diagnostic).ConfigureAwait(false); |
| | | 40 | | } |
| | 14 | 41 | | } |
| | | 42 | | |
| | | 43 | | private static void RegisterSourceFix(CodeFixContext context, SyntaxNode root, Diagnostic diagnostic) |
| | | 44 | | { |
| | 7 | 45 | | var token = root.FindToken(diagnostic.Location.SourceSpan.Start); |
| | 7 | 46 | | var classDeclaration = token.Parent?.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().FirstOrDefault(); |
| | 7 | 47 | | if (classDeclaration is null) |
| | 0 | 48 | | return; |
| | | 49 | | |
| | 7 | 50 | | context.RegisterCodeFix( |
| | 7 | 51 | | CodeAction.Create( |
| | 7 | 52 | | title: Title, |
| | 4 | 53 | | createChangedDocument: ct => AddAttributeToDocumentAsync(context.Document, classDeclaration, ct), |
| | 7 | 54 | | equivalenceKey: Title + MafDiagnosticIds.HandoffsToSourceNotNeedlrAgent), |
| | 7 | 55 | | diagnostic); |
| | 7 | 56 | | } |
| | | 57 | | |
| | | 58 | | private static async Task RegisterTargetFixAsync(CodeFixContext context, SyntaxNode root, Diagnostic diagnostic) |
| | | 59 | | { |
| | 7 | 60 | | var node = root.FindNode(diagnostic.Location.SourceSpan); |
| | 7 | 61 | | var attributeSyntax = node.AncestorsAndSelf().OfType<AttributeSyntax>().FirstOrDefault(); |
| | 7 | 62 | | if (attributeSyntax is null) |
| | 0 | 63 | | return; |
| | | 64 | | |
| | 7 | 65 | | var typeOfExpr = attributeSyntax.ArgumentList?.Arguments |
| | 7 | 66 | | .Select(a => a.Expression) |
| | 7 | 67 | | .OfType<TypeOfExpressionSyntax>() |
| | 7 | 68 | | .FirstOrDefault(); |
| | 7 | 69 | | if (typeOfExpr is null) |
| | 0 | 70 | | return; |
| | | 71 | | |
| | 7 | 72 | | var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false |
| | 7 | 73 | | if (semanticModel is null) |
| | 0 | 74 | | return; |
| | | 75 | | |
| | 7 | 76 | | var targetType = semanticModel.GetTypeInfo(typeOfExpr.Type, context.CancellationToken).Type as INamedTypeSymbol; |
| | 7 | 77 | | if (targetType is null) |
| | 0 | 78 | | return; |
| | | 79 | | |
| | 7 | 80 | | context.RegisterCodeFix( |
| | 7 | 81 | | CodeAction.Create( |
| | 7 | 82 | | title: Title, |
| | 4 | 83 | | createChangedSolution: ct => AddAttributeToTargetAsync( |
| | 4 | 84 | | context.Document.Project.Solution, targetType, ct), |
| | 7 | 85 | | equivalenceKey: Title + MafDiagnosticIds.HandoffsToTargetNotNeedlrAgent), |
| | 7 | 86 | | diagnostic); |
| | 7 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static async Task<Document> AddAttributeToDocumentAsync( |
| | | 90 | | Document document, |
| | | 91 | | ClassDeclarationSyntax classDeclaration, |
| | | 92 | | CancellationToken ct) |
| | | 93 | | { |
| | 8 | 94 | | var root = await document.GetSyntaxRootAsync(ct).ConfigureAwait(false); |
| | 8 | 95 | | if (root is null) |
| | 0 | 96 | | return document; |
| | | 97 | | |
| | 8 | 98 | | var updated = PrependNeedlrAiAgent(classDeclaration); |
| | 8 | 99 | | return document.WithSyntaxRoot(root.ReplaceNode(classDeclaration, updated)); |
| | 8 | 100 | | } |
| | | 101 | | |
| | | 102 | | private static async Task<Solution> AddAttributeToTargetAsync( |
| | | 103 | | Solution solution, |
| | | 104 | | INamedTypeSymbol targetType, |
| | | 105 | | CancellationToken ct) |
| | | 106 | | { |
| | 4 | 107 | | var syntaxRef = targetType.DeclaringSyntaxReferences.FirstOrDefault(); |
| | 4 | 108 | | if (syntaxRef is null) |
| | 0 | 109 | | return solution; |
| | | 110 | | |
| | 4 | 111 | | var syntax = await syntaxRef.GetSyntaxAsync(ct).ConfigureAwait(false); |
| | 4 | 112 | | if (syntax is not ClassDeclarationSyntax classDeclaration) |
| | 0 | 113 | | return solution; |
| | | 114 | | |
| | 4 | 115 | | var documentId = solution.GetDocumentId(syntaxRef.SyntaxTree); |
| | 4 | 116 | | if (documentId is null) |
| | 0 | 117 | | return solution; |
| | | 118 | | |
| | 4 | 119 | | var document = solution.GetDocument(documentId); |
| | 4 | 120 | | if (document is null) |
| | 0 | 121 | | return solution; |
| | | 122 | | |
| | 4 | 123 | | var updated = await AddAttributeToDocumentAsync(document, classDeclaration, ct).ConfigureAwait(false); |
| | 4 | 124 | | return updated.Project.Solution; |
| | 4 | 125 | | } |
| | | 126 | | |
| | | 127 | | private static ClassDeclarationSyntax PrependNeedlrAiAgent(ClassDeclarationSyntax classDeclaration) |
| | | 128 | | { |
| | 8 | 129 | | var attribute = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("NeedlrAiAgent")); |
| | 8 | 130 | | var firstToken = classDeclaration.GetFirstToken(); |
| | 8 | 131 | | var leadingTrivia = firstToken.LeadingTrivia; |
| | | 132 | | |
| | 16 | 133 | | var endOfLine = leadingTrivia.FirstOrDefault(t => t.IsKind(SyntaxKind.EndOfLineTrivia)); |
| | 8 | 134 | | if (endOfLine == default) |
| | 0 | 135 | | endOfLine = SyntaxFactory.LineFeed; |
| | | 136 | | |
| | 8 | 137 | | var attrList = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(attribute)) |
| | 8 | 138 | | .WithLeadingTrivia(leadingTrivia) |
| | 8 | 139 | | .WithTrailingTrivia(SyntaxFactory.TriviaList(endOfLine)); |
| | | 140 | | |
| | 8 | 141 | | var stripped = classDeclaration.ReplaceToken( |
| | 8 | 142 | | firstToken, |
| | 8 | 143 | | firstToken.WithLeadingTrivia(SyntaxFactory.TriviaList())); |
| | | 144 | | |
| | 8 | 145 | | return stripped.WithAttributeLists(stripped.AttributeLists.Insert(0, attrList)); |
| | | 146 | | } |
| | | 147 | | } |