From 74fe1ab6f473bdae67b896b6ce9be8bf6d181ffb Mon Sep 17 00:00:00 2001 From: Stuart Turner Date: Sat, 18 Jan 2025 10:46:26 -0600 Subject: [PATCH] Improve generator perf (#178) --- .../ImmediateHandlersGenerator.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs index bc5dc6cb..c8dbd643 100644 --- a/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs +++ b/src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs @@ -14,9 +14,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var hasMsDi = context .MetadataReferencesProvider .Collect() - .Select((refs, _) => refs - .Any(r => (r.Display ?? "") - .Contains("Microsoft.Extensions.DependencyInjection.Abstractions"))); + .Select( + (refs, _) => refs + .Any( + r => (r.Display ?? "") + .Contains("Microsoft.Extensions.DependencyInjection.Abstractions") + ) + ); var assemblyName = context.CompilationProvider .Select((cp, _) => cp.AssemblyName! @@ -27,9 +31,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var @namespace = context .AnalyzerConfigOptionsProvider - .Select((c, _) => c.GlobalOptions - .TryGetValue("build_property.rootnamespace", out var ns) - ? ns : null); + .Select( + (c, _) => c.GlobalOptions + .TryGetValue("build_property.rootnamespace", out var ns) + ? ns : null + ); var behaviors = context.SyntaxProvider .ForAttributeWithMetadataName( @@ -43,8 +49,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var handlers = context.SyntaxProvider .ForAttributeWithMetadataName( "Immediate.Handlers.Shared.HandlerAttribute", - predicate: (_, _) => true, - TransformHandler); + predicate: (node, _) => node is TypeDeclarationSyntax, + TransformHandler + ); var handlerNodes = handlers .Combine(behaviors) @@ -58,7 +65,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context) handler: node.Left.Left, behaviors: node.Left.Right, hasMsDi: node.Right, - template) + template + ) ); var registrationNodes = handlers @@ -67,7 +75,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .Combine(behaviors) .Combine(@namespace .Combine(hasMsDi) - .Combine(assemblyName)); + .Combine(assemblyName) + ); context.RegisterSourceOutput( registrationNodes,