@@ -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,12 +115,9 @@ internal static string GetTemplate(string snippet, string type)
105115
106116 static string GetSnippet ( string file )
107117 {
108- string content ;
109- using ( var reader = new StreamReader ( File . OpenRead ( file ) ) )
110- {
111- content = reader . ReadToEnd ( ) ;
112- reader . Close ( ) ;
113- }
118+ using var reader = new StreamReader ( File . OpenRead ( file ) ) ;
119+ var content = reader . ReadToEnd ( ) ;
120+ reader . Close ( ) ;
114121 return content ;
115122 }
116123
@@ -127,27 +134,24 @@ internal static KeyValuePair<string, string> GetVarNameToQualifiedName(ASResult
127134 var sci = PluginBase . MainForm . CurrentDocument . SciControl ;
128135 var lineNum = sci . CurrentLine ;
129136 var word = Reflector . ASGenerator . GetStatementReturnType ( sci , sci . GetLine ( lineNum ) , sci . PositionFromLine ( lineNum ) ) ? . Word ;
130- var varname = string . Empty ;
131- if ( member ? . Name != null ) varname = Reflector . ASGenerator . GuessVarName ( member . Name , type ) ;
137+ var value = string . Empty ;
138+ if ( member ? . Name != null ) value = Reflector . ASGenerator . GuessVarName ( member . Name , type ) ;
132139 if ( ! string . IsNullOrEmpty ( word ) && char . IsDigit ( word [ 0 ] ) ) word = null ;
133140 if ( ! string . IsNullOrEmpty ( word ) && ( string . IsNullOrEmpty ( type ) || Regex . IsMatch ( type , "(<[^]]+>)" ) ) ) word = null ;
134141 if ( ! string . IsNullOrEmpty ( type ) && type == ASContext . Context . Features . voidKey ) type = null ;
135- if ( string . IsNullOrEmpty ( varname ) ) varname = Reflector . ASGenerator . GuessVarName ( word , type ) ;
136- if ( ! string . IsNullOrEmpty ( varname ) && varname == word ) varname = $ "{ varname } 1";
137- return new KeyValuePair < string , string > ( varname , type ) ;
142+ if ( string . IsNullOrEmpty ( value ) ) value = Reflector . ASGenerator . GuessVarName ( word , type ) ;
143+ if ( ! string . IsNullOrEmpty ( value ) && value == word ) value = $ "{ value } 1";
144+ return new KeyValuePair < string , string > ( value , type ) ;
138145 }
139146
140147 internal static string ProcessTemplate ( string pattern , string template , ASResult expr )
141148 {
142- switch ( pattern )
149+ return pattern switch
143150 {
144- case PatternCollection :
145- return ProcessCollectionTemplate ( template , expr ) ;
146- case PatternHash :
147- return ProcessHashTemplate ( template , expr ) ;
148- default :
149- return template ;
150- }
151+ PatternCollection => ProcessCollectionTemplate ( template , expr ) ,
152+ PatternHash => ProcessHashTemplate ( template , expr ) ,
153+ _ => template
154+ } ;
151155 }
152156
153157 internal static string ProcessMemberTemplate ( string template , ASResult expr )
@@ -203,7 +207,7 @@ internal static string GetDescription(ASResult expr, string template, string pcc
203207 {
204208 var sci = PluginBase . MainForm . CurrentDocument . SciControl ;
205209 var position = ScintillaControlHelper . GetDotLeftStartPosition ( sci , sci . CurrentPos - 1 ) ;
206- var exprStartPosition = ASGenerator . GetStartOfStatement ( sci , sci . CurrentPos , expr ) ;
210+ var exprStartPosition = ASGenerator . GetStartOfStatement ( expr ) ;
207211 var lineNum = sci . CurrentLine ;
208212 var line = sci . GetLine ( lineNum ) ;
209213 var snippet = line . Substring ( exprStartPosition - sci . PositionFromLine ( lineNum ) , position - exprStartPosition ) ;
@@ -222,7 +226,7 @@ internal static void InsertSnippetText(ASResult expr, string template, string pc
222226 var position = ScintillaControlHelper . GetDotLeftStartPosition ( sci , sci . CurrentPos - 1 ) ;
223227 sci . SetSel ( position , sci . CurrentPos ) ;
224228 sci . ReplaceSel ( string . Empty ) ;
225- position = ASGenerator . GetStartOfStatement ( sci , sci . CurrentPos , expr ) ;
229+ position = ASGenerator . GetStartOfStatement ( expr ) ;
226230 sci . SetSel ( position , sci . CurrentPos ) ;
227231 var snippet = Regex . Replace ( template , string . Format ( PatternBlock , pccpattern ) , sci . SelText , RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
228232 snippet = ProcessMemberTemplate ( snippet , expr ) ;
0 commit comments