From dfb051bd798f65c2217eea7f95b03dce292e30bf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 09:46:32 -0300 Subject: layers: `Changes [revset]` pins a revset's changes on a file's pane in this view (bare `@`, bare again off, another replaces; refusals in jj's words) and draws them: what jj says is asked in two recorded answers (Site.jj), `jj log -p --git --context=0` restricted to the file (the pins: id, author, time, the description's first line, and each one's diff, read with diff.zig's walk into hunks and the removed lines' text) and `jj file annotate` at the shown revision (which pin wrote each line), kept as flat arrays in the pane (Pane.changes: replicated, a late joiner has them in the dump, a client never runs jj) and worn by the view whose revset is File.Layers.revset; a pin's lane runs unbroken from a line it wrote to the next while every line between is a newer pin's, so lanes nest, the spine keeping its place and a nested change a level to the right on its rows only, three levels in a gutter the lanes widen (a window two cells, strips of a third of a cell with half that of air: Surface.lanes, drawn by the gui; a terminal three, a `▎` a level), coloured by the theme's accents but its green and red in hue order from the top, one within 15 degrees of the last kept skipped (doom one: magenta, blue, yellow), oldest first; a line a pin added whole (in a run it took away and wrote, past as many as it took away) is green across the line as a diff's added line; with Changes off nothing is drawn and the gutter is as it was; tests: the data through the pane (pins, lines, hunks, removed text, unpin, `@` touching nothing, a refusal), a client replaying it ends equal, the lanes and grounds of a three-deep stack in a terminal's cells and a window's strips; core-test 895/1055 (160 skipped), shell-test 1097/1100 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) (limited to 'core/File.zig') diff --git a/core/File.zig b/core/File.zig index 450e4ec9..f7d6bced 100644 --- a/core/File.zig +++ b/core/File.zig @@ -372,7 +372,13 @@ pub fn gutterWidth(pane: *const Pane) u16 { var lines = if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content); var digits: u16 = 1; while (lines >= 10) : (lines /= 10) digits += 1; - return @max(config.PREFIX_W, digits + 1); + return @max(config.PREFIX_W, digits + 1) + laneCells(pane); +} + +/// The cells the changes' lanes take after the line number in this view +/// (File.Layers), none while it pins no changes. +pub fn laneCells(pane: *const Pane) u16 { + return pane.layers.lanes; } /// Convert between rendered cells and UTF-8 byte columns. Tag rows always @@ -1756,7 +1762,7 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context.revision orelse 0, f.context.declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len, pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, - pane.view.rect_h, + pane.view.rect_h, laneCells(pane), }; // One hash of them laid out as words: every reader of the map asks for // this key, a hundred times a frame. @@ -1813,7 +1819,8 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w @memset(prefix, ' '); if (at == 0 and prefix.len > 0) { var lineno = row + 1; - var digit: usize = prefix.len - 1; + // The number, a blank, then the lanes (laneCells). + var digit: usize = prefix.len - 1 - laneCells(pane); if (row == end_slot) { prefix[digit - 1] = '~'; } else while (true) { @@ -1868,13 +1875,41 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w /// made without the language's ink: highlightDiffPlain). /// /// Both are painted in recolorSyntax's one walk over the cells, each where -/// it says (one walk per painter cost a frame a sixth more). Ruled, not -/// built (PARDES.md "Changes on the source view"): 3. changes: a lane after -/// the line number, a line added whole green across it, a changed line's -/// words tinted, ghost rows for what the shown revision has not; 4. churn: -/// a heat lane after the number and a strip by the scroll thumb. +/// it says (one walk per painter cost a frame a sixth more). +/// +/// 3. changes (`Changes [revset]`, jj.zig): while `revset` is set, a +/// lane a pinned change after the line number (the gutter `lanes` +/// cells wider), a line one added whole green across it (jj.paint). +/// Ruled, not built (PARDES.md "Changes on the source view"): a +/// changed line's words tinted, ghost rows for what the shown +/// revision has not. +/// 4. churn, ruled, not built: a heat lane after the number and a strip +/// by the scroll thumb. pub const Layers = struct { + pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; ink: bool = true, + /// The changes this view pins (jj's revset), none while empty; what jj + /// answered for them is the pane's (`Pane.changes`). + revset: [revset_max]u8 = undefined, + revset_len: u8 = 0, + /// The cells its lanes take while it pins any, 0 while it pins none + /// (laneCells): set as it pins them, from the view's frontend: in a + /// window two, which its strips of pixels fill one and a half of + /// (Surface.Lane); in a terminal three, a glyph each. + lanes: u8 = 0, + + pub const revset_max = 64; + + pub fn pinned(l: *const Layers) []const u8 { + return l.revset[0..l.revset_len]; + } + + /// One word while it pins nothing: a digest walks every view's layers + /// of every pane (field by field it cost a step 0.1 to 0.7% more). + pub fn digest(l: *const Layers, h: *pardes.walk.Hasher) void { + h.update(&.{ @intFromBool(l.ink), l.revset_len, l.lanes }); + if (l.revset_len > 0) h.update(l.pinned()); + } }; pub const SynStyle = struct { fg: [3]u8, bold: bool }; -- cgit v1.3 From 8152cf383c8296be29e80175b57e40f8d41049bf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 14:17:28 -0300 Subject: layers: with `Changes` off nothing is paid (P45): the revset and jj's answers are the session's, a list by pane serial (`Pardes.changes`, `jj.answered`; `jj.forget` at the pane's end), no longer a field of every pane, and a view holds only whether it shows them (`File.Layers.lanes`, the cells their lanes take, hashed with `ink` in one update), so a second view's `Changes other` shows the new set in both and its bare `Changes` hides them in its own alone (bare with none pinned yet is `@`); measured first, per operation against gpu/tree3 (a gui ReleaseFast pardes-perf, perfops's way of counting: reorg/pardes-layers-4/perf): merge50 +0.005% a client key, round 3 +0.110%, a packed one-byte `Layers` +0.391% (a packed field cost the walks more), a plain two-byte one +0.152%, the per-pane field gone +0.118%, the gutter's three reads of the lanes taken out +0.066% with body-appends from +0.189% to -0.004%: what paid was fillBody's line number, formatted for every row of every geometry pass (no `dst`, a hundred a frame) and thrown away, whose row loop moved with any value added to it: it is now written only into a `dst` (client key +0.017%, deep-sel +0.002%, render -0.021%, body-appends -1.555%); and `jj.of` (round 1's) gave a revision's name as a slice of a by-value copy of the pane's Output, dangling once it returned (jj told "Non-UTF-8 argument" for `Changes` in a revision's pane in a window build): the pane's own now; tests: the pinned changes are the pane's, shown in a view by its own bit (a second view's revset shows in both, its bare Changes hides them there alone), the two of a terminal's cells said so (`.windowed = false`); core-test 896/1056 (160 skipped), shell-test 1098/1101 (3 skipped), gui unit-test 1695/1699 (4 skipped); the frame and perf gate against tree3 (gpu/l4-g0): 77/77 frames, 0 differ, perfab corpus2x +0.005% (noise 0.058%), perfops 121 counts within 0.1% (largest terminal on all +0.082%) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 6 ++ core/File.zig | 75 +++++++++++------------- core/body_layer.zig | 2 +- core/builtins.zig | 2 +- core/jj.zig | 141 ++++++++++++++++++++++++++++++++------------- core/panes.zig | 9 +-- docs/typ/builtins-gui.json | 2 +- docs/typ/builtins-tui.json | 2 +- docs/typ/guide.typ | 5 +- 9 files changed, 150 insertions(+), 94 deletions(-) (limited to 'core/File.zig') diff --git a/core/Core.zig b/core/Core.zig index 385c3e74..177a6e78 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -355,6 +355,10 @@ lsp_ask: Ask = .{}, pipe: Pipe = .{}, /// A query pane's Grep, and its Find's listing, on a worker (Stream.zig). stream: Stream = .{}, +/// What jj answered for each pane's `Changes` (jj.zig), by its serial: the +/// session's list, not a field of every pane (a digest and a rollback copy +/// walk every pane's fields at every step). +changes: std.ArrayList(@import("jj.zig").Changes) = .empty, fs: filesystem.Namespace = .{}, boot: Boot = .shell, @@ -864,6 +868,8 @@ pub fn deinit(p: *Pardes) void { for (p.spawn_dirs) |d| if (d) |dir| gpa.free(dir); p.pipe.deinit(gpa); p.stream.deinit(gpa); + for (p.changes.items) |*c| c.deinit(gpa); + p.changes.deinit(gpa); p.handover.deinit(gpa); if (p.stream.ahead.len > 0) p.tree_sitter_gpa.free(p.stream.ahead); p.locations_cache.deinit(p.tree_sitter_gpa); diff --git a/core/File.zig b/core/File.zig index f7d6bced..6e44f85e 100644 --- a/core/File.zig +++ b/core/File.zig @@ -1777,6 +1777,8 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { if (record_wrap) pane.wrap_n = 0; const prefix_width = gutterWidth(pane); + // Where a number ends: a blank, then the lanes (laneCells), after it. + const number_end = prefix_width -| laneCells(pane) -| 1; const total = nlines(pane.gpa, f); // The slot after the text's last newline, found once: every numbered // row below asks whether it is that one. @@ -1814,24 +1816,25 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w pane.wrap_n = @intCast(i + 1); } if (row < total) { - var lbuf: [@max(config.PREFIX_W, 32)]u8 = undefined; - const prefix = lbuf[0..prefix_width]; - @memset(prefix, ' '); - if (at == 0 and prefix.len > 0) { - var lineno = row + 1; - // The number, a blank, then the lanes (laneCells). - var digit: usize = prefix.len - 1 - laneCells(pane); - if (row == end_slot) { - prefix[digit - 1] = '~'; - } else while (true) { - digit -= 1; - prefix[digit] = '0' + @as(u8, @intCast(lineno % 10)); - lineno /= 10; - if (lineno == 0) break; + // The number only where the text is wanted: a geometry pass (no + // `dst`, a hundred a frame) counts its cells and nothing more. + if (dst) |out| { + const prefix = out[written..][0..prefix_width]; + @memset(prefix, ' '); + if (at == 0 and prefix.len > 0) { + var lineno = row + 1; + var digit: usize = number_end; + if (row == end_slot) { + prefix[digit - 1] = '~'; + } else while (true) { + digit -= 1; + prefix[digit] = '0' + @as(u8, @intCast(lineno % 10)); + lineno /= 10; + if (lineno == 0) break; + } } } - if (dst) |out| @memcpy(out[written..][0..prefix.len], prefix); - written += prefix.len; + written += prefix_width; if (dst) |out| @memcpy(out[written..][0..guide.len], guide); written += guide.len; // A table row's pads are blanks before their bytes: the row @@ -1877,38 +1880,28 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w /// Both are painted in recolorSyntax's one walk over the cells, each where /// it says (one walk per painter cost a frame a sixth more). /// -/// 3. changes (`Changes [revset]`, jj.zig): while `revset` is set, a -/// lane a pinned change after the line number (the gutter `lanes` -/// cells wider), a line one added whole green across it (jj.paint). -/// Ruled, not built (PARDES.md "Changes on the source view"): a -/// changed line's words tinted, ghost rows for what the shown -/// revision has not. +/// 3. changes (`Changes [revset]`, jj.zig): while `lanes` is set, a +/// lane each of the pane's pinned changes after the line number (the +/// gutter `lanes` cells wider), a line one added whole green across +/// it (jj.paint). Ruled, not built (PARDES.md "Changes on the source +/// view"): a changed line's words tinted, ghost rows for what the +/// shown revision has not. /// 4. churn, ruled, not built: a heat lane after the number and a strip /// by the scroll thumb. +/// +/// Two bytes, hashed in one update: a digest takes every view's of every +/// pane at every step. pub const Layers = struct { - pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; ink: bool = true, - /// The changes this view pins (jj's revset), none while empty; what jj - /// answered for them is the pane's (`Pane.changes`). - revset: [revset_max]u8 = undefined, - revset_len: u8 = 0, - /// The cells its lanes take while it pins any, 0 while it pins none - /// (laneCells): set as it pins them, from the view's frontend: in a - /// window two, which its strips of pixels fill one and a half of - /// (Surface.Lane); in a terminal three, a glyph each. + /// Whether this view shows the pane's pinned changes (`Pardes.changes`: + /// the revset and jj's answers are the pane's), as the cells their + /// lanes take (laneCells), 0 while it does not: set from the view's + /// frontend, in a window two, which its strips of pixels fill one and + /// a half of (Surface.Lane), in a terminal three, a glyph each. lanes: u8 = 0, - pub const revset_max = 64; - - pub fn pinned(l: *const Layers) []const u8 { - return l.revset[0..l.revset_len]; - } - - /// One word while it pins nothing: a digest walks every view's layers - /// of every pane (field by field it cost a step 0.1 to 0.7% more). pub fn digest(l: *const Layers, h: *pardes.walk.Hasher) void { - h.update(&.{ @intFromBool(l.ink), l.revset_len, l.lanes }); - if (l.revset_len > 0) h.update(l.pinned()); + h.update(&.{ @intFromBool(l.ink), l.lanes }); } }; diff --git a/core/body_layer.zig b/core/body_layer.zig index 8d54760f..28b86f78 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -338,7 +338,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane // File layout is independent of whether its producer requests colors. if (pane.file) |*f| { drawGutter(p, s, pane, r, tx, tw, body_h, active); - if (pane.layers.revset_len > 0) @import("jj.zig").paint(p, s, pane, r, tx, tw, body_h); + if (pane.layers.lanes > 0) @import("jj.zig").paint(p, s, pane, r, tx, tw, body_h); panes.Query.paintMark(p, s, pane, r, body_h); if (pane.colorAlgo() != .none) panes.File.recolorSyntax(p, s, pane, f, r, tx, tw, body_h); diff --git a/core/builtins.zig b/core/builtins.zig index e059916a..77fc58ed 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1080,7 +1080,7 @@ pub const At = struct { pub const run = @import("jj.zig").word; }; -/// Pin the changes `revset` names (jj; bare, `@`) on a file's pane in this view: a lane each after the line number, a line one added whole green; bare again, unpinned. +/// Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green; bare, shown here or not (none pinned yet: `@`). pub const Changes = struct { pub const takes_arg = true; pub const run = @import("jj.zig").changesWord; diff --git a/core/jj.zig b/core/jj.zig index ebd2ea27..b4d119e8 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -111,7 +111,8 @@ fn refusal(gpa: std.mem.Allocator, why: []const u8) Shown { /// A revision's pane: the file it is of (its name without `@rev`) and the /// revision. Null for any other pane. pub fn of(f: *const File.State) ?struct { path: []const u8, rev: []const u8 } { - const o = f.output orelse return null; + // The pane's own, not a copy: the revision is a slice of it. + const o = if (f.output) |*o| o else return null; if (o.from != .revision) return null; return .{ .path = f.path[0 .. f.path.len - o.arg().len - 1], .rev = o.arg() }; } @@ -199,15 +200,19 @@ pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look // ---- `Changes [revset]` (PARDES.md: pinned changes as lanes): what jj // answers for it, kept as flat arrays in the pane ---- -/// What jj answered for a pane's `Changes`: the pane's replicated state (a -/// client never runs jj; one that joins late has it in the dump), drawn by -/// a view that pins the same revset (File.Layers). Two jj runs, each a +/// What jj answered for a pane's `Changes`: replicated state, the session's +/// (`Pardes.changes`, by the pane's serial: a client never runs jj; one that +/// joins late has it in the dump), its revset with it, drawn by each view that shows them (File.Layers.lanes: +/// a view holds that bit, not the revset; one set a pane, so a view's +/// `Changes other` replaces what every view shows). Two jj runs, each a /// site of the step's tape: the changes with their diffs of the file /// (`jj log -p --git`, the diffs read with diff.zig's walk), and who wrote /// each line of the shown revision (`jj file annotate`). pub const Changes = struct { pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; - revset: [File.Layers.revset_max]u8 = undefined, + /// The pane's (its serial). + serial: u32, + revset: [revset_max]u8 = undefined, revset_len: u8 = 0, /// The revset's changes that touch the file, oldest first. pins: []Pin = &.{}, @@ -223,14 +228,31 @@ pub const Changes = struct { gpa.free(c.lines); gpa.free(c.hunks); gpa.free(c.removed); - c.* = .{}; + c.* = .{ .serial = c.serial }; } pub fn pinned(c: *const Changes) []const u8 { return c.revset[0..c.revset_len]; } + + pub const revset_max = 64; }; +/// What jj answered for pane `serial`'s `Changes` (`Pardes.changes`), if anything. +pub fn answered(p: *Pardes, serial: u32) ?*Changes { + for (p.changes.items) |*c| if (c.serial == serial) return c; + return null; +} + +/// Pane `serial`'s answers let go (the pane's end). +pub fn forget(p: *Pardes, serial: u32) void { + for (p.changes.items, 0..) |*c, i| if (c.serial == serial) { + c.deinit(p.gpa); + _ = p.changes.swapRemove(i); + return; + }; +} + /// A pinned change, as `jj log` says it. pub const Pin = struct { pub const replica = .{ .len = .{ .{ "author", "author_len" }, .{ "title", "title_len" } } }; @@ -280,21 +302,27 @@ pub const Hunk = struct { }; /// The `Changes` word (builtins.zig registers it): in a file's pane (or a -/// revision's), the changes `revset` names (bare: `@`, the change against -/// its parent) pinned in this view; bare again, unpinned; another revset -/// replaces them. What jj refuses is said in its words. +/// revision's), the changes `revset` names pinned on the pane and shown in +/// this view; another revset replaces them (in every view that shows +/// them). Bare: shown in this view or not, the pane's pinned ones (none +/// yet: `@`, the change against its parent). What jj refuses is said in +/// its words. pub fn changesWord(c: pardes.builtins.Ctx) void { const p = c.p; const pane = c.pane; const f = if (pane.file) |*file| file else return p.reportFailure(c.id, "Changes: only a file's pane takes it"); const arg = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); - if (arg.len == 0 and pane.layers.revset_len > 0) { - pane.layers.revset_len = 0; + if (arg.len == 0 and pane.layers.lanes > 0) { pane.layers.lanes = 0; return; } + const cells: u8 = if (p.view().frontend.windowed) 2 else levels; + if (arg.len == 0 and answered(p, pane.serial) != null) { + pane.layers.lanes = cells; + return; + } const revset = if (arg.len == 0) "@" else arg; - if (revset.len > File.Layers.revset_max) return p.reportFailure(c.id, "Changes: a revset that long"); + if (revset.len > Changes.revset_max) return p.reportFailure(c.id, "Changes: a revset that long"); const shown: struct { path: []const u8, rev: []const u8 } = if (of(f)) |r| .{ .path = r.path, .rev = r.rev } else if (f.output == null and pardes.filesystem.localPath(f.path) != null) @@ -315,16 +343,15 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { } @memcpy(changes.revset[0..revset.len], revset); changes.revset_len = @intCast(revset.len); - const kept = pane.changes orelse p.gpa.create(Changes) catch { + changes.serial = pane.serial; + if (answered(p, pane.serial)) |kept| { + kept.deinit(p.gpa); + kept.* = changes.*; + } else p.changes.append(p.gpa, changes.*) catch { changes.deinit(p.gpa); return p.reportFailure(c.id, "Changes: out of memory"); }; - if (pane.changes != null) kept.deinit(p.gpa); - kept.* = changes.*; - pane.changes = kept; - @memcpy(pane.layers.revset[0..revset.len], revset); - pane.layers.revset_len = @intCast(revset.len); - pane.layers.lanes = if (p.view().frontend.windowed) 2 else levels; + pane.layers.lanes = cells; }, } } @@ -339,7 +366,7 @@ fn read(p: *Pardes, path: []const u8, rev: []const u8, revset: []const u8, text: const arena = p.scratch.allocator(); const set = try fileset(arena, path); const dir = dirOf(path); - var out: Changes = .{}; + var out: Changes = .{ .serial = 0 }; var kept = false; defer if (!kept) out.deinit(gpa); // The changes, oldest first, each with its diff of the file: one run. @@ -568,16 +595,16 @@ fn hueApart(a: [3]u8, b: [3]u8) f32 { return @min(d, 360 - d); } -/// The changes a view pins, on its body's rows (body_layer.zig, while -/// File.Layers pins any): in the gutter's lane cells after the number a +/// The pane's pinned changes as a view shows them, on its body's rows (body_layer.zig, while +/// File.Layers.lanes is set): in the gutter's lane cells after the number a /// lane each pin whose block runs down the row, at its level, a glyph in a /// terminal, a strip of pixels in a window (Surface.lanes); and a line a /// pin added whole green across it, as a diff's added line. Not yet: the /// ghost rows (what the shown revision has not: removed lines, each pin's /// description), a changed line's words. pub fn paint(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { - const ch = pane.changes orelse return; - if (ch.lines.len == 0 or !std.mem.eql(u8, ch.pinned(), pane.layers.pinned())) return; + const ch = answered(p, pane.serial) orelse return; + if (ch.lines.len == 0) return; var buf: [8][3]u8 = undefined; const colours = laneColours(p.theme(), &buf); if (colours.len == 0) return; @@ -759,11 +786,11 @@ test "a client replays the open of a file at a revision and ends equal" { try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); } -test "Changes pins a revset's changes in this view: who wrote each line, which it added whole, each one's hunks and what they took away; bare again unpins; jj's refusals are said" { +test "Changes pins a revset's changes on the pane, shown in this view: who wrote each line, which it added whole, each one's hunks and what they took away; bare hides and shows them; jj's refusals are said" { const gpa = testing.allocator; var repo = try Repo.init(gpa) orelse return error.SkipZigTest; defer repo.deinit(gpa); - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30, .windowed = false }); defer p.deinit(); var line: [4200]u8 = undefined; p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}@@-", .{repo.file}) }); @@ -771,8 +798,8 @@ test "Changes pins a revset's changes in this view: who wrote each line, which i const pane = p.panes[id].?; try testing.expectEqualStrings(Repo.two, pane.file.?.content); p.update(.{ .command = "Changes @--::@-" }); - try testing.expectEqualStrings("@--::@-", pane.layers.pinned()); - const ch = pane.changes.?; + try testing.expectEqual(levels, pane.layers.lanes); + const ch = answered(p, pane.serial).?; try testing.expectEqualStrings("@--::@-", ch.pinned()); try testing.expectEqual(@as(usize, 2), ch.pins.len); for (ch.pins, [_][]const u8{ "one", "two" }) |pin, title| { @@ -796,18 +823,53 @@ test "Changes pins a revset's changes in this view: who wrote each line, which i }; try testing.expectEqualSlices(Hunk, &hunks, ch.hunks); try testing.expectEqualStrings("const one = 1;\n", ch.removed); - // Bare: unpinned in this view; what jj said is kept for another view. + // Bare: not shown in this view; what jj said is kept on the pane. p.update(.{ .command = "Changes" }); - try testing.expectEqual(@as(u8, 0), pane.layers.revset_len); + try testing.expectEqual(0, pane.layers.lanes); try testing.expectEqual(@as(usize, 2), ch.pins.len); - // Bare again: `@`, the working copy's change, which (never snapshotted) touches nothing. + // Bare again: the pane's, shown again (jj not asked). p.update(.{ .command = "Changes" }); - try testing.expect(std.mem.indexOf(u8, told(p, id), "Changes: no change of `@` touches a.zig") != null); - try testing.expectEqual(@as(u8, 0), pane.layers.revset_len); - // jj's refusal, in its words. + try testing.expectEqual(levels, pane.layers.lanes); + try testing.expectEqual(ch, answered(p, pane.serial).?); + // jj's refusal, in its words; what was shown stays. p.update(.{ .command = "Changes nosuch" }); try testing.expect(std.mem.indexOf(u8, told(p, id), "Changes: Revision `nosuch` doesn't exist") != null); - try testing.expectEqual(@as(u8, 0), pane.layers.revset_len); + try testing.expectEqualStrings("@--::@-", answered(p, pane.serial).?.pinned()); + // `@`, the working copy's change, which (never snapshotted) touches nothing. + p.update(.{ .command = "Changes @" }); + try testing.expect(std.mem.indexOf(u8, told(p, id), "Changes: no change of `@` touches a.zig") != null); +} + +test "the pinned changes are the pane's, shown in a view by its own bit: another view's Changes shows its set in both, and its bare Changes hides them there alone" { + const gpa = testing.allocator; + var repo = try Repo.init(gpa) orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30, .windowed = false }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}@@-", .{repo.file}) }); + const pane = p.panes[p.view().active].?; + const before = File.gutterWidth(pane); + const other = p.openView().?; + p.update(.{ .command = "Changes @--::@-" }); + try testing.expectEqual(before + levels, File.gutterWidth(pane)); + // A view of its own: nothing shown there until it asks. + p.toView(other); + try testing.expectEqual(0, pane.layers.lanes); + try testing.expectEqual(before, File.gutterWidth(pane)); + p.update(.{ .command = "Changes @-" }); + try testing.expectEqualStrings("@-", answered(p, pane.serial).?.pinned()); + try testing.expectEqual(@as(usize, 1), answered(p, pane.serial).?.pins.len); + // The first view shows the new set, not nothing. + p.toView(.at(0)); + try testing.expectEqual(levels, pane.layers.lanes); + // Hidden in the other view alone. + p.toView(other); + p.update(.{ .command = "Changes" }); + try testing.expectEqual(0, pane.layers.lanes); + p.toView(.at(0)); + try testing.expectEqual(levels, pane.layers.lanes); + try testing.expectEqual(before + levels, File.gutterWidth(pane)); } test "a client replays Changes from what the session's jj answered and ends equal" { @@ -825,9 +887,10 @@ test "a client replays Changes from what the session's jj answered and ends equa r.settle(); try testing.expect(r.diverged == null); const id = a.core.view().active; - try testing.expectEqual(@as(usize, 2), a.core.panes[id].?.changes.?.pins.len); - try testing.expectEqualSlices(Line, a.core.panes[id].?.changes.?.lines, r.core().panes[id].?.changes.?.lines); - try testing.expectEqualStrings("@--::@-", r.core().panes[id].?.layers.pinned()); + try testing.expectEqual(@as(usize, 2), answered(a.core, a.core.panes[id].?.serial).?.pins.len); + try testing.expectEqualSlices(Line, answered(a.core, a.core.panes[id].?.serial).?.lines, answered(r.core(), r.core().panes[id].?.serial).?.lines); + try testing.expectEqualStrings("@--::@-", answered(r.core(), r.core().panes[id].?.serial).?.pinned()); + try testing.expect(r.core().panes[id].?.layers.lanes > 0); try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); } @@ -849,7 +912,7 @@ test "Changes draws a lane a pinned change after the line number, nested ones a const pane = p.panes[id].?; const before = File.gutterWidth(pane); p.update(.{ .command = "Changes ::@" }); - try testing.expectEqual(@as(usize, 3), pane.changes.?.pins.len); + try testing.expectEqual(@as(usize, 3), answered(p, pane.serial).?.pins.len); const cells: u16 = if (windowed) 2 else levels; try testing.expectEqual(before + cells, File.gutterWidth(pane)); p.sync(); diff --git a/core/panes.zig b/core/panes.zig index ee03ccf2..8b152132 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -320,9 +320,6 @@ pub const Pane = struct { view: SessionView = .{}, /// The painters a file's body wears in this view (File.Layers). layers: File.Layers = .{}, - /// What jj answered for the last `Changes` in any view (jj.zig): the - /// pane's, so a client that joins late has it in the dump. - changes: ?*@import("jj.zig").Changes = null, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -1282,11 +1279,7 @@ pub fn teardownPane(p: *Pardes, pane: *Pane) void { pane.input.deinit(p.gpa); pane.jump.deinit(p.gpa); pane.places.deinit(p.gpa); - if (pane.changes) |changes| { - changes.deinit(p.gpa); - p.gpa.destroy(changes); - pane.changes = null; - } + @import("jj.zig").forget(p, pane.serial); if (pane.query) |q| { q.deinit(p.gpa); p.gpa.destroy(q); diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index b019eaee..af6f98ae 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -10,7 +10,7 @@ {"name": "Callstack", "leader": "SPC l b", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Show outermost callers first, with signatures and call sites (gc)."}, {"name": "CallstackDepth", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "Caller levels collected by Callstack, 1 through 64; default 8. Bare, it says the current limit."}, {"name": "Changelog", "leader": "SPC h c", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "This build's version and what changed to reach it, printed into an output buffer the same way Config prints the live settings."}, - {"name": "Changes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj; bare, `@`) on a file's pane in this view: a lane each after the line number, a line one added whole green; bare again, unpinned."}, + {"name": "Changes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green; bare, shown here or not (none pinned yet: `@`)."}, {"name": "ClipPaste", "leader": "SPC p", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard after the selection."}, {"name": "ClipPasteBefore", "leader": "SPC P", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard before the selection."}, {"name": "ClipReplace", "leader": "SPC R", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Replace the selection with the system clipboard."}, diff --git a/docs/typ/builtins-tui.json b/docs/typ/builtins-tui.json index 9d613189..90dbf091 100644 --- a/docs/typ/builtins-tui.json +++ b/docs/typ/builtins-tui.json @@ -10,7 +10,7 @@ {"name": "Callstack", "leader": "SPC l b", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Show outermost callers first, with signatures and call sites (gc)."}, {"name": "CallstackDepth", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "Caller levels collected by Callstack, 1 through 64; default 8. Bare, it says the current limit."}, {"name": "Changelog", "leader": "SPC h c", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "This build's version and what changed to reach it, printed into an output buffer the same way Config prints the live settings."}, - {"name": "Changes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj; bare, `@`) on a file's pane in this view: a lane each after the line number, a line one added whole green; bare again, unpinned."}, + {"name": "Changes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green; bare, shown here or not (none pinned yet: `@`)."}, {"name": "ClipPaste", "leader": "SPC p", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard after the selection."}, {"name": "ClipPasteBefore", "leader": "SPC P", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Paste the system clipboard before the selection."}, {"name": "ClipReplace", "leader": "SPC R", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Replace the selection with the system clipboard."}, diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index db428d7f..6216b364 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -296,8 +296,9 @@ it last saw it (`@`): pardes never snapshots it. #word("Syntax") (#key("SPC t h")) turns a text's syntax colours off in this view, and on again. #word("Changes") with a revset pins its changes on a file's pane (or a -revision's) in this view; bare, `@`, the change against its parent, and -bare again, off. Each change that touched the file gets a lane after the +revision's) and shows them in this view (one set a pane: another view's +revset replaces it there too); bare, they are hidden in this view or shown +again, and with none pinned yet it is `@`, the change against its parent. Each change that touched the file gets a lane after the line number, from its first line to its last, in the theme's accents in hue order, oldest first; a change that rewrote lines inside another's steps a lane to the right on those rows; a line one added whole is green -- cgit v1.3 From 2b9da7be7d9520701b24d46d8db2ec56517b45b8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 15:15:41 -0300 Subject: layers: the lanes tell the truth about the text in the pane: jj annotates its last snapshot (`--ignore-working-copy`), not the keys typed since, so annotate's answer now carries each line's text (`id\tcontent`, the same one run) and `jj.provenance` lines the annotated text up with the pane's by what the two agree on at either end (`File.spanOf`, the editor's differ; the one line its two ends meet inside counts when its bytes agree: an inserted line shares its indent with the line after it), a line only the pane has nobody's (no lane, no ground); and an edit after `Changes` carries the per-line values with the lines (`jj.edited`: one call in `File.edit`, the one place a text changes, an undo's too, made only while the session has pinned changes): the lines a splice takes out take their values with them, a line it puts in is nobody's, and a splice that starts in a line's leading blanks (as `o` puts a line in) leaves that line's value after what it puts in; jj is never asked again on a key; tests: a line on disk jj has not snapshotted is nobody's and the lines after it keep their changes; a line typed in a nested block (`o`) is nobody's and the rows around keep their lanes, its undo puts the values back, a deleted line takes its value, a client replaying the keys ends equal; core-test 901/1061 (160 skipped), shell-test 1103/1106 (3 skipped) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 2 + core/jj.zig | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 192 insertions(+), 16 deletions(-) (limited to 'core/File.zig') diff --git a/core/File.zig b/core/File.zig index 6e44f85e..ae052344 100644 --- a/core/File.zig +++ b/core/File.zig @@ -756,6 +756,8 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who }; }; reportEdit(p, f, off, len, bytes); + // A pinned change's per-line values ride with the lines (jj.zig). + if (p.changes.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes); if (f.mini) |*mini| mini.deinit(p.gpa); f.mini = null; if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes); diff --git a/core/jj.zig b/core/jj.zig index 5f86dbe6..a381443c 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -460,7 +460,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc if (!any) return .{ .gen = a.gen, .jj = a.serial, .rows = logged, .end = .done }; // Who wrote each line: a path, not a fileset (annotate takes one file // as it is named). - const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(a.path) }; + const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\t\" ++ content", "--", std.Io.Dir.path.basename(a.path) }; return switch (runJj(p, gpa, dirOf(a.path), &annotate_args)) { .refused => |why| { gpa.free(logged); @@ -497,6 +497,50 @@ pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { pinAnswer(p, id, b, revset[0..revset_len]); } +/// A splice of `f`'s text, before its bytes go in (File.edit, the one +/// place a text changes, an undo's too): each pane showing it that has +/// pins keeps a value for each of its lines, so the values ride with the +/// lines, never asking jj again: the lines a splice takes out take their +/// values with them, and a line it puts in is nobody's. +pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: []const u8) void { + var i = p.changes.items.len; + while (i > 0) { + i -= 1; + const c = &p.changes.items[i]; + const id = p.paneBySerial(c.serial) orelse continue; + const shown = if (p.panes[id].?.file) |*shown| shown else continue; + if (shown.content.ptr != f.content.ptr) continue; + splice(p.gpa, c, f.content, off, len, bytes) catch { + // No room for it: its values would lie, so it goes. + c.deinit(p.gpa); + _ = p.changes.swapRemove(i); + }; + } +} + +/// `c`'s values spliced as `text`'s lines are by the splice of `len` bytes +/// at `off` for `bytes`. A splice that starts in a line's leading blanks +/// (at its start, or after its indent, as `o` puts a line in) leaves that +/// line's text after what it puts in: the lines it takes and puts come +/// before it. Else the line it starts in keeps its value, and the lines +/// taken and put come after it. +fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len: usize, bytes: []const u8) error{OutOfMemory}!void { + const taken = std.mem.count(u8, text[off..][0..len], "\n"); + const put = std.mem.count(u8, bytes, "\n"); + if (taken == 0 and put == 0) return; + const line_start = if (std.mem.lastIndexOfScalar(u8, text[0..off], '\n')) |nl| nl + 1 else 0; + const in_lead = std.mem.trimStart(u8, text[line_start..off], " \t").len == 0; + const from = std.mem.count(u8, text[0..line_start], "\n") + @intFromBool(!in_lead); + if (from > c.lines.len) return; + const take = @min(taken, c.lines.len - from); + const next = try gpa.alloc(Line, c.lines.len - take + put); + @memcpy(next[0..from], c.lines[0..from]); + @memset(next[from..][0..put], .{ .pin = .none }); + @memcpy(next[from + put ..], c.lines[from + take ..]); + gpa.free(c.lines); + c.lines = next; +} + /// The session's end: the worker canceled, the asks let go. pub fn cancel(s: *pardes.Stream) void { if (s.changes) |t| { @@ -573,7 +617,7 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a kept = true; return .{ .read = out }; } - out.lines = try provenance(gpa, annotated, out.pins); + out.lines = try provenance(gpa, arena, annotated, out.pins, text); // Each change's hunks, and which of its lines it added whole. var hunks: std.ArrayList(Hunk) = .empty; defer hunks.deinit(gpa); @@ -608,22 +652,67 @@ fn pinOf(l: []const u8) ?Pin { return pin; } -/// Which pin wrote each line: a value a line, from `annotated` -/// (`annotate`'s answer); `whole` is the hunks' to say. The caller's, in -/// `gpa`. -pub fn provenance(gpa: std.mem.Allocator, annotated: []const u8, pins: []const Pin) error{OutOfMemory}![]Line { - var out: std.ArrayList(Line) = .empty; - errdefer out.deinit(gpa); - var lines = std.mem.splitScalar(u8, annotated, '\n'); - while (lines.next()) |id| { +/// Which pin wrote each line of `text` (what the pane shows: `whole` is the +/// hunks' to say), a value a line, the caller's in `gpa`, from `annotated` +/// (`jj file annotate`'s answer: an id, a tab and the line, a line each, of +/// the revision it annotated, which is jj's last snapshot of a file and +/// not the keys typed since): the two texts lined up by what they agree on +/// at either end (`File.spanOf`, the editor's differ: a line inside both +/// ends keeps its owner, a line only the pane has is nobody's). +pub fn provenance(gpa: std.mem.Allocator, arena: std.mem.Allocator, annotated: []const u8, pins: []const Pin, text: []const u8) error{OutOfMemory}![]Line { + var owners: std.ArrayList(PinIndex) = .empty; + var was: std.ArrayList(u8) = .empty; + var rows = std.mem.splitScalar(u8, annotated, '\n'); + while (rows.next()) |row| { // The empty text after the last newline is no line. - if (id.len == 0 and lines.peek() == null) break; - const at: PinIndex = for (pins, 0..) |pin, i| { - if (std.mem.eql(u8, &pin.id, id)) break @enumFromInt(i); - } else .none; - try out.append(gpa, .{ .pin = at }); + if (row.len == 0 and rows.peek() == null) break; + const tab = std.mem.indexOfScalar(u8, row, '\t') orelse row.len; + try owners.append(arena, for (pins, 0..) |pin, i| { + if (std.mem.eql(u8, &pin.id, row[0..tab])) break @enumFromInt(i); + } else .none); + try was.appendSlice(arena, row[@min(tab + 1, row.len)..]); + try was.append(arena, '\n'); + } + if (!std.mem.endsWith(u8, annotated, "\n") and was.items.len > 0) was.items.len -= 1; + const out = try gpa.alloc(Line, lineCount(text)); + @memset(out, .{ .pin = .none }); + if (std.mem.eql(u8, was.items, text)) { + for (out[0..@min(out.len, owners.items.len)], owners.items[0..@min(out.len, owners.items.len)]) |*l, pin| l.pin = pin; + return out; } - return out.toOwnedSlice(gpa); + const span = File.spanOf(was.items, text); + const head = @min(std.mem.count(u8, was.items[0..span.head], "\n"), out.len, owners.items.len); + for (out[0..head], owners.items[0..head]) |*l, pin| l.pin = pin; + var tail = @min(tailLines(was.items, span.tail), out.len - head, owners.items.len - head); + // The line the two ends meet inside (an insertion before it shares its + // indent with the line above) is the same line when its bytes are. + if (head + tail < @min(out.len, owners.items.len) and std.mem.eql(u8, lineFromEnd(was.items, tail), lineFromEnd(text, tail))) tail += 1; + for (out[out.len - tail ..], owners.items[owners.items.len - tail ..]) |*l, pin| l.pin = pin; + return out; +} + +/// `text`'s line `k` from its end (0: its last). +fn lineFromEnd(text: []const u8, k: usize) []const u8 { + var end = text.len - @intFromBool(std.mem.endsWith(u8, text, "\n")); + for (0..k) |_| end = (std.mem.lastIndexOfScalar(u8, text[0..end], '\n') orelse return ""); + const from = if (std.mem.lastIndexOfScalar(u8, text[0..end], '\n')) |nl| nl + 1 else 0; + return text[from..end]; +} + +/// The lines of `text`: one after each newline but a last one, as the +/// annotation numbers them. +fn lineCount(text: []const u8) usize { + return std.mem.count(u8, text, "\n") + @intFromBool(text.len > 0 and text[text.len - 1] != '\n'); +} + +/// How many of `text`'s lines lie whole in its last `tail` bytes. +fn tailLines(text: []const u8, tail: usize) usize { + if (tail == 0) return 0; + const from = text.len - tail; + // A line starts at `from`, and after each newline in the tail but the + // text's last byte. + const starts = if (from == 0) 1 else @intFromBool(text[from - 1] == '\n'); + return starts + std.mem.count(u8, text[from .. text.len - 1], "\n"); } /// Pin `k`'s hunks in `diffed` (its diff of the file, `--git --context=0`) onto @@ -1234,6 +1323,91 @@ test "Changes draws a lane a pinned change after the line number, nested ones a } } +/// A function three changes wrote: `fn` all of it, `mid` its lines 3-6 +/// (rewriting two, adding two), `shift` line 4 again. +const Stack = struct { + const fn_text = "fn f() void {\n a();\n b();\n c();\n g();\n h();\n i();\n}\n"; + const mid_text = "fn f() void {\n a();\n b2();\n c2();\n x();\n y();\n g();\n h();\n i();\n}\n"; + const shift_text = "fn f() void {\n a();\n b2();\n c3();\n x();\n y();\n g();\n h();\n i();\n}\n"; + /// Who wrote each of shift_text's lines: fn 0, mid 1, shift 2. + const pins = [_]u16{ 0, 0, 1, 2, 1, 1, 0, 0, 0, 0 }; + + fn repo(working: ?[]const u8) !?Repo { + return Repo.with(testing.allocator, &.{ .{ fn_text, "fn" }, .{ mid_text, "mid" }, .{ shift_text, "shift" } }, working); + } +}; + +fn pinsOf(ch: *const Changes, out: []u16) []u16 { + for (ch.lines, out[0..ch.lines.len]) |l, *o| o.* = @intFromEnum(l.pin); + return out[0..ch.lines.len]; +} + +test "Changes lines jj's annotation up with the pane's text: a line jj has not seen (typed, not snapshotted) is nobody's, the lines after it keep their changes" { + // The file on disk has a line more than jj's last snapshot of it. + const working = "fn f() void {\n a();\n b2();\n c3();\n x();\n new();\n y();\n g();\n h();\n i();\n}\n"; + var repo = try Stack.repo(working) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 24, .windowed = false }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + const pane = p.panes[p.view().active].?; + try testing.expectEqualStrings(working, pane.file.?.content); + p.update(.{ .command = "Changes ::@" }); + const none = @intFromEnum(PinIndex.none); + var got: [16]u16 = undefined; + try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, 1, none, 1, 0, 0, 0, 0 }, pinsOf(answered(p, pane.serial).?, &got)); +} + +test "the lanes ride with the text: a line typed in a nested block is nobody's, its undo puts the values back, a deleted line takes its value; a client replays it equal" { + var repo = try Stack.repo(null) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + const R = pardes.replica; + const a = try R.Authority.init(testing.allocator, .{ .cols = 80, .rows = 24 }, .{}, false); + defer a.deinit(); + const r = try R.Replica.init(testing.allocator, 1, try a.unit(0, 0), false); + defer r.deinit(); + const Keys = struct { + fn send(au: *R.Authority, re: *R.Replica, keys: []const u8) !void { + for (keys) |k| { + const ev: pardes.Event = if (k == 27) .{ .key = .{ .cp = pardes.Key.escape } } else .{ .key = .{ .cp = k, .text = &[_]u8{k} } }; + re.confirm(try au.local(ev)); + } + re.settle(); + } + }; + var line: [4200]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) })); + r.confirm(try a.local(.{ .command = "Changes ::@" })); + r.settle(); + const serial = a.core.panes[a.core.view().active].?.serial; + const ch = answered(a.core, serial).?; + var got: [16]u16 = undefined; + try testing.expectEqualSlices(u16, &Stack.pins, pinsOf(ch, &got)); + const before = try testing.allocator.dupe(Line, ch.lines); + defer testing.allocator.free(before); + const none = @intFromEnum(PinIndex.none); + // A line opened below `c3();` (line 4, inside mid's block): nobody's; + // the rows around keep their lanes. + r.confirm(try a.local(.{ .command = "Look a.zig:4" })); + try Keys.send(a, r, "oz\x1b"); + try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, none, 1, 1, 0, 0, 0, 0 }, pinsOf(answered(a.core, serial).?, &got)); + try testing.expectEqual(before[3].lanes, answered(a.core, serial).?.lines[3].lanes); + try testing.expectEqual(before[4].lanes, answered(a.core, serial).?.lines[5].lanes); + // Undone: the values as they were. + try Keys.send(a, r, "u"); + try testing.expectEqualSlices(Line, before, answered(a.core, serial).?.lines); + // `x();` (line 5, mid's) deleted: its value goes with it. + r.confirm(try a.local(.{ .command = "Look a.zig:5" })); + try Keys.send(a, r, ";xd"); + try testing.expectEqualStrings(Stack.shift_text[0..41] ++ Stack.shift_text[50..], a.core.panes[a.core.view().active].?.file.?.content); + try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, 1, 0, 0, 0, 0 }, pinsOf(answered(a.core, serial).?, &got)); + try testing.expectEqual(before[5].lanes, answered(a.core, serial).?.lines[4].lanes); + try testing.expect(r.diverged == null); + try testing.expectEqualSlices(Line, answered(a.core, serial).?.lines, answered(r.core(), serial).?.lines); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); +} + test "a B3 click on path@rev:line in running text opens the file at that revision there" { const gpa = testing.allocator; var repo = try Repo.init(gpa) orelse return error.SkipZigTest; -- cgit v1.3 From 71a68eac9d4bce7deb19236a9cc8338b1a37caa1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 17:31:25 -0300 Subject: layers: with `Changes` off a client's key pays for items A and B no more: all of jj's session state is one field, `Pardes.changes: jj.Session` (jj's answers by pane serial, the `Changes` jj runs for, the one asked meanwhile, how many were asked), whose digest is nothing until a `Changes` is asked and then one update while nothing is asked of jj, where item A had three `Stream` fields (three updates in every digest of the session, and a client's key takes several digests) beside round 4's answers list (one more); `Stream` holds nothing of jj's again (its dispatch lines stay), the session's end lets the worker and the asks go in `Session.deinit`, and the batch's 8-byte `jj` field goes (a `Changes` answer is told by its generation's bit, `jj.gen_bit`, after a paint's, and the pane it is for is the ask's, which every replica holds); measured first, a variant at a time, client-key-down against tree3's pardes-perf in the gate's own layout (ReleaseFast gui, its farms and HOME; my first script's layout read about 0.05% lower): item B as it was small +0.111%, the batch's field gone and the stream's three fields one +0.104%, the session's state one field digested as nothing until asked small +0.047%, large +0.047%, medium +0.010% Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 10 ++--- core/File.zig | 2 +- core/Stream.zig | 15 ++----- core/jj.zig | 125 +++++++++++++++++++++++++++++++++----------------------- 4 files changed, 83 insertions(+), 69 deletions(-) (limited to 'core/File.zig') diff --git a/core/Core.zig b/core/Core.zig index 177a6e78..efa2ac3d 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -355,10 +355,11 @@ lsp_ask: Ask = .{}, pipe: Pipe = .{}, /// A query pane's Grep, and its Find's listing, on a worker (Stream.zig). stream: Stream = .{}, -/// What jj answered for each pane's `Changes` (jj.zig), by its serial: the -/// session's list, not a field of every pane (a digest and a rollback copy -/// walk every pane's fields at every step). -changes: std.ArrayList(@import("jj.zig").Changes) = .empty, +/// The panes' `Changes` (jj.zig): what was asked of jj, and what it +/// answered for each pane, by its serial: the session's, not a field of +/// every pane (a digest and a rollback copy walk every pane's fields at +/// every step), and nothing in a digest until a `Changes` is asked. +changes: @import("jj.zig").Session = .{}, fs: filesystem.Namespace = .{}, boot: Boot = .shell, @@ -868,7 +869,6 @@ pub fn deinit(p: *Pardes) void { for (p.spawn_dirs) |d| if (d) |dir| gpa.free(dir); p.pipe.deinit(gpa); p.stream.deinit(gpa); - for (p.changes.items) |*c| c.deinit(gpa); p.changes.deinit(gpa); p.handover.deinit(gpa); if (p.stream.ahead.len > 0) p.tree_sitter_gpa.free(p.stream.ahead); diff --git a/core/File.zig b/core/File.zig index ae052344..8749417f 100644 --- a/core/File.zig +++ b/core/File.zig @@ -757,7 +757,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who }; reportEdit(p, f, off, len, bytes); // A pinned change's per-line values ride with the lines (jj.zig). - if (p.changes.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes); + if (p.changes.answers.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes); if (f.mini) |*mini| mini.deinit(p.gpa); f.mini = null; if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes); diff --git a/core/Stream.zig b/core/Stream.zig index 80812840..1d061b52 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -74,11 +74,9 @@ pub const Batch = struct { /// A paint's answer (`paintAhead`): the colours of pane `serial`'s text /// at `revision` from byte `start`, one syntax.Syn a byte. painted: ?struct { serial: u32, revision: u32, start: usize } = null, + /// Or, with a `Changes` generation (jj.gen_bit), jj's annotate (jj's + /// log in `rows`, or its refusal and `end` failed). styles: []u8 = &.{}, - /// A `Changes` answer (jj.zig) for pane `jj`, by serial: jj's log in - /// `rows` and its annotate in `styles`, or its refusal in `rows` and - /// `end` failed. - jj: ?u32 = null, pub const End = enum { done, failed }; @@ -259,14 +257,7 @@ rows_asked_at: u8 = 0, /// Rows asked for their colours while a 9P write was served: asked again /// on the next step (Query.produceAll). rows_later: bool = false, -/// The `Changes` asked of jj and its worker, the one asked while it ran, -/// and how many were asked (jj.zig: one at a time, the last one pinned). -changes: ?jj.Task = null, -changes_next: ?*jj.Ask = null, -changes_count: u32 = 0, - pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { - jj.cancel(s); s.cancelTask(); s.cancelParse(); s.cancelPaint(); @@ -700,8 +691,8 @@ const Collect = struct { pub fn receive(p: *Pardes, b: *const Batch) void { const s = &p.stream; if (b.parsed) |parsed| return receiveParse(p, b, parsed.serial, parsed.revision); - if (b.jj) |serial| return jj.receive(p, b, serial); if (b.painted != null) return receivePaint(p, b); + if (b.gen & jj.gen_bit != 0) return jj.receive(p, b); if (b.gen != s.gen or (s.state != .searching and s.state != .capped)) { s.dropped += 1; return; diff --git a/core/jj.zig b/core/jj.zig index a381443c..0cdf94a1 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -241,15 +241,15 @@ pub const Changes = struct { /// What jj answered for pane `serial`'s `Changes` (`Pardes.changes`), if anything. pub fn answered(p: *Pardes, serial: u32) ?*Changes { - for (p.changes.items) |*c| if (c.serial == serial) return c; + for (p.changes.answers.items) |*c| if (c.serial == serial) return c; return null; } /// Pane `serial`'s answers let go (the pane's end). pub fn forget(p: *Pardes, serial: u32) void { - for (p.changes.items, 0..) |*c, i| if (c.serial == serial) { + for (p.changes.answers.items, 0..) |*c, i| if (c.serial == serial) { c.deinit(p.gpa); - _ = p.changes.swapRemove(i); + _ = p.changes.answers.swapRemove(i); return; }; } @@ -339,7 +339,7 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { /// A `Changes` asked of jj: the pane (by serial), the file it shows and the /// revision whose lines are annotated, the revset. Its two runs go on a /// worker where the host has one (the `search` effect: Stream.start), their -/// raw answers back as one input (`Event.search`, its `jj` set) stamped +/// raw answers back as one input (`Event.search`) stamped /// with its generation, which the step takes (`receive`): a replica replays /// that input and never runs jj. With no worker (a test), on the loop, each /// run a site of the step's tape. @@ -367,9 +367,44 @@ pub const Ask = struct { } }; -/// The `Changes` asked and its worker (`Stream.changes`), one at a time: -/// one asked while it runs waits (`Stream.changes_next`), and only the last -/// asked is pinned. +/// The session's `Changes` (`Pardes.changes`): jj's answers, a pane's by +/// its serial; the `Changes` jj runs for, one at a time, the one asked +/// while it runs (only the last asked is pinned), and how many were asked +/// (the generation). In every digest of the session: nothing until a +/// `Changes` is asked, then one update while nothing is asked of jj. +pub const Session = struct { + answers: std.ArrayList(Changes) = .empty, + task: ?Task = null, + next: ?*Ask = null, + count: u32 = 0, + + pub fn digest(c: *const Session, h: *pardes.walk.Hasher) void { + if (c.count == 0) return; + const bits: u64 = c.count | @as(u64, @intFromBool(c.task != null)) << 32 | @as(u64, @intFromBool(c.next != null)) << 33; + h.update(std.mem.asBytes(&bits)); + pardes.walk.hash(std.ArrayList(Changes), &c.answers, h); + if (c.task) |*t| pardes.walk.hash(Task, t, h); + if (c.next) |n| pardes.walk.hash(Ask, n, h); + } + + /// The session's end: the worker canceled, the asks and the answers + /// let go. + pub fn deinit(c: *Session, gpa: std.mem.Allocator) void { + if (c.task) |t| { + c.task = null; + if (t.future) |f| pardes.Stream.join(f, t.io.?, .cancel); + t.ask.deinit(); + } + if (c.next) |a| { + c.next = null; + a.deinit(); + } + for (c.answers.items) |*a| a.deinit(gpa); + c.answers.deinit(gpa); + } +}; + +/// The `Changes` jj runs for, and its worker. pub const Task = struct { // The worker the host started for it, live: a replica has none. pub const replica = .{ .authority = .{ "future", "io" } }; @@ -382,27 +417,26 @@ pub const Task = struct { /// from 0; a paint's has the top one). pub const gen_bit: u32 = 1 << 29; -fn liveGen(s: *const pardes.Stream) u32 { - return gen_bit | (s.changes_count & (gen_bit - 1)); +fn liveGen(c: *const Session) u32 { + return gen_bit | (c.count & (gen_bit - 1)); } /// Ask jj of `revset` for pane `serial` (said at pane `id` while it runs): /// what the pane shows stays until the answer lands. fn ask(p: *Pardes, id: usize, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) void { - const s = &p.stream; - s.changes_count +%= 1; - const a = Ask.make(p.gpa, liveGen(s), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); + p.changes.count +%= 1; + const a = Ask.make(p.gpa, liveGen(&p.changes), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); p.setStatus(.at(id), "Changes: jj\u{2026}"); - if (s.changes != null) { - if (s.changes_next) |old| old.deinit(); - s.changes_next = a; + if (p.changes.task != null) { + if (p.changes.next) |old| old.deinit(); + p.changes.next = a; return; } go(p, a); } fn go(p: *Pardes, a: *Ask) void { - p.stream.changes = .{ .ask = a }; + p.changes.task = .{ .ask = a }; if (p.tape.has(p, .search)) return p.emit(.{ .search = .{ .gen = a.gen } }); runHere(p, a.gen, false); } @@ -410,7 +444,7 @@ fn go(p: *Pardes, a: *Ask) void { /// The host's `search` effect for a `Changes` generation (Stream.start): /// its runs on a worker that sends to `sink`. pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: pardes.Stream.Sink) void { - const t = if (p.stream.changes) |*t| t else return; + const t = if (p.changes.task) |*t| t else return; if (t.ask.gen != gen or t.future != null) return; t.ask.out = gpa; // No worker to be had: here, its answer an input. @@ -427,11 +461,11 @@ fn work(a: *Ask, ctx: ?*anyopaque, sink: pardes.Stream.Sink) void { /// `in_call`: in the host's `search` entry, the answer handed in as an /// input, as a worker's is. pub fn runHere(p: *Pardes, gen: u32, comptime in_call: bool) void { - const t = p.stream.changes orelse return; + const t = p.changes.task orelse return; if (t.ask.gen != gen or t.future != null) return; const b = answer(p, p.gpa, t.ask); defer b.deinit(p.gpa); - if (in_call) p.update(.{ .search = b }) else receive(p, &b, t.ask.serial); + if (in_call) p.update(.{ .search = b }) else receive(p, &b); } /// jj's two answers for `a`, raw, in `gpa`: through `p`'s tape when given @@ -440,7 +474,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc const failed = struct { fn f(g: std.mem.Allocator, at: *const Ask, why: []u8) pardes.Stream.Batch { _ = g; - return .{ .gen = at.gen, .jj = at.serial, .rows = why, .end = .failed }; + return .{ .gen = at.gen, .rows = why, .end = .failed }; } }.f; const set = fileset(gpa, a.path) catch return failed(gpa, a, refusal(gpa, "out of memory").refused); @@ -457,7 +491,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc const any = while (it.next()) |l| { if (pinOf(l) != null) break true; } else false; - if (!any) return .{ .gen = a.gen, .jj = a.serial, .rows = logged, .end = .done }; + if (!any) return .{ .gen = a.gen, .rows = logged, .end = .done }; // Who wrote each line: a path, not a fileset (annotate takes one file // as it is named). const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\t\" ++ content", "--", std.Io.Dir.path.basename(a.path) }; @@ -466,7 +500,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc gpa.free(logged); return failed(gpa, a, why); }, - .bytes => |annotated| .{ .gen = a.gen, .jj = a.serial, .rows = logged, .styles = annotated, .end = .done }, + .bytes => |annotated| .{ .gen = a.gen, .rows = logged, .styles = annotated, .end = .done }, }; } @@ -474,25 +508,27 @@ fn runJj(p: ?*Pardes, gpa: std.mem.Allocator, dir: []const u8, args: []const []c return if (p) |q| q.tape.site(.jj, gpa, runHost, .{ gpa, dir, args }) else runHost(gpa, dir, args); } -/// A `Changes` answer (`Event.search`, its `jj` set), borrowed: the last -/// asked's is parsed and pinned on its pane; an earlier one's, or a gone -/// pane's, dropped. Then the one asked meanwhile goes. -pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { - const s = &p.stream; +/// A `Changes` answer (`Event.search` with a `Changes` generation), +/// borrowed: the last asked's is parsed and pinned on its pane (the ask +/// names it); an earlier one's, or a gone pane's, dropped. Then the one +/// asked meanwhile goes. +pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch) void { var revset: [Changes.revset_max]u8 = undefined; var revset_len: usize = 0; - if (s.changes) |t| if (t.ask.gen == b.gen) { - s.changes = null; + var serial: u32 = 0; + if (p.changes.task) |t| if (t.ask.gen == b.gen) { + p.changes.task = null; if (t.future) |f| pardes.Stream.join(f, t.io.?, .wait); + serial = t.ask.serial; revset_len = t.ask.revset.len; @memcpy(revset[0..revset_len], t.ask.revset); t.ask.deinit(); }; - defer if (s.changes == null) if (s.changes_next) |next| { - s.changes_next = null; + defer if (p.changes.task == null) if (p.changes.next) |next| { + p.changes.next = null; go(p, next); }; - if (b.gen != liveGen(s) or revset_len == 0) return; + if (b.gen != liveGen(&p.changes) or revset_len == 0) return; const id = p.paneBySerial(serial) orelse return; pinAnswer(p, id, b, revset[0..revset_len]); } @@ -503,17 +539,17 @@ pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { /// lines, never asking jj again: the lines a splice takes out take their /// values with them, and a line it puts in is nobody's. pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: []const u8) void { - var i = p.changes.items.len; + var i = p.changes.answers.items.len; while (i > 0) { i -= 1; - const c = &p.changes.items[i]; + const c = &p.changes.answers.items[i]; const id = p.paneBySerial(c.serial) orelse continue; const shown = if (p.panes[id].?.file) |*shown| shown else continue; if (shown.content.ptr != f.content.ptr) continue; splice(p.gpa, c, f.content, off, len, bytes) catch { // No room for it: its values would lie, so it goes. c.deinit(p.gpa); - _ = p.changes.swapRemove(i); + _ = p.changes.answers.swapRemove(i); }; } } @@ -541,19 +577,6 @@ fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len c.lines = next; } -/// The session's end: the worker canceled, the asks let go. -pub fn cancel(s: *pardes.Stream) void { - if (s.changes) |t| { - s.changes = null; - if (t.future) |f| pardes.Stream.join(f, t.io.?, .cancel); - t.ask.deinit(); - } - if (s.changes_next) |a| { - s.changes_next = null; - a.deinit(); - } -} - /// jj's answer `b` for `revset` parsed and pinned on pane `id`, or its /// refusal said there. fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []const u8) void { @@ -583,7 +606,7 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con if (answered(p, pane.serial)) |kept| { kept.deinit(p.gpa); kept.* = changes.*; - } else p.changes.append(p.gpa, changes.*) catch { + } else p.changes.answers.append(p.gpa, changes.*) catch { changes.deinit(p.gpa); return p.reportFailure(id, "Changes: out of memory"); }; @@ -1199,7 +1222,7 @@ const WorkerJj = struct { /// answer the session's input, as its loop hands it, and the client's. fn land(w: *WorkerJj) !void { var waited: u32 = 0; - while (w.a.core.stream.changes != null or w.a.core.stream.changes_next != null) : (waited += 1) { + while (w.a.core.changes.task != null or w.a.core.changes.next != null) : (waited += 1) { if (waited == 5000) return error.Timeout; var batch: Inbox.Batch = .{}; w.shell.q.takeInto(&batch); @@ -1254,7 +1277,7 @@ test "the pane closed before its Changes answer: nothing pinned, nothing kept" { try w.word("Del"); try testing.expect(w.a.core.panes[w.id] == null or w.a.core.panes[w.id].?.serial != w.serial); try w.land(); - try testing.expectEqual(@as(usize, 0), w.a.core.changes.items.len); + try testing.expectEqual(@as(usize, 0), w.a.core.changes.answers.items.len); } test "Changes draws a lane a pinned change after the line number, nested ones a level to the right on their rows only, and a line one added whole green across it; bare again, the gutter as it was" { -- cgit v1.3