summaryrefslogtreecommitdiff
path: root/core/jj.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-21 18:47:01 -0300
committerGabriel Schneider <[email protected]>2026-11-21 21:34:04 -0300
commit0d7879195a05b450877d2bf6bd625c01fbfbef8a (patch)
tree78ee67251d7919335c2a7ac5e06afe2bbf158f24 /core/jj.zig
parentb7e843256982f5a22954d103bb40264daf2bb758 (diff)
downloadpardes-0d7879195a05b450877d2bf6bd625c01fbfbef8a.tar.gz
pardes-0d7879195a05b450877d2bf6bd625c01fbfbef8a.zip
layers: ghost rows (slice 5): with `Changes` shown, what the file no longer has is drawn where it stood, rows of their own with no number and dimmer ink (`jj.Ghost`, kept with jj's answers, `Changes.ghosts` in `at` order, their text in `Changes.ghost_text` beside the hunks' removed lines): a line a pinned change took away whole, red across, in the line numbers' grey, placed by carrying the place it was taken from through the later pins' hunks into the shown text (`jj.carried`), and each pin's description after its last line (`id title author age`, the id's first four letters, the author's first name, the age short with no "ago", in its lane's colour, the author and age grey; the age is of the wall clock jj's answer carries, read where jj runs, an input as jj's answer is), the descriptions of blocks that close just above a place first and newest first, then the lines taken away there, then the descriptions of pins that have only those; a ghost row carries its own pin's lane and those of the line above that run on below it or close at or after it (`jj.ghostLanes`); the view's geometry takes the rows (`File.fillBody`'s ghosted instance, only while this view shows the changes and there are some: a ghost row's `wrap_col` is `-1 - ` its index, its `wrap_line` the line it stands before; `Pane.ghosts`/`ghost_text`, derived, set by `jj.showGhosts` each step while shown and as an answer lands), so the rows below move down in this view alone (another view with the lanes hidden is unmoved: tested); `wrapAt` says `ghost` and answers the line below at its start, so the cursor steps over them (`j` from the line above lands on the line below), selection, search hits, places and tables do not paint them, the line numbers skip them, a page ends before one; a Look (B3) on one opens its line in the revision it came from (`path@id-:line` for a line taken away, `path@id:line` for a description: `jj.lookGhost`); `]g` and `[g` (helix's, free in pardes's map: `goto_change`) go to the next and the previous change's first line (`jj.nextChange`); edits carry them as the lines' values (`jj.splice`: a ghost row stands before the line it stood before; one before a line taken out stands before the line after what took its place); in a terminal and in a window (tested both); round 3's lanes test reads each line's row through the view's map; tests: what the rows show (text, numbers, lanes by pin, grounds, inks) in both frontends and the other view unmoved; the cursor, the selection, `]g`/`[g` and a Look; the rows riding through two deletions with a client replaying them equal
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/jj.zig')
-rw-r--r--core/jj.zig505
1 files changed, 488 insertions, 17 deletions
diff --git a/core/jj.zig b/core/jj.zig
index 0cdf94a1..f23d4454 100644
--- a/core/jj.zig
+++ b/core/jj.zig
@@ -221,14 +221,18 @@ pub const Changes = struct {
lines: []Line = &.{},
/// Each pin's hunks in the file, a pin's together, each in place order.
hunks: []Hunk = &.{},
- /// The lines the hunks took away, hunk after hunk, each with its `\n`.
- removed: []u8 = &.{},
+ /// The ghost rows' text: the lines the hunks took away, hunk after
+ /// hunk, each with its `\n`; then each pin's description row.
+ ghost_text: []u8 = &.{},
+ /// The rows a view shows that the text has not, in `at` order.
+ ghosts: []Ghost = &.{},
pub fn deinit(c: *Changes, gpa: std.mem.Allocator) void {
gpa.free(c.pins);
gpa.free(c.lines);
gpa.free(c.hunks);
- gpa.free(c.removed);
+ gpa.free(c.ghost_text);
+ gpa.free(c.ghosts);
c.* = .{ .serial = c.serial };
}
@@ -297,11 +301,33 @@ pub const Hunk = struct {
old_len: u32,
new_start: u32,
new_len: u32,
- /// Its removed lines' text: `Changes.removed[removed..][0..removed_len]`.
+ /// Its removed lines' text: `Changes.ghost_text[removed..][0..removed_len]`.
removed: u32,
removed_len: u32,
};
+/// A row a view shows that is not a line of the text (PARDES.md "Changes on
+/// the source view": a ghost row): a line a pin took away whole, drawn where
+/// it stood, red across; or a pin's description (`id title author age`),
+/// after its last line, in its lane's colour. No number, the cursor steps
+/// over it, search and selection do not see it; a Look on it goes to its
+/// line in the revision it came from (`lookGhost`).
+pub const Ghost = struct {
+ /// The line of the shown text it stands before (`lines.len`: after the
+ /// last). It rides with the text as the lines' values do (`splice`).
+ at: u32,
+ pin: u16,
+ kind: Kind,
+ /// Its line, from 1: a line taken away, in its pin's parent; a
+ /// description, its pin's first line written, in the pin's revision.
+ line: u32,
+ /// Its text, `Changes.ghost_text[text..][0..len]`, no `\n`.
+ text: u32,
+ len: u32,
+
+ pub const Kind = enum(u8) { deleted, description };
+};
+
/// The `Changes` word (builtins.zig registers it): in a file's pane (or a
/// revision's), the changes `revset` names pinned on the pane and shown in
/// this view; another revset replaces them (in every view that shows
@@ -485,7 +511,13 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc
const log_args = [_][]const u8{ "log", "--no-graph", "--reversed", "-r", query, "-T", log_template, "--git", "--context=0", "--", set };
const logged = switch (runJj(p, gpa, dirOf(a.path), &log_args)) {
.refused => |why| return failed(gpa, a, why),
- .bytes => |bytes| bytes,
+ // The wall clock first, read where jj runs (an input, as jj's
+ // answer is): the descriptions' ages are of it.
+ .bytes => |bytes| stamped: {
+ defer gpa.free(bytes);
+ const now = if (p) |q| q.tape.value(.clock, wallSeconds, .{}) else wallSeconds();
+ break :stamped std.fmt.allocPrint(gpa, "{d}\n{s}", .{ now, bytes }) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
+ },
};
var it = std.mem.splitScalar(u8, logged, '\n');
const any = while (it.next()) |l| {
@@ -504,6 +536,14 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc
};
}
+/// The wall clock, in seconds since the epoch (a `.clock` read as `u32`:
+/// a replica's guess and a stamp answer it so too).
+fn wallSeconds() u32 {
+ var ts: std.c.timespec = undefined;
+ if (std.c.clock_gettime(.REALTIME, &ts) != 0) return 0;
+ return std.math.cast(u32, ts.sec) orelse 0;
+}
+
fn runJj(p: ?*Pardes, gpa: std.mem.Allocator, dir: []const u8, args: []const []const u8) Shown {
return if (p) |q| q.tape.site(.jj, gpa, runHost, .{ gpa, dir, args }) else runHost(gpa, dir, args);
}
@@ -550,6 +590,7 @@ pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: [
// No room for it: its values would lie, so it goes.
c.deinit(p.gpa);
_ = p.changes.answers.swapRemove(i);
+ showGhosts(p, p.panes[id].?);
};
}
}
@@ -575,6 +616,12 @@ fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len
@memcpy(next[from + put ..], c.lines[from + take ..]);
gpa.free(c.lines);
c.lines = next;
+ // A ghost row stands before the line it stood before; one before a
+ // line taken out, before the line after what took its place.
+ for (c.ghosts) |*g| {
+ if (g.at <= from) continue;
+ g.at = @intCast(if (g.at < from + take) from + put else g.at - take + put);
+ }
}
/// jj's answer `b` for `revset` parsed and pinned on pane `id`, or its
@@ -611,10 +658,24 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con
return p.reportFailure(id, "Changes: out of memory");
};
p.setStatus(.at(id), "");
+ showGhosts(p, pane);
},
}
}
+/// The pane's ghost rows as its geometry reads them (`Pane.ghosts`): the
+/// session's answers' (one set a pane), its geometry made again when they
+/// change (each step while a view shows them, and as an answer lands).
+pub fn showGhosts(p: *Pardes, pane: *Pane) void {
+ const ch = answered(p, pane.serial);
+ const ghosts: []const Ghost = if (ch) |c| c.ghosts else &.{};
+ const text: []const u8 = if (ch) |c| c.ghost_text else "";
+ if (ghosts.ptr == pane.ghosts.ptr and ghosts.len == pane.ghosts.len and text.ptr == pane.ghost_text.ptr) return;
+ pane.ghosts = ghosts;
+ pane.ghost_text = text;
+ pane.geometry_key = 0;
+}
+
const Read = union(enum) { refused: []u8, read: Changes };
/// jj's answers parsed: `logged` (the pins, each with its diff of the
@@ -622,6 +683,7 @@ const Read = union(enum) { refused: []u8, read: Changes };
/// shows: a line is matched to the line its change added by its text).
fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, annotated: []const u8, text: []const u8) error{OutOfMemory}!Read {
var out: Changes = .{ .serial = 0 };
+ const now = std.fmt.parseInt(i64, logged[0 .. std.mem.indexOfScalar(u8, logged, '\n') orelse logged.len], 10) catch 0;
var kept = false;
defer if (!kept) out.deinit(gpa);
var pins: std.ArrayList(Pin) = .empty;
@@ -648,8 +710,9 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a
defer removed.deinit(gpa);
for (0..out.pins.len) |k| try hunksOf(gpa, arena, @intCast(k), logged[diffs.items[k]..diffs.items[k + 1]], text, out.lines, &hunks, &removed);
out.hunks = try hunks.toOwnedSlice(gpa);
- out.removed = try removed.toOwnedSlice(gpa);
nest(out.lines, out.pins.len);
+ out.ghosts = try ghostsOf(gpa, arena, out.pins, out.lines, out.hunks, &removed, now);
+ out.ghost_text = try removed.toOwnedSlice(gpa);
kept = true;
return .{ .read = out };
}
@@ -788,6 +851,192 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, diffed: []c
}
}
+/// The ghost rows (`Ghost`), in `at` order: each line a pin took away
+/// whole, where it stood (`carried` into the last pin's revision); and each
+/// pin's description after its last line (a pin none of whose lines is
+/// left: after its last ghost row, or where its last hunk wrote). At one
+/// place: the descriptions of pins whose lines end just above it, the
+/// newest first (an inner block closes first), then the lines taken away,
+/// then the descriptions of pins that have only those. `text` holds the
+/// hunks' removed lines; the descriptions' rows go on after them.
+fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, lines: []const Line, hunks: []const Hunk, text: *std.ArrayList(u8), now: i64) error{OutOfMemory}![]Ghost {
+ const Made = struct { g: Ghost, rank: u64 };
+ var made: std.ArrayList(Made) = .empty;
+ for (hunks) |h| {
+ // Round 3's stand-in until slice 6: as many lines taken away as
+ // were written are changed ones, the rest taken away whole.
+ const changed = @min(h.old_len, h.new_len);
+ if (changed == h.old_len) continue;
+ const at = carried(hunks, h.pin, if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start, lines.len);
+ var from: usize = h.removed;
+ for (0..h.old_len) |k| {
+ const nl = std.mem.indexOfScalarPos(u8, text.items, from, '\n') orelse text.items.len;
+ if (k >= changed) try made.append(arena, .{
+ .g = .{ .at = at, .pin = h.pin, .kind = .deleted, .line = h.old_start + @as(u32, @intCast(k)), .text = @intCast(from), .len = @intCast(nl - from) },
+ .rank = 1 << 32 | made.items.len,
+ });
+ from = nl + 1;
+ }
+ }
+ for (pins, 0..) |pin, k| {
+ var at: ?u32 = null;
+ var rank: u64 = pins.len - k;
+ var i = lines.len;
+ while (i > 0) {
+ i -= 1;
+ if (@intFromEnum(lines[i].pin) == k) {
+ at = @intCast(i + 1);
+ break;
+ }
+ }
+ if (at == null) {
+ rank |= 2 << 32;
+ for (made.items) |m| if (m.g.pin == k) {
+ at = @max(at orelse 0, m.g.at);
+ };
+ }
+ var first: u32 = 0;
+ var end: u32 = 0;
+ for (hunks) |h| if (h.pin == k) {
+ if (first == 0) first = h.new_start;
+ end = if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start;
+ };
+ const at_text = text.items.len;
+ try describe(gpa, text, pin, now);
+ try made.append(arena, .{
+ .g = .{ .at = at orelse carried(hunks, @intCast(k), end, lines.len), .pin = @intCast(k), .kind = .description, .line = @max(first, 1), .text = @intCast(at_text), .len = @intCast(text.items.len - at_text) },
+ .rank = rank,
+ });
+ }
+ std.sort.pdq(Made, made.items, {}, struct {
+ fn before(_: void, a: Made, b: Made) bool {
+ return if (a.g.at != b.g.at) a.g.at < b.g.at else a.rank < b.rank;
+ }
+ }.before);
+ const out = try gpa.alloc(Ghost, made.items.len);
+ for (out, made.items) |*o, m| o.* = m.g;
+ return out;
+}
+
+/// Where the place after line `after` of pin `k`'s revision is in the last
+/// pin's: moved by each later pin's hunks before it, or, inside a run of
+/// lines a later pin took away, after what it wrote there. (A change the
+/// revset does not pin, between two it does, is not seen: its lines move
+/// the places below it by as many.)
+fn carried(hunks: []const Hunk, k: u16, after: u32, lines: usize) u32 {
+ var pos: i64 = after;
+ var i: usize = 0;
+ while (i < hunks.len) {
+ const j = hunks[i].pin;
+ var shift: i64 = 0;
+ var inside: ?i64 = null;
+ while (i < hunks.len and hunks[i].pin == j) : (i += 1) {
+ const h = hunks[i];
+ if (j <= k) continue;
+ if (h.old_len == 0) {
+ if (h.old_start < pos) shift += h.new_len;
+ } else if (h.old_start + h.old_len - 1 <= pos) {
+ shift += @as(i64, h.new_len) - h.old_len;
+ } else if (h.old_start <= pos) inside = @as(i64, h.new_start) - 1 + h.new_len;
+ }
+ if (j > k) pos = inside orelse pos + shift;
+ }
+ return @intCast(std.math.clamp(pos, 0, @as(i64, @intCast(lines))));
+}
+
+/// A pin's description row (PARDES.md: compact, one blank between): its
+/// id's first four letters, its title, its author's first name, its age
+/// short (`5h`, `2d`, `3w`, `4m`, `1y`; under an hour `now`), no "ago".
+fn describe(gpa: std.mem.Allocator, text: *std.ArrayList(u8), pin: Pin, now: i64) error{OutOfMemory}!void {
+ const author = pin.author[0..pin.author_len];
+ try text.print(gpa, "{s} {s} {s} ", .{ pin.id[0..4], pin.title[0..pin.title_len], author[0 .. std.mem.indexOfScalar(u8, author, ' ') orelse author.len] });
+ const s = @max(0, now - pin.time);
+ const day = 24 * 3600;
+ for ([_]struct { u8, i64 }{ .{ 'y', 365 * day }, .{ 'm', 30 * day }, .{ 'w', 7 * day }, .{ 'd', day }, .{ 'h', 3600 } }) |unit| {
+ if (s >= unit[1]) return text.print(gpa, "{d}{c}", .{ @divFloor(s, unit[1]), unit[0] });
+ }
+ try text.appendSlice(gpa, "now");
+}
+
+/// The lanes on ghost row `gi` (`ghosts[gi]`): those of the line above it
+/// that run on below it or whose description is this row or one after it
+/// at the same place, then its own pin's.
+fn ghostLanes(ch: *const Changes, ghosts: []const Ghost, gi: usize) [levels]PinIndex {
+ const g = ghosts[gi];
+ var out: [levels]PinIndex = @splat(.none);
+ var n: usize = 0;
+ if (g.at > 0 and g.at - 1 < ch.lines.len) for (ch.lines[g.at - 1].lanes) |lane| {
+ if (lane == .none) break;
+ const below = g.at < ch.lines.len and std.mem.indexOfScalar(PinIndex, &ch.lines[g.at].lanes, lane) != null;
+ const closes = for (ghosts[gi..]) |later| {
+ if (later.at != g.at) break false;
+ if (later.kind == .description and later.pin == @intFromEnum(lane)) break true;
+ } else false;
+ if (!below and !closes) continue;
+ out[n] = lane;
+ n += 1;
+ };
+ if (n < levels and std.mem.indexOfScalar(PinIndex, out[0..n], @enumFromInt(g.pin)) == null) out[n] = @enumFromInt(g.pin);
+ return out;
+}
+
+/// A ghost row's cells: a line taken away whole red across, its text in
+/// the line numbers' grey (dimmer than the text's ink); a description in
+/// its lane's colour, its author and age grey.
+fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, text: []const u8, colours: []const [3]u8, x: u16, y: u16, w: u16) void {
+ const th = p.theme();
+ switch (g.kind) {
+ .deleted => s.tint(x, y, w, pardes.colors.Grounds.of(th).del.row, th.lineno),
+ .description => {
+ const said = text[g.text..][0..g.len];
+ // `id title author age`: the last two words grey.
+ var grey = said.len;
+ for (0..2) |_| grey = std.mem.lastIndexOfScalar(u8, said[0..grey], ' ') orelse 0;
+ const lane_cells = File.displayWidth(p.settings.tab, said[0..grey]);
+ const cells = @min(w, File.displayWidth(p.settings.tab, said));
+ for (0..cells) |c| s.at(x + @as(u16, @intCast(c)), y).style.fg = .{ .rgb = if (c < lane_cells) colours[g.pin % colours.len] else th.lineno };
+ },
+ }
+}
+
+/// The line `]g` (`forward`) or `[g` goes to from line `from` of pane's
+/// text: the next or the previous change's first line in this view (a run
+/// of lines one pin wrote, or where lines one took away stood); null with
+/// none shown, or none that way.
+pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?usize {
+ if (pane.layers.lanes == 0) return null;
+ const ch = answered(p, pane.serial) orelse return null;
+ var best: ?usize = null;
+ const Best = struct {
+ fn take(b: *?usize, at: usize, here: usize, fwd: bool) void {
+ if (if (fwd) at <= here else at >= here) return;
+ if (b.*) |was| if (if (fwd) at >= was else at <= was) return;
+ b.* = at;
+ }
+ };
+ for (ch.lines, 0..) |l, i| if (l.pin != .none and (i == 0 or ch.lines[i - 1].pin != l.pin)) Best.take(&best, i, from, forward);
+ for (ch.ghosts) |g| if (g.kind == .deleted) Best.take(&best, @min(g.at, ch.lines.len -| 1), from, forward);
+ return best;
+}
+
+/// A Look on body row `vr` of pane `id` when it is a ghost row (mouse.zig,
+/// a B3 click): the line it shows in the revision it came from, as the
+/// address `path@rev:line` opens it (a line taken away: its pin's parent's;
+/// a description: its pin's first line). False on any other row.
+pub fn lookGhost(p: *Pardes, id: usize, vr: i32) bool {
+ const pane = p.panes[id].?;
+ if (pane.layers.lanes == 0 or !pane.wrapAt(p.settings.tab, vr).ghost) return false;
+ const ch = answered(p, pane.serial) orelse return false;
+ const gi: usize = @intCast(-1 - pane.wrap_col[@intCast(vr)]);
+ if (gi >= pane.ghosts.len) return false;
+ const g = pane.ghosts[gi];
+ const f = &pane.file.?;
+ const path = if (of(f)) |r| r.path else f.path;
+ const rev: [9]u8 = ch.pins[g.pin].id ++ "-".*;
+ _ = open(p, id, path, rev[0 .. if (g.kind == .description) 8 else 9], .{ .line = g.line }, false);
+ return true;
+}
+
/// Each line's lanes: the blocks of the pins that span it, oldest first.
/// A pin's block runs from a line it wrote on to each next line it wrote
/// while every line between was written by a newer pin: its lane unbroken
@@ -878,9 +1127,9 @@ fn hueApart(a: [3]u8, b: [3]u8) f32 {
/// 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.
+/// pin added whole green across it, as a diff's added line; on a ghost row
+/// (`Ghost`) its lanes and its cells (`inkGhost`). Not yet: 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 = answered(p, pane.serial) orelse return;
if (ch.lines.len == 0) return;
@@ -897,12 +1146,20 @@ pub fn paint(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u1
const scroll = pane.scroll();
var vr: u16 = pane.context_rows;
while (vr < body_h) : (vr += 1) {
- const row: i32 = if (pane.wrap_n == 0) scroll + @as(i32, vr) else if (vr < pane.wrap_n) pane.wrap_line[vr] else break;
- if (row < 0 or row >= ch.lines.len) continue;
- const line = ch.lines[@intCast(row)];
const y = body_y + vr;
- if (line.whole and line.pin != .none) s.tint(tx + gutter, y, tw - gutter, ground, null);
- for (line.lanes, 0..) |lane, level| {
+ const lanes: [levels]PinIndex = if (vr < pane.wrap_n and pane.wrap_col[vr] < 0) ghost: {
+ const gi: usize = @intCast(-1 - pane.wrap_col[vr]);
+ if (gi >= pane.ghosts.len) continue;
+ inkGhost(p, s, pane.ghosts[gi], pane.ghost_text, colours, tx + gutter, y, tw - gutter);
+ break :ghost ghostLanes(ch, pane.ghosts, gi);
+ } else line: {
+ const row: i32 = if (pane.wrap_n == 0) scroll + @as(i32, vr) else if (vr < pane.wrap_n) pane.wrap_line[vr] else break;
+ if (row < 0 or row >= ch.lines.len) continue;
+ const line = ch.lines[@intCast(row)];
+ if (line.whole and line.pin != .none) s.tint(tx + gutter, y, tw - gutter, ground, null);
+ break :line line.lanes;
+ };
+ for (lanes, 0..) |lane, level| {
if (lane == .none) break;
const rgb = colours[@intFromEnum(lane) % colours.len];
if (windowed) {
@@ -1101,7 +1358,7 @@ test "Changes pins a revset's changes on the pane, shown in this view: who wrote
.{ .pin = 1, .old_start = 2, .old_len = 0, .new_start = 3, .new_len = 1, .removed = 15, .removed_len = 0 },
};
try testing.expectEqualSlices(Hunk, &hunks, ch.hunks);
- try testing.expectEqualStrings("const one = 1;\n", ch.removed);
+ try testing.expectStringStartsWith(ch.ghost_text, "const one = 1;\n");
// Bare: not shown in this view; what jj said is kept on the pane.
p.update(.{ .command = "Changes" });
try testing.expectEqual(0, pane.layers.lanes);
@@ -1311,10 +1568,11 @@ test "Changes draws a lane a pinned change after the line number, nested ones a
const colours = laneColours(p.theme(), &buf);
try testing.expect(colours.len >= 3);
const ground = pardes.colors.Grounds.of(p.theme()).add.row;
- // Rows 1-10: `fn`'s spine on all; `mid` on 3-6; `shift` on 4.
+ // Lines 1-10, each on the row the view draws it at (ghost rows
+ // stand between): `fn`'s spine on all; `mid` on 3-6; `shift` on 4.
const want = [_][]const u8{ "0", "0", "01", "012", "01", "01", "0", "0", "0", "0" };
for (want, 0..) |lanes, row| {
- const y = body.y + @as(u16, @intCast(row));
+ const y = body.y + @as(u16, @intCast(pane.wrapRow(p.settings.tab, @intCast(row), 0).row));
if (windowed) {
var n: usize = 0;
for (s.lanes.items) |lane| if (lane.y == y) {
@@ -1346,6 +1604,219 @@ test "Changes draws a lane a pinned change after the line number, nested ones a
}
}
+/// A function `fn` wrote, two of its lines `cut` took away whole, and one
+/// `mid` rewrote as two (one changed, one added whole).
+const Ghosts = struct {
+ const fn_text = "fn f() void {\n a();\n b();\n c();\n d();\n e();\n}\n";
+ const cut_text = "fn f() void {\n a();\n b();\n e();\n}\n";
+ const mid_text = "fn f() void {\n a();\n b2();\n x();\n e();\n}\n";
+
+ fn repo() !?Repo {
+ return Repo.with(testing.allocator, &.{ .{ fn_text, "fn" }, .{ cut_text, "cut" }, .{ mid_text, "mid" } }, null);
+ }
+
+ /// The body's rows as this view shows them, whole.
+ fn rows(p: *Pardes, pane: *Pane, arena: std.mem.Allocator, out: [][]const u8) ![]const []const u8 {
+ var it = std.mem.splitScalar(u8, try File.bodyText(p.settings.tab, arena, pane, &pane.file.?), '\n');
+ var n: usize = 0;
+ while (it.next()) |row| : (n += 1) out[n] = row;
+ return out[0..n];
+ }
+
+ /// Each row's text after the gutter, and its number (blank: none).
+ fn text(pane: *const Pane, row: []const u8) []const u8 {
+ return row[@min(row.len, File.gutterWidth(pane))..];
+ }
+ fn number(pane: *const Pane, row: []const u8) []const u8 {
+ return std.mem.trim(u8, row[0..@min(row.len, File.gutterWidth(pane))], " ");
+ }
+
+ /// The rows a `Changes ::@` shows: `said` each pin's description.
+ fn want(said: [3][]const u8) [11][]const u8 {
+ return .{ "fn f() void {", " a();", " b2();", " x();", said[2], " c();", " d();", said[1], " e();", "}", said[0] };
+ }
+};
+
+fn saidOf(arena: std.mem.Allocator, ch: *const Changes) ![3][]const u8 {
+ var said: [3][]const u8 = undefined;
+ for (&said, ch.pins, [_][]const u8{ "fn", "cut", "mid" }) |*d, pin, title| d.* = try std.fmt.allocPrint(arena, "{s} {s} t now", .{ pin.id[0..4], title });
+ return said;
+}
+
+test "Changes draws what the text has not where it stood, as ghost rows: a line a change took away whole red across, each change's description after its last line in its lane's colour; no number, their lanes, the rows below moved down in this view alone" {
+ const gpa = testing.allocator;
+ var repo = try Ghosts.repo() orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ for ([_]bool{ false, true }) |windowed| {
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 24, .windowed = windowed });
+ defer p.deinit();
+ var line: [4200]u8 = undefined;
+ p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) });
+ const id = p.view().active;
+ const pane = p.panes[id].?;
+ const other = p.openView().?;
+ p.update(.{ .command = "Changes ::@" });
+ const ch = answered(p, pane.serial).?;
+ try testing.expectEqual(@as(usize, 3), ch.pins.len);
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const said = try saidOf(arena.allocator(), ch);
+ var buf: [64][]const u8 = undefined;
+ const got = try Ghosts.rows(p, pane, arena.allocator(), &buf);
+ const want = Ghosts.want(said);
+ const numbers = [_][]const u8{ "1", "2", "3", "4", "", "", "", "", "5", "6", "" };
+ for (want, numbers, got[0..want.len]) |w, n, row| {
+ try testing.expectEqualStrings(w, Ghosts.text(pane, row));
+ try testing.expectEqualStrings(n, Ghosts.number(pane, row));
+ }
+ // Drawn: each row's lanes (pins by level), its ground, a ghost's ink.
+ p.sync();
+ const s = try p.render(arena.allocator());
+ const body = s.region(.body, id).?.rect;
+ const first = body.x + File.gutterWidth(pane) - File.laneCells(pane);
+ var cbuf: [8][3]u8 = undefined;
+ const colours = laneColours(p.theme(), &cbuf);
+ const grounds = pardes.colors.Grounds.of(p.theme());
+ const lanes = [_][]const u8{ "0", "0", "02", "02", "02", "01", "01", "01", "0", "0", "0" };
+ const Ground = enum { none, add, del };
+ const ground = [_]Ground{ .add, .add, .none, .add, .none, .del, .del, .none, .add, .add, .none };
+ for (lanes, ground, 0..) |pins, gr, row| {
+ const y = body.y + @as(u16, @intCast(row));
+ if (windowed) {
+ var n: usize = 0;
+ for (s.lanes.items) |lane| if (lane.y == y) {
+ try testing.expectEqual(colours[pins[lane.level] - '0'], lane.rgb);
+ n += 1;
+ };
+ try testing.expectEqual(pins.len, n);
+ } else for (pins, 0..) |pin, level| {
+ const cell = s.at(first + @as(u16, @intCast(level)), y);
+ try testing.expectEqualStrings("\u{258e}", cell.grapheme());
+ try testing.expectEqual(pardes.Color{ .rgb = colours[pin - '0'] }, cell.style.fg);
+ }
+ const cell = s.at(body.x + File.gutterWidth(pane) + 1, y);
+ const bg: ?[3]u8 = switch (gr) {
+ .none => null,
+ .add => grounds.add.row,
+ .del => grounds.del.row,
+ };
+ if (bg) |rgb| try testing.expectEqual(pardes.Color{ .rgb = rgb }, cell.style.bg) else {
+ try testing.expect(!std.meta.eql(cell.style.bg, pardes.Color{ .rgb = grounds.add.row }));
+ try testing.expect(!std.meta.eql(cell.style.bg, pardes.Color{ .rgb = grounds.del.row }));
+ }
+ }
+ // A description's id and title in its lane's colour; a line taken
+ // away in the numbers' grey.
+ for ([_]u16{ 4, 7, 10 }, [_]usize{ 2, 1, 0 }) |row, pin|
+ try testing.expectEqual(pardes.Color{ .rgb = colours[pin] }, s.at(body.x + File.gutterWidth(pane), body.y + row).style.fg);
+ try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(body.x + File.gutterWidth(pane) + 4, body.y + 5).style.fg);
+ // The other view shows no lanes: its rows are the text's, unmoved.
+ p.toView(other);
+ const plain = try Ghosts.rows(p, pane, arena.allocator(), &buf);
+ var lines = std.mem.splitScalar(u8, Ghosts.mid_text, '\n');
+ for (plain[0..6]) |row| try testing.expectEqualStrings(lines.next().?, Ghosts.text(pane, row));
+ p.toView(.at(0));
+ try testing.expectEqualStrings(said[2], Ghosts.text(pane, (try Ghosts.rows(p, pane, arena.allocator(), &buf))[4]));
+ }
+}
+
+test "the cursor steps over ghost rows and selection does not see them; ]g and [g go from change to change; a Look on one opens its line in the revision it came from" {
+ const gpa = testing.allocator;
+ var repo = try Ghosts.repo() orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ const p = try Pardes.init(gpa, .{ .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 id = p.view().active;
+ const pane = p.panes[id].?;
+ p.update(.{ .command = "Changes ::@" });
+ const key = struct {
+ fn send(q: *Pardes, keys: []const u8) void {
+ for (keys) |k| q.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } });
+ }
+ }.send;
+ // From `x();` (line 4) a `j` is on `e();` (line 5), three ghost rows below.
+ p.update(.{ .command = "Look a.zig:4" });
+ try testing.expectEqual(@as(i32, 3), pane.body.cur_row);
+ key(p, "j");
+ try testing.expectEqual(@as(i32, 4), pane.body.cur_row);
+ try testing.expectEqual(@as(i32, 8), pane.wrapRow(p.settings.tab, 4, 0).row);
+ // All selected: a ghost row keeps its own ground.
+ key(p, "%");
+ p.sync();
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var s = try p.render(arena.allocator());
+ var body = s.region(.body, id).?.rect;
+ const x = body.x + File.gutterWidth(pane) + 4;
+ try testing.expectEqual(pardes.Color{ .rgb = pardes.colors.Grounds.of(p.theme()).del.row }, s.at(x, body.y + 5).style.bg);
+ try testing.expect(!std.meta.eql(s.at(x, body.y + 5).style.bg, s.at(x, body.y + 3).style.bg));
+ key(p, ";");
+ // ]g, [g: the changes start on lines 1 (fn), 3 (mid) and 5 (fn again,
+ // after the lines cut took away).
+ p.update(.{ .command = "Look a.zig:1" });
+ for ([_]i32{ 2, 4, 4 }) |want| {
+ key(p, "]g");
+ try testing.expectEqual(want, pane.body.cur_row);
+ }
+ for ([_]i32{ 2, 0 }) |want| {
+ key(p, "[g");
+ try testing.expectEqual(want, pane.body.cur_row);
+ }
+ // A Look (B3) on `c();`'s ghost row: fn's revision (cut's parent) at its line 4.
+ p.sync();
+ s = try p.render(arena.allocator());
+ body = s.region(.body, id).?.rect;
+ p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = x, .row = body.y + 5 } });
+ p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = x, .row = body.y + 5 } });
+ const shown = p.view().active;
+ try testing.expect(shown != id);
+ const f = &p.panes[shown].?.file.?;
+ try testing.expect(of(f) != null);
+ try testing.expectEqualStrings(Ghosts.fn_text, f.content);
+ try testing.expectEqual(@as(i32, 3), p.panes[shown].?.body.cur_row);
+}
+
+test "ghost rows ride with the text: a line deleted above moves them up, a line they stood before deleted puts them before the next; a client replays it equal" {
+ var repo = try Ghosts.repo() 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();
+ 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 ats = struct {
+ fn of(ch: *const Changes, out: []u32) []u32 {
+ for (ch.ghosts, out[0..ch.ghosts.len]) |g, *o| o.* = g.at;
+ return out[0..ch.ghosts.len];
+ }
+ }.of;
+ var got: [8]u32 = undefined;
+ // mid's description, cut's two lines and its description before `e();`
+ // (line 5), fn's after the last.
+ try testing.expectEqualSlices(u32, &.{ 4, 4, 4, 4, 6 }, ats(answered(a.core, serial).?, &got));
+ // `a();` deleted: each one a line up.
+ r.confirm(try a.local(.{ .command = "Look a.zig:2" }));
+ for (";xd") |k| r.confirm(try a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }));
+ r.settle();
+ try testing.expectEqualSlices(u32, &.{ 3, 3, 3, 3, 5 }, ats(answered(a.core, serial).?, &got));
+ // `e();`, the line they stood before, deleted: they stand before `}`.
+ r.confirm(try a.local(.{ .command = "Look a.zig:4" }));
+ for (";xd") |k| r.confirm(try a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }));
+ r.settle();
+ try testing.expectEqualStrings("fn f() void {\n b2();\n x();\n}\n", a.core.panes[a.core.view().active].?.file.?.content);
+ try testing.expectEqualSlices(u32, &.{ 3, 3, 3, 3, 4 }, ats(answered(a.core, serial).?, &got));
+ try testing.expect(r.diverged == null);
+ try testing.expectEqualSlices(Ghost, answered(a.core, serial).?.ghosts, answered(r.core(), serial).?.ghosts);
+ try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core()));
+}
+
/// 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 {