Compare commits

..

2 commits

Author SHA1 Message Date
leo
0b6ecc0388
code_style: typo in s_extraGrammars
Some checks are pending
Continuous Integration / Build (push) Waiting to run
Continuous Integration / Prepare version string (push) Waiting to run
Continuous Integration / Package (push) Blocked by required conditions
2024-09-21 22:04:50 +08:00
leo
80017d8bd0
code_style: simplify Models.TextMateHelper 2024-09-21 22:02:50 +08:00

View file

@ -19,7 +19,7 @@ namespace SourceGit.Models
{
public static class GrammarUtility
{
private static readonly ExtraGrammar[] s_extraGrammas =
private static readonly ExtraGrammar[] s_extraGrammars =
[
new ExtraGrammar("source.toml", ".toml", "toml.json"),
new ExtraGrammar("source.kotlin", ".kotlin", "kotlin.json"),
@ -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")
@ -39,23 +39,18 @@ namespace SourceGit.Models
else if (extension == ".kt" || extension == ".kts")
extension = ".kotlin";
return extension;
}
public static string GetScopeByExtension(string extension)
{
foreach (var grammar in s_extraGrammas)
foreach (var grammar in s_extraGrammars)
{
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)
foreach (var grammar in s_extraGrammars)
{
if (grammar.Scope.Equals(scopeName, StringComparison.OrdinalIgnoreCase))
{
@ -73,7 +68,7 @@ namespace SourceGit.Models
}
}
return null;
return reg.GetGrammar(scopeName);
}
private record ExtraGrammar(string Scope, string Extension, string File)
@ -86,36 +81,12 @@ 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<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);
}
public IRawTheme GetTheme(string scopeName) => _backend.GetTheme(scopeName);
public IRawTheme GetDefaultTheme() => _backend.GetDefaultTheme();
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 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();
}
}