diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Addon.zig | 18 | ||||
| -rw-r--r-- | core/Core.zig | 1 | ||||
| -rw-r--r-- | core/Effect.zig | 7 | ||||
| -rw-r--r-- | core/File.zig | 15 | ||||
| -rw-r--r-- | core/Kind.zig | 2 | ||||
| -rw-r--r-- | core/Stream.zig | 2 | ||||
| -rw-r--r-- | core/body_layer.zig | 2 | ||||
| -rw-r--r-- | core/builtins.zig | 6 | ||||
| -rw-r--r-- | core/config.zig | 3 | ||||
| -rw-r--r-- | core/draw.zig | 2 | ||||
| -rw-r--r-- | core/exec.zig | 3 | ||||
| -rw-r--r-- | core/jj.zig | 537 | ||||
| -rw-r--r-- | core/regexp.zig | 45 | ||||
| -rw-r--r-- | core/replica.zig | 27 | ||||
| -rw-r--r-- | core/root.zig | 5 | ||||
| -rw-r--r-- | core/surface.zig | 6 | ||||
| -rw-r--r-- | core/tape.zig | 3 |
17 files changed, 609 insertions, 75 deletions
diff --git a/core/Addon.zig b/core/Addon.zig index c60f2430..aa3195f9 100644 --- a/core/Addon.zig +++ b/core/Addon.zig @@ -567,9 +567,11 @@ pub fn loadedOf(ops: *const Kind.Ops, path: []const u8, hash: [32]u8, words: *[6 /// This process's code for a kind a session's core lists, which a client /// runs itself: PA_PLAIN, of the same name, version, ABI and functions (a -/// compiled-in addon's library hash is 0: never by the hash), and not -/// caught breaking its promise here (`distrusted`, the client's). Null: its -/// panes say so. +/// compiled-in addon's library hash is 0: never by the hash), or else the +/// library at the session's path when its bytes are the session's (P51: a +/// client on its machine; never bytes the session sent), and not caught +/// breaking its promise here (`distrusted`, the client's). Null: its panes +/// say so. pub fn plainOps(e: *const Kind.Entry, distrusted: ?*const Distrusted) ?*const Kind.Ops { if (!e.caps.plain or e.refused) return null; // A client scans for its addons only when its session has a plain kind. @@ -582,6 +584,16 @@ pub fn plainOps(e: *const Kind.Entry, distrusted: ?*const Distrusted) ?*const Ki if (l.ops == 0) l.ops = loadListed(i, &why) orelse return null; return @ptrFromInt(l.ops); }; + var words: [64]LoadedWord = undefined; + var why: Why = .{}; + var data_buf: [4096]u8 = undefined; + const l = read(dataDir(&data_buf), e.path.slice(), &words, &why); + if (l.why.len > 0) return null; + const ops: *const Kind.Ops = @ptrFromInt(l.ops); + if (std.mem.eql(u8, &l.hash, &e.hash)) return ops; + const lib = libOf(ops).?; + lib.refs -= 1; + closeIdle(lib); return null; } diff --git a/core/Core.zig b/core/Core.zig index 2d832f48..a35cea8e 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -883,6 +883,7 @@ pub fn deinit(p: *Pardes) void { for (&p.surface.tag_layers) |*layer| gpa.free(layer.cells); p.surface.placements.deinit(gpa); p.surface.lanes.deinit(gpa); + p.surface.heat.deinit(gpa); gpa.destroy(p); } diff --git a/core/Effect.zig b/core/Effect.zig index abfe4711..bdf41e66 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -256,9 +256,10 @@ fn performInner(p: *Pardes, e: Effect) void { .dump_themes => |d| _ = p.tape.host(p, .dump_themes, .{d.pane}), .detach => |d| p.tape.host(p, .detach, .{}) orelse p.reportError(d.pane, "detach", error.NotAttached), // dlopen is the same on every host, so none has a method of its - // own; a replica's core holds no library (what it brought comes in - // the event this taped). - .addon => |a| if (!p.tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane.index(), a.arg.slice()), + // own; a replica's core holds no library: what it brought is the + // event inside this call, recorded there and applied by a replay + // (its code a client's own, Addon.plainOps); a speculation waits. + .addon => |a| if (!p.tape.authorityOnly("an addon's load")) p.tape.value(.landing, @import("Addon.zig").perform, .{ p, a.op, a.pane.index(), a.arg.slice() }), .job => |j| p.tape.host(p, .job, .{ j.pane, j.serial, j.token }) orelse if (!p.tape.replaying()) host_io.KindJob.runHere(p, j.pane.index(), j.serial, j.token), // the loop's own condition; a host tears down after its own loop .quit => p.endSession(), diff --git a/core/File.zig b/core/File.zig index 347f4809..c4fc8a38 100644 --- a/core/File.zig +++ b/core/File.zig @@ -373,7 +373,7 @@ 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) + laneCells(pane); + return @max(config.PREFIX_W, digits + 1) + laneCells(pane) + pane.layers.heat; } /// The cells the changes' lanes take after the line number in this view @@ -758,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) jj.edited(p, f, off, len, bytes); + if (p.changes.answers.items.len | p.changes.heats.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); @@ -1779,7 +1779,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, laneCells(pane), + pane.view.rect_h, laneCells(pane) + @as(u16, pane.layers.heat), }; // One hash of them laid out as words: every reader of the map asks for // this key, a hundred times a frame. @@ -1797,8 +1797,8 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { if (record_wrap) pane.wrap_n = 0; const prefix_width = gutterWidth(pane); - // Where a number ends: a blank, then the lanes (laneCells), after it. - const number_end = prefix_width -| laneCells(pane) -| 1; + // Where a number ends: a blank, then the heat lane and the lanes after it. + const number_end = prefix_width -| laneCells(pane) -| pane.layers.heat -| 1; const total = nlines(pane.gpa, f); // The slot after the text's last newline, found once: every numbered // row below asks whether it is that one. @@ -1949,9 +1949,12 @@ pub const Layers = struct { /// 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, + /// Whether this view shows the pane's churn (`Churn`, jj.Heat), as the + /// cells its lane takes: one after the number, before the lanes. + heat: u8 = 0, pub fn digest(l: *const Layers, h: *pardes.walk.Hasher) void { - h.update(&.{ @intFromBool(l.ink), l.lanes }); + h.update(&.{ @intFromBool(l.ink), l.lanes, l.heat }); } }; diff --git a/core/Kind.zig b/core/Kind.zig index 81997afb..49076f78 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -424,7 +424,7 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa if (slot.ops == null and slot.live) { // A client: a plain kind runs where it is shown, and this one lacks // it; any other runs in its session alone. - const why = if (caps(p, slot).plain) "it is not in its addons, or it broke its promise here" else "its state is not plain (PA_PLAIN), so it runs in the session alone"; + const why = if (caps(p, slot).plain) "it is not in its addons nor the session's library at its path here, or it broke its promise here" else "its state is not plain (PA_PLAIN), so it runs in the session alone"; var line: [200]u8 = undefined; _ = s.print(x, y, cols, std.fmt.bufPrint(&line, "addon {s} {d} does not run in this client: {s}", .{ slot.nameOf(), slot.version, why }) catch &line, .{ .fg = fg, .bg = bg }); return; diff --git a/core/Stream.zig b/core/Stream.zig index 1d061b52..7956e4f2 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -735,6 +735,8 @@ fn receiveParse(p: *Pardes, b: *const Batch, serial: u32, revision: u32) void { } else |_| {} }; p.needs_frame = true; + // A `Churn` waiting on these declarations (jj.zig). + @import("jj.zig").parsed(p, serial); if (s.parse_next) |next| { s.parse_next = null; if (p.paneBySerial(next.serial)) |id| parseFor(p, id); diff --git a/core/body_layer.zig b/core/body_layer.zig index cb518af4..95fb2700 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -341,7 +341,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane drawGutter(p, s, pane, r, tx, tw, body_h, active); // Never inlined: in renderBody's own frame it cost every body a // frame (a terminal's output about 33,000 instructions a pump). - if (pane.layers.lanes > 0) @call(.never_inline, @import("jj.zig").paint, .{ p, s, pane, r, tx, tw, body_h }); + if (pane.layers.lanes | pane.layers.heat > 0) @call(.never_inline, @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 7485af81..e6d22c17 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1086,6 +1086,12 @@ pub const Changes = struct { pub const run = @import("jj.zig").changesWord; }; +/// Count, for each declaration of a file's pane, the changes in `window` (jj) that touched it, and show it in this view: a heat lane after the line number and a strip by the scroll thumb, the line numbers' grey to the theme's red on a log scale; `window` a revset or a span of time (`3d`, `2w`, `6m`, `1y`); bare, shown here or not (none counted yet: the file's whole history). +pub const Churn = struct { + pub const takes_arg = true; + pub const run = @import("jj.zig").churnWord; +}; + /// 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 1f5819c9..aae832f4 100644 --- a/core/config.zig +++ b/core/config.zig @@ -77,7 +77,8 @@ pub const leader_path = paths: { .Filter = null, .Diff = null, .At = null, - .Changes = null, + .Changes = "tc", + .Churn = "tu", .Mode = null, .Togglettymode = null, .Collapse = null, diff --git a/core/draw.zig b/core/draw.zig index 3d2a1452..8198e2ff 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -86,6 +86,7 @@ pub fn place(p: *Pardes, s: *Surface) void { s.nregions = 0; s.nrail_ticks = 0; s.lanes.clearRetainingCapacity(); + s.heat.clearRetainingCapacity(); // The facts filled in the walk the frame's places take anyway. viewFacts(p, &p.facts); s.facts = &p.facts; @@ -510,6 +511,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: } try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*); + if (pane.layers.heat > 0) @import("jj.zig").railHeat(p, s, pane, id, rail.rect); // A terminal panned to its cursor in a view shorter than it: an arrow // in the gutter's first body row says rows are above (views.zig). if (pane.pan > 0) s.set(r.x, p.bodyTop(pane, r), "↑", .{ .fg = .{ .rgb = chrome.box } }); diff --git a/core/exec.zig b/core/exec.zig index ae9111f8..d1e1830f 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -20,7 +20,6 @@ const python_lang = blk: { @setEvalBranchQuota(20_000); break :blk @import("grammar_manifest.zig").byName("python").?; }; -const font_picker = pardes.font_picker; const fonts = pardes.fonts; const Pane = panes.Pane; const MAX_COLS = pardes.MAX_COLS; @@ -1013,7 +1012,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Settings.Setting, arg: ?[ return p.reportFailure(v.active, text.print("Theme: no theme \"{s}\"; like it: {s}; Themes lists all", .{ shown, colors.themesNear(near[0..@min(room, near.len)], want) })); }, .font => { - if (comptime !font_picker) return; + if (comptime !pardes.builtins.capabilities.font_picker) return; const spec = config.Settings.FontSpec.parse(arg orelse return) orelse return; // The session's machine's fonts, a question to its disk: a // client's replay takes the session's answer, never its own. diff --git a/core/jj.zig b/core/jj.zig index 6a7d7da4..a0dfec10 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -17,6 +17,7 @@ const Output = pardes.panes.Output; const look = @import("look.zig"); const layout = @import("layout.zig"); const diff = @import("diff.zig"); +const syntax = @import("syntax.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 @@ -253,15 +254,60 @@ pub fn answered(p: *Pardes, serial: u32) ?*Changes { return null; } +/// What jj answered for pane `serial`'s `Churn` (`Pardes.changes`), counted. +pub fn churned(p: *Pardes, serial: u32) ?*Heat { + for (p.changes.heats.items) |*h| if (h.serial == serial) return h; + return null; +} + /// Pane `serial`'s answers let go (the pane's end). pub fn forget(p: *Pardes, serial: u32) void { for (p.changes.answers.items, 0..) |*c, i| if (c.serial == serial) { c.deinit(p.gpa); _ = p.changes.answers.swapRemove(i); + break; + }; + forgetHeat(p, serial); +} + +fn forgetHeat(p: *Pardes, serial: u32) void { + for (p.changes.heats.items, 0..) |*h, i| if (h.serial == serial) { + h.deinit(p.gpa); + _ = p.changes.heats.swapRemove(i); return; }; } +/// A pane's `Churn [window]` (PARDES.md "Changes on the source view": +/// churn per declaration): for each line, how many of the window's changes +/// touched the declaration it is in (tree-sitter's innermost, as +/// TreeContext finds them; 0 outside any), counted in every process from +/// jj's raw answer (`heatOf`), replicated state as `Changes` is, riding +/// with the lines through edits. +pub const Heat = struct { + pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; + serial: u32, + /// The window as jj was asked it. + revset: [Changes.revset_max]u8 = undefined, + revset_len: u8 = 0, + lines: []u16 = &.{}, + /// The file's hottest declaration's count, the gradient's top. + hottest: u16 = 0, + /// jj's raw answer until it is counted (`parsed`): its log, then its + /// text of the revision from `pending_was` on. + pending: []u8 = &.{}, + pending_was: u32 = 0, + + pub fn deinit(h: *Heat, gpa: std.mem.Allocator) void { + gpa.free(h.lines); + gpa.free(h.pending); + } + + pub fn window(h: *const Heat) []const u8 { + return h.revset[0..h.revset_len]; + } +}; + /// A pinned change, as `jj log` says it. pub const Pin = struct { pub const replica = .{ .len = .{ .{ "author", "author_len" }, .{ "title", "title_len" } } }; @@ -378,7 +424,57 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { else return p.reportFailure(c.id, "Changes: only a file's pane takes it"); pane.layers.lanes = cells; - ask(p, c.id, pane.serial, shown.path, shown.rev, revset); + ask(p, .changes, c.id, pane.serial, shown.path, shown.rev, revset); +} + +/// The `Churn` word (builtins.zig registers it): in a file's pane (or a +/// revision's), how many changes in `window` touched each declaration, +/// shown in this view as a heat lane after the line number and a strip by +/// the scroll thumb. The window: a revset, or a span of time back from now +/// (`3d`, `2w`, `6m`, `1y`: what the file's history has in it); bare, shown +/// here or not, and with none counted yet the file's whole history (`::` +/// the revision shown). +pub fn churnWord(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, "Churn: 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.heat > 0) { + pane.layers.heat = 0; + return; + } + if (arg.len == 0 and churned(p, pane.serial) != null) { + pane.layers.heat = 1; + return; + } + 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, "Churn: only a file's pane takes it"); + var buf: [Changes.revset_max]u8 = undefined; + const window = spanOf(&buf, shown.rev, arg) orelse arg; + if (window.len > Changes.revset_max) return p.reportFailure(c.id, "Churn: a window that long"); + pane.layers.heat = 1; + ask(p, .churn, c.id, pane.serial, shown.path, shown.rev, window); +} + +/// `arg` as a revset: bare, every change of the revision's history; a span +/// of time (a count and `h`, `d`, `w`, `m` or `y`), its changes since then +/// (jj's `author_date`); null for a revset. +fn spanOf(buf: []u8, rev: []const u8, arg: []const u8) ?[]const u8 { + if (arg.len == 0) return std.fmt.bufPrint(buf, "::({s})", .{rev}) catch null; + const n = std.fmt.parseInt(u16, arg[0 .. arg.len - 1], 10) catch return null; + const unit: []const u8 = switch (arg[arg.len - 1]) { + 'h' => "hours", + 'd' => "days", + 'w' => "weeks", + 'm' => "months", + 'y' => "years", + else => return null, + }; + return std.fmt.bufPrint(buf, "::({s}) & author_date(after:\"{d} {s} ago\")", .{ rev, n, unit }) catch null; } // ---- jj off the loop (Stream.zig's model) ---- @@ -392,6 +488,7 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { /// run a site of the step's tape. pub const Ask = struct { gpa: std.mem.Allocator, + kind: Kind = .changes, /// What its answer is made with: the shell's, which frees a batch. out: std.mem.Allocator, gen: u32, @@ -400,11 +497,15 @@ pub const Ask = struct { rev: []u8, revset: []u8, - fn make(gpa: std.mem.Allocator, gen: u32, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) error{OutOfMemory}!*Ask { + /// `Changes`' (with who wrote each line), or `Churn`'s (with jj's text + /// of the revision, which the counts are lined up with). + pub const Kind = enum(u8) { changes, churn }; + + fn make(gpa: std.mem.Allocator, kind: Kind, gen: u32, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) error{OutOfMemory}!*Ask { const a = try gpa.create(Ask); errdefer gpa.destroy(a); const owned = try std.mem.concat(gpa, u8, &.{ path, rev, revset }); - a.* = .{ .gpa = gpa, .out = gpa, .gen = gen, .serial = serial, .path = owned[0..path.len], .rev = owned[path.len..][0..rev.len], .revset = owned[path.len + rev.len ..] }; + a.* = .{ .gpa = gpa, .kind = kind, .out = gpa, .gen = gen, .serial = serial, .path = owned[0..path.len], .rev = owned[path.len..][0..rev.len], .revset = owned[path.len + rev.len ..] }; return a; } @@ -421,6 +522,7 @@ pub const Ask = struct { /// `Changes` is asked, then one update while nothing is asked of jj. pub const Session = struct { answers: std.ArrayList(Changes) = .empty, + heats: std.ArrayList(Heat) = .empty, task: ?Task = null, next: ?*Ask = null, count: u32 = 0, @@ -430,6 +532,7 @@ pub const Session = struct { const bits: u64 = c.count | @as(u64, @intFromBool(c.task != null)) << 32 | @as(u64, @intFromBool(c.next != null)) << 33; h.update(std.mem.asBytes(&bits)); pardes.walk.hash(std.ArrayList(Changes), &c.answers, h); + pardes.walk.hash(std.ArrayList(Heat), &c.heats, h); if (c.task) |*t| pardes.walk.hash(Task, t, h); if (c.next) |n| pardes.walk.hash(Ask, n, h); } @@ -448,6 +551,8 @@ pub const Session = struct { } for (c.answers.items) |*a| a.deinit(gpa); c.answers.deinit(gpa); + for (c.heats.items) |*h| h.deinit(gpa); + c.heats.deinit(gpa); } }; @@ -470,10 +575,10 @@ fn liveGen(c: *const Session) u32 { /// Ask jj of `revset` for pane `serial` (said at pane `id` while it runs): /// what the pane shows stays until the answer lands. -fn ask(p: *Pardes, id: usize, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) void { +fn ask(p: *Pardes, kind: Ask.Kind, id: usize, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) void { p.changes.count +%= 1; - const a = Ask.make(p.gpa, liveGen(&p.changes), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); - p.setStatus(.at(id), "Changes: jj\u{2026}"); + const a = Ask.make(p.gpa, kind, liveGen(&p.changes), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); + p.setStatus(.at(id), if (kind == .churn) "Churn: jj\u{2026}" else "Changes: jj\u{2026}"); if (p.changes.task != null) { if (p.changes.next) |old| old.deinit(); p.changes.next = a; @@ -550,6 +655,14 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc if (if (entryOf(l)) |e| e.pinned else false) break true; } else false; if (!any) return .{ .gen = a.gen, .rows = logged, .end = .done }; + // Churn: jj's text of the revision, to line the counts up with the pane's. + if (a.kind == .churn) return switch (runJj(p, gpa, dirOf(a.path), &.{ "file", "show", "-r", a.rev, "--", set })) { + .refused => |why| { + gpa.free(logged); + return failed(gpa, a, why); + }, + .bytes => |shown| .{ .gen = a.gen, .rows = logged, .styles = shown, .end = .done }, + }; // Who wrote each line: a path, not a fileset (annotate takes one file // as it is named). const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\t\" ++ content", "--", std.Io.Dir.path.basename(a.path) }; @@ -575,17 +688,19 @@ fn runJj(p: ?*Pardes, gpa: std.mem.Allocator, dir: []const u8, args: []const []c } /// A `Changes` answer (`Event.search` with a `Changes` generation), -/// borrowed: the last asked's is parsed and pinned on its pane (the ask -/// names it); an earlier one's, or a gone pane's, dropped. Then the one -/// asked meanwhile goes. +/// borrowed: the last asked of its kind is parsed and pinned (or counted, +/// a `Churn`'s) on its pane (the ask names it); an earlier one's, or a gone +/// pane's, dropped. Then the one asked meanwhile goes. pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch) void { var revset: [Changes.revset_max]u8 = undefined; var revset_len: usize = 0; var serial: u32 = 0; + var kind: Ask.Kind = .changes; if (p.changes.task) |t| if (t.ask.gen == b.gen) { p.changes.task = null; if (t.future) |f| pardes.Stream.join(f, t.io.?, .wait); serial = t.ask.serial; + kind = t.ask.kind; revset_len = t.ask.revset.len; @memcpy(revset[0..revset_len], t.ask.revset); t.ask.deinit(); @@ -594,9 +709,14 @@ pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch) void { p.changes.next = null; go(p, next); }; - if (b.gen != liveGen(&p.changes) or revset_len == 0) return; + if (revset_len == 0) return; + // A later one of its kind asked meanwhile: only that one is shown. + if (p.changes.next) |n| if (n.kind == kind) return; const id = p.paneBySerial(serial) orelse return; - pinAnswer(p, id, b, revset[0..revset_len]); + switch (kind) { + .changes => pinAnswer(p, id, b, revset[0..revset_len]), + .churn => heatAnswer(p, id, b, revset[0..revset_len]), + } } /// A splice of `f`'s text, before its bytes go in (File.edit, the one @@ -619,6 +739,18 @@ pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: [ showGhosts(p, p.panes[id].?); }; } + i = p.changes.heats.items.len; + while (i > 0) { + i -= 1; + const h = &p.changes.heats.items[i]; + const id = p.paneBySerial(h.serial) orelse continue; + const shown = if (p.panes[id].?.file) |*shown| shown else continue; + if (shown.content.ptr != f.content.ptr) continue; + _ = spliceValues(u16, p.gpa, &h.lines, 0, f.content, off, len, bytes) catch { + h.deinit(p.gpa); + _ = p.changes.heats.swapRemove(i); + }; + } } /// `c`'s values spliced as `text`'s lines are by the splice of `len` bytes @@ -628,20 +760,10 @@ pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: [ /// before it. Else the line it starts in keeps its value, and the lines /// taken and put come after it. fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len: usize, bytes: []const u8) error{OutOfMemory}!void { - const taken = std.mem.count(u8, text[off..][0..len], "\n"); - const put = std.mem.count(u8, bytes, "\n"); - if (taken == 0 and put == 0) return; - const line_start = if (std.mem.lastIndexOfScalar(u8, text[0..off], '\n')) |nl| nl + 1 else 0; - const in_lead = std.mem.trimStart(u8, text[line_start..off], " \t").len == 0; - const from = std.mem.count(u8, text[0..line_start], "\n") + @intFromBool(!in_lead); - if (from > c.lines.len) return; - const take = @min(taken, c.lines.len - from); - const next = try gpa.alloc(Line, c.lines.len - take + put); - @memcpy(next[0..from], c.lines[0..from]); - @memset(next[from..][0..put], .{ .pin = .none }); - @memcpy(next[from + put ..], c.lines[from + take ..]); - gpa.free(c.lines); - c.lines = next; + const cut = try spliceValues(Line, gpa, &c.lines, .{ .pin = .none }, text, off, len, bytes) orelse return; + const from = cut.from; + const take = cut.take; + const put = cut.put; // A ghost row stands before the line it stood before; one before a // line taken out, before the line after what took its place. for (c.ghosts) |*g| { @@ -650,6 +772,26 @@ fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len } } +/// A value a line, `values`, spliced as `splice` says, a line put in +/// `none`; null when no line is taken or put. +fn spliceValues(comptime T: type, gpa: std.mem.Allocator, values: *[]T, none: T, text: []const u8, off: usize, len: usize, bytes: []const u8) error{OutOfMemory}!?struct { from: usize, take: usize, put: usize } { + const taken = std.mem.count(u8, text[off..][0..len], "\n"); + const put = std.mem.count(u8, bytes, "\n"); + if (taken == 0 and put == 0) return null; + const line_start = if (std.mem.lastIndexOfScalar(u8, text[0..off], '\n')) |nl| nl + 1 else 0; + const in_lead = std.mem.trimStart(u8, text[line_start..off], " \t").len == 0; + const from = std.mem.count(u8, text[0..line_start], "\n") + @intFromBool(!in_lead); + if (from > values.len) return null; + const take = @min(taken, values.len - from); + const next = try gpa.alloc(T, values.len - take + put); + @memcpy(next[0..from], values.*[0..from]); + @memset(next[from..][0..put], none); + @memcpy(next[from + put ..], values.*[from + take ..]); + gpa.free(values.*); + values.* = next; + return .{ .from = from, .take = take, .put = put }; +} + /// jj's answer `b` for `revset` parsed and pinned on pane `id`, or its /// refusal said there. fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []const u8) void { @@ -689,6 +831,118 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con } } +/// jj's answer `b` for a `Churn` of `window` on pane `id`, kept raw +/// until the text's declarations are there (a worker's parse, +/// Stream.parseFor: a big file's parse is seconds in a Debug build, never +/// the loop's), then counted (`count`); or its refusal said there. +fn heatAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, window: []const u8) void { + const pane = p.panes[id].?; + var line: pardes.Kind.Buf(512) = .{}; + if (pane.file == null) return; + if (b.end == .failed) { + if (churned(p, pane.serial) == null) pane.layers.heat = 0; + return p.reportFailure(id, line.print("Churn: {s}", .{b.rows[0..@min(b.rows.len, 480)]})); + } + var heat: Heat = .{ .serial = pane.serial, .revset_len = @intCast(window.len) }; + @memcpy(heat.revset[0..window.len], window); + heat.pending = std.mem.concat(p.gpa, u8, &.{ b.rows, b.styles }) catch return p.reportFailure(id, "Churn: out of memory"); + heat.pending_was = @intCast(b.rows.len); + if (churned(p, pane.serial)) |kept| { + kept.deinit(p.gpa); + kept.* = heat; + } else p.changes.heats.append(p.gpa, heat) catch { + heat.deinit(p.gpa); + pane.layers.heat = 0; + return p.reportFailure(id, "Churn: out of memory"); + }; + parsed(p, pane.serial); +} + +/// A parse of pane `serial`'s text landed (Stream.receiveParse), or jj's +/// answer did: a `Churn` waiting on it counted when the declarations are of +/// the text as it is, else a parse of it asked. +pub fn parsed(p: *Pardes, serial: u32) void { + const h = churned(p, serial) orelse return; + if (h.pending.len == 0) return; + const id = p.paneBySerial(serial) orelse return; + const pane = p.panes[id].?; + const f = if (pane.file) |*file| file else return; + var line: pardes.Kind.Buf(512) = .{}; + if (!syntax.supportsPath(f.path)) { + // No grammar for it (a revision's pane too: its name ends `@rev`). + p.reportFailure(id, line.print("Churn: no declarations to count in {s}", .{std.Io.Dir.path.basename(f.path)})); + pane.layers.heat = 0; + return forgetHeat(p, serial); + } + if (f.context.revision != f.revision) return pardes.Stream.parseFor(p, id); + const lines = heatOf(p.gpa, p.scratch.allocator(), h.pending[0..h.pending_was], h.pending[h.pending_was..], f.content, f.context.declarations, &h.hottest) catch |err| return p.reportFailure(id, line.print("Churn: {s}", .{@errorName(err)})); + p.gpa.free(h.pending); + h.pending = &.{}; + h.lines = lines; + if (h.hottest > 0) return p.setStatus(.at(id), ""); + p.reportFailure(id, line.print("Churn: no change of `{s}` touches a declaration of {s}", .{ h.window(), std.Io.Dir.path.basename(File.langPath(f)) })); + pane.layers.heat = 0; + forgetHeat(p, serial); +} + +/// For each line of `text` (`decls` its declarations), how many of the window's +/// changes in `logged` (jj's log, `p` the window's, `c` a later change +/// that only carries places) touched the declaration it is in: tree- +/// sitter's innermost, a line in none 0. A change's hunk touches the lines +/// it wrote, carried into the revision jj showed (`was`) through the later +/// changes' hunks, then into `text` (`Moved`); a hunk that only took lines +/// away, the line above where they stood. Each change counts once a +/// declaration. `hottest`: the largest count. +fn heatOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, was: []const u8, text: []const u8, decls: []const syntax.ContextDeclaration, hottest: *u16) error{OutOfMemory}![]u16 { + var hunks: std.ArrayList(Hunk) = .empty; + var removed: std.ArrayList(u8) = .empty; + var windowed: std.ArrayList(bool) = .empty; + var starts: std.ArrayList(usize) = .empty; + var it = std.mem.splitScalar(u8, logged, '\n'); + while (it.next()) |l| if (entryOf(l)) |e| { + if (windowed.items.len == Hunk.carrier) break; + try windowed.append(arena, e.pinned); + try starts.append(arena, it.index orelse logged.len); + }; + try starts.append(arena, logged.len); + for (starts.items[0 .. starts.items.len - 1], starts.items[1..], 0..) |from, to, i| + try hunksOf(arena, arena, Hunk.carrier, @intCast(i), logged[from..to], text, &.{}, &hunks, &removed, undefined, undefined); + const n = lineCount(text); + const moved: Moved = .of(was, text); + // Each line's innermost declaration (outermost first, so an inner one + // writes over its container's lines). + const none = std.math.maxInt(u32); + const inner = try arena.alloc(u32, n); + @memset(inner, none); + for (decls, 0..) |d, k| { + if (d.start_line >= n) continue; + @memset(inner[d.start_line..@min(n, d.end_line + 1)], @intCast(k)); + } + const counts = try arena.alloc(u16, decls.len); + @memset(counts, 0); + // The last change each declaration was counted for. + const seen = try arena.alloc(u32, decls.len); + @memset(seen, none); + for (hunks.items) |h| { + if (!windowed.items[h.step] or n == 0) continue; + const a = carried(hunks.items, h.step, h.new_start -| 1 + @intFromBool(h.new_len == 0), moved) orelse continue; + const b = carried(hunks.items, h.step, h.new_start -| 1 + h.new_len, moved) orelse continue; + const lo = @min(if (h.new_len == 0) a -| 1 else a, n - 1); + for (inner[lo..@max(lo + 1, @min(b, n))]) |k| { + if (k == none or seen[k] == h.step) continue; + seen[k] = h.step; + counts[k] +|= 1; + } + } + const out = try gpa.alloc(u16, n); + hottest.* = 0; + for (out, inner) |*o, k| { + o.* = if (k == none) 0 else counts[k]; + hottest.* = @max(hottest.*, o.*); + } + return out; +} + /// The pane's ghost rows as its geometry reads them (`Pane.ghosts`): the /// session's answers' (one set a pane), its geometry made again when they /// change (each step while a view shows them, and as an answer lands). @@ -808,21 +1062,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); - const head = @min(std.mem.count(u8, was.items[0..span.head], "\n"), out.len, owners.items.len); - for (out[0..head], owners.items[0..head]) |*l, pin| l.pin = pin; - var tail = @min(tailLines(was.items, span.tail), out.len - head, owners.items.len - head); - // The line the two ends meet inside (an insertion before it shares its - // 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; + moved.* = .of(was.items, text); + for (out[0..@min(moved.head, owners.items.len)], owners.items[0..@min(moved.head, owners.items.len)]) |*l, pin| l.pin = pin; + const tail = @min(moved.tail, owners.items.len); 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; } @@ -837,6 +1080,21 @@ pub const Moved = struct { was: u32, now: u32, + /// `was` (jj's text) lined up with `text` by what they agree on at + /// either end (`File.spanOf`, the editor's differ). + pub fn of(was: []const u8, text: []const u8) Moved { + const was_n: u32 = @intCast(lineCount(was)); + const now: u32 = @intCast(lineCount(text)); + if (std.mem.eql(u8, was, text)) return .{ .head = was_n, .tail = 0, .was = was_n, .now = was_n }; + const span = File.spanOf(was, text); + const head = @min(std.mem.count(u8, was[0..span.head], "\n"), now, was_n); + var tail = @min(tailLines(was, span.tail), now - head, was_n - head); + // The line the two ends meet inside (an insertion before it shares its + // indent with the line above) is the same line when its bytes are. + if (head + tail < @min(now, was_n) and std.mem.eql(u8, lineFromEnd(was, tail), lineFromEnd(text, tail))) tail += 1; + return .{ .head = @intCast(head), .tail = @intCast(tail), .was = was_n, .now = now }; + } + /// 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 { @@ -914,7 +1172,6 @@ fn hunksOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, k: u16, step: u16, else => return, }; }; - const syntax = @import("syntax.zig"); var said = std.mem.splitScalar(u8, patch.text, '\n'); var run_from = taken.items.len; var plus_from = added.items.len; @@ -1017,13 +1274,13 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, ti += h.old_len; // A changed line's old text right above the line; a line taken // away whole above the next changed one's, or after the run. - var at = carried(hunks, h.pin, if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start, moved); + var at = carried(hunks, h.step, if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start, moved); var ats: [max_run]?u32 = undefined; if (run.len > max_run) continue; var k = run.len; while (k > 0) { k -= 1; - if (run[k].paired) at = run[k].shown orelse carried(hunks, h.pin, run[k].new -| 1, moved); + if (run[k].paired) at = run[k].shown orelse carried(hunks, h.step, run[k].new -| 1, moved); ats[k] = at; } var from: usize = h.removed; @@ -1055,11 +1312,13 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, } var first: u32 = 0; var end: u32 = 0; + var step: u16 = 0; for (hunks) |h| if (h.pin == k) { if (first == 0) first = h.new_start; end = if (h.new_len > 0) h.new_start - 1 + h.new_len else h.new_start; + step = h.step; }; - const place = at orelse carried(hunks, @intCast(k), end, moved) orelse continue; + const place = at orelse carried(hunks, step, end, moved) orelse continue; // After its own ghost rows at its place. for (made.items) |m| if (m.g.pin == k and m.g.at == place) { rank = m.rank | 1; @@ -1081,15 +1340,12 @@ 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 -/// text: moved by each later change's hunks before it (a pin's, or one +/// Where the place after line `after` of step `from`'s revision (its +/// change's place in jj's answer, `Hunk.step`) 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; +fn carried(hunks: []const Hunk, from: u16, after: u32, moved: Moved) ?u32 { var pos: i64 = after; var i: usize = 0; while (i < hunks.len) { @@ -1290,7 +1546,7 @@ fn nest(lines: []Line, npins: usize) void { /// 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 grounds = [2][3]u8{ themeRed(th), 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; @@ -1347,6 +1603,64 @@ fn hueApart(a: [3]u8, b: [3]u8) f32 { /// (`Ghost`) its lanes and its cells (`inkGhost`). Not yet: a changed /// line's words. pub fn paint(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { + if (pane.layers.heat > 0) paintHeat(p, s, pane, r, tx, body_h); + if (pane.layers.lanes > 0) paintLanes(p, s, pane, r, tx, tw, body_h); +} + +/// The theme's red: what a change took away; churn's top. +pub fn themeRed(th: *const pardes.Theme) [3]u8 { + return if (th.palette) |c| c[1] else .{ 0xcf, 0x22, 0x2e }; +} + +/// A count's colour on churn's gradient: smooth from the line numbers' +/// grey to the theme's red, on a log scale of the count against the +/// hottest; null for none. +fn heatColour(th: *const pardes.Theme, count: u16, hottest: u16) ?[3]u8 { + if (count == 0 or hottest == 0) return null; + const t = @log(@as(f32, @floatFromInt(count)) + 1) / @log(@as(f32, @floatFromInt(hottest)) + 1); + return pardes.colors.mixOklab(th.lineno, themeRed(th), t); +} + +/// The heat lane: a cell after the line number (before the changes' +/// lanes, which stay next to the text they mark), a half block in its +/// line's colour; none on a ghost row or a line in no declaration. +fn paintHeat(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u16, body_h: u16) void { + const heat = churned(p, pane.serial) orelse return; + const x = tx + File.gutterWidth(pane) - File.laneCells(pane) - 1; + 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 (vr < pane.wrap_n and pane.wrap_col[vr] < 0) continue; + if (row < 0 or row >= heat.lines.len) continue; + const rgb = heatColour(p.theme(), heat.lines[@intCast(row)], heat.hottest) orelse continue; + var style = s.at(x, body_y + vr).style; + style.fg = .{ .rgb = rgb }; + s.set(x, body_y + vr, "\u{258c}", style); + } +} + +/// Churn's strip by the scroll thumb (draw.zig, while a view shows it): a +/// rail row each, the hottest line of the lines that row stands for; in a +/// terminal the rail's second cell's ground, for a pixel shell a list +/// (Surface.heat). +pub fn railHeat(p: *Pardes, s: *pardes.Surface, pane: *Pane, id: usize, rail: pardes.Rect) void { + const heat = churned(p, pane.serial) orelse return; + const total = heat.lines.len; + if (rail.h == 0 or total == 0) return; + for (0..rail.h) |row| { + const lo = row * total / rail.h; + const hi = @max(lo + 1, (row + 1) * total / rail.h); + const rgb = heatColour(p.theme(), std.mem.max(u16, heat.lines[lo..@min(hi, total)]), heat.hottest) orelse continue; + if (p.view().frontend.windowed) + s.heat.append(p.gpa, .{ .owner = @intCast(id), .row = @intCast(row), .rgb = rgb }) catch {} + else + s.fill(rail.x + 1, rail.y + @as(u16, @intCast(row)), 1, 1, .{ .bg = .{ .rgb = rgb } }); + } +} + +fn paintLanes(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { const ch = answered(p, pane.serial) orelse return; if (ch.lines.len == 0) return; var buf: [8][3]u8 = undefined; @@ -1702,7 +2016,7 @@ const WorkerJj = struct { /// answer the session's input, as its loop hands it, and the client's. fn land(w: *WorkerJj) !void { var waited: u32 = 0; - while (w.a.core.changes.task != null or w.a.core.changes.next != null) : (waited += 1) { + while (w.a.core.changes.task != null or w.a.core.changes.next != null or w.a.core.stream.parse != null) : (waited += 1) { if (waited == 5000) return error.Timeout; var batch: Inbox.Batch = .{}; w.shell.q.takeInto(&batch); @@ -2238,6 +2552,127 @@ test "the lanes ride with the text: a line typed in a nested block is nobody's, try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); } +/// Two functions four changes wrote: `fn` both, `f2` and `f3` rewrote +/// f's line, `g2` g's: f three changes, g two. +const Churned = struct { + const fn_text = "fn f() void {\n a();\n}\n\nfn g() void {\n b();\n}\n"; + const f2_text = "fn f() void {\n a2();\n}\n\nfn g() void {\n b();\n}\n"; + const f3_text = "fn f() void {\n a3();\n}\n\nfn g() void {\n b();\n}\n"; + const g2_text = "fn f() void {\n a3();\n}\n\nfn g() void {\n b2();\n}\n"; + + fn repo() !?Repo { + return Repo.with(testing.allocator, &.{ .{ fn_text, "fn" }, .{ f2_text, "f2" }, .{ f3_text, "f3" }, .{ g2_text, "g2" } }, null); + } +}; + +test "Churn counts the changes that touched each declaration and draws them after the line number and by the scroll thumb, grey to red on a log scale; bare again, off" { + const gpa = testing.allocator; + var repo = try Churned.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + for ([_]bool{ false, true }) |windowed| { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 24, .windowed = windowed }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + const id = p.view().active; + const pane = p.panes[id].?; + const before = File.gutterWidth(pane); + p.update(.{ .command = "Churn" }); + const heat = churned(p, pane.serial).?; + try testing.expectEqualSlices(u16, &.{ 3, 3, 3, 0, 2, 2, 2 }, heat.lines); + try testing.expectEqual(@as(u16, 3), heat.hottest); + try testing.expectEqual(before + 1, File.gutterWidth(pane)); + // Drawn: a cell after the number, f's the theme's red, g's along + // the way from the numbers' grey at ln 3 / ln 4, none between. + 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 x = body.x + File.gutterWidth(pane) - 1; + const red = themeRed(p.theme()); + const warm = pardes.colors.mixOklab(p.theme().lineno, red, @log(3.0) / @log(4.0)); + for (0..7) |row| { + const cell = s.at(x, body.y + @as(u16, @intCast(row))); + if (row == 3) { + try testing.expectEqualStrings(" ", cell.grapheme()); + continue; + } + try testing.expectEqualStrings("\u{258c}", cell.grapheme()); + try testing.expectEqual(pardes.Color{ .rgb = if (row < 3) red else warm }, cell.style.fg); + } + // By the scroll thumb: the rail's first row holds f's lines. + const rail = s.region(.rail, id).?.rect; + if (windowed) { + const strip = for (s.heat.items) |h| { + if (h.owner == id and h.row == 0) break h; + } else return error.TestExpectedEqual; + try testing.expectEqual(red, strip.rgb); + } else try testing.expectEqual(pardes.Color{ .rgb = red }, s.at(rail.x + 1, rail.y).style.bg); + // Bare again: hidden, the gutter as it was; again: shown, jj not asked. + p.update(.{ .command = "Churn" }); + try testing.expectEqual(before, File.gutterWidth(pane)); + p.update(.{ .command = "Churn" }); + try testing.expectEqual(before + 1, File.gutterWidth(pane)); + try testing.expectEqual(heat, churned(p, pane.serial).?); + } +} + +test "Churn's window: a revset, or a span of time back from now; jj's refusal said" { + const gpa = testing.allocator; + var repo = try Churned.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 24, .windowed = false }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + const id = p.view().active; + const pane = p.panes[id].?; + // f3 and g2 alone: one each. + p.update(.{ .command = "Churn @--::@" }); + try testing.expectEqualSlices(u16, &.{ 1, 1, 1, 0, 1, 1, 1 }, churned(p, pane.serial).?.lines); + // The last week: all four, just made. + p.update(.{ .command = "Churn 1w" }); + try testing.expectEqualSlices(u16, &.{ 3, 3, 3, 0, 2, 2, 2 }, churned(p, pane.serial).?.lines); + try testing.expectEqualStrings("::(@) & author_date(after:\"1 weeks ago\")", churned(p, pane.serial).?.window()); + p.update(.{ .command = "Churn nosuch" }); + try testing.expect(std.mem.indexOf(u8, told(p, id), "Churn: Revision `nosuch` doesn't exist") != null); +} + +test "Churn on a worker: jj's answer kept raw, counted when the worker's parse of the text lands; the client equal" { + var repo = try Churned.repo() orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + var w = try WorkerJj.init(&repo, ""); + defer w.deinit(); + try w.word("Churn"); + try w.land(); + try testing.expectEqualSlices(u16, &.{ 3, 3, 3, 0, 2, 2, 2 }, churned(w.a.core, w.serial).?.lines); + try testing.expectEqualSlices(u16, churned(w.a.core, w.serial).?.lines, churned(w.r.core(), w.serial).?.lines); +} + +test "Churn's counts ride with the text, and a client replays them equal" { + var repo = try Churned.repo() orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + const R = pardes.replica; + const a = try R.Authority.init(testing.allocator, .{ .cols = 80, .rows = 24 }, .{}); + defer a.deinit(); + const r = try R.Replica.init(testing.allocator, 1, try a.unit(0, 0)); + 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 = "Churn" })); + r.settle(); + const serial = a.core.panes[a.core.view().active].?.serial; + // `a3();` deleted: its value goes with it. + r.confirm(try a.local(.{ .command = "Look a.zig:2" })); + for (";xd") |k| r.confirm(try a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } })); + r.settle(); + try testing.expectEqualSlices(u16, &.{ 3, 3, 0, 2, 2, 2 }, churned(a.core, serial).?.lines); + try testing.expect(r.diverged == null); + try testing.expectEqualSlices(u16, churned(a.core, serial).?.lines, churned(r.core(), serial).?.lines); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); +} + test "a B3 click on path@rev:line in running text opens the file at that revision there" { const gpa = testing.allocator; var repo = try Repo.init(gpa) orelse return error.SkipZigTest; diff --git a/core/regexp.zig b/core/regexp.zig index 2936b491..4a51d0ce 100644 --- a/core/regexp.zig +++ b/core/regexp.zig @@ -681,3 +681,48 @@ test "a pattern that stops inside a count or an escape is refused, not read past } } } + +test "what mvzr compiles it can walk: an empty group matches the empty string; a repeat of `|` or `^`, and a count without its `}`, are refused" { + std.testing.log_level = .err; // mvzr warns of each bad string + // An empty group with more after it, searched to a line's end, where + // mvzr asks whether the group may match nothing (build/patches.zig). + const Case = struct { pat: []const u8, text: []const u8, hit: ?[2]usize }; + for ([_]Case{ + .{ .pat = "()a", .text = "b", .hit = null }, + .{ .pat = "()a", .text = "ba", .hit = .{ 1, 2 } }, + .{ .pat = "a()b", .text = "a", .hit = null }, + .{ .pat = "a()b", .text = "xab", .hit = .{ 1, 3 } }, + }) |c| { + var rx = try Regex.compile(c.pat); + const m = try rx.find(c.text, 0, c.text.len, c.text.len); + try std.testing.expectEqual(c.hit, if (m) |h| [2]usize{ h.start, h.end } else null); + } + // A repeat right after `|` or `^` took it for what it repeats, and a + // count ending in neither `,` nor `}` left a hole in the program: each + // walked off an empty slice in the matcher. + for ([_][]const u8{ "a|*", "|+", "(|?)", "|??", "^*", "^+?", "a|{2}", "({1))", "a{1b" }) |pat| + try std.testing.expectError(error.Bad, Regex.compile(pat)); + // Every pattern up to five characters over the first alphabet and four + // over the second, searched from every offset of a text with an empty + // line: refused, or searched to an answer inside the budget. + const text = "aa\n\na"; + var pat: [5]u8 = undefined; + for ([_][]const u8{ "()|*+?a", "()|*+?a^{1}" }, [_]usize{ 5, 4 }) |alphabet, longest| { + for (1..longest + 1) |len| { + var n: usize = 1; + for (0..len) |_| n *= alphabet.len; + for (0..n) |k| { + var rest = k; + for (pat[0..len]) |*c| { + c.* = alphabet[rest % alphabet.len]; + rest /= alphabet.len; + } + var rx = Regex.compile(pat[0..len]) catch continue; + for (0..text.len + 1) |from| { + const m = try rx.find(text, from, text.len, text.len) orelse continue; + try std.testing.expect(from <= m.start and m.start <= m.end and m.end <= text.len); + } + } + } + } +} diff --git a/core/replica.zig b/core/replica.zig index 6072eea4..f4a3284c 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1348,6 +1348,12 @@ pub const Peer = struct { again: bool = false, /// ...or that its window was closed (Peers.closed). close: bool = false, + /// It said a bye: none, and the session let it go with no word of its + /// own (`closing`, `cut`), is a client that died (killed: P48). + bye: bool = false, + /// A pair's first client (`Joining.own`): the terminal its session + /// keeps is its (`Peers.pair_left`). + pair: bool = false, /// Inputs read (each one's seq, as the client numbered them). seq: u32 = 0, /// The `stamp` frame its next input came with (its reader's). @@ -1630,6 +1636,7 @@ pub const Peer = struct { const b = (wire.decodeClient(tag, payload) catch return false).bye; peer.again = b.again; peer.close = b.close; + peer.bye = true; return false; }, .ping => { @@ -1762,6 +1769,14 @@ pub const Peers = struct { /// hung up) and the session is a pair's not kept (daemon.sessionState): it /// ends there (runtime/session.zig). closed: bool = false, + /// Its pair's first client left (P48): with no word at all (killed), + /// true; its session's frontend puts the user's terminal back then, + /// and lets it go either way (tui.zig `Own.sent`), and clears this. + pair_left: ?bool = null, + /// Its pair's first client's terminal or window found again (a focus + /// in): the session asks whether a newer build is installed, as its own + /// frontend asks at its own (runtime/session.zig), and clears this. + pair_focused: bool = false, /// An Upgrade told its clients so (`refuseAll`): a connection handed /// over before its exec is told the same, never closed by the exec /// unanswered. Its exec failed: the next boundary takes joins again. @@ -1883,6 +1898,7 @@ pub const Peers = struct { const t0 = nowNs(); defer if (ps.samples) ps.input_ns.append(ps.gpa, nowNs() - t0) catch {}; core.toView(peer.view); + if (peer.pair and in.event == .focus and in.event.focus) ps.pair_focused = true; ps.t.stamp = stampOf(in); core.update(in.event); core.step(); @@ -1987,7 +2003,7 @@ pub const Peers = struct { core.gpa.free(bytes); return daemon.refuseReplica(fd, "out of memory"); }; - peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = .at(k), .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = .at(k), .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa, .pair = joining.own }; ps.next_id +%= 1; if (ps.next_id == 0) ps.next_id = 1; // The session's digest as dumped, for the joiner to check its load by @@ -2062,6 +2078,7 @@ pub const Peers = struct { ps.closeViewOf(core, peer, peer.again or peer.cut); if (ps.count() == 0) core.unattended = ps.detached; if (ps.count() == 0 and peer.close and daemon.sessionState() == .pair) ps.closed = true; + if (peer.pair) ps.pair_left = !peer.bye and !peer.closing and !peer.cut; ps.seq += 1; ps.unit(0, ps.seq); ps.letGo(peer); @@ -6327,12 +6344,10 @@ const may_wait = [_]Wait{ .{ .what = "*", .on = .addon, .stop = "a kind's close", .why = "a kind's pane closed: its state let go on the session" }, .{ .what = "*", .on = .addon, .stop = "a kind's word", .why = "a word to a kind (a key it takes as one): its own state" }, .{ .what = "*", .on = .addon, .stop = "a kind's open", .why = "a kind's pane opened (`Life`): its state made on the session" }, -} ++ (if (pardes.font_picker) [_]Wait{ - // A window's view (the font words are a window's). +} ++ (if (pardes.builtins.capabilities.font_picker) [_]Wait{ + // A window's view's words, which a terminal's build has too: a window + // client of its session runs them. .{ .what = "Font *", .on = .any, .stop = "fonts", .why = "the session's machine's fonts, its disk" }, -} else [_]Wait{}) ++ (if (pardes.builtins.capabilities.font_picker) [_]Wait{ - // ...and Fonts a terminal's build has too, which a window client of its - // session runs (exec.zig's Font, a window build's alone, does not). .{ .what = "Fonts", .on = .any, .stop = "fonts", .why = "the session's machine's fonts, its disk" }, } else [_]Wait{}); diff --git a/core/root.zig b/core/root.zig index 99fb4845..3bca5e03 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 RailHeat = @import("surface.zig").Heat; pub const Lane = @import("surface.zig").Lane; pub const MAX_REGIONS = @import("surface.zig").MAX_REGIONS; /// What comes in (Event.zig). @@ -117,7 +118,9 @@ const Loc = Jump.Loc; pub const can_attach = platform == .tui or platform == .gui; -pub const fonts = if (font_picker) @import("dev").fonts else struct {}; +/// The machine's fonts: every desktop build's, which steps a window's Font +/// as the window does (builtins.capabilities), and the Mac's. +pub const fonts = if (builtins.capabilities.font_picker) @import("dev").fonts else struct {}; pub const Pane = panes.Pane; const Text = panes.Text; diff --git a/core/surface.zig b/core/surface.zig index 653abd82..3ab9a933 100644 --- a/core/surface.zig +++ b/core/surface.zig @@ -151,6 +151,9 @@ pub const max_rail_ticks = 4096; /// 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 }; +/// Churn's strip on a rail (jj.railHeat): a pixel shell draws its row of +/// pane `owner`'s rail, beside the track, in `rgb`. +pub const Heat = struct { owner: u8, row: u16, rgb: [3]u8 }; pub const rail_tick_scale = 1024; pub const Chrome = struct { @@ -266,6 +269,9 @@ pub const Surface = struct { /// makes many a temporary Surface, and a safe build fills an array's /// `undefined` in each). lanes: std.ArrayList(Lane) = .empty, + /// Churn's strips by the scroll thumbs, for a pixel shell (in a + /// terminal the rail's cells), as `lanes`. + heat: std.ArrayList(Heat) = .empty, chrome: Chrome = .{}, cells: []Cell = &.{}, /// bar: draw an insert-style thin cursor instead of the block diff --git a/core/tape.zig b/core/tape.zig index 1cbf8aed..dc53a7c9 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -140,6 +140,9 @@ pub const Site = enum(u16) { /// A kitty image's medium as a terminal's emulator read it (a file, a /// temporary file, shared memory: Term.media), its bytes or its failure. medium, + /// An addon's library loaded or let go (Addon.perform): what it brought + /// is the `addon` event inside it, which a replica applies from here. + landing, }; /// What the sites return. wire.semantics hashes their shape with the |
