From b95c65a30e50220fea7e82dac51e0ad645042d43 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 22 Mar 2026 15:23:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(generator):=20=E4=BC=98=E5=8C=96GetNod?= =?UTF-8?q?eGenerator=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 - 使用语法树遍历替代字符串匹配来检测注入方法调用 - 添加IsGeneratedInjectionInvocation辅助方法提高代码可读性 - 将字段分组逻辑从列表查找改为字典映射提升性能 - 优化GroupByContainingType方法的时间复杂度 --- .../GetNodeGenerator.cs | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/GFramework.Godot.SourceGenerators/GetNodeGenerator.cs b/GFramework.Godot.SourceGenerators/GetNodeGenerator.cs index 5133b4f..4518608 100644 --- a/GFramework.Godot.SourceGenerators/GetNodeGenerator.cs +++ b/GFramework.Godot.SourceGenerators/GetNodeGenerator.cs @@ -270,20 +270,34 @@ public sealed class GetNodeGenerator : IIncrementalGenerator if (syntaxReference.GetSyntax() is not MethodDeclarationSyntax methodSyntax) continue; - var bodyText = methodSyntax.Body?.ToString(); - if (!string.IsNullOrEmpty(bodyText) && - bodyText.Contains(InjectionMethodName, StringComparison.Ordinal)) - return true; - - var expressionBodyText = methodSyntax.ExpressionBody?.ToString(); - if (!string.IsNullOrEmpty(expressionBodyText) && - expressionBodyText.Contains(InjectionMethodName, StringComparison.Ordinal)) + if (methodSyntax.DescendantNodes() + .OfType() + .Any(IsGeneratedInjectionInvocation)) return true; } return false; } + private static bool IsGeneratedInjectionInvocation(InvocationExpressionSyntax invocation) + { + switch (invocation.Expression) + { + case IdentifierNameSyntax identifierName: + return string.Equals( + identifierName.Identifier.ValueText, + InjectionMethodName, + StringComparison.Ordinal); + case MemberAccessExpressionSyntax memberAccess: + return string.Equals( + memberAccess.Name.Identifier.ValueText, + InjectionMethodName, + StringComparison.Ordinal); + default: + return false; + } + } + private static bool ResolveRequired(AttributeData attribute) { return attribute.GetNamedArgument("Required", true); @@ -479,23 +493,23 @@ public sealed class GetNodeGenerator : IIncrementalGenerator private static IReadOnlyList GroupByContainingType(IEnumerable candidates) { - var groups = new List(); + var groupMap = new Dictionary(SymbolEqualityComparer.Default); + var orderedGroups = new List(); foreach (var candidate in candidates) { - var group = groups.FirstOrDefault(existing => - SymbolEqualityComparer.Default.Equals(existing.TypeSymbol, candidate.FieldSymbol.ContainingType)); - - if (group is null) + var typeSymbol = candidate.FieldSymbol.ContainingType; + if (!groupMap.TryGetValue(typeSymbol, out var group)) { - group = new TypeGroup(candidate.FieldSymbol.ContainingType); - groups.Add(group); + group = new TypeGroup(typeSymbol); + groupMap.Add(typeSymbol, group); + orderedGroups.Add(group); } group.Fields.Add(candidate); } - return groups; + return orderedGroups; } private sealed class FieldCandidate