code_style: simplify Models.TextMateHelper

This commit is contained in:
leo 2024-09-21 22:02:50 +08:00
parent 4a3620db5e
commit 80017d8bd0
No known key found for this signature in database

View file

@ -27,7 +27,7 @@ namespace SourceGit.Models
new ExtraGrammar("source.hxml", ".hxml", "hxml.json"), new ExtraGrammar("source.hxml", ".hxml", "hxml.json"),
]; ];
public static string GetExtension(string file) public static string GetScope(string file, RegistryOptions reg)
{ {
var extension = Path.GetExtension(file); var extension = Path.GetExtension(file);
if (extension == ".h") if (extension == ".h")
@ -38,22 +38,17 @@ namespace SourceGit.Models
extension = ".sh"; extension = ".sh";
else if (extension == ".kt" || extension == ".kts") else if (extension == ".kt" || extension == ".kts")
extension = ".kotlin"; extension = ".kotlin";
return extension;
}
public static string GetScopeByExtension(string extension)
{
foreach (var grammar in s_extraGrammas) foreach (var grammar in s_extraGrammas)
{ {
if (grammar.Extension.Equals(extension, StringComparison.OrdinalIgnoreCase)) if (grammar.Extension.Equals(extension, StringComparison.OrdinalIgnoreCase))
return grammar.Scope; return grammar.Scope;
} }
return null; return reg.GetScopeByExtension(extension);
} }
public static IRawGrammar Load(string scopeName) public static IRawGrammar GetGrammar(string scopeName, RegistryOptions reg)
{ {
foreach (var grammar in s_extraGrammas) foreach (var grammar in s_extraGrammas)
{ {
@ -73,7 +68,7 @@ namespace SourceGit.Models
} }
} }
return null; return reg.GetGrammar(scopeName);
} }
private record ExtraGrammar(string Scope, string Extension, string File) private record ExtraGrammar(string Scope, string Extension, string File)
@ -86,37 +81,13 @@ namespace SourceGit.Models
public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions
{ {
public IRawTheme GetTheme(string scopeName) public IRawTheme GetTheme(string scopeName) => _backend.GetTheme(scopeName);
{ public IRawTheme GetDefaultTheme() => _backend.GetDefaultTheme();
return _backend.GetTheme(scopeName); public IRawTheme LoadTheme(ThemeName name) => _backend.LoadTheme(name);
} public ICollection<string> GetInjections(string scopeName) => _backend.GetInjections(scopeName);
public IRawGrammar GetGrammar(string scopeName) => GrammarUtility.GetGrammar(scopeName, _backend);
public IRawGrammar GetGrammar(string scopeName) public string GetScope(string filename) => GrammarUtility.GetScope(filename, _backend);
{
return GrammarUtility.Load(scopeName) ?? _backend.GetGrammar(scopeName);
}
public ICollection<string> GetInjections(string scopeName)
{
return _backend.GetInjections(scopeName);
}
public IRawTheme GetDefaultTheme()
{
return _backend.GetDefaultTheme();
}
public IRawTheme LoadTheme(ThemeName name)
{
return _backend.LoadTheme(name);
}
public string GetScopeByFileName(string filename)
{
var ext = GrammarUtility.GetExtension(filename);
return GrammarUtility.GetScopeByExtension(ext) ?? _backend.GetScopeByExtension(ext);
}
private readonly RegistryOptions _backend = new(defaultTheme); private readonly RegistryOptions _backend = new(defaultTheme);
} }
@ -124,18 +95,14 @@ namespace SourceGit.Models
{ {
public static TextMate.Installation CreateForEditor(TextEditor editor) public static TextMate.Installation CreateForEditor(TextEditor editor)
{ {
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark) return editor.InstallTextMate(Application.Current?.ActualThemeVariant == ThemeVariant.Dark ?
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.DarkPlus)); new RegistryOptionsWrapper(ThemeName.DarkPlus) :
new RegistryOptionsWrapper(ThemeName.LightPlus));
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.LightPlus));
} }
public static void SetThemeByApp(TextMate.Installation installation) public static void SetThemeByApp(TextMate.Installation installation)
{ {
if (installation == null) if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
return;
if (installation.RegistryOptions is RegistryOptionsWrapper reg)
{ {
var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark; var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
installation.SetTheme(reg.LoadTheme(isDark ? ThemeName.DarkPlus : ThemeName.LightPlus)); installation.SetTheme(reg.LoadTheme(isDark ? ThemeName.DarkPlus : ThemeName.LightPlus));
@ -146,7 +113,7 @@ namespace SourceGit.Models
{ {
if (installation is { RegistryOptions: RegistryOptionsWrapper reg }) if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
{ {
installation.SetGrammar(reg.GetScopeByFileName(filePath)); installation.SetGrammar(reg.GetScope(filePath));
GC.Collect(); GC.Collect();
} }
} }