( cwd: string, directoryPath: string, regex: string, filePattern?: string, rooIgnoreController?: RooIgnoreController, )
| 161 | } |
| 162 | |
| 163 | export async function regexSearchFiles( |
| 164 | cwd: string, |
| 165 | directoryPath: string, |
| 166 | regex: string, |
| 167 | filePattern?: string, |
| 168 | rooIgnoreController?: RooIgnoreController, |
| 169 | ): Promise<string> { |
| 170 | const vscodeAppRoot = vscode.env.appRoot |
| 171 | const rgPath = await getBinPath(vscodeAppRoot) |
| 172 | |
| 173 | if (!rgPath) { |
| 174 | throw new Error("Could not find ripgrep binary") |
| 175 | } |
| 176 | |
| 177 | const args = ["--json", "-e", regex] |
| 178 | |
| 179 | // Only add --glob if a specific file pattern is provided |
| 180 | // Using --glob "*" overrides .gitignore behavior, so we omit it when no pattern is specified |
| 181 | if (filePattern) { |
| 182 | args.push("--glob", filePattern) |
| 183 | } |
| 184 | |
| 185 | args.push("--context", "1", "--no-messages", directoryPath) |
| 186 | |
| 187 | let output: string |
| 188 | try { |
| 189 | output = await execRipgrep(rgPath, args) |
| 190 | } catch (error) { |
| 191 | console.error("Error executing ripgrep:", error) |
| 192 | return "No results found" |
| 193 | } |
| 194 | |
| 195 | const results: SearchFileResult[] = [] |
| 196 | let currentFile: SearchFileResult | null = null |
| 197 | |
| 198 | output.split("\n").forEach((line) => { |
| 199 | if (line) { |
| 200 | try { |
| 201 | const parsed = JSON.parse(line) |
| 202 | if (parsed.type === "begin") { |
| 203 | currentFile = { |
| 204 | file: parsed.data.path.text.toString(), |
| 205 | searchResults: [], |
| 206 | } |
| 207 | } else if (parsed.type === "end") { |
| 208 | // Reset the current result when a new file is encountered |
| 209 | results.push(currentFile as SearchFileResult) |
| 210 | currentFile = null |
| 211 | } else if ((parsed.type === "match" || parsed.type === "context") && currentFile) { |
| 212 | const line = { |
| 213 | line: parsed.data.line_number, |
| 214 | text: truncateLine(parsed.data.lines.text), |
| 215 | isMatch: parsed.type === "match", |
| 216 | ...(parsed.type === "match" && { column: parsed.data.absolute_offset }), |
| 217 | } |
| 218 | |
| 219 | const lastResult = currentFile.searchResults[currentFile.searchResults.length - 1] |
| 220 | if (lastResult?.lines.length > 0) { |
no test coverage detected