mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-12 22:03:30 +08:00
feat(godot): 添加导出集合自动注册生成器功能
- 实现了 AutoRegisterExportedCollectionsGenerator 源生成器 - 支持扫描标记了 AutoRegisterExportedCollectionsAttribute 的 partial 类型 - 为使用 RegisterExportedCollectionAttribute 声明的集合成员生成集中注册方法 - 提供详细的诊断支持,包括 GF_AutoExport_001 到 GF_AutoExport_008 错误码 - 支持从基类和接口继承链查找注册方法 - 实现了完整的单元测试覆盖各种使用场景 - 验证集合可枚举性、元素类型推导和注册表成员可访问性 - 生成安全的空值检查代码防止运行时异常 - 支持泛型类型约束和复杂继承关系的处理
This commit is contained in:
parent
812235a243
commit
eeef5961d7
@ -278,6 +278,66 @@ public class AutoRegisterExportedCollectionsGeneratorTests
|
|||||||
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Reports_Diagnostic_When_Register_Method_Is_Only_Explicitly_Implemented_Interface_Member()
|
||||||
|
{
|
||||||
|
const string source = """
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
||||||
|
|
||||||
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class RegisterExportedCollectionAttribute : Attribute
|
||||||
|
{
|
||||||
|
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace TestApp
|
||||||
|
{
|
||||||
|
public interface IRegistry
|
||||||
|
{
|
||||||
|
void Register(int value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ExplicitRegistry : IRegistry
|
||||||
|
{
|
||||||
|
void IRegistry.Register(int value) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
[AutoRegisterExportedCollections]
|
||||||
|
public partial class Bootstrapper
|
||||||
|
{
|
||||||
|
private readonly ExplicitRegistry _registry = new();
|
||||||
|
|
||||||
|
[RegisterExportedCollection(nameof(_registry), "Register")]
|
||||||
|
public List<int> {|#0:Values|} { get; } = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
||||||
|
{
|
||||||
|
TestState =
|
||||||
|
{
|
||||||
|
Sources = { source }
|
||||||
|
},
|
||||||
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
||||||
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
||||||
|
};
|
||||||
|
|
||||||
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_003", DiagnosticSeverity.Error)
|
||||||
|
.WithLocation(0)
|
||||||
|
.WithArguments("Register", "_registry", "Values"));
|
||||||
|
|
||||||
|
await test.RunAsync();
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task Generates_Batch_Registration_Method_When_Register_Method_Comes_From_Base_Class()
|
public async Task Generates_Batch_Registration_Method_When_Register_Method_Comes_From_Base_Class()
|
||||||
{
|
{
|
||||||
@ -348,6 +408,75 @@ public class AutoRegisterExportedCollectionsGeneratorTests
|
|||||||
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Generates_Batch_Registration_Method_When_Registry_Member_Comes_From_Base_Class()
|
||||||
|
{
|
||||||
|
const string source = """
|
||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
||||||
|
|
||||||
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class RegisterExportedCollectionAttribute : Attribute
|
||||||
|
{
|
||||||
|
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace TestApp
|
||||||
|
{
|
||||||
|
public sealed class IntRegistry
|
||||||
|
{
|
||||||
|
public void Register(int value) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class BootstrapperBase
|
||||||
|
{
|
||||||
|
protected readonly IntRegistry? _registry = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
[AutoRegisterExportedCollections]
|
||||||
|
public partial class Bootstrapper : BootstrapperBase
|
||||||
|
{
|
||||||
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
||||||
|
public List<int>? Values { get; } = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
const string expected = """
|
||||||
|
// <auto-generated />
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
|
namespace TestApp;
|
||||||
|
|
||||||
|
partial class Bootstrapper
|
||||||
|
{
|
||||||
|
private void __RegisterExportedCollections_Generated()
|
||||||
|
{
|
||||||
|
if (this.Values is not null && this._registry is not null)
|
||||||
|
{
|
||||||
|
foreach (var __generatedItem in this.Values)
|
||||||
|
{
|
||||||
|
this._registry.Register(__generatedItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
""";
|
||||||
|
|
||||||
|
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
|
||||||
|
source,
|
||||||
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task Reports_Diagnostic_When_Collection_Member_Is_Not_Instance_Readable()
|
public async Task Reports_Diagnostic_When_Collection_Member_Is_Not_Instance_Readable()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -222,8 +222,7 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
|
|||||||
out var registerMethodName))
|
out var registerMethodName))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var registryMember = ownerType.GetMembers(registryMemberName)
|
var registryMember = FindRegistryMember(ownerType, registryMemberName);
|
||||||
.FirstOrDefault(member => member is IFieldSymbol or IPropertySymbol);
|
|
||||||
|
|
||||||
if (registryMember is null)
|
if (registryMember is null)
|
||||||
{
|
{
|
||||||
@ -236,7 +235,8 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsInstanceReadableMember(registryMember))
|
if (!IsInstanceReadableMember(registryMember) ||
|
||||||
|
!compilation.IsSymbolAccessibleWithin(registryMember, ownerType))
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(Diagnostic.Create(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
AutoRegisterExportedCollectionsDiagnostics.RegistryMemberMustBeInstanceReadable,
|
AutoRegisterExportedCollectionsDiagnostics.RegistryMemberMustBeInstanceReadable,
|
||||||
@ -318,19 +318,40 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
|
|||||||
return compilation.ClassifyConversion(elementType, parameterType).IsImplicit;
|
return compilation.ClassifyConversion(elementType, parameterType).IsImplicit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ISymbol? FindRegistryMember(
|
||||||
|
INamedTypeSymbol ownerType,
|
||||||
|
string registryMemberName)
|
||||||
|
{
|
||||||
|
for (var currentType = ownerType; currentType is not null; currentType = currentType.BaseType)
|
||||||
|
{
|
||||||
|
// Search the owner hierarchy one level at a time so the generator follows the same
|
||||||
|
// name-hiding order as `this.<member>` in generated code.
|
||||||
|
var candidateMember = currentType.GetMembers(registryMemberName)
|
||||||
|
.FirstOrDefault(static member => member is IFieldSymbol or IPropertySymbol);
|
||||||
|
|
||||||
|
if (candidateMember is not null)
|
||||||
|
return candidateMember;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举给定注册表类型上可能承载批量注册入口的候选实例方法。
|
/// 枚举给定注册表类型上可能承载批量注册入口的候选实例方法。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="registryType">声明注册表成员的静态类型。</param>
|
/// <param name="registryType">声明注册表成员的静态类型。</param>
|
||||||
/// <param name="registerMethodName">特性参数中声明的注册方法名称。</param>
|
/// <param name="registerMethodName">特性参数中声明的注册方法名称。</param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// 按“当前类型 -> 基类链 -> 已实现接口”顺序返回所有同名方法,供后续签名和可访问性筛选使用。
|
/// 按“当前类型 -> 基类链 -> 接口继承链(仅当静态类型本身是接口)”顺序返回所有同名方法,
|
||||||
|
/// 供后续签名和可访问性筛选使用。
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// 生成器需要沿这三条继承路径查找方法,因为用户代码可能通过派生类字段引用基类实现,
|
/// 生成器需要沿当前类型和基类链查找方法,因为用户代码可能通过派生类字段引用基类实现;
|
||||||
/// 或通过接口类型引用由上层接口声明的契约方法。这里故意不做去重:同一个语义方法可能同时经由
|
/// 当注册表成员本身声明为接口类型时,还要继续沿接口继承链查找由父接口声明的契约方法。
|
||||||
/// 覆盖链、接口继承或显式声明被枚举多次,但当前调用方只使用 <c>Any</c> 判断“是否存在至少一个可用候选”,
|
/// 对类或结构体不遍历 <see cref="INamedTypeSymbol.AllInterfaces"/>,避免把仅能通过接口调用的显式实现
|
||||||
/// 因此重复项只会带来额外的符号检查成本,不会改变生成结果或诊断边界。
|
/// 误判为可由 <c>this.<registry>.<method>(...)</c> 直接访问的方法。
|
||||||
|
/// 这里故意不做去重:同一个语义方法可能同时经由覆盖链、接口继承或显式声明被枚举多次,但当前调用方只使用
|
||||||
|
/// <c>Any</c> 判断“是否存在至少一个可用候选”,因此重复项只会带来额外的符号检查成本,不会改变生成结果或诊断边界。
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
private static IEnumerable<IMethodSymbol> EnumerateCandidateMethods(
|
private static IEnumerable<IMethodSymbol> EnumerateCandidateMethods(
|
||||||
INamedTypeSymbol registryType,
|
INamedTypeSymbol registryType,
|
||||||
@ -345,6 +366,9 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
|
|||||||
yield return method;
|
yield return method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (registryType.TypeKind != TypeKind.Interface)
|
||||||
|
yield break;
|
||||||
|
|
||||||
foreach (var interfaceType in registryType.AllInterfaces)
|
foreach (var interfaceType in registryType.AllInterfaces)
|
||||||
{
|
{
|
||||||
foreach (var method in interfaceType.GetMembers(registerMethodName).OfType<IMethodSymbol>())
|
foreach (var method in interfaceType.GetMembers(registerMethodName).OfType<IMethodSymbol>())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user