XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Modified
XFEExtension.NetCore.Analyzer/Generator/ConstExpressionAutoGenerator.cs
+0
-6
Deleted
XFEExtension.NetCore.Analyzer/Generator/ImplementAutoGenerator.cs
+0
-108
XFEstudio/XFEExtension
标题:简化`ConstExpressionAutoGenerator.cs`并移除`ImplementAutoGenerator.cs`
详细描述: 在`ConstExpressionAutoGenerator.cs`文件中,我们移除了四个不再需要的命名空间引用,并删除了在`OnVisitSyntaxNode`方法中查找带有特定属性的字段的代码。同时,也移除了对解析字段初始值的注释。 此外,我们完全移除了`ImplementAutoGenerator.cs`文件中的所有代码,包括`ImplementAutoGenerator`类的定义以及该类中的所有方法和属性。这可能意味着我们不再需要这个源代码生成器,或者其功能已经被移动到其他地方。 这些修改可能是为了简化代码,或者是因为某些功能已经不再需要。
355597f
代码差异
2 个文件
+0
-114
@@ -1,10 +1,6 @@
1
1
using Microsoft.CodeAnalysis;
2
using Microsoft.CodeAnalysis.CSharp;
3
2
using Microsoft.CodeAnalysis.CSharp.Syntax;
4
using Microsoft.CodeAnalysis.Text;
5
3
using System.Collections.Generic;
6
using System.Linq;
7
using System.Text;
8
4
9
5
namespace XFEExtension.NetCore.Analyzer.Generator
10
6
{
@@ -19,7 +15,6 @@ namespace XFEExtension.NetCore.Analyzer.Generator
19
15
foreach (var field in receiver.CandidateFields)
20
16
{
21
17
// TODO: 解析字段的初始值,执行方法并获取返回值
22
// 注意:这是一个非常困难的任务,可能需要运行时环境
23
18
24
19
// TODO: 在新的部分类中生成一个const常量,其值为方法的返回值
25
20
}
@@ -37,7 +32,6 @@ namespace XFEExtension.NetCore.Analyzer.Generator
37
32
38
33
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
39
34
{
40
// TODO: 查找带有特定属性的字段
41
35
if (syntaxNode is FieldDeclarationSyntax fieldDeclarationSyntax
42
36
&& fieldDeclarationSyntax.AttributeLists.Count > 0)
43
37
{
@@ -1,108 +0,0 @@
1
using Microsoft.CodeAnalysis;
2
using Microsoft.CodeAnalysis.CSharp;
3
using Microsoft.CodeAnalysis.CSharp.Syntax;
4
using System.Linq;
5
6
namespace XFEExtension.NetCore.Analyzer.Generator
7
{
8
[Generator]
9
public class ImplementAutoGenerator : ISourceGenerator
10
{
11
public void Initialize(GeneratorInitializationContext context)
12
{
13
}
14
15
public void Execute(GeneratorExecutionContext context)
16
{
17
if (!GeneratorOptions.AutoImplement)
18
return;
19
var syntaxTrees = context.Compilation.SyntaxTrees;
20
foreach (var syntaxTree in syntaxTrees)
21
{
22
var root = syntaxTree.GetRoot();
23
var classDeclarations = root.DescendantNodes().OfType<ClassDeclarationSyntax>()
24
.Where(classDeclaration => classDeclaration.AttributeLists.Any(IsCreateImplAttribute));
25
var usingDirectives = root.DescendantNodes().OfType<UsingDirectiveSyntax>().ToArray();
26
FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax = null;
27
var namespaceResults = root.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>();
28
if (namespaceResults != null && namespaceResults.Count() > 0)
29
fileScopedNamespaceDeclarationSyntax = namespaceResults.First();
30
foreach (var classDeclaration in classDeclarations)
31
{
32
var className = classDeclaration.Identifier.ValueText;
33
var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives, fileScopedNamespaceDeclarationSyntax);
34
context.AddSource($"{className}Impl.g.cs", implementationSyntaxTree.ToString());
35
}
36
}
37
}
38
39
private static bool IsCreateImplAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "CreateImpl");
40
41
private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
42
{
43
var className = classDeclaration.Identifier.ValueText;
44
ClassDeclarationSyntax implementationClass;
45
if (classDeclaration.ParameterList is null)
46
{
47
implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
48
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword), SyntaxFactory.Token(SyntaxKind.SealedKeyword))
49
.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(className)))
50
//.AddMembers(classDeclaration.Members.OfType<ConstructorDeclarationSyntax>().Select(constructor =>
51
//{
52
// return constructor.WithIdentifier(SyntaxFactory.Identifier($"{className}Impl"))
53
// .WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(constructor.ParameterList.Parameters.Select(parameter =>
54
// SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)))))));
55
//}).ToArray())
56
.AddMembers(classDeclaration.Members.OfType<ConstructorDeclarationSyntax>().Select(constructor =>
57
{
58
return SyntaxFactory.ConstructorDeclaration($"{className}Impl")
59
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
60
.WithBody(SyntaxFactory.Block())
61
.WithParameterList(constructor.ParameterList)
62
.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(constructor.ParameterList.Parameters.Select(parameter => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)))))));
63
}).ToArray())
64
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
65
/// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
66
/// </summary>
67
"))
68
.NormalizeWhitespace();
69
}
70
else
71
{
72
implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
73
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
74
.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(className)))
75
.AddMembers(SyntaxFactory.ConstructorDeclaration($"{className}Impl")
76
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
77
.WithBody(SyntaxFactory.Block())
78
.WithParameterList(classDeclaration.ParameterList)
79
.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(classDeclaration.ParameterList.Parameters.Select(parameter => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier))))))))
80
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
81
/// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
82
/// </summary>
83
"))
84
.NormalizeWhitespace();
85
}
86
MemberDeclarationSyntax memberDeclaration;
87
if (fileScopedNamespaceDeclarationSyntax is null)
88
{
89
var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
90
if (namespaceDeclaration is null)
91
memberDeclaration = implementationClass;
92
else
93
memberDeclaration = SyntaxFactory.NamespaceDeclaration(namespaceDeclaration.Name)
94
.AddMembers(implementationClass);
95
}
96
else
97
{
98
memberDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(fileScopedNamespaceDeclarationSyntax.Name)
99
.AddMembers(implementationClass);
100
}
101
var implementationCompilationUnit = SyntaxFactory.CompilationUnit()
102
.AddUsings(usingDirectiveSyntaxes)
103
.AddMembers(memberDeclaration)
104
.NormalizeWhitespace();
105
return SyntaxFactory.SyntaxTree(implementationCompilationUnit);
106
}
107
}
108
}