diff --git a/app/src/lib/xtermLineJoin.test.ts b/app/src/lib/xtermLineJoin.test.ts index b8cf530..b777b3b 100644 --- a/app/src/lib/xtermLineJoin.test.ts +++ b/app/src/lib/xtermLineJoin.test.ts @@ -38,6 +38,50 @@ describe("joinWrappedRows", () => { }); }); +describe("joinWrappedRows on a long chain", () => { + const cols = 40; + /** 1000 hard-wrapped rows: row 0 at column 0, every later row indented 2. */ + const chainRows = (): Array<[string, boolean]> => { + const rows: Array<[string, boolean]> = [["/" + "a".repeat(cols - 1), false]]; + for (let i = 1; i < 1000; i++) rows.push([" " + String(i).padStart(4, "0") + "b".repeat(cols - 6), false]); + return rows; + }; + const counting = (rows: Array<[string, boolean]>) => { + const b = buffer(rows); + let reads = 0; + return { + reads: () => reads, + source: { getLine: (y: number) => (reads++, b.getLine(y)) } as RowSource, + }; + }; + + it("always includes the hovered row, deep in a chain longer than the budget", () => { + const rows = chainRows(); + for (const row of [45, 300, 999]) { + const j = joinWrappedRows(buffer(rows), row, { cols }); + expect(j.firstRow).toBeLessThanOrEqual(row); + expect(j.firstRow + j.rowStarts.length).toBeGreaterThan(row); + expect(j.text).toContain(String(row).padStart(4, "0")); + expect(j.text.length).toBeLessThanOrEqual(MAX_JOINED_LENGTH); + } + }); + + it("drops a continuation's indent when the window starts mid-chain", () => { + const j = joinWrappedRows(buffer(chainRows()), 500, { cols }); + expect(j.firstRow).toBeGreaterThan(0); + expect(j.rowIndents[0]).toBe(2); + expect(j.text.startsWith(" ")).toBe(false); + }); + + it("reads a bounded number of rows, not the whole chain above", () => { + const c = counting(chainRows()); + joinWrappedRows(c.source, 999, { cols }); + // The window holds at most MAX_JOINED_LENGTH / (cols - 2) rows; each is + // read a few times by the walk. Reading all 1000 rows would be far more. + expect(c.reads()).toBeLessThan(1000); + }); +}); + /** * Claude Code wraps its own output: a word longer than the line is broken * mid-word and the rest is drawn on the next row with cursor moves, so xterm diff --git a/app/src/lib/xtermLineJoin.ts b/app/src/lib/xtermLineJoin.ts index 0e8ac1a..d1de4d4 100644 --- a/app/src/lib/xtermLineJoin.ts +++ b/app/src/lib/xtermLineJoin.ts @@ -97,10 +97,24 @@ function continuationIndent(buffer: RowSource, y: number, cols: number | undefin return hardWrapIndent(prev.translateToString(true), line.translateToString(true), cols); } +/** + * Joins `row` with the rows it wraps from and into. A chain longer than + * {@link MAX_JOINED_LENGTH} is windowed around `row`: the walk up stops after + * half the budget, the walk down after the whole of it, and `row` itself is + * always included — so a hover deep in a huge wrapped blob reads a bounded + * number of rows and still finds the text under the pointer. + */ export function joinWrappedRows(buffer: RowSource, row: number, options: JoinOptions = {}): JoinedLine { const { cols } = options; let top = row; - while (top > 0 && continuationIndent(buffer, top, cols) !== null) top--; + let above = 0; + while (top > 0 && continuationIndent(buffer, top, cols) !== null) { + // Every row counts at least 1, so a run of blank soft-wrapped rows is bounded too. + const len = Math.max(1, buffer.getLine(top - 1)?.translateToString(true).length ?? 0); + if (above + len > MAX_JOINED_LENGTH / 2) break; + above += len; + top--; + } const parts: string[] = []; const rowIndents: number[] = []; @@ -110,13 +124,15 @@ export function joinWrappedRows(buffer: RowSource, row: number, options: JoinOpt const line = buffer.getLine(y); if (!line) break; let indent = 0; + const c = continuationIndent(buffer, y, cols); if (y !== top) { - const c = continuationIndent(buffer, y, cols); if (c === null) break; indent = c; + } else if (c !== null) { + indent = c; // the window starts mid-chain: drop this row's indent as well } const text = line.translateToString(true).slice(indent); - if (length + text.length > MAX_JOINED_LENGTH && parts.length > 0) break; + if (length + text.length > MAX_JOINED_LENGTH && y > row) break; parts.push(text); rowIndents.push(indent); length += text.length;