fix(links): bound the upward wrap walk and keep the hovered row in the window

With hard-wrap bridging a long chain of continuation rows made the walk up
to the chain's start unbounded while the walk down stopped at the
2048-char budget, so a hover deep in the chain could get back text that
did not contain its own row, after reading the whole chain. Stop the walk
up after half the budget, always include the hovered row, and drop the
indent of a mid-chain first row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-02 10:42:48 -08:00
1 parent c992a7959e
commit f6f89ab6f0
2 files changed
+63 -3

No files matched your search

+44
View File
@@ -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
+19 -3
View File
@@ -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;