@@ -62,46 +62,57 @@ internal async Task<List<Result>> SearchAsync(Query query, CancellationToken tok
6262 return new List < Result > ( ) ;
6363 }
6464
65- IAsyncEnumerable < SearchResult > searchResults = null ;
65+ IAsyncEnumerable < SearchResult > searchResults ;
6666
67- bool isPathSearch = query . Search . IsLocationPathString ( ) ;
67+ bool isPathSearch = query . Search . IsLocationPathString ( ) || query . Search . StartsWith ( "%" ) ;
68+
69+ string EngineName ;
6870
6971 switch ( isPathSearch )
7072 {
7173 case true
7274 when ActionKeywordMatch ( query , Settings . ActionKeyword . PathSearchActionKeyword )
73- || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
74-
75+ || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
76+
7577 results . UnionWith ( await PathSearchAsync ( query , token ) . ConfigureAwait ( false ) ) ;
76-
78+
7779 return results . ToList ( ) ;
7880
79- case false
81+ case false
8082 when ActionKeywordMatch ( query , Settings . ActionKeyword . FileContentSearchActionKeyword ) :
81-
83+
8284 // Intentionally require enabling of Everything's content search due to its slowness
8385 if ( Settings . ContentIndexProvider is EverythingSearchManager && ! Settings . EnableEverythingContentSearch )
8486 return EverythingContentSearchResult ( query ) ;
85-
87+
8688 searchResults = Settings . ContentIndexProvider . ContentSearchAsync ( "" , query . Search , token ) ;
87-
89+ EngineName = Enum . GetName ( Settings . ContentSearchEngine ) ;
8890 break ;
89-
91+
9092 case false
9193 when ActionKeywordMatch ( query , Settings . ActionKeyword . IndexSearchActionKeyword )
92- || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
93-
94+ || ActionKeywordMatch ( query , Settings . ActionKeyword . SearchActionKeyword ) :
95+
9496 searchResults = Settings . IndexProvider . SearchAsync ( query . Search , token ) ;
95-
97+ EngineName = Enum . GetName ( Settings . IndexSearchEngine ) ;
9698 break ;
99+ default :
100+ return results . ToList ( ) ;
97101 }
98102
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 ( ) ;
104112
113+ throw new SearchException ( EngineName , e . Message , e ) ;
114+ }
115+
105116 results . RemoveWhere ( r => Settings . IndexSearchExcludedSubdirectoryPaths . Any (
106117 excludedPath => r . SubTitle . StartsWith ( excludedPath . Path , StringComparison . OrdinalIgnoreCase ) ) ) ;
107118
@@ -175,46 +186,47 @@ private async Task<List<Result>> PathSearchAsync(Query query, CancellationToken
175186
176187 var retrievedDirectoryPath = FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( locationPath ) ;
177188
178- results . Add ( retrievedDirectoryPath . EndsWith ( ":\\ " )
189+ results . Add ( retrievedDirectoryPath . EndsWith ( ":\\ " )
179190 ? ResultManager . CreateDriveSpaceDisplayResult ( retrievedDirectoryPath , query . ActionKeyword , useIndexSearch )
180191 : ResultManager . CreateOpenCurrentFolderResult ( retrievedDirectoryPath , query . ActionKeyword , useIndexSearch ) ) ;
181192
182193 if ( token . IsCancellationRequested )
183194 return new List < Result > ( ) ;
184195
185- IEnumerable < SearchResult > directoryResult ;
196+ IAsyncEnumerable < SearchResult > directoryResult ;
186197
187198 var recursiveIndicatorIndex = query . Search . IndexOf ( '>' ) ;
188199
189200 if ( recursiveIndicatorIndex > 0 && Settings . PathEnumerationEngine != Settings . PathEnumerationEngineOption . DirectEnumeration )
190201 {
191202 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 ) ;
199208
200209 }
201210 else
202211 {
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 ( ) ;
211213 }
212214
215+ if ( token . IsCancellationRequested )
216+ return new List < Result > ( ) ;
213217
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+ }
214229
215- token . ThrowIfCancellationRequested ( ) ;
216-
217- results . UnionWith ( directoryResult . Select ( searchResult => ResultManager . CreateResult ( query , searchResult ) ) ) ;
218230
219231 return results . ToList ( ) ;
220232 }
@@ -227,7 +239,7 @@ private bool UseWindowsIndexForDirectorySearch(string locationPath)
227239 var pathToDirectory = FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( locationPath ) ;
228240
229241 return ! Settings . IndexSearchExcludedSubdirectoryPaths . Any (
230- x => FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( pathToDirectory ) . StartsWith ( x . Path , StringComparison . OrdinalIgnoreCase ) )
242+ x => FilesFolders . ReturnPreviousDirectoryIfIncompleteString ( pathToDirectory ) . StartsWith ( x . Path , StringComparison . OrdinalIgnoreCase ) )
231243 && WindowsIndex . WindowsIndex . PathIsIndexed ( pathToDirectory ) ;
232244 }
233245 }
0 commit comments