refactor: rewrite the way using extension to get custom grammars

This commit is contained in:
leo 2024-08-18 09:33:40 +08:00
parent 39fba17648
commit fe9f4f5743
No known key found for this signature in database

View file

@ -22,7 +22,7 @@ namespace SourceGit.Models
public RegistryOptionsWrapper(ThemeName defaultTheme) public RegistryOptionsWrapper(ThemeName defaultTheme)
{ {
_backend = new RegistryOptions(defaultTheme); _backend = new RegistryOptions(defaultTheme);
_extraGrammars = new Dictionary<string, IRawGrammar>(); _extraGrammars = new List<IRawGrammar>();
string[] extraGrammarFiles = ["toml.json"]; string[] extraGrammarFiles = ["toml.json"];
foreach (var file in extraGrammarFiles) foreach (var file in extraGrammarFiles)
@ -33,7 +33,7 @@ namespace SourceGit.Models
try try
{ {
var grammar = GrammarReader.ReadGrammarSync(new StreamReader(asset)); var grammar = GrammarReader.ReadGrammarSync(new StreamReader(asset));
_extraGrammars.Add(grammar.GetScopeName(), grammar); _extraGrammars.Add(grammar);
} }
catch catch
{ {
@ -49,10 +49,8 @@ namespace SourceGit.Models
public IRawGrammar GetGrammar(string scopeName) public IRawGrammar GetGrammar(string scopeName)
{ {
if (_extraGrammars.TryGetValue(scopeName, out var grammar)) var grammar = _extraGrammars.Find(x => x.GetScopeName().Equals(scopeName, StringComparison.Ordinal));
return grammar; return grammar ?? _backend.GetGrammar(scopeName);
return _backend.GetGrammar(scopeName);
} }
public ICollection<string> GetInjections(string scopeName) public ICollection<string> GetInjections(string scopeName)
@ -73,13 +71,13 @@ namespace SourceGit.Models
public string GetScopeByFileName(string filename) public string GetScopeByFileName(string filename)
{ {
var extension = Path.GetExtension(filename); var extension = Path.GetExtension(filename);
var scope = $"source{extension}"; var grammar = _extraGrammars.Find(x => x.GetScopeName().EndsWith(extension, StringComparison.OrdinalIgnoreCase));
if (_extraGrammars.ContainsKey(scope)) if (grammar != null)
return scope; return grammar.GetScopeName();
if (extension == ".h") if (extension == ".h")
extension = ".cpp"; extension = ".cpp";
else if (extension == ".resx" || extension == ".plist") else if (extension == ".resx" || extension == ".plist" || extension == ".manifest")
extension = ".xml"; extension = ".xml";
else if (extension == ".command") else if (extension == ".command")
extension = ".sh"; extension = ".sh";
@ -87,8 +85,8 @@ namespace SourceGit.Models
return _backend.GetScopeByExtension(extension); return _backend.GetScopeByExtension(extension);
} }
private RegistryOptions _backend = null; private readonly RegistryOptions _backend;
private Dictionary<string, IRawGrammar> _extraGrammars = null; private readonly List<IRawGrammar> _extraGrammars;
} }
public static class TextMateHelper public static class TextMateHelper
@ -108,10 +106,8 @@ namespace SourceGit.Models
if (installation.RegistryOptions is RegistryOptionsWrapper reg) if (installation.RegistryOptions is RegistryOptionsWrapper reg)
{ {
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark) var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
installation.SetTheme(reg.LoadTheme(ThemeName.DarkPlus)); installation.SetTheme(reg.LoadTheme(isDark ? ThemeName.DarkPlus : ThemeName.LightPlus));
else
installation.SetTheme(reg.LoadTheme(ThemeName.LightPlus));
} }
} }