* 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 } = {},
)
| 8254 | * through validatePathWithinRoot (#527). |
| 8255 | */ |
| 8256 | private async handleFileView( |
| 8257 | cg: CodeGraph, |
| 8258 | fileArg: string, |
| 8259 | opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {}, |
| 8260 | ): Promise<ToolResult> { |
| 8261 | const normalize = (p: string) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, ''); |
| 8262 | const allFiles = cg.getFiles(); |
| 8263 | if (allFiles.length === 0) return this.textResult('No files indexed. Run `codegraph index` first.'); |
| 8264 | |
| 8265 | // Resolve ONE spelling of the path against the index: exact, then |
| 8266 | // suffix-of-path, then substring — narrowing to a single file or handing |
| 8267 | // back the ambiguous set. |
| 8268 | const resolveOne = (want: string): { file?: (typeof allFiles)[number]; candidates: typeof allFiles } => { |
| 8269 | const wantLower = normalize(want).toLowerCase(); |
| 8270 | let file = allFiles.find((f) => f.path.toLowerCase() === wantLower); |
| 8271 | let found: typeof allFiles = []; |
| 8272 | if (!file) { |
| 8273 | found = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower)); |
| 8274 | if (found.length === 1) file = found[0]; |
| 8275 | } |
| 8276 | if (!file && found.length === 0) { |
| 8277 | found = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower)); |
| 8278 | if (found.length === 1) file = found[0]; |
| 8279 | } |
| 8280 | return { file, candidates: found }; |
| 8281 | }; |
| 8282 | |
| 8283 | // Agents and humans paste file references WITH a line suffix — `a.ts:12`, |
| 8284 | // `a.ts:12-40`, `a.ts#L88`, `a.ts#L12-L40`. explore already strips exactly |
| 8285 | // these shapes (src/search/query-paths.ts); file-view used to treat them as |
| 8286 | // part of the filename and report an indexed file as missing (#1831). |
| 8287 | // The literal spelling is tried FIRST, so a file genuinely named `foo:12` |
| 8288 | // still resolves to itself; only when that finds nothing is the suffix |
| 8289 | // stripped, and then the range becomes the read window. |
| 8290 | const LINE_SUFFIX = /(?::(\d+)(?:-(\d+))?|#L(\d+)(?:-L?(\d+))?)$/; |
| 8291 | let { file: resolved, candidates } = resolveOne(fileArg); |
| 8292 | let shownArg = fileArg; |
| 8293 | if (!resolved) { |
| 8294 | const m = LINE_SUFFIX.exec(normalize(fileArg)); |
| 8295 | const stripped = m ? fileArg.slice(0, fileArg.length - m[0].length) : ''; |
| 8296 | const retry = stripped ? resolveOne(stripped) : undefined; |
| 8297 | if (m && retry && (retry.file || retry.candidates.length > 0)) { |
| 8298 | resolved = retry.file; |
| 8299 | candidates = retry.candidates; |
| 8300 | shownArg = stripped; |
| 8301 | const startLine = Number(m[1] ?? m[3]); |
| 8302 | const endRaw = m[2] ?? m[4]; |
| 8303 | const endLine = endRaw === undefined ? undefined : Number(endRaw); |
| 8304 | if (Number.isFinite(startLine) && startLine > 0) { |
| 8305 | // An explicit offset/limit from the caller always wins over the |
| 8306 | // suffix. `:12` alone is a "start here" pointer (Read given only an |
| 8307 | // offset); `:12-40` pins both ends. |
| 8308 | opts = { |
| 8309 | ...opts, |
| 8310 | offset: opts.offset ?? startLine, |
| 8311 | limit: opts.limit ?? (endLine !== undefined && endLine >= startLine ? endLine - startLine + 1 : undefined), |
| 8312 | }; |
| 8313 | } |
no test coverage detected