dsh-hashline-edit 0.5.2 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -19
- package/cordis.patch.yml +7 -2
- package/lib/client.js +8 -3
- package/lib/index.js +6777 -6511
- package/lib/types/dsh/index.d.ts +14 -1
- package/lib/types/dsh/rg/tools.d.ts +7 -5
- package/lib/types/dsh/shell-view.d.ts +25 -1
- package/lib/types/dsh/write-hook.d.ts +16 -2
- package/lib/types/src/file-content/preview.d.ts +1 -1
- package/lib/types/src/hashline/anchor-codebook.d.ts +15 -0
- package/lib/types/src/hashline/apply.d.ts +1 -1
- package/lib/types/src/hashline/gutter.d.ts +13 -0
- package/lib/types/src/hashline/hash-identity.d.ts +8 -5
- package/lib/types/src/hashline/row-mode.d.ts +17 -0
- package/lib/types/src/served-session/serve-render.d.ts +6 -0
- package/package.json +1 -1
- package/prompts/apply-patch-guidelines.md +2 -2
- package/prompts/codegraph-guidelines.md +2 -2
- package/prompts/diagnostics-guidelines.md +1 -1
- package/prompts/edit-guidelines.md +2 -2
- package/prompts/read-description.md +1 -1
- package/prompts/read-guidelines.md +3 -3
- package/prompts/rg-guidelines.md +2 -2
- package/prompts/write-guidelines.md +2 -1
- package/src/file-content/preview.ts +23 -13
- package/src/hashline/anchor-codebook.ts +19 -0
- package/src/hashline/apply.ts +10 -4
- package/src/hashline/gutter.ts +35 -0
- package/src/hashline/hash-identity.ts +61 -19
- package/src/hashline/parse.ts +3 -2
- package/src/hashline/resolve.ts +7 -5
- package/src/hashline/row-mode.ts +43 -0
- package/src/prompts.ts +3 -1
- package/src/served-session/serve-render.ts +18 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { HashSnapshotIO } from "../snapshot-store";
|
|
2
2
|
import { initHasher } from "./hasher.js";
|
|
3
3
|
import { HASH_LEN, ALPHA_RE, HASH_CLASS } from "./alphabet.js";
|
|
4
|
+
import { type RowGutter } from "./row-mode.js";
|
|
4
5
|
export { initHasher, HASH_LEN, ALPHA_RE, HASH_CLASS };
|
|
5
6
|
type HashPrior = {
|
|
6
7
|
content: string;
|
|
@@ -45,10 +46,10 @@ export interface AnchorWalk {
|
|
|
45
46
|
export declare const ANCHOR_LEN: number;
|
|
46
47
|
export declare const HASH_SEP = "\u2502";
|
|
47
48
|
export declare const HASH_SPACE: number;
|
|
48
|
-
export declare const USABLE_HASH_SPACE: number;
|
|
49
49
|
export declare const DIGIT_ANCHOR_RE: RegExp;
|
|
50
|
-
export declare
|
|
51
|
-
export declare
|
|
50
|
+
export declare let USABLE_HASH_SPACE: number;
|
|
51
|
+
export declare let MAX_HASH_LINES: number;
|
|
52
|
+
export declare let HASH_SPACE_EXHAUSTED_PAYLOAD: {
|
|
52
53
|
readonly limitKind: "hash-space";
|
|
53
54
|
readonly limit: number;
|
|
54
55
|
};
|
|
@@ -83,8 +84,10 @@ export declare const CANON_VERSION = 3;
|
|
|
83
84
|
* another. Width 4 stays generation 1 — the original persisted generation — so existing stores keep
|
|
84
85
|
* their anchors; width 3 gets generation 2, and any future width gets `100 + width`.
|
|
85
86
|
*/
|
|
86
|
-
export declare function anchorGenerationFor(width: number): number;
|
|
87
|
-
export declare
|
|
87
|
+
export declare function anchorGenerationFor(width: number, gutter?: RowGutter): number;
|
|
88
|
+
export declare let ANCHOR_GENERATION: number;
|
|
89
|
+
/** Recompute the active codebook, capacity, exhaustion payload and generation for the current gutter mode. */
|
|
90
|
+
export declare function configureAnchorSpace(): void;
|
|
88
91
|
export declare function canon(line: string): string;
|
|
89
92
|
/**
|
|
90
93
|
* SAFETY: the canon digest — `String(xxh32(canon(line)))`, the exact value `line_lineage.canon_hash`
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The active line-number gutter mode, shared by every renderer and by the anchor codebook.
|
|
3
|
+
*
|
|
4
|
+
* WHY module state + a setter rather than a load-time constant: the mode is a live plugin setting
|
|
5
|
+
* (the `nativeGutter` Volatile switch). The host re-mounts agents on a volatile update, and the plugin
|
|
6
|
+
* calls `applyConfigRowGutter()` then `configureAnchorSpace()` so the codebook, capacity and anchor
|
|
7
|
+
* generation all follow the switch. Changing it invalidates old anchors (new generation) — the caller
|
|
8
|
+
* opens a fresh session. `PI_BETTER_EDIT_ROW_GUTTER` pins the mode for tests/CI and wins over config.
|
|
9
|
+
*/
|
|
10
|
+
export type RowGutter = "arrow" | "native";
|
|
11
|
+
export declare const ROW_GUTTER_ENV_VAR = "PI_BETTER_EDIT_ROW_GUTTER";
|
|
12
|
+
/** Arrow is the default: it does not merge with the following anchor token. */
|
|
13
|
+
export declare const ROW_GUTTER_DEFAULT: RowGutter;
|
|
14
|
+
export declare function resolveRowGutter(env: Record<string, string | undefined>): RowGutter;
|
|
15
|
+
export declare function getRowGutter(): RowGutter;
|
|
16
|
+
/** Apply a config-driven mode unless the env var is set (tests/CI pin it). */
|
|
17
|
+
export declare function applyConfigRowGutter(mode: RowGutter): RowGutter;
|
|
@@ -13,6 +13,12 @@ export interface FileSnapshotContext {
|
|
|
13
13
|
*/
|
|
14
14
|
export declare function buildRangeServeRows(startLine: number, endLine: number, fileHashes: string[]): ServedRow[];
|
|
15
15
|
export declare function denseServeRows(hashes: readonly string[]): ServedRow[];
|
|
16
|
+
export declare function lineGutter(lineNumber: number): string;
|
|
17
|
+
/**
|
|
18
|
+
* One served row: `12: abc│content`. WHY: line numbers are display-only, never part of the anchor —
|
|
19
|
+
* the anchor stays the HASH immediately left of the separator.
|
|
20
|
+
*/
|
|
21
|
+
export declare function formatServedRow(lineNumber: number, hash: string, content: string): string;
|
|
16
22
|
export declare function fmtServedRows(rows: ServedRow[], fileLines: string[]): string;
|
|
17
23
|
export declare function paginationHint(nextOffset: number, more: number): string;
|
|
18
24
|
export declare function servedPositionsOf(served: (string | null)[], hash: string): number[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-hashline-edit",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.5",
|
|
4
4
|
"description": "Hash-anchored read/apply_patch/edit tools for DeepSeek Harness (dsh). Every line gets a unique 3- or 4-char hash (A-Za-z0-9, configurable) that stays stable across edits; apply_patch and the anchored range edit tool are the file-mutation entry points, each committing its whole batch as one transaction.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"anchors",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
- apply_patch: one call = one transaction; batch related changes and split only when an earlier RESULT decides the next.
|
|
2
2
|
- apply_patch: an identity hunk is `@@<anchor>` or `@@<from>-<to>` followed by `+` lines; an empty body deletes every row of the span. Minimal shape: `*** Begin Patch` / `*** Update File: <path>` / `@@<anchor>` / `+<new line>` / `*** End Patch`.
|
|
3
|
-
- apply_patch: an identity anchor is the
|
|
3
|
+
- apply_patch: an identity anchor is the `{{HASH_LEN}}` characters immediately left of `│` in a served row — the segment between the display-only line-number gutter (`12{{GUTTER}}`) and `│`, never the whole row, never the gutter or a line number; syntax is `@@` plus the anchor with no space between them.
|
|
4
4
|
- apply_patch: served beats text — when this session served the file, an identity hunk is REQUIRED, not preferred; a fragment hunk is limited to a file or region this session never served, and a text match never licenses one.
|
|
5
5
|
- apply_patch: anchors in hand mean no re-read — apply the identity hunk to the served rows instead of reading the file again; a rejection already serves the current rows back.
|
|
6
6
|
- apply_patch: change only what the task changes — an identity hunk carries `+` lines for the edited region and no unchanged rows, so a small edit stays a small patch.
|
|
7
7
|
- apply_patch: for a one-line or single-region change to a served file, one identity hunk is the whole call; do not switch to `write` because the file content is already in context.
|
|
8
8
|
- apply_patch: hunks are validated against the pre-call file in order (overlapping ranges reject the batch); a fragment hunk must match exactly once — `E_UNIQUE_MATCH` writes nothing — while a stale or unknown anchor is rejected; both serve the current rows back.
|
|
9
9
|
- apply_patch: a rejection writes nothing; retry from the served `HASH│content` rows without another `read`.
|
|
10
|
-
- apply_patch: success returns fresh anchors for the changed lines; a patch carries only the changed rows, so a new file or an every-line rewrite stays with `write`.
|
|
10
|
+
- apply_patch: success returns fresh anchors for the changed lines; a patch carries only the changed rows, so a new file or an every-line rewrite stays with `write`.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# codegraph guidelines
|
|
2
2
|
|
|
3
3
|
- Prefer `codegraph_explore` over grep/find for structural questions — architecture, a flow from X to Y, where a symbol lives, what would break — whenever the workspace has a `.codegraph` index: one call returns the relevant source plus the call paths a text search cannot follow.
|
|
4
|
-
- Source returned by `codegraph_explore`, `codegraph_node` and `codegraph_context` is served as `HASH│content` rows and is therefore already read: edit it with the anchors it shows instead of reading the file again.
|
|
4
|
+
- Source returned by `codegraph_explore`, `codegraph_node` and `codegraph_context` is served as `HASH│content` rows (each prefixed by a display-only line-number gutter) and is therefore already read: edit it with the anchors it shows instead of reading the file again.
|
|
5
5
|
- Use `codegraph_search`, `codegraph_callers`, `codegraph_callees`, `codegraph_impact` and `codegraph_implementations` for one-hop follow-ups; they return locations only, so call `codegraph_node` when a body is needed.
|
|
6
6
|
- A call that reports no `.codegraph` index means the workspace is not indexed: do not index it yourself, ask the user first (`codegraph init` in the repository root).
|
|
7
7
|
- The `.codegraph` index is derived state: when a call reports stale or empty results, `codegraph sync` in the project root refreshes it.
|
|
8
|
-
- Served rows stay editable for the rest of the session while the file is unchanged: anchor leases survive later edits (unchanged lines keep their anchors, changed lines come back with fresh ones), so chain edits without re-reading. An external write to the file, or a different session, invalidates them — a refusal returns the current rows as a fresh serve.
|
|
8
|
+
- Served rows stay editable for the rest of the session while the file is unchanged: anchor leases survive later edits (unchanged lines keep their anchors, changed lines come back with fresh ones), so chain edits without re-reading. An external write to the file, or a different session, invalidates them — a refusal returns the current rows as a fresh serve; the display-only line-number gutter is never part of an anchor.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# diagnostics guidelines
|
|
2
2
|
|
|
3
3
|
- Prefer `diagnostics` over re-reading a build log: one call turns a compiler's or linter's errors and warnings into anchored rows for the files it names.
|
|
4
|
-
- Every block is served as `HASH│content` rows and is therefore already read: fix the file with those anchors instead of reading it again.
|
|
4
|
+
- Every block is served as `HASH│content` rows (each prefixed by a display-only line-number gutter) and is therefore already read: fix the file with those anchors instead of reading it again.
|
|
5
5
|
- The row a diagnostic points at is named in the header above its block (`— path:line:col`); the rows stay byte-identical to a `read` page, so the marker never appears inside them.
|
|
6
6
|
- `tool` picks the toolchain: `cargo-check` and `cargo-clippy` for Rust, `tsc` for TypeScript. Run it from — or pass `cwd` as — the directory the toolchain expects: the cargo workspace root, or the one holding `tsconfig.json`.
|
|
7
7
|
- Narrow the report with `maxDiagnostics` and widen each snippet with `contextLines` (1-10) rather than reading a whole file around one error.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
- edit: one call targets one `file` in one transaction — batch all N changes to that file as N items in the same call, and use parallel `edit` calls for different target files.
|
|
2
2
|
- edit: every item carries exactly one payload — `text` (literal content; `""` deletes the range when `at` is `"in-place"`, and is a no-op for `"before"`/`"after"`), `text_ref` (with required non-null `mode`: `"copy"` re-inserts the served span's bytes, `"cut"` also deletes the source lines), or one of the insertion slots `after`/`before` (the lines to place immediately below/above `anchor_from`); never two, never none.
|
|
3
3
|
- edit: when `text_ref` uses `mode: "cut"` (same file or another served file via `text_ref.file`), the source lines are deleted automatically in the same correlated transaction — never send a second `edit` to delete the source span.
|
|
4
|
-
- edit: anchors are strictly the
|
|
4
|
+
- edit: anchors are strictly the `{{HASH_LEN}}` characters immediately left of `│` in served rows — the segment between the display-only line-number gutter (`12{{GUTTER}}`) and `│`, never the gutter, a line number, the full row, or null. `anchor_from` is always required and names the first line of the range; `anchor_to` is optional and names the last line — omit it to edit the single line `anchor_from` names (the range is single-line without it, which is mandatory when `at` is `"before"` or `"after"`), and a `text_ref` span always names both bounds.
|
|
5
5
|
- edit: change only what the task changes — one item per intended range, keeping unchanged boundary rows and `HASH│` prefixes out of `text`.
|
|
6
6
|
- edit: a stale or unknown anchor, or an overlapping range, rejects the whole call and writes nothing. A `text` payload echoing served rows in default top-level `mode: "general"` is refused (`E_SUSPICIOUS_TEXT`); prefer `text_ref` to duplicate or move served spans, or set top-level `mode: "literal"` when `text` must intentionally reproduce served lines.
|
|
7
7
|
- edit: anchors already in context (from `read`, an auto-read, or an intercepted shell view that output `HASH│content` rows) need no re-read; an anchor rejection serves the current `HASH│content` rows back, so repair directly from those rows instead of re-reading or guessing.
|
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
- edit: `indent: "auto"` (optional; default `"as-is"`) writes a flush-left or uniformly indented `text`/`after`/`before` payload at the depth of the `anchor_from` line — the tool strips the payload's shared leading-whitespace prefix and keeps tabs, spaces and blank lines as written. `text_ref` refuses `indent` (copied bytes are verbatim). The result's notes report the lines it moved.
|
|
10
10
|
- edit: `expand: "item"` (optional) replaces the whole syntax item containing `anchor_from` — its leading doc comment/attributes and its closing brace included — so name only `anchor_from` and send a `text` payload; `anchor_to`, `text_ref`, `after`/`before` and a non-in-place `at` are refused. Prefer it over a hand-written multi-line range when the intent is "replace this function/class/method": you do not reproduce the doc comment or the closing brace, and the diff shows the whole replaced node. It needs a shipped code grammar (rust, typescript, tsx, vue, python, go, java); a non-code file, an unavailable grammar, or an anchor outside any item refuses (`E_EXPAND_UNAVAILABLE`) and writes nothing. One expanded item per call.
|
|
11
11
|
- edit: a successful call that turns a clean file into one with parse errors appends a `[W_SYNTAX]` warning naming the damaged line; the write already landed, so re-read around that line and correct the range instead of repeating the call.
|
|
12
|
-
- edit: `write` is the whole-file path — use `write` to create a new file or replace every line, never `edit`.
|
|
12
|
+
- edit: `write` is the whole-file path — use `write` to create a new file or replace every line, never `edit`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Read a UTF-8 text file as hash-anchored
|
|
1
|
+
Read a UTF-8 text file as hash-anchored served rows. Each served row is a display-only line-number gutter, then the anchor, the `│` separator, and the content — `12{{GUTTER}}HASH│content` by default (arrow, U+2192) or `12: HASH│content` in the opt-in native gutter. The anchor is the `{{HASH_LEN}}` characters immediately left of `│` (the segment between the number gutter and `│`); the line-number gutter is display-only, never part of the anchor, never addressable. Served mode is the default; `mode: "verbatim"` returns plain text as `12{{GUTTER}}content` (arrow) or `12: content` (native) with no anchors and no line-budget cap (and does not register anchors for `edit`); `mode: "folded"` returns the file's structural skeleton — a single-line item as `12{{GUTTER}}HASH│opening`, a range as `12..30{{GUTTER}}HASH1..HASH2│opening`, a gap run as `12..30{{GUTTER}}HASH1..HASH2│… (N lines) …`, one row per syntax item — and always covers the whole file, so it refuses `offset`/`limit`/`windows`. Supports `offset`/`limit` paging and `windows` to batch up to 16 disjoint ranges (`offset`/`limit`, or `around_anchor` + `radius` for an anchor already served in this session) in one call. Image, binary, and directory paths are refused (`E_UNSUPPORTED_FILE`).
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
- read: call it only for spans not yet served as `HASH│content` rows in this session; an intercepted shell view (`cat`, `rg`, `Get-Content`, `Select-String`, etc.) or write-hook auto-read that output `HASH│content` rows already served the file for `edit`, whereas raw unanchored stdout is not served.
|
|
1
|
+
- read: call it only for spans not yet served as `HASH│content` rows (each prefixed by a display-only line-number gutter, `12{{GUTTER}}`) in this session; an intercepted shell view (`cat`, `rg`, `Get-Content`, `Select-String`, etc.) or write-hook auto-read that output `HASH│content` rows already served the file for `edit`, whereas raw unanchored stdout is not served.
|
|
2
2
|
- read: batch up to 16 disjoint ranges of a file in ONE call with `windows` — each using `offset`/`limit`, or `around_anchor` plus `radius` around an anchor already served in this session (unserved anchors are omitted with a warning). Read multiple files with parallel `read` calls, one file per call.
|
|
3
3
|
- read: keep requested windows compact (61 lines or fewer) — the tool automatically widens compact windows to their enclosing syntax block (capped at 80 lines for code/Markdown and 40 for plain text, noted `[widened …]`), so do not manually pad `offset`/`limit` margins. Single-page output is also bounded by a byte budget.
|
|
4
4
|
- read: omit `mode` for any file you might later edit (code, configs, READMEs) so rows carry anchors; pass `mode: "verbatim"` only for strictly read-only reference files (e.g., skill files), as `verbatim` is uncapped and records no anchors for `edit`.
|
|
5
5
|
- read: `read` accepts UTF-8 text files only — images, binary files, and directories are refused (`E_UNSUPPORTED_FILE`).
|
|
6
|
-
- read: pass `mode: "folded"` to survey a large source file as its structural skeleton — one row per syntax item carrying that item's first and last anchors (`
|
|
7
|
-
- read: a folded range row is a whole-item target — copy its two anchors into `anchor_from` and `anchor_to` to replace or delete the item. A `… (N lines) …` row is a run the page did not show: its first..last anchors are served but its lines are not, so read that range with `offset`/`limit` before editing inside it.
|
|
6
|
+
- read: pass `mode: "folded"` to survey a large source file as its structural skeleton — one row per syntax item carrying that item's first and last anchors, each row prefixed by a display-only line-number gutter (`12{{GUTTER}}HASH│opening line` for a single-line item, `12..30{{GUTTER}}HASH1..HASH2│opening line` for a range), with `foldDepth` 1-3 (default 1) revealing nested members. A folded read always covers the whole file and refuses `offset`/`limit`/`windows`.
|
|
7
|
+
- read: a folded range row is a whole-item target — copy its two anchors into `anchor_from` and `anchor_to` to replace or delete the item (the anchors are the `{{HASH_LEN}}` characters immediately left of `│`; the `12..30{{GUTTER}}` gutter is display-only). A `… (N lines) …` row is a run the page did not show: its first..last anchors are served but its lines are not, so read that range with `offset`/`limit` before editing inside it.
|
package/prompts/rg-guidelines.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# rg guidelines
|
|
2
2
|
|
|
3
3
|
- `rg` is the file-content search tool: call it instead of a shell `grep`/`rg` command, and instead of the built-in `grep`, which this profile hides while `rg` is on.
|
|
4
|
-
- Prefer one `rg` call over a search-then-read loop: with `read` on, every matched line comes back as `HASH│content` and
|
|
4
|
+
- Prefer one `rg` call over a search-then-read loop: with `read` on, every matched line comes back served and leased — as `HASH│content` rows (each prefixed by a display-only line-number gutter, `12{{GUTTER}}`) on a direct call's page, and as each match's `hash` (the anchor of that same served row) inside `run_code` — so `edit`/`apply_patch` can target those anchors with no separate `read`.
|
|
5
5
|
- The default is ripgrep's smart case (insensitive only when the pattern has no uppercase); pass `ignoreCase` to force `-i`, `fixedStrings` for literal text, `wordMatch` for whole words.
|
|
6
6
|
- `hidden` adds dotfiles and directories, `noIgnore` drops `.gitignore`/`.ignore` filtering: use them to reach a file the default search skipped, not by default.
|
|
7
7
|
- Narrow instead of enlarging: `include` takes ONE positive glob (`*.ts`, `*.{js,jsx}`) and `path` scopes the search to a file or directory. Past 250 matches the result is reported as a count and passes through unserved.
|
|
8
|
-
- A result that reports no `rg` on PATH, a pattern ripgrep rejected or the time budget is guidance, not a failure: fix `rgPath`, simplify the pattern, or narrow the path rather than repeating the call.
|
|
8
|
+
- A result that reports no `rg` on PATH, a pattern ripgrep rejected or the time budget is guidance, not a failure: fix `rgPath`, simplify the pattern, or narrow the path rather than repeating the call.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
- write: read the file before overwriting it; `write` replaces the whole file, so use it for a new file or a rewrite where every line changes.
|
|
2
2
|
- write: a partial change is not a whole-file rewrite even when the whole file is already in context — use `edit` on the served rows instead.
|
|
3
|
-
- write: never paste served `HASH
|
|
3
|
+
- write: never paste served rows — a display-only line-number gutter (`12{{GUTTER}}`) then `HASH│content` — into the content; a line that reproduces a served row is refused as a served hash echo.
|
|
4
|
+
- write: the result is the native one-line success — no anchors come back with it (the opt-in `writeAutoRead` switch restores an appended anchored page). `read` the file when a follow-up `edit` needs anchors.
|
|
@@ -17,7 +17,8 @@ import {
|
|
|
17
17
|
import { fmtRegion } from "../hashline/apply.js";
|
|
18
18
|
import { loadHashStore } from "../hash-store.js";
|
|
19
19
|
import { snapshotIOFor } from "../snapshot-store";
|
|
20
|
-
import type
|
|
20
|
+
import { type ServedRow, formatServedRow } from "../served-session/serve-render.js";
|
|
21
|
+
import { lineGutter } from "../hashline/gutter.js";
|
|
21
22
|
import { formatSectionHeader } from "./served-header.js";
|
|
22
23
|
import { mergeRanges, visibleLineTotal, walkLines, type LineRange } from "./line-walker.js";
|
|
23
24
|
|
|
@@ -80,11 +81,16 @@ function normWindows(windows: ReadWindow[] | undefined): ReadWindow[] | undefine
|
|
|
80
81
|
|
|
81
82
|
// WHY: verbatim renders the identical admitted rows with no anchor prefix; the row set and budgets
|
|
82
83
|
// WHY: are computed once, so only this formatter differs between the two modes.
|
|
83
|
-
function fmtRows(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
function fmtRows(
|
|
85
|
+
hashes: readonly string[],
|
|
86
|
+
lines: readonly string[],
|
|
87
|
+
verbatim: boolean,
|
|
88
|
+
startLine: number,
|
|
89
|
+
): string {
|
|
90
|
+
if (!verbatim) return fmtRegion(startLine, hashes, lines);
|
|
91
|
+
if (lines.length === 0) return "";
|
|
92
|
+
// WHY: the `N: ` gutter already distinguishes a lone empty line from an empty result.
|
|
93
|
+
return lines.map((line, index) => `${lineGutter(startLine + index)}${line}`).join("\n");
|
|
88
94
|
}
|
|
89
95
|
|
|
90
96
|
function emptyFilePreview(
|
|
@@ -102,7 +108,7 @@ function emptyFilePreview(
|
|
|
102
108
|
throw new Error("empty file preview is missing its walk anchor");
|
|
103
109
|
}
|
|
104
110
|
return {
|
|
105
|
-
text: `${emptyLineHash}
|
|
111
|
+
text: `${formatServedRow(1, emptyLineHash, "")}\n[File is empty. Use edit to insert content.]`,
|
|
106
112
|
served: [{ position: 0, hash: emptyLineHash }],
|
|
107
113
|
};
|
|
108
114
|
}
|
|
@@ -163,7 +169,7 @@ function buildOversizedPreview(params: {
|
|
|
163
169
|
if (hash === undefined || line === undefined) {
|
|
164
170
|
throw new Error(`preview row ${index} has no selected line`);
|
|
165
171
|
}
|
|
166
|
-
return fmtRows([hash], [line], verbatim);
|
|
172
|
+
return fmtRows([hash], [line], verbatim, startLine + index);
|
|
167
173
|
});
|
|
168
174
|
const skippedTruncation = truncateHead(rows.join("\n"), { maxBytes, maxLines: maxTruncLines });
|
|
169
175
|
const shownRowCount =
|
|
@@ -295,7 +301,7 @@ function buildWindowSection(params: {
|
|
|
295
301
|
});
|
|
296
302
|
}
|
|
297
303
|
const normal = buildNormalPreview({
|
|
298
|
-
formatted: fmtRows(selectedHashes, selected, verbatim),
|
|
304
|
+
formatted: fmtRows(selectedHashes, selected, verbatim, startLine),
|
|
299
305
|
startLine,
|
|
300
306
|
endIdx,
|
|
301
307
|
totalLines,
|
|
@@ -431,7 +437,7 @@ function buildWindowedPreview(params: {
|
|
|
431
437
|
);
|
|
432
438
|
// WHY: `metrics.truncated` must be honest when the shared budget cut a window away, so the
|
|
433
439
|
// WHY: same function that reports truncation elsewhere derives it from the budget actually spent.
|
|
434
|
-
const skipped = truncateHead(fmtRows(selectedHashes, selected, verbatim), {
|
|
440
|
+
const skipped = truncateHead(fmtRows(selectedHashes, selected, verbatim, window.offset), {
|
|
435
441
|
maxBytes: Math.max(0, remainingBytes),
|
|
436
442
|
maxLines: Math.max(0, remainingLines),
|
|
437
443
|
});
|
|
@@ -441,7 +447,9 @@ function buildWindowedPreview(params: {
|
|
|
441
447
|
const rowSizes = selected.map((line, index) => ({
|
|
442
448
|
lineNumber: window.offset + index,
|
|
443
449
|
bytes: Buffer.byteLength(
|
|
444
|
-
verbatim
|
|
450
|
+
verbatim
|
|
451
|
+
? `${lineGutter(window.offset + index)}${line}`
|
|
452
|
+
: formatServedRow(window.offset + index, selectedHashes[index] ?? "", line),
|
|
445
453
|
"utf-8",
|
|
446
454
|
),
|
|
447
455
|
}));
|
|
@@ -738,12 +746,14 @@ export async function fmtReadPreview(
|
|
|
738
746
|
// WHY: a ranged source is already page-aligned (its array starts at `startLine`), while a full anchor
|
|
739
747
|
// WHY: array is indexed by absolute line — slice only the latter (ADR-0043).
|
|
740
748
|
const selectedHashes = ranged !== undefined ? allHashes : allHashes.slice(startLine - 1, endIdx);
|
|
741
|
-
const formatted = fmtRows(selectedHashes, selected, verbatim);
|
|
749
|
+
const formatted = fmtRows(selectedHashes, selected, verbatim, startLine);
|
|
742
750
|
const maxBytes = maxLineBytes;
|
|
743
751
|
const rowSizes = selected.map((line, index) => ({
|
|
744
752
|
lineNumber: startLine + index,
|
|
745
753
|
bytes: Buffer.byteLength(
|
|
746
|
-
verbatim
|
|
754
|
+
verbatim
|
|
755
|
+
? `${lineGutter(startLine + index)}${line}`
|
|
756
|
+
: formatServedRow(startLine + index, selectedHashes[index] ?? "", line),
|
|
747
757
|
"utf-8",
|
|
748
758
|
),
|
|
749
759
|
}));
|