整理代码

This commit is contained in:
GG Z
2025-09-03 22:11:56 +08:00
parent da46257be2
commit b25f8d7ec9
4 changed files with 16 additions and 27 deletions

View File

@@ -10,7 +10,6 @@ namespace RevitGen.Generator
[Generator(LanguageNames.CSharp)] [Generator(LanguageNames.CSharp)]
public class RevitCommandGenerator : ISourceGenerator public class RevitCommandGenerator : ISourceGenerator
{ {
// ★★ 1. 修正常量,使用 RevitCommandAttribute ★★
private const string RevitCommandAttributeFullName = "RevitGen.Attributes.RevitCommandAttribute"; private const string RevitCommandAttributeFullName = "RevitGen.Attributes.RevitCommandAttribute";
private const string CommandHandlerAttributeFullName = "RevitGen.Attributes.CommandHandlerAttribute"; private const string CommandHandlerAttributeFullName = "RevitGen.Attributes.CommandHandlerAttribute";
@@ -27,18 +26,15 @@ namespace RevitGen.Generator
if (!(context.SyntaxReceiver is SyntaxReceiver receiver)) if (!(context.SyntaxReceiver is SyntaxReceiver receiver))
{ {
// ... [日志和返回逻辑不变] ...
return; return;
} }
log.AppendLine($"// Candidate classes found by SyntaxReceiver: {receiver.CandidateClasses.Count}"); log.AppendLine($"// Candidate classes found by SyntaxReceiver: {receiver.CandidateClasses.Count}");
if (receiver.CandidateClasses.Count == 0) if (receiver.CandidateClasses.Count == 0)
{ {
// ... [日志和返回逻辑不变] ...
return; return;
} }
// ★★ 2. 确保我们查找的是正确的特性符号 ★★
var attributeSymbol = context.Compilation.GetTypeByMetadataName(RevitCommandAttributeFullName); var attributeSymbol = context.Compilation.GetTypeByMetadataName(RevitCommandAttributeFullName);
if (attributeSymbol == null) if (attributeSymbol == null)
{ {
@@ -80,7 +76,6 @@ namespace RevitGen.Generator
if (commandClasses.Any()) if (commandClasses.Any())
{ {
// ... [生成 partial 类和 App 类的逻辑不变] ...
foreach (var classSymbol in commandClasses) foreach (var classSymbol in commandClasses)
{ {
var partialClassSource = SourceGenerationHelper.GenerateCommandPartialClass(classSymbol); var partialClassSource = SourceGenerationHelper.GenerateCommandPartialClass(classSymbol);

View File

@@ -12,7 +12,6 @@ namespace RevitGen.Generator
/// </summary> /// </summary>
internal static class SourceGenerationHelper internal static class SourceGenerationHelper
{ {
// 定义我们属性的完整名称,以使用最终确定的、正确的命名方案。
private const string RevitCommandAttributeFullName = "RevitGen.Attributes.RevitCommandAttribute"; private const string RevitCommandAttributeFullName = "RevitGen.Attributes.RevitCommandAttribute";
private const string CommandHandlerAttributeFullName = "RevitGen.Attributes.CommandHandlerAttribute"; private const string CommandHandlerAttributeFullName = "RevitGen.Attributes.CommandHandlerAttribute";

View File

@@ -17,24 +17,24 @@ namespace RevitGen.Generator
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{ {
// 检查节点是否是一个类声明,并且它带有属性 // 检查节点是否是一个类声明,并且它带有属性
//if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax &&
// classDeclarationSyntax.AttributeLists.Count > 0)
//{
// // 检查类是否被声明为 partial
// foreach (var modifier in classDeclarationSyntax.Modifiers)
// {
// if (modifier.ValueText == "partial")
// {
// CandidateClasses.Add(classDeclarationSyntax);
// break;
// }
// }
//}
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax && if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax &&
classDeclarationSyntax.AttributeLists.Count > 0) classDeclarationSyntax.AttributeLists.Count > 0)
{
// 检查类是否被声明为 partial
foreach (var modifier in classDeclarationSyntax.Modifiers)
{
if (modifier.ValueText == "partial")
{ {
CandidateClasses.Add(classDeclarationSyntax); CandidateClasses.Add(classDeclarationSyntax);
break;
} }
} }
} }
//if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax &&
// classDeclarationSyntax.AttributeLists.Count > 0)
//{
// CandidateClasses.Add(classDeclarationSyntax);
//}
}
}
} }

View File

@@ -1,10 +1,5 @@
using System; using Autodesk.Revit.DB;
using System.Drawing;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI; using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using RevitGen.Attributes; using RevitGen.Attributes;