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 de813fb
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/file-search/src/node/file-search-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,29 @@ 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];
// 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) {
Expand All @@ -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,
Expand Down

0 comments on commit de813fb

Please sign in to comment.