diff options
Diffstat (limited to 'src/Jump.zig')
| -rw-r--r-- | src/Jump.zig | 148 |
1 files changed, 75 insertions, 73 deletions
diff --git a/src/Jump.zig b/src/Jump.zig index 2220f3c8..0d82d104 100644 --- a/src/Jump.zig +++ b/src/Jump.zig @@ -81,7 +81,7 @@ const JumpsTest = struct { for (order, 0..) |letter, i| { const which = std.mem.indexOfScalar(u8, letters, letter).?; const pane = p.panes[t.ids[which]].?; - p.jumps.list[i] = .{ + p.view().jumps.list[i] = .{ .pane = @intCast(t.ids[which]), .serial = pane.serial, .line = 10 + 40 * seen[which], @@ -90,9 +90,9 @@ const JumpsTest = struct { }; seen[which] += 1; } - p.jumps.n = order.len; - p.jumps.cur = order.len - 1; - look.focusPaneLine(p, p.jumps.list[p.jumps.cur].pane, .{ .line = p.jumps.list[p.jumps.cur].line, .col = 1 }, .center); + p.view().jumps.n = order.len; + p.view().jumps.cur = order.len - 1; + look.focusPaneLine(p, p.view().jumps.list[p.view().jumps.cur].pane, .{ .line = p.view().jumps.list[p.view().jumps.cur].line, .col = 1 }, .center); p.sync(); return t; } @@ -114,14 +114,14 @@ const JumpsTest = struct { 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.jumps.cur; + const before = t.p.view().jumps.cur; t.step(way, delta); - if (t.p.jumps.cur == before) break; - const pane = t.p.panes[t.p.active].?; - const which = std.mem.indexOfScalar(usize, &t.ids, t.p.active).?; + if (t.p.view().jumps.cur == before) break; + const pane = t.p.panes[t.p.view().active].?; + const which = std.mem.indexOfScalar(usize, &t.ids, t.p.view().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 pane = t.p.panes[t.p.view().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(); @@ -145,15 +145,16 @@ fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool { 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); + const body = @max(1, p.view().rects[b.pane].h -| BOX_H); return @max(a.line, b.line) - @min(a.line, b.line) <= body; } pub fn trackJump(p: *Pardes) void { + const v = p.view(); var w: usize = 0; var cur: usize = 0; - for (p.jumps.list[0..p.jumps.n], 0..) |j, i| { - if (i == p.jumps.cur) cur = w; // survives -> lands at w; dies -> the next one does + for (v.jumps.list[0..p.view().jumps.n], 0..) |j, i| { + if (i == v.jumps.cur) 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 @@ -174,51 +175,51 @@ pub fn trackJump(p: *Pardes) void { } // 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.list[w - 1], kept)) { - if (i == p.jumps.cur) cur = w - 1; + if (w > 0 and std.meta.eql(v.jumps.list[w - 1], kept)) { + if (i == v.jumps.cur) cur = w - 1; continue; } - p.jumps.list[w] = kept; + v.jumps.list[w] = kept; w += 1; } - p.jumps.n = w; - p.jumps.cur = @min(cur, w -| 1); + v.jumps.n = w; + v.jumps.cur = @min(cur, w -| 1); - const pane = p.panes[p.active] orelse return; + const pane = p.panes[v.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.jumps.force; - p.jumps.force = false; + const force = v.jumps.force; + v.jumps.force = false; const pdf_place = panes.Pdf.place(pane); const now: Loc = .{ - .pane = @intCast(p.active), + .pane = @intCast(v.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.jumps.n > 0 and samePlace(p, p.jumps.list[p.jumps.cur], now)) { - p.jumps.list[p.jumps.cur] = now; // same visit; the entry IS where you are + if (!force and v.jumps.n > 0 and samePlace(p, v.jumps.list[v.jumps.cur], now)) { + v.jumps.list[v.jumps.cur] = 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.jumps.n > 0) p.jumps.n = p.jumps.cur + 1; - if (!force and p.jumps.n >= 2 and samePlace(p, p.jumps.list[p.jumps.n - 2], now)) { - p.jumps.list[p.jumps.n - 2] = p.jumps.list[p.jumps.n - 1]; - p.jumps.list[p.jumps.n - 1] = now; - p.jumps.cur = p.jumps.n - 1; + if (v.jumps.n > 0) v.jumps.n = v.jumps.cur + 1; + if (!force and v.jumps.n >= 2 and samePlace(p, v.jumps.list[v.jumps.n - 2], now)) { + v.jumps.list[v.jumps.n - 2] = v.jumps.list[v.jumps.n - 1]; + v.jumps.list[v.jumps.n - 1] = now; + v.jumps.cur = v.jumps.n - 1; return; } - if (p.jumps.n == MAX_JUMPS) { - std.mem.copyForwards(Loc, p.jumps.list[0 .. MAX_JUMPS - 1], p.jumps.list[1..]); - p.jumps.n -= 1; + if (v.jumps.n == MAX_JUMPS) { + std.mem.copyForwards(Loc, v.jumps.list[0 .. MAX_JUMPS - 1], v.jumps.list[1..]); + v.jumps.n -= 1; } - p.jumps.list[p.jumps.n] = now; - p.jumps.cur = p.jumps.n; - p.jumps.n += 1; + v.jumps.list[v.jumps.n] = now; + v.jumps.cur = v.jumps.n; + v.jumps.n += 1; } /// One step through the jumplist, older for a negative `delta`, newer @@ -233,22 +234,23 @@ pub fn trackJump(p: *Pardes) void { /// 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 v = p.view(); const older = delta < 0; // At an end of the list: said, not a step that silently does nothing. - if (p.jumps.n == 0 or (older and p.jumps.cur == 0) or (!older and p.jumps.cur + 1 >= p.jumps.n)) - return p.setMessage(p.active, if (older) "Back: nothing further back" else "Forward: nothing further on"); - var next = if (older) p.jumps.cur - 1 else p.jumps.cur + 1; + if (v.jumps.n == 0 or (older and v.jumps.cur == 0) or (!older and v.jumps.cur + 1 >= v.jumps.n)) + return p.setMessage(v.active, if (older) "Back: nothing further back" else "Forward: nothing further on"); + var next = if (older) v.jumps.cur - 1 else v.jumps.cur + 1; // A terminal or a scratch has no file to keep to: the step is global. - if (p.panes[p.active]) |here| if (Pardes.recentPlace(here)) |place| if (place.path.len > 0) { + if (p.panes[v.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.jumps.cur; - while (if (older) i > 0 else i + 1 < p.jumps.n) { + var i = v.jumps.cur; + while (if (older) i > 0 else i + 1 < v.jumps.n) { i = if (older) i - 1 else i + 1; - const k = p.jumps.list[i]; - if (k.pane == p.active and k.serial == here.serial) { + const k = v.jumps.list[i]; + if (k.pane == v.active and k.serial == here.serial) { pick = i; break; } @@ -256,7 +258,7 @@ pub fn jumpBy(p: *Pardes, delta: i32) void { } next = pick orelse next; }; - const j = p.jumps.list[next]; + const j = v.jumps.list[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. @@ -267,45 +269,45 @@ pub fn jumpBy(p: *Pardes, delta: i32) void { // 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"); + if (found) |e| p.setMessage(v.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.jumps.cur; - for (p.jumps.list[0..p.jumps.n], 0..) |k, i| { + var cur = v.jumps.cur; + for (v.jumps.list[0..p.view().jumps.n], 0..) |k, i| { if (k.serial == 0 and k.path_hash == hash) { - if (i < p.jumps.cur) cur -= 1; + if (i < v.jumps.cur) cur -= 1; continue; } - p.jumps.list[w] = k; + v.jumps.list[w] = k; w += 1; } - p.jumps.n = w; - p.jumps.cur = @min(cur, w -| 1); + v.jumps.n = w; + v.jumps.cur = @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; + const from = v.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 = p.panes[v.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.list[0..p.jumps.n]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { - k.pane = @intCast(p.active); + for (v.jumps.list[0..p.view().jumps.n]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { + k.pane = @intCast(v.active); k.serial = opened.serial; }; - p.jumps.cur = next; + v.jumps.cur = 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.jumps.cur = next; + if (p.panes[v.active]) |source| look.clearNavigationSelection(p, source); + if (v.active != j.pane) look.clearNavigationSelection(p, pane); + v.jumps.cur = 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 }); @@ -320,36 +322,36 @@ test "the jump list never holds two entries in a row the same, when the one betw 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.list[0] = at; - p.jumps.list[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 }; - p.jumps.list[2] = at; - p.jumps.list[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 }; - p.jumps.n = 4; - p.jumps.cur = 3; + p.view().jumps.list[0] = at; + p.view().jumps.list[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 }; + p.view().jumps.list[2] = at; + p.view().jumps.list[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 }; + p.view().jumps.n = 4; + p.view().jumps.cur = 3; pane.body.cur_row = 80; // where you are now: far from both trackJump(p); - for (1..p.jumps.n) |i| try std.testing.expect(!std.meta.eql(p.jumps.list[i - 1], p.jumps.list[i])); - try std.testing.expectEqual(@as(usize, 3), p.jumps.n); - try std.testing.expectEqual(@as(u32, 1), p.jumps.list[0].line); - try std.testing.expectEqual(@as(u32, 150), p.jumps.list[1].line); + for (1..p.view().jumps.n) |i| try std.testing.expect(!std.meta.eql(p.view().jumps.list[i - 1], p.view().jumps.list[i])); + try std.testing.expectEqual(@as(usize, 3), p.view().jumps.n); + try std.testing.expectEqual(@as(u32, 1), p.view().jumps.list[0].line); + try std.testing.expectEqual(@as(u32, 150), p.view().jumps.list[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.jumps.n = 2; - p.jumps.cur = 1; - p.jumps.list[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; + p.view().jumps.n = 2; + p.view().jumps.cur = 1; + p.view().jumps.list[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.list[0] = missing; + p.view().jumps.list[0] = missing; jumpBy(p, -1); - try std.testing.expectEqual(@as(usize, 1), p.jumps.cur); + try std.testing.expectEqual(@as(usize, 1), p.view().jumps.cur); try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0)); } } |
