Refactor source generator a bit

This commit is contained in:
Dan Balasescu 2024-12-09 10:51:26 +09:00
parent 53a87f4a74
commit b3dec6b80c
No known key found for this signature in database
1 changed files with 8 additions and 7 deletions

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -16,7 +15,7 @@ namespace SDL.SourceGeneration
{ {
public readonly Dictionary<string, List<GeneratedMethod>> Methods = new Dictionary<string, List<GeneratedMethod>>(); public readonly Dictionary<string, List<GeneratedMethod>> Methods = new Dictionary<string, List<GeneratedMethod>>();
private static readonly string[] sdlPrefixes = ["SDL", "TTF", "IMG"]; private static readonly string[] sdlPrefixes = ["SDL_", "TTF_", "IMG_"];
/// <summary> /// <summary>
/// Checks whether the method is from any SDL library. /// Checks whether the method is from any SDL library.
@ -24,11 +23,13 @@ namespace SDL.SourceGeneration
/// </summary> /// </summary>
private static bool IsMethodFromSDL(MethodDeclarationSyntax methodNode) private static bool IsMethodFromSDL(MethodDeclarationSyntax methodNode)
{ {
string? libraryPrefix = methodNode.Identifier.ValueText.Split('_').FirstOrDefault(); foreach (string prefix in sdlPrefixes)
if (libraryPrefix == null) {
return false; if (methodNode.Identifier.ValueText.StartsWith(prefix, StringComparison.Ordinal))
return true;
}
return sdlPrefixes.Contains(libraryPrefix); return false;
} }
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
@ -36,7 +37,7 @@ namespace SDL.SourceGeneration
if (syntaxNode is MethodDeclarationSyntax method) if (syntaxNode is MethodDeclarationSyntax method)
{ {
string name = method.Identifier.ValueText; string name = method.Identifier.ValueText;
bool isUnsafe = name.StartsWith($"{Helper.UnsafePrefix}", StringComparison.Ordinal); bool isUnsafe = name.StartsWith(Helper.UnsafePrefix, StringComparison.Ordinal);
if (!IsMethodFromSDL(method) && !isUnsafe) if (!IsMethodFromSDL(method) && !isUnsafe)
return; return;