From 42d6590edddc9850d38324f70643dfa7b3ad24ce Mon Sep 17 00:00:00 2001
From: GwWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Thu, 1 Jan 2026 10:55:13 +0800
Subject: [PATCH] =?UTF-8?q?refactor(generator):=20=E9=87=8D=E6=9E=84Contex?=
=?UTF-8?q?tAwareGenerator=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 移除IContextAware接口的继承要求
- 添加partial关键字检查验证
- 更新诊断规则描述和错误消息
- 修改代码生成逻辑以自动实现IContextAware接口
- 调整Analyzer发布文档格式和链接地址
---
.../controller/IController.cs | 14 +++++-----
.../AnalyzerReleases.Shipped.md | 2 +-
.../AnalyzerReleases.Unshipped.md | 9 +++----
.../diagnostics/ContextAwareDiagnostic.cs | 15 +++--------
.../rule/ContextAwareGenerator.cs | 27 +++++++++++++------
5 files changed, 35 insertions(+), 32 deletions(-)
diff --git a/GFramework.Core.Abstractions/controller/IController.cs b/GFramework.Core.Abstractions/controller/IController.cs
index e667209..4f39bb3 100644
--- a/GFramework.Core.Abstractions/controller/IController.cs
+++ b/GFramework.Core.Abstractions/controller/IController.cs
@@ -1,10 +1,10 @@
-using GFramework.Core.Abstractions.rule;
-
-namespace GFramework.Core.Abstractions.controller;
+namespace GFramework.Core.Abstractions.controller;
///
-/// 控制器接口,定义了控制器需要实现的所有功能契约
-/// 该接口继承了多个框架核心接口,用于支持控制器的各种能力
-/// 包括架构归属、命令发送、系统获取、模型获取、事件注册、查询发送和工具获取等功能
+/// 控制器接口,定义了控制器的基本契约和行为规范
///
-public interface IController : IContextAware;
\ No newline at end of file
+///
+/// 该接口为框架中的控制器组件提供统一的抽象定义,
+/// 用于实现控制器的标准功能和生命周期管理
+///
+public interface IController;
\ No newline at end of file
diff --git a/GFramework.SourceGenerators/AnalyzerReleases.Shipped.md b/GFramework.SourceGenerators/AnalyzerReleases.Shipped.md
index 60b59dd..2080dcd 100644
--- a/GFramework.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/GFramework.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -1,3 +1,3 @@
; Shipped analyzer releases
-; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
+; https://github.com/dotnet/roslyn/blob/main/src/RoslynAnalyzers/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
diff --git a/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md b/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md
index db19379..76924ff 100644
--- a/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md
+++ b/GFramework.SourceGenerators/AnalyzerReleases.Unshipped.md
@@ -1,9 +1,8 @@
; Unshipped analyzer release
-; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
+; https://github.com/dotnet/roslyn/blob/main/src/RoslynAnalyzers/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
### New Rules
- Rule ID | Category | Severity | Notes
-----------------|----------------------------------|----------|------------------------
- GF_Logging_001 | GFramework.Godot.Logging | Warning | LoggerDiagnostics
- GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic
\ No newline at end of file
+ Rule ID | Category | Severity | Notes
+-------------|----------------------------------|----------|------------------------
+ GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic
\ No newline at end of file
diff --git a/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs b/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs
index 80f50d7..d32841d 100644
--- a/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs
+++ b/GFramework.SourceGenerators/diagnostics/ContextAwareDiagnostic.cs
@@ -8,19 +8,12 @@ namespace GFramework.SourceGenerators.diagnostics;
public static class ContextAwareDiagnostic
{
///
- /// 定义类必须实现IContextAware接口的诊断规则
+ /// 诊断规则:ContextAwareAttribute只能应用于类
///
- ///
- /// 诊断ID: GF_Rule_001
- /// 诊断类别: GFramework.SourceGenerators.rule
- /// 严重级别: 错误
- /// 启用状态: true
- /// 消息格式: "Class '{0}' must implement IContextAware"
- ///
- public static readonly DiagnosticDescriptor ClassMustImplementIContextAware = new(
+ public static readonly DiagnosticDescriptor ContextAwareOnlyForClass = new(
"GF_Rule_001",
- "Class must implement IContextAware",
- "Class '{0}' must implement IContextAware",
+ "ContextAware can only be applied to class",
+ "ContextAwareAttribute can only be applied to class '{0}'",
"GFramework.SourceGenerators.rule",
DiagnosticSeverity.Error,
true
diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
index 99e80a6..4caffa4 100644
--- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
+++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
@@ -1,9 +1,11 @@
using System.Linq;
using System.Text;
using GFramework.SourceGenerators.Common.constants;
+using GFramework.SourceGenerators.Common.diagnostics;
using GFramework.SourceGenerators.Common.generator;
using GFramework.SourceGenerators.diagnostics;
using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace GFramework.SourceGenerators.rule;
@@ -41,15 +43,21 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
INamedTypeSymbol symbol,
AttributeData attr)
{
- var iContextAware = compilation.GetTypeByMetadataName(
- $"{PathContests.CoreAbstractionsNamespace}.rule.IContextAware");
-
- if (iContextAware is null ||
- !symbol.AllInterfaces.Any(i =>
- SymbolEqualityComparer.Default.Equals(i, iContextAware)))
+ // 1. 必须是 partial
+ if (!syntax.Modifiers.Any(SyntaxKind.PartialKeyword))
{
context.ReportDiagnostic(Diagnostic.Create(
- ContextAwareDiagnostic.ClassMustImplementIContextAware,
+ CommonDiagnostics.ClassMustBePartial,
+ syntax.Identifier.GetLocation(),
+ symbol.Name));
+ return false;
+ }
+
+ // 2. 必须是 class
+ if (symbol.TypeKind != TypeKind.Class)
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ ContextAwareDiagnostic.ContextAwareOnlyForClass,
syntax.Identifier.GetLocation(),
symbol.Name));
return false;
@@ -58,6 +66,7 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
return true;
}
+
///
/// 生成源代码
///
@@ -90,7 +99,9 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
sb.AppendLine();
}
- sb.AppendLine($"partial class {symbol.Name}");
+ var interfaceName = iContextAware.ToDisplayString(
+ SymbolDisplayFormat.FullyQualifiedFormat);
+ sb.AppendLine($"partial class {symbol.Name} : {interfaceName}");
sb.AppendLine("{");
GenerateContextProperty(sb);