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