From 5a40dca5adb6e9e650f55f3ec6f254a13bf50493 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 13 Nov 2026 01:24:16 -0300 Subject: debugger: align source and add rich Vim-style navigation --- gui/src/Navigation.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 gui/src/Navigation.zig (limited to 'gui/src/Navigation.zig') diff --git a/gui/src/Navigation.zig b/gui/src/Navigation.zig new file mode 100644 index 0000000..f9ae34a --- /dev/null +++ b/gui/src/Navigation.zig @@ -0,0 +1,56 @@ +//! 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: f32 = 0, follow: bool = true }; +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(f32, 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); +} -- cgit v1.3