summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 14:54:39 -0300
committerGabriel Schneider <[email protected]>2026-11-23 14:57:18 -0300
commit0e2012de121a9735d3c2ab7871a04d144636b849 (patch)
tree5ec65db3f88cc73ff7c846ba674e5d7b9015c205 /core
parent3728b4704962672688f0b4461d20481263378165 (diff)
downloadpardes-0e2012de121a9735d3c2ab7871a04d144636b849.tar.gz
pardes-0e2012de121a9735d3c2ab7871a04d144636b849.zip
the Changes layer from the middle, and every way into a patch view: in a file at a revision a pinned change behind it (the revision or an ancestor) shows what it took away where it stood, and one ahead of it (a descendant) shows the lines it will write as read-only lines, green, where they will stand, the lines it will take away the revision's own text, red across, going; a change that is neither (a sibling) is left out (round 7's evidence, `layer-gone/mid2.py`: a deleted line's ghost a line late there, a change ahead drawn as behind); the causes: `carried` moved a pin's place through every later change's hunks, those ahead of the shown revision too, so jj's log now marks each change behind (`::REV`) or ahead (`logTemplate`: `p`/`c` behind, `a`/`d` ahead; the query takes the revset's changes behind or ahead and the unpinned ones between) and `carried` passes only the changes behind (`Hunk.ahead`); a pin ahead's places are carried back from its parent through the changes ahead before it (`carriedBack`), the lines it takes away marked (`Line.going`, its lane beside them: `going`), the lines it writes the layer's (`Ghost.Kind.added`), its description after them; and "a changed line's old text below the new where the guide says above": a replaced line the patch view's alignment does not pair was a line taken away standing after what was written in its place: now a diff's order, what went right above what came (lines taken away after a run's last changed line right after its new text; with none changed, before the lines written in their place), in the file's own pane and from the middle alike; Churn counts no change ahead; `Diff` with a word that names no file, in a jj repository, takes it for a revision (`Diff @-`, a bookmark): its patch with jj show's header (`jj.diffChange(..., any)`), the one way in that had none (round 7's table; `jj diff` prints no header, so its paged text has none to keep; a header was never dropped on purpose in core/hunks.zig's and core/diff.zig's history); the guide's chapter (the layer the pane's, its lines text and read-only, what an edit there says, what reads the file without them, from the middle, `Diff @-`), the cheatsheet's line, the `Changes` builtin's doc and both JSON lists; tests (core/jj.zig): "from the middle: in a revision's pane a pin behind it ... a pin ahead of it ..." (terminal and window), "a line replaced by one too unlike it ... a diff's order ...", "Diff with a revision jj takes (`@-`, a bookmark) ..."; not gated yet: jj's 44 tests pass (core-test filtered), the book builds
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
-rw-r--r--core/builtins.zig2
-rw-r--r--core/hunks.zig9
-rw-r--r--core/jj.zig279
3 files changed, 245 insertions, 45 deletions
diff --git a/core/builtins.zig b/core/builtins.zig
index fbb82fc3..7bdb773f 100644
--- a/core/builtins.zig
+++ b/core/builtins.zig
@@ -1080,7 +1080,7 @@ pub const At = struct {
pub const run = @import("jj.zig").word;
};
-/// Pin the changes `revset` names (jj) on a file's pane and show them in this view: a lane each after the line number, a line one added whole green, what the file no longer has drawn where it stood (ghost rows: a line taken away whole red, each change's description after its last line); bare, shown here or not (none pinned yet: `@`).
+/// Pin the changes `revset` names (jj) on a file's pane and show them in every view of it: a lane each after the line number, a line one added whole green, what the file no longer has as lines of its text where it stood, read-only (a line taken away whole red, each change's description after its last line); bare, shown or not (none pinned yet: `@`).
pub const Changes = struct {
pub const takes_arg = true;
pub const run = @import("jj.zig").changesWord;
diff --git a/core/hunks.zig b/core/hunks.zig
index e109b788..db2401f6 100644
--- a/core/hunks.zig
+++ b/core/hunks.zig
@@ -703,7 +703,9 @@ fn trimmedWidth(ctx: *const Ctx, section: []const u8) usize {
/// a digit among them), `git show` of it, paged into a +Pager that stacks
/// its hunks (ctl.fillPager), run in the pane's git work tree; with a jj
/// change (`Diff wrnnmymq [path[:line]]`: jj.diffChange), its patch, header
-/// and all, the cursor at that place in it. A plumbing
+/// and all, the cursor at that place in it; with any other word that names
+/// no file, in a jj repository, the revision jj takes it for (`Diff @-`, a
+/// bookmark), the same. A plumbing
/// rule's Diff that cannot (no hunk, no work tree) declines, saying
/// nothing (Pardes.plumb_declined), and the look looks itself.
pub fn word(c: pardes.builtins.Ctx) void {
@@ -734,12 +736,15 @@ pub fn word(c: pardes.builtins.Ctx) void {
return;
}
// A jj change (its letters k to z), with a place in its patch to stand at.
- if (@import("jj.zig").diffChange(p, c.id, arg)) return;
+ if (@import("jj.zig").diffChange(p, c.id, arg, false)) return;
if (@import("jj.zig").isChangeId(arg[0 .. std.mem.indexOfAny(u8, arg, " \t") orelse arg.len])) return declined(p, c.id, "Diff: no such change");
var buf: [4096]u8 = undefined;
const path = if (arg[0] == '/') arg else std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, Pardes.paneDir(c.pane), "/"), arg }) catch arg;
const bytes = pardes.filesystem.read(p, path) catch |err| {
if (p.plumb_declined != null) return declined(p, c.id, "");
+ // No such file: a revision jj takes (`Diff @-`, a bookmark), its
+ // patch with its header, as a change's.
+ if (err == error.FileNotFound and @import("jj.zig").diffChange(p, c.id, arg, true)) return;
return p.reportError(.at(c.id), path, err);
};
defer p.gpa.free(bytes);
diff --git a/core/jj.zig b/core/jj.zig
index 18926594..2ecd58a7 100644
--- a/core/jj.zig
+++ b/core/jj.zig
@@ -339,6 +339,9 @@ pub const Line = struct {
/// byte ranges of the line).
marks: u32 = 0,
nmarks: u16 = 0,
+ /// The pin ahead of the shown revision that takes it away: a line
+ /// going (`going`).
+ going: PinIndex = .none,
};
/// The lanes a row shows at most (a window's strips the same).
@@ -361,6 +364,9 @@ pub const Hunk = struct {
pin: u16,
/// Its change's place in jj's answer, oldest first.
step: u16,
+ /// Its change is ahead of the shown revision (a descendant): its
+ /// `removed` text is what it adds (`carriedBack` carries its places).
+ ahead: bool = false,
old_start: u32,
old_len: u32,
new_start: u32,
@@ -400,8 +406,9 @@ pub const Ghost = struct {
author: u32 = 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 };
+ /// place right below it; a pin's description; a line a pin ahead of the
+ /// shown revision will write, after the lines it will take away.
+ pub const Kind = enum(u8) { deleted, changed, description, added };
};
/// The `Changes` word (builtins.zig registers it): in a file's pane (or a
@@ -658,12 +665,15 @@ 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.
- // 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);
+ // Of the revset, those behind the shown revision (its ancestors, it
+ // too) and those ahead of it (its descendants); a sibling of it is
+ // neither and is left out (its lines are no revision's on its line).
+ // With them the changes the revset does not pin between the first and
+ // the shown revision, and between it and those ahead (marked so),
+ // whose hunks move the places of the pins' lines.
+ const query = std.fmt.allocPrint(gpa, "((({0s}) & (::({1s}) | ({1s})::)) | (roots({0s})::({1s})) | (({1s})::({0s}))) & files({2s})", .{ a.revset, a.rev, set }) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
defer gpa.free(query);
- const template = logTemplate(gpa, a.revset) catch return failed(gpa, a, refusal(gpa, "out of memory").refused);
+ const template = logTemplate(gpa, a.revset, a.rev) 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)) {
@@ -941,14 +951,16 @@ fn heatOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8,
var windowed: std.ArrayList(bool) = .empty;
var starts: std.ArrayList(usize) = .empty;
var it = std.mem.splitScalar(u8, logged, '\n');
+ var ahead: std.ArrayList(bool) = .empty;
while (it.next()) |l| if (entryOf(l)) |e| {
if (windowed.items.len == Hunk.carrier) break;
try windowed.append(arena, e.pinned);
+ try ahead.append(arena, e.ahead);
try starts.append(arena, it.index orelse logged.len);
};
try starts.append(arena, logged.len);
for (starts.items[0 .. starts.items.len - 1], starts.items[1..], 0..) |from, to, i|
- try hunksOf(arena, arena, Hunk.carrier, @intCast(i), logged[from..to], text, &.{}, &hunks, &removed, undefined, undefined);
+ try hunksOf(arena, arena, Hunk.carrier, @intCast(i), ahead.items[i], logged[from..to], text, &.{}, &hunks, &removed, undefined, undefined);
const n = lineCount(text);
const moved: Moved = .of(was, text);
// Each line's innermost declaration (outermost first, so an inner one
@@ -966,7 +978,8 @@ fn heatOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8,
const seen = try arena.alloc(u32, decls.len);
@memset(seen, none);
for (hunks.items) |h| {
- if (!windowed.items[h.step] or n == 0) continue;
+ // A change ahead of the shown revision touched none of its lines.
+ if (!windowed.items[h.step] or h.ahead or n == 0) continue;
const a = carried(hunks.items, h.step, h.new_start -| 1 + @intFromBool(h.new_len == 0), moved) orelse continue;
const b = carried(hunks.items, h.step, h.new_start -| 1 + h.new_len, moved) orelse continue;
const lo = @min(if (h.new_len == 0) a -| 1 else a, n - 1);
@@ -1090,7 +1103,7 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a
var whole: std.ArrayList([2][]const u8) = .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 };
+ const Step = struct { pin: u16, diff: usize, ahead: bool = false };
var steps: std.ArrayList(Step) = .empty;
var it = std.mem.splitScalar(u8, logged, '\n');
while (it.next()) |l| if (entryOf(l)) |e| {
@@ -1098,7 +1111,7 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a
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);
if (e.pinned) try whole.append(arena, .{ e.author, e.title });
- try steps.append(arena, .{ .pin = if (e.pinned) @intCast(pins.items.len - 1) else Hunk.carrier, .diff = it.index orelse logged.len });
+ try steps.append(arena, .{ .pin = if (e.pinned) @intCast(pins.items.len - 1) else Hunk.carrier, .diff = it.index orelse logged.len, .ahead = e.ahead });
};
try steps.append(arena, .{ .pin = Hunk.carrier, .diff = logged.len });
out.pins = try pins.toOwnedSlice(gpa);
@@ -1116,9 +1129,10 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a
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);
+ for (steps.items[0 .. steps.items.len - 1], steps.items[1..], 0..) |s, next, i| try hunksOf(gpa, arena, s.pin, @intCast(i), s.ahead, logged[s.diff..next.diff], text, out.lines, &hunks, &removed, &marks, &taken);
out.hunks = try hunks.toOwnedSlice(gpa);
nest(out.lines, out.pins.len);
+ going(out.lines, out.hunks, moved);
out.ghosts = try ghostsOf(gpa, arena, out.pins, whole.items, out.lines, out.hunks, taken.items, &removed, now, moved);
out.ghost_text = try removed.toOwnedSlice(gpa);
out.marks = try marks.toOwnedSlice(gpa);
@@ -1126,25 +1140,35 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a
return .{ .read = out };
}
-/// `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 {
+/// `jj log`'s line a change, before its diff: its id, its mark (`p` when
+/// `revset` pins it and it is behind the shown revision `rev` or is it, `a`
+/// pinned and ahead; `c` and `d` the same for one it does not pin), its
+/// author's time, its author, its description's first line, a tab between.
+fn logTemplate(gpa: std.mem.Allocator, revset: []const u8, rev: []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\"");
+ const Quoted = struct {
+ fn put(g: std.mem.Allocator, out: *std.ArrayList(u8), text: []const u8) error{OutOfMemory}!void {
+ for (text) |c| {
+ if (c == '"' or c == '\\') try out.append(g, '\\');
+ try out.append(g, c);
+ }
+ }
+ };
+ try t.appendSlice(gpa, "change_id.short(8) ++ \"\\t\" ++ if(self.contained_in(\"::(");
+ try Quoted.put(gpa, &t, rev);
+ try t.appendSlice(gpa, ")\"), if(self.contained_in(\"");
+ try Quoted.put(gpa, &t, revset);
+ try t.appendSlice(gpa, "\"), \"p\", \"c\"), if(self.contained_in(\"");
+ try Quoted.put(gpa, &t, revset);
+ try t.appendSlice(gpa, "\"), \"a\", \"d\")) ++ \"\\t\" ++ author.timestamp().utc().format(\"%s\") ++ \"\\t\" ++ author.name() ++ \"\\t\" ++ description.first_line() ++ \"\\n\"");
return t.toOwnedSlice(gpa);
}
/// 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 entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool, author: []const u8, title: []const u8 } {
+fn entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool, ahead: bool, author: []const u8, title: []const u8 } {
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[11..], '\t');
@@ -1155,7 +1179,7 @@ fn entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool, author: []const u8,
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 = pin, .pinned = l[9] == 'p', .author = author, .title = title };
+ return .{ .pin = pin, .pinned = l[9] == 'p' or l[9] == 'a', .ahead = l[9] == 'a' or l[9] == 'd', .author = author, .title = title };
}
/// Which pin wrote each line of `text` (what the pane shows: `whole` is the
@@ -1262,7 +1286,7 @@ const Taken = struct { paired: bool, marks: u32, nmarks: u16, new: u32 = 0, show
/// 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 {
+fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, step: u16, ahead: bool, 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 = .{};
@@ -1272,9 +1296,17 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, step: u16,
switch (l.kind) {
.hunk => {
const h = diff.parseHunk(std.mem.trimEnd(u8, raw, "\r")).?;
- 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 });
+ try hunks.append(gpa, .{ .pin = k, .step = step, .ahead = ahead, .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 });
+ },
+ // A pin ahead of the shown revision: the lines it will write
+ // are what the text has not.
+ .added => if (k != Hunk.carrier and ahead) {
+ 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);
},
- .removed => if (k != Hunk.carrier) {
+ .removed => if (k != Hunk.carrier and !ahead) {
const body = std.mem.trimEnd(u8, raw, "\r")[1..];
try removed.appendSlice(gpa, body);
try removed.append(gpa, '\n');
@@ -1283,7 +1315,7 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, step: u16,
else => {},
}
}
- if (k == Hunk.carrier) return;
+ if (k == Hunk.carrier or ahead) 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: {
@@ -1390,14 +1422,40 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin,
var made: std.ArrayList(Made) = .empty;
var ti: usize = 0;
for (hunks) |h| {
+ // A pin ahead: the lines it will write, after those it will take
+ // away (or where it writes), where they will stand.
+ if (h.ahead) {
+ if (h.pin == Hunk.carrier or h.new_len == 0) continue;
+ const at = carriedBack(hunks, h.step, if (h.old_len == 0) h.old_start else h.old_start - 1 + h.old_len, moved) orelse continue;
+ var from: usize = h.removed;
+ for (0..h.new_len) |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 = at, .pin = h.pin, .kind = .added, .line = h.new_start + @as(u32, @intCast(n)), .text = @intCast(from), .len = @intCast(nl - from) },
+ .rank = 1 << 32 | made.items.len << 1,
+ });
+ }
+ continue;
+ }
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.step, if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start, moved);
- var ats: [max_run]?u32 = undefined;
+ // away whole above the next changed one's; after the last changed
+ // one, right after its new text; with none changed, before the
+ // lines written in their place (a diff's order: what went, then
+ // what came).
if (run.len > max_run) continue;
+ var last: ?usize = null;
+ for (run, 0..) |t, n| if (t.paired) {
+ last = n;
+ };
+ var at = if (last) |n| next: {
+ const shown = run[n].shown orelse carried(hunks, h.step, run[n].new -| 1, moved);
+ break :next if (shown) |v| v + 1 else null;
+ } else carried(hunks, h.step, if (h.new_len > 0) h.new_start - 1 else h.new_start, moved);
+ var ats: [max_run]?u32 = undefined;
var k = run.len;
while (k > 0) {
k -= 1;
@@ -1420,7 +1478,7 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin,
var i = lines.len;
while (i > 0) {
i -= 1;
- if (@intFromEnum(lines[i].pin) == k) {
+ if (@intFromEnum(lines[i].pin) == k or @intFromEnum(lines[i].going) == k) {
at = @intCast(i + 1);
break;
}
@@ -1434,12 +1492,15 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin,
var first: u32 = 0;
var end: u32 = 0;
var step: u16 = 0;
+ var ahead = false;
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;
+ // A pin ahead: after the lines it takes away, in its parent's.
+ end = if (h.ahead) h.old_start - 1 + h.old_len + @intFromBool(h.old_len == 0) else if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start;
step = h.step;
+ ahead = h.ahead;
};
- const place = at orelse carried(hunks, step, end, moved) orelse continue;
+ const place = at orelse (if (ahead) carriedBack(hunks, step, end, moved) else carried(hunks, step, 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;
@@ -1475,7 +1536,8 @@ fn carried(hunks: []const Hunk, from: u16, after: u32, moved: Moved) ?u32 {
var inside: ?i64 = null;
while (i < hunks.len and hunks[i].step == j) : (i += 1) {
const h = hunks[i];
- if (j <= from) continue;
+ // A change ahead of the shown revision moves nothing in it.
+ if (j <= from or h.ahead) 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) {
@@ -1487,6 +1549,49 @@ fn carried(hunks: []const Hunk, from: u16, after: u32, moved: Moved) ?u32 {
return moved.place(@intCast(std.math.clamp(pos, 0, @as(i64, moved.was))));
}
+/// Where the place after line `after` of the revision step `from`'s change
+/// (a pin ahead of the shown revision) was made from is in the text: moved
+/// back through each change ahead of the shown revision before it, newest
+/// first, their hunks turned round; then from jj's lines to the text's
+/// (`moved`).
+fn carriedBack(hunks: []const Hunk, from: u16, after: u32, moved: Moved) ?u32 {
+ var pos: i64 = after;
+ var i = hunks.len;
+ while (i > 0) {
+ const j = hunks[i - 1].step;
+ var lo = i;
+ while (lo > 0 and hunks[lo - 1].step == j) lo -= 1;
+ defer i = lo;
+ if (j >= from or !hunks[lo].ahead) continue;
+ var shift: i64 = 0;
+ var inside: ?i64 = null;
+ for (hunks[lo..i]) |h| {
+ if (h.new_len == 0) {
+ if (h.new_start < pos) shift += h.old_len;
+ } else if (h.new_start + h.new_len - 1 <= pos) {
+ shift += @as(i64, h.old_len) - h.new_len;
+ } else if (h.new_start <= pos) inside = @as(i64, h.old_start) - 1 + h.old_len;
+ }
+ pos = inside orelse pos + shift;
+ }
+ return moved.place(@intCast(std.math.clamp(pos, 0, @as(i64, moved.was))));
+}
+
+/// The lines of the text a pin ahead of the shown revision takes away,
+/// going (`Line.going`), its lane beside them.
+fn going(lines: []Line, hunks: []const Hunk, moved: Moved) void {
+ for (hunks) |h| {
+ if (!h.ahead or h.pin == Hunk.carrier or h.old_len == 0) continue;
+ const first = carriedBack(hunks, h.step, h.old_start - 1, moved) orelse continue;
+ const pin: PinIndex = @enumFromInt(h.pin);
+ for (lines[@min(first, lines.len)..@min(first + h.old_len, lines.len)]) |*l| {
+ l.going = pin;
+ if (std.mem.indexOfScalar(PinIndex, &l.lanes, pin) != null) continue;
+ if (std.mem.indexOfScalar(PinIndex, &l.lanes, .none)) |free| l.lanes[free] = pin;
+ }
+ }
+}
+
/// A pin's description line (PARDES.md: `id title author age`, one blank
/// between): its id's first four letters, its whole title (its
/// description's first line), its whole author, its age short (`5h`, `2d`,
@@ -1574,6 +1679,7 @@ fn inkGhost(p: *Pardes, s: *pardes.Surface, pane: *const Pane, g: Ghost, row: i3
_ = tab;
switch (g.kind) {
.deleted => s.tint(x, y, w, pardes.colors.Grounds.of(th).del.row, th.lineno),
+ .added => s.tint(x, y, w, pardes.colors.Grounds.of(th).add.row, null),
.changed => {
ink.fg(0, said.len, th.lineno);
for (marks[g.marks..][0..g.nmarks]) |m| {
@@ -1620,7 +1726,8 @@ fn ownerOf(ch: *const Changes, f: *const File.State, row: usize) PinIndex {
return if (g.kind == .description) .none else @enumFromInt(g.pin);
}
const line = File.fileRow(f, row);
- return if (line < ch.lines.len) ch.lines[line].pin else .none;
+ if (line >= ch.lines.len) return .none;
+ return if (ch.lines[line].going != .none) ch.lines[line].going else ch.lines[line].pin;
}
/// The line `]g` (`forward`) or `[g` goes to from buffer line `from` of
@@ -1687,14 +1794,16 @@ pub fn isChangeId(text: []const u8) bool {
/// prints it (its ids, author, dates and whole message above its hunks),
/// stacked in its +Diff (one a change: a second goes to it); the cursor on
/// the patch's row for `path`'s line `line` (that file's first row
-/// without one; none of it: the top). False when it names no change.
-pub fn diffChange(p: *Pardes, id: usize, arg: []const u8) bool {
+/// without one; none of it: the top). With `any`, `change` is any word jj
+/// takes for one revision (`@-`, a bookmark, a commit). False when it
+/// names none.
+pub fn diffChange(p: *Pardes, id: usize, arg: []const u8, any: bool) bool {
var words = std.mem.tokenizeAny(u8, arg, " \t");
const change = words.next() orelse return false;
- if (!isChangeId(change)) return false;
+ if (!any and !isChangeId(change)) return false;
const dir = pardes.filesystem.localPath(Pardes.paneDir(p.panes[id].?)) orelse return false;
- var buf: [64]u8 = undefined;
- const revset = std.fmt.bufPrint(&buf, "change_id({s})", .{change}) catch return false;
+ var buf: [128]u8 = undefined;
+ const revset = if (any) change else std.fmt.bufPrint(&buf, "change_id({s})", .{change}) catch return false;
const shown = p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dir, @as([]const []const u8, &.{ "show", "--git", "-r", revset }) });
const bytes = switch (shown) {
.refused => |why| {
@@ -1939,13 +2048,16 @@ fn paintLanes(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u
const first_row = w.at <= 0;
inkGhost(p, s, pane, g, w.line, @intCast(@max(0, w.at)), to, w.pad, ch.marks, if (one == null) colours[g.pin % colours.len] else null, tx + gutter, y, tw - gutter);
if (g.kind == .description and first_row) points = @enumFromInt(g.pin);
- taken = g.kind != .description or takenAbove(ch.ghosts, gi);
+ taken = g.kind == .deleted or g.kind == .changed or (g.kind == .description and takenAbove(ch.ghosts, gi));
break :ghost ghostLanes(ch, ch.ghosts, gi, first_row);
} else line: {
const at = File.fileRow(f, row);
if (at >= ch.lines.len) continue;
const line = ch.lines[at];
if (line.whole and line.pin != .none) s.tint(tx + gutter, y, tw - gutter, ground, null);
+ // Going: a pin ahead takes it away.
+ if (line.going != .none) s.tint(tx + gutter, y, tw - gutter, pardes.colors.Grounds.of(p.theme()).del.row, null);
+ taken = line.going != .none;
if (!line.whole and line.nmarks > 0) inkCame(p, s, pane, ch.marks[line.marks..][0..line.nmarks], w.line, vr, tx + gutter, y, tw - gutter);
break :line line.lanes;
};
@@ -1978,6 +2090,7 @@ fn takenAbove(ghosts: []const Ghost, gi: usize) bool {
while (j > 0) {
j -= 1;
if (ghosts[j].at != ghosts[gi].at) return false;
+ if (ghosts[j].kind == .added) return false;
if (ghosts[j].kind != .description) return true;
}
return false;
@@ -2697,6 +2810,32 @@ test "a Look on a change's id opens its patch, its header and whole message at t
}
}
+test "Diff with a revision jj takes (`@-`, a bookmark) and no file of that name: the revision's patch, its commit's ids, author, date and whole message at its top; a word that names neither is still no such file" {
+ const gpa = testing.allocator;
+ var repo = try Ghosts.repo() orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ if (!try Repo.run(gpa, repo.dir, &.{ "bookmark", "create", "-r", "@--", "cutmark" })) return error.SkipZigTest;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 30, .windowed = false });
+ defer p.deinit();
+ var line: [4200]u8 = undefined;
+ for ([_]struct { []const u8, []const u8 }{ .{ "@-", "\n mid\n" }, .{ "cutmark", "\n cut\n" } }) |case| {
+ p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) });
+ const from = p.view().active;
+ p.update(.{ .command = try std.fmt.bufPrint(&line, "Diff {s}", .{case[0]}) });
+ const shown = p.panes[p.view().active].?;
+ try testing.expect(p.view().active != from);
+ try testing.expectStringStartsWith(shown.file.?.content, "Commit ID: ");
+ try testing.expect(std.mem.indexOf(u8, shown.file.?.content, "\nAuthor : t <t@t>") != null);
+ try testing.expect(std.mem.indexOf(u8, shown.file.?.content, case[1]) != null);
+ try testing.expect(std.mem.indexOf(u8, shown.file.?.content, "a.zig") != null);
+ }
+ p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) });
+ const id = p.view().active;
+ p.update(.{ .command = "Diff nosuchword" });
+ try testing.expectEqual(id, p.view().active);
+ try testing.expect(std.mem.indexOf(u8, told(p, id), "nosuchword") != null);
+}
+
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);
@@ -3225,6 +3364,62 @@ test "a session dumped by a build whose layer was a view's bit and no lines (wir
}
}
+test "a line replaced by one too unlike it to be one line changed: what went right above what came, a diff's order, in the file's own pane and from the middle alike" {
+ const gpa = testing.allocator;
+ const one = "l1\nl2\nline 10\nl4\n";
+ const two = "l1\nl2\nline ten CHANGED by two\nl4\n";
+ var repo = try Repo.with(gpa, &.{ .{ one, "one" }, .{ two, "two" }, .{ two ++ "l5\n", "three" } }, null) orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 30, .windowed = false });
+ defer p.deinit();
+ var line: [4200]u8 = undefined;
+ for ([_][]const u8{ "", "@@--" }) |at| {
+ p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}{s}", .{ repo.file, at }) });
+ const pane = p.panes[p.view().active].?;
+ p.update(.{ .command = "Changes @--" });
+ try testing.expectEqualStrings("line 10", File.sourceLine(pane, 2));
+ try testing.expectEqualStrings("line ten CHANGED by two", File.sourceLine(pane, 3));
+ }
+}
+
+test "from the middle: in a revision's pane a pin behind it shows the lines it took away where they stood (a changed line's old text right above it), and a pin ahead of it shows the lines it will write as the read-only lines where they will stand, the lines it will take away the text's, going" {
+ const gpa = testing.allocator;
+ // `two` changes l3 and takes l7 away; `three`, ahead of it, writes a
+ // line after l1 and changes l5.
+ const one = "l1\nl2\nl3\nl4\nl5\nl6\nl7\n";
+ const two = "l1\nl2\nl3 two\nl4\nl5\nl6\n";
+ const three = "l1\nnew three\nl2\nl3 two\nl4\nl5 three\nl6\n";
+ var repo = try Repo.with(gpa, &.{ .{ one, "one" }, .{ two, "two" }, .{ three, "three" } }, null) 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].?;
+ 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_two = try std.fmt.allocPrint(arena.allocator(), "{s} two t now", .{ch.pins[0].id[0..4]});
+ const said_three = try std.fmt.allocPrint(arena.allocator(), "{s} three t now", .{ch.pins[1].id[0..4]});
+ const want = [_][]const u8{ "l1", "new three", "l2", "l3", "l3 two", said_two, "l4", "l5", "l5 three", said_three, "l6", "l7" };
+ for (want, 0..) |w, row| try testing.expectEqualStrings(w, File.sourceLine(pane, @intCast(row)));
+ try testing.expectEqualStrings(two, try File.fileText(&pane.file.?, arena.allocator()));
+ // Drawn: what three will write green across, what it will take
+ // away red across, what two took away red.
+ p.sync();
+ const s = try p.render(arena.allocator());
+ const body = s.region(.body, id).?.rect;
+ const x = body.x + File.gutterWidth(pane) + 1;
+ const grounds = pardes.colors.Grounds.of(p.theme());
+ for ([_]u16{ 1, 8 }) |row| try testing.expectEqual(pardes.Color{ .rgb = grounds.add.row }, s.at(x, body.y + row).style.bg);
+ for ([_]u16{ 7, 11 }) |row| try testing.expectEqual(pardes.Color{ .rgb = grounds.del.row }, s.at(x, body.y + row).style.bg);
+ }
+}
+
test "a client replays the layer's lines and an edit refused among them, and ends equal; a client joined over a pane showing the layer loads its lines" {
var repo = try Lines.repo() orelse return error.SkipZigTest;
defer repo.deinit(testing.allocator);