Skip to content

Commit 80017d8

Browse files
committed
code_style: simplify Models.TextMateHelper
1 parent 4a3620d commit 80017d8

File tree

1 file changed

+17
-50
lines changed

1 file changed

+17
-50
lines changed

src/Models/TextMateHelper.cs

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class GrammarUtility
2727
new ExtraGrammar("source.hxml", ".hxml", "hxml.json"),
2828
];
2929

30-
public static string GetExtension(string file)
30+
public static string GetScope(string file, RegistryOptions reg)
3131
{
3232
var extension = Path.GetExtension(file);
3333
if (extension == ".h")
@@ -38,22 +38,17 @@ public static string GetExtension(string file)
3838
extension = ".sh";
3939
else if (extension == ".kt" || extension == ".kts")
4040
extension = ".kotlin";
41-
42-
return extension;
43-
}
44-
45-
public static string GetScopeByExtension(string extension)
46-
{
41+
4742
foreach (var grammar in s_extraGrammas)
4843
{
4944
if (grammar.Extension.Equals(extension, StringComparison.OrdinalIgnoreCase))
5045
return grammar.Scope;
5146
}
5247

53-
return null;
48+
return reg.GetScopeByExtension(extension);
5449
}
5550

56-
public static IRawGrammar Load(string scopeName)
51+
public static IRawGrammar GetGrammar(string scopeName, RegistryOptions reg)
5752
{
5853
foreach (var grammar in s_extraGrammas)
5954
{
@@ -73,7 +68,7 @@ public static IRawGrammar Load(string scopeName)
7368
}
7469
}
7570

76-
return null;
71+
return reg.GetGrammar(scopeName);
7772
}
7873

7974
private record ExtraGrammar(string Scope, string Extension, string File)
@@ -86,56 +81,28 @@ private record ExtraGrammar(string Scope, string Extension, string File)
8681

8782
public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions
8883
{
89-
public IRawTheme GetTheme(string scopeName)
90-
{
91-
return _backend.GetTheme(scopeName);
92-
}
93-
94-
public IRawGrammar GetGrammar(string scopeName)
95-
{
96-
return GrammarUtility.Load(scopeName) ?? _backend.GetGrammar(scopeName);
97-
}
98-
99-
public ICollection<string> GetInjections(string scopeName)
100-
{
101-
return _backend.GetInjections(scopeName);
102-
}
103-
104-
public IRawTheme GetDefaultTheme()
105-
{
106-
return _backend.GetDefaultTheme();
107-
}
108-
109-
public IRawTheme LoadTheme(ThemeName name)
110-
{
111-
return _backend.LoadTheme(name);
112-
}
113-
114-
public string GetScopeByFileName(string filename)
115-
{
116-
var ext = GrammarUtility.GetExtension(filename);
117-
return GrammarUtility.GetScopeByExtension(ext) ?? _backend.GetScopeByExtension(ext);
118-
}
119-
84+
public IRawTheme GetTheme(string scopeName) => _backend.GetTheme(scopeName);
85+
public IRawTheme GetDefaultTheme() => _backend.GetDefaultTheme();
86+
public IRawTheme LoadTheme(ThemeName name) => _backend.LoadTheme(name);
87+
public ICollection<string> GetInjections(string scopeName) => _backend.GetInjections(scopeName);
88+
public IRawGrammar GetGrammar(string scopeName) => GrammarUtility.GetGrammar(scopeName, _backend);
89+
public string GetScope(string filename) => GrammarUtility.GetScope(filename, _backend);
90+
12091
private readonly RegistryOptions _backend = new(defaultTheme);
12192
}
12293

12394
public static class TextMateHelper
12495
{
12596
public static TextMate.Installation CreateForEditor(TextEditor editor)
12697
{
127-
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark)
128-
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.DarkPlus));
129-
130-
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.LightPlus));
98+
return editor.InstallTextMate(Application.Current?.ActualThemeVariant == ThemeVariant.Dark ?
99+
new RegistryOptionsWrapper(ThemeName.DarkPlus) :
100+
new RegistryOptionsWrapper(ThemeName.LightPlus));
131101
}
132102

133103
public static void SetThemeByApp(TextMate.Installation installation)
134104
{
135-
if (installation == null)
136-
return;
137-
138-
if (installation.RegistryOptions is RegistryOptionsWrapper reg)
105+
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
139106
{
140107
var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
141108
installation.SetTheme(reg.LoadTheme(isDark ? ThemeName.DarkPlus : ThemeName.LightPlus));
@@ -146,7 +113,7 @@ public static void SetGrammarByFileName(TextMate.Installation installation, stri
146113
{
147114
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
148115
{
149-
installation.SetGrammar(reg.GetScopeByFileName(filePath));
116+
installation.SetGrammar(reg.GetScope(filePath));
150117
GC.Collect();
151118
}
152119
}

0 commit comments

Comments
 (0)