summaryrefslogtreecommitdiff
path: root/core/jj.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/jj.zig')
-rw-r--r--core/jj.zig941
1 files changed, 874 insertions, 67 deletions
diff --git a/core/jj.zig b/core/jj.zig
index 0cdf94a1..dd54dfc5 100644
--- a/core/jj.zig
+++ b/core/jj.zig
@@ -221,14 +221,22 @@ 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 = &.{},
+ /// The words a changed line changed: byte ranges of a line (`Line`), or
+ /// of `ghost_text` (`Ghost`), each `.{ from, to }`.
+ marks: [][2]u32 = &.{},
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);
+ gpa.free(c.marks);
c.* = .{ .serial = c.serial };
}
@@ -275,6 +283,10 @@ pub const Line = struct {
whole: bool = false,
/// The pins whose lanes run down it, outermost (oldest) first (`nest`).
lanes: [levels]PinIndex = @splat(.none),
+ /// A changed line's words that came (`Changes.marks[marks..][0..nmarks]`,
+ /// byte ranges of the line).
+ marks: u32 = 0,
+ nmarks: u16 = 0,
};
/// The lanes a row shows at most: the ruled gutter fits three.
@@ -292,14 +304,49 @@ pub const pins_max = 64;
/// revisions' numbers (from 1; `old_len` 0: it only wrote, after old line
/// `old_start`).
pub const Hunk = struct {
+ /// Its pin, or `carrier`: a change between the pins the revset does
+ /// not pin (its hunks only carry places, `carried`).
pin: u16,
+ /// Its change's place in jj's answer, oldest first.
+ step: u16,
old_start: u32,
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,
+
+ pub const carrier = std.math.maxInt(u16);
+};
+
+/// 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`). A line a pin CHANGED
+/// is a ghost row too: its old text right above the line, the words that
+/// went in red, the line with the words that came in green.
+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,
+ /// A changed line's: the words that went (`Changes.marks`).
+ marks: u32 = 0,
+ nmarks: u16 = 0,
+
+ /// A line taken away whole; a line's old text, the line that took its
+ /// place right below it; a pin's description.
+ pub const Kind = enum(u8) { deleted, changed, description };
};
/// The `Changes` word (builtins.zig registers it): in a file's pane (or a
@@ -480,16 +527,27 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc
const set = fileset(gpa, a.path) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
defer gpa.free(set);
// The changes, oldest first, each with its diff of the file: one run.
- const query = std.fmt.allocPrint(gpa, "({s}) & files({s})", .{ a.revset, set }) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
+ // With them the changes between the first and the shown revision that
+ // the revset does not pin (marked so), whose hunks move the places of
+ // the lines the older pins took away.
+ const query = std.fmt.allocPrint(gpa, "(({s}) | (roots({s})::({s}))) & files({s})", .{ a.revset, a.revset, a.rev, set }) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
defer gpa.free(query);
- const log_args = [_][]const u8{ "log", "--no-graph", "--reversed", "-r", query, "-T", log_template, "--git", "--context=0", "--", set };
+ const template = logTemplate(gpa, a.revset) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
+ defer gpa.free(template);
+ const log_args = [_][]const u8{ "log", "--no-graph", "--reversed", "-r", query, "-T", 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| {
- if (pinOf(l) != null) break true;
+ if (if (entryOf(l)) |e| e.pinned else false) break true;
} else false;
if (!any) return .{ .gen = a.gen, .rows = logged, .end = .done };
// Who wrote each line: a path, not a fileset (annotate takes one file
@@ -504,6 +562,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 +616,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 +642,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 +684,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,49 +709,70 @@ 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;
defer pins.deinit(gpa);
- // Where each pin's diff starts in `logged`, and the end.
- var diffs: std.ArrayList(usize) = .empty;
+ // Each change jj answered, oldest first: its pin (or `Hunk.carrier`)
+ // and where its diff starts in `logged`; then the end.
+ const Step = struct { pin: u16, diff: usize };
+ var steps: std.ArrayList(Step) = .empty;
var it = std.mem.splitScalar(u8, logged, '\n');
- while (it.next()) |l| if (pinOf(l)) |pin| {
- if (pins.items.len == pins_max) return .{ .refused = try std.fmt.allocPrint(gpa, "more than {d} changes touch it", .{pins_max}) };
- try pins.append(gpa, pin);
- try diffs.append(arena, it.index orelse logged.len);
+ while (it.next()) |l| if (entryOf(l)) |e| {
+ if (e.pinned and pins.items.len == pins_max) return .{ .refused = try std.fmt.allocPrint(gpa, "more than {d} changes touch it", .{pins_max}) };
+ if (steps.items.len == Hunk.carrier) return .{ .refused = try std.fmt.allocPrint(gpa, "more than {d} changes between them touch it", .{Hunk.carrier}) };
+ if (e.pinned) try pins.append(gpa, e.pin);
+ try steps.append(arena, .{ .pin = if (e.pinned) @intCast(pins.items.len - 1) else Hunk.carrier, .diff = it.index orelse logged.len });
};
- try diffs.append(arena, logged.len);
+ try steps.append(arena, .{ .pin = Hunk.carrier, .diff = logged.len });
out.pins = try pins.toOwnedSlice(gpa);
if (out.pins.len == 0) {
kept = true;
return .{ .read = out };
}
- out.lines = try provenance(gpa, arena, annotated, out.pins, text);
- // Each change's hunks, and which of its lines it added whole.
+ var moved: Moved = undefined;
+ out.lines = try provenance(gpa, arena, annotated, out.pins, text, &moved);
+ // Each change's hunks, and which of its lines a pin added whole.
var hunks: std.ArrayList(Hunk) = .empty;
defer hunks.deinit(gpa);
var removed: std.ArrayList(u8) = .empty;
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);
+ var marks: std.ArrayList([2]u32) = .empty;
+ defer marks.deinit(gpa);
+ var taken: std.ArrayList(Taken) = .empty;
+ for (steps.items[0 .. steps.items.len - 1], steps.items[1..], 0..) |s, next, i| try hunksOf(gpa, arena, s.pin, @intCast(i), logged[s.diff..next.diff], text, out.lines, &hunks, &removed, &marks, &taken);
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, taken.items, &removed, now, moved);
+ out.ghost_text = try removed.toOwnedSlice(gpa);
+ out.marks = try marks.toOwnedSlice(gpa);
kept = true;
return .{ .read = out };
}
-/// `jj log`'s line a change, before its diff: its id, its author's time,
-/// its author, its description's first line, a tab between.
-const log_template = "change_id.short(8) ++ \"\\t\" ++ author.timestamp().utc().format(\"%s\") ++ \"\\t\" ++ author.name() ++ \"\\t\" ++ description.first_line() ++ \"\\n\"";
+/// `jj log`'s line a change, before its diff: its id, `p` when `revset`
+/// pins it (else `c`), its author's time, its author, its description's
+/// first line, a tab between.
+fn logTemplate(gpa: std.mem.Allocator, revset: []const u8) error{OutOfMemory}![]u8 {
+ var t: std.ArrayList(u8) = .empty;
+ errdefer t.deinit(gpa);
+ try t.appendSlice(gpa, "change_id.short(8) ++ \"\\t\" ++ if(self.contained_in(\"");
+ for (revset) |c| {
+ if (c == '"' or c == '\\') try t.append(gpa, '\\');
+ try t.append(gpa, c);
+ }
+ try t.appendSlice(gpa, "\"), \"p\", \"c\") ++ \"\\t\" ++ author.timestamp().utc().format(\"%s\") ++ \"\\t\" ++ author.name() ++ \"\\t\" ++ description.first_line() ++ \"\\n\"");
+ return t.toOwnedSlice(gpa);
+}
-/// A `log_template` line, or null for a line of a diff (which starts with
+/// A `logTemplate` line, or null for a line of a diff (which starts with
/// a blank, a sign, `@`, `\\` or a word and a blank: never eight of jj's
/// id letters, k to z, and a tab).
-fn pinOf(l: []const u8) ?Pin {
- if (l.len < 9 or l[8] != '\t') return null;
+fn entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool } {
+ if (l.len < 11 or l[8] != '\t' or l[10] != '\t') return null;
for (l[0..8]) |c| if (c < 'k' or c > 'z') return null;
- var cols = std.mem.splitScalar(u8, l[9..], '\t');
+ var cols = std.mem.splitScalar(u8, l[11..], '\t');
var pin: Pin = .{ .id = l[0..8].*, .time = std.fmt.parseInt(i64, cols.next() orelse "", 10) catch 0 };
const author = cols.next() orelse "";
pin.author_len = @intCast(@min(author.len, pin.author.len));
@@ -672,7 +780,7 @@ fn pinOf(l: []const u8) ?Pin {
const title = cols.rest();
pin.title_len = @intCast(@min(title.len, pin.title.len));
@memcpy(pin.title[0..pin.title_len], title[0..pin.title_len]);
- return pin;
+ return .{ .pin = pin, .pinned = l[9] == 'p' };
}
/// Which pin wrote each line of `text` (what the pane shows: `whole` is the
@@ -681,8 +789,9 @@ fn pinOf(l: []const u8) ?Pin {
/// 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 {
+/// ends keeps its owner, a line only the pane has is nobody's); `moved`,
+/// where jj's lines stand in `text`.
+pub fn provenance(gpa: std.mem.Allocator, arena: std.mem.Allocator, annotated: []const u8, pins: []const Pin, text: []const u8, moved: *Moved) error{OutOfMemory}![]Line {
var owners: std.ArrayList(PinIndex) = .empty;
var was: std.ArrayList(u8) = .empty;
var rows = std.mem.splitScalar(u8, annotated, '\n');
@@ -699,8 +808,10 @@ pub fn provenance(gpa: std.mem.Allocator, arena: std.mem.Allocator, annotated: [
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 });
+ const was_n: u32 = @intCast(owners.items.len);
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;
+ moved.* = .{ .head = was_n, .tail = 0, .was = was_n, .now = was_n };
return out;
}
const span = File.spanOf(was.items, text);
@@ -711,9 +822,29 @@ pub fn provenance(gpa: std.mem.Allocator, arena: std.mem.Allocator, annotated: [
// 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;
+ moved.* = .{ .head = @intCast(head), .tail = @intCast(tail), .was = was_n, .now = @intCast(out.len) };
return out;
}
+/// Where jj's lines (the revision it annotated) stand in the pane's text,
+/// which has keys typed since (before the word, never snapshotted, or
+/// while jj ran): its first `head` lines and its last `tail` are the same
+/// lines, those between were rewritten.
+pub const Moved = struct {
+ head: u32,
+ tail: u32,
+ /// jj's lines, and the text's.
+ was: u32,
+ now: u32,
+
+ /// The text's place of jj's place `at` (before its line `at`), or null
+ /// inside lines rewritten since: no place there tells the truth.
+ fn place(m: Moved, at: u32) ?u32 {
+ if (at >= m.was - m.tail) return at - (m.was - m.tail) + (m.now - m.tail);
+ return if (at <= m.head) at else null;
+ }
+};
+
/// `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"));
@@ -738,43 +869,92 @@ fn tailLines(text: []const u8, tail: usize) usize {
return starts + std.mem.count(u8, text[from .. text.len - 1], "\n");
}
+/// A line a pin took away (in hunk order, a pin's after another's): one
+/// line changed (`paired`, with the line it became, by its number in the
+/// pin's revision and, while it is shown, its line of the text) or taken
+/// away whole; the words that went, in `Changes.marks`.
+const Taken = struct { paired: bool, marks: u32, nmarks: u16, new: u32 = 0, shown: ?u32 = null, partner: usize = 0 };
+
/// Pin `k`'s hunks in `diffed` (its diff of the file, `--git --context=0`) onto
-/// `hunks`, their removed lines onto `removed`; and each line of `text`
-/// pin `k` wrote (`lines`) marked `whole` when the line it is, matched in
-/// order by its text, was one the change added whole: in a run of lines
-/// taken away and written, the first as many written as were taken away
-/// are changed, the rest added whole.
-fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, diffed: []const u8, text: []const u8, lines: []Line, hunks: *std.ArrayList(Hunk), removed: *std.ArrayList(u8)) error{OutOfMemory}!void {
- const Added = struct { text: []const u8, whole: bool };
+/// `hunks`, their removed lines onto `removed`, what became of each onto
+/// `taken`; and each line of `text` pin `k` wrote (`lines`), matched in
+/// order by its text, marked `whole` when the change added it whole, else
+/// with the words that came. Which lines a run of lines taken away and
+/// written changed, and their words, is the patch view's alignment
+/// (hunks.format: its `inferEdits`), read off its rows. A change the
+/// revset does not pin (`k` is `Hunk.carrier`): its hunks alone.
+fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, step: u16, diffed: []const u8, text: []const u8, lines: []Line, hunks: *std.ArrayList(Hunk), removed: *std.ArrayList(u8), marks: *std.ArrayList([2]u32), taken: *std.ArrayList(Taken)) error{OutOfMemory}!void {
+ const Added = struct { text: []const u8, whole: bool, marks: u32, nmarks: u16, new: u32, shown: ?u32 = null };
var added: std.ArrayList(Added) = .empty;
var walk: diff.Walk = .{};
- var taken: u32 = 0;
var it = std.mem.splitScalar(u8, diffed, '\n');
while (it.next()) |raw| {
const l = walk.step(raw);
switch (l.kind) {
.hunk => {
const h = diff.parseHunk(std.mem.trimEnd(u8, raw, "\r")).?;
- try hunks.append(gpa, .{ .pin = k, .old_start = @intCast(h.old_start), .old_len = @intCast(h.old_count), .new_start = @intCast(h.new_start), .new_len = @intCast(h.new_count), .removed = @intCast(removed.items.len), .removed_len = 0 });
- taken = 0;
+ try hunks.append(gpa, .{ .pin = k, .step = step, .old_start = @intCast(h.old_start), .old_len = @intCast(h.old_count), .new_start = @intCast(h.new_start), .new_len = @intCast(h.new_count), .removed = @intCast(removed.items.len), .removed_len = 0 });
},
- .removed => {
+ .removed => if (k != Hunk.carrier) {
const body = std.mem.trimEnd(u8, raw, "\r")[1..];
try removed.appendSlice(gpa, body);
try removed.append(gpa, '\n');
hunks.items[hunks.items.len - 1].removed_len += @intCast(body.len + 1);
- taken += 1;
- },
- .added => {
- try added.append(arena, .{ .text = std.mem.trimEnd(u8, raw, "\r")[1..], .whole = taken == 0 });
- taken -|= 1;
},
else => {},
}
}
+ if (k == Hunk.carrier) return;
+ // The patch view's rows of the same diff: a line it paired is one
+ // changed (its marker has no `ground_line`), its words `ground_emph`.
+ const patch = format: {
+ const ctx: @import("hunks.zig").Ctx = .{ .arena = arena };
+ break :format @import("hunks.zig").format(&ctx, "", diffed) catch |err| switch (err) {
+ error.OutOfMemory => return error.OutOfMemory,
+ else => return,
+ };
+ };
+ const syntax = @import("syntax.zig");
+ var said = std.mem.splitScalar(u8, patch.text, '\n');
+ var run_from = taken.items.len;
+ var plus_from = added.items.len;
+ for (patch.rows) |row| {
+ const row_text = said.next() orelse break;
+ if (row.kind == .hunk) {
+ pairRun(taken.items[run_from..], added.items[plus_from..], plus_from);
+ run_from = taken.items.len;
+ plus_from = added.items.len;
+ }
+ if (row.kind != .added and row.kind != .removed) continue;
+ const paired = row.styles.len > 0 and row.styles[0] & syntax.ground_line == 0;
+ const from = marks.items.len;
+ var i: usize = 1;
+ while (i < row.styles.len) {
+ if (row.styles[i] & syntax.ground_emph == 0) {
+ i += 1;
+ continue;
+ }
+ const run = i;
+ while (i < row.styles.len and row.styles[i] & syntax.ground_emph != 0) i += 1;
+ try marks.append(gpa, .{ @intCast(run - 1), @intCast(i - 1) });
+ }
+ const nmarks: u16 = @intCast(marks.items.len - from);
+ if (row.kind == .added)
+ try added.append(arena, .{ .text = row_text[@min(1, row_text.len)..], .whole = !paired, .marks = @intCast(from), .nmarks = nmarks, .new = @intCast(row.new orelse 0) })
+ else
+ try taken.append(arena, .{ .paired = paired, .marks = @intCast(from), .nmarks = nmarks });
+ }
+ pairRun(taken.items[run_from..], added.items[plus_from..], plus_from);
var next: usize = 0;
var rows = std.mem.splitScalar(u8, text, '\n');
- for (lines) |*line| {
+ const taken_from = taken.items.len - count: {
+ var n: usize = 0;
+ for (hunks.items) |h| if (h.pin == k) {
+ n += h.old_len;
+ };
+ break :count @min(n, taken.items.len);
+ };
+ for (lines, 0..) |*line, line_index| {
const row = rows.next() orelse break;
if (@intFromEnum(line.pin) != k) continue;
const from = next;
@@ -784,8 +964,293 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, diffed: []c
continue;
}
line.whole = added.items[next].whole;
+ if (!line.whole) {
+ line.marks = added.items[next].marks;
+ line.nmarks = added.items[next].nmarks;
+ }
+ added.items[next].shown = @intCast(line_index);
next += 1;
}
+ for (taken.items[taken_from..]) |*t| if (t.paired) {
+ t.new = added.items[t.partner].new;
+ t.shown = added.items[t.partner].shown;
+ };
+}
+
+/// A run's lines taken away paired with the lines written, in order (the
+/// alignment pairs each with a later one than the last it paired): each
+/// paired one's partner, by its place in `plus`'s list (from `base`).
+fn pairRun(minus: []Taken, plus: anytype, base: usize) void {
+ var j: usize = 0;
+ for (minus) |*m| if (m.paired) {
+ while (j < plus.len and plus[j].whole) j += 1;
+ if (j == plus.len) {
+ m.paired = false;
+ continue;
+ }
+ m.partner = base + j;
+ j += 1;
+ };
+}
+
+/// A run longer than this (lines taken away in one hunk) shows none of
+/// them as ghost rows; the patch view's alignment gives up past 256 too.
+const max_run = 1024;
+
+/// The ghost rows (`Ghost`), in `at` order: each line a pin took away
+/// whole or changed, where it stood (`carried` into the text); 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,
+/// older pins' first, each pin's description right after its own. A place
+/// inside lines rewritten since jj's snapshot (`Moved.place`) has no row.
+/// `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, taken: []const Taken, text: *std.ArrayList(u8), now: i64, moved: Moved) error{OutOfMemory}![]Ghost {
+ const Made = struct { g: Ghost, rank: u64 };
+ var made: std.ArrayList(Made) = .empty;
+ var ti: usize = 0;
+ for (hunks) |h| {
+ if (h.old_len == 0 or h.pin == Hunk.carrier) continue;
+ const run = taken[@min(ti, taken.len)..@min(ti + h.old_len, taken.len)];
+ ti += h.old_len;
+ // A changed line's old text right above the line; a line taken
+ // away whole above the next changed one's, or after the run.
+ var at = carried(hunks, h.pin, if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start, moved);
+ var ats: [max_run]?u32 = undefined;
+ if (run.len > max_run) continue;
+ var k = run.len;
+ while (k > 0) {
+ k -= 1;
+ if (run[k].paired) at = run[k].shown orelse carried(hunks, h.pin, run[k].new -| 1, moved);
+ ats[k] = at;
+ }
+ var from: usize = h.removed;
+ for (run, 0..) |t, n| {
+ const nl = std.mem.indexOfScalarPos(u8, text.items, from, '\n') orelse text.items.len;
+ defer from = nl + 1;
+ try made.append(arena, .{
+ .g = .{ .at = ats[n] orelse continue, .pin = h.pin, .kind = if (t.paired) .changed else .deleted, .line = h.old_start + @as(u32, @intCast(n)), .text = @intCast(from), .len = @intCast(nl - from), .marks = if (t.paired) t.marks else 0, .nmarks = if (t.paired) t.nmarks else 0 },
+ .rank = 1 << 32 | made.items.len << 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 place = at orelse carried(hunks, @intCast(k), end, moved) orelse continue;
+ // After its own ghost rows at its place.
+ for (made.items) |m| if (m.g.pin == k and m.g.at == place) {
+ rank = m.rank | 1;
+ };
+ const at_text = text.items.len;
+ try describe(gpa, text, pin, now);
+ try made.append(arena, .{
+ .g = .{ .at = place, .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
+/// text: moved by each later change's hunks before it (a pin's, or one
+/// between the pins the revset does not pin), or, inside a run of lines a
+/// later one took away, after what it wrote there; then from jj's lines
+/// to the text's (`moved`).
+fn carried(hunks: []const Hunk, k: u16, after: u32, moved: Moved) ?u32 {
+ const from = for (hunks) |h| {
+ if (h.pin == k) break h.step;
+ } else 0;
+ var pos: i64 = after;
+ var i: usize = 0;
+ while (i < hunks.len) {
+ const j = hunks[i].step;
+ var shift: i64 = 0;
+ var inside: ?i64 = null;
+ while (i < hunks.len and hunks[i].step == j) : (i += 1) {
+ const h = hunks[i];
+ if (j <= from) 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 > from) pos = inside orelse pos + shift;
+ }
+ return moved.place(@intCast(std.math.clamp(pos, 0, @as(i64, moved.was))));
+}
+
+/// 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 bytes a description row has at most, cut (`fitted`).
+pub const row_max = 256;
+
+/// A description row (`describe`: `id title author age`) as a body row of
+/// `room` cells shows it: whole when it fits, else its title cut to the
+/// room left (a `…` where it is cut), so the author and the age stay at
+/// its end (PARDES.md: one compact row, both shown).
+pub fn fitted(tab: u16, said: []const u8, room: usize, buf: *[row_max]u8) []const u8 {
+ if (said.len + 3 > row_max or File.displayWidth(tab, said) <= room) return said;
+ var tail = said.len;
+ for (0..2) |_| tail = std.mem.lastIndexOfScalar(u8, said[0..tail], ' ') orelse return said;
+ const head = @min(5, tail);
+ const keep = room -| File.displayWidth(tab, said[0..head]) -| File.displayWidth(tab, said[tail..]) -| 1;
+ const title = if (keep == 0) "" else std.mem.trimEnd(u8, said[head..tail][0..File.fitEnd(tab, said[head..tail], 0, keep)], " ");
+ var out: std.ArrayList(u8) = .initBuffer(buf);
+ out.appendSliceAssumeCapacity(said[0..head]);
+ out.appendSliceAssumeCapacity(title);
+ out.appendSliceAssumeCapacity("\u{2026}");
+ out.appendSliceAssumeCapacity(said[tail..]);
+ return out.items;
+}
+
+/// 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; `said`, the row's text as
+/// the body shows it (a description `fitted`).
+fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, said: []const u8, marks: []const [2]u32, 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),
+ .changed => {
+ 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 = th.lineno };
+ for (marks[g.marks..][0..g.nmarks]) |m| {
+ const from = File.displayWidth(p.settings.tab, said[0..@min(m[0], said.len)]);
+ const to = @min(w, File.displayWidth(p.settings.tab, said[0..@min(m[1], said.len)]));
+ if (from < to) s.tint(x + @as(u16, @intCast(from)), y, @intCast(to - from), pardes.colors.Grounds.of(th).del.emph, null);
+ }
+ },
+ .description => {
+ // `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 };
+ },
+ }
+}
+
+/// A changed line's words that came (`marks`, byte ranges of line `row`),
+/// green on body row `vr`, as much of them as the row shows.
+fn inkCame(p: *Pardes, s: *pardes.Surface, pane: *Pane, marks: []const [2]u32, row: i32, vr: u16, x: u16, y: u16, w: u16) void {
+ const tab = p.settings.tab;
+ const len: u32 = @intCast(File.sourceLine(pane, row).len);
+ const here = pane.wrapAt(tab, vr);
+ const next = pane.wrapAt(tab, @as(i32, vr) + 1);
+ const first: u32 = @intCast(here.at);
+ const end: u32 = if (next.line == row and !next.ghost) @intCast(next.at) else len;
+ for (marks) |m| {
+ const lo = @max(first, @min(m[0], len));
+ const hi = @min(end, @min(m[1], len));
+ if (lo >= hi) continue;
+ const from = here.pad + File.displayOffset(tab, pane, row, @intCast(first), @intCast(lo));
+ const to = @min(@as(i32, w), here.pad + File.displayOffset(tab, pane, row, @intCast(first), @intCast(hi)));
+ if (from >= 0 and from < to) s.tint(x + @as(u16, @intCast(from)), y, @intCast(to - from), pardes.colors.Grounds.of(p.theme()).add.emph, null);
+ }
+}
+
+/// 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.
@@ -878,9 +1343,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 +1362,24 @@ 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;
+ const g = pane.ghosts[gi];
+ const said = pane.ghost_text[g.text..][0..g.len];
+ var fit: [row_max]u8 = undefined;
+ inkGhost(p, s, g, if (g.kind == .description) fitted(p.settings.tab, said, File.bodyWidth(pane), &fit) else said, ch.marks, 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);
+ if (!line.whole and line.nmarks > 0) inkCame(p, s, pane, ch.marks[line.marks..][0..line.nmarks], row, vr, tx + gutter, y, tw - gutter);
+ break :line line.lanes;
+ };
+ for (lanes, 0..) |lane, level| {
if (lane == .none) break;
const rgb = colours[@intFromEnum(lane) % colours.len];
if (windowed) {
@@ -1090,18 +1567,21 @@ test "Changes pins a revset's changes on the pane, shown in this view: who wrote
const one: PinIndex = @enumFromInt(0);
const two: PinIndex = @enumFromInt(1);
const want = [_]Line{
- .{ .pin = two, .whole = false, .lanes = .{ two, .none, .none } },
+ .{ .pin = two, .whole = false, .lanes = .{ two, .none, .none }, .marks = 2, .nmarks = 2 },
.{ .pin = one, .whole = true, .lanes = .{ one, .none, .none } },
.{ .pin = two, .whole = true, .lanes = .{ two, .none, .none } },
};
try testing.expectEqualSlices(Line, &want, ch.lines);
+ // The patch view's alignment: the words that went (`one`, `1`), then
+ // those that came (`two`, `2`), byte ranges of each line.
+ try testing.expectEqualSlices([2]u32, &.{ .{ 6, 9 }, .{ 12, 13 }, .{ 6, 9 }, .{ 12, 13 } }, ch.marks);
const hunks = [_]Hunk{
- .{ .pin = 0, .old_start = 0, .old_len = 0, .new_start = 1, .new_len = 2, .removed = 0, .removed_len = 0 },
- .{ .pin = 1, .old_start = 1, .old_len = 1, .new_start = 1, .new_len = 1, .removed = 0, .removed_len = 15 },
- .{ .pin = 1, .old_start = 2, .old_len = 0, .new_start = 3, .new_len = 1, .removed = 15, .removed_len = 0 },
+ .{ .pin = 0, .step = 0, .old_start = 0, .old_len = 0, .new_start = 1, .new_len = 2, .removed = 0, .removed_len = 0 },
+ .{ .pin = 1, .step = 1, .old_start = 1, .old_len = 1, .new_start = 1, .new_len = 1, .removed = 0, .removed_len = 15 },
+ .{ .pin = 1, .step = 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);
@@ -1183,7 +1663,7 @@ const WorkerJj = struct {
shell: *Shell,
a: *R.Authority,
r: *R.Replica,
- /// The revision `@-` of the repository's `a.zig`, open.
+ /// The repository's `a.zig` open (`at`: `@@-`, its revision `@-`).
id: usize,
serial: u32,
@@ -1193,7 +1673,7 @@ const WorkerJj = struct {
}
const vtable: pardes.Host.VTable = .{ .search = search };
- fn init(repo: *const Repo) !WorkerJj {
+ fn init(repo: *const Repo, at: []const u8) !WorkerJj {
const shell = try testing.allocator.create(Shell);
shell.* = .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } };
const a = try R.Authority.init(testing.allocator, .{ .cols = 80, .rows = 24 }, .{ .vtable = &vtable, .ctx = shell }, false);
@@ -1201,7 +1681,7 @@ const WorkerJj = struct {
shell.core = a.core;
const r = try R.Replica.init(testing.allocator, 1, try a.unit(0, 0), false);
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 = try std.fmt.bufPrint(&line, "Look {s}{s}", .{ repo.file, at }) }));
const id = a.core.view().active;
return .{ .shell = shell, .a = a, .r = r, .id = id, .serial = a.core.panes[id].?.serial };
}
@@ -1244,7 +1724,7 @@ const WorkerJj = struct {
test "Changes asks jj on a worker: the word says so and returns, its answer lands as one input and pins; a client replays that input and ends equal" {
var repo = try Repo.init(testing.allocator) orelse return error.SkipZigTest;
defer repo.deinit(testing.allocator);
- var w = try WorkerJj.init(&repo);
+ var w = try WorkerJj.init(&repo, "@@-");
defer w.deinit();
try w.word("Changes @--::@-");
// Asked, not answered: the word did not wait for jj.
@@ -1258,7 +1738,7 @@ test "Changes asks jj on a worker: the word says so and returns, its answer land
test "a second Changes before the first's answer: only the second's pins" {
var repo = try Repo.init(testing.allocator) orelse return error.SkipZigTest;
defer repo.deinit(testing.allocator);
- var w = try WorkerJj.init(&repo);
+ var w = try WorkerJj.init(&repo, "@@-");
defer w.deinit();
try w.word("Changes @--::@-");
try w.word("Changes @-");
@@ -1271,7 +1751,7 @@ test "a second Changes before the first's answer: only the second's pins" {
test "the pane closed before its Changes answer: nothing pinned, nothing kept" {
var repo = try Repo.init(testing.allocator) orelse return error.SkipZigTest;
defer repo.deinit(testing.allocator);
- var w = try WorkerJj.init(&repo);
+ var w = try WorkerJj.init(&repo, "@@-");
defer w.deinit();
try w.word("Changes @--::@-");
try w.word("Del");
@@ -1311,10 +1791,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 +1827,332 @@ 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;
+ /// `b();` the old text of the line `mid` changed, right above it.
+ fn want(said: [3][]const u8) [12][]const u8 {
+ return .{ "fn f() void {", " a();", " b();", " 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", "02", "01", "01", "01", "0", "0", "0" };
+ const Ground = enum { none, add, del };
+ const ground = [_]Ground{ .add, .add, .none, .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{ 5, 8, 11 }, [_]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 + 6).style.fg);
+ // The changed line: on its old text the word that went (`b`) red,
+ // on it the word that came (`b2`) green; the rest of each neither.
+ const text_x = body.x + File.gutterWidth(pane);
+ for ([_]u16{ 4, 5 }, [_]bool{ true, false }) |col, went| try testing.expectEqual(went, std.meta.eql(s.at(text_x + col, body.y + 2).style.bg, pardes.Color{ .rgb = grounds.del.emph }));
+ for ([_]u16{ 4, 5, 6 }, [_]bool{ true, true, false }) |col, came| try testing.expectEqual(came, std.meta.eql(s.at(text_x + col, body.y + 3).style.bg, pardes.Color{ .rgb = grounds.add.emph }));
+ // 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))[5]));
+ }
+}
+
+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, 9), 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 + 6).style.bg);
+ try testing.expect(!std.meta.eql(s.at(x, body.y + 6).style.bg, s.at(x, body.y + 4).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 + 6 } });
+ p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = x, .row = body.y + 6 } });
+ 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, &.{ 2, 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, &.{ 1, 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, &.{ 1, 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()));
+}
+
+test "a description's ghost row keeps its author and age: in a pane narrower than the row its title gives way, cut to the room" {
+ const gpa = testing.allocator;
+ const title = "a title long enough that no narrow pane holds the whole of it beside its author and its age";
+ var repo = try Repo.with(gpa, &.{.{ "fn f() void {\n a();\n}\n", title }}, null) orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ for ([_]bool{ false, true }) |windowed| {
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 50, .rows = 20, .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].?;
+ p.update(.{ .command = "Changes ::@" });
+ const ch = answered(p, pane.serial).?;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var buf: [64][]const u8 = undefined;
+ const got = try Ghosts.rows(p, pane, arena.allocator(), &buf);
+ const row = Ghosts.text(pane, got[3]);
+ const room = pane.cols - File.gutterWidth(pane) - 1;
+ try testing.expect(File.displayWidth(p.settings.tab, row) <= room);
+ try testing.expectStringStartsWith(row, try std.fmt.allocPrint(arena.allocator(), "{s} a title long", .{ch.pins[0].id[0..4]}));
+ try testing.expectStringEndsWith(row, "\u{2026} t now");
+ // Drawn: the id and the title in its lane's colour, the author and
+ // the age grey, at the row's end.
+ p.sync();
+ const s = try p.render(arena.allocator());
+ const body = s.region(.body, id).?.rect;
+ const x = body.x + File.gutterWidth(pane);
+ var cbuf: [8][3]u8 = undefined;
+ try testing.expectEqual(pardes.Color{ .rgb = laneColours(p.theme(), &cbuf)[0] }, s.at(x, body.y + 3).style.fg);
+ const age_x = x + @as(u16, @intCast(File.displayWidth(p.settings.tab, row))) - 1;
+ try testing.expectEqualStrings("w", s.at(age_x, body.y + 3).grapheme());
+ try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(age_x, body.y + 3).style.fg);
+ try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(age_x - 4, body.y + 3).style.fg);
+ }
+}
+
+test "at one place a change's ghost rows come before its description: a line it changed and the next it took away, then its description" {
+ const gpa = testing.allocator;
+ const fn_text = "fn f() void {\n a();\n b();\n c();\n d();\n}\n";
+ const chg_text = "fn f() void {\n a();\n b2();\n d();\n}\n";
+ var repo = try Repo.with(gpa, &.{ .{ fn_text, "fn" }, .{ chg_text, "chg" } }, null) 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 pane = p.panes[p.view().active].?;
+ p.update(.{ .command = "Changes ::@" });
+ const ch = answered(p, pane.serial).?;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const said = [2][]const u8{ try std.fmt.allocPrint(arena.allocator(), "{s} fn t now", .{ch.pins[0].id[0..4]}), try std.fmt.allocPrint(arena.allocator(), "{s} chg t now", .{ch.pins[1].id[0..4]}) };
+ var buf: [64][]const u8 = undefined;
+ const got = try Ghosts.rows(p, pane, arena.allocator(), &buf);
+ const want = [_][]const u8{ "fn f() void {", " a();", " b();", " b2();", " c();", said[1], " d();", "}", said[0] };
+ for (want, got[0..want.len]) |w, row| try testing.expectEqualStrings(w, Ghosts.text(pane, row));
+}
+
+test "a change the revset does not pin, between two it does, moves the older one's ghost rows to where they stand: jj's answer has its hunks" {
+ const gpa = testing.allocator;
+ const fn_text = "fn f() void {\n a();\n b();\n c();\n d();\n}\n";
+ // `old` takes `c();` away; `mid` (not pinned) writes two lines above
+ // it; `last` changes `d();`.
+ const old_text = "fn f() void {\n a();\n b();\n d();\n}\n";
+ const mid_text = "fn f() void {\n a();\n x();\n y();\n b();\n d();\n}\n";
+ const last_text = "fn f() void {\n a();\n x();\n y();\n b();\n d2();\n}\n";
+ var repo = try Repo.with(gpa, &.{ .{ fn_text, "fn" }, .{ old_text, "old" }, .{ mid_text, "mid" }, .{ last_text, "last" } }, null) 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 pane = p.panes[p.view().active].?;
+ p.update(.{ .command = "Changes @--- | @-" });
+ const ch = answered(p, pane.serial).?;
+ try testing.expectEqual(@as(usize, 2), ch.pins.len);
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const said = [2][]const u8{ try std.fmt.allocPrint(arena.allocator(), "{s} old t now", .{ch.pins[0].id[0..4]}), try std.fmt.allocPrint(arena.allocator(), "{s} last t now", .{ch.pins[1].id[0..4]}) };
+ var buf: [64][]const u8 = undefined;
+ const got = try Ghosts.rows(p, pane, arena.allocator(), &buf);
+ const want = [_][]const u8{ "fn f() void {", " a();", " x();", " y();", " b();", " c();", said[0], " d();", " d2();", said[1], "}" };
+ for (want, got[0..want.len]) |w, row| try testing.expectEqualStrings(w, Ghosts.text(pane, row));
+}
+
+test "lines typed between the word and jj's answer: the ghost rows stand where they stood, carried across them" {
+ var repo = try Ghosts.repo() orelse return error.SkipZigTest;
+ defer repo.deinit(testing.allocator);
+ var w = try WorkerJj.init(&repo, "");
+ defer w.deinit();
+ try w.word("Changes ::@");
+ try testing.expect(answered(w.a.core, w.serial) == null);
+ // A line opened above `a();` before the answer lands.
+ try w.word("Look a.zig:2");
+ for ("Oz();") |k| w.r.confirm(try w.a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }));
+ w.r.confirm(try w.a.local(.{ .key = .{ .cp = pardes.Key.escape } }));
+ try w.land();
+ try testing.expectStringStartsWith(w.a.core.panes[w.id].?.file.?.content, "fn f() void {\n z();\n a();\n");
+ var got: [8]u32 = undefined;
+ const ch = answered(w.a.core, w.serial).?;
+ for (ch.ghosts, got[0..ch.ghosts.len]) |g, *o| o.* = g.at;
+ try testing.expectEqualSlices(u32, &.{ 3, 5, 5, 5, 5, 7 }, got[0..ch.ghosts.len]);
+ try testing.expectEqualSlices(Ghost, ch.ghosts, answered(w.r.core(), w.serial).?.ghosts);
+}
+
/// 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 {