|
| 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 | +} |
0 commit comments