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