mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57:19 -08:00
code_style: simplify Models.TextMateHelper
This commit is contained in:
parent
4a3620db5e
commit
80017d8bd0
1 changed files with 17 additions and 50 deletions
|
@ -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,21 +39,16 @@ 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)
|
||||
{
|
||||
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,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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue