diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-22 00:22:40 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-22 00:24:47 -0300 |
| commit | 2015baa4ccd26a74b51e7b7cda0eb3533a325142 (patch) | |
| tree | 98ebfdce836b02a466d11b100d1674d1cf57e513 /core | |
| parent | 98ff1e611e2fd4cd7f1f3db855a6d52f53ab4b71 (diff) | |
| download | pardes-2015baa4ccd26a74b51e7b7cda0eb3533a325142.tar.gz pardes-2015baa4ccd26a74b51e7b7cda0eb3533a325142.zip | |
layers: what round 5's picture got wrong against the design's words: a description row keeps its author and age in a pane narrower than it, its title what gives way (`jj.fitted`: the title cut to the room the row has, `File.bodyWidth`, with a `…` where it is cut, the same text the geometry lays out and `inkGhost` colours, whatever the horizontal scroll); at one place a change's ghost rows come before its own description (the description ranked right after its last ghost row there; descriptions of blocks closing just above still first, newest first: a hunk that changed a line and took the next away had put its description between them); a change the revset does not pin, between two it does, no longer shifts the older pins' ghost rows: jj's one log run now asks `((R) | (roots(R)::REV)) & files(F)` and marks each change `p` or `c` (`self.contained_in(R)` in the template, `logTemplate`), the unpinned ones' hunks kept only to carry places (`Hunk.carrier`, `Hunk.step`, `carried` walking the steps); and lines typed before jj's snapshot or while jj ran no longer shift them: a place jj gives is carried into the pane's text by the same alignment that lines the lanes up (`Moved`, from `provenance`: the lines both ends agree on), a ghost row whose place lies inside lines rewritten since is not drawn; tests at the pane's surface: a long title in a 50-column pane cut before ` t now`, the id in its lane's colour, author and age grey, in a terminal and a window; `b();` changed and `c();` taken by one hunk then its description; `old | last` with `mid` unpinned between them puts `c();` before `d();`; a line typed between the word and the worker's answer moves the six places one down, the client equal; core-test 908/1068 (160 skipped), shell-test 1110/1113 (3 skipped)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
| -rw-r--r-- | core/File.zig | 22 | ||||
| -rw-r--r-- | core/jj.zig | 322 |
2 files changed, 275 insertions, 69 deletions
diff --git a/core/File.zig b/core/File.zig index a94353d5..347f4809 100644 --- a/core/File.zig +++ b/core/File.zig @@ -19,6 +19,7 @@ const locations = @import("locations.zig"); const Colors = @import("colors.zig"); const Tables = @import("Tables.zig"); const Stream = @import("Stream.zig"); +const jj = @import("jj.zig"); const Pane = panes.Pane; const Output = panes.Output; const Mini = panes.mini; @@ -399,7 +400,7 @@ pub fn renderedLineDisplayCol(tab: u16, pane: *const Pane, row: i32, line_text: return prefix +| rawDisplayCol(tab, line_text[prefix..], byte_col - prefix); } -fn fitEnd(tab: u16, text: []const u8, start: usize, width: usize) usize { +pub fn fitEnd(tab: u16, text: []const u8, start: usize, width: usize) usize { var end = start; var used: usize = 0; while (used < width and end < text.len) { @@ -757,7 +758,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who }; reportEdit(p, f, off, len, bytes); // A pinned change's per-line values ride with the lines (jj.zig). - if (p.changes.answers.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes); + if (p.changes.answers.items.len > 0) jj.edited(p, f, off, len, bytes); if (f.mini) |*mini| mini.deinit(p.gpa); f.mini = null; if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes); @@ -1659,7 +1660,7 @@ fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { /// The path a text's language is known by: a file at a revision's /// (jj.zig) without its `@rev`. pub fn langPath(f: *const State) []const u8 { - return if (@import("jj.zig").of(f)) |r| r.path else f.path; + return if (jj.of(f)) |r| r.path else f.path; } pub fn supportsContext(pane: *const Pane) bool { @@ -1686,6 +1687,12 @@ fn prepareContextRows(pane: *Pane, file: *State) void { pub fn wrapWidth(pane: *const Pane, wrap: bool) usize { if (!wrap or pane.rows > pane.wrap_line.len) return 0; + return bodyWidth(pane); +} + +/// The cells a body row has for text after the gutter (and a cell for a +/// wrapped line's marker). +pub fn bodyWidth(pane: *const Pane) usize { return @max(1, @as(usize, pane.cols -| gutterWidth(pane)) -| 1); } @@ -1813,7 +1820,14 @@ fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State if (ghosted and !context and body_at == 0 and ghost < pane.ghosts.len and pane.ghosts[ghost].at == body_line) { const g = pane.ghosts[ghost]; const said = pane.ghost_text[g.text..][0..g.len]; - const shown = if (width > 0) said[0..fitEnd(tab, said, 0, width)] else said[modal.graphemeStart(said, @min(@as(usize, @intCast(@max(0, pane.hscroll))), said.len))..]; + var fit: [jj.row_max]u8 = undefined; + // A description is one compact row whatever the scroll. + const shown = if (g.kind == .description) + jj.fitted(tab, said, bodyWidth(pane), &fit) + else if (width > 0) + said[0..fitEnd(tab, said, 0, width)] + else + said[modal.graphemeStart(said, @min(@as(usize, @intCast(@max(0, pane.hscroll))), said.len))..]; if (record_wrap) { pane.wrap_line[i] = @intCast(body_line); pane.wrap_col[i] = -1 - @as(i32, @intCast(ghost)); diff --git a/core/jj.zig b/core/jj.zig index 96d9abad..dd54dfc5 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -304,7 +304,11 @@ 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, @@ -312,6 +316,8 @@ pub const Hunk = struct { /// 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 @@ -521,9 +527,14 @@ 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), // The wall clock first, read where jj runs (an input, as jj's @@ -536,7 +547,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc }; 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 @@ -703,22 +714,26 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a 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; @@ -726,27 +741,38 @@ 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 (0..out.pins.len) |k| try hunksOf(gpa, arena, @intCast(k), logged[diffs.items[k]..diffs.items[k + 1]], 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), logged[s.diff..next.diff], text, out.lines, &hunks, &removed, &marks, &taken); out.hunks = try hunks.toOwnedSlice(gpa); nest(out.lines, out.pins.len); - out.ghosts = try ghostsOf(gpa, arena, out.pins, out.lines, out.hunks, taken.items, &removed, now); + 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)); @@ -754,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 @@ -763,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'); @@ -781,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); @@ -793,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")); @@ -832,8 +881,9 @@ const Taken = struct { paired: bool, marks: u32, nmarks: u16, new: u32 = 0, show /// 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. -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), marks: *std.ArrayList([2]u32), taken: *std.ArrayList(Taken)) error{OutOfMemory}!void { +/// (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 = .{}; @@ -843,9 +893,9 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, diffed: []c 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 }); + 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'); @@ -854,6 +904,7 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, diffed: []c 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: { @@ -947,40 +998,42 @@ fn pairRun(minus: []Taken, plus: anytype, base: usize) void { 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 last pin's revision); and each +/// 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, -/// then the descriptions of pins that have only those. `text` holds the -/// hunks' removed lines; the descriptions' rows go on after them. -fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, lines: []const Line, hunks: []const Hunk, taken: []const Taken, text: *std.ArrayList(u8), now: i64) error{OutOfMemory}![]Ghost { +/// 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) 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.pin, if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start, lines.len); - var ats: [max_run]u32 = undefined; + 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, lines.len); + 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], .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, + .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, }); - from = nl + 1; } } for (pins, 0..) |pin, k| { @@ -1006,10 +1059,15 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, 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 = at orelse carried(hunks, @intCast(k), end, lines.len), .pin = @intCast(k), .kind = .description, .line = @max(first, 1), .text = @intCast(at_text), .len = @intCast(text.items.len - at_text) }, + .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, }); } @@ -1023,30 +1081,33 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, return out; } -/// Where the place after line `after` of pin `k`'s revision is in the last -/// pin's: moved by each later pin's hunks before it, or, inside a run of -/// lines a later pin took away, after what it wrote there. (A change the -/// revset does not pin, between two it does, is not seen: its lines move -/// the places below it by as many.) -fn carried(hunks: []const Hunk, k: u16, after: u32, lines: usize) u32 { +/// 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].pin; + const j = hunks[i].step; var shift: i64 = 0; var inside: ?i64 = null; - while (i < hunks.len and hunks[i].pin == j) : (i += 1) { + while (i < hunks.len and hunks[i].step == j) : (i += 1) { const h = hunks[i]; - if (j <= k) continue; + 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 > k) pos = inside orelse pos + shift; + if (j > from) pos = inside orelse pos + shift; } - return @intCast(std.math.clamp(pos, 0, @as(i64, @intCast(lines)))); + 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 @@ -1063,6 +1124,28 @@ fn describe(gpa: std.mem.Allocator, text: *std.ArrayList(u8), pin: Pin, now: i64 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. @@ -1087,13 +1170,13 @@ fn ghostLanes(ch: *const Changes, ghosts: []const Ghost, gi: usize) [levels]PinI /// A ghost row's cells: a line taken away whole red across, its text in /// the line numbers' grey (dimmer than the text's ink); a description in -/// its lane's colour, its author and age grey. -fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, text: []const u8, marks: []const [2]u32, colours: []const [3]u8, x: u16, y: u16, w: u16) void { +/// 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 said = text[g.text..][0..g.len]; 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| { @@ -1103,7 +1186,6 @@ fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, text: []const u8, marks: [ } }, .description => { - const said = text[g.text..][0..g.len]; // `id title author age`: the last two words grey. var grey = said.len; for (0..2) |_| grey = std.mem.lastIndexOfScalar(u8, said[0..grey], ' ') orelse 0; @@ -1284,7 +1366,10 @@ pub fn paint(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u1 const lanes: [levels]PinIndex = if (vr < pane.wrap_n and pane.wrap_col[vr] < 0) ghost: { const gi: usize = @intCast(-1 - pane.wrap_col[vr]); if (gi >= pane.ghosts.len) continue; - inkGhost(p, s, pane.ghosts[gi], pane.ghost_text, ch.marks, colours, tx + gutter, y, tw - gutter); + 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; @@ -1491,9 +1576,9 @@ test "Changes pins a revset's changes on the pane, shown in this view: who wrote // 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.expectStringStartsWith(ch.ghost_text, "const one = 1;\n"); @@ -1578,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, @@ -1588,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); @@ -1596,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 }; } @@ -1639,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. @@ -1653,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 @-"); @@ -1666,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"); @@ -1961,6 +2046,113 @@ test "ghost rows ride with the text: a line deleted above moves them up, a line 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 { |
