Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

grepOutput.ts

82 lines3.2 KB

1export interface GrepOutputResult {
2 readonly fileName: string;
3 readonly line?: number;
4 readonly preview?: string;
5}
6​
7// Search results ride the timeline wire, so matched text shares one budget; later matches keep
8// their file and line without the text.
9const PREVIEW_BUDGET_CHARS = 8_000;
10const NOTE_MAX_CHARS = 500;
11​
12const SUMMARY_LINE = /^(?:Found \d+ (?:match|file)|No (?:files|matches) found)/i;
13const NOTE_LINE = /^\(.*\)$/;
14const FILE_HEADING = /^File: (.+)$/;
15// OpenCode and Gemini put a space after the colon; ripgrep's `--heading` lines start right after it.
16const NUMBERED_LINE = /^\s*(?:(?:Line |L)(\d+): ?|(\d+):)(.*)$/;
17const CONTEXT_LINE = /^\s*\d+-/;
18const PATH_LINE_TEXT = /^(\S.*?):(\d+):(.*)$/;
19const COLON_HEADING = /^(\S.*):$/;
20​
21/**
22 * Grep tool text as one result per matched line, from ripgrep's `path:line:text` and `--heading`
23 * forms, OpenCode's `path:` / `Line N: text`, and Gemini's `File: path` / `LN: text`. Returns
24 * undefined unless every line fits, so listings, errors, and prose stay as the provider wrote them.
25 * Parenthesized notes such as truncation warnings stay, as one line-less result on `root`.
26 */
27export function parseGrepOutput(
28 output: string,
29 root: string,
30): ReadonlyArray<GrepOutputResult> | undefined {
31 const matches: Array<{ fileName: string; line: number; text: string }> = [];
32 const notes: string[] = [];
33 let file: string | undefined;
34 for (const line of output.split(/\r?\n/)) {
35 const trimmed = line.trim();
36 if (!trimmed || trimmed === "--" || trimmed === "---" || SUMMARY_LINE.test(trimmed)) continue;
37 const heading = FILE_HEADING.exec(trimmed);
38 if (heading) {
39 file = heading[1];
40 continue;
41 }
42 const numbered = file === undefined ? null : NUMBERED_LINE.exec(line);
43 if (file !== undefined && numbered) {
44 const lineNumber = numbered[1] ?? numbered[2];
45 matches.push({ fileName: file, line: Number(lineNumber), text: numbered[3] ?? "" });
46 continue;
47 }
48 // ripgrep context lines (`N-text`) can't be told apart from a file named like one.
49 if (file !== undefined && CONTEXT_LINE.test(line)) return undefined;
50 const inline = PATH_LINE_TEXT.exec(line);
51 if (inline) {
52 matches.push({ fileName: inline[1] ?? "", line: Number(inline[2]), text: inline[3] ?? "" });
53 continue;
54 }
55 const colonHeading = COLON_HEADING.exec(line.trimEnd());
56 if (colonHeading) {
57 file = colonHeading[1];
58 continue;
59 }
60 if (NOTE_LINE.test(trimmed)) {
61 notes.push(trimmed);
62 continue;
63 }
64 // ripgrep --heading prints the bare path above its numbered lines.
65 if (!/\s/.test(line.trimEnd())) {
66 file = line.trimEnd();
67 continue;
68 }
69 return undefined;
70 }
71 if (matches.length === 0 || matches.some((match) => match.line < 1 || !match.fileName.trim())) {
72 return undefined;
73 }
74 const note = notes.join("\n").slice(0, NOTE_MAX_CHARS);
75 let budget = PREVIEW_BUDGET_CHARS - note.length;
76 const results: GrepOutputResult[] = matches.map((match) => {
77 const fits = match.text.length <= budget;
78 if (fits) budget -= match.text.length;
79 return { fileName: match.fileName, line: match.line, ...(fits ? { preview: match.text } : {}) };
80 });
81 return note ? [...results, { fileName: root, preview: note }] : results;