refactor(logging): 优化日志生成器的语法分析实现

- 使用 SyntaxProvider 替换原有的 candidates 遍历方式
- 实现更精确的语法节点匹配逻辑
- 添加对 [Log] 和 [Log(...)] 属性的完整支持
- 改进属性名称匹配,支持命名空间前缀
- 重构代码结构以提高性能和准确性
- 保持原有功能逻辑不变
This commit is contained in:
GwWuYou 2025-12-23 23:20:09 +08:00
parent ce8dca3631
commit 2db8a5d215

View File

@ -22,10 +22,26 @@ namespace GFramework.Generator.generator.logging
/// <param name="context">增量生成器初始化上下文</param>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// 查找所有类声明语法节点,并获取对应的符号信息
var candidates =
// 筛选出带有指定属性标记的类
var targets =
context.SyntaxProvider.CreateSyntaxProvider(
static (node, _) => node is ClassDeclarationSyntax,
static (node, _) =>
{
if (node is not ClassDeclarationSyntax cls)
return false;
// 只要写了 [Log] / [Log(...)] 就命中
return cls.AttributeLists
.SelectMany(a => a.Attributes)
.Any(a =>
{
var name = a.Name.ToString();
return name == "Log"
|| name == "LogAttribute"
|| name.EndsWith(".Log")
|| name.EndsWith(".LogAttribute");
});
},
static (ctx, _) =>
{
var classDecl = (ClassDeclarationSyntax)ctx.Node;
@ -34,16 +50,6 @@ namespace GFramework.Generator.generator.logging
})
.Where(x => x.Symbol is not null);
// 筛选出带有指定属性标记的类
var targets =
candidates.Where(x =>
x.Symbol!.GetAttributes().Any(a =>
{
var c = a.AttributeClass;
if (c is null) return false;
return c.ToDisplayString() == AttributeMetadataName
|| c.Name == "LogAttribute";
}));
// 注册源代码输出,为符合条件的类生成日志相关的源代码
context.RegisterSourceOutput(targets, (spc, pair) =>