summaryrefslogtreecommitdiff
path: root/core/Trail.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 19:16:35 -0300
committerGabriel Schneider <[email protected]>2026-11-24 19:16:35 -0300
commit08524143c84c069a80861895fafd0b7411a201f5 (patch)
treee48aa971e9d31b038c428d7f8fcd9e1c9f6864bc /core/Trail.zig
parent7e7d9569a008124cb7ea01b6720a8d5963b0a42f (diff)
downloadpardes-08524143c84c069a80861895fafd0b7411a201f5.tar.gz
pardes-08524143c84c069a80861895fafd0b7411a201f5.zip
walk (pick-ui SPEC §6.1, §6.2, item 2 of the Walk UI line B): a jump goes through look.open(from, path, at, how), the one way a Look at an address, a definition (lsp/Ask.zig), an error's place, a list's row and a closed jump-list entry land, for D's pickers too: under `Jumps walk` a held pane showing the file is focused and nothing replaced, the asking pane showing it moves in it (a step when it reads), a reading pane steps in place and a held one steps its one reader (Trail.readerOf: the reading pane whose trail starts with its owner), made on its first jump under it with half its rows (layout.placeNew .reader: unless that leaves it under reader_keeps 8 body rows; then the bottom of the next column, the previous from the last); a step to another file is a new pane in the reader's place in every view (layout.replacePane), its walk with it, the old one closed as a pane is (Recent keeps its dot) but kept by no Store record; a place on the trail is gone back or forward to, a jump to where it is takes no step, a path over max_text opens a pane as new-pane does; `Jumps new-pane` (the default) runs the code it always ran (openNew is lookAt's new-pane branch moved, and the .pane branch is untouched); TEST FIRST: Trail.zig "a jump from a held pane steps its reader ...", "a Look from inside the reader at a file its held pane shows focuses the held pane and replaces nothing", "the reader of a held pane too short to halve goes to the bottom of the next column ..."; core-test 996/1162 (166 skipped), pane-test 183/184, behaviour-test 277/277
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/Trail.zig')
-rw-r--r--core/Trail.zig252
1 files changed, 252 insertions, 0 deletions
diff --git a/core/Trail.zig b/core/Trail.zig
index fc163751..63a57c2c 100644
--- a/core/Trail.zig
+++ b/core/Trail.zig
@@ -74,6 +74,126 @@ pub fn reading(p: *const Pardes, pane: *const Pane) bool {
return p.settings.jumps == .walk and isFile(pane) and !pane.trail.held;
}
+/// The reading pane that is held pane `owner`'s reader, if it has one.
+pub fn readerOf(p: *const Pardes, owner: *const Pane) ?usize {
+ for (p.panes, 0..) |slot, id| {
+ const r = slot orelse continue;
+ if (r == owner or !isFile(r) or r.trail.held or r.trail.n == 0) continue;
+ if (r.trail.steps[0] == .owner and r.trail.steps[0].owner == owner.serial) return id;
+ }
+ return null;
+}
+
+/// A new reader's trail: its owner, then where it opened.
+pub fn reader(owner: u32, path: []const u8, at: Spot) Trail {
+ var t: Trail = .{};
+ push(&t, .{ .owner = owner });
+ if (placeOf(path, at.line, at.col)) |s| push(&t, s);
+ t.cur = t.n - 1;
+ return t;
+}
+
+/// A place as a step, null when its path does not fit one.
+fn placeOf(path: []const u8, line: usize, col: usize) ?Step {
+ if (path.len > max_text) return null;
+ var at: Place = .{ .line = std.math.lossyCast(u32, line), .col = std.math.lossyCast(u32, col) };
+ at.path.set(path);
+ return .{ .place = at };
+}
+
+/// Where `pane` is now, as a step.
+fn here(pane: *const Pane) ?Step {
+ const f = pane.file orelse return null;
+ return placeOf(f.path, @as(usize, @intCast(@max(0, pane.body.cur_row))) + 1, @as(usize, @intCast(@max(0, pane.body.cur_col))) + 1);
+}
+
+fn samePlace(s: Step, path: []const u8, line: usize) bool {
+ return s == .place and s.place.line == line and std.mem.eql(u8, s.place.path.slice(), path);
+}
+
+/// `s` as the newest step, the oldest that is not the owner dropped when
+/// the trail is full.
+fn push(t: *Trail, s: Step) void {
+ if (t.n == max_steps) {
+ const first: usize = @intFromBool(t.steps[0] == .owner);
+ std.mem.copyForwards(Step, t.steps[first .. max_steps - 1], t.steps[first + 1 ..]);
+ t.n -= 1;
+ t.cur -|= 1;
+ }
+ t.steps[t.n] = s;
+ t.n += 1;
+}
+
+/// Step reading pane `id` to `path` at `at` (§6.2): a place already on its
+/// trail is gone back or forward to, never held twice; anything else is a
+/// new step, which drops the forward steps. The pane's slot after it: a
+/// step to another file is a new pane in its place (`replace`).
+pub fn step(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize {
+ const pane = p.panes[id].?;
+ const t = &pane.trail;
+ // Where it is already: no step.
+ if (here(pane)) |was| if (samePlace(was, path, at.line)) return show(p, id, path, at);
+ for (t.steps[0..t.cur], 0..) |s, i| if (samePlace(s, path, at.line)) return go(p, id, @intCast(i));
+ if (t.n > t.cur + 1) for (t.steps[t.cur + 1 .. t.n], t.cur + 1..) |s, j| if (samePlace(s, path, at.line)) return go(p, id, @intCast(j));
+ const to = placeOf(path, at.line, at.col) orelse return error.NameTooLong;
+ // Where it was: the current step's place, or a first one.
+ if (here(pane)) |was| {
+ if (t.n > t.cur) {
+ t.n = t.cur + 1;
+ t.steps[t.cur] = was;
+ } else push(t, was);
+ } else t.n = t.cur;
+ push(t, to);
+ t.cur = t.n - 1;
+ return show(p, id, path, at);
+}
+
+/// Go to step `i` of reading pane `id`'s trail, back or forward (§6.4):
+/// where it is now becomes the step it leaves, the ones between stay.
+pub fn go(p: *Pardes, id: usize, i: u8) !usize {
+ const pane = p.panes[id].?;
+ const t = &pane.trail;
+ if (i >= t.n or t.steps[i] != .place) return id;
+ const s = t.steps[i];
+ if (t.n > t.cur) if (here(pane)) |was| {
+ t.steps[t.cur] = was;
+ };
+ t.cur = i;
+ return show(p, id, s.place.path.slice(), .{ .line = s.place.line, .col = s.place.col });
+}
+
+/// 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.
+fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize {
+ const pane = p.panes[id].?;
+ if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) {
+ look.clearNavigationSelection(p, pane);
+ look.focusPaneLine(p, id, at, .center);
+ return id;
+ };
+ const free = p.freeSlot() orelse return error.NoPaneSlots;
+ const nt = try panes.File.open(p, free, path, at.line);
+ // Refused: closed at the step's end, never placed (Pardes.unplaced).
+ errdefer {
+ p.unplaced.set(free);
+ nt.vweight = 0;
+ }
+ if (at.line > panes.File.nlines(p.gpa, &nt.file.?)) return error.LineMissing;
+ nt.trail = pane.trail;
+ // Before the layout gives its place away: its file is told closed
+ // while it is still in its slot.
+ try panes.deinitPane(p, pane);
+ layout.replacePane(p, id, free);
+ p.panes[id] = null;
+ layout.compute(p);
+ 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);
+ panes.File.setTop(nt, panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, rows / 2)).to);
+ return free;
+}
+
/// Pane `id` held (§6.5): Keep, or an edit. It no longer walks, so the
/// walk leaves its trail (places, lists, its owner, forward steps); words
/// about its own text (a search, a recipe) stay. The next jump from its
@@ -164,3 +284,135 @@ test "Keep holds a reading pane; a scratch and a listing are never reading" {
const listing = p.panes[p.view().active].?;
try std.testing.expect(listing.file.?.listing != null and !reading(p, listing));
}
+
+/// A session over three files of 100 lines in a directory of its own,
+/// search.zig, rank.zig and place.zig; search.zig open and edited (held)
+/// in pane `held`, under the session's first pane.
+const Fixture = struct {
+ p: *Pardes,
+ tmp: std.testing.TmpDir,
+ dir_buf: [4096]u8 = undefined,
+ dir: []const u8 = "",
+ held: usize = 0,
+
+ fn init(f: *Fixture, rows: u16) !void {
+ f.tmp = std.testing.tmpDir(.{});
+ errdefer f.tmp.cleanup();
+ var text: [100 * 9]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&text);
+ for (1..101) |n| try w.print("line {d}\n", .{n});
+ for ([_][]const u8{ "search.zig", "rank.zig", "place.zig" }) |name|
+ try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() });
+ f.dir = f.dir_buf[0..try f.tmp.dir.realPath(std.testing.io, &f.dir_buf)];
+ f.p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = rows });
+ errdefer f.p.deinit();
+ _ = try f.p.setTestFile("scratch\n");
+ f.p.settings.jumps = .@"new-pane";
+ f.look(0, "search.zig", 88);
+ f.p.settings.jumps = .walk;
+ f.held = f.p.view().active;
+ typeKeys(f.p, "ix");
+ f.p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ f.p.sync();
+ }
+
+ fn deinit(f: *Fixture) void {
+ f.p.deinit();
+ f.tmp.cleanup();
+ }
+
+ /// A Look from pane `id` at `name:line` in the fixture's directory.
+ fn look(f: *Fixture, id: usize, name: []const u8, line: usize) void {
+ var buf: [4096 + 64]u8 = undefined;
+ pardes.look.lookAt(f.p, id, std.fmt.bufPrint(&buf, "{s}/{s}:{d}", .{ f.dir, name, line }) catch unreachable);
+ f.p.sync();
+ }
+
+ fn active(f: *Fixture) *Pane {
+ return f.p.panes[f.p.view().active].?;
+ }
+
+ /// The active pane's file's name and line, as `rank.zig:12`.
+ fn at(f: *Fixture, buf: []u8) []const u8 {
+ const pane = f.active();
+ return std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(pane.file.?.path), pane.body.cur_row + 1 }) catch unreachable;
+ }
+};
+
+test "a jump from a held pane steps its reader, made under it with half its rows; the reader walks in place, and a place on its trail is gone back to, never held twice" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ const held = f.p.panes[f.held].?;
+ try std.testing.expect(!reading(f.p, held));
+ const before = f.p.view().rects[f.held];
+ var buf: [64]u8 = undefined;
+ // g d on rank: its reader, under it, half its rows.
+ f.look(f.held, "rank.zig", 12);
+ const r1 = f.p.view().active;
+ try std.testing.expect(r1 != f.held);
+ try std.testing.expectEqualStrings("rank.zig:12", f.at(&buf));
+ try std.testing.expectEqual(held.serial, f.active().trail.steps[0].owner);
+ const col = layout.findPane(f.p, f.held).?;
+ try std.testing.expectEqual(col.col, layout.findPane(f.p, r1).?.col);
+ try std.testing.expectEqual(col.idx + 1, layout.findPane(f.p, r1).?.idx);
+ try std.testing.expect(f.p.view().rects[f.held].h <= before.h / 2 + 1);
+ // Look at place.zig:8 in the reader, from line 14: the reader walks.
+ typeKeys(f.p, "2j");
+ f.look(r1, "place.zig", 8);
+ const r2 = f.p.view().active;
+ try std.testing.expect(f.p.panes[r1] == null or r2 == r1);
+ try std.testing.expectEqualStrings("place.zig:8", f.at(&buf));
+ try std.testing.expectEqual(layout.findPane(f.p, f.held).?.idx + 1, layout.findPane(f.p, r2).?.idx);
+ // g d on Span, from line 9: one more step in the same file.
+ typeKeys(f.p, "j");
+ f.look(r2, "place.zig", 40);
+ try std.testing.expectEqual(r2, f.p.view().active);
+ try std.testing.expectEqualStrings("place.zig:40", f.at(&buf));
+ const t = &f.active().trail;
+ try std.testing.expectEqual(@as(u8, 4), t.n);
+ try std.testing.expectEqual(@as(u8, 3), t.cur);
+ try std.testing.expectEqual(@as(u32, 14), t.steps[1].place.line);
+ try std.testing.expectEqual(@as(u32, 9), t.steps[2].place.line);
+ // The next jump from the held pane lands in the same reader.
+ f.look(f.held, "rank.zig", 30);
+ const r3 = f.p.view().active;
+ try std.testing.expectEqualStrings("rank.zig:30", f.at(&buf));
+ try std.testing.expectEqual(held.serial, f.active().trail.steps[0].owner);
+ var readers: usize = 0;
+ for (f.p.panes) |slot| if (slot) |pane| {
+ readers += @intFromBool(pane.trail.n > 0 and pane.trail.steps[0] == .owner);
+ };
+ try std.testing.expectEqual(@as(usize, 1), readers);
+ // A Look at a place on its trail goes back to it: nothing pushed.
+ f.look(r3, "rank.zig", 14);
+ try std.testing.expectEqualStrings("rank.zig:14", f.at(&buf));
+ try std.testing.expectEqual(@as(u8, 1), f.active().trail.cur);
+ try std.testing.expectEqual(@as(u8, 5), f.active().trail.n);
+}
+
+test "a Look from inside the reader at a file its held pane shows focuses the held pane and replaces nothing" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ f.look(f.held, "rank.zig", 12);
+ const reader_id = f.p.view().active;
+ const reader_pane = f.active();
+ f.look(reader_id, "search.zig", 89);
+ try std.testing.expectEqual(f.held, f.p.view().active);
+ try std.testing.expectEqual(@as(i32, 88), f.active().body.cur_row);
+ try std.testing.expectEqual(reader_pane, f.p.panes[reader_id].?);
+ try std.testing.expectEqualStrings("rank.zig", std.Io.Dir.path.basename(reader_pane.file.?.path));
+}
+
+test "the reader of a held pane too short to halve goes to the bottom of the next column, of the previous one from the last" {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(24);
+ defer f.deinit();
+ const col = layout.findPane(f.p, f.held).?.col;
+ try std.testing.expect(f.p.view().rects[f.held].h / 2 < f.p.panes[f.held].?.tag_rows + layout.reader_keeps);
+ try std.testing.expect(layout.insertColumn(f.p, col, false) != null);
+ f.look(f.held, "rank.zig", 12);
+ const reader_id = f.p.view().active;
+ try std.testing.expectEqual(col + 1, layout.findPane(f.p, reader_id).?.col);
+}