From de813fbcd3aafe88765c7f830082d06ea6c7ab22 Mon Sep 17 00:00:00 2001 From: Vincent Fugnitto Date: Thu, 4 Jul 2019 08:29:50 -0400 Subject: [PATCH] Update file-search to prioritize exact matches Fixes #5636 Fixes an issue where `exact` file results were not being displayed since `fuzzy` matches were added instead. Due to the limit present when searching for files, `exact` matches should be prioritized more while `fuzzy` matches should be used to fill up the result list if necessary. Adjusting the code means that better results are returned, and for an end-user, they get more consistent results in respect to their workspace. Signed-off-by: Vincent Fugnitto --- .../src/node/file-search-service-impl.ts | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) 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..8bc330cd6e11c 100644 --- a/packages/file-search/src/node/file-search-service-impl.ts +++ b/packages/file-search/src/node/file-search-service-impl.ts @@ -68,25 +68,29 @@ 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]; + // Get 'exact' match results. 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 (matches.size !== opts.limit) { + matches.add(fileUri); + } } - if (exactMatches.size + fuzzyMatches.size === opts.limit) { - cancellationSource.cancel(); + }, token); + // Get 'fuzzy' match results. + await this.doFind(rootUri, rootOptions, candidate => { + const fileUri = rootUri.resolve(candidate).toString(); + if (opts.fuzzyMatch && fuzzy.test(searchPattern, candidate)) { + if (matches.size !== opts.limit) { + matches.add(fileUri); + } } }, token); } catch (e) { @@ -96,7 +100,8 @@ export class FileSearchServiceImpl implements FileSearchService { if (clientToken && clientToken.isCancellationRequested) { return []; } - return [...exactMatches, ...fuzzyMatches]; + + return [...matches]; } private doFind(rootUri: URI, options: FileSearchService.BaseOptions,