|
| 1 | +import * as vscode from "vscode" |
| 2 | +import * as path from "path" |
| 3 | +import * as fs from "fs" |
| 4 | +import * as childProcess from "child_process" |
| 5 | +import * as readline from "readline" |
| 6 | +import { Fzf } from "fzf" |
| 7 | +import { getBinPath } from "../ripgrep" |
| 8 | + |
| 9 | +async function executeRipgrepForFiles( |
| 10 | + rgPath: string, |
| 11 | + workspacePath: string, |
| 12 | + limit: number = 5000, |
| 13 | +): Promise<{ path: string; type: "file" | "folder"; label?: string }[]> { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + const args = [ |
| 16 | + "--files", |
| 17 | + "--follow", |
| 18 | + "-g", |
| 19 | + "!**/node_modules/**", |
| 20 | + "-g", |
| 21 | + "!**/.git/**", |
| 22 | + "-g", |
| 23 | + "!**/out/**", |
| 24 | + "-g", |
| 25 | + "!**/dist/**", |
| 26 | + workspacePath, |
| 27 | + ] |
| 28 | + |
| 29 | + const rgProcess = childProcess.spawn(rgPath, args) |
| 30 | + const rl = readline.createInterface({ |
| 31 | + input: rgProcess.stdout, |
| 32 | + crlfDelay: Infinity, |
| 33 | + }) |
| 34 | + |
| 35 | + const results: { path: string; type: "file" | "folder"; label?: string }[] = [] |
| 36 | + let count = 0 |
| 37 | + |
| 38 | + rl.on("line", (line) => { |
| 39 | + if (count < limit) { |
| 40 | + try { |
| 41 | + const relativePath = path.relative(workspacePath, line) |
| 42 | + results.push({ |
| 43 | + path: relativePath, |
| 44 | + type: "file", |
| 45 | + label: path.basename(relativePath), |
| 46 | + }) |
| 47 | + count++ |
| 48 | + } catch (error) { |
| 49 | + // Silently ignore errors processing individual paths |
| 50 | + } |
| 51 | + } else { |
| 52 | + rl.close() |
| 53 | + rgProcess.kill() |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + let errorOutput = "" |
| 58 | + rgProcess.stderr.on("data", (data) => { |
| 59 | + errorOutput += data.toString() |
| 60 | + }) |
| 61 | + |
| 62 | + rl.on("close", () => { |
| 63 | + if (errorOutput && results.length === 0) { |
| 64 | + reject(new Error(`ripgrep process error: ${errorOutput}`)) |
| 65 | + } else { |
| 66 | + resolve(results) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + rgProcess.on("error", (error) => { |
| 71 | + reject(new Error(`ripgrep process error: ${error.message}`)) |
| 72 | + }) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +export async function searchWorkspaceFiles( |
| 77 | + query: string, |
| 78 | + workspacePath: string, |
| 79 | + limit: number = 20, |
| 80 | +): Promise<{ path: string; type: "file" | "folder"; label?: string }[]> { |
| 81 | + try { |
| 82 | + const vscodeAppRoot = vscode.env.appRoot |
| 83 | + const rgPath = await getBinPath(vscodeAppRoot) |
| 84 | + |
| 85 | + if (!rgPath) { |
| 86 | + throw new Error("Could not find ripgrep binary") |
| 87 | + } |
| 88 | + |
| 89 | + const allFiles = await executeRipgrepForFiles(rgPath, workspacePath, 5000) |
| 90 | + |
| 91 | + if (!query.trim()) { |
| 92 | + return allFiles.slice(0, limit) |
| 93 | + } |
| 94 | + |
| 95 | + const searchItems = allFiles.map((file) => ({ |
| 96 | + original: file, |
| 97 | + searchStr: `${file.path} ${file.label || ""}`, |
| 98 | + })) |
| 99 | + |
| 100 | + const fzf = new Fzf(searchItems, { |
| 101 | + selector: (item) => item.searchStr, |
| 102 | + }) |
| 103 | + |
| 104 | + const results = fzf |
| 105 | + .find(query) |
| 106 | + .slice(0, limit) |
| 107 | + .map((result) => result.item.original) |
| 108 | + |
| 109 | + const resultsWithDirectoryCheck = await Promise.all( |
| 110 | + results.map(async (result) => { |
| 111 | + const fullPath = path.join(workspacePath, result.path) |
| 112 | + const isDirectory = fs.existsSync(fullPath) && fs.lstatSync(fullPath).isDirectory() |
| 113 | + |
| 114 | + return { |
| 115 | + ...result, |
| 116 | + type: isDirectory ? ("folder" as const) : ("file" as const), |
| 117 | + } |
| 118 | + }), |
| 119 | + ) |
| 120 | + |
| 121 | + return resultsWithDirectoryCheck |
| 122 | + } catch (error) { |
| 123 | + return [] |
| 124 | + } |
| 125 | +} |
0 commit comments