1717
1818namespace SourceGit . Models
1919{
20- public class RegistryOptionsWrapper : IRegistryOptions
20+ public static class GrammarUtility
2121 {
22- public RegistryOptionsWrapper ( ThemeName defaultTheme )
22+ private static readonly ExtraGrammar [ ] s_extraGrammas =
23+ [
24+ new ExtraGrammar ( "source.toml" , ".toml" , "toml.json" ) ,
25+ new ExtraGrammar ( "source.kotlin" , ".kotlin" , "kotlin.json" ) ,
26+ new ExtraGrammar ( "source.hx" , ".hx" , "haxe.json" ) ,
27+ new ExtraGrammar ( "source.hxml" , ".hxml" , "hxml.json" ) ,
28+ ] ;
29+
30+ public static string GetExtension ( string file )
2331 {
24- _backend = new RegistryOptions ( defaultTheme ) ;
25- _extraGrammars = new List < IRawGrammar > ( ) ;
32+ var extension = Path . GetExtension ( file ) ;
33+ if ( extension == ".h" )
34+ extension = ".cpp" ;
35+ else if ( extension == ".resx" || extension == ".plist" || extension == ".manifest" )
36+ extension = ".xml" ;
37+ else if ( extension == ".command" )
38+ extension = ".sh" ;
39+ else if ( extension == ".kt" || extension == ".kts" )
40+ extension = ".kotlin" ;
2641
27- string [ ] extraGrammarFiles = [ "toml.json" , "kotlin.json" , "haxe.json" , "hxml.json" ] ;
28- foreach ( var file in extraGrammarFiles )
42+ return extension ;
43+ }
44+
45+ public static string GetScopeByExtension ( string extension )
46+ {
47+ foreach ( var grammar in s_extraGrammas )
2948 {
30- var asset = AssetLoader . Open ( new Uri ( $ "avares://SourceGit/Resources/Grammars/{ file } ",
31- UriKind . RelativeOrAbsolute ) ) ;
49+ if ( grammar . Extension . Equals ( extension , StringComparison . OrdinalIgnoreCase ) )
50+ return grammar . Scope ;
51+ }
3252
33- try
34- {
35- var grammar = GrammarReader . ReadGrammarSync ( new StreamReader ( asset ) ) ;
36- _extraGrammars . Add ( grammar ) ;
37- }
38- catch
53+ return null ;
54+ }
55+
56+ public static IRawGrammar Load ( string scopeName )
57+ {
58+ foreach ( var grammar in s_extraGrammas )
59+ {
60+ if ( grammar . Scope . Equals ( scopeName , StringComparison . OrdinalIgnoreCase ) )
3961 {
40- // ignore
62+ var asset = AssetLoader . Open ( new Uri ( $ "avares://SourceGit/Resources/Grammars/{ grammar . File } ",
63+ UriKind . RelativeOrAbsolute ) ) ;
64+
65+ try
66+ {
67+ return GrammarReader . ReadGrammarSync ( new StreamReader ( asset ) ) ;
68+ }
69+ catch
70+ {
71+ break ;
72+ }
4173 }
4274 }
75+
76+ return null ;
4377 }
4478
79+ private record ExtraGrammar ( string Scope , string Extension , string File )
80+ {
81+ public readonly string Scope = Scope ;
82+ public readonly string Extension = Extension ;
83+ public readonly string File = File ;
84+ }
85+ }
86+
87+ public class RegistryOptionsWrapper ( ThemeName defaultTheme ) : IRegistryOptions
88+ {
4589 public IRawTheme GetTheme ( string scopeName )
4690 {
4791 return _backend . GetTheme ( scopeName ) ;
4892 }
4993
5094 public IRawGrammar GetGrammar ( string scopeName )
5195 {
52- var grammar = _extraGrammars . Find ( x => x . GetScopeName ( ) . Equals ( scopeName , StringComparison . Ordinal ) ) ;
53- return grammar ?? _backend . GetGrammar ( scopeName ) ;
96+ return GrammarUtility . Load ( scopeName ) ?? _backend . GetGrammar ( scopeName ) ;
5497 }
5598
5699 public ICollection < string > GetInjections ( string scopeName )
@@ -70,25 +113,11 @@ public IRawTheme LoadTheme(ThemeName name)
70113
71114 public string GetScopeByFileName ( string filename )
72115 {
73- var extension = Path . GetExtension ( filename ) ;
74- if ( extension == ".h" )
75- extension = ".cpp" ;
76- else if ( extension == ".resx" || extension == ".plist" || extension == ".manifest" )
77- extension = ".xml" ;
78- else if ( extension == ".command" )
79- extension = ".sh" ;
80- else if ( extension == ".kt" || extension == ".kts" )
81- extension = ".kotlin" ;
82-
83- var grammar = _extraGrammars . Find ( x => x . GetScopeName ( ) . EndsWith ( extension , StringComparison . OrdinalIgnoreCase ) ) ;
84- if ( grammar != null )
85- return grammar . GetScopeName ( ) ;
86-
87- return _backend . GetScopeByExtension ( extension ) ;
116+ var ext = GrammarUtility . GetExtension ( filename ) ;
117+ return GrammarUtility . GetScopeByExtension ( ext ) ?? _backend . GetScopeByExtension ( ext ) ;
88118 }
89119
90- private readonly RegistryOptions _backend ;
91- private readonly List < IRawGrammar > _extraGrammars ;
120+ private readonly RegistryOptions _backend = new ( defaultTheme ) ;
92121 }
93122
94123 public static class TextMateHelper
0 commit comments