diff options
Diffstat (limited to 'core/jj.zig')
| -rw-r--r-- | core/jj.zig | 1065 |
1 files changed, 800 insertions, 265 deletions
diff --git a/core/jj.zig b/core/jj.zig index 1ec1a0dc..18926594 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -203,10 +203,10 @@ pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look /// What jj answered for a pane's `Changes`: replicated state, the /// session's (`Pardes.changes`, by the pane's serial: a client never runs -/// jj; one that joins late has it in the dump), its revset with it, drawn -/// by each view that shows them (File.Layers.lanes: a view holds that, not -/// the revset; one set a pane, so a view's `Changes other` replaces what -/// every view shows). From two jj runs off the loop (`Ask`): the changes +/// jj; one that joins late has it in the dump), its revset with it. Shown, +/// its ghosts are lines of the pane's buffer, read-only (File.read_only, +/// `showLayer`), so the layer is the pane's: every view of it shows it or +/// none does. From two jj runs off the loop (`Ask`): 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`). @@ -222,10 +222,11 @@ pub const Changes = struct { lines: []Line = &.{}, /// Each pin's hunks in the file, a pin's together, each in place order. hunks: []Hunk = &.{}, - /// The ghost rows' text: the lines the hunks took away, hunk after - /// hunk, each with its `\n`; then each pin's description row. + /// The ghosts' text: the lines the hunks took away, hunk after hunk, + /// each with its `\n`; then each pin's description. ghost_text: []u8 = &.{}, - /// The rows a view shows that the text has not, in `at` order. + /// The lines the layer puts in the buffer, the text has not, in `at` + /// order (shown, the buffer's read-only lines in this order). ghosts: []Ghost = &.{}, /// The words a changed line changed: byte ranges of a line (`Line`), or /// of `ghost_text` (`Ghost`), each `.{ from, to }`. @@ -260,8 +261,13 @@ pub fn churned(p: *Pardes, serial: u32) ?*Heat { return null; } -/// Pane `serial`'s answers let go (the pane's end). +/// Pane `serial`'s answers let go (the pane's end); a twin showing its +/// layer, its lines out. pub fn forget(p: *Pardes, serial: u32) void { + for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| if (f.layer == serial and pane.serial != serial) { + hideLayer(p, f); + setLanes(p, f, false); + }; for (p.changes.answers.items, 0..) |*c, i| if (c.serial == serial) { c.deinit(p.gpa); _ = p.changes.answers.swapRemove(i); @@ -366,15 +372,15 @@ pub const Hunk = struct { pub const carrier = std.math.maxInt(u16); }; -/// A row a view shows that is not a line of the text (PARDES.md "Changes on -/// the source view": a ghost row): a line a pin took away whole, drawn where -/// it stood, red across; or a pin's description (`id title author age`), -/// after its last line, its lane an arrow to it. No number, the cursor -/// steps over it, search and selection do not see it; a Look on it looks at -/// its word as anywhere, on a description's id its change's patch -/// (`lookGhost`). A line a pin CHANGED -/// is a ghost row too: its old text right above the line, the words that -/// went in red, the line with the words that came in green. +/// A line the layer puts in the buffer that the file has not (PARDES.md +/// "Changes on the source view": a ghost line): a line a pin took away +/// whole, where it stood, red across; or a pin's description (`id title +/// author age`, whole), after its last line, its lane an arrow to it. A +/// line a pin CHANGED has one too: its old text right above the line, the +/// words that went in red, the line with the words that came in green. +/// Shown, it is regular text, read-only (File.ReadOnly): no number, the +/// cursor goes onto it, a search finds in it, a Look looks at its word, on +/// a description's id its change's patch (`lookGhost`). pub const Ghost = struct { /// The line of the shown text it stands before (`lines.len`: after the /// last). It rides with the text as the lines' values do (`splice`). @@ -390,6 +396,8 @@ pub const Ghost = struct { /// A changed line's: the words that went (`Changes.marks`). marks: u32 = 0, nmarks: u16 = 0, + /// A description's: where its author starts in its text. + author: u32 = 0, /// A line taken away whole; a line's old text, the line that took its /// place right below it; a pin's description. @@ -397,23 +405,24 @@ pub const Ghost = struct { }; /// The `Changes` word (builtins.zig registers it): in a file's pane (or a -/// revision's), the changes `revset` names pinned on the pane and shown in -/// this view; another revset replaces them (in every view that shows -/// them). Bare: shown in this view or not, the pane's pinned ones (none -/// yet: `@`, the change against its parent). What jj refuses is said in -/// its words. +/// revision's), the changes `revset` names pinned on the pane and shown, +/// in every view of it (its lines are its buffer's); another revset +/// replaces them. Bare: shown or not, the pane's pinned ones (none yet: +/// `@`, the change against its parent). 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.lanes > 0) { - pane.layers.lanes = 0; + hideLayer(p, f); + setLanes(p, f, false); return; } - const cells: u8 = if (p.view().frontend.windowed) 2 else levels; if (arg.len == 0 and answered(p, pane.serial) != null) { - pane.layers.lanes = cells; + setLanes(p, f, true); + showLayer(p, f, pane.serial); return; } const revset = if (arg.len == 0) "@" else arg; @@ -424,10 +433,26 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { .{ .path = f.path, .rev = "@" } else return p.reportFailure(c.id, "Changes: only a file's pane takes it"); - pane.layers.lanes = cells; + setLanes(p, f, true); ask(p, .changes, c.id, pane.serial, shown.path, shown.rev, revset); } +/// The layer shown in every view of every pane on `f`'s buffer, or in +/// none: its lines are the buffer's (File.read_only). A view's lanes take +/// cells by its frontend (File.Layers.lanes). +fn setLanes(p: *Pardes, f: *const File.State, on: bool) void { + for (p.panes) |slot| { + const pane = slot orelse continue; + const tf = if (pane.file) |*file| file else continue; + if (tf.content.ptr != f.content.ptr) continue; + for (0..pardes.views.max) |k| { + const v = p.views[k] orelse continue; + const cells: u8 = if (!on) 0 else if (v.frontend.windowed) 2 else levels; + if (k == p.live) pane.layers.lanes = cells else pane.pv[k].layers.lanes = cells; + } + } +} + /// 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 @@ -726,6 +751,13 @@ pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch) void { /// lines, never asking jj again: the lines a splice takes out take their /// values with them, and a line it puts in is nobody's. pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: []const u8) void { + // The values are the file's lines': the layer's lines before the edit + // are none of them. + var shown_before: usize = 0; + for (f.read_only) |r| { + if (r.at >= off) break; + shown_before += 1; + } var i = p.changes.answers.items.len; while (i > 0) { i -= 1; @@ -733,11 +765,14 @@ pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: [ const id = p.paneBySerial(c.serial) orelse continue; const shown = if (p.panes[id].?.file) |*shown| shown else continue; if (shown.content.ptr != f.content.ptr) continue; - splice(p.gpa, c, f.content, off, len, bytes) catch { - // No room for it: its values would lie, so it goes. + // The layer shown: its ghosts stand where its lines are (`placed`). + const placed_by_lines = c.serial == f.layer; + splice(p.gpa, c, f.content, off, len, bytes, shown_before, placed_by_lines) catch { + // No room for it: its values would lie, so it goes; the one + // shown stays (its lines are the buffer's, its places theirs). + if (placed_by_lines) continue; c.deinit(p.gpa); _ = p.changes.answers.swapRemove(i); - showGhosts(p, p.panes[id].?); }; } i = p.changes.heats.items.len; @@ -747,7 +782,7 @@ pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: [ 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 { + _ = spliceValues(u16, p.gpa, &h.lines, 0, f.content, off, len, bytes, shown_before) catch { h.deinit(p.gpa); _ = p.changes.heats.swapRemove(i); }; @@ -760,11 +795,12 @@ pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: [ /// line's text after what it puts in: the lines it takes and puts come /// 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 cut = try spliceValues(Line, gpa, &c.lines, .{ .pin = .none }, text, off, len, bytes) orelse return; +fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len: usize, bytes: []const u8, shown_before: usize, placed_by_lines: bool) error{OutOfMemory}!void { + const cut = try spliceValues(Line, gpa, &c.lines, .{ .pin = .none }, text, off, len, bytes, shown_before) orelse return; const from = cut.from; const take = cut.take; const put = cut.put; + if (placed_by_lines) return; // 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| { @@ -774,14 +810,16 @@ 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 } { +/// `none`; null when no line is taken or put. `text` is the buffer, whose +/// `shown_before` lines before the edit are the layer's (none of the +/// file's: File.read_only). +fn spliceValues(comptime T: type, gpa: std.mem.Allocator, values: *[]T, none: T, text: []const u8, off: usize, len: usize, bytes: []const u8, shown_before: usize) 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); + const from = std.mem.count(u8, text[0..line_start], "\n") - shown_before + @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); @@ -800,11 +838,11 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con var line: pardes.Kind.Buf(512) = .{}; const f = if (pane.file) |*file| file else return; // Nothing pinned yet: no gutter for it. - defer if (answered(p, pane.serial) == null) { - pane.layers.lanes = 0; - }; + defer if (answered(p, pane.serial) == null) setLanes(p, f, false); if (b.end == .failed) return p.reportFailure(id, line.print("Changes: {s}", .{b.rows[0..@min(b.rows.len, 480)]})); - var got = parse(p.gpa, p.scratch.allocator(), b.rows, b.styles, f.content) catch |err| return p.reportFailure(id, line.print("Changes: {s}", .{@errorName(err)})); + // jj's lines lined up with the file's (the layer's are none of it). + const text = File.fileText(f, p.scratch.allocator()) catch |err| return p.reportFailure(id, line.print("Changes: {s}", .{@errorName(err)})); + var got = parse(p.gpa, p.scratch.allocator(), b.rows, b.styles, text) catch |err| return p.reportFailure(id, line.print("Changes: {s}", .{@errorName(err)})); switch (got) { .refused => |why| { defer p.gpa.free(why); @@ -819,6 +857,8 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con @memcpy(changes.revset[0..revset.len], revset); changes.revset_len = @intCast(revset.len); changes.serial = pane.serial; + // The lines shown go before the answer they are of. + hideLayer(p, f); if (answered(p, pane.serial)) |kept| { kept.deinit(p.gpa); kept.* = changes.*; @@ -827,7 +867,7 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con return p.reportFailure(id, "Changes: out of memory"); }; p.setStatus(.at(id), ""); - showGhosts(p, pane); + if (pane.layers.lanes > 0) showLayer(p, f, pane.serial); }, } } @@ -876,7 +916,8 @@ pub fn parsed(p: *Pardes, serial: u32) void { 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)})); + const text = File.fileText(f, p.scratch.allocator()) catch |err| return p.reportFailure(id, line.print("Churn: {s}", .{@errorName(err)})); + const lines = heatOf(p.gpa, p.scratch.allocator(), h.pending[0..h.pending_was], h.pending[h.pending_was..], text, 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; @@ -944,17 +985,92 @@ fn heatOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, 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). -pub fn showGhosts(p: *Pardes, pane: *Pane) void { - const ch = answered(p, pane.serial); - const ghosts: []const Ghost = if (ch) |c| c.ghosts else &.{}; - const text: []const u8 = if (ch) |c| c.ghost_text else ""; - if (ghosts.ptr == pane.ghosts.ptr and ghosts.len == pane.ghosts.len and text.ptr == pane.ghost_text.ptr) return; - pane.ghosts = ghosts; - pane.ghost_text = text; - pane.geometry_key = 0; +/// The layer of pane `owner` (its `Changes` answer's ghosts, in order) +/// put in `f`'s buffer as its read-only lines (File.ReadOnly), each where +/// it stands: before its file line, a line of its own (PARDES.md "Changes +/// on the source view"). One already there goes first. Every view keeps +/// its place in the file (File.relayer). +pub fn showLayer(p: *Pardes, f: *File.State, owner: u32) void { + const ch = answered(p, owner) orelse return hideLayer(p, f); + if (f.layer != 0) hideLayer(p, f); + // Not out (no memory): nothing more. + if (f.layer != 0) return; + // The file's text, the layer out. + const text = f.content; + var size = text.len; + for (ch.ghosts) |g| size += g.len + 1; + const new = pardes.memory.unfilled(p.gpa, size) catch return; + const spans = p.gpa.alloc(File.ReadOnly, ch.ghosts.len) catch { + pardes.memory.freeRaw(p.gpa, new); + return; + }; + const lines = lineCount(text); + // `text` copied up to `from`; line `line` starts at `line_at`; `n` + // bytes of `new` written. + var from: usize = 0; + var line: usize = 0; + var line_at: usize = 0; + var n: usize = 0; + for (ch.ghosts, spans) |g, *r| { + const at = @min(g.at, lines); + while (line < at) : (line += 1) line_at = if (std.mem.indexOfScalarPos(u8, text, line_at, '\n')) |nl| nl + 1 else text.len; + @memcpy(new[n..][0 .. line_at - from], text[from..line_at]); + n += line_at - from; + from = line_at; + const said = ch.ghost_text[g.text..][0..g.len]; + // After a last line with no `\n`, its `\n` first. + const lead = line_at == text.len and text.len > 0 and text[text.len - 1] != '\n'; + r.* = .{ .at = n, .len = g.len + 1, .line = @intCast(at), .lead = lead }; + if (lead) { + new[n] = '\n'; + @memcpy(new[n + 1 ..][0..g.len], said); + } else { + @memcpy(new[n..][0..g.len], said); + new[n + g.len] = '\n'; + } + n += g.len + 1; + } + @memcpy(new[n..][0 .. text.len - from], text[from..]); + std.debug.assert(n + text.len - from == size); + if (File.relayer(p, f, new, spans, owner)) return; + pardes.memory.freeRaw(p.gpa, new); + p.gpa.free(spans); +} + +/// The layer's lines out of `f`'s buffer, which is the file's text again; +/// every view keeps its place in the file: one in a line that leaves goes +/// to the start of the file line it stood before (File.relayer). +pub fn hideLayer(p: *Pardes, f: *File.State) void { + if (f.layer == 0) return; + const text = File.fileText(f, p.scratch.allocator()) catch return; + const new = pardes.memory.dupeUnfilled(p.gpa, text) catch return; + if (!File.relayer(p, f, new, &.{}, 0)) pardes.memory.freeRaw(p.gpa, new); +} + +/// A session loaded from a dump that kept a layer shown as a view's bit +/// and no lines (wire 31 and older: the ghost rows were drawn, not the +/// buffer's): each pane whose views showed it, its lines in its buffer +/// and shown in every view (File.read_only), its places kept in the file. +pub fn loaded(p: *Pardes) void { + for (p.panes) |slot| { + const pane = slot orelse continue; + const f = if (pane.file) |*file| file else continue; + if (f.layer != 0 or answered(p, pane.serial) == null) continue; + const shown = pane.layers.lanes > 0 or for (0..pardes.views.max) |k| { + if (k != p.live and p.views[k] != null and pane.pv[k].layers.lanes > 0) break true; + } else false; + if (!shown) continue; + setLanes(p, f, true); + showLayer(p, f, pane.serial); + } +} + +/// After an edit of `f`'s buffer (File.edit): the shown layer's ghosts +/// stand before the file lines their lines stand before. +pub fn placed(p: *Pardes, f: *const File.State) void { + const ch = answered(p, f.layer) orelse return; + if (ch.ghosts.len != f.read_only.len) return; + for (ch.ghosts, f.read_only) |*g, r| g.at = r.line; } const Read = union(enum) { refused: []u8, read: Changes }; @@ -969,6 +1085,9 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a defer if (!kept) out.deinit(gpa); var pins: std.ArrayList(Pin) = .empty; defer pins.deinit(gpa); + // Each pin's whole author and title, for its description (`describe`): + // never cut. + var whole: std.ArrayList([2][]const u8) = .empty; // Each change jj answered, oldest first: its pin (or `Hunk.carrier`) // and where its diff starts in `logged`; then the end. const Step = struct { pin: u16, diff: usize }; @@ -978,6 +1097,7 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a if (e.pinned and pins.items.len == pins_max) return .{ .refused = try std.fmt.allocPrint(gpa, "more than {d} changes touch it", .{pins_max}) }; if (steps.items.len == Hunk.carrier) return .{ .refused = try std.fmt.allocPrint(gpa, "more than {d} changes between them touch it", .{Hunk.carrier}) }; if (e.pinned) try pins.append(gpa, e.pin); + if (e.pinned) try whole.append(arena, .{ e.author, e.title }); try steps.append(arena, .{ .pin = if (e.pinned) @intCast(pins.items.len - 1) else Hunk.carrier, .diff = it.index orelse logged.len }); }; try steps.append(arena, .{ .pin = Hunk.carrier, .diff = logged.len }); @@ -999,7 +1119,7 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a for (steps.items[0 .. steps.items.len - 1], steps.items[1..], 0..) |s, next, i| try hunksOf(gpa, arena, s.pin, @intCast(i), logged[s.diff..next.diff], text, out.lines, &hunks, &removed, &marks, &taken); out.hunks = try hunks.toOwnedSlice(gpa); nest(out.lines, out.pins.len); - out.ghosts = try ghostsOf(gpa, arena, out.pins, out.lines, out.hunks, taken.items, &removed, now, moved); + out.ghosts = try ghostsOf(gpa, arena, out.pins, whole.items, out.lines, out.hunks, taken.items, &removed, now, moved); out.ghost_text = try removed.toOwnedSlice(gpa); out.marks = try marks.toOwnedSlice(gpa); kept = true; @@ -1024,7 +1144,7 @@ fn logTemplate(gpa: std.mem.Allocator, revset: []const u8) error{OutOfMemory}![] /// A `logTemplate` line, or null for a line of a diff (which starts with /// a blank, a sign, `@`, `\\` or a word and a blank: never eight of jj's /// id letters, k to z, and a tab). -fn entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool } { +fn entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool, author: []const u8, title: []const u8 } { if (l.len < 11 or l[8] != '\t' or l[10] != '\t') return null; for (l[0..8]) |c| if (c < 'k' or c > 'z') return null; var cols = std.mem.splitScalar(u8, l[11..], '\t'); @@ -1035,7 +1155,7 @@ fn entryOf(l: []const u8) ?struct { pin: Pin, pinned: bool } { const title = cols.rest(); pin.title_len = @intCast(@min(title.len, pin.title.len)); @memcpy(pin.title[0..pin.title_len], title[0..pin.title_len]); - return .{ .pin = pin, .pinned = l[9] == 'p' }; + return .{ .pin = pin, .pinned = l[9] == 'p', .author = author, .title = title }; } /// Which pin wrote each line of `text` (what the pane shows: `whole` is the @@ -1265,7 +1385,7 @@ const max_run = 1024; /// inside lines rewritten since jj's snapshot (`Moved.place`) has no row. /// `text` holds the hunks' removed lines; the descriptions' rows go on /// after them. -fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, lines: []const Line, hunks: []const Hunk, taken: []const Taken, text: *std.ArrayList(u8), now: i64, moved: Moved) error{OutOfMemory}![]Ghost { +fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, whole: []const [2][]const u8, lines: []const Line, hunks: []const Hunk, taken: []const Taken, text: *std.ArrayList(u8), now: i64, moved: Moved) error{OutOfMemory}![]Ghost { const Made = struct { g: Ghost, rank: u64 }; var made: std.ArrayList(Made) = .empty; var ti: usize = 0; @@ -1325,9 +1445,9 @@ fn ghostsOf(gpa: std.mem.Allocator, arena: std.mem.Allocator, pins: []const Pin, rank = m.rank | 1; }; const at_text = text.items.len; - try describe(gpa, text, pin, now); + const author = try describe(gpa, text, pin, whole[k][0], whole[k][1], now); try made.append(arena, .{ - .g = .{ .at = place, .pin = @intCast(k), .kind = .description, .line = @max(first, 1), .text = @intCast(at_text), .len = @intCast(text.items.len - at_text) }, + .g = .{ .at = place, .pin = @intCast(k), .kind = .description, .line = @max(first, 1), .text = @intCast(at_text), .len = @intCast(text.items.len - at_text), .author = @intCast(author - at_text) }, .rank = rank, }); } @@ -1367,46 +1487,33 @@ fn carried(hunks: []const Hunk, from: u16, after: u32, moved: Moved) ?u32 { return moved.place(@intCast(std.math.clamp(pos, 0, @as(i64, moved.was)))); } -/// A pin's description row (PARDES.md: compact, one blank between): its -/// id's first four letters, its title, its author's first name, its age -/// short (`5h`, `2d`, `3w`, `4m`, `1y`; under an hour `now`), no "ago". -fn describe(gpa: std.mem.Allocator, text: *std.ArrayList(u8), pin: Pin, now: i64) error{OutOfMemory}!void { - const author = pin.author[0..pin.author_len]; - try text.print(gpa, "{s} {s} {s} ", .{ pin.id[0..4], pin.title[0..pin.title_len], author[0 .. std.mem.indexOfScalar(u8, author, ' ') orelse author.len] }); +/// A pin's description line (PARDES.md: `id title author age`, one blank +/// between): its id's first four letters, its whole title (its +/// description's first line), its whole author, its age short (`5h`, `2d`, +/// `3w`, `4m`, `1y`; under an hour `now`), no "ago". Nothing is cut: a +/// line too long for its pane wraps as any line does. Where its author +/// starts in `text`. +fn describe(gpa: std.mem.Allocator, text: *std.ArrayList(u8), pin: Pin, author: []const u8, title: []const u8, now: i64) error{OutOfMemory}!usize { + try text.print(gpa, "{s} {s} ", .{ pin.id[0..4], title }); + const at = text.items.len; + try text.print(gpa, "{s} ", .{author}); const s = @max(0, now - pin.time); const day = 24 * 3600; for ([_]struct { u8, i64 }{ .{ 'y', 365 * day }, .{ 'm', 30 * day }, .{ 'w', 7 * day }, .{ 'd', day }, .{ 'h', 3600 } }) |unit| { - if (s >= unit[1]) return text.print(gpa, "{d}{c}", .{ @divFloor(s, unit[1]), unit[0] }); + if (s >= unit[1]) { + try text.print(gpa, "{d}{c}", .{ @divFloor(s, unit[1]), unit[0] }); + return at; + } } try text.appendSlice(gpa, "now"); + return at; } -/// The bytes a description row has at most, cut (`fitted`). -pub const row_max = 256; - -/// A description row (`describe`: `id title author age`) as a body row of -/// `room` cells shows it: whole when it fits, else its title cut to the -/// room left (a `…` where it is cut), so the author and the age stay at -/// its end (PARDES.md: one compact row, both shown). -pub fn fitted(tab: u16, said: []const u8, room: usize, buf: *[row_max]u8) []const u8 { - if (said.len + 3 > row_max or File.displayWidth(tab, said) <= room) return said; - var tail = said.len; - for (0..2) |_| tail = std.mem.lastIndexOfScalar(u8, said[0..tail], ' ') orelse return said; - const head = @min(5, tail); - const keep = room -| File.displayWidth(tab, said[0..head]) -| File.displayWidth(tab, said[tail..]) -| 1; - const title = if (keep == 0) "" else std.mem.trimEnd(u8, said[head..tail][0..File.fitEnd(tab, said[head..tail], 0, keep)], " "); - var out: std.ArrayList(u8) = .initBuffer(buf); - out.appendSliceAssumeCapacity(said[0..head]); - out.appendSliceAssumeCapacity(title); - out.appendSliceAssumeCapacity("\u{2026}"); - out.appendSliceAssumeCapacity(said[tail..]); - return out.items; -} - -/// The lanes on ghost row `gi` (`ghosts[gi]`): those of the line above it -/// that run on below it or whose description is this row or one after it -/// at the same place, then its own pin's. -fn ghostLanes(ch: *const Changes, ghosts: []const Ghost, gi: usize) [levels]PinIndex { +/// The lanes on ghost line `gi` (`ghosts[gi]`): those of the line above it +/// that run on below it or whose description is this line or one after it +/// at the same place, then its own pin's (on a description's rows after +/// its first, not: its lane ended in the arrow). +fn ghostLanes(ch: *const Changes, ghosts: []const Ghost, gi: usize, first_row: bool) [levels]PinIndex { const g = ghosts[gi]; var out: [levels]PinIndex = @splat(.none); var n: usize = 0; @@ -1418,43 +1525,69 @@ fn ghostLanes(ch: *const Changes, ghosts: []const Ghost, gi: usize) [levels]PinI if (later.kind == .description and later.pin == @intFromEnum(lane)) break true; } else false; if (!below and !closes) continue; + if (!first_row and g.kind == .description and @intFromEnum(lane) == g.pin) continue; out[n] = lane; n += 1; }; - if (n < levels and std.mem.indexOfScalar(PinIndex, out[0..n], @enumFromInt(g.pin)) == null) out[n] = @enumFromInt(g.pin); + const own = first_row or g.kind != .description; + if (own and n < levels and std.mem.indexOfScalar(PinIndex, out[0..n], @enumFromInt(g.pin)) == null) out[n] = @enumFromInt(g.pin); return out; } -/// A ghost row's cells: a line taken away whole red across, its text in -/// the line numbers' grey (dimmer than the text's ink); a description's id -/// and title in its lane's colour `lane`, or with one change pinned (no one -/// colour a lane) its id in the theme's keyword ink; its author and age -/// grey; `said`, the row's text as the body shows it (a description -/// `fitted`). -fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, said: []const u8, marks: []const [2]u32, lane: ?[3]u8, x: u16, y: u16, w: u16) void { +/// A ghost line's cells on one of its rows (body row `vr`, from byte +/// `from` of buffer line `row` to byte `to`): a line taken away whole red +/// across, its text in the line numbers' grey (dimmer than the text's ink); +/// a changed line's old text grey, the words that went red; a +/// description's id and title in its lane's colour `lane`, or with one +/// change pinned (no one colour a lane) its id in the theme's keyword ink, +/// its author and age grey. +fn inkGhost(p: *Pardes, s: *pardes.Surface, pane: *const Pane, g: Ghost, row: i32, from: usize, to: usize, pad: i32, marks: []const [2]u32, lane: ?[3]u8, x: u16, y: u16, w: u16) void { const th = p.theme(); + const tab = p.settings.tab; + const said = File.sourceLine(pane, row); + const Ink = struct { + q: *Pardes, + s: *pardes.Surface, + pane: *const Pane, + row: i32, + from: usize, + to: usize, + pad: i32, + x: u16, + y: u16, + w: u16, + /// The cells of bytes `lo` to `hi` on this row. + fn cells(k: @This(), lo: usize, hi: usize) [2]u16 { + const a = @max(k.from, lo); + const b = @min(k.to, hi); + if (a >= b) return .{ 0, 0 }; + const c0 = k.pad + File.displayOffset(k.q.settings.tab, k.pane, k.row, @intCast(k.from), @intCast(a)); + const c1 = k.pad + File.displayOffset(k.q.settings.tab, k.pane, k.row, @intCast(k.from), @intCast(b)); + return .{ @intCast(std.math.clamp(c0, 0, k.w)), @intCast(std.math.clamp(c1, 0, k.w)) }; + } + fn fg(k: @This(), lo: usize, hi: usize, rgb: [3]u8) void { + const c = k.cells(lo, hi); + for (c[0]..c[1]) |cell| k.s.at(k.x + @as(u16, @intCast(cell)), k.y).style.fg = .{ .rgb = rgb }; + } + }; + const ink: Ink = .{ .q = p, .s = s, .pane = pane, .row = row, .from = from, .to = to, .pad = pad, .x = x, .y = y, .w = w }; + _ = tab; switch (g.kind) { .deleted => s.tint(x, y, w, pardes.colors.Grounds.of(th).del.row, th.lineno), .changed => { - const cells = @min(w, File.displayWidth(p.settings.tab, said)); - for (0..cells) |c| s.at(x + @as(u16, @intCast(c)), y).style.fg = .{ .rgb = th.lineno }; + ink.fg(0, said.len, th.lineno); for (marks[g.marks..][0..g.nmarks]) |m| { - const from = File.displayWidth(p.settings.tab, said[0..@min(m[0], said.len)]); - const to = @min(w, File.displayWidth(p.settings.tab, said[0..@min(m[1], said.len)])); - if (from < to) s.tint(x + @as(u16, @intCast(from)), y, @intCast(to - from), pardes.colors.Grounds.of(th).del.emph, null); + const c = ink.cells(m[0], m[1]); + if (c[0] < c[1]) s.tint(x + c[0], y, c[1] - c[0], pardes.colors.Grounds.of(th).del.emph, null); } }, .description => { - // `id title author age`: the last two words grey. - var grey = said.len; - for (0..2) |_| grey = std.mem.lastIndexOfScalar(u8, said[0..grey], ' ') orelse 0; - const id_cells = if (lane != null) File.displayWidth(p.settings.tab, said) else std.mem.indexOfScalar(u8, said, ' ') orelse said.len; - const title_cells = File.displayWidth(p.settings.tab, said[0..grey]); - const cells = @min(w, File.displayWidth(p.settings.tab, said)); - for (0..cells) |c| { - if (c >= id_cells and c < title_cells) continue; - s.at(x + @as(u16, @intCast(c)), y).style.fg = .{ .rgb = if (c >= title_cells) th.lineno else lane orelse th.kw }; - } + // `id title author age`: the id (and with several pinned the + // title) in the lane's colour, the author and the age grey. + const id_end = std.mem.indexOfScalar(u8, said, ' ') orelse said.len; + const author = @min(g.author, said.len); + ink.fg(0, if (lane != null) author else id_end, lane orelse th.kw); + ink.fg(author, said.len, th.lineno); }, } } @@ -1467,7 +1600,7 @@ fn inkCame(p: *Pardes, s: *pardes.Surface, pane: *Pane, marks: []const [2]u32, r const here = pane.wrapAt(tab, vr); const next = pane.wrapAt(tab, @as(i32, vr) + 1); const first: u32 = @intCast(here.at); - const end: u32 = if (next.line == row and !next.ghost) @intCast(next.at) else len; + const end: u32 = if (next.line == row) @intCast(next.at) else len; for (marks) |m| { const lo = @max(first, @min(m[0], len)); const hi = @min(end, @min(m[1], len)); @@ -1478,74 +1611,70 @@ fn inkCame(p: *Pardes, s: *pardes.Surface, pane: *Pane, marks: []const [2]u32, r } } -/// The line `]g` (`forward`) or `[g` goes to from line `from` of pane's -/// text: the next or the previous change's first line in this view (a run -/// of lines one pin wrote, or where lines one took away stood); null with -/// none shown, or none that way. +/// The pin buffer row `row` of `f` is one of: of a ghost line, its pin's +/// (none for a description); of a file line, the pin that wrote it. +fn ownerOf(ch: *const Changes, f: *const File.State, row: usize) PinIndex { + if (File.readOnlyRow(f, row)) |k| { + if (k >= ch.ghosts.len) return .none; + const g = ch.ghosts[k]; + return if (g.kind == .description) .none else @enumFromInt(g.pin); + } + const line = File.fileRow(f, row); + return if (line < ch.lines.len) ch.lines[line].pin else .none; +} + +/// The line `]g` (`forward`) or `[g` goes to from buffer line `from` of +/// the pane: the next or the previous change's first line (the first of a +/// run of lines one pin wrote or took away: a ghost line is a line); null +/// with none shown, or none that way. pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?usize { if (pane.layers.lanes == 0) return null; - const ch = answered(p, pane.serial) orelse return null; - var best: ?usize = null; - const Best = struct { - fn take(b: *?usize, at: usize, here: usize, fwd: bool) void { - if (if (fwd) at <= here else at >= here) return; - if (b.*) |was| if (if (fwd) at >= was else at <= was) return; - b.* = at; + const f = if (pane.file) |*file| file else return null; + const ch = answered(p, f.layer) orelse return null; + const n = File.lineCount(f.content); + var row = from; + while (true) { + if (forward) { + row += 1; + if (row >= n) return null; + } else { + if (row == 0) return null; + row -= 1; } - }; - for (ch.lines, 0..) |l, i| if (l.pin != .none and (i == 0 or ch.lines[i - 1].pin != l.pin)) Best.take(&best, i, from, forward); - for (ch.ghosts) |g| if (g.kind == .deleted) Best.take(&best, @min(g.at, ch.lines.len -| 1), from, forward); - return best; + const owner = ownerOf(ch, f, row); + if (owner != .none and (row == 0 or ownerOf(ch, f, row - 1) != owner)) return row; + } } -/// A Look (B3) at column `col` of body row `vr` of pane `id` when it is a -/// ghost row (mouse.zig): a regular Look of the word there, as the same -/// text anywhere is looked at; on a description's id, its change's patch at -/// the row for the last line of its this file has (`Diff <id> <path>:<line>`, -/// what a user could type). False on any other row. +/// A Look (B3) at column `col` of body row `vr` of pane `id` on the id of +/// a change's description line (mouse.zig): its change's patch at the row +/// for the last line of it this file has (`Diff <id> <path>:<line>`, what a +/// user could type). False anywhere else: the Look is a regular one. pub fn lookGhost(p: *Pardes, id: usize, vr: i32, col: i32) bool { const pane = p.panes[id].?; - if (pane.layers.lanes == 0 or !pane.wrapAt(p.settings.tab, vr).ghost) return false; - const ch = answered(p, pane.serial) orelse return false; - const gi: usize = @intCast(-1 - pane.wrap_col[@intCast(vr)]); - if (gi >= pane.ghosts.len) return false; - const g = pane.ghosts[gi]; - const said = pane.ghost_text[g.text..][0..g.len]; - const at = File.rawAtDisplay(p.settings.tab, said, @intCast(@max(0, col - @as(i32, File.gutterWidth(pane))))); - const what = wordAt(said, at) orelse return true; - const id_end = std.mem.indexOfScalar(u8, said, ' ') orelse said.len; - if (g.kind == .description and what.ptr == said.ptr and what.len == id_end) { - const f = &pane.file.?; - const path = if (of(f)) |r| r.path else f.path; - var last: u32 = g.line; - for (ch.hunks) |h| if (h.pin == g.pin) { - last = @max(last, h.new_start + h.new_len -| 1); - }; - var line: [4200]u8 = undefined; - const arg = std.fmt.bufPrint(&line, "{s} {s}:{d}", .{ ch.pins[g.pin].id, path, last }) catch return true; - pardes.exec.runBuiltin(p, .Diff, id, "Diff", arg); - return true; - } - pardes.look.lookAt(p, id, what); + const f = if (pane.file) |*file| file else return false; + const ch = answered(p, f.layer) orelse return false; + const tab = p.settings.tab; + const w = pane.wrapAt(tab, vr); + if (w.line < 0 or w.at != 0) return false; + const gi = File.readOnlyRow(f, @intCast(w.line)) orelse return false; + if (gi >= ch.ghosts.len) return false; + const g = ch.ghosts[gi]; + if (g.kind != .description) return false; + const said = File.sourceLine(pane, w.line); + const at = File.byteAtRowDisplay(tab, pane, w.line, 0, col - @as(i32, File.gutterWidth(pane)) - w.pad); + if (at < 0 or at >= (std.mem.indexOfScalar(u8, said, ' ') orelse said.len)) return false; + const path = if (of(f)) |r| r.path else f.path; + var last: u32 = g.line; + for (ch.hunks) |h| if (h.pin == g.pin) { + last = @max(last, h.new_start + h.new_len -| 1); + }; + var line: [4200]u8 = undefined; + const arg = std.fmt.bufPrint(&line, "{s} {s}:{d}", .{ ch.pins[g.pin].id, path, last }) catch return true; + pardes.exec.runBuiltin(p, .Diff, id, "Diff", arg); return true; } -/// The word of a ghost row's text at byte `at`: a run of what a path, a -/// name, an address or an id is made of. -fn wordAt(text: []const u8, at: usize) ?[]const u8 { - const part = struct { - fn of(c: u8) bool { - return std.ascii.isAlphanumeric(c) or std.mem.indexOfScalar(u8, "_./:@-+~", c) != null or c >= 0x80; - } - }.of; - if (at >= text.len or !part(text[at])) return null; - var lo = at; - while (lo > 0 and part(text[lo - 1])) lo -= 1; - var hi = at; - while (hi < text.len and part(text[hi])) hi += 1; - return std.mem.trimEnd(u8, text[lo..hi], ".:"); -} - /// A jj change id as jj writes one: its letters, k to z. pub fn isChangeId(text: []const u8) bool { if (text.len < 4 or text.len > 32) return false; @@ -1708,10 +1837,10 @@ fn hueApart(a: [3]u8, b: [3]u8) f32 { /// The pane's pinned changes as a view shows them, on its body's rows (body_layer.zig, while /// File.Layers.lanes is set): 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; on a ghost row -/// (`Ghost`) its lanes and its cells (`inkGhost`). Not yet: a changed -/// line's words. +/// terminal, a strip of pixels in a window (Surface.lanes); a line a pin +/// added whole green across it, as a diff's added line, a changed line's +/// words that came green; on each row of a ghost line (`Ghost`) its lanes +/// and its cells (`inkGhost`). 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); @@ -1733,18 +1862,20 @@ fn heatColour(th: *const pardes.Theme, count: u16, hottest: u16) ?[3]u8 { /// 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. +/// line's colour; none on a ghost line 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 f = if (pane.file) |*file| file else 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; + if (row < 0 or File.readOnlyRow(f, @intCast(row)) != null) continue; + const line = File.fileRow(f, @intCast(row)); + if (line >= heat.lines.len) continue; + const rgb = heatColour(p.theme(), heat.lines[line], 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); @@ -1771,8 +1902,10 @@ pub fn railHeat(p: *Pardes, s: *pardes.Surface, pane: *Pane, id: usize, rail: pa } 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; + const f = if (pane.file) |*file| file else return; + const ch = answered(p, f.layer) orelse return; if (ch.lines.len == 0) return; + const tab = p.settings.tab; var buf: [8][3]u8 = undefined; const colours = laneColours(p.theme(), &buf); if (colours.len == 0) return; @@ -1786,29 +1919,34 @@ fn paintLanes(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u 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(); + const nrows = File.lineCount(f.content); var vr: u16 = pane.context_rows; while (vr < body_h) : (vr += 1) { const y = body_y + vr; var taken = false; - // On a description row, its change's lane: an arrow to it. + // On a description's first row, its change's lane: an arrow to it. var points: PinIndex = .none; - const lanes: [levels]PinIndex = if (vr < pane.wrap_n and pane.wrap_col[vr] < 0) ghost: { - const gi: usize = @intCast(-1 - pane.wrap_col[vr]); - if (gi >= pane.ghosts.len) continue; - const g = pane.ghosts[gi]; - const said = pane.ghost_text[g.text..][0..g.len]; - var fit: [row_max]u8 = undefined; - inkGhost(p, s, g, if (g.kind == .description) fitted(p.settings.tab, said, File.bodyWidth(pane), &fit) else said, ch.marks, if (one == null) colours[g.pin % colours.len] else null, tx + gutter, y, tw - gutter); - if (g.kind == .description) points = @enumFromInt(g.pin); - taken = g.kind != .description or takenAbove(pane.ghosts, gi); - break :ghost ghostLanes(ch, pane.ghosts, gi); + const w = pane.wrapAt(tab, vr); + if (w.line < 0) continue; + const row: usize = @intCast(w.line); + if (row >= nrows) break; + const lanes: [levels]PinIndex = if (File.readOnlyRow(f, row)) |gi| ghost: { + // A ghost line, on each row it wraps to. + if (gi >= ch.ghosts.len) continue; + const g = ch.ghosts[gi]; + const next = pane.wrapAt(tab, @as(i32, vr) + 1); + const to: usize = if (next.line == w.line) @intCast(next.at) else File.sourceLine(pane, w.line).len; + const first_row = w.at <= 0; + inkGhost(p, s, pane, g, w.line, @intCast(@max(0, w.at)), to, w.pad, ch.marks, if (one == null) colours[g.pin % colours.len] else null, tx + gutter, y, tw - gutter); + if (g.kind == .description and first_row) points = @enumFromInt(g.pin); + taken = g.kind != .description or takenAbove(ch.ghosts, gi); + break :ghost ghostLanes(ch, ch.ghosts, gi, first_row); } else line: { - const row: i32 = if (pane.wrap_n == 0) scroll + @as(i32, vr) else if (vr < pane.wrap_n) pane.wrap_line[vr] else break; - if (row < 0 or row >= ch.lines.len) continue; - const line = ch.lines[@intCast(row)]; + const at = File.fileRow(f, row); + if (at >= ch.lines.len) continue; + const line = ch.lines[at]; if (line.whole and line.pin != .none) s.tint(tx + gutter, y, tw - gutter, ground, null); - if (!line.whole and line.nmarks > 0) inkCame(p, s, pane, ch.marks[line.marks..][0..line.nmarks], row, vr, tx + gutter, y, tw - gutter); + if (!line.whole and line.nmarks > 0) inkCame(p, s, pane, ch.marks[line.marks..][0..line.nmarks], w.line, vr, tx + gutter, y, tw - gutter); break :line line.lanes; }; for (lanes, 0..) |lane, level| { @@ -2049,7 +2187,7 @@ test "Changes pins a revset's changes on the pane, shown in this view: who wrote try testing.expect(std.mem.indexOf(u8, told(p, id), "Changes: no change of `@` touches a.zig") != null); } -test "the pinned changes are the pane's, shown in a view by its own bit: another view's Changes shows its set in both, and its bare Changes hides them there alone" { +test "the layer is the pane's: Changes in one view shows its lines in every view of the pane, another revset there replaces them in all, and a bare Changes in any hides them in all, the text the file's again" { const gpa = testing.allocator; var repo = try Repo.init(gpa) orelse return error.SkipZigTest; defer repo.deinit(gpa); @@ -2062,23 +2200,28 @@ test "the pinned changes are the pane's, shown in a view by its own bit: another const other = p.openView().?; p.update(.{ .command = "Changes @--::@-" }); try testing.expectEqual(before + levels, File.gutterWidth(pane)); - // A view of its own: nothing shown there until it asks. + const shown = try gpa.dupe(u8, pane.file.?.content); + defer gpa.free(shown); + try testing.expect(pane.file.?.read_only.len > 0); + // Shown in the other view too, its lines the same. p.toView(other); - try testing.expectEqual(0, pane.layers.lanes); - try testing.expectEqual(before, File.gutterWidth(pane)); + try testing.expectEqual(levels, pane.layers.lanes); + try testing.expectEqual(before + levels, File.gutterWidth(pane)); + try testing.expectEqualStrings(shown, pane.file.?.content); p.update(.{ .command = "Changes @-" }); try testing.expectEqualStrings("@-", answered(p, pane.serial).?.pinned()); try testing.expectEqual(@as(usize, 1), answered(p, pane.serial).?.pins.len); - // The first view shows the new set, not nothing. p.toView(.at(0)); try testing.expectEqual(levels, pane.layers.lanes); - // Hidden in the other view alone. + // Hidden from the other view: hidden in both, the buffer the file's. p.toView(other); p.update(.{ .command = "Changes" }); try testing.expectEqual(0, pane.layers.lanes); p.toView(.at(0)); - try testing.expectEqual(levels, pane.layers.lanes); - try testing.expectEqual(before + levels, File.gutterWidth(pane)); + try testing.expectEqual(0, pane.layers.lanes); + try testing.expectEqual(before, File.gutterWidth(pane)); + try testing.expectEqualStrings(Repo.two, pane.file.?.content); + try testing.expectEqual(@as(usize, 0), pane.file.?.read_only.len); } test "a client replays Changes from what the session's jj answered and ends equal" { @@ -2222,7 +2365,9 @@ test "Changes draws a lane a pinned change after the line number, nested ones a 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 }); + // Tall enough that every line is in view however long the test's + // directory makes the tag. + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 40, .windowed = windowed }); defer p.deinit(); var line: [4200]u8 = undefined; p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); @@ -2247,7 +2392,7 @@ test "Changes draws a lane a pinned change after the line number, nested ones a // stand between): `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(pane.wrapRow(p.settings.tab, @intCast(row), 0).row)); + const y = body.y + @as(u16, @intCast(pane.wrapRow(p.settings.tab, @intCast(File.bufferRow(&pane.file.?, row)), 0).row)); if (windowed) { var n: usize = 0; for (s.lanes.items) |lane| if (lane.y == y) { @@ -2332,7 +2477,7 @@ fn saidOf(arena: std.mem.Allocator, ch: *const Changes) ![3][]const u8 { return said; } -test "Changes draws what the text has not where it stood, as ghost rows: a line a change took away whole red across, each change's description after its last line in its lane's colour, its lane an arrow to it; no number, their lanes; one change pinned, its lane green beside what it wrote and red beside what it took away; the rows below moved down in this view alone" { +test "Changes draws what the text has not where it stood, as ghost rows: a line a change took away whole red across, each change's description after its last line in its lane's colour, its lane an arrow to it; no number, their lanes; one change pinned, its lane green beside what it wrote and red beside what it took away; the lines below moved down in every view of the pane: the layer is the pane's" { const gpa = testing.allocator; var repo = try Ghosts.repo() orelse return error.SkipZigTest; defer repo.deinit(gpa); @@ -2414,13 +2559,13 @@ test "Changes draws what the text has not where it stood, as ghost rows: a line const text_x = body.x + File.gutterWidth(pane); for ([_]u16{ 4, 5 }, [_]bool{ true, false }) |col, went| try testing.expectEqual(went, std.meta.eql(s.at(text_x + col, body.y + 2).style.bg, pardes.Color{ .rgb = grounds.del.emph })); for ([_]u16{ 4, 5, 6 }, [_]bool{ true, true, false }) |col, came| try testing.expectEqual(came, std.meta.eql(s.at(text_x + col, body.y + 3).style.bg, pardes.Color{ .rgb = grounds.add.emph })); - // The other view shows no lanes: its rows are the text's, unmoved. + // The layer is the pane's (its lines are its buffer's): the other + // view shows the same lines. p.toView(other); - const plain = try Ghosts.rows(p, pane, arena.allocator(), &buf); - var lines = std.mem.splitScalar(u8, Ghosts.mid_text, '\n'); - for (plain[0..6]) |row| try testing.expectEqualStrings(lines.next().?, Ghosts.text(pane, row)); + try testing.expect(pane.layers.lanes > 0); + const there = try Ghosts.rows(p, pane, arena.allocator(), &buf); + for (want, there[0..want.len]) |w, row| try testing.expectEqualStrings(w, Ghosts.text(pane, row)); p.toView(.at(0)); - try testing.expectEqualStrings(said[2], Ghosts.text(pane, (try Ghosts.rows(p, pane, arena.allocator(), &buf))[5])); // One change pinned, mid: a diff. Its rows `b();` (its old text), // `b2();`, `x();`, its description: the lane red beside the first, // green beside the lines, green into its arrow; its id the @@ -2449,7 +2594,7 @@ test "Changes draws what the text has not where it stood, as ghost rows: a line } } -test "the cursor steps over ghost rows and selection does not see them; ]g and [g go from change to change; a Look on one looks at its word as anywhere" { +test "]g and [g go from change to change, to each one's first line, a layer's line among them; a Look on a word of a layer's line is a Look of that word as anywhere" { const gpa = testing.allocator; var repo = try Ghosts.repo() orelse return error.SkipZigTest; defer repo.deinit(gpa); @@ -2460,51 +2605,30 @@ test "the cursor steps over ghost rows and selection does not see them; ]g and [ const id = p.view().active; const pane = p.panes[id].?; p.update(.{ .command = "Changes ::@" }); - const key = struct { - fn send(q: *Pardes, keys: []const u8) void { - for (keys) |k| q.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); - } - }.send; - // From `x();` (line 4) a `j` is on `e();` (line 5), three ghost rows below. - p.update(.{ .command = "Look a.zig:4" }); - try testing.expectEqual(@as(i32, 3), pane.body.cur_row); - key(p, "j"); - try testing.expectEqual(@as(i32, 4), pane.body.cur_row); - try testing.expectEqual(@as(i32, 9), pane.wrapRow(p.settings.tab, 4, 0).row); - // All selected: a ghost row keeps its own ground. - key(p, "%"); - p.sync(); - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); - var s = try p.render(arena.allocator()); - var body = s.region(.body, id).?.rect; - const x = body.x + File.gutterWidth(pane) + 4; - try testing.expectEqual(pardes.Color{ .rgb = pardes.colors.Grounds.of(p.theme()).del.row }, s.at(x, body.y + 6).style.bg); - try testing.expect(!std.meta.eql(s.at(x, body.y + 6).style.bg, s.at(x, body.y + 4).style.bg)); - key(p, ";"); - // ]g, [g: the changes start on lines 1 (fn), 3 (mid) and 5 (fn again, - // after the lines cut took away). - p.update(.{ .command = "Look a.zig:1" }); - for ([_]i32{ 2, 4, 4 }) |want| { - key(p, "]g"); + // The lines (Ghosts.want): fn's first two, mid's from the old text of + // the line it changed (2), cut's two lines taken away (6), fn's again + // after cut's description (9). + Lines.at(pane, 0, 0); + for ([_]i32{ 2, 6, 9, 9 }) |want| { + Lines.send(p, "]g"); try testing.expectEqual(want, pane.body.cur_row); } - for ([_]i32{ 2, 0 }) |want| { - key(p, "[g"); + for ([_]i32{ 6, 2, 0 }) |want| { + Lines.send(p, "[g"); try testing.expectEqual(want, pane.body.cur_row); } - // A Look (B3) on `c();`'s ghost row is a Look of `c`, as `Look c` - // typed in the pane: no file at a revision opens. - p.update(.{ .command = "Look a.zig:1" }); + // A Look (B3) on `c();`, a line cut took away: a Look of `c`, as + // `Look c` typed in the pane; no file at a revision opens. p.sync(); - s = try p.render(arena.allocator()); - body = s.region(.body, id).?.rect; - p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = body.x + File.gutterWidth(pane) + 4, .row = body.y + 6 } }); - p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = body.x + File.gutterWidth(pane) + 4, .row = body.y + 6 } }); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const body = s.region(.body, id).?.rect; + Lines.click(p, .right, body.x + File.gutterWidth(pane) + 4, body.y + 6); try testing.expectEqual(id, p.view().active); for (p.panes) |slot| if (slot) |q| if (q.file) |*f| try testing.expect(of(f) == null); const clicked: [2]i32 = .{ pane.body.cur_row, pane.body.cur_col }; - p.update(.{ .command = "Look a.zig:1" }); + Lines.at(pane, 0, 0); look.lookAt(p, id, "c"); try testing.expectEqual(clicked, [2]i32{ pane.body.cur_row, pane.body.cur_col }); } @@ -2605,14 +2729,14 @@ test "ghost rows ride with the text: a line deleted above moves them up, a line r.confirm(try a.local(.{ .command = "Look a.zig:4" })); for (";xd") |k| r.confirm(try a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } })); r.settle(); - try testing.expectEqualStrings("fn f() void {\n b2();\n x();\n}\n", a.core.panes[a.core.view().active].?.file.?.content); + try testing.expectEqualStrings("fn f() void {\n b2();\n x();\n}\n", try File.fileText(&a.core.panes[a.core.view().active].?.file.?, a.core.scratch.allocator())); try testing.expectEqualSlices(u32, &.{ 1, 3, 3, 3, 3, 4 }, ats(answered(a.core, serial).?, &got)); try testing.expect(r.diverged == null); try testing.expectEqualSlices(Ghost, answered(a.core, serial).?.ghosts, answered(r.core(), serial).?.ghosts); try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); } -test "a description's ghost row keeps its author and age: in a pane narrower than the row its title gives way, cut to the room" { +test "a description is whole, its title and its author never cut: in a pane narrower than it, it wraps as any line does, its author and age at the end of its last row; its lane ends on its first row in the arrow, its rows after carry the lane's cells blank" { const gpa = testing.allocator; const title = "a title long enough that no narrow pane holds the whole of it beside its author and its age"; var repo = try Repo.with(gpa, &.{.{ "fn f() void {\n a();\n}\n", title }}, null) orelse return error.SkipZigTest; @@ -2628,24 +2752,51 @@ test "a description's ghost row keeps its author and age: in a pane narrower tha const ch = answered(p, pane.serial).?; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); + const said = try std.fmt.allocPrint(arena.allocator(), "{s} {s} t now", .{ ch.pins[0].id[0..4], title }); + try testing.expectEqualStrings(said, File.sourceLine(pane, 3)); + // Its rows from the fourth, each within the room, put back together: + // the whole of it. var buf: [64][]const u8 = undefined; const got = try Ghosts.rows(p, pane, arena.allocator(), &buf); - const row = Ghosts.text(pane, got[3]); const room = pane.cols - File.gutterWidth(pane) - 1; - try testing.expect(File.displayWidth(p.settings.tab, row) <= room); - try testing.expectStringStartsWith(row, try std.fmt.allocPrint(arena.allocator(), "{s} a title long", .{ch.pins[0].id[0..4]})); - try testing.expectStringEndsWith(row, "\u{2026} t now"); - // Drawn: the id in the keyword's ink, the author and the age grey, - // at the row's end. + var whole: std.ArrayList(u8) = .empty; + var n: usize = 0; + while (whole.items.len < said.len) : (n += 1) { + const row = Ghosts.text(pane, got[3 + n]); + try testing.expect(File.displayWidth(p.settings.tab, row) <= room); + try whole.appendSlice(arena.allocator(), row); + } + try testing.expectEqualStrings(said, whole.items); + try testing.expect(n >= 3); + try testing.expectStringEndsWith(Ghosts.text(pane, got[3 + n - 1]), " t now"); + // Drawn: the id in the keyword's ink on its first row, the author and + // the age grey at the end of its last; the arrow on the first row, + // the rows after it no lane. p.sync(); const s = try p.render(arena.allocator()); const body = s.region(.body, id).?.rect; const x = body.x + File.gutterWidth(pane); try testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, s.at(x, body.y + 3).style.fg); - const age_x = x + @as(u16, @intCast(File.displayWidth(p.settings.tab, row))) - 1; - try testing.expectEqualStrings("w", s.at(age_x, body.y + 3).grapheme()); - try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(age_x, body.y + 3).style.fg); - try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(age_x - 4, body.y + 3).style.fg); + const last: u16 = body.y + 3 + @as(u16, @intCast(n - 1)); + const end = x + @as(u16, @intCast(File.displayWidth(p.settings.tab, Ghosts.text(pane, got[3 + n - 1])))) - 1; + try testing.expectEqualStrings("w", s.at(end, last).grapheme()); + try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(end, last).style.fg); + try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(end - 4, last).style.fg); + const first = x - File.laneCells(pane); + for (0..n) |k| { + const y = body.y + 3 + @as(u16, @intCast(k)); + if (windowed) { + var lanes: usize = 0; + for (s.lanes.items) |lane| if (lane.y == y) { + lanes += 1; + try testing.expectEqual(@as(u8, if (k == 0) @intCast(File.laneCells(pane)) else 0), lane.arrow); + }; + try testing.expectEqual(@as(usize, @intFromBool(k == 0)), lanes); + } else for (0..levels) |level| { + const glyph = if (k > 0) " " else if (level == 0) "\u{2570}" else if (level + 1 == levels) "\u{2192}" else "\u{2500}"; + try testing.expectEqualStrings(glyph, s.at(first + @as(u16, @intCast(level)), y).grapheme()); + } + } } } @@ -2718,6 +2869,390 @@ test "lines typed between the word and jj's answer: the ghost rows stand where t try testing.expectEqualSlices(Ghost, ch.ghosts, answered(w.r.core(), w.serial).?.ghosts); } +/// A function `cut` took a line away from and changed another in, and a +/// file beside it: `Changes @-` puts in the buffer the line taken away (a +/// builtin's name and a path in it), the changed line's old text and +/// cut's description, as lines. +const Lines = struct { + const fn_text = "fn f() void {\n a();\n // Syntax b.zig\n c();\n}\n"; + const cut_text = "fn f() void {\n a();\n c2();\n}\n"; + + fn repo() !?Repo { + var r = try Repo.with(testing.allocator, &.{ .{ fn_text, "fn" }, .{ cut_text, "cut" } }, null) orelse return null; + errdefer r.deinit(testing.allocator); + try r.tmp.dir.writeFile(testing.io, .{ .sub_path = "b.zig", .data = "const b = 1;\n" }); + return r; + } + + /// The buffer's lines, the layer shown; `said` cut's description. + fn shown(said: []const u8) [7][]const u8 { + return .{ "fn f() void {", " a();", " // Syntax b.zig", " c();", " c2();", said, "}" }; + } + + fn open(p: *Pardes, file: []const u8) !*Pane { + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{file}) }); + p.update(.{ .command = "Changes @-" }); + return p.panes[p.view().active].?; + } + + fn send(p: *Pardes, keys: []const u8) void { + for (keys) |k| p.update(.{ .key = if (k == 27) .{ .cp = pardes.Key.escape } else .{ .cp = k, .text = &[_]u8{k} } }); + } + + fn click(p: *Pardes, button: pardes.Mouse.Button, col: u16, row: u16) void { + p.update(.{ .mouse = .{ .button = button, .kind = .press, .col = col, .row = row } }); + p.update(.{ .mouse = .{ .button = button, .kind = .release, .col = col, .row = row } }); + } + + fn at(pane: *Pane, row: i32, col: i32) void { + pane.body.cur_row = row; + pane.body.cur_col = col; + pane.body.vsel.active = false; + pane.body.nsel = 0; + } + + fn description(arena: std.mem.Allocator, ch: *const Changes) ![]const u8 { + return std.fmt.allocPrint(arena, "{s} cut t now", .{ch.pins[0].id[0..4]}); + } +}; + +test "the layer's lines are the pane's text, read-only: j and k go onto them, a selection through them yanks the texts in order, / finds a word only a deleted line has, B2 on a builtin's name there runs it and B3 on a path opens it, a text object and a count work there; typing, d, a paste and a replace there or across their edge are refused in words, the file as it was; a Look at :N is the file's line N; hidden with the cursor in one, it is at the start of the file line it stood before" { + const gpa = testing.allocator; + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + for ([_]bool{ false, true }) |windowed| { + // Tall enough that the body's lines stay in view beside the `/` + // query however long the test's directory makes the tags. + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 44, .windowed = windowed }); + defer p.deinit(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const pane = try Lines.open(p, repo.file); + const id = p.view().active; + const f = &pane.file.?; + const ch = answered(p, pane.serial).?; + const want = Lines.shown(try Lines.description(arena.allocator(), ch)); + for (want, 0..) |w, row| try testing.expectEqualStrings(w, File.sourceLine(pane, @intCast(row))); + try testing.expectEqualStrings(Lines.cut_text, try File.fileText(f, arena.allocator())); + // j and k: onto the line taken away, the old text, the line, the + // description, and back. + p.update(.{ .command = "Look :2" }); + Lines.send(p, ";"); + try testing.expectEqual(@as(i32, 1), pane.body.cur_row); + for ([_]i32{ 2, 3, 4, 5 }) |row| { + Lines.send(p, "j"); + try testing.expectEqual(row, pane.body.cur_row); + } + Lines.send(p, "k"); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + // From `a();` through the two lines taken away to `c2();`: yanked, + // the texts as shown, in order. + Lines.at(pane, 1, 0); + Lines.send(p, "xxxxy;"); + try testing.expectEqualStrings(" a();\n // Syntax b.zig\n c();\n c2();\n", p.registers.text(p.gpa, pardes.Registers.default).?); + // A text object (the word under the cursor) and a count there. + Lines.at(pane, 2, 8); + Lines.send(p, "miwy;"); + try testing.expectEqualStrings("Syntax", p.registers.text(p.gpa, pardes.Registers.default).?); + Lines.at(pane, 2, 0); + Lines.send(p, "2j"); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + // Refused, in words, the buffer and the file as they were: a key + // typed in it, its line deleted, a paste in it, a character + // replaced, a deletion from the line above into it. + const before = try arena.allocator().dupe(u8, f.content); + for ([_][]const u8{ "iz\x1b", "d", "xd", "p", "rq", "kxxd" }) |keys| { + p.view().active = id; + Lines.at(pane, 2, 6); + p.setMessage(.at(id), ""); + Lines.send(p, keys); + try testing.expectEqualStrings(before, f.content); + try testing.expect(std.mem.indexOf(u8, told(p, id), File.read_only_said) != null); + Lines.send(p, "\x1b;"); + } + try testing.expectEqualStrings(Lines.cut_text, try File.fileText(f, arena.allocator())); + try testing.expectEqual(f.saved_revision, f.revision); + // B2 on the builtin's name in the line taken away runs it; B3 on + // the path there opens it (no selection held: B2 in one runs it). + // (The refusals' words, stacked in the notice band over the body's + // first rows, gone.) + pane.msg_len = 0; + pane.msg_older_len = 0; + p.view().active = id; + Lines.at(pane, 0, 0); + p.sync(); + const s = try p.render(arena.allocator()); + const body = s.region(.body, id).?.rect; + const tx = body.x + File.gutterWidth(pane); + const ty = body.y + @as(u16, @intCast(pane.wrapRow(p.settings.tab, 2, 0).row)); + const ink = pane.layers.ink; + Lines.click(p, .middle, tx + 9, ty); + try testing.expectEqual(!ink, pane.layers.ink); + Lines.click(p, .right, tx + 16, ty); + const opened = p.panes[p.view().active].?; + try testing.expect(std.mem.endsWith(u8, opened.file.?.path, "/b.zig")); + p.view().active = id; + // `:3` is the file's line 3, `c2();`: past the two lines above it. + p.update(.{ .command = "Look :3" }); + Lines.send(p, ";"); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + // Hidden with the cursor in the old text: at the start of the line + // it stood before, `c2();`. + Lines.at(pane, 3, 6); + p.update(.{ .command = "Changes" }); + try testing.expectEqualStrings(Lines.cut_text, f.content); + try testing.expectEqual(@as(i32, 2), pane.body.cur_row); + try testing.expectEqual(@as(i32, 0), pane.body.cur_col); + // Shown again: the cursor stays on its line. + p.update(.{ .command = "Changes" }); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + // `/` finds a word only the line taken away has. + p.view().active = id; + Lines.at(pane, 0, 0); + Lines.send(p, "/Syntax"); + p.update(.{ .key = .{ .cp = pardes.Key.enter } }); + p.view().active = id; + try testing.expectEqual(@as(i32, 2), pane.body.cur_row); + } +} + +test "THE FILE'S TEXT: with the layer's lines in the buffer, every reader of the file reads what it reads with them out, byte for byte: a Save, a Save to another name, Edit's w, body over 9P, the dirty check, a reread from the disk, a Get, jj's lines asked again, the context declarations, the colours of the file's lines, the language server's text and the place it is asked at" { + const gpa = testing.allocator; + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const th = @import("ninep/harness.zig"); + const Node = @import("ninep/tree.zig").Node; + const Lsp = @import("dev").host_io.Lsp; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const keep = arena.allocator(); + const Seen = struct { + lsp_source: []const u8 = "", + lsp_offset: u32 = 0, + saved: []const u8 = "", + clean: bool = false, + other: []const u8 = "", + written: []const u8 = "", + body: []const u8 = "", + reread_clean: bool = false, + gotten: []const u8 = "", + lines: []const Line = &.{}, + declarations: []const [3]usize = &.{}, + colours: []const u8 = "", + }; + var seen: [2]Seen = .{ .{}, .{} }; + const other = try std.fmt.allocPrint(keep, "{s}/copy.zig", .{repo.dir}); + const w_path = try std.fmt.allocPrint(keep, "{s}/w.zig", .{repo.dir}); + for ([_]bool{ false, true }, &seen) |shown, *got| { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 30, .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].?; + const f = &pane.file.?; + if (shown) p.update(.{ .command = "Changes @-" }); + try testing.expectEqual(shown, f.read_only.len > 0); + // The same edit of a file line, `c2();` (its line 3). + p.update(.{ .command = "Look :3" }); + Lines.send(p, ";A // e\x1b"); + try testing.expect(f.revision != f.saved_revision); + // The language server: the place it is asked at, the text it is sent. + p.update(.{ .command = "Def" }); + got.lsp_offset = p.lsp_ask.last.?.offset; + const job = try Lsp.snapshot(gpa, p, .{ .id = 1, .kind = .definition, .pane = .at(id), .offset = got.lsp_offset, .arg = "" }); + defer job.free(gpa); + got.lsp_source = try keep.dupe(u8, job.source); + // The context declarations and the colours of the file's lines. + f.tree_context = true; + File.refreshContext(p, pane, f); + var decls: std.ArrayList([3]usize) = .empty; + for (f.context.declarations) |d| try decls.append(keep, .{ d.start_line, d.end_line, d.header_end_line }); + got.declarations = decls.items; + f.tree_context = false; + p.sync(); + _ = try p.render(arena.allocator()); + var colours: std.ArrayList(u8) = .empty; + for (0..File.fileLen(f)) |b| { + const at = File.bufferOffset(f, b, false); + try colours.append(keep, if (at >= f.highlight_start and at - f.highlight_start < f.highlights.len) f.highlights[at - f.highlight_start] else 0xff); + } + got.colours = colours.items; + // body over 9P. + got.body = try keep.dupe(u8, th.rd(p, Node.of(pane.serial, .body), 0, 1 << 16).bytes); + // A Save: what the host is asked to write; clean after it. + p.update(.{ .command = "Save" }); + while (p.nextEffect()) |e| p.perform(e); + got.saved = try keep.dupe(u8, p.fallback.get(repo.file) orelse ""); + got.clean = f.revision == f.saved_revision and !pardes.ctlfs.pane.dirtyOf(pane); + // A Save to another name; Edit's w. + p.update(.{ .command = try std.fmt.bufPrint(&line, "Save {s}", .{other}) }); + while (p.nextEffect()) |e| p.perform(e); + got.other = try keep.dupe(u8, p.fallback.get(other) orelse ""); + p.view().active = id; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Edit ,w {s}", .{w_path}) }); + while (p.nextEffect()) |e| p.perform(e); + got.written = try keep.dupe(u8, p.fallback.get(w_path) orelse ""); + // The disk reread as what was saved: nothing reloaded, clean. + const revision = f.revision; + File.changed(p, @intCast(id), got.saved); + got.reread_clean = f.revision == revision and f.revision == f.saved_revision; + // jj asked again: who wrote each of the file's lines. + p.view().active = id; + p.update(.{ .command = "Changes @-" }); + got.lines = try keep.dupe(Line, answered(p, pane.serial).?.lines); + if (!shown) p.update(.{ .command = "Changes" }); + try testing.expectEqual(shown, f.read_only.len > 0); + // A Get: the file's text from where it was read, the layer kept. + p.update(.{ .command = "Get" }); + got.gotten = try keep.dupe(u8, try File.fileText(f, arena.allocator())); + try testing.expectEqual(shown, f.read_only.len > 0); + } + const off, const on = seen; + try testing.expectEqualStrings("fn f() void {\n a();\n c2(); // e\n}\n", off.saved); + inline for (comptime std.meta.fieldNames(Seen)) |name| { + const a = @field(off, name); + const b = @field(on, name); + if (@TypeOf(a) == bool or @TypeOf(a) == u32) try testing.expectEqual(a, b) else try testing.expectEqualSlices(@TypeOf(a[0]), a, b); + } + try testing.expect(off.clean and off.reread_clean); + try testing.expect(off.declarations.len > 0); +} + +test "an edit made while the layer is shown is the file's: journaled in its lines, its undo lands right shown or hidden, hidden and shown again between; the layer's lines are never in the journal" { + const gpa = testing.allocator; + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 20, .windowed = false }); + defer p.deinit(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const pane = try Lines.open(p, repo.file); + const f = &pane.file.?; + const typed = "fn f() void {\n a();\n c2(); // e\n}\n"; + // `c2();`, the line after the two of the layer: appended to, shown. + Lines.at(pane, 4, 0); + Lines.send(p, "A // e\x1b"); + try testing.expectEqualStrings(typed, try File.fileText(f, arena.allocator())); + try testing.expectEqualStrings(" c2(); // e", File.sourceLine(pane, 4)); + // Undone while shown, redone. + Lines.send(p, "u"); + try testing.expectEqualStrings(Lines.cut_text, try File.fileText(f, arena.allocator())); + try testing.expectEqual(@as(usize, 3), f.read_only.len); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + Lines.send(p, "U"); + try testing.expectEqualStrings(typed, try File.fileText(f, arena.allocator())); + // Hidden, shown again, then undone: the same file text, the lines + // where they stood. + p.update(.{ .command = "Changes" }); + try testing.expectEqualStrings(typed, f.content); + p.update(.{ .command = "Changes" }); + Lines.send(p, "u"); + try testing.expectEqualStrings(Lines.cut_text, try File.fileText(f, arena.allocator())); + try testing.expectEqualStrings(" c2();", File.sourceLine(pane, 4)); + // Hidden, undone back to before the edit and redone: the file's. + p.update(.{ .command = "Changes" }); + Lines.send(p, "U"); + try testing.expectEqualStrings(typed, f.content); + Lines.send(p, "u"); + try testing.expectEqualStrings(Lines.cut_text, f.content); + // No entry of the journal holds a byte of the layer's. + for (f.history.journal.entries.items) |e| try testing.expect(std.mem.indexOf(u8, e.removed, "Syntax") == null); +} + +test "9P serves the file's text: body, addr and data in its offsets; dot and sel with the cursor in a layer's line are after the file line above it; a write over 9P is the file's, the layer's lines standing where they stood" { + const gpa = testing.allocator; + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const th = @import("ninep/harness.zig"); + const Node = @import("ninep/tree.zig").Node; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 20, .windowed = false }); + defer p.deinit(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const pane = try Lines.open(p, repo.file); + const serial = pane.serial; + try testing.expectEqualStrings(Lines.cut_text, th.rd(p, Node.of(serial, .body), 0, 1 << 16).bytes); + // The cursor in the line taken away: dot is where `c2();` starts in + // the file, after `a();` (14 + 9 bytes). + Lines.at(pane, 2, 6); + try testing.expectEqualStrings(" 23 23 ", th.rd(p, Node.of(serial, .dot), 0, 64).bytes); + // A selection from `a();` into the old text: what the file has of it. + Lines.at(pane, 1, 0); + Lines.send(p, "xx"); + try testing.expectEqualStrings(" a();\n", th.rd(p, Node.of(serial, .sel), 0, 64).bytes); + Lines.send(p, ";"); + // `addr` 3 is the file's line 3; `data` there is it. + _ = th.wr(p, Node.of(serial, .addr), "3"); + try testing.expectEqualStrings(" c2();\n", th.rd(p, Node.of(serial, .xdata), 0, 64).bytes); + // Written over: the file's line, the layer's lines still before it. + _ = th.wr(p, Node.of(serial, .addr), "3"); + _ = th.wr(p, Node.of(serial, .data), " c3();\n"); + try testing.expectEqualStrings("fn f() void {\n a();\n c3();\n}\n", th.rd(p, Node.of(serial, .body), 0, 1 << 16).bytes); + try testing.expectEqualStrings(" // Syntax b.zig", File.sourceLine(pane, 2)); + try testing.expectEqualStrings(" c3();", File.sourceLine(pane, 4)); + // `dot=addr` from 9P lands on the file's line. + _ = th.wr(p, Node.of(serial, .addr), "3"); + _ = th.wr(p, Node.of(serial, .ctl), "dot=addr"); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); +} + +test "a session dumped by a build whose layer was a view's bit and no lines (wire 31) loads with the layer shown as it was, its lines in the buffer, the file's text and its places as they were; a hidden one stays hidden" { + const gpa = testing.allocator; + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + for ([_]bool{ true, false }) |shown| { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 20, .windowed = false }); + defer p.deinit(); + const pane = try Lines.open(p, repo.file); + const f = &pane.file.?; + // As 31 kept it: the view's bit (or none), the buffer the file's. + hideLayer(p, f); + if (!shown) setLanes(p, f, false); + try testing.expectEqualStrings(Lines.cut_text, f.content); + Lines.at(pane, 2, 3); + try pardes.dump.dumpState(p); + const q = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 60, .rows = 20, .windowed = false }, p.dump_out.?); + defer q.deinit(); + const back = q.panes[q.view().active].?; + const bf = &back.file.?; + try testing.expectEqualStrings(Lines.cut_text, try File.fileText(bf, q.scratch.allocator())); + try testing.expectEqual(shown, bf.read_only.len == 3); + try testing.expectEqual(shown, back.layers.lanes > 0); + // The cursor on `c2();` (the file's line 3) as it was. + try testing.expectEqualStrings(" c2();", File.sourceLine(back, back.body.cur_row)); + } +} + +test "a client replays the layer's lines and an edit refused among them, and ends equal; a client joined over a pane showing the layer loads its lines" { + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + 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 = "Changes @-" })); + r.confirm(try a.local(.{ .command = "Look :2" })); + for (";jjiz") |k| r.confirm(try a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } })); + r.confirm(try a.local(.{ .key = .{ .cp = pardes.Key.escape } })); + for ("jA;") |k| r.confirm(try a.local(.{ .key = .{ .cp = k, .text = &[_]u8{k} } })); + r.confirm(try a.local(.{ .key = .{ .cp = pardes.Key.escape } })); + r.settle(); + try testing.expect(r.diverged == null); + const id = a.core.view().active; + const af = &a.core.panes[id].?.file.?; + const rf = &r.core().panes[id].?.file.?; + try testing.expectEqualStrings("fn f() void {\n a();\n c2();;\n}\n", try File.fileText(af, a.core.scratch.allocator())); + try testing.expectEqualStrings(af.content, rf.content); + try testing.expectEqualSlices(File.ReadOnly, af.read_only, rf.read_only); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); + try R.loadsAsSession(a, 2); +} + /// A function three changes wrote: `fn` all of it, `mid` its lines 3-6 /// (rewriting two, adding two), `shift` line 4 again. const Stack = struct { @@ -2795,7 +3330,7 @@ test "the lanes ride with the text: a line typed in a nested block is nobody's, // `x();` (line 5, mid's) deleted: its value goes with it. r.confirm(try a.local(.{ .command = "Look a.zig:5" })); try Keys.send(a, r, ";xd"); - try testing.expectEqualStrings(Stack.shift_text[0..41] ++ Stack.shift_text[50..], a.core.panes[a.core.view().active].?.file.?.content); + try testing.expectEqualStrings(Stack.shift_text[0..41] ++ Stack.shift_text[50..], try File.fileText(&a.core.panes[a.core.view().active].?.file.?, a.core.scratch.allocator())); try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, 1, 0, 0, 0, 0 }, pinsOf(answered(a.core, serial).?, &got)); try testing.expectEqual(before[5].lanes, answered(a.core, serial).?.lines[4].lanes); try testing.expect(r.diverged == null); |
