From b1fb4242c0ab52767df1b8e155edcf2692f37873 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 18:00:35 -0300 Subject: scroll: a view's top is a line and a row within that line's own wrapped rows, and a scroll moves it by rows (P62, round 3, on the layer's new model where the `Changes` layer's lines are lines of the buffer): a wheel's notch, the glide, PageDown/PageUp, zj/zk, Ctrl-d/u, zt/zz/zb and the margin (`scroll_off` now ROWS) go a row at a time through a line wrapped over many rows and through a long description line of the layer, where the base jumped all of a wrapped line's rows at once (the user, 2026-11-23: "the scroll view jumps around near deleted lines on the diff view"; one-row deleted lines were cured by the layer's second step, a wrapped one was not): TESTS FIRST: core/jj.zig "a view scrolls by rows through a line wrapped over 12 rows and a change's description wrapped over several (a line of the layer): ..." (a notch is one row down the whole text and back, the notches the text's rows exactly; PageDown/PageUp a page of rows; zj/zk one; a held j and k keep the cursor's row `scroll_off` rows inside the body) failing on the base at notch 21 (line 20's 12 rows crossed at once), and "a client wheels its session's view a row a notch into a change's description wrapped over several rows ..., agreeing at every notch; a client joined over that view loads its row"; kept from round 1: core/replica.zig's join over a view scrolled into a wrapped line (with a rollback and a dump) and the explorer of a view's rows (30 seeds), dev/gui's glide a row a notch; THE CHANGE, taken from pardes-row-scroll (`xrxmqlnw`) and -2 (`xxzmwnvq`) without their ghost-row "blocks" (a block is now just a line's own rows): `File.State.scroll_row` and `views.PaneView.scroll_row` in the generic walk (digested, dumped, copied; a dump of 34 loads at its lines' first rows), the wire 35; core/File.zig `Top`, `rowIn`/`rowWalked` (a line's rows as fillBody wraps them, `guideOf` shared), `rowOf`, `lineRows`, `top`/`setTop`, `walkRows`/`walkKnown`, `rowsTo`, `rowsOf` (off the map where it holds the line), `mapFresh`, `scrollRows`, `shownRows` (the geometry laid out first), `show` (a cursor's or a result's row inside the band, the line's rows whole when they fit), `textRowLine`, fillBody's skip to the top's row walked once a geometry (`Pane.top_skip`, derived), the geometry's key with `scroll_row` and stored as what the map was made from; the one-row fact `State.widest` (`Widest`, an upper bound kept by the index's own vector passes `countLines`/`fillLines`) so a view whose lines are each one row walks nothing (`oneRow`: line arithmetic, checked against the walk in Debug); `fitEnd` a vector of printable ASCII at a time; the callers in rows: panes.zig `scrollBy(tab, delta)` and `ensureCursorVisible` (a file's through `File.show`), the wheel and the rail and the edge scroll (mouse.zig), Ctrl-d/u (normal.zig `halfPageMove`), zt/zz/zb, Ctrl-f/b and PgUp/PgDn (Text.zig `scrollViewMove`), Look's reveal and centring, Places' `goTo`/`restore`, Query's `showCurrent`/`caretTo`, the frames' `Region.row` and the glide (dev/glide.zig, the window's `topOf`); test/perf.zig's checks by (line, row) and its seek at a line's first row; Debug in the workspace: core-test 963/1129 (166 skipped), shell-test 1172/1175 (3 skipped) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/panes.zig | 62 ++++++++++++++++------------------------------------------ 1 file changed, 17 insertions(+), 45 deletions(-) (limited to 'core/panes.zig') diff --git a/core/panes.zig b/core/panes.zig index 02ee805f..b238992a 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -31,7 +31,7 @@ pub const Pane = struct { /// it (views.zig). (The 9P server's part of `fs` says so in its type.) pub const replica = .{ .local = .{ "pv", "pv_known" }, - .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "notices", "pan" }, + .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan" }, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's memory is its own allocator's (a dump loaded makes // it there). @@ -309,6 +309,10 @@ pub const Pane = struct { wrap_n: u16 = 0, /// A row map also exists for sticky headers when soft wrapping is off. body_wrapped: bool = false, + /// Where the geometry's walk found the view's top inside its line's + /// rows (File.Top): the row it is and the bytes of the line before it; + /// a frame's text starts there with no walk again. + top_skip: struct { row: u32 = 0, at: usize = 0 } = .{}, context_rows: u16 = 0, body_rows: u16 = 0, body_visible_rows: u16 = 0, @@ -636,15 +640,11 @@ pub const Pane = struct { return .{ .row = -1, .at = 0 }; } - pub fn scrollBy(pane: *Pane, delta: i32) void { - if (pane.file) |*f| { - const max: i64 = @intCast(File.nlines(pane.gpa, f) -| 1); - const n = std.math.clamp(@as(i64, @intCast(f.scroll)) + delta, 0, max); - const next: usize = @intCast(n); - if (next != f.scroll) { - f.scroll = next; - f.syntax_dirty = true; - } + /// The view moved `delta` rows: a file's through its lines' wrapped + /// rows (File.Top), a terminal's through its history. + pub fn scrollBy(pane: *Pane, tab: u16, delta: i32) void { + if (pane.file != null) { + _ = File.scrollRows(tab, pane, delta); } else if (pane.addon == null) { // the vt scrolls in SHELL rows; convert through the edit buffer. // The alternate screen has no history (ghostty's scrolls none): @@ -669,42 +669,14 @@ pub const Pane = struct { pub fn ensureCursorVisible(pane: *Pane, tab: u16) void { File.geometry(tab, pane); - const off = pane.scroll(); - var in_context = false; - // The map is the view's as it is now (File.geometry): an ancestor is - // pinned only in the view it was found for. - if (pane.context_rows < pane.wrap_n and pane.wrap_line[pane.context_rows] == off) { - for (pane.wrap_line[0..pane.context_rows]) |row| { - if (row == pane.body.cur_row) { - in_context = true; - break; - } - } - } - // Scrolloff is measured in the ordinary body, excluding sticky rows. - var margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, pane.rows) - 1, 2)); - var last = off + @as(i32, pane.rows) - 1; - const visible_rows = if (pane.body_visible_rows > 0) @min(pane.wrap_n, pane.body_visible_rows) else pane.wrap_n; - if (pane.context_rows < visible_rows) { - const lines_shown = pane.wrap_line[visible_rows - 1] - pane.wrap_line[pane.context_rows]; - last = off + lines_shown; - margin = @min(margin, @divTrunc(@max(0, lines_shown), 2)); - } - if (!in_context) { - if (pane.follow_edges != 0 and (pane.body.cur_row < off or pane.body.cur_row > last)) pane.follow_crossed = true; + if (pane.file != null) File.show(tab, pane, pane.body.cur_row, pane.body.cur_col, config.scroll_off) else { + // A terminal's rows are its lines. + const off = pane.scroll(); + const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, pane.rows) - 1, 2)); + const last = off + @as(i32, pane.rows) - 1; if (pane.body.cur_row < off + margin) { - if (pane.follow_edges & 1 == 0) pane.scrollBy(pane.body.cur_row - margin - off); - } else if (pane.body.cur_row > last - margin and pane.follow_edges & 2 == 0) { - var to = pane.body.cur_row + margin; - if (pane.file) |*f| { - // The margin only reveals text: a pane its whole text - // fits in never scrolls for it. A longer one keeps - // helix's clamp at the buffer's last line. - const lines: i32 = @intCast(File.nlines(pane.gpa, f)); - to = @min(to, if (off == 0 and pane.textFits()) @max(pane.body.cur_row, lines - 1 - @intFromBool(std.mem.endsWith(u8, f.content, "\n"))) else lines - 1); - } - pane.scrollBy(@max(0, to - last)); - } + pane.scrollBy(tab, pane.body.cur_row - margin - off); + } else if (pane.body.cur_row > last - margin) pane.scrollBy(tab, pane.body.cur_row + margin - last); } if (pane.file) |f| { if (pane.body_wrapped) return; -- cgit v1.3 From cdab9ba0f0fdb433217f6b408ef81357176fa965 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 18:20:56 -0300 Subject: perf: a long wrapped line's row walks cut to the rows the view needs, each row's end found without a grapheme step, the top's byte handed to the geometry by the step that found it, the geometry's key hashed once a step (P62 round 3, item 2): perfops against tree6 from 11 of 121 over (item 1's tree: body-appends on longline +18.2%, client-key-down on longline +1.44%) to 10 (deep-sel's eight, the view drawing the selection deep in its 2 MB line; body-appends on longline's SETUP +0.66%, derived; client-key-down on longline +0.44%; terminal on all +0.12%, below the base's own ruled +0.14%), body-appends on longline per-op -7.4%, every key, wheel, page and edit cell at or under tree6; each cut found by Intel PT by symbol (`.scratch/sketch/reorg/pardes-row-scroll-3/pt-*.txt`): `fitEnd` ended every full row with a grapheme step to learn the next byte did not fit (~118 instructions a row): a full row before a printable ASCII byte that no non-ASCII byte follows now ends there, and the rest of a row of printable ASCII past the vector loop is one masked vector (both checked by the exhaustive fitEnd tests); `show` walks the cursor's line once and no further than it needs (`Bound`: `margin` rows past the cursor's and `body - 2 margin + 1` in all, its rows a lower bound then, which its walks never pass), keeping the first bytes of its last rows in a ring on the stack, and where the view lands in that line hands that byte to the geometry (`Pane.top_skip` gains the `key` of the geometry it is for: fillBody walks to the top's row only for a key no step found it for); a wheel's notch down off the map hands the map's byte the same way; `show` and `scrollRows` hash the geometry's key once (`topNow`, `shownNow`; ensureCursorVisible's extra `geometry` call gone) and a walk asks whether the map is fresh only when it reads a line off it; `rowsOf` walks a line with the walk's width (`rowWalked`), not through `rowIn`'s one-row check again; Debug in the workspace: core-test 963/1129 (166 skipped) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 113 ++++++++++++++++++++++++++++++++++++++++++--------------- core/panes.zig | 10 ++--- 2 files changed, 88 insertions(+), 35 deletions(-) (limited to 'core/panes.zig') diff --git a/core/File.zig b/core/File.zig index 86861792..b2439b29 100644 --- a/core/File.zig +++ b/core/File.zig @@ -622,6 +622,14 @@ pub fn fitEnd(tab: u16, text: []const u8, start: usize, width: usize) usize { used += lanes; end += lanes; } + // The rest of a row of printable ASCII in one vector more: its bytes and + // the two after them printable ASCII, the row ends where the width does + // (what the loops below find a byte at a time). + if (used < width and width - used + 2 <= lanes and end + lanes <= text.len) { + const v: Lanes = text[end..][0..lanes].*; + const bad: @Int(.unsigned, lanes) = @bitCast(v -% @as(Lanes, @splat(0x20)) >= @as(Lanes, @splat(0x5f))); + if (@ctz(bad) >= width - used + 2) return end + (width - used); + } while (used < width and end < text.len) { const b = text[end]; if (b < 0x20 or b >= 0x7f) break; @@ -629,6 +637,10 @@ pub fn fitEnd(tab: u16, text: []const u8, start: usize, width: usize) usize { used += 1; end += 1; } + // A full row before a printable ASCII byte no non-ASCII byte follows (a + // grapheme of its own, a cell wide): the row ends there, nothing walked + // (every wrapped row ends so, and the walk cost a row a hundred more). + if (used == width and end > start and end < text.len and text[end] -% 0x20 < 0x5f and (end + 1 == text.len or text[end + 1] < 0x80)) return end; while (end < text.len) { const next_end = modal.nextGrapheme(text, end); const next_used = used +| graphemeDisplayWidth(tab, text[end..next_end]); @@ -1768,8 +1780,7 @@ pub fn lineRows(tab: u16, pane: *const Pane, line: usize) u32 { /// Whether every line of `pane`'s view is one row: none wider than a row /// (the text's widest, kept with its index), so its rows are its lines and /// the walks below are line arithmetic, nothing read. -fn oneRow(tab: u16, pane: *const Pane) bool { - const width = wrapWidth(pane, pane.view.wrap); +fn oneRow(tab: u16, pane: *const Pane, width: usize) bool { const f = &pane.file.?; return width == 0 or (f.line_starts.len > 0 and f.widest.cells(tab) <= width); } @@ -1780,16 +1791,24 @@ const RowIn = struct { row: u32, rows: u32 }; /// of a line's rows every map from rows to lines asks, beside the /// geometry's own map of the body (the same `fitEnd` at the same width). fn rowIn(tab: u16, pane: *const Pane, line: usize, col: usize) RowIn { - if (pane.file != null and oneRow(tab, pane)) { - if (builtin.mode == .debug) std.debug.assert(rowWalked(tab, pane, line, col).rows == 1); + const width = wrapWidth(pane, pane.view.wrap); + if (pane.file != null and oneRow(tab, pane, width)) { + if (builtin.mode == .debug) std.debug.assert(rowWalked(tab, pane, width, line, col, .{}, &.{}).rows == 1); return .{ .row = 0, .rows = 1 }; } - return rowWalked(tab, pane, line, col); + return rowWalked(tab, pane, width, line, col, .{}, &.{}); } -fn rowWalked(tab: u16, pane: *const Pane, line: usize, col: usize) RowIn { +/// How far a walk of a line's rows goes once the row showing its byte is +/// found: `after` rows past it and `least` in all, its rows a lower bound +/// then (what `show` needs of a long line); else to its end. +const Bound = struct { after: u32 = std.math.maxInt(u32), least: u32 = 0 }; + +/// rowIn's walk at `width`, as far as `bound`; `starts`, when it has room, +/// keeps the first byte of each row walked after the first, the last +/// `starts.len` of them (row `r` at `r % starts.len`; its caller's row 0). +fn rowWalked(tab: u16, pane: *const Pane, width: usize, line: usize, col: usize, bound: Bound, starts: []usize) RowIn { const f = if (pane.file) |*file| file else return .{ .row = 0, .rows = 1 }; - const width = wrapWidth(pane, pane.view.wrap); if (width == 0) return .{ .row = 0, .rows = 1 }; const text = sourceLine(pane, @intCast(line)); // No more bytes than a row has cells and no tab is one row: a tab is @@ -1807,8 +1826,10 @@ fn rowWalked(tab: u16, pane: *const Pane, line: usize, col: usize) RowIn { const end = fitEnd(tab, text, at, width - displayWidth(tab, guideOf(f, text, at, width, &guide))); if (found == null and col < end) found = row; if (end >= text.len or end <= at) return .{ .row = found orelse row, .rows = row + 1 }; + if (found) |r| if (row >= r +| bound.after and row + 1 >= bound.least) return .{ .row = r, .rows = row + 1 }; at = end; row += 1; + if (starts.len > 0) starts[row % starts.len] = at; } } @@ -1816,10 +1837,15 @@ fn rowWalked(tab: u16, pane: *const Pane, line: usize, col: usize) RowIn { /// rewrapped, an edit, the layer's lines come or gone) is read as its /// last, as the geometry's walk found it (no walk of the line a frame). pub fn top(tab: u16, pane: *Pane) Top { + if (pane.file.?.scroll_row > 0) geometry(tab, pane); + return topNow(pane); +} + +/// `top` off a geometry made already (a step that made it reads its key +/// once: each hash of it costs a long line's step a hundredth more). +fn topNow(pane: *const Pane) Top { const f = &pane.file.?; - if (f.scroll_row == 0) return .{ .line = f.scroll }; - geometry(tab, pane); - return .{ .line = f.scroll, .row = pane.top_skip.row }; + return .{ .line = f.scroll, .row = if (f.scroll_row == 0) 0 else pane.top_skip.row }; } pub fn setTop(pane: *Pane, to: Top) void { @@ -1844,7 +1870,7 @@ const Walked = struct { to: Top, moved: i64 }; fn walkKnown(tab: u16, pane: *Pane, from: Top, n: i64, known: ?Known) Walked { const last = nlines(pane.gpa, &pane.file.?) -| 1; - if (!oneRow(tab, pane)) return walkedRows(tab, pane, from, n, last, known); + if (!oneRow(tab, pane, wrapWidth(pane, pane.view.wrap))) return walkedRows(tab, pane, from, n, last, known); const to = if (n < 0) from.line -| @abs(n) else if (from.line >= last) from.line else @min(from.line +| @abs(n), last); const w: Walked = .{ .to = .{ .line = to }, .moved = @as(i64, @intCast(to)) - @as(i64, @intCast(from.line)) }; if (builtin.mode == .debug) std.debug.assert(std.meta.eql(w, walkedRows(tab, pane, from, n, last, known))); @@ -1852,9 +1878,10 @@ fn walkKnown(tab: u16, pane: *Pane, from: Top, n: i64, known: ?Known) Walked { } fn walkedRows(tab: u16, pane: *Pane, from: Top, n: i64, last: usize, known: ?Known) Walked { - const map = mapFresh(pane); + var map: ?bool = null; + const width = wrapWidth(pane, pane.view.wrap); var t = from; - var rows = rowsOf(tab, pane, map, known, t.line); + var rows = rowsOf(tab, pane, width, &map, known, t.line); t.row = @min(t.row, rows - 1); var left = n; while (left > 0) { @@ -1870,7 +1897,7 @@ fn walkedRows(tab: u16, pane: *Pane, from: Top, n: i64, last: usize, known: ?Kno left -= room + 1; t.line += 1; t.row = 0; - rows = rowsOf(tab, pane, map, known, t.line); + rows = rowsOf(tab, pane, width, &map, known, t.line); } } while (left < 0) { @@ -1884,7 +1911,7 @@ fn walkedRows(tab: u16, pane: *Pane, from: Top, n: i64, last: usize, known: ?Kno } else { left += t.row + 1; t.line -= 1; - rows = rowsOf(tab, pane, map, known, t.line); + rows = rowsOf(tab, pane, width, &map, known, t.line); t.row = rows - 1; } } @@ -1894,7 +1921,7 @@ fn walkedRows(tab: u16, pane: *Pane, from: Top, n: i64, last: usize, known: ?Kno /// The rows from `from` down to line `line`'s first row (up to it, /// negative), or null when it is more than `bound` rows away. pub fn rowsTo(tab: u16, pane: *const Pane, from: Top, line: usize, bound: u32) ?i64 { - if (!oneRow(tab, pane)) return rowsWalked(tab, pane, from, line, bound); + if (!oneRow(tab, pane, wrapWidth(pane, pane.view.wrap))) return rowsWalked(tab, pane, from, line, bound); const d = @as(i64, @intCast(line)) - @as(i64, @intCast(from.line)) - from.row; const to: ?i64 = if ((line > from.line and d > bound) or (line < from.line and -d > bound)) null else d; if (builtin.mode == .debug) std.debug.assert(std.meta.eql(to, rowsWalked(tab, pane, from, line, bound))); @@ -1902,16 +1929,17 @@ pub fn rowsTo(tab: u16, pane: *const Pane, from: Top, line: usize, bound: u32) ? } fn rowsWalked(tab: u16, pane: *const Pane, from: Top, line: usize, bound: u32) ?i64 { - const map = mapFresh(pane); + var map: ?bool = null; + const width = wrapWidth(pane, pane.view.wrap); var d: i64 = -@as(i64, from.row); var l = from.line; while (l < line) : (l += 1) { - d += rowsOf(tab, pane, map, null, l); + d += rowsOf(tab, pane, width, &map, null, l); if (d > bound) return null; } while (l > line) { l -= 1; - d -= rowsOf(tab, pane, map, null, l); + d -= rowsOf(tab, pane, width, &map, null, l); if (-d > bound) return null; } return d; @@ -1926,10 +1954,17 @@ fn mapFresh(pane: *const Pane) bool { /// The rows line `line` takes: counted off the map when the map is fresh /// and holds all of them in the body (no line walked: a page, a cursor -/// past the body's edge), else walked (lineRows). -fn rowsOf(tab: u16, pane: *const Pane, map: bool, known: ?Known, line: usize) u32 { +/// past the body's edge), else walked (a walk's: no line is one row). Its +/// walk's `map`: whether the map is fresh, asked the first time a line in +/// it is. +fn rowsOf(tab: u16, pane: *const Pane, width: usize, map: *?bool, known: ?Known, line: usize) u32 { if (known) |k| if (k.line == line) return k.rows; - if (map and pane.wrap_n > pane.context_rows and line >= pane.wrap_line[pane.context_rows] and line <= pane.wrap_line[pane.wrap_n - 1]) { + if (pane.wrap_n > pane.context_rows and line >= pane.wrap_line[pane.context_rows] and line <= pane.wrap_line[pane.wrap_n - 1] and + (map.* orelse fresh: { + map.* = mapFresh(pane); + break :fresh map.*.?; + })) + { const f = &pane.file.?; var rows: u32 = 0; var i: usize = pane.context_rows; @@ -1941,24 +1976,26 @@ fn rowsOf(tab: u16, pane: *const Pane, map: bool, known: ?Known, line: usize) u3 } else if (rows > 0) return rows; } } - return lineRows(tab, pane, line); + return rowWalked(tab, pane, width, line, std.math.maxInt(usize), .{}, &.{}).rows; } /// `pane`'s view moved `delta` rows (a wheel's notch is one): the rows it /// moved. pub fn scrollRows(tab: u16, pane: *Pane, delta: i64) i64 { geometry(tab, pane); - const t = top(tab, pane); + const t = topNow(pane); // Down to a row the body shows: the map has it, its line and its row in // that line, no line walked (a notch, a page). const i = @as(i64, pane.context_rows) + delta; - if (delta > 0 and pane.wrap_n > 0 and i < shownRows(tab, pane) + @as(i64, pane.context_rows)) { + if (delta > 0 and pane.wrap_n > 0 and i < shownNow(pane) + @as(i64, pane.context_rows)) { const k: usize = @intCast(i); const line: usize = @intCast(pane.wrap_line[k]); if (line < nlines(pane.gpa, &pane.file.?)) { var row: u32 = if (line == t.line) t.row else 0; for (pane.wrap_line[pane.context_rows..k]) |l| row += @intFromBool(l == pane.wrap_line[k]); setTop(pane, .{ .line = line, .row = row }); + // Its first byte is the map's: the geometry starts there. + if (pane.body_wrapped) pane.top_skip = .{ .key = geometryKey(pane, &pane.file.?), .row = row, .at = @intCast(pane.wrap_col[k]) }; return delta; } } @@ -1972,6 +2009,10 @@ pub fn scrollRows(tab: u16, pane: *Pane, delta: i64) i64 { /// before any draw, and only the state it is made of may decide it). pub fn shownRows(tab: u16, pane: *Pane) u16 { geometry(tab, pane); + return shownNow(pane); +} + +fn shownNow(pane: *const Pane) u16 { const visible = if (pane.body_visible_rows > 0) @min(pane.wrap_n, pane.body_visible_rows) else pane.wrap_n; return @max(1, if (pane.wrap_n == 0) pane.rows else visible -| pane.context_rows); } @@ -1984,12 +2025,12 @@ pub fn shownRows(tab: u16, pane: *Pane) u16 { pub fn show(tab: u16, pane: *Pane, line_at: i32, col: i32, margin_rows: i64) void { const f = &pane.file.?; geometry(tab, pane); - const t = top(tab, pane); + const t = topNow(pane); // The line held at the top (a sticky context row): as it is. if (pane.context_rows < pane.wrap_n and pane.wrap_line[pane.context_rows] == t.line) { for (pane.wrap_line[0..pane.context_rows]) |row| if (row == line_at) return; } - const body: i64 = shownRows(tab, pane); + const body: i64 = shownNow(pane); const margin: i64 = @min(margin_rows, @divTrunc(body - 1, 2)); // The row in the body (the map's, made just now), or none: the line is // above the top, or below the map. Inside the band the view stays, and @@ -1998,8 +2039,18 @@ pub fn show(tab: u16, pane: *Pane, line_at: i32, col: i32, margin_rows: i64) voi const row: ?i64 = if (mapped >= 0 and mapped < @max(pane.wrap_n, pane.rows) -| pane.context_rows) mapped else null; if (row) |r| if (r >= margin and r <= body - 1 - margin) return; const line: usize = @intCast(@max(0, line_at)); - const at = rowIn(tab, pane, line, @intCast(@max(0, col))); + // The line walked once and no further than the view needs (`Bound`: a + // long line's rows past the band are not), the first bytes of its last + // rows kept: where the view lands in it, its geometry starts there. + const width = wrapWidth(pane, pane.view.wrap); + var ring: [limits.wrap_rows]usize = undefined; + ring[0] = 0; + const starts = ring[0..@intCast(@min(body, ring.len))]; + const at = if (oneRow(tab, pane, width)) RowIn{ .row = 0, .rows = 1 } else rowWalked(tab, pane, width, line, @intCast(@max(0, col)), .{ .after = @intCast(margin), .least = @intCast(body - 2 * margin + 1) }, starts); const known: Known = .{ .line = line, .rows = at.rows }; + defer if (f.scroll == line and f.scroll_row < at.rows and at.rows - 1 - f.scroll_row < starts.len) { + pane.top_skip = .{ .key = geometryKey(pane, f), .row = f.scroll_row, .at = starts[f.scroll_row % starts.len] }; + }; if (pane.follow_edges != 0 and (row == null or row.? < 0 or row.? >= body)) pane.follow_crossed = true; const whole = at.rows <= body - 2 * margin; if (if (row) |r| r < margin else line <= t.line) { @@ -2659,7 +2710,9 @@ fn fillBody(comptime layered: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State // The top line from the view's row in it (`top`): the rows above // skipped, never past its last; walked once a geometry (the map's // pass), its text's pass a frame starting where that walk ended. - if (record_wrap) { + // A top whose byte a step already found (`show`, a scroll off the map) + // is not walked again. + if (record_wrap and pane.top_skip.key != pane.geometry_key) { var skipped: u32 = 0; while (skipped < f.scroll_row and width > 0 and body_line < total) : (skipped += 1) { const text = sourceLine(pane, @intCast(body_line)); @@ -2667,7 +2720,7 @@ fn fillBody(comptime layered: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State if (end >= text.len or end <= body_at) break; body_at = end; } - pane.top_skip = .{ .row = skipped, .at = body_at }; + pane.top_skip = .{ .key = pane.geometry_key, .row = skipped, .at = body_at }; } else body_at = pane.top_skip.at; for (0..if (pane.body_rows > 0) pane.body_rows else pane.rows) |i| { if (i > 0) { diff --git a/core/panes.zig b/core/panes.zig index b238992a..08d91df5 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -309,10 +309,11 @@ pub const Pane = struct { wrap_n: u16 = 0, /// A row map also exists for sticky headers when soft wrapping is off. body_wrapped: bool = false, - /// Where the geometry's walk found the view's top inside its line's - /// rows (File.Top): the row it is and the bytes of the line before it; - /// a frame's text starts there with no walk again. - top_skip: struct { row: u32 = 0, at: usize = 0 } = .{}, + /// Where the view's top is inside its line's rows (File.Top) for the + /// geometry of `key`: the row it is and the bytes of the line before + /// it, found by the geometry's walk or by the step that put the top + /// there; a frame's text starts there with no walk again. + top_skip: struct { key: u64 = 0, row: u32 = 0, at: usize = 0 } = .{}, context_rows: u16 = 0, body_rows: u16 = 0, body_visible_rows: u16 = 0, @@ -668,7 +669,6 @@ pub const Pane = struct { } pub fn ensureCursorVisible(pane: *Pane, tab: u16) void { - File.geometry(tab, pane); if (pane.file != null) File.show(tab, pane, pane.body.cur_row, pane.body.cur_col, config.scroll_off) else { // A terminal's rows are its lines. const off = pane.scroll(); -- cgit v1.3