* FILE READ MODE: resolve `fileArg` (path or basename) to an indexed file and * read it like the Read tool — its current on-disk source with line numbers, * narrowable with `offset`/`limit` exactly as Read's are — preceded by a * one-line blast-radius header (which files depend on it). `sym
(
cg: CodeGraph,
fileArg: string,
opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {},
)
| 8709 | * through validatePathWithinRoot (#527). |
| 8710 | */ |
| 8711 | private async handleFileView( |
| 8712 | cg: CodeGraph, |
| 8713 | fileArg: string, |
| 8714 | opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {}, |
| 8715 | ): Promise<ToolResult> { |
| 8716 | const normalize = (p: string) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, ''); |
| 8717 | const allFiles = cg.getFiles(); |
| 8718 | if (allFiles.length === 0) return this.textResult('No files indexed. Run `codegraph index` first.'); |
| 8719 | |
| 8720 | // Resolve ONE spelling of the path against the index: exact, then |
| 8721 | // suffix-of-path, then substring — narrowing to a single file or handing |
| 8722 | // back the ambiguous set. |
| 8723 | const resolveOne = (want: string): { file?: (typeof allFiles)[number]; candidates: typeof allFiles } => { |
| 8724 | const wantLower = normalize(want).toLowerCase(); |
| 8725 | let file = allFiles.find((f) => f.path.toLowerCase() === wantLower); |
| 8726 | let found: typeof allFiles = []; |
| 8727 | if (!file) { |
| 8728 | found = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower)); |
| 8729 | if (found.length === 1) file = found[0]; |
| 8730 | } |
| 8731 | if (!file && found.length === 0) { |
| 8732 | found = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower)); |
| 8733 | if (found.length === 1) file = found[0]; |
| 8734 | } |
| 8735 | return { file, candidates: found }; |
| 8736 | }; |
| 8737 | |
| 8738 | // Agents and humans paste file references WITH a line suffix — `a.ts:12`, |
| 8739 | // `a.ts:12-40`, `a.ts#L88`, `a.ts#L12-L40`. explore already strips exactly |
| 8740 | // these shapes (src/search/query-paths.ts); file-view used to treat them as |
| 8741 | // part of the filename and report an indexed file as missing (#1831). |
| 8742 | // The literal spelling is tried FIRST, so a file genuinely named `foo:12` |
| 8743 | // still resolves to itself; only when that finds nothing is the suffix |
| 8744 | // stripped, and then the range becomes the read window. |
| 8745 | const LINE_SUFFIX = /(?::(\d+)(?:-(\d+))?|#L(\d+)(?:-L?(\d+))?)$/; |
| 8746 | let { file: resolved, candidates } = resolveOne(fileArg); |
| 8747 | let shownArg = fileArg; |
| 8748 | if (!resolved) { |
| 8749 | const m = LINE_SUFFIX.exec(normalize(fileArg)); |
| 8750 | const stripped = m ? fileArg.slice(0, fileArg.length - m[0].length) : ''; |
| 8751 | const retry = stripped ? resolveOne(stripped) : undefined; |
| 8752 | if (m && retry && (retry.file || retry.candidates.length > 0)) { |
| 8753 | resolved = retry.file; |
| 8754 | candidates = retry.candidates; |
| 8755 | shownArg = stripped; |
| 8756 | const startLine = Number(m[1] ?? m[3]); |
| 8757 | const endRaw = m[2] ?? m[4]; |
| 8758 | const endLine = endRaw === undefined ? undefined : Number(endRaw); |
| 8759 | if (Number.isFinite(startLine) && startLine > 0) { |
| 8760 | // An explicit offset/limit from the caller always wins over the |
| 8761 | // suffix. `:12` alone is a "start here" pointer (Read given only an |
| 8762 | // offset); `:12-40` pins both ends. |
| 8763 | opts = { |
| 8764 | ...opts, |
| 8765 | offset: opts.offset ?? startLine, |
| 8766 | limit: opts.limit ?? (endLine !== undefined && endLine >= startLine ? endLine - startLine + 1 : undefined), |
| 8767 | }; |
| 8768 | } |
no test coverage detected