diff --git a/packages/file-search/src/node/file-search-service-impl.ts b/packages/file-search/src/node/file-search-service-impl.ts index 84076019b780b..f856bc6c0d375 100644 --- a/packages/file-search/src/node/file-search-service-impl.ts +++ b/packages/file-search/src/node/file-search-service-impl.ts @@ -68,27 +68,36 @@ export class FileSearchServiceImpl implements FileSearchService { } } - const exactMatches = new Set(); - const fuzzyMatches = new Set(); + const matches = new Set(); + const stringPattern = searchPattern.toLocaleLowerCase(); await Promise.all(Object.keys(roots).map(async root => { try { const rootUri = new URI(root); const rootOptions = roots[root]; - await this.doFind(rootUri, rootOptions, candidate => { - const fileUri = rootUri.resolve(candidate).toString(); - if (exactMatches.has(fileUri) || fuzzyMatches.has(fileUri)) { - return; - } - if (!searchPattern || searchPattern === '*' || candidate.toLocaleLowerCase().indexOf(stringPattern) !== -1) { - exactMatches.add(fileUri); - } else if (opts.fuzzyMatch && fuzzy.test(searchPattern, candidate)) { - fuzzyMatches.add(fileUri); - } - if (exactMatches.size + fuzzyMatches.size === opts.limit) { - cancellationSource.cancel(); - } - }, token); + // Perform searches for `exact` and `fuzzy` matches in parallel. + await Promise.all([ + // Get 'exact' match results. + this.doFind(rootUri, rootOptions, candidate => { + const fileUri = rootUri.resolve(candidate).toString(); + if (!searchPattern || searchPattern === '*' || candidate.toLocaleLowerCase().indexOf(stringPattern) !== -1) { + if (matches.size === opts.limit) { + cancellationSource.cancel(); + } + matches.add(fileUri); + } + }, token), + // Get the 'fuzzy' match results. + this.doFind(rootUri, rootOptions, candidate => { + const fileUri = rootUri.resolve(candidate).toString(); + if (opts.fuzzyMatch && fuzzy.test(searchPattern, candidate)) { + if (matches.size === opts.limit) { + cancellationSource.cancel(); + } + matches.add(fileUri); + } + }, token) + ]); } catch (e) { console.error('Failed to search:', root, e); } @@ -96,7 +105,8 @@ export class FileSearchServiceImpl implements FileSearchService { if (clientToken && clientToken.isCancellationRequested) { return []; } - return [...exactMatches, ...fuzzyMatches]; + + return [...matches]; } private doFind(rootUri: URI, options: FileSearchService.BaseOptions,