@@ -27,7 +27,7 @@ public static class GrammarUtility
27
27
new ExtraGrammar ( "source.hxml" , ".hxml" , "hxml.json" ) ,
28
28
] ;
29
29
30
- public static string GetExtension ( string file )
30
+ public static string GetScope ( string file , RegistryOptions reg )
31
31
{
32
32
var extension = Path . GetExtension ( file ) ;
33
33
if ( extension == ".h" )
@@ -38,22 +38,17 @@ public static string GetExtension(string file)
38
38
extension = ".sh" ;
39
39
else if ( extension == ".kt" || extension == ".kts" )
40
40
extension = ".kotlin" ;
41
-
42
- return extension ;
43
- }
44
-
45
- public static string GetScopeByExtension ( string extension )
46
- {
41
+
47
42
foreach ( var grammar in s_extraGrammas )
48
43
{
49
44
if ( grammar . Extension . Equals ( extension , StringComparison . OrdinalIgnoreCase ) )
50
45
return grammar . Scope ;
51
46
}
52
47
53
- return null ;
48
+ return reg . GetScopeByExtension ( extension ) ;
54
49
}
55
50
56
- public static IRawGrammar Load ( string scopeName )
51
+ public static IRawGrammar GetGrammar ( string scopeName , RegistryOptions reg )
57
52
{
58
53
foreach ( var grammar in s_extraGrammas )
59
54
{
@@ -73,7 +68,7 @@ public static IRawGrammar Load(string scopeName)
73
68
}
74
69
}
75
70
76
- return null ;
71
+ return reg . GetGrammar ( scopeName ) ;
77
72
}
78
73
79
74
private record ExtraGrammar ( string Scope , string Extension , string File )
@@ -86,56 +81,28 @@ private record ExtraGrammar(string Scope, string Extension, string File)
86
81
87
82
public class RegistryOptionsWrapper ( ThemeName defaultTheme ) : IRegistryOptions
88
83
{
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
+
120
91
private readonly RegistryOptions _backend = new ( defaultTheme ) ;
121
92
}
122
93
123
94
public static class TextMateHelper
124
95
{
125
96
public static TextMate . Installation CreateForEditor ( TextEditor editor )
126
97
{
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 ) ) ;
131
101
}
132
102
133
103
public static void SetThemeByApp ( TextMate . Installation installation )
134
104
{
135
- if ( installation == null )
136
- return ;
137
-
138
- if ( installation . RegistryOptions is RegistryOptionsWrapper reg )
105
+ if ( installation is { RegistryOptions : RegistryOptionsWrapper reg } )
139
106
{
140
107
var isDark = Application . Current ? . ActualThemeVariant == ThemeVariant . Dark ;
141
108
installation . SetTheme ( reg . LoadTheme ( isDark ? ThemeName . DarkPlus : ThemeName . LightPlus ) ) ;
@@ -146,7 +113,7 @@ public static void SetGrammarByFileName(TextMate.Installation installation, stri
146
113
{
147
114
if ( installation is { RegistryOptions : RegistryOptionsWrapper reg } )
148
115
{
149
- installation . SetGrammar ( reg . GetScopeByFileName ( filePath ) ) ;
116
+ installation . SetGrammar ( reg . GetScope ( filePath ) ) ;
150
117
GC . Collect ( ) ;
151
118
}
152
119
}
0 commit comments