|
| 1 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 2 | + |
1 | 3 | namespace Thinktecture.CodeAnalysis.Annotations; |
2 | 4 |
|
3 | 5 | [Generator] |
4 | | -public class AnnotationsSourceGenerator : ThinktectureSourceGeneratorBase, IIncrementalGenerator |
| 6 | +public class AnnotationsSourceGenerator() : ThinktectureSourceGeneratorBase(1), IIncrementalGenerator |
5 | 7 | { |
6 | | - public AnnotationsSourceGenerator() |
7 | | - : base(1) |
8 | | - { |
9 | | - } |
| 8 | + private const string _INSTANT_HANDLE_ATTRIBUTE = "InstantHandleAttribute"; |
10 | 9 |
|
11 | 10 | public void Initialize(IncrementalGeneratorInitializationContext context) |
12 | 11 | { |
13 | 12 | var options = GetGeneratorOptions(context); |
14 | 13 |
|
15 | | - var annotationsCheck = context.MetadataReferencesProvider |
16 | | - .Select((reference, _) => HasJetbrainsAnnotations(reference)) |
17 | | - .Collect(); |
| 14 | + var localAttribute = context.SyntaxProvider |
| 15 | + .CreateSyntaxProvider( |
| 16 | + (node, _) => node is ClassDeclarationSyntax { Identifier.Text: _INSTANT_HANDLE_ATTRIBUTE }, |
| 17 | + (ctx, token) => IsInstantHandleAttribute(ctx.SemanticModel.GetDeclaredSymbol(ctx.Node, token))) |
| 18 | + .Where(hasLocal => hasLocal) |
| 19 | + .Collect(); |
| 20 | + |
| 21 | + var referencedAttribute = context.MetadataReferencesProvider |
| 22 | + .Select((reference, _) => HasJetbrainsAnnotations(reference)) |
| 23 | + .Where(hasAttribute => hasAttribute) |
| 24 | + .Collect(); |
| 25 | + |
| 26 | + var pipeline = localAttribute |
| 27 | + .Combine(referencedAttribute.Combine(options)) |
| 28 | + .SelectMany((tuple, _) => |
| 29 | + { |
| 30 | + var (localAttributes, (referencedAttributes, opts)) = tuple; |
18 | 31 |
|
19 | | - context.RegisterSourceOutput( |
20 | | - annotationsCheck.Combine(options) |
21 | | - .SelectMany((tuple, _) => tuple.Left.Any(hasAnnotations => hasAnnotations) || !tuple.Right.GenerateJetbrainsAnnotations ? ImmutableArray<bool>.Empty : [false]), |
22 | | - (ctx, _) => AddAnnotations(ctx)); |
| 32 | + return !localAttributes.IsDefaultOrEmpty || !referencedAttributes.IsDefaultOrEmpty || !opts.GenerateJetbrainsAnnotations ? ImmutableArray<bool>.Empty : [true]; |
| 33 | + }); |
| 34 | + |
| 35 | + context.RegisterSourceOutput(pipeline, (ctx, _) => AddAnnotations(ctx)); |
| 36 | + } |
| 37 | + |
| 38 | + private static bool IsInstantHandleAttribute(ISymbol? symbol) |
| 39 | + { |
| 40 | + return symbol is |
| 41 | + { |
| 42 | + Name: _INSTANT_HANDLE_ATTRIBUTE, ContainingNamespace: |
| 43 | + { |
| 44 | + Name: "Annotations", |
| 45 | + ContainingNamespace : |
| 46 | + { |
| 47 | + Name: "JetBrains", |
| 48 | + ContainingNamespace.IsGlobalNamespace: true |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
23 | 52 | } |
24 | 53 |
|
25 | | - private bool HasJetbrainsAnnotations(MetadataReference reference) |
| 54 | + private static bool HasJetbrainsAnnotations(MetadataReference reference) |
26 | 55 | { |
27 | 56 | try |
28 | 57 | { |
|
0 commit comments