@@ -62,46 +62,57 @@ internal async Task<List<Result>> SearchAsync(Query query, CancellationToken tok
62
62
return new List < Result > ( ) ;
63
63
}
64
64
65
- IAsyncEnumerable < SearchResult > searchResults = null ;
65
+ IAsyncEnumerable < SearchResult > searchResults ;
66
66
67
- bool isPathSearch = query . Search . IsLocationPathString ( ) ;
67
+ bool isPathSearch = query . Search . IsLocationPathString ( ) || query . Search . StartsWith ( "%" ) ;
68
+
69
+ string EngineName ;
68
70
69
71
switch ( isPathSearch )
70
72
{
71
73
case true
72
74
when ActionKeywordMatch ( query , Settings . ActionKeyword . PathSearchActionKeyword )
73
- || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
74
-
75
+ || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
76
+
75
77
results . UnionWith ( await PathSearchAsync ( query , token ) . ConfigureAwait ( false ) ) ;
76
-
78
+
77
79
return results . ToList ( ) ;
78
80
79
- case false
81
+ case false
80
82
when ActionKeywordMatch ( query , Settings . ActionKeyword . FileContentSearchActionKeyword ) :
81
-
83
+
82
84
// Intentionally require enabling of Everything's content search due to its slowness
83
85
if ( Settings . ContentIndexProvider is EverythingSearchManager && ! Settings . EnableEverythingContentSearch )
84
86
return EverythingContentSearchResult ( query ) ;
85
-
87
+
86
88
searchResults = Settings . ContentIndexProvider . ContentSearchAsync ( "" , query . Search , token ) ;
87
-
89
+ EngineName = Enum . GetName ( Settings . ContentSearchEngine ) ;
88
90
break ;
89
-
91
+
90
92
case false
91
93
when ActionKeywordMatch ( query , Settings . ActionKeyword . IndexSearchActionKeyword )
92
- || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
93
-
94
+ || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
95
+
94
96
searchResults = Settings . IndexProvider . SearchAsync ( query . Search , token ) ;
95
-
97
+ EngineName = Enum . GetName ( Settings . IndexSearchEngine ) ;
96
98
break ;
99
+ default :
100
+ return results . ToList ( ) ;
97
101
}
98
102
99
- if ( searchResults == null )
100
- return results . ToList ( ) ;
101
-
102
- await foreach ( var search in searchResults . WithCancellation ( token ) . ConfigureAwait ( false ) )
103
- results . Add ( ResultManager . CreateResult ( query , search ) ) ;
103
+ try
104
+ {
105
+ await foreach ( var search in searchResults . WithCancellation ( token ) . ConfigureAwait ( false ) )
106
+ results . Add ( ResultManager . CreateResult ( query , search ) ) ;
107
+ }
108
+ catch ( Exception e )
109
+ {
110
+ if ( e is OperationCanceledException )
111
+ return results . ToList ( ) ;
104
112
113
+ throw new SearchException ( EngineName , e . Message , e ) ;
114
+ }
115
+
105
116
results . RemoveWhere ( r => Settings . IndexSearchExcludedSubdirectoryPaths . Any (
106
117
excludedPath => r . SubTitle . StartsWith ( excludedPath . Path , StringComparison . OrdinalIgnoreCase ) ) ) ;
107
118
@@ -175,46 +186,47 @@ private async Task<List<Result>> PathSearchAsync(Query query, CancellationToken
175
186
176
187
var retrievedDirectoryPath = FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( locationPath ) ;
177
188
178
- results . Add ( retrievedDirectoryPath . EndsWith ( ":\\ " )
189
+ results . Add ( retrievedDirectoryPath . EndsWith ( ":\\ " )
179
190
? ResultManager . CreateDriveSpaceDisplayResult ( retrievedDirectoryPath , query . ActionKeyword , useIndexSearch )
180
191
: ResultManager . CreateOpenCurrentFolderResult ( retrievedDirectoryPath , query . ActionKeyword , useIndexSearch ) ) ;
181
192
182
193
if ( token . IsCancellationRequested )
183
194
return new List < Result > ( ) ;
184
195
185
- IEnumerable < SearchResult > directoryResult ;
196
+ IAsyncEnumerable < SearchResult > directoryResult ;
186
197
187
198
var recursiveIndicatorIndex = query . Search . IndexOf ( '>' ) ;
188
199
189
200
if ( recursiveIndicatorIndex > 0 && Settings . PathEnumerationEngine != Settings . PathEnumerationEngineOption . DirectEnumeration )
190
201
{
191
202
directoryResult =
192
- await Settings . PathEnumerator . EnumerateAsync (
193
- query . Search [ ..recursiveIndicatorIndex ] ,
194
- query . Search [ ( recursiveIndicatorIndex + 1 ) ..] ,
195
- true ,
196
- token )
197
- . ToListAsync ( cancellationToken : token )
198
- . ConfigureAwait ( false ) ;
203
+ Settings . PathEnumerator . EnumerateAsync (
204
+ query . Search [ ..recursiveIndicatorIndex ] ,
205
+ query . Search [ ( recursiveIndicatorIndex + 1 ) ..] ,
206
+ true ,
207
+ token ) ;
199
208
200
209
}
201
210
else
202
211
{
203
- try
204
- {
205
- directoryResult = DirectoryInfoSearch . TopLevelDirectorySearch ( query , query . Search , token ) ;
206
- }
207
- catch ( Exception e )
208
- {
209
- throw new SearchException ( "DirectoryInfoSearch" , e . Message , e ) ;
210
- }
212
+ directoryResult = DirectoryInfoSearch . TopLevelDirectorySearch ( query , query . Search , token ) . ToAsyncEnumerable ( ) ;
211
213
}
212
214
215
+ if ( token . IsCancellationRequested )
216
+ return new List < Result > ( ) ;
213
217
218
+ try
219
+ {
220
+ await foreach ( var directory in directoryResult . WithCancellation ( token ) . ConfigureAwait ( false ) )
221
+ {
222
+ results . Add ( ResultManager . CreateResult ( query , directory ) ) ;
223
+ }
224
+ }
225
+ catch ( Exception e )
226
+ {
227
+ throw new SearchException ( Enum . GetName ( Settings . PathEnumerationEngine ) , e . Message , e ) ;
228
+ }
214
229
215
- token . ThrowIfCancellationRequested ( ) ;
216
-
217
- results . UnionWith ( directoryResult . Select ( searchResult => ResultManager . CreateResult ( query , searchResult ) ) ) ;
218
230
219
231
return results . ToList ( ) ;
220
232
}
@@ -227,7 +239,7 @@ private bool UseWindowsIndexForDirectorySearch(string locationPath)
227
239
var pathToDirectory = FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( locationPath ) ;
228
240
229
241
return ! Settings . IndexSearchExcludedSubdirectoryPaths . Any (
230
- x => FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( pathToDirectory ) . StartsWith ( x . Path , StringComparison . OrdinalIgnoreCase ) )
242
+ x => FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( pathToDirectory ) . StartsWith ( x . Path , StringComparison . OrdinalIgnoreCase ) )
231
243
&& WindowsIndex . WindowsIndex . PathIsIndexed ( pathToDirectory ) ;
232
244
}
233
245
}
0 commit comments