Fix fuzzy searching for findFiles2 (#204768)

* progress on making fuzzy option
* finish connection to findfiles API
This commit is contained in:
Andrea Mah 2024-02-08 15:17:58 -06:00 committed by GitHub
parent bcf9b4ff0f
commit 90cebfaeb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 26 additions and 9 deletions

View file

@ -598,9 +598,8 @@ suite('vscode API - workspace', () => {
}); });
test('`findFiles2`', () => { test('`findFiles2`', () => {
return vscode.workspace.findFiles2('*image.png').then((res) => { return vscode.workspace.findFiles2('**/image.png').then((res) => {
assert.strictEqual(res.length, 4); assert.strictEqual(res.length, 2);
// TODO: see why this is fuzzy matching
}); });
}); });
@ -619,9 +618,8 @@ suite('vscode API - workspace', () => {
}); });
test('findFiles2, exclude', () => { test('findFiles2, exclude', () => {
return vscode.workspace.findFiles2('*image.png', { exclude: '**/sub/**' }).then((res) => { return vscode.workspace.findFiles2('**/image.png', { exclude: '**/sub/**' }).then((res) => {
assert.strictEqual(res.length, 3); assert.strictEqual(res.length, 1);
// TODO: see why this is fuzzy matching
}); });
}); });

View file

@ -497,6 +497,7 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac
disregardSearchExcludeSettings: typeof options.useDefaultSearchExcludes === 'boolean' ? !options.useDefaultSearchExcludes : false, disregardSearchExcludeSettings: typeof options.useDefaultSearchExcludes === 'boolean' ? !options.useDefaultSearchExcludes : false,
maxResults: options.maxResults, maxResults: options.maxResults,
excludePattern: excludePattern, excludePattern: excludePattern,
shouldGlobSearch: typeof options.fuzzy === 'boolean' ? !options.fuzzy : true,
_reason: 'startFileSearch' _reason: 'startFileSearch'
}; };
let folderToUse: URI | undefined; let folderToUse: URI | undefined;

View file

@ -72,6 +72,7 @@ export interface IFileQueryBuilderOptions extends ICommonQueryBuilderOptions {
exists?: boolean; exists?: boolean;
sortByScore?: boolean; sortByScore?: boolean;
cacheKey?: string; cacheKey?: string;
shouldGlobSearch?: boolean;
} }
export interface ITextQueryBuilderOptions extends ICommonQueryBuilderOptions { export interface ITextQueryBuilderOptions extends ICommonQueryBuilderOptions {
@ -188,6 +189,7 @@ export class QueryBuilder {
exists: options.exists, exists: options.exists,
sortByScore: options.sortByScore, sortByScore: options.sortByScore,
cacheKey: options.cacheKey, cacheKey: options.cacheKey,
shouldGlobMatchFilePattern: options.shouldGlobSearch
}; };
} }

View file

@ -81,6 +81,8 @@ export interface ICommonQueryProps<U extends UriComponents> {
_reason?: string; _reason?: string;
folderQueries: IFolderQuery<U>[]; 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; includePattern?: glob.IExpression;
excludePattern?: glob.IExpression; excludePattern?: glob.IExpression;
extraFileResources?: U[]; extraFileResources?: U[];
@ -95,6 +97,10 @@ export interface IFileQueryProps<U extends UriComponents> extends ICommonQueryPr
type: QueryType.File; type: QueryType.File;
filePattern?: string; 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. * 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'. * Currently does not work with queries including a 'siblings clause'.
@ -586,9 +592,11 @@ export function isSerializedFileMatch(arg: ISerializedSearchProgressItem): arg i
return !!(<ISerializedFileMatch>arg).path; 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; const pathToMatch = candidate.searchPath ? candidate.searchPath : candidate.relativePath;
return fuzzyContains(pathToMatch, normalizedFilePatternLowercase); return fuzzy ?
fuzzyContains(pathToMatch, filePatternToUse) :
glob.match(filePatternToUse, pathToMatch);
} }
export interface ISerializedFileMatch { export interface ISerializedFileMatch {

View file

@ -75,7 +75,7 @@ export class FileWalker {
this.errors = []; this.errors = [];
if (this.filePattern) { 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); this.globalExcludePattern = config.excludePattern && glob.parse(config.excludePattern);
@ -579,6 +579,8 @@ export class FileWalker {
if (this.normalizedFilePatternLowercase) { if (this.normalizedFilePatternLowercase) {
return isFilePatternMatch(candidate, this.normalizedFilePatternLowercase); return isFilePatternMatch(candidate, this.normalizedFilePatternLowercase);
} else if (this.filePattern) {
return isFilePatternMatch(candidate, this.filePattern, false);
} }
} }

View file

@ -55,6 +55,12 @@ declare module 'vscode' {
* See the vscode setting `"search.followSymlinks"`. * See the vscode setting `"search.followSymlinks"`.
*/ */
followSymlinks?: boolean; 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;
} }
/** /**