diff options
Diffstat (limited to 'core/Trail.zig')
| -rw-r--r-- | core/Trail.zig | 198 |
1 files changed, 178 insertions, 20 deletions
diff --git a/core/Trail.zig b/core/Trail.zig index cb5dedf8..9223d1d3 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -18,6 +18,7 @@ const look = @import("look.zig"); const Pardes = pardes.Pardes; const Pane = panes.Pane; const Spot = look.Spot; +const TagState = @import("TagState.zig"); const Trail = @This(); @@ -164,6 +165,32 @@ pub fn go(p: *Pardes, id: usize, i: u8) !usize { return show(p, id, s.place.path.slice(), .{ .line = s.place.line, .col = s.place.col }); } +/// A list the pane's newest step came through (§6.2: a picker's Enter, +/// Pick.zig): its word before the step's place. +pub fn through(t: *Trail, s: Step) void { + if (t.cur >= t.n or t.steps[t.cur] != .place) return; + if (t.n == max_steps) { + const first: usize = @intFromBool(t.steps[0] == .owner); + if (t.cur <= first) return; + std.mem.copyForwards(Step, t.steps[first .. max_steps - 1], t.steps[first + 1 ..]); + t.n -= 1; + t.cur -= 1; + } + std.mem.copyBackwards(Step, t.steps[t.cur + 1 .. t.n + 1], t.steps[t.cur..t.n]); + t.steps[t.cur] = s; + t.n += 1; + t.cur += 1; +} + +/// Reading pane `id`'s current step replaced by `path` at `at`, no step +/// pushed (a list's `n`, §5.6); its slot after it, as `step`'s. +pub fn replaceStep(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { + const to = placeOf(path, at.line, at.col) orelse return error.NameTooLong; + const t = &p.panes[id].?.trail; + if (t.cur < t.n) t.steps[t.cur] = to; + return show(p, id, path, at); +} + /// Pane `id` showing `path` at `at`, the keyboard on it: its caret moved /// in the file it shows, or a new pane on `path` in its place, its walk /// with it. A line past the new file's end is a miss that changes nothing. @@ -172,6 +199,7 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) { look.clearNavigationSelection(p, pane); look.focusPaneLine(p, id, at, .center); + lift(p, id); return id; }; const free = p.freeSlot() orelse return error.NoPaneSlots; @@ -188,7 +216,7 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { try panes.deinitPane(p, pane); layout.replacePane(p, id, free); p.panes[id] = null; - layout.compute(p); + lift(p, free); look.focusPaneLine(p, free, at, .keep); // Its line half the body's rows down, as a look's new pane has it. const rows = @max(1, p.view().rects[free].h -| pardes.BOX_H); @@ -196,6 +224,17 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { return free; } +/// Pane `id`'s tag after a step may take more rows than it had (its walk's +/// words, a longer name): it is given them from its column, as a step that +/// wraps tags gives them (layout.restoreMinimaFrom), the others keeping +/// their own minimum. +fn lift(p: *Pardes, id: usize) void { + const now = p.view().rects; + var floor = layout.minima(p); + floor[id] = 0; + layout.restoreMinimaFrom(p, &now, &floor); +} + /// `s`, a search's or a recipe's word, the pane's one of its kind (§7.3, /// §7.5): the one it had goes, and it is the newest back step. pub fn setWord(t: *Trail, s: Step) void { @@ -239,7 +278,11 @@ pub fn keep(pane: *Pane) void { } /// An edit of file `f`: the panes showing it are held from now on (§6.5). +/// Only where a pane could read: a session whose jumps walk, a file +/// (isFile), so that an edit anywhere else (an output's, a 9P append's +/// under `Jumps new-pane`) costs nothing past these checks. pub fn edited(p: *Pardes, f: *const panes.File.State) void { + if (p.settings.jumps != .walk or f.output != null or f.listing != null or f.pager or f.mini != null) return; for (p.panes) |slot| { const pane = slot orelse continue; if (pane.trail.held) continue; @@ -272,9 +315,9 @@ pub const Part = enum { back, forward }; /// Step `i`'s word: `‹@p3`, `rank.zig:14`, a list's, search's or recipe's /// own text; `·` when it is folded. -fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 { +fn wordOf(t: *const Trail, i: usize, whole: usize, buf: *[max_text + 16]u8) []const u8 { const s = &t.steps[i]; - if (s.* != .owner and i < t.cur and t.cur - i > shown_steps) return "·"; + if (s.* != .owner and i < t.cur and t.cur - i > whole) return "·"; // The step itself, not a copy: a word's text is the step's own bytes. return switch (s.*) { .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits @@ -294,42 +337,52 @@ fn range(t: *const Trail, part: Part) [2]usize { } /// The back steps' words, each with the separator after it (before the -/// path), or the forward steps', each with one before it (after the path). -pub fn write(t: *const Trail, part: Part, w: *std.Io.Writer) std.Io.Writer.Error!void { +/// path), or the forward steps', each with one before it (after the path): +/// the newest `whole` back steps whole, older ones `·` (`folded`). +pub fn write(t: *const Trail, part: Part, whole: usize, w: *std.Io.Writer) std.Io.Writer.Error!void { var buf: [max_text + 16]u8 = undefined; const r = range(t, part); for (r[0]..r[1]) |i| switch (part) { - .back => try w.print("{s}" ++ sep, .{wordOf(t, i, &buf)}), - .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, &buf)}), + .back => try w.print("{s}" ++ sep, .{wordOf(t, i, whole, &buf)}), + .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, whole, &buf)}), }; } /// The bytes `write` writes for `part`. -pub fn bytes(t: *const Trail, part: Part) usize { +pub fn bytes(t: *const Trail, part: Part, whole: usize) usize { var buf: [max_text + 16]u8 = undefined; var n: usize = 0; const r = range(t, part); - for (r[0]..r[1]) |i| n += wordOf(t, i, &buf).len + sep.len; + for (r[0]..r[1]) |i| n += wordOf(t, i, whole, &buf).len + sep.len; return n; } -/// Where back step `i`'s word is in what `write` writes for the back steps. -pub fn spanOf(t: *const Trail, i: usize) [2]usize { +/// The back steps pane `pane`'s tag writes whole: `shown_steps` at rest, +/// the newest alone while its tag is out of rest (SPEC §3.4.3), so a +/// state's head and the band's spines keep their room. +pub fn folded(p: *const Pardes, pane: *const Pane) usize { + const host = TagState.hostOf(p) orelse return shown_steps; + return if (p.panes[host] == pane) 1 else shown_steps; +} + +/// Where back step `i`'s word is in what `write` writes for the back +/// steps, the newest `whole` whole. +pub fn spanOf(t: *const Trail, i: usize, whole: usize) [2]usize { var buf: [max_text + 16]u8 = undefined; var x: usize = 0; - for (0..i) |j| x += wordOf(t, j, &buf).len + sep.len; - return .{ x, x + wordOf(t, i, &buf).len }; + for (0..i) |j| x += wordOf(t, j, whole, &buf).len + sep.len; + return .{ x, x + wordOf(t, i, whole, &buf).len }; } /// The step whose word in `part` is at byte `off` of what `write` wrote /// for it, null on a separator or past them. -pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { +pub fn stepAt(t: *const Trail, part: Part, whole: usize, off: usize) ?u8 { var buf: [max_text + 16]u8 = undefined; var x: usize = 0; const r = range(t, part); for (r[0]..r[1]) |i| { if (part == .forward) x += sep.len; - const n = wordOf(t, i, &buf).len; + const n = wordOf(t, i, whole, &buf).len; if (off >= x and off < x + n) return @intCast(i); x += n; if (part == .back) x += sep.len; @@ -338,8 +391,9 @@ pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { } /// Look on step `i` of pane `id`'s trail (§6.3): a place is gone back or -/// forward to, the owner focused, a search lit again, a recipe's -/// selections put back (Prompt.zig); a list is the pickers' (not yet: said). +/// forward to, the owner focused, a list unrolled again as it was +/// (Pick.reopen), a search lit again, a recipe's selections put back +/// (Prompt.zig). pub fn lookStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { @@ -347,21 +401,21 @@ pub fn lookStep(p: *Pardes, id: usize, i: u8) void { .owner => |serial| if (p.paneBySerial(serial)) |owner| { p.view().active = owner; } else p.reportFailure(id, "walk: its owner is closed"), + .list => |w| pardes.Pick.reopen(p, id, w.index), .search => pardes.Prompt.lookWord(p, id), .recipe => pardes.Prompt.lookRecipe(p, id), - .list => p.setMessage(.at(id), "walk: a list is not walked yet"), } } /// Exec on step `i` (§6.3): a place and the owner as Look; a list runs its -/// source again, a search searches again, a recipe replays (not yet). +/// source again, a search searches again, a recipe replays. pub fn execStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { .place, .owner => lookStep(p, id, i), + .list => |w| pardes.Pick.rerun(p, id, w.index), .search => pardes.Prompt.execWord(p, id), .recipe => pardes.Prompt.execRecipe(p, id), - .list => p.setMessage(.at(id), "walk: a list is not run again yet"), } } @@ -629,6 +683,51 @@ test "the session of pick-ui §6.7: four jumps across three files, back by a cli try std.testing.expectEqual(owner, p.panes[fresh].?.trail.steps[0].owner); } +test "the walk drawn with a picker's list and the notice row (walk UI G 2e, SPEC §3.7, §6.7): a list handed to a held pane lands in its reader, its word in the reader's trail before the place, the reader's tag in the reading tint with Keep, the held pane's not, n walking the list, the notice row at the foot saying where the caret is" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(40); + defer f.deinit(); + const p = f.p; + p.messages.live_len = 0; + const owner = p.panes[f.held].?.serial; + var buf: [4096 + 128]u8 = undefined; + var want: [4096 + 128]u8 = undefined; + // g d from the held pane: its reader, made below it. + f.look(f.held, "rank.zig", 12); + const r = p.view().active; + // A list asked from the held pane (SPC e's places, a server's answer): + // its Enter lands in the reader, nothing made. + const rows = try std.fmt.bufPrint(&buf, "{s}/place.zig:8:1 of\n{s}/place.zig:40:1 Span\n", .{ f.dir, f.dir }); + pardes.Pick.open(p, f.held, .list, "Refs", "", rows); + try std.testing.expectEqual(p.panes[r].?.serial, p.view().tag_state.host); + pardes.Pick.landHere(p); + p.sync(); + // The reader stepped (to another file: a pane of its own, its owner's + // word kept), no pane more. + var n: usize = 0; + for (p.panes) |slot| n += @intFromBool(slot != null); + try std.testing.expectEqual(@as(usize, 3), n); + const rd = p.view().active; + try std.testing.expect(rd != f.held and p.panes[r] == null); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[rd].?), try std.fmt.bufPrint(&want, "‹@p{d} › rank.zig:12 › Refs 1/2 › {s}/place.zig Keep ", .{ owner, f.dir }))); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + // The reader's tag in the reading tint, the held one's not; the notice + // row, nothing said, where the reader's caret is. + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().tag_reading_bg }, try tagGroundOf(p, &arena, rd)); + try std.testing.expect(!std.meta.eql(pardes.Color{ .rgb = p.chromeTheme().tag_reading_bg }, try tagGroundOf(p, &arena, f.held))); + const s = try p.render(arena.allocator()); + const foot = s.region(.notice_row, pardes.Region.none).?.rect; + var row: std.ArrayList(u8) = .empty; + for (0..s.cols) |x| try row.appendSlice(arena.allocator(), s.at(@intCast(x), foot.y).grapheme()); + try std.testing.expect(std.mem.indexOf(u8, row.items, try std.fmt.bufPrint(&want, "{s}/place.zig:8", .{f.dir})) != null); + // n: the list's next place, in place of the current step. + typeKeys(p, "n"); + p.sync(); + try std.testing.expectEqual(@as(i32, 39), p.panes[rd].?.body.cur_row); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, p.panes[rd].?), "› Refs 2/2 ›") != null); +} + test "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); @@ -700,3 +799,62 @@ test "closing a held pane closes its reader; a kept reader stays; typed into, a try std.testing.expect(p.paneBySerial(fresh_serial) == null); try std.testing.expect(p.panes[r] != null); } + +test "Jumps new-pane jumps as before the walk: a new pane for a file no pane shows, under the asking one, the one showing it focused, nothing stepped" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + p.settings.jumps = .@"new-pane"; + var at: [64]u8 = undefined; + var open: usize = 0; + for (p.panes) |slot| open += @intFromBool(slot != null); + // From the held pane and from the reading one: a new pane each time. + f.look(f.held, "rank.zig", 12); + const rank = p.view().active; + try std.testing.expectEqualStrings("rank.zig:12", f.at(&at)); + f.look(rank, "place.zig", 8); + try std.testing.expect(p.view().active != rank and p.panes[rank] != null); + try std.testing.expectEqualStrings("place.zig:8", f.at(&at)); + // A file a pane shows: that pane, focused; nothing new, no walk. + f.look(p.view().active, "rank.zig", 30); + try std.testing.expectEqual(rank, p.view().active); + try std.testing.expectEqualStrings("rank.zig:30", f.at(&at)); + var now: usize = 0; + for (p.panes) |slot| if (slot) |pane| { + now += 1; + try std.testing.expectEqual(@as(u8, 0), pane.trail.n); + }; + try std.testing.expectEqual(open + 2, now); +} + +test "Enter on a word of the walk, the keyboard in the tag, goes back to its step as a click does; out of rest the walk folds to its newest word" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + const owner = p.panes[f.held].?.serial; + for ([_]usize{ 10, 20, 30 }) |line| f.look(f.held, "rank.zig", line); + const r = p.view().active; + const pane = p.panes[r].?; + var buf: [4096 + 128]u8 = undefined; + var at: [64]u8 = undefined; + const rest = try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:10 › rank.zig:20 › {s}/rank.zig Keep", .{ owner, f.dir }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), rest)); + // SPC: out of rest, the older steps are `·`, the owner and the newest + // whole; back at rest, as they were. + p.update(.{ .key = .{ .cp = ' ' } }); + var folded_buf: [4096 + 128]u8 = undefined; + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&folded_buf, "‹@p{d} › · › rank.zig:20 › {s}/rank.zig", .{ owner, f.dir }))); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), rest)); + // The keyboard in the tag, its cursor on `rank.zig:10`: Enter goes back + // to that step, the keyboard in the body again. + pane.focus = .tag; + pane.tag.cur_row = 0; + pane.tag.cur_col = @intCast(std.mem.indexOf(u8, rest, "rank.zig:10").? + 2); + p.update(.{ .key = .{ .cp = pardes.Key.enter } }); + p.sync(); + try std.testing.expectEqualStrings("rank.zig:10", f.at(&at)); + try std.testing.expect(pane.focus == .body); +} |
