Skip to content

Commit

Permalink
Update file-search to prioritize exact matches
Browse files Browse the repository at this point in the history
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 <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Jul 9, 2019
1 parent 37c9659 commit 73262de
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions packages/file-search/src/node/file-search-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,45 @@ export class FileSearchServiceImpl implements FileSearchService {
}
}

const exactMatches = new Set<string>();
const fuzzyMatches = new Set<string>();
const matches = new Set<string>();

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);
}
}));
if (clientToken && clientToken.isCancellationRequested) {
return [];
}
return [...exactMatches, ...fuzzyMatches];

return [...matches];
}

private doFind(rootUri: URI, options: FileSearchService.BaseOptions,
Expand Down

0 comments on commit 73262de

Please sign in to comment.