1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
//! Bounded per-pane jump history. Scrolling changes the current location, not the target.
const std = @import("std");
const Navigation = @This();
pub const Location = struct { address: u64 = 0, row: f64 = 0, follow: bool = true, manual: bool = false };
place: Location = .{},
history: [32]Location = @splat(.{}),
len: u8 = 0,
cursor: u8 = 0,
pub fn visit(n: *Navigation, address: u64) void {
if (n.place.address == address and !n.place.follow) return;
if (n.len == 0) {
n.history[0] = n.place;
n.len = 1;
}
n.history[n.cursor] = n.place;
n.len = n.cursor + 1;
if (n.len == n.history.len) {
std.mem.copyForwards(Location, n.history[0..31], n.history[1..]);
n.len -= 1;
}
n.cursor = n.len;
n.place = .{ .address = address, .follow = false };
n.history[n.len] = n.place;
n.len += 1;
}
pub fn back(n: *Navigation) void {
if (n.len == 0 or n.cursor == 0) return;
n.history[n.cursor] = n.place;
n.cursor -= 1;
n.place = n.history[n.cursor];
}
pub fn forward(n: *Navigation) void {
if (n.cursor + 1 >= n.len) return;
n.history[n.cursor] = n.place;
n.cursor += 1;
n.place = n.history[n.cursor];
}
test "pane jumps retain scroll and discard the abandoned forward branch" {
var n: Navigation = .{};
n.visit(0x1000);
n.place.row = 3;
n.visit(0x2000);
n.back();
try std.testing.expectEqual(@as(f64, 3), n.place.row);
n.visit(0x3000);
n.forward();
try std.testing.expectEqual(@as(u64, 0x3000), n.place.address);
for (0..40) |i| n.visit(0x4000 + i);
try std.testing.expectEqual(@as(u8, 32), n.len);
for (0..40) |_| n.back();
try std.testing.expectEqual(@as(u64, 0x4008), n.place.address);
}
test "large mapping row history keeps individual words reachable" {
var n: Navigation = .{};
n.visit(0x100000000);
n.place.row = 268435455;
n.place.row -= 1;
n.visit(0x200000000);
n.back();
try std.testing.expectEqual(@as(f64, 268435454), n.place.row);
}
|