From eebd7de409cbccfb5f972b387cec2700b2c7706d Mon Sep 17 00:00:00 2001 From: GwWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 26 Dec 2025 23:10:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(generator):=20=E4=BF=AE=E5=A4=8DContextAwar?= =?UTF-8?q?e=E7=94=9F=E6=88=90=E5=99=A8=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复生成代码末尾多余的换行符问题 - 添加GFramework.Core.rule命名空间引用 - 在测试中添加框架存根代码以提供必要的接口依赖 - 更新解决方案设置以包含新的分析器测试文件 --- .../rule/ContextAwareGeneratorTests.cs | 28 ++++++++++++++----- .../rule/ContextAwareGenerator.cs | 3 +- GFramework.sln.DotSettings.user | 3 ++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs index c1b5b37..dec09f2 100644 --- a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs +++ b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs @@ -30,17 +30,31 @@ public class ContextAwareGeneratorTests } } - namespace TestApp; - - using GFramework.SourceGenerators.Attributes.rule; - - [ContextAware] - public partial class MyRule + namespace TestApp { + using GFramework.SourceGenerators.Attributes.rule; + + [ContextAware] + public partial class MyRule + { + } } """; + const string frameworkStub = """ + namespace GFramework.Core.rule + { + public interface IContextAware + { + void SetContext(GFramework.Core.architecture.IArchitectureContext context); + } + } + namespace GFramework.Core.architecture + { + public interface IArchitectureContext {} + } + """; // 定义期望的生成结果代码 const string expected = """ // @@ -61,7 +75,7 @@ public class ContextAwareGeneratorTests // 执行源代码生成器测试 await GeneratorTest.RunAsync( - source, + source + "\n" + frameworkStub, ("MyRule.ContextAware.g.cs", expected) ); } diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs index 2c2de4f..0f005c9 100644 --- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs +++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs @@ -105,9 +105,8 @@ public sealed class ContextAwareGenerator : IIncrementalGenerator sb.AppendLine(" Context = context;"); sb.AppendLine(" }"); - sb.AppendLine("}"); - return sb.ToString(); + return sb.ToString().TrimEnd(); } } \ No newline at end of file diff --git a/GFramework.sln.DotSettings.user b/GFramework.sln.DotSettings.user index 8865a13..51ff59d 100644 --- a/GFramework.sln.DotSettings.user +++ b/GFramework.sln.DotSettings.user @@ -1,6 +1,9 @@  + ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded <SessionState ContinuousTestingMode="0" IsActive="True" Name="Generates_ContextAware_Code" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <TestAncestor> <TestId>NUnit3x::BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E::net8.0::GFramework.SourceGenerators.Tests.rule.ContextAwareGeneratorTests</TestId>