Skip to content

Commit

Permalink
Fix fuzzy searching for findFiles2 (microsoft#204768)
Browse files Browse the repository at this point in the history
* progress on making fuzzy option
* finish connection to findfiles API
  • Loading branch information
andreamah authored Feb 8, 2024
1 parent bcf9b4f commit 90cebfa
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,8 @@ suite('vscode API - workspace', () => {
});

test('`findFiles2`', () => {
return vscode.workspace.findFiles2('*image.png').then((res) => {
assert.strictEqual(res.length, 4);
// TODO: see why this is fuzzy matching
return vscode.workspace.findFiles2('**/image.png').then((res) => {
assert.strictEqual(res.length, 2);
});
});

Expand All @@ -619,9 +618,8 @@ suite('vscode API - workspace', () => {
});

test('findFiles2, exclude', () => {
return vscode.workspace.findFiles2('*image.png', { exclude: '**/sub/**' }).then((res) => {
assert.strictEqual(res.length, 3);
// TODO: see why this is fuzzy matching
return vscode.workspace.findFiles2('**/image.png', { exclude: '**/sub/**' }).then((res) => {
assert.strictEqual(res.length, 1);
});
});

Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHostWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac
disregardSearchExcludeSettings: typeof options.useDefaultSearchExcludes === 'boolean' ? !options.useDefaultSearchExcludes : false,
maxResults: options.maxResults,
excludePattern: excludePattern,
shouldGlobSearch: typeof options.fuzzy === 'boolean' ? !options.fuzzy : true,
_reason: 'startFileSearch'
};
let folderToUse: URI | undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/services/search/common/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface IFileQueryBuilderOptions extends ICommonQueryBuilderOptions {
exists?: boolean;
sortByScore?: boolean;
cacheKey?: string;
shouldGlobSearch?: boolean;
}

export interface ITextQueryBuilderOptions extends ICommonQueryBuilderOptions {
Expand Down Expand Up @@ -188,6 +189,7 @@ export class QueryBuilder {
exists: options.exists,
sortByScore: options.sortByScore,
cacheKey: options.cacheKey,
shouldGlobMatchFilePattern: options.shouldGlobSearch
};
}

Expand Down
12 changes: 10 additions & 2 deletions src/vs/workbench/services/search/common/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export interface ICommonQueryProps<U extends UriComponents> {
_reason?: string;

folderQueries: IFolderQuery<U>[];
// The include pattern for files that gets passed into ripgrep.
// Note that this will override any ignore files if applicable.
includePattern?: glob.IExpression;
excludePattern?: glob.IExpression;
extraFileResources?: U[];
Expand All @@ -95,6 +97,10 @@ export interface IFileQueryProps<U extends UriComponents> extends ICommonQueryPr
type: QueryType.File;
filePattern?: string;

// when walking through the tree to find the result, don't use the filePattern to fuzzy match.
// Instead, should use glob matching.
shouldGlobMatchFilePattern?: boolean;

/**
* If true no results will be returned. Instead `limitHit` will indicate if at least one result exists or not.
* Currently does not work with queries including a 'siblings clause'.
Expand Down Expand Up @@ -586,9 +592,11 @@ export function isSerializedFileMatch(arg: ISerializedSearchProgressItem): arg i
return !!(<ISerializedFileMatch>arg).path;
}

export function isFilePatternMatch(candidate: IRawFileMatch, normalizedFilePatternLowercase: string): boolean {
export function isFilePatternMatch(candidate: IRawFileMatch, filePatternToUse: string, fuzzy = true): boolean {
const pathToMatch = candidate.searchPath ? candidate.searchPath : candidate.relativePath;
return fuzzyContains(pathToMatch, normalizedFilePatternLowercase);
return fuzzy ?
fuzzyContains(pathToMatch, filePatternToUse) :
glob.match(filePatternToUse, pathToMatch);
}

export interface ISerializedFileMatch {
Expand Down
4 changes: 3 additions & 1 deletion src/vs/workbench/services/search/node/fileSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class FileWalker {
this.errors = [];

if (this.filePattern) {
this.normalizedFilePatternLowercase = prepareQuery(this.filePattern).normalizedLowercase;
this.normalizedFilePatternLowercase = config.shouldGlobMatchFilePattern ? null : prepareQuery(this.filePattern).normalizedLowercase;
}

this.globalExcludePattern = config.excludePattern && glob.parse(config.excludePattern);
Expand Down Expand Up @@ -579,6 +579,8 @@ export class FileWalker {

if (this.normalizedFilePatternLowercase) {
return isFilePatternMatch(candidate, this.normalizedFilePatternLowercase);
} else if (this.filePattern) {
return isFilePatternMatch(candidate, this.filePattern, false);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/vscode-dts/vscode.proposed.findFiles2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ declare module 'vscode' {
* See the vscode setting `"search.followSymlinks"`.
*/
followSymlinks?: boolean;

/**
* If set to true, the `filePattern` arg will be fuzzy-searched instead of glob-searched.
* If `filePattern` is a `GlobPattern`, then the fuzzy search will act on the `pattern` of the `RelativePattern`
*/
fuzzy?: boolean;
}

/**
Expand Down

0 comments on commit 90cebfa

Please sign in to comment.