diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/hunks.zig | 49 | ||||
| -rw-r--r-- | core/jj.zig | 192 | ||||
| -rw-r--r-- | core/mouse.zig | 6 | ||||
| -rw-r--r-- | core/plumbing | 9 |
4 files changed, 231 insertions, 25 deletions
diff --git a/core/hunks.zig b/core/hunks.zig index bee06cc2..e109b788 100644 --- a/core/hunks.zig +++ b/core/hunks.zig @@ -701,7 +701,9 @@ fn trimmedWidth(ctx: *const Ctx, section: []const u8) usize { /// (a .diff, a .patch: relative to the pane's directory), its hunks /// stacked so; with a revision (7 to 40 lowercase hex digits, a letter and /// 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. A plumbing +/// 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 /// 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 { @@ -731,6 +733,9 @@ pub fn word(c: pardes.builtins.Ctx) void { if (p.panes[dst]) |cp| cp.command_pages = true; 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").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| { @@ -1072,6 +1077,48 @@ test "a mailed patch keeps its headers, message and diffstat above its hunks" { try testing.expectEqualStrings("From 0123 Mon Sep 17 00:00:00 2001\nSubject: [PATCH] change x\n\nWhy it changed.\n---\n x.txt | 2 +-\n\nx.txt:1\n-a\n+b\n", result.content); } +test "a log of several changes keeps each one's header and whole message above its own hunks: git log -p, jj show's header" { + var t: TestCtx = undefined; + t.init(); + defer t.deinit(); + const log = + \\commit 1111111111111111111111111111111111111111 + \\Author: A <a@a> + \\Date: Mon Nov 23 10:00:00 2026 -0300 + \\ + \\ second + \\ + \\ its body, a line + \\ + \\diff --git a/x.txt b/x.txt + \\index 1..2 100644 + \\--- a/x.txt + \\+++ b/x.txt + \\@@ -1 +1 @@ + \\-b + \\+c + \\ + \\commit 2222222222222222222222222222222222222222 + \\Author: A <a@a> + \\Date: Mon Nov 23 09:00:00 2026 -0300 + \\ + \\ first + \\ + \\diff --git a/x.txt b/x.txt + \\index 0..1 100644 + \\--- a/x.txt + \\+++ b/x.txt + \\@@ -1 +1 @@ + \\-a + \\+b + \\ + ; + const result = try paneResult(testing.allocator, try format(&t.ctx, "/pardes-surely-missing-dir", log)); + defer testing.allocator.free(result.content); + defer locations.freeRows(testing.allocator, result.rows); + try testing.expectEqualStrings("commit 1111111111111111111111111111111111111111\nAuthor: A <a@a>\nDate: Mon Nov 23 10:00:00 2026 -0300\n\n second\n\n its body, a line\n\nx.txt:1\n-b\n+c\n\ncommit 2222222222222222222222222222222222222222\nAuthor: A <a@a>\nDate: Mon Nov 23 09:00:00 2026 -0300\n\n first\n\nx.txt:1\n-a\n+b\n", result.content); +} + test "a .patch looked at opens its +Diff, and Diff goes to the text and back" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true }); defer p.deinit(); diff --git a/core/jj.zig b/core/jj.zig index 96009d74..ed4d7e2a 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -370,8 +370,9 @@ pub const Hunk = struct { /// 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, its lane an arrow to it. 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 +/// steps over it, search and selection do not see it; a Look on it looks at +/// its word as anywhere, on a description's id its change's patch +/// (`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 { @@ -1497,21 +1498,104 @@ pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?us 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 { +/// A Look (B3) at column `col` of body row `vr` of pane `id` when it is a +/// ghost row (mouse.zig): a regular Look of the word there, as the same +/// text anywhere is looked at; on a description's id, its change's patch at +/// the row for the last line of its this file has (`Diff <id> <path>:<line>`, +/// what a user could type). False on any other row. +pub fn lookGhost(p: *Pardes, id: usize, vr: i32, col: 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); + const said = pane.ghost_text[g.text..][0..g.len]; + const at = File.rawAtDisplay(p.settings.tab, said, @intCast(@max(0, col - @as(i32, File.gutterWidth(pane))))); + const what = wordAt(said, at) orelse return true; + const id_end = std.mem.indexOfScalar(u8, said, ' ') orelse said.len; + if (g.kind == .description and what.ptr == said.ptr and what.len == id_end) { + const f = &pane.file.?; + const path = if (of(f)) |r| r.path else f.path; + var last: u32 = g.line; + for (ch.hunks) |h| if (h.pin == g.pin) { + last = @max(last, h.new_start + h.new_len -| 1); + }; + var line: [4200]u8 = undefined; + const arg = std.fmt.bufPrint(&line, "{s} {s}:{d}", .{ ch.pins[g.pin].id, path, last }) catch return true; + pardes.exec.runBuiltin(p, .Diff, id, "Diff", arg); + return true; + } + pardes.look.lookAt(p, id, what); + return true; +} + +/// The word of a ghost row's text at byte `at`: a run of what a path, a +/// name, an address or an id is made of. +fn wordAt(text: []const u8, at: usize) ?[]const u8 { + const part = struct { + fn of(c: u8) bool { + return std.ascii.isAlphanumeric(c) or std.mem.indexOfScalar(u8, "_./:@-+~", c) != null or c >= 0x80; + } + }.of; + if (at >= text.len or !part(text[at])) return null; + var lo = at; + while (lo > 0 and part(text[lo - 1])) lo -= 1; + var hi = at; + while (hi < text.len and part(text[hi])) hi += 1; + return std.mem.trimEnd(u8, text[lo..hi], ".:"); +} + +/// A jj change id as jj writes one: its letters, k to z. +pub fn isChangeId(text: []const u8) bool { + if (text.len < 4 or text.len > 32) return false; + for (text) |c| if (c < 'k' or c > 'z') return false; + return true; +} + +/// `Diff <change> [path[:line]]` (hunks.zig): change `change` of the +/// repository pane `id`'s directory is in, its patch as `jj show --git` +/// 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 { + var words = std.mem.tokenizeAny(u8, arg, " \t"); + const change = words.next() orelse return false; + if (!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; + 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| { + p.gpa.free(why); + return false; + }, + .bytes => |b| b, + }; + defer p.gpa.free(bytes); + var name: [4200]u8 = undefined; + const hid = @import("hunks.zig").openDiff(p, id, std.fmt.bufPrint(&name, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), change }) catch return false, bytes) catch return false; + p.view().active = hid; + const place = look.parsePathLine(words.next() orelse return true); + const pane = p.panes[hid].?; + const rows = pane.file.?.location_rows; + const want = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ dir, place.path }) catch return true; + var best: ?usize = null; + for (rows, 0..) |row, i| { + if (!std.mem.endsWith(u8, want, row.path) or row.path.len == 0) continue; + if (best == null) best = i; + if (row.at.line == place.at.line) { + best = i; + break; + } + } + if (best) |i| { + pane.body.cur_row = @intCast(i); + pane.body.cur_col = 0; + pane.ensureCursorVisible(p.settings.tab); + } return true; } @@ -2363,7 +2447,7 @@ test "Changes draws what the text has not where it stood, as ghost rows: a line } } -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" { +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 looks at its word as anywhere" { const gpa = testing.allocator; var repo = try Ghosts.repo() orelse return error.SkipZigTest; defer repo.deinit(gpa); @@ -2407,18 +2491,84 @@ test "the cursor steps over ghost rows and selection does not see them; ]g and [ 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. + // A Look (B3) on `c();`'s ghost row is a Look of `c`, as `Look c` + // typed in the pane: no file at a revision opens. + p.update(.{ .command = "Look a.zig:1" }); 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); + p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = body.x + File.gutterWidth(pane) + 4, .row = body.y + 6 } }); + p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = body.x + File.gutterWidth(pane) + 4, .row = body.y + 6 } }); + try testing.expectEqual(id, p.view().active); + for (p.panes) |slot| if (slot) |q| if (q.file) |*f| try testing.expect(of(f) == null); + const clicked: [2]i32 = .{ pane.body.cur_row, pane.body.cur_col }; + p.update(.{ .command = "Look a.zig:1" }); + look.lookAt(p, id, "c"); + try testing.expectEqual(clicked, [2]i32{ pane.body.cur_row, pane.body.cur_col }); +} + +test "a Look on a change's id opens its patch, its header and whole message at the top: on a description row, at the row of that change's last line in this file; in any text, at the top; a word that names no change is looked as any word" { + 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 = 80, .rows = 30, .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).?; + const mid = ch.pins[2].id; + p.sync(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const body = s.region(.body, id).?.rect; + // mid's description (row 5): a B3 on its id. + const x = body.x + File.gutterWidth(pane) + 1; + p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = x, .row = body.y + 5 } }); + p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = x, .row = body.y + 5 } }); + const hid = p.view().active; + try testing.expect(hid != id); + const patch = p.panes[hid].?; + const f = &patch.file.?; + try testing.expectStringStartsWith(f.path, try std.fmt.allocPrint(arena.allocator(), "{s}/+Diff-", .{repo.dir})); + // jj show's header, the whole message, above the hunks. + var rows = std.mem.splitScalar(u8, f.content, '\n'); + try testing.expectStringStartsWith(rows.next().?, "Commit ID: "); + try testing.expectStringStartsWith(rows.next().?, try std.fmt.allocPrint(arena.allocator(), "Change ID: {s}", .{mid})); + try testing.expectStringStartsWith(rows.next().?, "Author : t <t@t>"); + try testing.expect(std.mem.indexOf(u8, f.content, "\n mid\n") != null); + // The cursor on mid's last line in a.zig, `x();` (its line 4). + try testing.expectEqualStrings("a.zig:4", try std.fmt.allocPrint(arena.allocator(), "{s}:{d}", .{ std.Io.Dir.path.basename(f.location_rows[@intCast(patch.body.cur_row)].path), f.location_rows[@intCast(patch.body.cur_row)].at.line })); + // The same, typed: one pane a change, the cursor moved there. + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + p.update(.{ .command = try std.fmt.bufPrint(&line, "Diff {s} a.zig:2", .{mid}) }); + try testing.expectEqual(hid, p.view().active); + try testing.expectEqual(@as(usize, 2), f.location_rows[@intCast(patch.body.cur_row)].at.line); + // In running text: the patch at its top. + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{ch.pins[1].id}) }); + const cut = p.panes[p.view().active].?; + try testing.expectStringStartsWith(cut.file.?.content, "Commit ID: "); + try testing.expect(std.mem.indexOf(u8, cut.file.?.content, "\n cut\n") != null); + try testing.expectEqual(@as(i32, 0), cut.body.cur_row); + // A word of k to z that names no change: no patch. + const patches = struct { + fn count(q: *Pardes) usize { + var n: usize = 0; + for (q.panes) |slot| if (slot) |o| if (o.file) |*of_| { + n += @intFromBool(std.mem.indexOf(u8, of_.path, "+Diff-") != null); + }; + return n; + } + }.count; + const before = patches(p); + p.update(.{ .command = "Look zzzzzzzz" }); + try testing.expectEqual(before, patches(p)); + } } 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" { diff --git a/core/mouse.zig b/core/mouse.zig index 91d2932c..7c9007a1 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -1250,10 +1250,10 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { } } else { const clk = pane.sel[b]; - // A right-click on a ghost row (jj.zig) looks at its line - // in the revision it came from. + // A right-click on a ghost row (jj.zig) looks at the word + // there, as anywhere. if (s.button == config.look_button and clk.r0 >= pane.tag_rows and clk.r0 == clk.r1 and clk.c0 == clk.c1 and - pane.layers.lanes > 0 and @import("jj.zig").lookGhost(p, s.id, clk.r0 - pane.tag_rows)) + pane.layers.lanes > 0 and @import("jj.zig").lookGhost(p, s.id, clk.r0 - pane.tag_rows, clk.c0)) { pane.sel[b].state = .none; return; diff --git a/core/plumbing b/core/plumbing index 4e8cd702..7292577f 100644 --- a/core/plumbing +++ b/core/plumbing @@ -43,6 +43,15 @@ data matches '[0-9a-f]*[0-9][0-9a-f]*' data matches '[0-9a-f]*[a-f][0-9a-f]*' plumb start Diff $0 +# a jj change id looked at in the editor (eight or more of its letters, +# k to z, as jj log shows them), in a jj repository: jj show, its header +# and its hunks, stacked in a +Diff (a word that names no change: Diff +# declines and the word is searched for) +type is text +src is pardes +data matches '[k-z]{8,32}' +plumb start Diff $0 + # a diff paged (git show, git log -p) is shown stacked in a +Diff in # its +Pager's place type is text/x-diff |
