mirror of
https://github.com/RPCS3/discord-bot.git
synced 2026-01-31 01:25:22 +01:00
update syntax in code generators
This commit is contained in:
@@ -12,7 +12,7 @@ using Microsoft.CodeAnalysis.Text;
|
||||
namespace SourceGenerators;
|
||||
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
public class ConfusablesSourceGenerator : IIncrementalGenerator
|
||||
public class ConfusablesSourceGenerator: IIncrementalGenerator
|
||||
{
|
||||
private static readonly char[] CommentSplitter = ['#'];
|
||||
private static readonly char[] FieldSplitter = [';'];
|
||||
@@ -102,25 +102,29 @@ public class ConfusablesSourceGenerator : IIncrementalGenerator
|
||||
if (!Version.TryParse(version, out _))
|
||||
version = "";
|
||||
|
||||
var result = new StringBuilder()
|
||||
.AppendLine("using System;")
|
||||
.AppendLine("using System.Collections.Generic;")
|
||||
.AppendLine()
|
||||
.AppendLine($"namespace {ns}")
|
||||
.AppendLine("{")
|
||||
.AppendLine($" internal static class {cn}")
|
||||
.AppendLine(" {")
|
||||
.AppendLine($" public const string Version = \"{version}\";")
|
||||
.AppendLine()
|
||||
.AppendLine($" public const string Date = \"{date}\";")
|
||||
.AppendLine()
|
||||
.AppendLine(" public static readonly Dictionary<uint, uint[]> Mapping = new()")
|
||||
.AppendLine(" {");
|
||||
var result = new StringBuilder().AppendLine($$"""
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace {{ns}};
|
||||
|
||||
internal static class {{cn}}
|
||||
{
|
||||
public const string Version = "{{version}}";
|
||||
public const string Date = "{{date}}";
|
||||
public static readonly Dictionary<uint, uint[]> Mapping = new()
|
||||
{
|
||||
"""
|
||||
);
|
||||
foreach (var kvp in mapping.OrderBy(i => i.Key))
|
||||
result.AppendLine($@" [0x{kvp.Key:X5}u] = new[] {{ {string.Join(", ", kvp.Value!.OrderBy(i => i).Select(n => $"0x{n:X5}u"))} }},");
|
||||
result.AppendLine(" };")
|
||||
.AppendLine(" }")
|
||||
.AppendLine("}");
|
||||
result.AppendLine($"""
|
||||
[0x{kvp.Key:X5}u] = [{string.Join(", ", kvp.Value!.OrderBy(i => i).Select(n => $"0x{n:X5}u"))}],
|
||||
""");
|
||||
result.AppendLine("""
|
||||
};
|
||||
}
|
||||
"""
|
||||
);
|
||||
|
||||
context.AddSource($"{cn}.Generated.cs", SourceText.From(result.ToString(), Encoding.UTF8));
|
||||
|
||||
|
||||
@@ -10,9 +10,8 @@ using Microsoft.CodeAnalysis.Text;
|
||||
namespace SourceGenerators;
|
||||
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
public class Win32ErrorsSourceGenerator : IIncrementalGenerator
|
||||
public class Win32ErrorsSourceGenerator: IIncrementalGenerator
|
||||
{
|
||||
private const string Indent = " ";
|
||||
private static readonly char[] Separator = ['\t'];
|
||||
|
||||
private static readonly DiagnosticDescriptor Win32ErrorFormatError = new(
|
||||
@@ -44,21 +43,21 @@ public class Win32ErrorsSourceGenerator : IIncrementalGenerator
|
||||
|
||||
if (!args.generatorContext.configOptions.GlobalOptions.TryGetValue("build_property.RootNamespace", out var ns))
|
||||
ns = args.generatorContext.compilation.AssemblyName;
|
||||
var cn = "Win32ErrorCodes";
|
||||
var result = new StringBuilder()
|
||||
.AppendLine("using System.Collections.Generic;")
|
||||
.AppendLine()
|
||||
.AppendLine($"namespace {ns}")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{Indent}public static class {cn}")
|
||||
.AppendLine($"{Indent}{{")
|
||||
.AppendLine($"{Indent}{Indent}public static readonly Dictionary<int, (string name, string description)> Map = new()")
|
||||
.AppendLine($"{Indent}{Indent}{{");
|
||||
const string cn = "Win32ErrorCodes";
|
||||
var result = new StringBuilder().AppendLine($$"""
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace {{ns}};
|
||||
|
||||
public static class {{cn}}
|
||||
{
|
||||
public static readonly Dictionary<int, (string name, string description)> Map = new()
|
||||
{
|
||||
"""
|
||||
);
|
||||
|
||||
var previousPos = 0;
|
||||
var line = 0;
|
||||
var codeLine = 0;
|
||||
var descLine = 0;
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8, false);
|
||||
while (reader.ReadLine() is string errorCodeLine)
|
||||
{
|
||||
@@ -66,14 +65,14 @@ public class Win32ErrorsSourceGenerator : IIncrementalGenerator
|
||||
if (string.IsNullOrWhiteSpace(errorCodeLine))
|
||||
continue;
|
||||
|
||||
codeLine = line - 1;
|
||||
var codeLine = line - 1;
|
||||
string? errorNameAndDescriptionLine;
|
||||
do
|
||||
{
|
||||
errorNameAndDescriptionLine = reader.ReadLine();
|
||||
line++;
|
||||
} while (string.IsNullOrWhiteSpace(errorNameAndDescriptionLine));
|
||||
descLine = line - 1;
|
||||
var descLine = line - 1;
|
||||
|
||||
var nameDescParts = errorNameAndDescriptionLine.Split(Separator, 2);
|
||||
if (nameDescParts.Length != 2 || !Regex.IsMatch(errorCodeLine, @"0x[0-9a-f]+"))
|
||||
@@ -99,13 +98,16 @@ public class Win32ErrorsSourceGenerator : IIncrementalGenerator
|
||||
|
||||
var name = nameDescParts[0];
|
||||
var desc = nameDescParts[1].Replace(@"\", @"\\").Replace("\"", "\\\"");
|
||||
result.AppendLine($"{Indent}{Indent}{Indent}[{errorCodeLine.Trim()}] = (\"{name.Trim()}\", \"{desc.Trim()}\"),");
|
||||
result.AppendLine($"""
|
||||
[{errorCodeLine.Trim()}] = ("{name.Trim()}", "{desc.Trim()}"),
|
||||
"""
|
||||
);
|
||||
}
|
||||
|
||||
result.AppendLine($"{Indent}{Indent}}};")
|
||||
.AppendLine($"{Indent}}}")
|
||||
.AppendLine("}");
|
||||
|
||||
result.AppendLine("""
|
||||
};
|
||||
}
|
||||
"""
|
||||
);
|
||||
context.AddSource($"{cn}.Generated.cs", SourceText.From(result.ToString(), Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user