Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Explorer Find widget work in 1.94 as long as proposed APIs are enabled (fix #1443) #1444

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/providers/FileSystemProvider/FileSearchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class FileSearchProvider implements vscode.FileSearchProvider {
): Promise<vscode.Uri[]> {
let counter = 0;
let pattern = query.pattern.charAt(0) == "/" ? query.pattern.slice(1) : query.pattern;

// Drop a leading **/ from the glob pattern if it exists (added by Find widget of Explorer tree, which since 1.94 uses FileSearchProvider)
if (pattern.startsWith("**/")) {
pattern = pattern.slice(3);
}
const params = new URLSearchParams(options.folder.query);
const csp = params.has("csp") && ["", "1"].includes(params.get("csp"));
if (params.has("project") && params.get("project").length) {
Expand All @@ -44,13 +49,17 @@ export class FileSearchProvider implements vscode.FileSearchProvider {
// When this is called without a query.pattern, every file is supposed to be returned, so do not provide a filter
let filter = "";
if (pattern.length) {
pattern = !csp ? query.pattern.replace(/\//g, ".") : query.pattern;
let escapeClause = "";
pattern = !csp ? pattern.replace(/\//g, ".") : pattern;
if (pattern.includes("_") || pattern.includes("%")) {
// Need to escape any % or _ characters
filter = `Name LIKE '%${pattern.replace(/(_|%|\\)/g, "\\$1")}%' ESCAPE '\\'`;
} else {
filter = `Name LIKE '%${pattern}%'`;
pattern = pattern.replace(/(_|%|\\)/g, "\\$1");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not always do this replacement and provide the ESCAPE clause? I doubt it impacts performance significantly and it makes this code shorter:

filter = `Name LIKE '%${pattern.replace(/(_|%|\\)/g, "\\$1").replace(/\*/g, "%").replace(/\?/g, "_")}%' ESCAPE '\\'`

escapeClause = " ESCAPE '\\'";
}
// Change glob syntax to SQL LIKE syntax
pattern = pattern.replace(/\*/g, "%");
pattern = pattern.replace(/\?/g, "_");
filter = `Name LIKE '%${pattern}%'${escapeClause}`;
}
if (token.isCancellationRequested) {
return;
Expand Down