From ee2936e0a24c265017d9b67cf5b6aa4095ea585b Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 28 Mar 2026 11:47:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(generator):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E8=8E=B7=E5=8F=96=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8=E7=9A=84=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 ContextSymbols 的创建提取为独立方法 CreateContextSymbols - 将源码生成逻辑提取为独立方法 GenerateSources - 将绑定收集逻辑提取为独立方法 CollectBindings - 将显式绑定添加逻辑提取为独立方法 AddExplicitBindings - 将推断绑定添加逻辑提取为独立方法 AddInferredBindings - 将绑定推断检查逻辑提取为独立方法 CanInferBinding - 优化了代码组织结构和可读性 --- .../Rule/ContextGetGenerator.cs | 194 +++++++++++------- 1 file changed, 121 insertions(+), 73 deletions(-) diff --git a/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs b/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs index d2beb2a..902b0df 100644 --- a/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs +++ b/GFramework.SourceGenerators/Rule/ContextGetGenerator.cs @@ -171,11 +171,22 @@ public sealed class ContextGetGenerator : IIncrementalGenerator var descriptors = ResolveBindingDescriptors(compilation); var getAllAttribute = compilation.GetTypeByMetadataName(GetAllAttributeMetadataName); - if (descriptors.Length == 0 && getAllAttribute is null) return; - var symbols = new ContextSymbols( + var symbols = CreateContextSymbols(compilation); + var workItems = CollectWorkItems( + fieldCandidates, + typeCandidates, + descriptors, + getAllAttribute); + + GenerateSources(context, descriptors, symbols, workItems); + } + + private static ContextSymbols CreateContextSymbols(Compilation compilation) + { + return new ContextSymbols( compilation.GetTypeByMetadataName(ContextAwareAttributeMetadataName), compilation.GetTypeByMetadataName(IContextAwareMetadataName), compilation.GetTypeByMetadataName(ContextAwareBaseMetadataName), @@ -184,83 +195,20 @@ public sealed class ContextGetGenerator : IIncrementalGenerator compilation.GetTypeByMetadataName(IUtilityMetadataName), compilation.GetTypeByMetadataName(IReadOnlyListMetadataName), compilation.GetTypeByMetadataName(GodotNodeMetadataName)); + } - var workItems = CollectWorkItems( - fieldCandidates, - typeCandidates, - descriptors, - getAllAttribute); - + private static void GenerateSources( + SourceProductionContext context, + ImmutableArray descriptors, + ContextSymbols symbols, + Dictionary workItems) + { foreach (var workItem in workItems.Values) { if (!CanGenerateForType(context, workItem, symbols)) continue; - var bindings = new List(); - var explicitFields = new HashSet(SymbolEqualityComparer.Default); - - foreach (var candidate in workItem.FieldCandidates - .OrderBy(static candidate => candidate.Variable.SpanStart) - .ThenBy(static candidate => candidate.FieldSymbol.Name, StringComparer.Ordinal)) - { - var matches = ResolveExplicitBindings(candidate.FieldSymbol, descriptors); - if (matches.Length == 0) - continue; - - explicitFields.Add(candidate.FieldSymbol); - - if (matches.Length > 1) - { - ReportFieldDiagnostic( - context, - ContextGetDiagnostics.MultipleBindingAttributesNotSupported, - candidate); - continue; - } - - if (!TryCreateExplicitBinding( - context, - candidate, - matches[0], - symbols, - out var binding)) - continue; - - bindings.Add(binding); - } - - if (workItem.GetAllDeclaration is not null) - { - foreach (var field in GetAllFields(workItem.TypeSymbol)) - { - if (explicitFields.Contains(field)) - continue; - - if (field.IsStatic) - { - ReportFieldDiagnostic( - context, - ContextGetDiagnostics.StaticFieldNotSupported, - field); - continue; - } - - if (field.IsReadOnly) - { - ReportFieldDiagnostic( - context, - ContextGetDiagnostics.ReadOnlyFieldNotSupported, - field); - continue; - } - - if (!TryCreateInferredBinding(field, symbols, out var binding)) - continue; - - bindings.Add(binding); - } - } - + var bindings = CollectBindings(context, workItem, descriptors, symbols); if (bindings.Count == 0 && workItem.GetAllDeclaration is null) continue; @@ -269,6 +217,106 @@ public sealed class ContextGetGenerator : IIncrementalGenerator } } + private static List CollectBindings( + SourceProductionContext context, + TypeWorkItem workItem, + ImmutableArray descriptors, + ContextSymbols symbols) + { + var bindings = new List(); + var explicitFields = new HashSet(SymbolEqualityComparer.Default); + + AddExplicitBindings(context, workItem, descriptors, symbols, bindings, explicitFields); + AddInferredBindings(context, workItem, symbols, bindings, explicitFields); + + return bindings; + } + + private static void AddExplicitBindings( + SourceProductionContext context, + TypeWorkItem workItem, + ImmutableArray descriptors, + ContextSymbols symbols, + ICollection bindings, + ISet explicitFields) + { + foreach (var candidate in workItem.FieldCandidates + .OrderBy(static candidate => candidate.Variable.SpanStart) + .ThenBy(static candidate => candidate.FieldSymbol.Name, StringComparer.Ordinal)) + { + var matches = ResolveExplicitBindings(candidate.FieldSymbol, descriptors); + if (matches.Length == 0) + continue; + + explicitFields.Add(candidate.FieldSymbol); + + if (matches.Length > 1) + { + ReportFieldDiagnostic( + context, + ContextGetDiagnostics.MultipleBindingAttributesNotSupported, + candidate); + continue; + } + + if (!TryCreateExplicitBinding( + context, + candidate, + matches[0], + symbols, + out var binding)) + continue; + + bindings.Add(binding); + } + } + + private static void AddInferredBindings( + SourceProductionContext context, + TypeWorkItem workItem, + ContextSymbols symbols, + ICollection bindings, + ISet explicitFields) + { + if (workItem.GetAllDeclaration is null) + return; + + foreach (var field in GetAllFields(workItem.TypeSymbol)) + { + if (explicitFields.Contains(field)) + continue; + + if (!CanInferBinding(context, field)) + continue; + + if (!TryCreateInferredBinding(field, symbols, out var binding)) + continue; + + bindings.Add(binding); + } + } + + private static bool CanInferBinding(SourceProductionContext context, IFieldSymbol field) + { + if (field.IsStatic) + { + ReportFieldDiagnostic( + context, + ContextGetDiagnostics.StaticFieldNotSupported, + field); + return false; + } + + if (!field.IsReadOnly) + return true; + + ReportFieldDiagnostic( + context, + ContextGetDiagnostics.ReadOnlyFieldNotSupported, + field); + return false; + } + private static Dictionary CollectWorkItems( ImmutableArray fieldCandidates, ImmutableArray typeCandidates,