|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | +using OmniSharp.Extensions.JsonRpc.Generators.Cache; |
| 10 | +using OmniSharp.Extensions.JsonRpc.Generators.Contexts; |
| 11 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 12 | + |
| 13 | +namespace OmniSharp.Extensions.JsonRpc.Generators |
| 14 | +{ |
| 15 | + [Generator] |
| 16 | + public class AssemblyJsonRpcHandlersAttributeGenerator : CachedSourceGenerator<AssemblyJsonRpcHandlersAttributeGenerator.SyntaxReceiver, TypeDeclarationSyntax> |
| 17 | + { |
| 18 | + protected override void Execute( |
| 19 | + GeneratorExecutionContext context, SyntaxReceiver syntaxReceiver, AddCacheSource<TypeDeclarationSyntax> addCacheSource, |
| 20 | + ReportCacheDiagnostic<TypeDeclarationSyntax> cacheDiagnostic |
| 21 | + ) |
| 22 | + { |
| 23 | + var namespaces = new HashSet<string>() { "OmniSharp.Extensions.JsonRpc" }; |
| 24 | + var types = syntaxReceiver.FoundNodes |
| 25 | + .Concat(syntaxReceiver.Handlers) |
| 26 | + .Select( |
| 27 | + options => { |
| 28 | + var semanticModel = context.Compilation.GetSemanticModel(options.SyntaxTree); |
| 29 | + var typeSymbol = semanticModel.GetDeclaredSymbol(options)!; |
| 30 | + |
| 31 | + return AttributeArgument(TypeOfExpression(ParseName(typeSymbol.ToDisplayString()))); |
| 32 | + } |
| 33 | + ) |
| 34 | + .ToArray(); |
| 35 | + if (types.Any()) |
| 36 | + { |
| 37 | + var cu = CompilationUnit() |
| 38 | + .WithUsings(List(namespaces.OrderBy(z => z).Select(z => UsingDirective(ParseName(z))))) |
| 39 | + .AddAttributeLists( |
| 40 | + AttributeList( |
| 41 | + target: AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)), |
| 42 | + SingletonSeparatedList(Attribute(IdentifierName("AssemblyJsonRpcHandlers"), AttributeArgumentList(SeparatedList(types)))) |
| 43 | + ) |
| 44 | + ) |
| 45 | + .WithLeadingTrivia(Comment(Preamble.GeneratedByATool)) |
| 46 | + .WithTrailingTrivia(CarriageReturnLineFeed); |
| 47 | + |
| 48 | + context.AddSource("AssemblyJsonRpcHandlers.cs", cu.NormalizeWhitespace().GetText(Encoding.UTF8)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public AssemblyJsonRpcHandlersAttributeGenerator() : base(() => new SyntaxReceiver(Cache)) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + public static CacheContainer<TypeDeclarationSyntax> Cache = new(); |
| 57 | + |
| 58 | + public class SyntaxReceiver : SyntaxReceiverCache<TypeDeclarationSyntax> |
| 59 | + { |
| 60 | + public List<TypeDeclarationSyntax> Handlers { get; } = new(); |
| 61 | + |
| 62 | + public override string? GetKey(TypeDeclarationSyntax syntax) |
| 63 | + { |
| 64 | + var hasher = new CacheKeyHasher(); |
| 65 | + hasher.Append(syntax.SyntaxTree.FilePath); |
| 66 | + hasher.Append(syntax.Keyword.Text); |
| 67 | + hasher.Append(syntax.Identifier.Text); |
| 68 | + hasher.Append(syntax.TypeParameterList); |
| 69 | + hasher.Append(syntax.AttributeLists); |
| 70 | + hasher.Append(syntax.BaseList); |
| 71 | + return hasher; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation |
| 76 | + /// </summary> |
| 77 | + public override void OnVisitNode(TypeDeclarationSyntax syntaxNode) |
| 78 | + { |
| 79 | + if (syntaxNode.Parent is TypeDeclarationSyntax) return; |
| 80 | + if (syntaxNode is ClassDeclarationSyntax or RecordDeclarationSyntax |
| 81 | + && syntaxNode.Arity == 0 |
| 82 | + && !syntaxNode.Modifiers.Any(SyntaxKind.AbstractKeyword) |
| 83 | + && syntaxNode.AttributeLists.ContainsAttribute("Method") |
| 84 | + && syntaxNode.BaseList is { } bl && bl.Types.Any( |
| 85 | + z => z.Type switch { |
| 86 | + SimpleNameSyntax { Identifier: { Text: "IJsonRpcNotificationHandler" }, Arity: 0 or 1 } => true, |
| 87 | + SimpleNameSyntax { Identifier: { Text: "IJsonRpcRequestHandler" }, Arity: 1 or 2 } => true, |
| 88 | + SimpleNameSyntax { Identifier: { Text: "IJsonRpcHandler" }, Arity: 0 } => true, |
| 89 | + _ => false |
| 90 | + } |
| 91 | + )) |
| 92 | + { |
| 93 | + Handlers.Add(syntaxNode); |
| 94 | + } |
| 95 | + |
| 96 | + if (syntaxNode is InterfaceDeclarationSyntax |
| 97 | + && syntaxNode.Arity == 0 |
| 98 | + && syntaxNode.AttributeLists.ContainsAttribute("Method") |
| 99 | + && syntaxNode.BaseList is { } bl2 && bl2.Types.Any( |
| 100 | + z => z.Type switch { |
| 101 | + SimpleNameSyntax { Identifier: { Text: "IJsonRpcNotificationHandler" }, Arity: 0 or 1 } => true, |
| 102 | + SimpleNameSyntax { Identifier: { Text: "IJsonRpcRequestHandler" }, Arity: 1 or 2 } => true, |
| 103 | + SimpleNameSyntax { Identifier: { Text: "IJsonRpcHandler" }, Arity: 0 } => true, |
| 104 | + _ => false |
| 105 | + } |
| 106 | + )) |
| 107 | + { |
| 108 | + Handlers.Add(syntaxNode); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public SyntaxReceiver(CacheContainer<TypeDeclarationSyntax> cache) : base(cache) |
| 113 | + { |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments