* 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 } = {},
)
| 8401 | * through validatePathWithinRoot (#527). |
| 8402 | */ |
| 8403 | private async handleFileView( |
| 8404 | cg: CodeGraph, |
| 8405 | fileArg: string, |
| 8406 | opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {}, |
| 8407 | ): Promise<ToolResult> { |
| 8408 | const normalize = (p: string) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, ''); |
| 8409 | const allFiles = cg.getFiles(); |
| 8410 | if (allFiles.length === 0) return this.textResult('No files indexed. Run `codegraph index` first.'); |
| 8411 | |
| 8412 | // Resolve ONE spelling of the path against the index: exact, then |
| 8413 | // suffix-of-path, then substring — narrowing to a single file or handing |
| 8414 | // back the ambiguous set. |
| 8415 | const resolveOne = (want: string): { file?: (typeof allFiles)[number]; candidates: typeof allFiles } => { |
| 8416 | const wantLower = normalize(want).toLowerCase(); |
| 8417 | let file = allFiles.find((f) => f.path.toLowerCase() === wantLower); |
| 8418 | let found: typeof allFiles = []; |
| 8419 | if (!file) { |
| 8420 | found = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower)); |
| 8421 | if (found.length === 1) file = found[0]; |
| 8422 | } |
| 8423 | if (!file && found.length === 0) { |
| 8424 | found = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower)); |
| 8425 | if (found.length === 1) file = found[0]; |
| 8426 | } |
| 8427 | return { file, candidates: found }; |
| 8428 | }; |
| 8429 | |
| 8430 | // Agents and humans paste file references WITH a line suffix — `a.ts:12`, |
| 8431 | // `a.ts:12-40`, `a.ts#L88`, `a.ts#L12-L40`. explore already strips exactly |
| 8432 | // these shapes (src/search/query-paths.ts); file-view used to treat them as |
| 8433 | // part of the filename and report an indexed file as missing (#1831). |
| 8434 | // The literal spelling is tried FIRST, so a file genuinely named `foo:12` |
| 8435 | // still resolves to itself; only when that finds nothing is the suffix |
| 8436 | // stripped, and then the range becomes the read window. |
| 8437 | const LINE_SUFFIX = /(?::(\d+)(?:-(\d+))?|#L(\d+)(?:-L?(\d+))?)$/; |
| 8438 | let { file: resolved, candidates } = resolveOne(fileArg); |
| 8439 | let shownArg = fileArg; |
| 8440 | if (!resolved) { |
| 8441 | const m = LINE_SUFFIX.exec(normalize(fileArg)); |
| 8442 | const stripped = m ? fileArg.slice(0, fileArg.length - m[0].length) : ''; |
| 8443 | const retry = stripped ? resolveOne(stripped) : undefined; |
| 8444 | if (m && retry && (retry.file || retry.candidates.length > 0)) { |
| 8445 | resolved = retry.file; |
| 8446 | candidates = retry.candidates; |
| 8447 | shownArg = stripped; |
| 8448 | const startLine = Number(m[1] ?? m[3]); |
| 8449 | const endRaw = m[2] ?? m[4]; |
| 8450 | const endLine = endRaw === undefined ? undefined : Number(endRaw); |
| 8451 | if (Number.isFinite(startLine) && startLine > 0) { |
| 8452 | // An explicit offset/limit from the caller always wins over the |
| 8453 | // suffix. `:12` alone is a "start here" pointer (Read given only an |
| 8454 | // offset); `:12-40` pins both ends. |
| 8455 | opts = { |
| 8456 | ...opts, |
| 8457 | offset: opts.offset ?? startLine, |
| 8458 | limit: opts.limit ?? (endLine !== undefined && endLine >= startLine ? endLine - startLine + 1 : undefined), |
| 8459 | }; |
| 8460 | } |
no test coverage detected