Skip to content

Commit 0c4ea40

Browse files
committed
添加 X11Sharp.SourceGenerator 并生成 IXid 类型转换方法
1 parent b36fc53 commit 0c4ea40

File tree

12 files changed

+134
-182
lines changed

12 files changed

+134
-182
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
4+
namespace SeWzc.X11Sharp.SourceGenerator;
5+
6+
[Generator(LanguageNames.CSharp)]
7+
public class IncrementalGenerator : IIncrementalGenerator
8+
{
9+
/// <inheritdoc />
10+
public void Initialize(IncrementalGeneratorInitializationContext context)
11+
{
12+
var xids = context.SyntaxProvider
13+
.CreateSyntaxProvider(
14+
static (node, token) =>
15+
{
16+
if (node is TypeDeclarationSyntax { BaseList.Types: { } types })
17+
{
18+
// 检查是否有 IXid 接口
19+
return types.Any(type => type.Type is IdentifierNameSyntax { Identifier.ValueText: "IXid" });
20+
}
21+
22+
return false;
23+
},
24+
static (context, token) =>
25+
{
26+
// 获取类型声明语法节点
27+
var typeDeclaration = (TypeDeclarationSyntax)context.Node;
28+
29+
// 获取类型声明的命名空间
30+
var namespaceDeclarationSyntax = typeDeclaration.Ancestors().OfType<BaseNamespaceDeclarationSyntax>().First();
31+
var namespaceName = namespaceDeclarationSyntax.Name;
32+
33+
// 获取类型名称
34+
var typeName = typeDeclaration.Identifier.ValueText;
35+
36+
// 返回类型名称和类型声明语法节点
37+
return (typeDeclaration, namespaceName);
38+
});
39+
40+
context.RegisterSourceOutput(xids,
41+
static (context, source) =>
42+
{
43+
// 获取类型名称和类型声明语法节点
44+
var (typeDeclaration, namespaceName) = source;
45+
46+
var keyword = typeDeclaration.Keyword.ToString();
47+
if (typeDeclaration is RecordDeclarationSyntax recordDeclaration)
48+
{
49+
// 处理记录类型
50+
keyword += " " + recordDeclaration.ClassOrStructKeyword;
51+
}
52+
53+
// 生成源代码
54+
var sourceCode = $$"""
55+
// <auto-generated/>
56+
using SeWzc.X11Sharp.Structs;
57+
58+
namespace {{namespaceName}};
59+
60+
partial {{keyword}} {{typeDeclaration.Identifier}}
61+
{
62+
#region 运算符重载
63+
64+
// 强制转换不需要文档
65+
#pragma warning disable CS1591
66+
67+
public static implicit operator ULong({{typeDeclaration.Identifier}} value)
68+
{
69+
return value.Id;
70+
}
71+
72+
public static implicit operator nuint({{typeDeclaration.Identifier}} value)
73+
{
74+
return value.Id;
75+
}
76+
77+
public static implicit operator nint({{typeDeclaration.Identifier}} value)
78+
{
79+
return (nint)value.Id;
80+
}
81+
82+
public static implicit operator int({{typeDeclaration.Identifier}} value)
83+
{
84+
return value.ToInt32();
85+
}
86+
87+
public static implicit operator uint({{typeDeclaration.Identifier}} value)
88+
{
89+
return value.ToUInt32();
90+
}
91+
92+
#pragma warning restore CS1591
93+
94+
#endregion
95+
}
96+
""";
97+
98+
context.AddSource($"{typeDeclaration.Identifier.ValueText}.g.cs", sourceCode);
99+
});
100+
}
101+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
8+
<LangVersion>latest</LangVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
13+
</ItemGroup>
14+
15+
</Project>

SeWzc.X11Sharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeWzc.X11Sharp", "SeWzc.X11Sharp\SeWzc.X11Sharp.csproj", "{8A1A69CA-1A76-4CC7-A0EF-AC9012870527}"
44
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeWzc.X11Sharp.SourceGenerator", "SeWzc.X11Sharp.SourceGenerator\SeWzc.X11Sharp.SourceGenerator.csproj", "{1C820C47-9166-4534-9C64-7F91B21D5F93}"
6+
EndProject
57
Global
68
GlobalSection(SolutionConfigurationPlatforms) = preSolution
79
Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
1214
{8A1A69CA-1A76-4CC7-A0EF-AC9012870527}.Debug|Any CPU.Build.0 = Debug|Any CPU
1315
{8A1A69CA-1A76-4CC7-A0EF-AC9012870527}.Release|Any CPU.ActiveCfg = Release|Any CPU
1416
{8A1A69CA-1A76-4CC7-A0EF-AC9012870527}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{1C820C47-9166-4534-9C64-7F91B21D5F93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{1C820C47-9166-4534-9C64-7F91B21D5F93}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{1C820C47-9166-4534-9C64-7F91B21D5F93}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{1C820C47-9166-4534-9C64-7F91B21D5F93}.Release|Any CPU.Build.0 = Release|Any CPU
1521
EndGlobalSection
1622
EndGlobal

SeWzc.X11Sharp/SeWzc.X11Sharp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
1515
</ItemGroup>
1616

17+
<ItemGroup>
18+
<ProjectReference Include="..\SeWzc.X11Sharp.SourceGenerator\SeWzc.X11Sharp.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
19+
</ItemGroup>
20+
1721
</Project>

SeWzc.X11Sharp/Xids/X11Atom.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SeWzc.X11Sharp;
55
/// <summary>
66
/// X11 原子。
77
/// </summary>
8-
public readonly record struct X11Atom : IXid
8+
public readonly partial record struct X11Atom : IXid
99
{
1010
/// <summary>
1111
/// 通过 Id 构造 X11Atom。
@@ -63,28 +63,4 @@ public X11DisplayAtom WithDisplay(X11Display display)
6363
{
6464
return new X11DisplayAtom(display, this);
6565
}
66-
67-
#region 运算符重载
68-
69-
// 强制转换不需要文档
70-
#pragma warning disable CS1591
71-
72-
public static implicit operator ULong(X11Atom value)
73-
{
74-
return value.Id;
75-
}
76-
77-
public static implicit operator nuint(X11Atom value)
78-
{
79-
return value.ToUPtrInt();
80-
}
81-
82-
public static implicit operator nint(X11Atom value)
83-
{
84-
return (nint)value.Id;
85-
}
86-
87-
#pragma warning restore CS1591
88-
89-
#endregion
9066
}

SeWzc.X11Sharp/Xids/X11Colormap.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SeWzc.X11Sharp;
55
/// <summary>
66
/// X11 颜色映射表。
77
/// </summary>
8-
public readonly record struct X11Colormap : IXid
8+
public readonly partial record struct X11Colormap : IXid
99
{
1010
/// <summary>
1111
/// 通过 Id 构造 X11Colormap。
@@ -53,28 +53,4 @@ public nuint ToUPtrInt()
5353
{
5454
return Id;
5555
}
56-
57-
#region 运算符重载
58-
59-
// 强制转换不需要文档
60-
#pragma warning disable CS1591
61-
62-
public static implicit operator ULong(X11Colormap value)
63-
{
64-
return value.Id;
65-
}
66-
67-
public static implicit operator nuint(X11Colormap value)
68-
{
69-
return value.ToUPtrInt();
70-
}
71-
72-
public static implicit operator nint(X11Colormap value)
73-
{
74-
return (nint)value.Id;
75-
}
76-
77-
#pragma warning restore CS1591
78-
79-
#endregion
8056
}

SeWzc.X11Sharp/Xids/X11Cursor.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SeWzc.X11Sharp;
55
/// <summary>
66
/// X11 光标。
77
/// </summary>
8-
public readonly record struct X11Cursor : IXid
8+
public readonly partial record struct X11Cursor : IXid
99
{
1010
/// <summary>
1111
/// 通过 Id 构造 X11Cursor。
@@ -63,28 +63,4 @@ public X11DisplayCursor WithDisplay(X11Display display)
6363
{
6464
return new X11DisplayCursor(display, this);
6565
}
66-
67-
#region 运算符重载
68-
69-
// 强制转换不需要文档
70-
#pragma warning disable CS1591
71-
72-
public static implicit operator ULong(X11Cursor value)
73-
{
74-
return value.Id;
75-
}
76-
77-
public static implicit operator nuint(X11Cursor value)
78-
{
79-
return value.ToUPtrInt();
80-
}
81-
82-
public static implicit operator nint(X11Cursor value)
83-
{
84-
return (nint)value.Id;
85-
}
86-
87-
#pragma warning restore CS1591
88-
89-
#endregion
9066
}

SeWzc.X11Sharp/Xids/X11Drawable.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SeWzc.X11Sharp;
55
/// <summary>
66
/// X11 可绘制对象。
77
/// </summary>
8-
public readonly record struct X11Drawable : IXid, IDrawable
8+
public readonly partial record struct X11Drawable : IXid, IDrawable
99
{
1010
/// <summary>
1111
/// 通过 Id 构造 X11Drawable。
@@ -59,28 +59,4 @@ public nuint ToUPtrInt()
5959
{
6060
return Id;
6161
}
62-
63-
#region 运算符重载
64-
65-
// 强制转换不需要文档
66-
#pragma warning disable CS1591
67-
68-
public static implicit operator ULong(X11Drawable value)
69-
{
70-
return value.Id;
71-
}
72-
73-
public static implicit operator nuint(X11Drawable value)
74-
{
75-
return value.ToUPtrInt();
76-
}
77-
78-
public static implicit operator nint(X11Drawable value)
79-
{
80-
return (nint)value.Id;
81-
}
82-
83-
#pragma warning restore CS1591
84-
85-
#endregion
8662
}

SeWzc.X11Sharp/Xids/X11Font.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SeWzc.X11Sharp;
55
/// <summary>
66
/// X11 字体。
77
/// </summary>
8-
public readonly record struct X11Font : IXid
8+
public readonly partial record struct X11Font : IXid
99
{
1010
/// <summary>
1111
/// 通过 Id 构造 X11Font。
@@ -53,28 +53,4 @@ public nuint ToUPtrInt()
5353
{
5454
return Id;
5555
}
56-
57-
#region 运算符重载
58-
59-
// 强制转换不需要文档
60-
#pragma warning disable CS1591
61-
62-
public static implicit operator ULong(X11Font value)
63-
{
64-
return value.Id;
65-
}
66-
67-
public static implicit operator nuint(X11Font value)
68-
{
69-
return value.ToUPtrInt();
70-
}
71-
72-
public static implicit operator nint(X11Font value)
73-
{
74-
return (nint)value.Id;
75-
}
76-
77-
#pragma warning restore CS1591
78-
79-
#endregion
8056
}

SeWzc.X11Sharp/Xids/X11GContext.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace SeWzc.X11Sharp;
88
/// <remarks>
99
/// 与 <see cref="X11GC"/> 的区别是,该类是资源 ID 的包装,<see cref="X11GC"/> 用于提供函数调用操作。
1010
/// </remarks>
11-
public readonly record struct X11GContext : IXid
11+
public readonly partial record struct X11GContext : IXid
1212
{
1313
/// <summary>
1414
/// 通过 Id 构造 X11GContext。
@@ -56,28 +56,4 @@ public nuint ToUPtrInt()
5656
{
5757
return Id;
5858
}
59-
60-
#region 运算符重载
61-
62-
// 强制转换不需要文档
63-
#pragma warning disable CS1591
64-
65-
public static implicit operator ULong(X11GContext value)
66-
{
67-
return value.Id;
68-
}
69-
70-
public static implicit operator nuint(X11GContext value)
71-
{
72-
return value.ToUPtrInt();
73-
}
74-
75-
public static implicit operator nint(X11GContext value)
76-
{
77-
return (nint)value.Id;
78-
}
79-
80-
#pragma warning restore CS1591
81-
82-
#endregion
8359
}

0 commit comments

Comments
 (0)