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/views.zig | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'core/views.zig') diff --git a/core/views.zig b/core/views.zig index f414eb6c..4f2d5744 100644 --- a/core/views.zig +++ b/core/views.zig @@ -63,9 +63,11 @@ pub const PaneView = struct { follow_scroll: usize = 0, scrolled_to: ?usize = null, body: Part(Text, &body_fields) = .{}, - /// A document's scroll and size; a terminal's size is its pty's, which - /// every view shows (sized by its last typist, `Pardes.sync`). + /// A document's scroll (its top: File.Top) and size; a terminal's size + /// is its pty's, which every view shows (sized by its last typist, + /// `Pardes.sync`). scroll: usize = 0, + scroll_row: u32 = 0, cols: u16 = 1, rows: u16 = 1, @@ -252,7 +254,10 @@ fn extract(pane: *const Pane) PaneView { var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows }; copy(&pane_fields, &v, pane); copy(&body_fields, &v.body, &pane.body); - if (pane.file) |f| v.scroll = f.scroll; + if (pane.file) |f| { + v.scroll = f.scroll; + v.scroll_row = f.scroll_row; + } return v; } @@ -269,6 +274,7 @@ fn apply(pane: *Pane, v: *const PaneView) void { // brought back inside it. const lines = File.nlines(pane.gpa, f); f.scroll = @min(v.scroll, lines -| 1); + f.scroll_row = v.scroll_row; if (pane.body.cur_row >= lines) { pane.body.vsel.active = false; pane.body.nsel = 0; @@ -552,7 +558,10 @@ pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pa pane.cols = pv.cols; pane.rows = pv.rows; } - if (pane.file) |*file| file.scroll = pv.scroll; + if (pane.file) |*file| { + file.scroll = pv.scroll; + file.scroll_row = pv.scroll_row; + } pv.body.sels = &.{}; pv.body.nsel = 0; }; -- cgit v1.3 From c90db8c04787d923bbef500eb78c68fd65715cf0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 05:55:02 -0300 Subject: walk, views, Core: what the 40,000-seed dump fuzz reached once a view's geometry is in the dump again (its seeds land elsewhere; and the dump holds its tree's path, so each tree's seeds damage bytes of their own), each a run first: (1) THE WORKSPACE'S RUN (my gate's fuzz40k, the join road): `views.undumped` took `bit(d.k)` of a view's shares whose `k` was past any view (`integer does not fit in destination type` at views.zig `bit`), and `Shares.k` declared an index (`slots`) did NOT hold it: the `[]Shares` was a REFERENCE (walk.zig `Loader.ref`) to bytes made for another type, read as shares without being decoded (no-panic-1's open item, "references read back as another type", reached): every allocation a load makes records what it holds (`Made.of`, `typeWord` of its element), and a reference read back is to a value of its own type or the dump is refused (`Loader.ref`, `Get.again`, at a pointer's and a slice's, in both readers: a damaged one had one type's bytes read as another's, pointers and alignment and all; the writers share a reference between values of one type only, so no session's dump is refused); the skips' placeholders hold no type, so a reference to one is refused; `Shares.k` stays declared (a slot read back is held); (3) THE WORKSPACE'S RUN with (1) (my gate h2's fuzz40k): leaked 1 allocation, 7 bytes, a byte dump's theme (dump.zig `loadKnowing`: `l.get([]const u8)` failed after its slice was made, and the load's `defer` cut its books back to before the mounts, dropping the half-made slice unfreed: a leak at every such damaged join dump or Restore): a root read that fails lets go of what it had made (`Loader.get`, `Get.get`: `mark`/`letGo`, merge65's mechanism), so no caller's books hold half of a value; (2) MY SECOND TREE'S RUN (the join road): `Pardes.resize_count` read back as the most a usize holds, `+= 1` at the next resize (`integer overflow`, Core.zig `updateInner`, the fuzz's `use` resizing to 40x12): a count that only grows saturates (`+|=`); the fuzz with them: 40000 join and 1779 image seeds green in my second tree three times (212 seeds refused by `.within` each: 211 a pane's rect off its screen, 1 a column; 210 on the join road, 2 on the image's); in the workspace 40000 join seeds green with no leak (206 refused by `.within`), the image road green to seed 1366: seed 1367 there reaches xitdb's own reader (`switch on corrupt value`, xitdb lib.zig `readBytesObjectAlloc`, from Db.zig `Read.open` in dump.check: the last agent's seed 1780 in another tree, a dependency's, not here) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 2 +- core/views.zig | 6 ++++++ core/walk.zig | 55 ++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 22 deletions(-) (limited to 'core/views.zig') diff --git a/core/Core.zig b/core/Core.zig index 6c981adf..835eb86c 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1753,7 +1753,7 @@ fn updateInner(p: *Pardes, ev: Event) void { v.cell_pixels.w = @max(1, sz.cell_pixels.w); v.cell_pixels.h = @max(1, sz.cell_pixels.h); layout.compute(p); - p.resize_count += 1; + p.resize_count +|= 1; }, .output => |o| { const pane = p.panes[o.pane.index()] orelse return; diff --git a/core/views.zig b/core/views.zig index 4f2d5744..4c020981 100644 --- a/core/views.zig +++ b/core/views.zig @@ -82,6 +82,12 @@ pub const Shares = struct { k: u8, panes: []const Share = &.{}, + /// Its view, a slot (walk.zig `.index`). + pub const replica = .{ .index = .{.{ "k", "slots" }} }; + pub fn slots(_: *const Shares, k: usize) bool { + return k < max; + } + pub const Share = struct { id: u16, pv: PaneView }; }; diff --git a/core/walk.zig b/core/walk.zig index 51b0a384..7a77fdb6 100644 --- a/core/walk.zig +++ b/core/walk.zig @@ -1398,8 +1398,11 @@ fn countOf(n: u64) DecodeError!usize { } /// An allocation a load made: what a reference to it finds, and what a -/// failed load frees. -const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false }; +/// failed load frees. `of`: what it holds (`typeWord` of its element); a +/// reference read back is to a value of its own type, or the dump is +/// refused (a damaged one would have one type's bytes read as another's, +/// pointers and all). +const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false, of: u64 = 0 }; /// How far a reader had made things before a value: what that value made /// is everything after it (`letGo`). @@ -1509,8 +1512,8 @@ pub const Loader = struct { } } - fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment) DecodeError!usize { - try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa }); + fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment, of: u64) DecodeError!usize { + try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa, .of = of }); return l.shared.items.len - 1; } @@ -1609,6 +1612,11 @@ pub const Loader = struct { /// The next root, made: what it points at is `gpa`'s. pub fn get(l: *Self, comptime T: type) DecodeError!T { + // A root that fails lets go of what it had made: its caller's books + // (a load's, cut back past what it frees itself) never hold half + // of a value. + const at = l.mark(); + errdefer l.letGo(at); var out: T = undefined; try l.fresh(T, &out); try l.value(T, try l.root(), &out); @@ -1750,12 +1758,12 @@ pub const Loader = struct { }, .one => { const C = @typeInfo(T).pointer.child; - if (try l.ref()) |again| { + if (try l.ref(C)) |again| { out.* = @ptrCast(@alignCast(again.ptr)); return; } const one = try l.gpa.create(C); - const at = l.made(std.mem.asBytes(one), .of(C)) catch |err| { + const at = l.made(std.mem.asBytes(one), .of(C), typeWord(C)) catch |err| { l.gpa.destroy(one); return err; }; @@ -1821,22 +1829,23 @@ pub const Loader = struct { /// A reference to a pointer or slice made before, or null for a /// new one (its length then read). - fn ref(l: *Self) DecodeError!?[]u8 { + fn ref(l: *Self, comptime E: type) DecodeError!?[]u8 { const n = try l.scalar(u32); if (n == 0) return null; if (n > l.shared.items.len) return error.BadDump; const got = l.shared.items[n - 1]; if (got.dead) return error.Incompatible; + if (got.of != typeWord(E)) return error.BadDump; return got.bytes; } fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E { - if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; + if (try l.ref(E)) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; const n = try l.count(); if (n == 0) return &.{}; if (n > l.bytes.len - l.at) return error.EndOfDump; const many = try l.gpa.alloc(E, n); - const at = l.made(std.mem.sliceAsBytes(many), .of(E)) catch |err| { + const at = l.made(std.mem.sliceAsBytes(many), .of(E), typeWord(E)) catch |err| { l.gpa.free(many); return err; }; @@ -1994,14 +2003,14 @@ pub const Loader = struct { .float => _ = try l.take(s.bits / 8), .optional => if ((try l.take(1))[0] != 0) try l.skip(s.child), .one => if (try l.scalar(u32) == 0) { - _ = try l.made(&.{}, .@"1"); + _ = try l.made(&.{}, .@"1", 0); l.shared.items[l.shared.items.len - 1].dead = true; try l.skip(s.child); }, .slice => if (try l.scalar(u32) == 0) { const n = try l.scalar(u64); if (n > 0) { - _ = try l.made(&.{}, .@"1"); + _ = try l.made(&.{}, .@"1", 0); l.shared.items[l.shared.items.len - 1].dead = true; } try l.skipMany(s.child, n); @@ -2390,6 +2399,9 @@ pub const Get = struct { /// The value at `c`, made: what it points at is `gpa`'s. pub fn get(l: *Get, comptime T: type, c: Cursor) Error!T { + // As `Loader.get`: a value that fails leaves nothing made. + const at = l.mark(); + errdefer l.letGo(at); var out: T = undefined; try l.fresh(T, &out); try l.value(T, c, &out); @@ -2441,8 +2453,8 @@ pub const Get = struct { /// An allocation made for the value at `slot`, kept: what a value met /// again is, and what `rollback` frees. - fn keep(l: *Get, bytes: []u8, alignment: std.mem.Alignment, slot: xitdb.Slot) Error!usize { - try l.made.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa }); + fn keep(l: *Get, bytes: []u8, alignment: std.mem.Alignment, slot: xitdb.Slot, of: u64) Error!usize { + try l.made.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa, .of = of }); const at = l.made.items.len - 1; // Not kept after all: the caller frees it. if (placed(slot)) l.memo.put(l.own, slotWord(slot), at) catch |err| { @@ -2453,13 +2465,14 @@ pub const Get = struct { } /// The value made before for the same place, when there was one. - fn again(l: *Get, slot: xitdb.Slot) Error!?[]u8 { + fn again(l: *Get, slot: xitdb.Slot, comptime E: type) Error!?[]u8 { if (!placed(slot)) return null; const at = l.memo.get(slotWord(slot)) orelse return null; // Made, then let go of by the reader's caller (a dump's mounts). if (at >= l.made.items.len) return null; const m = l.made.items[at]; if (m.dead) return error.Incompatible; + if (m.of != typeWord(E)) return error.BadDump; return m.bytes; } @@ -2539,12 +2552,12 @@ pub const Get = struct { }, .one => { const C = @typeInfo(T).pointer.child; - if (try l.again(slot)) |bytes| { + if (try l.again(slot, C)) |bytes| { out.* = @ptrCast(@alignCast(bytes.ptr)); return; } const one = try l.gpa.create(C); - const at = l.keep(std.mem.asBytes(one), .of(C), slot) catch |err| { + const at = l.keep(std.mem.asBytes(one), .of(C), slot, typeWord(C)) catch |err| { l.gpa.destroy(one); return err; }; @@ -2674,7 +2687,7 @@ pub const Get = struct { /// A slice of `E` at `c`: numbers' bytes, a blob's text by its hash, or /// a list; one met again is the same. fn slice(l: *Get, comptime E: type, c: Cursor, slot: xitdb.Slot) Error![]E { - if (try l.again(slot)) |bytes| return @as([*]E, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @max(1, @sizeOf(E))]; + if (try l.again(slot, E)) |bytes| return @as([*]E, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @max(1, @sizeOf(E))]; if (comptime numbers(E)) { switch (slot.tag) { .bytes, .short_bytes => {}, @@ -2692,7 +2705,7 @@ pub const Get = struct { l.gpa.free(got.bytes); return &.{}; } - _ = l.keep(got.bytes, .@"1", slot) catch |err| { + _ = l.keep(got.bytes, .@"1", slot, typeWord(u8)) catch |err| { l.gpa.free(got.bytes); return err; }; @@ -2704,7 +2717,7 @@ pub const Get = struct { const n = got.bytes.len / width; if (n == 0) return &.{}; const many = try l.gpa.alloc(E, n); - const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot) catch |err| { + const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot, typeWord(E)) catch |err| { l.gpa.free(many); return err; }; @@ -2721,7 +2734,7 @@ pub const Get = struct { // sizes nothing. if (n > (c.db.core.length() catch return error.BadDump) / (@bitSizeOf(xitdb.Slot) / 8)) return error.BadDump; const many = try l.gpa.alloc(E, n); - const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot) catch |err| { + const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot, typeWord(E)) catch |err| { l.gpa.free(many); return err; }; @@ -2742,7 +2755,7 @@ pub const Get = struct { const at = (texts.getCursor(std.mem.readInt(u256, got.value[0..32], .big)) catch return error.BadDump) orelse return error.BadDump; const text = at.readBytesAlloc(l.gpa, most(at)) catch |err| return if (err == error.OutOfMemory) error.OutOfMemory else error.BadDump; const bytes: []u8 = @constCast(text); - _ = l.keep(bytes, .@"1", slot) catch |err| { + _ = l.keep(bytes, .@"1", slot, typeWord(u8)) catch |err| { l.gpa.free(bytes); return err; }; -- cgit v1.3 From 8d0b21544824051873950ddcae1d4aab027a01fc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 14:06:54 -0300 Subject: tests: a view's top read off a geometry it is not the view of (row scroll round 4, fault 1, TESTS FIRST, each failing on the base ynxykzrt): the release soak's panic (run 18 of 24, a client's drawOne: `index out of bounds: index 41, len 38` at core/File.zig fillBody `text[from..end]`) is a view drawn after ANOTHER VIEW's step moved the top: the step's `show` hands its next geometry the top's byte (`Pane.top_skip`, keyed for the geometry after the move) while the map stays the one made before it; a view that was where the first was (a client's view opened as a copy of the live one, the same size) has that map's key, so `geometry` returns early and the text pass takes `top_skip.at`, a byte of the OTHER view's top line, with no look at its key: views.zig "a view drawn after another view's step moved the top into a long wrapped line, the two alike before it: ..." panics on the base `index out of bounds: index 1584, len 5` (a probe run first, throwaway, every top_skip read checked against the walk: made by `show`, line 10 row 22, read for line 0); a STEP takes the same stale row (`topNow`): views.zig "a view's step after another view's step moved the top, the two alike before it: it scrolls from its own top's row, as a core loaded from the session (none of its geometry) does" (PageDown in the second view: expected 12, found 14 on the base: a session that drew and a client that did not step apart); and the tab width is not in the geometry's key (a Tab zeroes `geometry_key` but not `top_skip`, whose key matches again: a row start of the old width trusted; in bounds, a wrong row; 180 such reads in one Debug soak with the probe): File.zig "a view scrolled by rows into a line of tabs, the tab width changed after a frame: its body drawn from the row the new width makes, as a core loaded from it draws it" fails on the base (rows from the 8-cell width's byte 27, the 2-cell width's is 74); and a second derived reader of the same kind, found by the table of every reader of a pane's geometry (normal.zig `gotoWindow` read `context_rows` before `shownRows` made the geometry): normal.zig "gt after a step that moved the view off the place its geometry was made for: ..." (two notches up off the held headers, then gt: expected 3, found 5 on the base: the session read the held rows of the place before the notch) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 28 ++++++++++++++++++ core/normal.zig | 32 ++++++++++++++++++++ core/views.zig | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) (limited to 'core/views.zig') diff --git a/core/File.zig b/core/File.zig index 8e464d61..163f40ff 100644 --- a/core/File.zig +++ b/core/File.zig @@ -3679,3 +3679,31 @@ test "Syntax off in a patch's pane takes the language's ink and leaves the patch if (pass == 1) try std.testing.expect(differ); } } + +test "a view scrolled by rows into a line of tabs, the tab width changed after a frame: its body drawn from the row the new width makes, as a core loaded from it draws it" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + try text.appendSlice(gpa, "head\n"); + for (0..40) |i| try text.print(gpa, "\tw{d}", .{i}); + try text.appendSlice(gpa, "\ntail\n"); + const pane = try p.setTestFile(text.items); + try std.testing.expect(p.executeBuiltinLine(0, "Tab 8")); + // The top on line 1's fourth row (at 8 cells a tab), drawn. + const f = &pane.file.?; + f.scroll = 1; + f.scroll_row = 3; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + try std.testing.expect(p.executeBuiltinLine(0, "Tab 2")); + const drawn = try bodyText(p.settings.tab, arena.allocator(), pane, f); + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, bytes, null); + defer loaded.deinit(); + const lp = loaded.panes[0].?; + try std.testing.expectEqualStrings(try bodyText(loaded.settings.tab, arena.allocator(), lp, &lp.file.?), drawn); +} diff --git a/core/normal.zig b/core/normal.zig index fe0c729a..5a9794ee 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -1527,6 +1527,38 @@ test "gt and gb go to the row scroll_off rows in from the body's top and from it } } +test "gt after a step that moved the view off the place its geometry was made for: the row scroll_off in from the body's top under the context rows the view has now, as a core loaded from the session goes" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer p.deinit(); + const pane = try p.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][21]u8, @splat(" // body line\n".*)))) ++ " }\n};\n"); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/test.zig"); + if (!File.supportsContext(pane)) return error.SkipZigTest; + pane.file.?.tree_context = true; + // The view inside the function, its two headers held over the body, + // drawn. + File.setTop(pane, .{ .line = 3 }); + p.sync(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + try std.testing.expectEqual(@as(u16, 2), pane.context_rows); + // Two notches up: the top on the function's header, one header held + // (the geometry the second notch read was the one before it). + const rect = p.view().rects[0]; + for (0..2) |_| p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = rect.x + 10, .row = rect.y + 5 } }); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.scroll); + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, bytes, null); + defer loaded.deinit(); + for ([_]*Pardes{ p, loaded }) |core| for ("gt") |k| core.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); + try std.testing.expectEqual(loaded.panes[0].?.body.cur_row, pane.body.cur_row); + try std.testing.expectEqual(@as(u16, 1), pane.context_rows); + try std.testing.expectEqual(pane.wrapAt(p.settings.tab, 1 + config.scroll_off).line, pane.body.cur_row); +} + 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(); diff --git a/core/views.zig b/core/views.zig index 4c020981..5ad9b749 100644 --- a/core/views.zig +++ b/core/views.zig @@ -898,6 +898,96 @@ test "a view drawn while another is live keeps its own cursors" { try testing.expectEqual(@as(u16, 1), pane.body.nsel); } +test "a view drawn after another view's step moved the top into a long wrapped line, the two alike before it: drawn from its own top, line 0, not from the row the step found in the other's line" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + // A short first line, and line 10 wrapped over more rows than the body. + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + try text.appendSlice(gpa, "short\n"); + for (1..10) |i| try text.print(gpa, "line {d}\n", .{i}); + for (0..500) |w| try text.print(gpa, "w{d:0>4} ", .{w}); + try text.appendSlice(gpa, "\nlast\n"); + const pane = try p.setTestFile(text.items); + const k = p.openView().?; + // View 0's step puts the cursor at line 10's end: the top goes into + // that line's rows, a row far past line 0's five bytes. View k is + // where view 0 was: its top line 0, its size view 0's. + pane.body.cur_row = 10; + pane.body.cur_col = @intCast(pardes.panes.File.sourceLine(pane, 10).len - 1); + pane.body.cur_pinned = true; + pane.ensureCursorVisible(p.settings.tab); + p.sync(); + try testing.expectEqual(@as(usize, 10), pane.file.?.scroll); + try testing.expect(pane.file.?.scroll_row > 0); + const Seen = struct { + short: bool = false, + fn present(ctx: ?*anyopaque, s: *const pardes.Surface) bool { + const seen: *@This() = @ptrCast(@alignCast(ctx.?)); + var row: [512]u8 = undefined; + for (0..s.rows) |y| { + var n: usize = 0; + for (s.cells[y * s.cols ..][0..s.cols]) |c| { + if (n + c.len > row.len) break; + @memcpy(row[n..][0..c.len], c.text[0..c.len]); + n += c.len; + } + if (std.mem.indexOf(u8, row[0..n], "short") != null) seen.short = true; + } + return true; + } + }; + var seen: Seen = .{}; + _ = try drawOne(p, k.index(), .{ .ctx = &seen, .f = Seen.present }); + try testing.expect(seen.short); +} + +test "a view's step after another view's step moved the top, the two alike before it: it scrolls from its own top's row, as a core loaded from the session (none of its geometry) does" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + // Line 3 wrapped over several rows, line 10 over more than the body. + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..12) |i| switch (i) { + 3, 10 => { + for (0..@as(usize, if (i == 3) 60 else 500)) |w| try text.print(gpa, "w{d:0>4} ", .{w}); + try text.append(gpa, '\n'); + }, + else => try text.print(gpa, "line {d}\n", .{i}), + }; + const pane = try p.setTestFile(text.items); + const f = &pane.file.?; + // Both views' top on line 3's third row, the cursor on it. + pane.body.cur_row = 3; + pane.body.cur_col = 150; + f.scroll = 3; + f.scroll_row = 2; + p.sync(); + const k = p.openView().?; + // View 0's step: the cursor to line 10's end, the top into its rows. + pane.body.cur_row = 10; + pane.body.cur_col = @intCast(pardes.panes.File.sourceLine(pane, 10).len - 1); + pane.body.cur_pinned = true; + pane.ensureCursorVisible(p.settings.tab); + p.sync(); + try testing.expect(f.scroll == 10 and f.scroll_row > 2); + // The session as a client joining it loads it: no geometry made. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); + defer loaded.deinit(); + // View k's PageDown in each, from line 3's third row. + for ([_]*Pardes{ p, loaded }) |core| { + core.toView(k); + core.update(.{ .key = .{ .cp = pardes.Key.page_down } }); + } + const lf = &loaded.panes[0].?.file.?; + try testing.expectEqual(lf.scroll, f.scroll); + try testing.expectEqual(lf.scroll_row, f.scroll_row); +} + test "a view not live keeps its place in a text another view edits" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); -- cgit v1.3 From 8688c01e9d3ce7524d37e2add652a6168c77880a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 14:50:56 -0300 Subject: load: a view's top row is read back as the session holds it, never held to its line's bytes (row scroll round 4, fault 2: 'client 3's dump loaded as another session', once in the merge67b release soak): `File.State.scroll_row` was a `.place` held at a load to `lastRow` (the top line's bytes), but no step holds it so: an edit leaves a view's row where it was while its top line shrinks or another line becomes its top (`ParkedPlaces`, a twin's, the live pane's own), and every reader takes a row past its line's as the line's last (`top`/`topSkip`, the walks' `@min(t.row, rows - 1)`), so the SESSION holds such a row and a join's load cut it: another digest. TEST FIRST, failing on uxxwxpvt (the fault 1 fix): views.zig "a view's top on a wrapped line's third row, the line taken out by another view and the next one empty: its top row past its line's, read as its last, and the session's dump loads as the session" (the session's row 2 on an empty line, read as row 0; the loaded core's digest another); the place and `lastRow` deleted (a row of any size is read as its line's last by every reader: `topSkip`'s walk ends at the line's end; the digest and the key take it as a number); fault 1's stale `top_skip.row` read by a step (`scrollRows`' map path: a row of another view's line plus the notch) was a second road to such a row Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 15 ++++----------- core/views.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) (limited to 'core/views.zig') diff --git a/core/File.zig b/core/File.zig index 1547756f..07dfa52d 100644 --- a/core/File.zig +++ b/core/File.zig @@ -108,8 +108,10 @@ pub const State = struct { // none of their bytes, and takes back what a speculation wrote // (core/rollback.zig `Epoch.wrote`). .journal = .{ "content", "line_starts" }, - // Where the view's top is, held to its text (walk.zig `.place`). - .place = .{ .{ "scroll", "lastLine" }, .{ "scroll_row", "lastRow" } }, + // Where the view's top is, held to its text (walk.zig `.place`); + // its row in that line is any (an edit leaves it past the line's + // rows, read as the last: `top`), as the session holds it. + .place = .{.{ "scroll", "lastLine" }}, }; /// The last line a view's top may be at: its text's. A clean file a @@ -121,15 +123,6 @@ pub const State = struct { return modal.lineCount(f.content) -| 1; } - /// The last row of the top's line its top may be at: a row holds a byte - /// of it at least (its rows as wrapped are the view's, made after). - pub fn lastRow(f: *const State) usize { - if (f.content.len == 0) return 0; - const at = modal.lineStartOffset(f.content, f.scroll); - const end = std.mem.indexOfScalarPos(u8, f.content, at, '\n') orelse f.content.len; - return end - at; - } - path: []u8, content: []u8, /// The bytes `content`'s buffer has room for (0: as many as it holds): diff --git a/core/views.zig b/core/views.zig index 5ad9b749..e5f2038e 100644 --- a/core/views.zig +++ b/core/views.zig @@ -988,6 +988,46 @@ test "a view's step after another view's step moved the top, the two alike befor try testing.expectEqual(lf.scroll_row, f.scroll_row); } +test "a view's top on a wrapped line's third row, the line taken out by another view and the next one empty: its top row past its line's, read as its last, and the session's dump loads as the session" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..30) |i| switch (i) { + 3 => { + for (0..60) |w| try text.print(gpa, "w{d:0>4} ", .{w}); + try text.append(gpa, '\n'); + }, + 4 => try text.append(gpa, '\n'), + else => try text.print(gpa, "line {d}\n", .{i}), + }; + const pane = try p.setTestFile(text.items); + const f = &pane.file.?; + // View k: its top on line 3's third row, its cursor well inside the body. + pane.body.cur_row = 10; + f.scroll = 3; + f.scroll_row = 2; + p.sync(); + const k = p.openView().?; + // View 0 takes line 3 out: view k's top line is the empty one after it. + cursorTo(p, pane, 3); + typeKeys(p, "xd"); + p.toView(k); + // A step of view k's own: its cursor inside the band, its top kept. + escape(p); + try testing.expectEqual(@as(usize, 3), f.scroll); + try testing.expectEqual(@as(usize, 0), pardes.panes.File.sourceLine(pane, 3).len); + try testing.expectEqual(@as(u32, 2), f.scroll_row); + try testing.expectEqual(pardes.panes.File.Top{ .line = 3, .row = 0 }, pardes.panes.File.top(p.settings.tab, pane)); + // A client joining loads the session's dump as the session. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); + defer loaded.deinit(); + try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded)); +} + test "a view not live keeps its place in a text another view edits" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); -- cgit v1.3