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/look.zig | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'core/look.zig') diff --git a/core/look.zig b/core/look.zig index b240b6e2..f60b90f5 100644 --- a/core/look.zig +++ b/core/look.zig @@ -423,9 +423,7 @@ pub fn jumpResult(p: *Pardes, id: usize, row: usize) bool { rp.body.nsel = 0; rp.body.cur_row = r; rp.body.cur_pinned = true; - const off = rp.scroll(); - const last = off + @as(i32, rp.rows) - 1; - if (r < off) rp.scrollBy(r - off) else if (r > last) rp.scrollBy(r - last); + if (rp.file != null) panes.File.show(p.settings.tab, rp, r, 0, 0); var realbuf: [4096]u8 = undefined; const span = if (tr.commands) wholeRowSpan(ln) @@ -744,14 +742,9 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at_in: Spot, landing: enum { center, }; if (pane.file) |*f| { if (at.line > panes.File.nlines(p.gpa, f)) return; - if (landing == .center) { - // center, clamp at top; a text that fits is shown whole - const next = if (pane.textFits()) 0 else (at.line - 1) -| pane.rows / 2; - if (next != f.scroll) { - f.scroll = next; - f.syntax_dirty = true; - } - } + // centred (its first row of text half the body's rows down, as + // zz), clamped at the top; a text that fits is shown whole + if (landing == .center) panes.File.setTop(pane, if (pane.textFits()) .{ .line = 0 } else panes.File.walkRows(p.settings.tab, pane, .{ .line = at.line - 1, .row = panes.File.rowOf(p.settings.tab, pane, at.line - 1, 0) }, -@as(i64, pane.rows / 2)).to); } pane.body.cur_row = @intCast(at.line - 1); pane.body.cur_col = if (at.col > 0) @intCast(at.col - 1) else 0; @@ -2195,6 +2188,7 @@ test "Look on a diff: headers and a hunk line's prefix are the whole line, hover // Back to the diff, scrolled to its top again. p.view().active = 0; pane.file.?.scroll = 0; + pane.file.?.scroll_row = 0; p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); @@ -2268,6 +2262,7 @@ test "a click, a 9P whole-line look and an empty event write-back take one expan for (points) |at| { p.view().active = 0; pane.file.?.scroll = 0; + pane.file.?.scroll_row = 0; pane.body.cur_row = 0; p.sync(); _ = frame.reset(.retain_capacity); @@ -2344,6 +2339,7 @@ test "every entry point to Look takes one expansion: a diff line and a file:12:5 pane.body.cur_col = case.col; pane.body.cur_pinned = true; pane.file.?.scroll = 0; + pane.file.?.scroll_row = 0; p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); -- cgit v1.3 From defafb5ccce2ae30d3e16cc8fb57fb73485e03f4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 18:26:46 -0300 Subject: scroll: what still moved by lines now counts rows where a wrapped line made it wrong (P62 round 3, item 3): gt/gb land on the row `scroll_off` rows in from the body's top or its last row of text, a wrapped line's later row as any, the cursor on the first byte that row shows (helix's goto_window), where they went `scroll_off` LINES down from the top line (inside a wrapped line: a line far below, the view moved); gw labels the words of the bytes the body shows (`File.shownBytes`: its first row's first to its last row's last), none on a wrapped line's rows above the view's top (a word the first row starts inside of begins above it: its label would not show), where it labelled the words of `rows` lines from the top line's start; a new pane goes below the rows a pane's text takes (layout.zig `blankRows`: the rows from the view's top to the text's end, `File.rowsTo`, a wrapped line's each), where a pane of a few long lines counted a row a line and had the new pane laid over its text; a Look opening a file in a pane of its own puts its line half the body's rows down, a wrapped line's rows above it counted (look.zig, `walkRows`), where it went half the body's LINES up; TESTS FIRST, each failing on item 1's tree (`qwssppvo`) and passing here: core/normal.zig "gt and gb go to the row scroll_off rows in from the body's top and from its bottom, ..." (the view moved, row 4 to 5), "gw labels the words the body shows and no others: ..." (a label above the top), core/layout.zig "a new pane goes below the rows a pane's text takes, ..." (the pane kept fewer rows than its text), core/look.zig "a Look that opens a file in a pane of its own puts its line half the body's rows down, ..." (24 rows above it, not 17); STILL LINES, each right there: Places' first range (its search pieces are whole lines, so a match is never cut at a piece's edge, and `scroll + rows + 1` lines hold every row shown, each line at least one), the frames' `view` (a statistic's label: no decision reads it), the rail's thumb (rows would wrap the whole text at each width), the syntax window (the rows shown lie inside the lines `scroll .. scroll + rows`) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 17 ++++++++++ core/layout.zig | 35 +++++++++++++++++--- core/look.zig | 37 +++++++++++++++++++-- core/normal.zig | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 179 insertions(+), 11 deletions(-) (limited to 'core/look.zig') diff --git a/core/File.zig b/core/File.zig index b2439b29..8b7adae3 100644 --- a/core/File.zig +++ b/core/File.zig @@ -2095,6 +2095,23 @@ pub fn textRowLine(tab: u16, pane: *Pane, from: i64, margin: i64, way: enum { do return null; } +pub const Bytes = struct { start: usize, end: usize }; + +/// The bytes of the text `pane`'s body shows: its first row's first to its +/// last row's last (whole lines unwrapped, as each row is one). +pub fn shownBytes(tab: u16, pane: *Pane) Bytes { + const f = &pane.file.?; + const shown = shownRows(tab, pane); + if (!pane.body_wrapped) return .{ .start = lineStart(pane.gpa, f, f.scroll), .end = lineStart(pane.gpa, f, f.scroll + shown) }; + const first = pane.context_rows; + const last = first + shown - 1; + const start = lineStart(pane.gpa, f, @intCast(pane.wrap_line[first])) + @as(usize, @intCast(pane.wrap_col[first])); + const line: usize = @intCast(pane.wrap_line[last]); + if (line >= nlines(pane.gpa, f)) return .{ .start = start, .end = f.content.len }; + const text = sourceLine(pane, @intCast(line)); + return .{ .start = start, .end = lineStart(pane.gpa, f, line) + fitEnd(tab, text, @intCast(pane.wrap_col[last]), wrapWidth(pane, pane.view.wrap) -| pane.wrap_pad[last]) }; +} + /// A call stack's row after its line's first starts with its tree's guide /// (locations.treeContinuation), in `buffer`; any other row with none. fn guideOf(f: *const State, text: []const u8, at: usize, width: usize, buffer: *[1024]u8) []const u8 { diff --git a/core/layout.zig b/core/layout.zig index 4c7d6afb..7fa57ee9 100644 --- a/core/layout.zig +++ b/core/layout.zig @@ -845,10 +845,14 @@ fn bodyRows(p: *const Pardes, id: usize) u16 { fn blankRows(p: *const Pardes, id: usize) u16 { const pane = p.panes[id].?; const rows = bodyRows(p, id); - const used: usize = if (pane.file) |*f| - // The empty line after a final newline shows nothing. - (panes.File.nlines(p.gpa, f) -| @intFromBool(std.mem.endsWith(u8, f.content, "\n"))) -| f.scroll - else if (pane.isTerminal()) + const used: usize = if (pane.file) |*f| used: { + // The rows from the view's top to the text's end, a wrapped line's + // each; the empty line after a final newline shows nothing. + const end = panes.File.nlines(p.gpa, f) -| @intFromBool(std.mem.endsWith(u8, f.content, "\n")); + const top = panes.File.top(p.settings.tab, pane); + if (end <= top.line) break :used 0; + break :used @intCast(panes.File.rowsTo(p.settings.tab, pane, top, end, rows) orelse rows); + } else if (pane.isTerminal()) @as(usize, panes.Term.grid(pane.terminal).cursor_y) + 1 else rows; @@ -2158,3 +2162,26 @@ test "tiny screens keep pane space before adding a column tag" { try std.testing.expectEqual(TOPBAR_H, p.view().rects[0].y); _ = try p.render(p.scratch.allocator()); } + +test "a new pane goes below the rows a pane's text takes, a wrapped line's each counted: a pane whose few lines fill its rows keeps them all" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(io, .{ .sub_path = "b.txt", .data = "other\n" }); + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(io, &buf)]; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 40 }); + defer p.deinit(); + p.settings.wrap = true; + // Two lines of 400 bytes: seven rows each at this width. + const long = &@as([400]u8, @splat('a')); + const pane = try p.setTestFile(long ++ "\n" ++ long ++ "\n"); + const a = p.view().active; + const used = panes.File.lineRows(p.settings.tab, pane, 0) + panes.File.lineRows(p.settings.tab, pane, 1); + try std.testing.expect(used >= 12); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/b.txt", .{dir}) }); + try std.testing.expect(p.view().active != a); + try std.testing.expect(p.view().rects[a].h -| pane.tag_rows >= used); +} diff --git a/core/look.zig b/core/look.zig index f60b90f5..eee808cb 100644 --- a/core/look.zig +++ b/core/look.zig @@ -1605,11 +1605,13 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // miss of its line too. if (p.fs.failures != failures) return; layout.compute(p); - nt.file.?.scroll -|= @max(1, v.rects[free].h -| BOX_H) / 2; + // Its line half the body's rows down, a wrapped line's + // rows above it counted. + const centered = panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, @max(1, v.rects[free].h -| BOX_H) / 2)).to; + panes.File.setTop(nt, centered); if (target.at.end_line != 0) { - const centered = nt.file.?.scroll; selectSpan(p, nt, target.at); - nt.file.?.scroll = centered; + panes.File.setTop(nt, centered); } opened = free; } @@ -1840,6 +1842,35 @@ test "lookableLineSpan prefers the longest run, so a blank inside a path is one ); } +test "a Look that opens a file in a pane of its own puts its line half the body's rows down, a wrapped line's rows above it counted" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + // Line 10 of 600 bytes, eleven rows or more in a pane 60 wide. + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..60) |i| if (i == 10) { + try text.appendNTimes(gpa, 'a', 600); + try text.append(gpa, '\n'); + } else try text.print(gpa, "line {d:0>2}\n", .{i}); + try tmp.dir.writeFile(io, .{ .sub_path = "c.txt", .data = text.items }); + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(io, &buf)]; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 40 }); + defer p.deinit(); + p.settings.wrap = true; + _ = try p.setTestFile("x\n"); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/c.txt:14", .{dir}) }); + const id = p.view().active; + const pane = p.panes[id].?; + try std.testing.expect(panes.File.lineRows(p.settings.tab, pane, 10) >= 11); + try std.testing.expectEqual(@as(i32, 13), pane.body.cur_row); + const half: i64 = @max(1, p.view().rects[id].h -| BOX_H) / 2; + try std.testing.expectEqual(half, panes.File.rowsTo(p.settings.tab, pane, panes.File.top(p.settings.tab, pane), 13, 100).?); +} + test "a miss names a long path by its start and its end, its middle given up to …" { var cut: [miss_path_cap + "…".len]u8 = undefined; const long = "/home/someone/" ++ std.mem.asBytes(&@as([80][5]u8, @splat("deep/".*))) ++ "the-file.zig"; diff --git a/core/normal.zig b/core/normal.zig index 61f8f26b..fe0c729a 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -339,11 +339,22 @@ fn startJump(p: *Pardes, pane: *Pane, t: *Text) edit.Error!void { const text = try edit.flatSurface(p.editor(), t); const cursor = modal.selectionCursor(text, t.primaryRange(text, 0)); const top: usize = @intCast(@max(0, pane.scroll())); - const start = @min(modal.lineStartOffset(text, top), text.len); - const end = @min(modal.lineStartOffset(text, top + pane.rows), text.len); + // A file's words the body shows, its rows' bytes; a terminal's rows + // are its lines. + const shown = if (pane.file != null) File.shownBytes(p.settings.tab, pane) else File.Bytes{ .start = modal.lineStartOffset(text, top), .end = modal.lineStartOffset(text, top + pane.rows) }; + const start = @min(shown.start, text.len); + const end = @min(shown.end, text.len); const letters = config.jump_label_alphabet.len; try pane.jump.resize(p.gpa, letters * letters); - pane.jump.shrinkRetainingCapacity(modal.jumpWords(text, cursor, start, end, pane.jump.items)); + const n = modal.jumpWords(text, cursor, start, end, pane.jump.items); + // A word the body's first row starts inside of (a wrapped line's row + // cut mid-word) begins above it: no label it would not show. + var kept: usize = 0; + for (pane.jump.items[0..n]) |r| if (@min(r.anchor, r.head) >= start) { + pane.jump.items[kept] = r; + kept += 1; + }; + pane.jump.shrinkRetainingCapacity(kept); pane.jump_first = null; } @@ -978,11 +989,28 @@ fn gotoWindow(tab: u16, t: *Text, text: []const u8, range: modal.Selection, whic // a view to move through is a body's; a tag or an answer is all shown if (t.what != .body) return; const w = t.pane() orelse return; + const n: i32 = @intCast(cnt - 1); + if (w.file != null) { + // In rows, as helix's goto_window: the row `margin + n` in from the + // body's top or its last row of text, inside the margins, the + // cursor on the first byte it shows (a wrapped line's later row). + const held: i32 = w.context_rows; + const shown: i32 = File.shownRows(tab, w); + const lines = File.textLineCount(t, text); + var last: i32 = shown - 1; + while (last > 0 and w.wrapAt(tab, held + last).line >= lines) last -= 1; + const margin: i32 = @min(config.scroll_off, @divTrunc(shown - 1, 2)); + const vr = std.math.clamp(@min(@max(switch (which) { + .top => margin + n, + .bottom => last - (margin + n), + }, margin), last - margin), 0, last); + const at = w.wrapAt(tab, held + vr); + return pointMove(tab, t, text, range, File.textLineStart(t, text, @intCast(at.line)) + if (w.body_wrapped) @as(usize, @intCast(at.at)) else 0); + } const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, w.rows) - 1, 2)); const top = w.scroll(); const last_row: i32 = @intCast(File.textLineCount(t, text) - 1); const last_vis: i32 = @min(@as(i32, w.rows) - 1, last_row - top); - const n: i32 = @intCast(cnt - 1); var vline: i32 = switch (which) { .top => top + margin + n, .bottom => top + last_vis - (margin + n), @@ -1461,6 +1489,71 @@ test "gj/gk step the wrapped rows a body draws while j/k keep the file's lines" try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); } +/// 10 short lines, line 10 of 200 numbered words (17 rows or more at 60 +/// columns), 40 short lines after it; the view's top four rows into line +/// 10, the cursor on a row the body shows. +fn wrappedView(p: *Pardes) !*Pane { + p.settings.wrap = true; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(p.gpa); + for (0..51) |i| if (i == 10) { + for (0..200) |w| try text.print(p.gpa, "w{d:0>3} ", .{w}); + try text.append(p.gpa, '\n'); + } else try text.print(p.gpa, "line {d:0>2}\n", .{i}); + const pane = try p.setTestFile(text.items); + File.setTop(pane, .{ .line = 10, .row = 4 }); + p.sync(); + const at = pane.wrapAt(p.settings.tab, pane.context_rows + 6); + pane.body.cur_row = at.line; + pane.body.cur_col = at.at; + return pane; +} + +test "gt and gb go to the row scroll_off rows in from the body's top and from its bottom, a wrapped line's later row as any, the cursor on its first byte; the view stays" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 24 }); + defer p.deinit(); + const tab = p.settings.tab; + const pane = try wrappedView(p); + const top = File.top(tab, pane); + try std.testing.expectEqual(@as(u32, 4), top.row); + for ("tb") |k| { + p.update(.{ .key = .{ .cp = 'g', .text = "g" } }); + p.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); + try std.testing.expectEqual(top, File.top(tab, pane)); + const row: i32 = if (k == 't') config.scroll_off else File.shownRows(tab, pane) - 1 - config.scroll_off; + const want = pane.wrapAt(tab, pane.context_rows + row); + try std.testing.expectEqual(want.line, pane.body.cur_row); + try std.testing.expectEqual(want.at, pane.body.cur_col); + } +} + +test "gw labels the words the body shows and no others: none on a wrapped line's rows above the view's top" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 24 }); + defer p.deinit(); + const tab = p.settings.tab; + const pane = try wrappedView(p); + p.update(.{ .key = .{ .cp = 'g', .text = "g" } }); + p.update(.{ .key = .{ .cp = 'w', .text = "w" } }); + try std.testing.expect(pane.jump.items.len > 20); + const f = &pane.file.?; + const shown = File.shownRows(tab, pane); + // Rows with a label: the body's first and its last among them. + var labelled: [2]bool = .{ false, false }; + for (pane.jump.items) |r| { + const at = @min(r.anchor, r.head); + const line = std.mem.count(u8, f.content[0..at], "\n"); + const col = at - File.lineStart(p.gpa, f, line); + // The row that shows its first byte is in the body, at or after + // that row's start (no row above the top). + const row = pane.wrapRow(tab, @intCast(line), @intCast(col)); + try std.testing.expect(row.row >= pane.context_rows and row.row < pane.context_rows + shown); + try std.testing.expect(col >= row.at); + if (row.row == pane.context_rows) labelled[0] = true; + if (row.row == pane.context_rows + shown - 1) labelled[1] = true; + } + try std.testing.expectEqual([2]bool{ true, true }, labelled); +} + test "flat text movement and selection replay need no scratch rows" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); -- cgit v1.3