From fda9338a06af85966f7d2730415d5e08b633e55f Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 9 Sep 2025 09:24:18 +0200 Subject: [PATCH] fix: clamp to end of last line if the cursor is past the end of the buffer closes #289 --- src/buffer/Cursor.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/buffer/Cursor.zig b/src/buffer/Cursor.zig index aca5339..9cbf2e8 100644 --- a/src/buffer/Cursor.zig +++ b/src/buffer/Cursor.zig @@ -28,8 +28,12 @@ pub inline fn right_of(self: Self, other: Self) bool { } pub fn clamp_to_buffer(self: *Self, root: Buffer.Root, metrics: Metrics) void { - self.row = @min(self.row, root.lines() - 1); - self.col = @min(self.col, root.line_width(self.row, metrics) catch 0); + if (self.row > root.lines() - 1) { + self.row = root.lines() - 1; + self.col = root.line_width(self.row, metrics) catch 0; + } else { + self.col = @min(self.col, root.line_width(self.row, metrics) catch 0); + } } fn follow_target(self: *Self, root: Buffer.Root, metrics: Metrics) void {