From e06383c0cab82232148a45091ed637d3f30e1261 Mon Sep 17 00:00:00 2001 From: Jephthah Okpukoro Date: Tue, 1 Dec 2026 06:12:15 +0100 Subject: [PATCH] fix(selection): correct absoluteRow mapping in select() and selectAll() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both methods treated getViewportY() — the distance scrolled back from the live bottom — as an absolute row index. Whenever scrollback existed they anchored selections on scrollback-head rows instead of the visible viewport, so select() silently selected the wrong line and selectAll() produced text disjoint from what the user saw. select() now maps viewport rows through viewportRowToAbsolute(), the same mapping selectLines() uses. selectAll() now spans the whole buffer: [0, scrollbackLength + rows - 1], matching what getSelection() can extract and what "select all" means. Regression tests cover both: selectAll() over scrollback+screen, and select() on viewport row 0 with scrollback present. Fixes #233 (selection part) --- lib/selection-manager.test.ts | 47 +++++++++++++++++++++++++++++++++++ lib/selection-manager.ts | 22 ++++++++++------ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/selection-manager.test.ts b/lib/selection-manager.test.ts index 663abc01..5f5e5802 100644 --- a/lib/selection-manager.test.ts +++ b/lib/selection-manager.test.ts @@ -488,6 +488,32 @@ describe('SelectionManager', () => { term.dispose(); }); + + test('selectAll selects scrollback plus screen, not scrollback-head rows', async () => { + if (!container) return; + + const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 }); + term.open(container); + + // 50 lines → 26 lines of scrollback over a 24-row viewport. + for (let i = 0; i < 50; i++) { + term.write(`SB${i.toString().padStart(3, '0')}\r\n`); + } + expect(term.wasmTerm!.getScrollbackLength()).toBeGreaterThan(0); + + const selMgr = (term as any).selectionManager; + selMgr.selectAll(); + const text = selMgr.getSelection(); + + // Whole buffer: oldest scrollback line through the last screen row — + // previously this returned rows [viewportY, viewportY+rows) which at + // the live bottom is the scrollback head, not the visible content. + expect(text).toContain('SB000'); + expect(text).toContain('SB049'); + expect(selMgr.hasSelection()).toBe(true); + + term.dispose(); + }); }); describe('select() API', () => { @@ -507,6 +533,27 @@ describe('SelectionManager', () => { term.dispose(); }); + + test('select() anchors on the viewport, not scrollback-head rows', async () => { + if (!container) return; + + const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 }); + term.open(container); + + for (let i = 0; i < 50; i++) { + term.write(`SB${i.toString().padStart(3, '0')}\r\n`); + } + expect(term.wasmTerm!.getScrollbackLength()).toBeGreaterThan(0); + + const selMgr = (term as any).selectionManager; + // The trailing newline parks the cursor on line 51, so viewport row + // 0 is 'SB027' — the buggy absoluteRow = viewportY + row read + // scrollback row 0 ('SB000'). + selMgr.select(0, 0, 5); + expect(selMgr.getSelection()).toBe('SB027'); + + term.dispose(); + }); }); describe('selectLines() API', () => { diff --git a/lib/selection-manager.ts b/lib/selection-manager.ts index 56d46059..1f5e4488 100644 --- a/lib/selection-manager.ts +++ b/lib/selection-manager.ts @@ -260,9 +260,15 @@ export class SelectionManager { */ selectAll(): void { const dims = this.wasmTerm.getDimensions(); - const viewportY = this.getViewportY(); - this.selectionStart = { col: 0, absoluteRow: viewportY }; - this.selectionEnd = { col: dims.cols - 1, absoluteRow: viewportY + dims.rows - 1 }; + // The whole buffer — scrollback plus screen — not just the visible + // viewport. getViewportY() is a distance scrolled back, so the old + // [viewportY, viewportY+rows) range landed on scrollback-head rows + // whenever scrollback existed. + this.selectionStart = { col: 0, absoluteRow: 0 }; + this.selectionEnd = { + col: dims.cols - 1, + absoluteRow: this.wasmTerm.getScrollbackLength() + dims.rows - 1, + }; this.requestRender(); this.selectionChangedEmitter.fire(); } @@ -290,10 +296,12 @@ export class SelectionManager { // Clamp end row endRow = Math.min(endRow, dims.rows - 1); - // Convert viewport rows to absolute rows - const viewportY = this.getViewportY(); - this.selectionStart = { col: column, absoluteRow: viewportY + row }; - this.selectionEnd = { col: endCol, absoluteRow: viewportY + endRow }; + // Convert viewport rows to absolute rows — through the same mapping + // selectLines uses, not getViewportY()+row: getViewportY() is the + // distance scrolled back, so raw addition anchored on scrollback-head + // rows whenever scrollback existed. + this.selectionStart = { col: column, absoluteRow: this.viewportRowToAbsolute(row) }; + this.selectionEnd = { col: endCol, absoluteRow: this.viewportRowToAbsolute(endRow) }; this.requestRender(); this.selectionChangedEmitter.fire(); }