|
1 | 1 | using Microsoft.CodeAnalysis;
|
2 |
| -using Microsoft.CodeAnalysis.CSharp; |
3 | 2 | using Microsoft.CodeAnalysis.CSharp.Syntax;
|
4 |
| -using Microsoft.CodeAnalysis.Text; |
5 |
| -using System.Collections.Generic; |
6 |
| -using System.Linq; |
7 |
| -using System.Text; |
8 | 3 |
|
9 | 4 | namespace StringLiteralGenerator;
|
10 | 5 |
|
11 | 6 | [Generator]
|
12 |
| -public partial class Utf8StringLiteralGenerator : ISourceGenerator |
| 7 | +public partial class Utf8StringLiteralGenerator : IIncrementalGenerator |
13 | 8 | {
|
14 |
| - public void Execute(GeneratorExecutionContext context) |
| 9 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
15 | 10 | {
|
16 |
| - if (context.SyntaxReceiver is not SyntaxReceiver receiver) return; |
| 11 | + context.RegisterPostInitializationOutput(AddAttribute); |
17 | 12 |
|
18 |
| - var compilation = context.Compilation; |
| 13 | + var provider = context.SyntaxProvider |
| 14 | + .CreateSyntaxProvider( |
| 15 | + static (node, _) => IsSyntaxTargetForGeneration(node), |
| 16 | + static (context, _) => GetSemanticTargetForGeneration(context.SemanticModel, (MethodDeclarationSyntax)context.Node)! |
| 17 | + ) |
| 18 | + .Where(x => x is not null) |
| 19 | + .Collect(); |
19 | 20 |
|
20 |
| - var buffer = new StringBuilder(); |
21 |
| - |
22 |
| - var group = enumerate().GroupBy(x => x.type, x => x.method); |
23 |
| - |
24 |
| - foreach (var g in group) |
25 |
| - { |
26 |
| - var containingType = g.Key; |
27 |
| - var generatedSource = Generate(containingType, g, buffer); |
28 |
| - var filename = GetFilename(containingType, buffer); |
29 |
| - context.AddSource(filename, SourceText.From(generatedSource, Encoding.UTF8)); |
30 |
| - } |
31 |
| - |
32 |
| - IEnumerable<(TypeInfo type, MethodInfo method)> enumerate() |
33 |
| - { |
34 |
| - foreach (var m in receiver.CandidateMethods) |
35 |
| - { |
36 |
| - if (!IsStaticPartial(m)) continue; |
37 |
| - |
38 |
| - var model = compilation.GetSemanticModel(m.SyntaxTree); |
39 |
| - |
40 |
| - if (m.ParameterList.Parameters.Count != 0) continue; |
41 |
| - if (model.GetDeclaredSymbol(m) is not { } methodSymbol) continue; |
42 |
| - if (!ReturnsString(methodSymbol)) continue; |
43 |
| - if (GetUtf8Attribute(methodSymbol) is not { } value) continue; |
44 |
| - |
45 |
| - |
46 |
| - yield return (new(methodSymbol.ContainingType), new(methodSymbol, value)); |
47 |
| - } |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - public void Initialize(GeneratorInitializationContext context) |
52 |
| - { |
53 |
| - context.RegisterForPostInitialization(AddAttribute); |
54 |
| - context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); |
55 |
| - } |
56 |
| - |
57 |
| - class SyntaxReceiver : ISyntaxReceiver |
58 |
| - { |
59 |
| - public List<MethodDeclarationSyntax> CandidateMethods { get; } = new List<MethodDeclarationSyntax>(); |
60 |
| - |
61 |
| - public void OnVisitSyntaxNode(SyntaxNode syntaxNode) |
62 |
| - { |
63 |
| - // any field with at least one attribute is a candidate for property generation |
64 |
| - if (syntaxNode is MethodDeclarationSyntax methodDeclarationSyntax |
65 |
| - && methodDeclarationSyntax.AttributeLists.Count > 0) |
66 |
| - { |
67 |
| - CandidateMethods.Add(methodDeclarationSyntax); |
68 |
| - } |
69 |
| - } |
| 21 | + context.RegisterImplementationSourceOutput(provider, Emit); |
70 | 22 | }
|
71 | 23 | }
|
0 commit comments