diff --git a/src/tools/grep/cli.ts b/src/tools/grep/cli.ts index 6109139b5..c44bda377 100644 --- a/src/tools/grep/cli.ts +++ b/src/tools/grep/cli.ts @@ -95,7 +95,7 @@ function buildArgs(options: GrepOptions, backend: GrepBackend): string[] { return backend === "rg" ? buildRgArgs(options) : buildGrepArgs(options) } -function parseOutput(output: string): GrepMatch[] { +function parseOutput(output: string, filesOnly = false): GrepMatch[] { if (!output.trim()) return [] const matches: GrepMatch[] = [] @@ -104,6 +104,16 @@ function parseOutput(output: string): GrepMatch[] { for (const line of lines) { if (!line.trim()) continue + if (filesOnly) { + // --files-with-matches outputs only file paths, one per line + matches.push({ + file: line.trim(), + line: 0, + text: "", + }) + continue + } + const match = line.match(/^(.+?):(\d+):(.*)$/) if (match) { matches.push({ @@ -191,7 +201,7 @@ async function runRgInternal(options: GrepOptions): Promise { } } - const matches = parseOutput(outputToProcess) + const matches = parseOutput(outputToProcess, options.outputMode === "files_with_matches") const limited = options.headLimit && options.headLimit > 0 ? matches.slice(0, options.headLimit) : matches diff --git a/src/tools/grep/tools.ts b/src/tools/grep/tools.ts index 356eb53a4..e4d0e0e42 100644 --- a/src/tools/grep/tools.ts +++ b/src/tools/grep/tools.ts @@ -22,7 +22,7 @@ export function createGrepTools(ctx: PluginInput): Record