diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-05 19:32:34 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 22:03:21 -0300 |
| commit | 8fe1cbd5ba5792b71f4a1b9be6f3e8df591ec52b (patch) | |
| tree | 6977f394e1f07e9cee6f4ebe791de3e09ba95c4e | |
| parent | 542e6677e23e277ce39edb3e96d729e9adca99d0 (diff) | |
| download | pardes-8fe1cbd5ba5792b71f4a1b9be6f3e8df591ec52b.tar.gz pardes-8fe1cbd5ba5792b71f4a1b9be6f3e8df591ec52b.zip | |
split pardes.zig by noun, Jump: the jump list's functions and its place type move into src/Jump.zig, the clean room's name: Loc and the path cap a closed place names, jumpPath, samePlace, trackJump and jumpBy (the list's fields stay the session's until #38); callers name Jump (builtins.zig's Back and Forward, mouse.zig's side buttons, sync's trackJump, test/pdf.zig as pardes.Jump); the session's recentDot is pub for it; tests moved with them, by name: the jump list never holds two entries in a row the same, when the one between them goes; jump history skips an entry whose pane slot or serial is gone, leaving the selection; Back and Forward take the same steps from the mouse's side buttons and jumpBy (and its JumpsTest); jump history terminal rectangle follows output and clears on reflow; a pure move, no field touched, a dump and the wire as they were; gates on tui: core-test 960/960, behaviour-test 238/238, pane-test 171/172 (1 skipped)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/Jump.zig | 369 | ||||
| -rw-r--r-- | src/builtins.zig | 5 | ||||
| -rw-r--r-- | src/config.zig | 2 | ||||
| -rw-r--r-- | src/mouse.zig | 3 | ||||
| -rw-r--r-- | src/pardes.zig | 358 | ||||
| -rw-r--r-- | test/pdf.zig | 8 |
6 files changed, 384 insertions, 361 deletions
diff --git a/src/Jump.zig b/src/Jump.zig new file mode 100644 index 00000000..237be82a --- /dev/null +++ b/src/Jump.zig @@ -0,0 +1,369 @@ +//! The jump list: places jumped from, Back and Forward (Ctrl-o, Ctrl-i), +//! a closed file's place reopened from Recent. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const panes = @import("panes.zig"); +const look = @import("look.zig"); +const exec = @import("exec.zig"); +const mouse = @import("mouse.zig"); +const edit = @import("edit.zig"); +const builtins = @import("builtins.zig"); +const layout = @import("layout.zig"); +const limits = @import("memory.zig").limits; +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const MAX_PANES = pardes.MAX_PANES; +const MAX_JUMPS = pardes.MAX_JUMPS; +const BOX_H = pardes.BOX_H; +const Mouse = pardes.Mouse; + +const recent_path_cap = limits.host_path_cap; + +pub const Loc = struct { + pane: u16, + /// 0 once its pane has closed: a file's entry stays, reopened by a + /// jump to it from `path_hash`'s recent entry. + serial: u32, + line: u32, + col: u32, + /// The file it is in (Wyhash of the path), 0 for a pane with none. + path_hash: u64 = 0, + /// A PDF's place: `line` its page (one-based), `col` how far down it + /// (pdf_view.Place.at, a fraction of the page in 2^-32). + pdf: bool = false, +}; + +/// A session for the side-button jump test: four panes of 100 lines in one +/// column, A (pane 0) and B each on a file of its own, C on A's file, and O +/// an output buffer, no file behind it. `order` is the jumplist, a letter a +/// place, each letter's n-th place its line 10 + 40n: "ABABA" is A10 B10 A50 +/// B50 A90. The keyboard is on the last place, as after jumping there. +const JumpsTest = struct { + p: *Pardes, + ids: [4]usize = .{ 0, 0, 0, 0 }, + + const letters = "ABCO"; + const Way = enum { call, mouse }; + + fn init(order: []const u8) !JumpsTest { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 80 }); + errdefer p.deinit(); + const text = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))); + _ = try p.setTestFile(text); + var t: JumpsTest = .{ .p = p }; + for ([_][]const u8{ "/b.txt", "/test.txt", "" }, 1..) |path, n| { + const id = p.freeSlot().?; + const pane = try p.newDocPane(id); + pane.file = .{ + .path = try p.gpa.dupe(u8, path), + .content = try p.gpa.dupe(u8, text), + .history = try panes.File.History.create(p.gpa), + }; + if (path.len == 0) pane.file.?.output = .{ .from = .search }; + layout.insert(p, 0, n, id); + t.ids[n] = id; + } + layout.compute(p); + var seen: [4]u32 = @splat(0); + for (order, 0..) |letter, i| { + const which = std.mem.indexOfScalar(u8, letters, letter).?; + const pane = p.panes[t.ids[which]].?; + p.jumps[i] = .{ + .pane = @intCast(t.ids[which]), + .serial = pane.serial, + .line = 10 + 40 * seen[which], + .col = 1, + .path_hash = if (letter == 'O') 0 else std.hash.Wyhash.hash(0, pane.file.?.path), + }; + seen[which] += 1; + } + p.njumps = order.len; + p.jcur = order.len - 1; + look.focusPaneLine(p, p.jumps[p.jcur].pane, .{ .line = p.jumps[p.jcur].line, .col = 1 }, .center); + p.sync(); + return t; + } + + /// One step `delta` through `way`: the function, or the mouse's side + /// button pressed and let go. + fn step(t: JumpsTest, way: Way, delta: i32) void { + const back = delta < 0; + switch (way) { + .call => jumpBy(t.p, delta), + .mouse => for ([_]Mouse.Kind{ .press, .release }) |kind| + t.p.update(.{ .mouse = .{ .button = if (back) .back else .forward, .kind = kind, .col = 0, .row = 0 } }), + } + t.p.sync(); + } + + /// Steps `delta` until the list ends that way, saying so; the places + /// landed on, as "A50 A10 B10". + fn walk(t: JumpsTest, way: Way, delta: i32, buf: []u8) ![]const u8 { + var out: std.Io.Writer = .fixed(buf); + for (0..2 * MAX_JUMPS) |_| { + const before = t.p.jcur; + t.step(way, delta); + if (t.p.jcur == before) break; + const pane = t.p.panes[t.p.active].?; + const which = std.mem.indexOfScalar(usize, &t.ids, t.p.active).?; + try out.print("{s}{c}{d}", .{ if (out.end > 0) " " else "", letters[which], pane.body.cur_row + 1 }); + } else return error.NoEnd; + const pane = t.p.panes[t.p.active].?; + const said = pane.msg[0..pane.msg_len]; + try std.testing.expect(std.mem.indexOf(u8, said, if (delta < 0) "nothing further back" else "nothing further on") != null); + return out.buffered(); + } +}; + +/// The file a jump in `pane` is in: a text's, a PDF's. +fn jumpPath(pane: *const Pane) ?[]const u8 { + if (Pardes.recentDot(pane)) |d| return d.path; + return pane.pdfPath(); +} + +fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool { + if (a.pane != b.pane or a.serial != b.serial) return false; + // A PDF's places by where they stand on its strip, as a text's by + // lines: within a body of each other, one visit. + if (a.pdf and b.pdf) if (p.panes[b.pane]) |pane| { + const view = panes.Pdf.paneViewport(p, pane) orelse return a.line == b.line; + const ya = panes.Pdf.placeY(pane, .{ .page = a.line - 1, .at = a.col }) orelse return a.line == b.line; + const yb = panes.Pdf.placeY(pane, .{ .page = b.line - 1, .at = b.col }) orelse return a.line == b.line; + return @abs(ya - yb) <= @as(f64, @floatFromInt(view.pixel_h)); + }; + if (a.line == 0 or b.line == 0) return true; + const body = @max(1, p.rects[b.pane].h -| BOX_H); + return @max(a.line, b.line) - @min(a.line, b.line) <= body; +} + +pub fn trackJump(p: *Pardes) void { + var w: usize = 0; + var cur: usize = 0; + for (p.jumps[0..p.njumps], 0..) |j, i| { + if (i == p.jcur) cur = w; // survives -> lands at w; dies -> the next one does + var kept = j; + const live = if (j.serial == 0) false else if (p.panes[j.pane]) |pane| pane.serial == j.serial else false; + // A file's pane closed: its entry stays, closed, and a jump to it + // opens it again from Recent, or drops it when Recent has + // forgotten it (`jumpBy`: Recent is the authority's, read when + // it is wanted, not at every step). + if (!live) { + if (j.path_hash == 0) continue; + kept.serial = 0; + // Open again in some pane (a look, Recent): the entry is that + // pane's, and closed only while no pane has the file. + for (p.panes, 0..) |slot, id| if (slot) |pane| if (jumpPath(pane)) |path| { + if (std.hash.Wyhash.hash(0, path) != j.path_hash) continue; + kept.pane = @intCast(id); + kept.serial = pane.serial; + break; + }; + } + // Two in a row the same (a closed one reopened beside its own, + // or the entry between them gone): one entry, one step. + if (w > 0 and std.meta.eql(p.jumps[w - 1], kept)) { + if (i == p.jcur) cur = w - 1; + continue; + } + p.jumps[w] = kept; + w += 1; + } + p.njumps = w; + p.jcur = @min(cur, w -| 1); + + const pane = p.panes[p.active] orelse return; + // A query being typed moves the owner's selection to show, never a + // jump; its pane is no place to come back to either. + if (pane.query) |q| if (!q.frozen) return; + const force = p.jump_force; + p.jump_force = false; + const pdf_place = panes.Pdf.place(pane); + const now: Loc = .{ + .pane = @intCast(p.active), + .serial = pane.serial, + .line = if (pdf_place) |at| @intCast(at.page + 1) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0, + .col = if (pdf_place) |at| at.at else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_col) + 1) else 0, + .path_hash = if (jumpPath(pane)) |path| std.hash.Wyhash.hash(0, path) else 0, + .pdf = pdf_place != null, + }; + if (!force and p.njumps > 0 and samePlace(p, p.jumps[p.jcur], now)) { + p.jumps[p.jcur] = now; // same visit; the entry IS where you are + return; + } + // a new jump made from the middle of the list drops everything ahead of + // it, the way vim's does: the future you did not take is not history. + if (p.njumps > 0) p.njumps = p.jcur + 1; + if (!force and p.njumps >= 2 and samePlace(p, p.jumps[p.njumps - 2], now)) { + p.jumps[p.njumps - 2] = p.jumps[p.njumps - 1]; + p.jumps[p.njumps - 1] = now; + p.jcur = p.njumps - 1; + return; + } + if (p.njumps == MAX_JUMPS) { + std.mem.copyForwards(Loc, p.jumps[0 .. MAX_JUMPS - 1], p.jumps[1..]); + p.njumps -= 1; + } + p.jumps[p.njumps] = now; + p.jcur = p.njumps; + p.njumps += 1; +} + +/// One step through the jumplist, older for a negative `delta`, newer +/// otherwise: Back and Forward from every key, mouse button, builtin and +/// 9P exec. `jcur` is the entry where you are; the step moves it to the +/// entry it lands on, and the entries it passed over stay where they are. +/// +/// JumpScope file, from a pane with a file: the nearest entry that way in +/// this pane, else in this file in any pane (a closed one's too), else, +/// with none of this file left that way, the nearest of any file, as +/// JumpScope all always takes. Forward mirrors Back, so it retraces a run +/// of Backs inside one file step for step, and Forwards end, as they +/// always have, at the newest entry. +pub fn jumpBy(p: *Pardes, delta: i32) void { + const older = delta < 0; + // At an end of the list: said, not a step that silently does nothing. + if (p.njumps == 0 or (older and p.jcur == 0) or (!older and p.jcur + 1 >= p.njumps)) + return p.setMessage(p.active, if (older) "Back: nothing further back" else "Forward: nothing further on"); + var next = if (older) p.jcur - 1 else p.jcur + 1; + // A terminal or a scratch has no file to keep to: the step is global. + if (p.settings.jump_scope == .file) if (p.panes[p.active]) |here| if (Pardes.recentPlace(here)) |place| if (place.path.len > 0) { + const hash = if (jumpPath(here)) |path| std.hash.Wyhash.hash(0, path) else 0; + // The nearest of this pane's own wins over a nearer one of this + // file in another pane: the step does not move the keyboard. + var pick: ?usize = null; + var i = p.jcur; + while (if (older) i > 0 else i + 1 < p.njumps) { + i = if (older) i - 1 else i + 1; + const k = p.jumps[i]; + if (k.pane == p.active and k.serial == here.serial) { + pick = i; + break; + } + if (pick == null and hash != 0 and k.path_hash == hash) pick = i; + } + next = pick orelse next; + }; + const j = p.jumps[next]; + if (j.pane >= MAX_PANES) return; + // A closed file's entry: opened again at its dot, placed as a look + // places it, and the entry is that pane from here on. + if (j.serial == 0) { + const found = p.recentByHash(j.path_hash); + // Deleted since, or Recent has forgotten it: its entries go (the + // first said), and the step goes on to the next place, never + // stuck on one that cannot open. + if (found == null or !Pardes.recentKeeps(found.?.path)) { + var said: [recent_path_cap + 32]u8 = undefined; + if (found) |e| p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); + const hash = j.path_hash; + var w: usize = 0; + var cur = p.jcur; + for (p.jumps[0..p.njumps], 0..) |k, i| { + if (k.serial == 0 and k.path_hash == hash) { + if (i < p.jcur) cur -= 1; + continue; + } + p.jumps[w] = k; + w += 1; + } + p.njumps = w; + p.jcur = @min(cur, w -| 1); + // The same step again, over the list without them: each + // round takes one file's entries away, so it ends. + return jumpBy(p, delta); + } + const e = found.?; + const from = p.active; + look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.pdf) 0 else @max(1, j.col) }); + const opened = p.panes[p.active] orelse return; + const opened_path = jumpPath(opened) orelse return; + if (!std.mem.eql(u8, opened_path, e.path)) return; // it did not open + if (j.pdf) panes.Pdf.landAt(p, opened, .{ .page = j.line -| 1, .at = j.col }); + // Every entry of that file is the reopened pane from here on, so + // a step onto another of them moves in it rather than reopening. + for (p.jumps[0..p.njumps]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { + k.pane = @intCast(p.active); + k.serial = opened.serial; + }; + p.jcur = next; + return; + } + const pane = p.panes[j.pane] orelse return; + if (pane.serial != j.serial) return; + if (p.panes[p.active]) |source| look.clearNavigationSelection(p, source); + if (p.active != j.pane) look.clearNavigationSelection(p, pane); + p.jcur = next; + if (pane.pdfPath() != null) { + look.focusPaneLine(p, j.pane, .{ .line = 0, .col = 0 }, .keep); + panes.Pdf.landAt(p, pane, .{ .page = j.line -| 1, .at = if (j.pdf) j.col else 0 }); + return; + } + look.focusPaneLine(p, j.pane, .{ .line = j.line, .col = j.col }, .center); +} + +test "the jump list never holds two entries in a row the same, when the one between them goes" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile(std.mem.asBytes(&@as([200][2]u8, @splat("a\n".*)))); + const at: Loc = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; + // A, a pane long closed with no file to keep it, A again, then far on. + p.jumps[0] = at; + p.jumps[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 }; + p.jumps[2] = at; + p.jumps[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 }; + p.njumps = 4; + p.jcur = 3; + pane.body.cur_row = 80; // where you are now: far from both + trackJump(p); + for (1..p.njumps) |i| try std.testing.expect(!std.meta.eql(p.jumps[i - 1], p.jumps[i])); + try std.testing.expectEqual(@as(usize, 3), p.njumps); + try std.testing.expectEqual(@as(u32, 1), p.jumps[0].line); + try std.testing.expectEqual(@as(u32, 150), p.jumps[1].line); +} + +test "jump history skips an entry whose pane slot or serial is gone, leaving the selection" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha beta\ngamma delta\nepsilon zeta\n"); + p.njumps = 2; + p.jcur = 1; + p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; + pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); + const selected = pane.body.primaryRange(pane.file.?.content, 0); + for ([_]Loc{ + .{ .pane = 15, .serial = pane.serial, .line = 1, .col = 1 }, + .{ .pane = 0, .serial = pane.serial + 1, .line = 1, .col = 1 }, + }) |missing| { + p.jumps[0] = missing; + jumpBy(p, -1); + try std.testing.expectEqual(@as(usize, 1), p.jcur); + try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0)); + } +} + +// The keys and builtins are test/behaviour/modes.zig's; a mouse's side +// buttons have no /mousein bits. +test "Back and Forward take the same steps from the mouse's side buttons and jumpBy" { + for ([_]JumpsTest.Way{ .call, .mouse }) |way| { + const t = try JumpsTest.init("BABABA"); + defer t.p.deinit(); + var buf: [128]u8 = undefined; + try std.testing.expectEqualStrings("A50 A10 B10", try t.walk(way, -1, &buf)); + try std.testing.expectEqualStrings("B50 B90 A90", try t.walk(way, 1, &buf)); + } +} + +test "jump history terminal rectangle follows output and clears on reflow" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12, .tty_only = true }); + defer p.deinit(); + const pane = p.panes[0].?; + p.update(.{ .output = .{ .pane = 0, .bytes = "alpha bravo\r\nsecond line\r\n" } }); + pane.sel[0] = .{ .state = .done, .c0 = 0, .c1 = 4, .r0 = BOX_H, .r1 = BOX_H }; + try edit.capturePointerSelection(p, pane, 0); + try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); + for (0..40) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "more output\r\n" } }); + try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); + panes.terminal.resizeGrid(pane, pane.cols - 1, pane.rows); + try std.testing.expect(pane.pointerSelection(pane.sel[0]) == null); + try std.testing.expectEqual(.none, pane.sel[0].state); +} diff --git a/src/builtins.zig b/src/builtins.zig index 08c41510..398e699d 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -14,6 +14,7 @@ const layout = @import("layout.zig"); const config = @import("config.zig"); const limits = @import("memory.zig").limits; const keys = @import("keys.zig"); +const Jump = @import("Jump.zig"); /// What this build's own frontend draws and its host does: the render's. pub const frontend: config.Runtime.Capabilities = .{ @@ -1810,7 +1811,7 @@ pub const Window = struct { pub const Back = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { - c.p.jumpBy(-1); + Jump.jumpBy(c.p, -1); } }; @@ -1818,7 +1819,7 @@ pub const Back = struct { pub const Forward = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { - c.p.jumpBy(1); + Jump.jumpBy(c.p, 1); } }; diff --git a/src/config.zig b/src/config.zig index 7b9e4cba..4b56cfa7 100644 --- a/src/config.zig +++ b/src/config.zig @@ -765,7 +765,7 @@ pub const Runtime = struct { /// (yazi's previews): `real`, as pixels where the shell can, or /// `petscii`, as glyph art. The Petscii builtin flips one pane. term_images: TermImages = .real, - /// What Back and Forward keep to (Pardes.jumpBy): `file`, the places in + /// What Back and Forward keep to (Jump.jumpBy): `file`, the places in /// the focused pane's file first, other files only once those run out /// that way; `all`, every place in the order it was jumped to. jump_scope: JumpScope = .file, diff --git a/src/mouse.zig b/src/mouse.zig index 6cb602f7..5c315b4e 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -20,6 +20,7 @@ const platform = pardes.platform; const Pane = panes.Pane; const MAX_PANES = pardes.MAX_PANES; const Layer = @import("Layer.zig"); +const Jump = @import("Jump.zig"); const TOPBAR_H = pardes.TOPBAR_H; const BOX_H = pardes.BOX_H; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; @@ -484,7 +485,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { if (pane.prompt == .del_side or pane.prompt == .repl_choice) pane.prompt = .none; }; if (m.button == .back or m.button == .forward) { - if (m.kind == .press) p.jumpBy(if (m.button == .back) -1 else 1); + if (m.kind == .press) Jump.jumpBy(p, if (m.button == .back) -1 else 1); return; } // A grip let go past the window's edge, as a browser's tab dragged out diff --git a/src/pardes.zig b/src/pardes.zig index 6b56d8cc..55b0832f 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -100,6 +100,8 @@ pub const font_picker = platform == .gui or platform == .macos; /// second thread, and every yield is a no-op. pub var turn: Turn = .{}; pub const Turn = @import("Turn.zig"); +pub const Jump = @import("Jump.zig"); +const Loc = Jump.Loc; pub const can_attach = platform == .tui or platform == .gui; @@ -277,24 +279,9 @@ test "a compact tagline anchors at its own pane, and both shells step from the s try std.testing.expectEqual(@as(u16, 3), gridColAt(p, 3.5 * body_w, body_row, body_w, tag_w)); } -const recent_path_cap = limits.host_path_cap; - -pub const Loc = struct { - pane: u16, - /// 0 once its pane has closed: a file's entry stays, reopened by a - /// jump to it from `path_hash`'s recent entry. - serial: u32, - line: u32, - col: u32, - /// The file it is in (Wyhash of the path), 0 for a pane with none. - path_hash: u64 = 0, - /// A PDF's place: `line` its page (one-based), `col` how far down it - /// (pdf_view.Place.at, a fraction of the page in 2^-32). - pdf: bool = false, -}; - test { _ = @import("rollback.zig"); + _ = @import("Jump.zig"); _ = @import("keys.zig"); _ = @import("Effect.zig"); _ = @import("tape.zig"); @@ -3131,14 +3118,8 @@ pub const Pardes = struct { return null; } - /// The file a jump in `pane` is in: a text's, a PDF's. - fn jumpPath(pane: *const Pane) ?[]const u8 { - if (recentDot(pane)) |d| return d.path; - return pane.pdfPath(); - } - /// A file pane's path and dot, for the recent list (0 when none). - fn recentDot(pane: *const Pane) ?struct { path: []const u8, line: u32, col: u32, aline: u32 = 0, acol: u32 = 0 } { + pub fn recentDot(pane: *const Pane) ?struct { path: []const u8, line: u32, col: u32, aline: u32 = 0, acol: u32 = 0 } { const f = pane.file orelse return null; if (f.output != null) return null; // The selection's anchor too, so a reopen restores both its ends. @@ -5105,186 +5086,6 @@ pub const Pardes = struct { return null; } - fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool { - if (a.pane != b.pane or a.serial != b.serial) return false; - // A PDF's places by where they stand on its strip, as a text's by - // lines: within a body of each other, one visit. - if (a.pdf and b.pdf) if (p.panes[b.pane]) |pane| { - const view = panes.Pdf.paneViewport(p, pane) orelse return a.line == b.line; - const ya = panes.Pdf.placeY(pane, .{ .page = a.line - 1, .at = a.col }) orelse return a.line == b.line; - const yb = panes.Pdf.placeY(pane, .{ .page = b.line - 1, .at = b.col }) orelse return a.line == b.line; - return @abs(ya - yb) <= @as(f64, @floatFromInt(view.pixel_h)); - }; - if (a.line == 0 or b.line == 0) return true; - const body = @max(1, p.rects[b.pane].h -| BOX_H); - return @max(a.line, b.line) - @min(a.line, b.line) <= body; - } - - fn trackJump(p: *Pardes) void { - var w: usize = 0; - var cur: usize = 0; - for (p.jumps[0..p.njumps], 0..) |j, i| { - if (i == p.jcur) cur = w; // survives -> lands at w; dies -> the next one does - var kept = j; - const live = if (j.serial == 0) false else if (p.panes[j.pane]) |pane| pane.serial == j.serial else false; - // A file's pane closed: its entry stays, closed, and a jump to it - // opens it again from Recent, or drops it when Recent has - // forgotten it (`jumpBy`: Recent is the authority's, read when - // it is wanted, not at every step). - if (!live) { - if (j.path_hash == 0) continue; - kept.serial = 0; - // Open again in some pane (a look, Recent): the entry is that - // pane's, and closed only while no pane has the file. - for (p.panes, 0..) |slot, id| if (slot) |pane| if (jumpPath(pane)) |path| { - if (std.hash.Wyhash.hash(0, path) != j.path_hash) continue; - kept.pane = @intCast(id); - kept.serial = pane.serial; - break; - }; - } - // Two in a row the same (a closed one reopened beside its own, - // or the entry between them gone): one entry, one step. - if (w > 0 and std.meta.eql(p.jumps[w - 1], kept)) { - if (i == p.jcur) cur = w - 1; - continue; - } - p.jumps[w] = kept; - w += 1; - } - p.njumps = w; - p.jcur = @min(cur, w -| 1); - - const pane = p.panes[p.active] orelse return; - // A query being typed moves the owner's selection to show, never a - // jump; its pane is no place to come back to either. - if (pane.query) |q| if (!q.frozen) return; - const force = p.jump_force; - p.jump_force = false; - const pdf_place = panes.Pdf.place(pane); - const now: Loc = .{ - .pane = @intCast(p.active), - .serial = pane.serial, - .line = if (pdf_place) |at| @intCast(at.page + 1) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0, - .col = if (pdf_place) |at| at.at else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_col) + 1) else 0, - .path_hash = if (jumpPath(pane)) |path| std.hash.Wyhash.hash(0, path) else 0, - .pdf = pdf_place != null, - }; - if (!force and p.njumps > 0 and p.samePlace(p.jumps[p.jcur], now)) { - p.jumps[p.jcur] = now; // same visit; the entry IS where you are - return; - } - // a new jump made from the middle of the list drops everything ahead of - // it, the way vim's does: the future you did not take is not history. - if (p.njumps > 0) p.njumps = p.jcur + 1; - if (!force and p.njumps >= 2 and p.samePlace(p.jumps[p.njumps - 2], now)) { - p.jumps[p.njumps - 2] = p.jumps[p.njumps - 1]; - p.jumps[p.njumps - 1] = now; - p.jcur = p.njumps - 1; - return; - } - if (p.njumps == MAX_JUMPS) { - std.mem.copyForwards(Loc, p.jumps[0 .. MAX_JUMPS - 1], p.jumps[1..]); - p.njumps -= 1; - } - p.jumps[p.njumps] = now; - p.jcur = p.njumps; - p.njumps += 1; - } - - /// One step through the jumplist, older for a negative `delta`, newer - /// otherwise: Back and Forward from every key, mouse button, builtin and - /// 9P exec. `jcur` is the entry where you are; the step moves it to the - /// entry it lands on, and the entries it passed over stay where they are. - /// - /// JumpScope file, from a pane with a file: the nearest entry that way in - /// this pane, else in this file in any pane (a closed one's too), else, - /// with none of this file left that way, the nearest of any file, as - /// JumpScope all always takes. Forward mirrors Back, so it retraces a run - /// of Backs inside one file step for step, and Forwards end, as they - /// always have, at the newest entry. - pub fn jumpBy(p: *Pardes, delta: i32) void { - const older = delta < 0; - // At an end of the list: said, not a step that silently does nothing. - if (p.njumps == 0 or (older and p.jcur == 0) or (!older and p.jcur + 1 >= p.njumps)) - return p.setMessage(p.active, if (older) "Back: nothing further back" else "Forward: nothing further on"); - var next = if (older) p.jcur - 1 else p.jcur + 1; - // A terminal or a scratch has no file to keep to: the step is global. - if (p.settings.jump_scope == .file) if (p.panes[p.active]) |here| if (recentPlace(here)) |place| if (place.path.len > 0) { - const hash = if (jumpPath(here)) |path| std.hash.Wyhash.hash(0, path) else 0; - // The nearest of this pane's own wins over a nearer one of this - // file in another pane: the step does not move the keyboard. - var pick: ?usize = null; - var i = p.jcur; - while (if (older) i > 0 else i + 1 < p.njumps) { - i = if (older) i - 1 else i + 1; - const k = p.jumps[i]; - if (k.pane == p.active and k.serial == here.serial) { - pick = i; - break; - } - if (pick == null and hash != 0 and k.path_hash == hash) pick = i; - } - next = pick orelse next; - }; - const j = p.jumps[next]; - if (j.pane >= MAX_PANES) return; - // A closed file's entry: opened again at its dot, placed as a look - // places it, and the entry is that pane from here on. - if (j.serial == 0) { - const found = p.recentByHash(j.path_hash); - // Deleted since, or Recent has forgotten it: its entries go (the - // first said), and the step goes on to the next place, never - // stuck on one that cannot open. - if (found == null or !recentKeeps(found.?.path)) { - var said: [recent_path_cap + 32]u8 = undefined; - if (found) |e| p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); - const hash = j.path_hash; - var w: usize = 0; - var cur = p.jcur; - for (p.jumps[0..p.njumps], 0..) |k, i| { - if (k.serial == 0 and k.path_hash == hash) { - if (i < p.jcur) cur -= 1; - continue; - } - p.jumps[w] = k; - w += 1; - } - p.njumps = w; - p.jcur = @min(cur, w -| 1); - // The same step again, over the list without them: each - // round takes one file's entries away, so it ends. - return p.jumpBy(delta); - } - const e = found.?; - const from = p.active; - look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.pdf) 0 else @max(1, j.col) }); - const opened = p.panes[p.active] orelse return; - const opened_path = jumpPath(opened) orelse return; - if (!std.mem.eql(u8, opened_path, e.path)) return; // it did not open - if (j.pdf) panes.Pdf.landAt(p, opened, .{ .page = j.line -| 1, .at = j.col }); - // Every entry of that file is the reopened pane from here on, so - // a step onto another of them moves in it rather than reopening. - for (p.jumps[0..p.njumps]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { - k.pane = @intCast(p.active); - k.serial = opened.serial; - }; - p.jcur = next; - return; - } - const pane = p.panes[j.pane] orelse return; - if (pane.serial != j.serial) return; - if (p.panes[p.active]) |source| look.clearNavigationSelection(p, source); - if (p.active != j.pane) look.clearNavigationSelection(p, pane); - p.jcur = next; - if (pane.pdfPath() != null) { - look.focusPaneLine(p, j.pane, .{ .line = 0, .col = 0 }, .keep); - panes.Pdf.landAt(p, pane, .{ .page = j.line -| 1, .at = if (j.pdf) j.col else 0 }); - return; - } - look.focusPaneLine(p, j.pane, .{ .line = j.line, .col = j.col }, .center); - } - /// The rows pane `id`'s tag takes at its width, wrapped as `sync` wraps /// it: what a placement must leave it above its body rows. pub fn wantedTagRows(p: *Pardes, id: usize) u16 { @@ -5320,7 +5121,7 @@ pub const Pardes = struct { p.reapPanes(); if (views.several(p)) views.show(p); layout.compute(p); - p.trackJump(); + Jump.trackJump(p); for (&p.panes, 0..) |*slot, id| { const pane = slot.* orelse continue; // The keyboard is on one pane, or on a header: a tag it has left @@ -5941,26 +5742,6 @@ test "tty output follows only from the bottom" { try std.testing.expect(panes.terminal.scrollbar(sp).offset > live); } -test "the jump list never holds two entries in a row the same, when the one between them goes" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 40 }); - defer p.deinit(); - const pane = try p.setTestFile(std.mem.asBytes(&@as([200][2]u8, @splat("a\n".*)))); - const at: Loc = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; - // A, a pane long closed with no file to keep it, A again, then far on. - p.jumps[0] = at; - p.jumps[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 }; - p.jumps[2] = at; - p.jumps[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 }; - p.njumps = 4; - p.jcur = 3; - pane.body.cur_row = 80; // where you are now: far from both - p.trackJump(); - for (1..p.njumps) |i| try std.testing.expect(!std.meta.eql(p.jumps[i - 1], p.jumps[i])); - try std.testing.expectEqual(@as(usize, 3), p.njumps); - try std.testing.expectEqual(@as(u32, 1), p.jumps[0].line); - try std.testing.expectEqual(@as(u32, 150), p.jumps[1].line); -} - test "Callstack always opens and focuses its own pane, reusing it for another symbol" { if (!lsp.supports.contains(.callstack)) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); @@ -6273,135 +6054,6 @@ test "every allocation failure during boot is a clean OutOfMemory" { try std.testing.checkAllAllocationFailures(std.testing.allocator, bootAndTearDown, .{}); } -test "jump history skips an entry whose pane slot or serial is gone, leaving the selection" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("alpha beta\ngamma delta\nepsilon zeta\n"); - p.njumps = 2; - p.jcur = 1; - p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; - pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); - const selected = pane.body.primaryRange(pane.file.?.content, 0); - for ([_]Loc{ - .{ .pane = 15, .serial = pane.serial, .line = 1, .col = 1 }, - .{ .pane = 0, .serial = pane.serial + 1, .line = 1, .col = 1 }, - }) |missing| { - p.jumps[0] = missing; - p.jumpBy(-1); - try std.testing.expectEqual(@as(usize, 1), p.jcur); - try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0)); - } -} - -/// A session for the side-button jump test: four panes of 100 lines in one -/// column, A (pane 0) and B each on a file of its own, C on A's file, and O -/// an output buffer, no file behind it. `order` is the jumplist, a letter a -/// place, each letter's n-th place its line 10 + 40n: "ABABA" is A10 B10 A50 -/// B50 A90. The keyboard is on the last place, as after jumping there. -const JumpsTest = struct { - p: *Pardes, - ids: [4]usize = .{ 0, 0, 0, 0 }, - - const letters = "ABCO"; - const Way = enum { call, mouse }; - - fn init(order: []const u8) !JumpsTest { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 80 }); - errdefer p.deinit(); - const text = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))); - _ = try p.setTestFile(text); - var t: JumpsTest = .{ .p = p }; - for ([_][]const u8{ "/b.txt", "/test.txt", "" }, 1..) |path, n| { - const id = p.freeSlot().?; - const pane = try p.newDocPane(id); - pane.file = .{ - .path = try p.gpa.dupe(u8, path), - .content = try p.gpa.dupe(u8, text), - .history = try panes.File.History.create(p.gpa), - }; - if (path.len == 0) pane.file.?.output = .{ .from = .search }; - layout.insert(p, 0, n, id); - t.ids[n] = id; - } - layout.compute(p); - var seen: [4]u32 = @splat(0); - for (order, 0..) |letter, i| { - const which = std.mem.indexOfScalar(u8, letters, letter).?; - const pane = p.panes[t.ids[which]].?; - p.jumps[i] = .{ - .pane = @intCast(t.ids[which]), - .serial = pane.serial, - .line = 10 + 40 * seen[which], - .col = 1, - .path_hash = if (letter == 'O') 0 else std.hash.Wyhash.hash(0, pane.file.?.path), - }; - seen[which] += 1; - } - p.njumps = order.len; - p.jcur = order.len - 1; - look.focusPaneLine(p, p.jumps[p.jcur].pane, .{ .line = p.jumps[p.jcur].line, .col = 1 }, .center); - p.sync(); - return t; - } - - /// One step `delta` through `way`: the function, or the mouse's side - /// button pressed and let go. - fn step(t: JumpsTest, way: Way, delta: i32) void { - const back = delta < 0; - switch (way) { - .call => t.p.jumpBy(delta), - .mouse => for ([_]Mouse.Kind{ .press, .release }) |kind| - t.p.update(.{ .mouse = .{ .button = if (back) .back else .forward, .kind = kind, .col = 0, .row = 0 } }), - } - t.p.sync(); - } - - /// Steps `delta` until the list ends that way, saying so; the places - /// landed on, as "A50 A10 B10". - fn walk(t: JumpsTest, way: Way, delta: i32, buf: []u8) ![]const u8 { - var out: std.Io.Writer = .fixed(buf); - for (0..2 * MAX_JUMPS) |_| { - const before = t.p.jcur; - t.step(way, delta); - if (t.p.jcur == before) break; - const pane = t.p.panes[t.p.active].?; - const which = std.mem.indexOfScalar(usize, &t.ids, t.p.active).?; - try out.print("{s}{c}{d}", .{ if (out.end > 0) " " else "", letters[which], pane.body.cur_row + 1 }); - } else return error.NoEnd; - const pane = t.p.panes[t.p.active].?; - const said = pane.msg[0..pane.msg_len]; - try std.testing.expect(std.mem.indexOf(u8, said, if (delta < 0) "nothing further back" else "nothing further on") != null); - return out.buffered(); - } -}; - -// The keys and builtins are test/behaviour/modes.zig's; a mouse's side -// buttons have no /mousein bits. -test "Back and Forward take the same steps from the mouse's side buttons and jumpBy" { - for ([_]JumpsTest.Way{ .call, .mouse }) |way| { - const t = try JumpsTest.init("BABABA"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - try std.testing.expectEqualStrings("A50 A10 B10", try t.walk(way, -1, &buf)); - try std.testing.expectEqualStrings("B50 B90 A90", try t.walk(way, 1, &buf)); - } -} - -test "jump history terminal rectangle follows output and clears on reflow" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12, .tty_only = true }); - defer p.deinit(); - const pane = p.panes[0].?; - p.update(.{ .output = .{ .pane = 0, .bytes = "alpha bravo\r\nsecond line\r\n" } }); - pane.sel[0] = .{ .state = .done, .c0 = 0, .c1 = 4, .r0 = BOX_H, .r1 = BOX_H }; - try edit.capturePointerSelection(p, pane, 0); - try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); - for (0..40) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "more output\r\n" } }); - try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); - panes.terminal.resizeGrid(pane, pane.cols - 1, pane.rows); - try std.testing.expect(pane.pointerSelection(pane.sel[0]) == null); - try std.testing.expectEqual(.none, pane.sel[0].state); -} - test "tagline bands center in their cells and Tagbottom faces the window edge" { const cell_h: u32 = 20; const tagline_h: u32 = 16; diff --git a/test/pdf.zig b/test/pdf.zig index de7afb95..722e28b4 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -2121,7 +2121,7 @@ test "a PDF's jumps: Back after a far jump returns to the exact offset, two plac look.lookAt(p, id, if (pv.page_count > 3000) ":3000" else ":4"); try jumpFrame(p); try std.testing.expect(pv.page >= 3); - p.jumpBy(-1); + pardes.Jump.jumpBy(p, -1); try jumpFrame(p); try std.testing.expectEqual(@as(usize, 1), pv.page); try std.testing.expectApproxEqAbs(near, pv.document_scroll_y, 0.5); @@ -2157,12 +2157,12 @@ test "a PDF's jumps: JumpScope file keeps to the PDF, and a closed PDF reopens a p.active = doc; _ = try stripAtPlace(p, pane, 3, 0.5); // Back from the PDF keeps to it: its own earlier place, not the text. - p.jumpBy(-1); + pardes.Jump.jumpBy(p, -1); try jumpFrame(p); try std.testing.expectEqual(doc, p.active); try std.testing.expectApproxEqAbs(first, pane.pdf.?.document_scroll_y, 0.5); // Closed, its entries stay; Back from the text reopens it at the place. - p.jumpBy(1); + pardes.Jump.jumpBy(p, 1); try jumpFrame(p); try p.removePane(doc, null); try jumpFrame(p); @@ -2171,7 +2171,7 @@ test "a PDF's jumps: JumpScope file keeps to the PDF, and a closed PDF reopens a try std.testing.expect(closed); var tries: usize = 0; while (tries < 4) : (tries += 1) { - p.jumpBy(-1); + pardes.Jump.jumpBy(p, -1); try jumpFrame(p); if (p.panes[p.active].?.pdf != null) break; } |
