@@ -27,6 +27,8 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IContextMe
27
27
private readonly PluginJsonStorage < Settings > _storage ;
28
28
private IContextMenu _contextMenuLoader ;
29
29
30
+ private static Dictionary < string , string > _envStringPaths ;
31
+
30
32
public Main ( )
31
33
{
32
34
_storage = new PluginJsonStorage < Settings > ( ) ;
@@ -48,6 +50,7 @@ public void Init(PluginInitContext context)
48
50
_context = context ;
49
51
_contextMenuLoader = new ContextMenuLoader ( context ) ;
50
52
InitialDriverList ( ) ;
53
+ LoadEnvironmentStringPaths ( ) ;
51
54
}
52
55
53
56
public List < Result > Query ( Query query )
@@ -58,7 +61,14 @@ public List<Result> Query(Query query)
58
61
if ( ! IsDriveOrSharedFolder ( search ) )
59
62
return results ;
60
63
61
- results . AddRange ( QueryInternal_Directory_Exists ( query ) ) ;
64
+ if ( search . StartsWith ( "%" ) )
65
+ {
66
+ results . AddRange ( GetEnvironmentStringPathResults ( search , query ) ) ;
67
+ }
68
+ else
69
+ {
70
+ results . AddRange ( QueryInternal_Directory_Exists ( query . Search , query ) ) ;
71
+ }
62
72
63
73
// todo why was this hack here?
64
74
foreach ( var result in results )
@@ -72,7 +82,12 @@ public List<Result> Query(Query query)
72
82
private static bool IsDriveOrSharedFolder ( string search )
73
83
{
74
84
if ( search . StartsWith ( @"\\" ) )
75
- { // share folder
85
+ { // shared folder
86
+ return true ;
87
+ }
88
+
89
+ if ( _envStringPaths != null && search . StartsWith ( "%" ) )
90
+ { // environment string formatted folder
76
91
return true ;
77
92
}
78
93
@@ -146,14 +161,37 @@ private void InitialDriverList()
146
161
}
147
162
}
148
163
164
+ private void LoadEnvironmentStringPaths ( )
165
+ {
166
+ _envStringPaths = new Dictionary < string , string > ( ) ;
167
+
168
+ var specialPaths =
169
+ new Dictionary < string , Environment . SpecialFolder > {
170
+ { "appdata" , Environment . SpecialFolder . ApplicationData } ,
171
+ { "localappdata" , Environment . SpecialFolder . LocalApplicationData } ,
172
+ { "programfiles" , Environment . SpecialFolder . ProgramFiles } ,
173
+ { "programfiles(x86)" , Environment . SpecialFolder . ProgramFilesX86 } ,
174
+ { "programdata" , Environment . SpecialFolder . CommonApplicationData } ,
175
+ { "userprofile" , Environment . SpecialFolder . UserProfile }
176
+ } ;
177
+
178
+ foreach ( var special in specialPaths )
179
+ {
180
+ _envStringPaths . Add ( special . Key , Environment . GetFolderPath ( special . Value ) ) ;
181
+ }
182
+
183
+ var tempDirectoryPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "Temp" ) ;
184
+
185
+ _envStringPaths . Add ( "temp" , tempDirectoryPath ) ;
186
+ }
187
+
149
188
private static readonly char [ ] _specialSearchChars = new char [ ]
150
189
{
151
190
'?' , '*' , '>'
152
191
} ;
153
192
154
- private List < Result > QueryInternal_Directory_Exists ( Query query )
193
+ private List < Result > QueryInternal_Directory_Exists ( string search , Query query )
155
194
{
156
- var search = query . Search ;
157
195
var results = new List < Result > ( ) ;
158
196
var hasSpecial = search . IndexOfAny ( _specialSearchChars ) >= 0 ;
159
197
string incompleteName = "" ;
@@ -243,6 +281,63 @@ private List<Result> QueryInternal_Directory_Exists(Query query)
243
281
return results . Concat ( folderList . OrderBy ( x => x . Title ) ) . Concat ( fileList . OrderBy ( x => x . Title ) ) . ToList ( ) ;
244
282
}
245
283
284
+ private List < Result > GetEnvironmentStringPathSuggestions ( string search , Query query )
285
+ {
286
+ var results = new List < Result > ( ) ;
287
+ foreach ( var p in _envStringPaths )
288
+ {
289
+ if ( p . Key . StartsWith ( search ) )
290
+ {
291
+ results . Add ( CreateFolderResult ( $ "%{ p . Key } %", p . Value , p . Value , query ) ) ;
292
+ }
293
+ }
294
+ return results ;
295
+ }
296
+
297
+ private List < Result > GetEnvironmentStringPathResults ( string envStringSearch , Query query )
298
+ {
299
+ if ( envStringSearch == "%" )
300
+ { // return all environment string options as path suggestions
301
+ return GetEnvironmentStringPathSuggestions ( "" , query ) ;
302
+ }
303
+
304
+ var results = new List < Result > ( ) ;
305
+ var search = envStringSearch . Substring ( 1 ) ;
306
+
307
+ if ( search . EndsWith ( "%" ) && search . Length > 1 )
308
+ { // query starts and ends with a %, find an exact match from env-string paths
309
+ var exactEnvStringPath = search . Substring ( 0 , search . Length - 1 ) ;
310
+
311
+ if ( _envStringPaths . ContainsKey ( exactEnvStringPath ) )
312
+ {
313
+ var expandedPath = _envStringPaths [ exactEnvStringPath ] ;
314
+ results . Add ( CreateFolderResult ( $ "%{ exactEnvStringPath } %", expandedPath , expandedPath , query ) ) ;
315
+ }
316
+ }
317
+ else if ( search . Contains ( "%" ) )
318
+ { // query starts with a % and contains another % somewhere before the end
319
+ var splitSearch = search . Split ( "%" ) ;
320
+ var exactEnvStringPath = splitSearch [ 0 ] ;
321
+
322
+ // if there are more than 2 % characters in the query, don't bother
323
+ if ( splitSearch . Length == 2 && _envStringPaths . ContainsKey ( exactEnvStringPath ) )
324
+ {
325
+ var queryPartToReplace = $ "%{ exactEnvStringPath } %";
326
+ var expandedPath = _envStringPaths [ exactEnvStringPath ] ;
327
+ // replace the %envstring% part of the query with its expanded equivalent
328
+ var updatedSearch = envStringSearch . Replace ( queryPartToReplace , expandedPath ) ;
329
+
330
+ results . AddRange ( QueryInternal_Directory_Exists ( updatedSearch , query ) ) ;
331
+ }
332
+ }
333
+ else
334
+ { // query simply starts wtih a %, suggest env-string paths that match the rest of the search
335
+ results . AddRange ( GetEnvironmentStringPathSuggestions ( search , query ) ) ;
336
+ }
337
+
338
+ return results ;
339
+ }
340
+
246
341
private static Result CreateFileResult ( string filePath , Query query )
247
342
{
248
343
var result = new Result
0 commit comments