|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using EasySourceGenerators.Abstractions; |
| 7 | + |
| 8 | +namespace EasySourceGenerators.Generators.IncrementalGenerators; |
| 9 | + |
| 10 | +[Generator] |
| 11 | +public class MethodGenerator : IIncrementalGenerator |
| 12 | +{ |
| 13 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 14 | + { |
| 15 | + //TODO: Uncomment when new generators code is ready |
| 16 | + |
| 17 | + //IncrementalValueProvider<ImmutableArray<MethodDeclarationSyntax?>> methodsWithAttribute = context.SyntaxProvider |
| 18 | + // .CreateSyntaxProvider( |
| 19 | + // predicate: IsMethodWithGeneratesMethodAttribute, |
| 20 | + // transform: GetMethodDeclaration) |
| 21 | + // .Where(method => method != null) |
| 22 | + // .Collect(); |
| 23 | + |
| 24 | + //context.RegisterSourceOutput( |
| 25 | + // methodsWithAttribute.Combine(context.CompilationProvider), |
| 26 | + // (productionContext, data) => GeneratesMethodGenerationPipeline.Execute(productionContext, data.Left, data.Right)); |
| 27 | + } |
| 28 | + |
| 29 | + private static bool IsMethodWithGeneratesMethodAttribute(SyntaxNode node, CancellationToken _) |
| 30 | + { |
| 31 | + if (node is not MethodDeclarationSyntax method) |
| 32 | + { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + return method.AttributeLists |
| 37 | + .SelectMany(attributeList => attributeList.Attributes) |
| 38 | + .Any(attribute => attribute.Name.ToString() is nameof(MethodBodyGenerator)); |
| 39 | + } |
| 40 | + |
| 41 | + private static MethodDeclarationSyntax? GetMethodDeclaration(GeneratorSyntaxContext context, CancellationToken _) |
| 42 | + { |
| 43 | + return context.Node as MethodDeclarationSyntax; |
| 44 | + } |
| 45 | +} |
0 commit comments