diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 09:46:32 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 14:17:20 -0300 |
| commit | dfb051bd798f65c2217eea7f95b03dce292e30bf (patch) | |
| tree | 4307ce068343f57de735be1ee7f6093ea106f4f8 /core | |
| parent | c4f7bb9477c7aa4c8864f14edb9e85b6c8c08437 (diff) | |
| download | pardes-dfb051bd798f65c2217eea7f95b03dce292e30bf.tar.gz pardes-dfb051bd798f65c2217eea7f95b03dce292e30bf.zip | |
layers: `Changes [revset]` pins a revset's changes on a file's pane in this view (bare `@`, bare again off, another replaces; refusals in jj's words) and draws them: what jj says is asked in two recorded answers (Site.jj), `jj log -p --git --context=0` restricted to the file (the pins: id, author, time, the description's first line, and each one's diff, read with diff.zig's walk into hunks and the removed lines' text) and `jj file annotate` at the shown revision (which pin wrote each line), kept as flat arrays in the pane (Pane.changes: replicated, a late joiner has them in the dump, a client never runs jj) and worn by the view whose revset is File.Layers.revset; a pin's lane runs unbroken from a line it wrote to the next while every line between is a newer pin's, so lanes nest, the spine keeping its place and a nested change a level to the right on its rows only, three levels in a gutter the lanes widen (a window two cells, strips of a third of a cell with half that of air: Surface.lanes, drawn by the gui; a terminal three, a `▎` a level), coloured by the theme's accents but its green and red in hue order from the top, one within 15 degrees of the last kept skipped (doom one: magenta, blue, yellow), oldest first; a line a pin added whole (in a run it took away and wrote, past as many as it took away) is green across the line as a diff's added line; with Changes off nothing is drawn and the gutter is as it was; tests: the data through the pane (pins, lines, hunks, removed text, unpin, `@` touching nothing, a refusal), a client replaying it ends equal, the lanes and grounds of a three-deep stack in a terminal's cells and a window's strips; core-test 895/1055 (160 skipped), shell-test 1097/1100
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
| -rw-r--r-- | core/Core.zig | 1 | ||||
| -rw-r--r-- | core/File.zig | 51 | ||||
| -rw-r--r-- | core/body_layer.zig | 1 | ||||
| -rw-r--r-- | core/builtins.zig | 6 | ||||
| -rw-r--r-- | core/config.zig | 1 | ||||
| -rw-r--r-- | core/draw.zig | 1 | ||||
| -rw-r--r-- | core/jj.zig | 603 | ||||
| -rw-r--r-- | core/panes.zig | 8 | ||||
| -rw-r--r-- | core/replica.zig | 1 | ||||
| -rw-r--r-- | core/root.zig | 1 | ||||
| -rw-r--r-- | core/surface.zig | 10 |
11 files changed, 613 insertions, 71 deletions
diff --git a/core/Core.zig b/core/Core.zig index d2cfadfc..385c3e74 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -876,6 +876,7 @@ pub fn deinit(p: *Pardes) void { for (&p.surface.body_layers) |*layer| gpa.free(layer.cells); for (&p.surface.tag_layers) |*layer| gpa.free(layer.cells); p.surface.placements.deinit(gpa); + p.surface.lanes.deinit(gpa); gpa.destroy(p); } diff --git a/core/File.zig b/core/File.zig index 450e4ec9..f7d6bced 100644 --- a/core/File.zig +++ b/core/File.zig @@ -372,7 +372,13 @@ pub fn gutterWidth(pane: *const Pane) u16 { var lines = if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content); var digits: u16 = 1; while (lines >= 10) : (lines /= 10) digits += 1; - return @max(config.PREFIX_W, digits + 1); + return @max(config.PREFIX_W, digits + 1) + laneCells(pane); +} + +/// The cells the changes' lanes take after the line number in this view +/// (File.Layers), none while it pins no changes. +pub fn laneCells(pane: *const Pane) u16 { + return pane.layers.lanes; } /// Convert between rendered cells and UTF-8 byte columns. Tag rows always @@ -1756,7 +1762,7 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context.revision orelse 0, f.context.declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len, pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, - pane.view.rect_h, + pane.view.rect_h, laneCells(pane), }; // One hash of them laid out as words: every reader of the map asks for // this key, a hundred times a frame. @@ -1813,7 +1819,8 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w @memset(prefix, ' '); if (at == 0 and prefix.len > 0) { var lineno = row + 1; - var digit: usize = prefix.len - 1; + // The number, a blank, then the lanes (laneCells). + var digit: usize = prefix.len - 1 - laneCells(pane); if (row == end_slot) { prefix[digit - 1] = '~'; } else while (true) { @@ -1868,13 +1875,41 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w /// made without the language's ink: highlightDiffPlain). /// /// Both are painted in recolorSyntax's one walk over the cells, each where -/// it says (one walk per painter cost a frame a sixth more). Ruled, not -/// built (PARDES.md "Changes on the source view"): 3. changes: a lane after -/// the line number, a line added whole green across it, a changed line's -/// words tinted, ghost rows for what the shown revision has not; 4. churn: -/// a heat lane after the number and a strip by the scroll thumb. +/// it says (one walk per painter cost a frame a sixth more). +/// +/// 3. changes (`Changes [revset]`, jj.zig): while `revset` is set, a +/// lane a pinned change after the line number (the gutter `lanes` +/// cells wider), a line one added whole green across it (jj.paint). +/// Ruled, not built (PARDES.md "Changes on the source view"): a +/// changed line's words tinted, ghost rows for what the shown +/// revision has not. +/// 4. churn, ruled, not built: a heat lane after the number and a strip +/// by the scroll thumb. pub const Layers = struct { + pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; ink: bool = true, + /// The changes this view pins (jj's revset), none while empty; what jj + /// answered for them is the pane's (`Pane.changes`). + revset: [revset_max]u8 = undefined, + revset_len: u8 = 0, + /// The cells its lanes take while it pins any, 0 while it pins none + /// (laneCells): set as it pins them, from the view's frontend: in a + /// window two, which its strips of pixels fill one and a half of + /// (Surface.Lane); in a terminal three, a glyph each. + lanes: u8 = 0, + + pub const revset_max = 64; + + pub fn pinned(l: *const Layers) []const u8 { + return l.revset[0..l.revset_len]; + } + + /// One word while it pins nothing: a digest walks every view's layers + /// of every pane (field by field it cost a step 0.1 to 0.7% more). + pub fn digest(l: *const Layers, h: *pardes.walk.Hasher) void { + h.update(&.{ @intFromBool(l.ink), l.revset_len, l.lanes }); + if (l.revset_len > 0) h.update(l.pinned()); + } }; pub const SynStyle = struct { fg: [3]u8, bold: bool }; diff --git a/core/body_layer.zig b/core/body_layer.zig index 1582136c..8d54760f 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -338,6 +338,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane // File layout is independent of whether its producer requests colors. if (pane.file) |*f| { drawGutter(p, s, pane, r, tx, tw, body_h, active); + if (pane.layers.revset_len > 0) @import("jj.zig").paint(p, s, pane, r, tx, tw, body_h); panes.Query.paintMark(p, s, pane, r, body_h); if (pane.colorAlgo() != .none) panes.File.recolorSyntax(p, s, pane, f, r, tx, tw, body_h); diff --git a/core/builtins.zig b/core/builtins.zig index 18b49a41..e059916a 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1080,6 +1080,12 @@ pub const At = struct { pub const run = @import("jj.zig").word; }; +/// Pin the changes `revset` names (jj; bare, `@`) on a file's pane in this view: a lane each after the line number, a line one added whole green; bare again, unpinned. +pub const Changes = struct { + pub const takes_arg = true; + pub const run = @import("jj.zig").changesWord; +}; + /// Stack a diff's hunks as a diff viewer shows them, each under its place and the declarations around it, in a +Diff; on a +Diff, show the diff itself as text. `Diff <file>` stacks that diff's, `Diff <revision>` git show's. /// The diff viewer's one registration: its word and traits here, all else /// its own (hunks.zig). diff --git a/core/config.zig b/core/config.zig index 361a59ed..6338c5d1 100644 --- a/core/config.zig +++ b/core/config.zig @@ -77,6 +77,7 @@ pub const leader_path = paths: { .Filter = null, .Diff = null, .At = null, + .Changes = null, .Mode = null, .Togglettymode = null, .Collapse = null, diff --git a/core/draw.zig b/core/draw.zig index b87eb56f..ed317650 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -85,6 +85,7 @@ pub fn place(p: *Pardes, s: *Surface) void { const v = p.view(); s.nregions = 0; s.nrail_ticks = 0; + s.lanes.clearRetainingCapacity(); // The facts filled in the walk the frame's places take anyway. viewFacts(p, &p.facts); s.facts = &p.facts; diff --git a/core/jj.zig b/core/jj.zig index 7784c1c1..ebd2ea27 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -16,6 +16,7 @@ const File = pardes.panes.File; const Output = pardes.panes.Output; const look = @import("look.zig"); const layout = @import("layout.zig"); +const diff = @import("diff.zig"); /// What jj answered: why not, in its own words (its first line, without /// `Error: `), or the file's bytes. The caller's, in the allocator the site @@ -195,8 +196,216 @@ pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look return true; } -// ---- what `Changes` reads (PARDES.md: pinned changes as lanes), the -// data alone: flat arrays, no drawing yet ---- +// ---- `Changes [revset]` (PARDES.md: pinned changes as lanes): what jj +// answers for it, kept as flat arrays in the pane ---- + +/// What jj answered for a pane's `Changes`: the pane's replicated state (a +/// client never runs jj; one that joins late has it in the dump), drawn by +/// a view that pins the same revset (File.Layers). Two jj runs, each a +/// site of the step's tape: the changes with their diffs of the file +/// (`jj log -p --git`, the diffs read with diff.zig's walk), and who wrote +/// each line of the shown revision (`jj file annotate`). +pub const Changes = struct { + pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; + revset: [File.Layers.revset_max]u8 = undefined, + revset_len: u8 = 0, + /// The revset's changes that touch the file, oldest first. + pins: []Pin = &.{}, + /// A value a line of the shown revision. + 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 = &.{}, + + 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); + c.* = .{}; + } + + pub fn pinned(c: *const Changes) []const u8 { + return c.revset[0..c.revset_len]; + } +}; + +/// A pinned change, as `jj log` says it. +pub const Pin = struct { + pub const replica = .{ .len = .{ .{ "author", "author_len" }, .{ "title", "title_len" } } }; + id: [8]u8, + /// Its author's time, in seconds since the epoch. + time: i64, + author: [32]u8 = undefined, + author_len: u8 = 0, + /// Its description's first line, cut to fit. + title: [96]u8 = undefined, + title_len: u8 = 0, +}; + +/// A line of the shown revision: which pin wrote it (its place in +/// `Changes.pins`) or none, and whether that pin added it whole (no line +/// it took away stood in its place) or changed one. +pub const Line = struct { + pin: PinIndex, + whole: bool = false, + /// The pins whose lanes run down it, outermost (oldest) first (`nest`). + lanes: [levels]PinIndex = @splat(.none), +}; + +/// The lanes a row shows at most: the ruled gutter fits three. +pub const levels = 3; + +pub const PinIndex = enum(u16) { + none = std.math.maxInt(u16), + _, +}; + +/// The changes a `Changes` takes at most. +pub const pins_max = 64; + +/// A run of lines a pin took away and wrote in their place, in its own +/// revisions' numbers (from 1; `old_len` 0: it only wrote, after old line +/// `old_start`). +pub const Hunk = struct { + pin: u16, + old_start: u32, + old_len: u32, + new_start: u32, + new_len: u32, + /// Its removed lines' text: `Changes.removed[removed..][0..removed_len]`. + removed: u32, + removed_len: u32, +}; + +/// The `Changes` word (builtins.zig registers it): in a file's pane (or a +/// revision's), the changes `revset` names (bare: `@`, the change against +/// its parent) pinned in this view; bare again, unpinned; another revset +/// replaces them. What jj refuses is said in its words. +pub fn changesWord(c: pardes.builtins.Ctx) void { + const p = c.p; + const pane = c.pane; + const f = if (pane.file) |*file| file else return p.reportFailure(c.id, "Changes: only a file's pane takes it"); + const arg = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); + if (arg.len == 0 and pane.layers.revset_len > 0) { + pane.layers.revset_len = 0; + pane.layers.lanes = 0; + return; + } + const revset = if (arg.len == 0) "@" else arg; + if (revset.len > File.Layers.revset_max) return p.reportFailure(c.id, "Changes: a revset that long"); + const shown: struct { path: []const u8, rev: []const u8 } = if (of(f)) |r| + .{ .path = r.path, .rev = r.rev } + else if (f.output == null and pardes.filesystem.localPath(f.path) != null) + .{ .path = f.path, .rev = "@" } + else + return p.reportFailure(c.id, "Changes: only a file's pane takes it"); + var line: pardes.Kind.Buf(512) = .{}; + var got = read(p, shown.path, shown.rev, revset, f.content) catch |err| return p.reportFailure(c.id, line.print("Changes: {s}", .{@errorName(err)})); + switch (got) { + .refused => |why| { + defer p.gpa.free(why); + return p.reportFailure(c.id, line.print("Changes: {s}", .{why[0..@min(why.len, 480)]})); + }, + .read => |*changes| { + if (changes.pins.len == 0) { + changes.deinit(p.gpa); + return p.reportFailure(c.id, line.print("Changes: no change of `{s}` touches {s}", .{ revset, std.Io.Dir.path.basename(shown.path) })); + } + @memcpy(changes.revset[0..revset.len], revset); + changes.revset_len = @intCast(revset.len); + const kept = pane.changes orelse p.gpa.create(Changes) catch { + changes.deinit(p.gpa); + return p.reportFailure(c.id, "Changes: out of memory"); + }; + if (pane.changes != null) kept.deinit(p.gpa); + kept.* = changes.*; + pane.changes = kept; + @memcpy(pane.layers.revset[0..revset.len], revset); + pane.layers.revset_len = @intCast(revset.len); + pane.layers.lanes = if (p.view().frontend.windowed) 2 else levels; + }, + } +} + +const Read = union(enum) { refused: []u8, read: Changes }; + +/// What jj says of `revset`'s changes in `path` (absolute) at `rev`, whose +/// text is `text` (what the pane shows: a line is matched to the line its +/// change added by its text). +fn read(p: *Pardes, path: []const u8, rev: []const u8, revset: []const u8, text: []const u8) error{OutOfMemory}!Read { + const gpa = p.gpa; + const arena = p.scratch.allocator(); + const set = try fileset(arena, path); + const dir = dirOf(path); + var out: Changes = .{}; + var kept = false; + defer if (!kept) out.deinit(gpa); + // The changes, oldest first, each with its diff of the file: one run. + const query = try std.fmt.allocPrint(arena, "({s}) & files({s})", .{ revset, set }); + const log_args = [_][]const u8{ "log", "--no-graph", "--reversed", "-r", query, "-T", log_template, "--git", "--context=0", "--", set }; + const logged = switch (p.tape.site(.jj, gpa, runHost, .{ gpa, dir, @as([]const []const u8, &log_args) })) { + .refused => |why| return .{ .refused = why }, + .bytes => |b| b, + }; + defer gpa.free(logged); + 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; + 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); + }; + try diffs.append(arena, logged.len); + out.pins = try pins.toOwnedSlice(gpa); + if (out.pins.len == 0) { + kept = true; + return .{ .read = out }; + } + // Who wrote each line. + const annotated = switch (annotate(p, path, rev)) { + .refused => |why| return .{ .refused = why }, + .bytes => |b| b, + }; + defer gpa.free(annotated); + out.lines = try provenance(gpa, annotated, out.pins); + // Each change's hunks, and which of its lines it 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); + out.hunks = try hunks.toOwnedSlice(gpa); + out.removed = try removed.toOwnedSlice(gpa); + nest(out.lines, out.pins.len); + 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\""; + +/// A `log_template` 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; + for (l[0..8]) |c| if (c < 'k' or c > 'z') return null; + var cols = std.mem.splitScalar(u8, l[9..], '\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)); + @memcpy(pin.author[0..pin.author_len], author[0..pin.author_len]); + 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; +} /// Who last wrote each line of `path` (absolute) at `rev`: a change id /// (`change_id.short(8)`) a line, as `jj file annotate` says it. @@ -205,38 +414,204 @@ pub fn annotate(p: *Pardes, path: []const u8, rev: []const u8) Shown { return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dirOf(path), @as([]const []const u8, &.{ "file", "annotate", "-r", rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(path) }) }); } -/// The changes `revset` names, oldest first: a change id a line, as -/// `annotate` writes them. Asked in directory `dir`, the file's. -pub fn pinned(p: *Pardes, dir: []const u8, revset: []const u8) Shown { - return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dir, @as([]const []const u8, &.{ "log", "--no-graph", "--reversed", "-r", revset, "-T", "change_id.short(8) ++ \"\\n\"" }) }); -} - -/// A line no pinned change wrote (`provenance`). -pub const unpinned = std.math.maxInt(u16); - -/// Which pinned change wrote each line: a value a line, the change's place -/// in `changes` (`pinned`'s answer, oldest first) or `unpinned`, from -/// `annotated` (`annotate`'s answer). The caller's, in `gpa`. -pub fn provenance(gpa: std.mem.Allocator, annotated: []const u8, changes: []const u8) error{ OutOfMemory, TooManyChanges }![]u16 { - var ids: std.ArrayList([]const u8) = .empty; - defer ids.deinit(gpa); - var it = std.mem.tokenizeScalar(u8, changes, '\n'); - while (it.next()) |id| try ids.append(gpa, id); - if (ids.items.len >= unpinned) return error.TooManyChanges; - var out: std.ArrayList(u16) = .empty; +/// Which pin wrote each line: a value a line, from `annotated` +/// (`annotate`'s answer); `whole` is the hunks' to say. The caller's, in +/// `gpa`. +pub fn provenance(gpa: std.mem.Allocator, annotated: []const u8, pins: []const Pin) error{OutOfMemory}![]Line { + var out: std.ArrayList(Line) = .empty; errdefer out.deinit(gpa); var lines = std.mem.splitScalar(u8, annotated, '\n'); while (lines.next()) |id| { // The empty text after the last newline is no line. if (id.len == 0 and lines.peek() == null) break; - const at = for (ids.items, 0..) |pin, i| { - if (std.mem.eql(u8, pin, id)) break i; - } else unpinned; - try out.append(gpa, @intCast(at)); + const at: PinIndex = for (pins, 0..) |pin, i| { + if (std.mem.eql(u8, &pin.id, id)) break @enumFromInt(i); + } else .none; + try out.append(gpa, .{ .pin = at }); } return out.toOwnedSlice(gpa); } +/// 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 }; + 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; + }, + .removed => { + 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 => {}, + } + } + var next: usize = 0; + var rows = std.mem.splitScalar(u8, text, '\n'); + for (lines) |*line| { + const row = rows.next() orelse break; + if (@intFromEnum(line.pin) != k) continue; + const from = next; + while (next < added.items.len and !std.mem.eql(u8, added.items[next].text, std.mem.trimEnd(u8, row, "\r"))) next += 1; + if (next == added.items.len) { + next = from; + continue; + } + line.whole = added.items[next].whole; + next += 1; + } +} + +/// Each line's lanes: the blocks of the pins that span it, oldest first. +/// A pin's block runs from a line it wrote on to each next line it wrote +/// while every line between was written by a newer pin: its lane unbroken +/// down what it wrote, through what later pins rewrote inside it. An older +/// pin's line (or one no pin wrote) ends a newer pin's block, so blocks +/// nest, the older outside, and a block keeps its level down all its rows: +/// the spine keeps its place, a nested change steps to the right on its +/// rows only. Past `levels`, the innermost are not drawn. +fn nest(lines: []Line, npins: usize) void { + for (0..npins) |k| { + var i: usize = 0; + while (i < lines.len) { + if (@intFromEnum(lines[i].pin) != k) { + i += 1; + continue; + } + var last = i; + for (lines[i + 1 ..], i + 1..) |l, j| { + const q = @intFromEnum(l.pin); + if (q == k) last = j else if (l.pin == .none or q < k) break; + } + for (lines[i .. last + 1]) |*l| for (&l.lanes) |*lane| if (lane.* == .none) { + lane.* = @enumFromInt(k); + break; + }; + i = last + 1; + } + } +} + +/// The lanes' colours, a pin's the `pin % len`th, oldest first: the +/// theme's accents but the two that mean written and taken away (its green +/// and red), in hue order from the top, each a hue `hue_apart` or more from +/// the one kept before it (doom one: magenta, blue, yellow; cyan and +/// orange go). Distinct colours, never one between two. +pub fn laneColours(th: *const pardes.Theme, buf: *[8][3]u8) []const [3]u8 { + const pal = th.palette; + const grounds = [2][3]u8{ if (pal) |c| c[1] else .{ 0xcf, 0x22, 0x2e }, if (pal) |c| c[2] else .{ 0x2e, 0xa0, 0x43 } }; + const accents: [6][3]u8 = if (pal) |c| .{ c[3], c[4], c[5], c[6], th.num, th.kw } else .{ th.kw, th.num, th.str, th.box, th.column_box orelse th.box, th.comment }; + var sorted: [6][3]u8 = undefined; + var n: usize = 0; + for (accents) |c| { + // A grey is no accent; one near written or taken away means that. + if (chroma(c) < 48 or hueApart(c, grounds[0]) < hue_apart or hueApart(c, grounds[1]) < hue_apart) continue; + sorted[n] = c; + n += 1; + } + std.sort.insertion([3]u8, sorted[0..n], {}, struct { + fn above(_: void, a: [3]u8, b: [3]u8) bool { + return hue(a) > hue(b); + } + }.above); + var kept: usize = 0; + for (sorted[0..n]) |c| { + if (kept > 0 and hueApart(c, buf[kept - 1]) < hue_apart) continue; + buf[kept] = c; + kept += 1; + } + return buf[0..kept]; +} + +/// Two hues closer than this are one colour to a lane (doom one's cyan is +/// 12 degrees from its blue, its orange 11 from its yellow). +const hue_apart = 15; + +fn chroma(c: [3]u8) u8 { + return @max(c[0], c[1], c[2]) - @min(c[0], c[1], c[2]); +} + +/// HSV's hue, in degrees. +fn hue(c: [3]u8) f32 { + const r: f32 = @floatFromInt(c[0]); + const g: f32 = @floatFromInt(c[1]); + const b: f32 = @floatFromInt(c[2]); + const hi = @max(r, g, b); + const d = hi - @min(r, g, b); + if (d == 0) return 0; + const h = if (hi == r) @mod((g - b) / d, 6) else if (hi == g) (b - r) / d + 2 else (r - g) / d + 4; + return 60 * h; +} + +fn hueApart(a: [3]u8, b: [3]u8) f32 { + const d = @abs(hue(a) - hue(b)); + return @min(d, 360 - d); +} + +/// The changes a view pins, on its body's rows (body_layer.zig, while +/// File.Layers pins any): 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. +pub fn paint(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { + const ch = pane.changes orelse return; + if (ch.lines.len == 0 or !std.mem.eql(u8, ch.pinned(), pane.layers.pinned())) return; + var buf: [8][3]u8 = undefined; + const colours = laneColours(p.theme(), &buf); + if (colours.len == 0) return; + const gutter = File.gutterWidth(pane); + const cells = File.laneCells(pane); + if (gutter >= tw or cells == 0) return; + const first = tx + gutter - cells; + const windowed = p.view().frontend.windowed; + const ground = pardes.colors.Grounds.of(p.theme()).add.row; + const body_y = p.bodyTop(pane, r); + 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| { + if (lane == .none) break; + const rgb = colours[@intFromEnum(lane) % colours.len]; + if (windowed) { + // A body a pixel shell draws as a layer is painted on a + // temporary surface first: its lanes are not drawn yet. + if (s == &p.surface) s.lanes.append(p.gpa, .{ .x = first, .y = y, .level = @intCast(level), .rgb = rgb }) catch {}; + } else if (level < cells) { + var style = s.at(first + @as(u16, @intCast(level)), y).style; + style.fg = .{ .rgb = rgb }; + s.set(first + @as(u16, @intCast(level)), y, "\u{258e}", style); + } + } + } +} + // ---- tests ---- const testing = std.testing; @@ -268,17 +643,23 @@ const Repo = struct { const three = "const three = 3;\n"; fn init(gpa: std.mem.Allocator) !?Repo { + return with(gpa, &.{ .{ one, "one" }, .{ two, "two" } }, three); + } + + /// `a.zig` committed as each text with its message, in order; then + /// `working` in the working copy, never snapshotted. + fn with(gpa: std.mem.Allocator, commits: []const [2][]const u8, working: ?[]const u8) !?Repo { var tmp = testing.tmpDir(.{}); errdefer tmp.cleanup(); var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = try gpa.dupe(u8, buf[0..try tmp.dir.realPath(testing.io, &buf)]); errdefer gpa.free(dir); if (!try run(gpa, dir, &.{ "git", "init" })) return null; - for ([_][]const u8{ one, two }, [_][]const u8{ "one", "two" }) |text, message| { - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = text }); - if (!try run(gpa, dir, &.{ "commit", "-m", message })) return null; + for (commits) |c| { + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = c[0] }); + if (!try run(gpa, dir, &.{ "commit", "-m", c[1] })) return null; } - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = three }); + if (working) |text| try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = text }); return .{ .tmp = tmp, .dir = dir, .file = try std.fmt.allocPrint(gpa, "{s}/a.zig", .{dir}) }; } @@ -378,45 +759,141 @@ test "a client replays the open of a file at a revision and ends equal" { try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); } -test "provenance: a value a line, which pinned change wrote it, from what jj answers" { +test "Changes pins a revset's changes in this view: who wrote each line, which it added whole, each one's hunks and what they took away; bare again unpins; jj's refusals are said" { const gpa = testing.allocator; var repo = try Repo.init(gpa) orelse return error.SkipZigTest; defer repo.deinit(gpa); - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); - // `two` wrote lines 1 and 3 of its text, `one` line 2. - const annotated = switch (annotate(p, repo.file, "@-")) { - .bytes => |b| b, - .refused => |why| { - defer gpa.free(why); - std.debug.print("annotate: {s}\n", .{why}); - return error.Refused; - }, - }; - defer gpa.free(annotated); - for ([_][]const u8{ "@--::@-", "@-" }, [_][3]u16{ .{ 1, 0, 1 }, .{ 0, unpinned, 0 } }) |revset, want| { - const changes = switch (pinned(p, repo.dir, revset)) { - .bytes => |b| b, - .refused => |why| { - gpa.free(why); - return error.Refused; - }, - }; - defer gpa.free(changes); - const lines = try provenance(gpa, annotated, changes); - defer gpa.free(lines); - try testing.expectEqualSlices(u16, &want, lines); + 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].?; + try testing.expectEqualStrings(Repo.two, pane.file.?.content); + p.update(.{ .command = "Changes @--::@-" }); + try testing.expectEqualStrings("@--::@-", pane.layers.pinned()); + const ch = pane.changes.?; + try testing.expectEqualStrings("@--::@-", ch.pinned()); + try testing.expectEqual(@as(usize, 2), ch.pins.len); + for (ch.pins, [_][]const u8{ "one", "two" }) |pin, title| { + try testing.expectEqualStrings(title, pin.title[0..pin.title_len]); + try testing.expectEqualStrings("t", pin.author[0..pin.author_len]); + try testing.expect(pin.time > 0); } - // A revset jj refuses, in its words. - switch (pinned(p, repo.dir, "nosuch")) { - .bytes => |b| { - gpa.free(b); - return error.NotRefused; - }, - .refused => |why| { - defer gpa.free(why); - try testing.expect(std.mem.indexOf(u8, why, "nosuch") != null); - }, + // `two` changed line 1 (`one`'s first) and added line 3 whole; line 2 is `one`'s, added whole. + const one: PinIndex = @enumFromInt(0); + const two: PinIndex = @enumFromInt(1); + const want = [_]Line{ + .{ .pin = two, .whole = false, .lanes = .{ two, .none, .none } }, + .{ .pin = one, .whole = true, .lanes = .{ one, .none, .none } }, + .{ .pin = two, .whole = true, .lanes = .{ two, .none, .none } }, + }; + try testing.expectEqualSlices(Line, &want, ch.lines); + 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 }, + }; + try testing.expectEqualSlices(Hunk, &hunks, ch.hunks); + try testing.expectEqualStrings("const one = 1;\n", ch.removed); + // Bare: unpinned in this view; what jj said is kept for another view. + p.update(.{ .command = "Changes" }); + try testing.expectEqual(@as(u8, 0), pane.layers.revset_len); + try testing.expectEqual(@as(usize, 2), ch.pins.len); + // Bare again: `@`, the working copy's change, which (never snapshotted) touches nothing. + p.update(.{ .command = "Changes" }); + try testing.expect(std.mem.indexOf(u8, told(p, id), "Changes: no change of `@` touches a.zig") != null); + try testing.expectEqual(@as(u8, 0), pane.layers.revset_len); + // jj's refusal, in its words. + p.update(.{ .command = "Changes nosuch" }); + try testing.expect(std.mem.indexOf(u8, told(p, id), "Changes: Revision `nosuch` doesn't exist") != null); + try testing.expectEqual(@as(u8, 0), pane.layers.revset_len); +} + +test "a client replays Changes from what the session's jj answered and ends equal" { + const gpa = testing.allocator; + var repo = try Repo.init(gpa) orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const R = pardes.replica; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24 }, .{}, false); + defer a.deinit(); + const r = try R.Replica.init(gpa, 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(); + try testing.expect(r.diverged == null); + const id = a.core.view().active; + try testing.expectEqual(@as(usize, 2), a.core.panes[id].?.changes.?.pins.len); + try testing.expectEqualSlices(Line, a.core.panes[id].?.changes.?.lines, r.core().panes[id].?.changes.?.lines); + try testing.expectEqualStrings("@--::@-", r.core().panes[id].?.layers.pinned()); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); +} + +test "Changes draws a lane a pinned change after the line number, nested ones a level to the right on their rows only, and a line one added whole green across it; bare again, the gutter as it was" { + const gpa = testing.allocator; + // `fn` wrote the function; `mid` rewrote its lines 3-4 as 3-6 (two + // changed, two added whole); `shift` rewrote line 4 again. + const fn_text = "fn f() void {\n a();\n b();\n c();\n g();\n h();\n i();\n}\n"; + const mid_text = "fn f() void {\n a();\n b2();\n c2();\n x();\n y();\n g();\n h();\n i();\n}\n"; + const shift_text = "fn f() void {\n a();\n b2();\n c3();\n x();\n y();\n g();\n h();\n i();\n}\n"; + var repo = try Repo.with(gpa, &.{ .{ fn_text, "fn" }, .{ mid_text, "mid" }, .{ shift_text, "shift" } }, 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].?; + const before = File.gutterWidth(pane); + p.update(.{ .command = "Changes ::@" }); + try testing.expectEqual(@as(usize, 3), pane.changes.?.pins.len); + const cells: u16 = if (windowed) 2 else levels; + try testing.expectEqual(before + cells, File.gutterWidth(pane)); + 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; + const first = body.x + before; + var buf: [8][3]u8 = undefined; + 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. + 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)); + if (windowed) { + var n: usize = 0; + for (s.lanes.items) |lane| if (lane.y == y) { + try testing.expectEqual(first, lane.x); + try testing.expectEqual(lanes[lane.level] - '0', lane.level); + try testing.expectEqual(colours[lane.level], lane.rgb); + n += 1; + }; + try testing.expectEqual(lanes.len, n); + } else for (0..levels) |level| { + const cell = s.at(first + @as(u16, @intCast(level)), y); + if (level < lanes.len) { + try testing.expectEqualStrings("\u{258e}", cell.grapheme()); + try testing.expectEqual(pardes.Color{ .rgb = colours[level] }, cell.style.fg); + } else if (!cell.default and !std.mem.eql(u8, cell.grapheme(), " ")) { + std.debug.print("row {d} level {d}: [{s}] {any}\n", .{ row, level, cell.grapheme(), cell.style }); + return error.TestUnexpectedResult; + } + } + // `fn`'s lines it still has (1, 2, 7-10) and `mid`'s x() and + // y() (5, 6) were added whole; b2 (3) and c3 (4) changed one. + const whole = row != 2 and row != 3; + const text_cell = s.at(body.x + File.gutterWidth(pane) + 1, y); + try testing.expectEqual(whole, std.meta.eql(text_cell.style.bg, pardes.Color{ .rgb = ground })); + } + if (!windowed) try testing.expectEqual(@as(usize, 0), s.lanes.items.len); + p.update(.{ .command = "Changes" }); + try testing.expectEqual(before, File.gutterWidth(pane)); } } diff --git a/core/panes.zig b/core/panes.zig index 5277ebba..ee03ccf2 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -320,6 +320,9 @@ pub const Pane = struct { view: SessionView = .{}, /// The painters a file's body wears in this view (File.Layers). layers: File.Layers = .{}, + /// What jj answered for the last `Changes` in any view (jj.zig): the + /// pane's, so a client that joins late has it in the dump. + changes: ?*@import("jj.zig").Changes = null, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -1279,6 +1282,11 @@ pub fn teardownPane(p: *Pardes, pane: *Pane) void { pane.input.deinit(p.gpa); pane.jump.deinit(p.gpa); pane.places.deinit(p.gpa); + if (pane.changes) |changes| { + changes.deinit(p.gpa); + p.gpa.destroy(changes); + pane.changes = null; + } if (pane.query) |q| { q.deinit(p.gpa); p.gpa.destroy(q); diff --git a/core/replica.zig b/core/replica.zig index 97da9c74..dd3f13d1 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -6583,6 +6583,7 @@ const may_wait = [_]Wait{ .{ .what = "Mini *", .on = .any, .stop = "resolve", .why = "a file opened by name, from the session's disk" }, .{ .what = "Get", .on = .any, .stop = "read_file", .why = "the file read again from the session's disk" }, .{ .what = "Save", .on = .any, .stop = "write_file", .why = "a save" }, + .{ .what = "Changes", .on = .any, .stop = "jj", .why = "what jj says of the pinned changes, the session's (P14: a word typed may take a moment once)" }, // A program run, a shell opened or closed: the session's processes. .{ .what = "Exec *", .on = .any, .stop = "kind", .why = "a program run: its name asked of the disk first" }, .{ .what = "Exec *", .on = .any, .stop = "tty_taken", .why = "a program run in a shell: whether one has it, the session's" }, diff --git a/core/root.zig b/core/root.zig index 65f61e0d..de1c44b0 100644 --- a/core/root.zig +++ b/core/root.zig @@ -55,6 +55,7 @@ pub const Facts = @import("surface.zig").Facts; pub const Region = @import("surface.zig").Region; pub const Chrome = @import("surface.zig").Chrome; pub const RailTick = @import("surface.zig").RailTick; +pub const Lane = @import("surface.zig").Lane; pub const MAX_REGIONS = @import("surface.zig").MAX_REGIONS; /// What comes in (Event.zig). pub const Mouse = @import("Event.zig").Mouse; diff --git a/core/surface.zig b/core/surface.zig index 6e05f25f..653abd82 100644 --- a/core/surface.zig +++ b/core/surface.zig @@ -147,6 +147,10 @@ pub const MAX_REGIONS = 1 + MAX_PANES * (4 + panes.Pane.Notices.max) + pardes.MA /// to a detached session draws them from the frame alone. pub const RailTick = struct { owner: u8, at: u16, current: bool }; pub const max_rail_ticks = 4096; +/// A pinned change's lane on a body row (jj.paint): a pixel shell draws +/// it as a strip in gutter cell `x`, at its `level`'s place (sample A of +/// the ruled design: each a third of a cell wide, half that of air between). +pub const Lane = struct { x: u16, y: u16, level: u8, rgb: [3]u8 }; pub const rail_tick_scale = 1024; pub const Chrome = struct { @@ -256,6 +260,12 @@ pub const Surface = struct { /// rail's track, the pane's current match's marked. rail_ticks: [max_rail_ticks]RailTick = undefined, nrail_ticks: u16 = 0, + /// The lanes of the changes pinned on the bodies, for a pixel shell + /// (in a terminal they are glyphs in the cells): the frame's own + /// surface's, gpa's, as `placements` (a list, not an array: a frame + /// makes many a temporary Surface, and a safe build fills an array's + /// `undefined` in each). + lanes: std.ArrayList(Lane) = .empty, chrome: Chrome = .{}, cells: []Cell = &.{}, /// bar: draw an insert-style thin cursor instead of the block |
