Optimize highlight name fetching
Some checks failed
MSBuild / build (push) Has been cancelled
MSBuild / publish (push) Has been cancelled

This commit is contained in:
ema
2025-05-26 09:48:22 +08:00
parent 6deadf9d73
commit 645edf2439

View File

@@ -176,17 +176,17 @@ public class Plugin : IViewer
if (s == null)
continue;
Debug.WriteLine(resourceName);
try
{
Debug.WriteLine(resourceName);
var hlm = resourceName.Contains(".Syntax.Dark.") ? _hlmDark : _hlmLight;
var ext = Path.GetFileNameWithoutExtension(resourceName.ToResourceDummyName());
var name = EmbeddedResource.GetFileNameWithoutExtension(resourceName);
using var reader = new XmlTextReader(s);
var xshd = HighlightingLoader.LoadXshd(reader);
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
if (xshd.Extensions.Count > 0)
hlm.RegisterHighlighting(ext, [.. xshd.Extensions], highlightingDefinition);
hlm.RegisterHighlighting(name, [.. xshd.Extensions], highlightingDefinition);
}
catch (Exception e)
{
@@ -217,34 +217,36 @@ public class Plugin : IViewer
}
}
file static class ResourceNameHelper
file static class EmbeddedResource
{
/// <summary>
/// Converts a resource name (using '.' as separators) into a dummy file path
/// by replacing inner dots with backslashes, while preserving the file extension.
///
/// Example:
/// Input: "Resources.Images.icon.png"
/// Output: "Resources\Images\icon.png"
///
/// Input: "Assets.Sounds.music.background.mp3"
/// Output: "Assets\Sounds\music\background.mp3"
/// </summary>
/// <param name="resourceName">The embedded resource name (excluding the default namespace).</param>
/// <returns>A string representing the resource as a dummy file path.</returns>
public static string ToResourceDummyName(this string resourceName)
public static string GetFileNameWithoutExtension(string resourceName)
{
if (string.IsNullOrWhiteSpace(resourceName))
return resourceName;
// Requires the embedded resource file name
// must have a file extension and have only one '.' character
int start = int.MinValue, end = int.MinValue;
int lastDotIndex = resourceName.LastIndexOf('.');
if (lastDotIndex <= 0) // Either no dot or dot is at the beginning
return resourceName;
for (int i = resourceName.Length - 1; i >= 0; i--)
{
if (resourceName[i] == '.')
{
if (end == int.MinValue)
{
end = i;
continue;
}
// Replace dots before the extension with backslashes
string pathWithoutExtension = resourceName.Substring(0, lastDotIndex).Replace('.', '\\');
string extension = resourceName.Substring(lastDotIndex);
if (start == int.MinValue)
{
start = i + 1; // Exinclude '.' character
break;
}
}
}
return pathWithoutExtension + extension;
if ((start != int.MinValue) && (end != int.MinValue))
{
return resourceName.Substring(start, end - start);
}
return resourceName;
}
}