mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 新增 PriorityGenerator 源生成器,自动生成 IPrioritized 接口实现 - 添加 PriorityAttribute 特性,用于标记类的优先级值 - 实现 PriorityUsageAnalyzer 分析器,检测优先级使用建议 - 添加预定义的 PriorityGroup 常量,提供标准优先级分组 - 在 AnalyzerReleases.Unshipped.md 中注册新的诊断规则 - 更新项目依赖,升级 Meziantou.Analyzer 和 Polyfill 版本 - 为测试项目添加源生成器项目引用 - 添加 PriorityGenerator 的快照测试用例
134 lines
4.3 KiB
C#
134 lines
4.3 KiB
C#
using System.Text;
|
|
using GFramework.SourceGenerators.Common.constants;
|
|
using GFramework.SourceGenerators.Common.generator;
|
|
using GFramework.SourceGenerators.diagnostics;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
namespace GFramework.SourceGenerators.bases;
|
|
|
|
/// <summary>
|
|
/// Priority 特性生成器,为标记了 [Priority] 的类自动生成 IPrioritized 接口实现
|
|
/// </summary>
|
|
[Generator]
|
|
public sealed class PriorityGenerator : MetadataAttributeClassGeneratorBase
|
|
{
|
|
/// <summary>
|
|
/// 获取特性的元数据名称
|
|
/// </summary>
|
|
protected override string AttributeMetadataName =>
|
|
$"{PathContests.SourceGeneratorsAbstractionsPath}.bases.PriorityAttribute";
|
|
|
|
/// <summary>
|
|
/// 获取特性的短名称(不包含后缀)
|
|
/// </summary>
|
|
protected override string AttributeShortNameWithoutSuffix => "Priority";
|
|
|
|
/// <summary>
|
|
/// 验证符号是否符合生成条件
|
|
/// </summary>
|
|
protected override bool ValidateSymbol(
|
|
SourceProductionContext context,
|
|
Compilation compilation,
|
|
ClassDeclarationSyntax syntax,
|
|
INamedTypeSymbol symbol,
|
|
AttributeData attr)
|
|
{
|
|
// 1. 必须是 class
|
|
if (symbol.TypeKind != TypeKind.Class)
|
|
{
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
PriorityDiagnostic.OnlyApplyToClass,
|
|
syntax.Identifier.GetLocation(),
|
|
symbol.ToDisplayString()));
|
|
return false;
|
|
}
|
|
|
|
// 2. 必须是 partial
|
|
if (!syntax.Modifiers.Any(SyntaxKind.PartialKeyword))
|
|
{
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
PriorityDiagnostic.MustBePartial,
|
|
syntax.Identifier.GetLocation(),
|
|
symbol.Name));
|
|
return false;
|
|
}
|
|
|
|
// 3. 检查是否已手动实现 IPrioritized
|
|
var iPrioritized = compilation.GetTypeByMetadataName(
|
|
$"{PathContests.CoreAbstractionsNamespace}.bases.IPrioritized");
|
|
|
|
if (iPrioritized != null && symbol.AllInterfaces.Contains(iPrioritized, SymbolEqualityComparer.Default))
|
|
{
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
PriorityDiagnostic.AlreadyImplemented,
|
|
syntax.Identifier.GetLocation(),
|
|
symbol.Name));
|
|
return false;
|
|
}
|
|
|
|
// 4. 验证特性参数
|
|
if (attr.ConstructorArguments.Length == 0 ||
|
|
attr.ConstructorArguments[0].Value is not int)
|
|
{
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
PriorityDiagnostic.InvalidValue,
|
|
syntax.Identifier.GetLocation()));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成源代码
|
|
/// </summary>
|
|
protected override string Generate(
|
|
SourceProductionContext context,
|
|
Compilation compilation,
|
|
INamedTypeSymbol symbol,
|
|
AttributeData attr)
|
|
{
|
|
var ns = symbol.ContainingNamespace.IsGlobalNamespace
|
|
? null
|
|
: symbol.ContainingNamespace.ToDisplayString();
|
|
|
|
var priorityValue = (int)attr.ConstructorArguments[0].Value!;
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("// <auto-generated/>");
|
|
sb.AppendLine("#nullable enable");
|
|
sb.AppendLine();
|
|
|
|
if (ns is not null)
|
|
{
|
|
sb.AppendLine($"namespace {ns};");
|
|
sb.AppendLine();
|
|
}
|
|
|
|
// 生成泛型参数(如果有)
|
|
var typeParameters = symbol.TypeParameters.Length > 0
|
|
? $"<{string.Join(", ", symbol.TypeParameters.Select(tp => tp.Name))}>"
|
|
: string.Empty;
|
|
|
|
sb.AppendLine(
|
|
$"partial class {symbol.Name}{typeParameters} : global::GFramework.Core.Abstractions.bases.IPrioritized");
|
|
sb.AppendLine("{");
|
|
sb.AppendLine(" /// <summary>");
|
|
sb.AppendLine($" /// 获取优先级值: {priorityValue}");
|
|
sb.AppendLine(" /// </summary>");
|
|
sb.AppendLine($" public int Priority => {priorityValue};");
|
|
sb.AppendLine("}");
|
|
|
|
return sb.ToString().TrimEnd();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取生成文件的提示名称
|
|
/// </summary>
|
|
protected override string GetHintName(INamedTypeSymbol symbol)
|
|
{
|
|
return $"{symbol.Name}.Priority.g.cs";
|
|
}
|
|
} |