@@ -41,30 +41,39 @@ internal static class TemplateUtils
4141 PatternType
4242 } ;
4343
44- internal static bool GetHasTemplates ( ) => GetHasTemplates ( PluginBase . MainForm . CurrentDocument . SciControl . ConfigurationLanguage . ToLower ( ) ) ;
44+ internal static bool GetHasTemplates ( )
45+ => PluginBase . MainForm . CurrentDocument ? . SciControl is { } sci
46+ && GetHasTemplates ( sci . ConfigurationLanguage . ToLower ( ) ) ;
4547
4648 internal static bool GetHasTemplates ( string language )
47- {
48- return GetHasTemplates ( PathHelper . SnippetDir , language ) || Settings . CustomSnippetDirectories . Any ( it => GetHasTemplates ( it . Path , language ) ) ;
49- }
49+ => GetHasTemplates ( PathHelper . SnippetDir , language )
50+ || Settings . CustomSnippetDirectories . Any ( it => GetHasTemplates ( it . Path , language ) ) ;
5051
5152 static bool GetHasTemplates ( string snippetPath , string language )
5253 {
5354 snippetPath = GetTemplatesDir ( snippetPath , language ) ;
54- return Directory . Exists ( snippetPath ) && Directory . GetFiles ( snippetPath , "*.fds" ) . Length > 0 ;
55+ return Directory . Exists ( snippetPath ) && Directory . EnumerateFiles ( snippetPath , "*.fds" ) . Any ( ) ;
5556 }
5657
57- static string GetTemplatesDir ( string snippetPath ) => GetTemplatesDir ( snippetPath , PluginBase . MainForm . CurrentDocument . SciControl . ConfigurationLanguage . ToLower ( ) ) ;
58+ static string GetTemplatesDir ( string snippetPath )
59+ {
60+ return PluginBase . MainForm . CurrentDocument ? . SciControl is { } sci
61+ ? GetTemplatesDir ( snippetPath , sci . ConfigurationLanguage . ToLower ( ) )
62+ : null ;
63+ }
5864
59- static string GetTemplatesDir ( string snippetPath , string language ) => Path . Combine ( Path . Combine ( snippetPath , language ) , PostfixGenerators ) ;
65+ static string GetTemplatesDir ( string snippetPath , string language ) => Path . Combine ( snippetPath , language , PostfixGenerators ) ;
6066
6167 internal static Dictionary < string , string > GetTemplates ( string type )
6268 {
63- var pattern = Templates . Contains ( type ) ? string . Format ( PatternBlock , type ) : string . Format ( PatternTBlock , type ) ;
6469 var result = new Dictionary < string , string > ( ) ;
6570 var paths = Settings . CustomSnippetDirectories . Select ( it => GetTemplatesDir ( it . Path ) ) . ToList ( ) ;
6671 paths . Add ( GetTemplatesDir ( PathHelper . SnippetDir ) ) ;
6772 paths . RemoveAll ( s => ! Directory . Exists ( s ) ) ;
73+ if ( paths . Count == 0 ) return result ;
74+ var pattern = Templates . Contains ( type )
75+ ? string . Format ( PatternBlock , type )
76+ : string . Format ( PatternTBlock , type ) ;
6877 foreach ( var path in paths )
6978 {
7079 foreach ( var file in Directory . GetFiles ( path , "*.fds" ) )
@@ -83,11 +92,12 @@ internal static string GetTemplate(string snippet, string[] types)
8392 var result = string . Empty ;
8493 foreach ( var type in types )
8594 {
86- var r = GetTemplate ( snippet , type ) ;
87- if ( ! string . IsNullOrEmpty ( r ) && r != result ) result = r ;
95+ var template = GetTemplate ( snippet , type ) ;
96+ if ( ! string . IsNullOrEmpty ( template ) && template != result ) result = template ;
8897 }
8998 return result ;
9099 }
100+
91101 internal static string GetTemplate ( string snippet , string type )
92102 {
93103 var marker = $ "#pcc:{ type } ";
@@ -105,9 +115,8 @@ internal static string GetTemplate(string snippet, string type)
105115
106116 static string GetSnippet ( string file )
107117 {
108- string content ;
109118 using var reader = new StreamReader ( File . OpenRead ( file ) ) ;
110- content = reader . ReadToEnd ( ) ;
119+ var content = reader . ReadToEnd ( ) ;
111120 reader . Close ( ) ;
112121 return content ;
113122 }
0 commit comments