From bb53ba0fc1bf72cc36e3039430bfb229cc8c1f83 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 27 Nov 2025 16:11:53 +0100 Subject: [PATCH] refactor: add Cursor char_at, char_left/_right, test_left/_right functions --- src/buffer/Cursor.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/buffer/Cursor.zig b/src/buffer/Cursor.zig index fea7aa0..d288c8d 100644 --- a/src/buffer/Cursor.zig +++ b/src/buffer/Cursor.zig @@ -193,10 +193,39 @@ pub fn egc_at(self: *const Self, root: Buffer.Root, metrics: Metrics) error{NotF return root.egc_at(self.row, self.col, metrics); } +pub fn char_at(self: *const Self, root: Buffer.Root, metrics: Metrics) []const u8 { + const char, _, _ = root.egc_at(self.row, self.col, metrics) catch return &.{}; + return char; +} + +pub fn char_left(self: *const Self, root: Buffer.Root, metrics: Metrics) []const u8 { + var tmp = self.*; + tmp.move_left(root, metrics) catch return &.{}; + return tmp.char_at(root, metrics); +} + +pub fn char_right(self: *const Self, root: Buffer.Root, metrics: Metrics) []const u8 { + var tmp = self.*; + tmp.move_right(root, metrics) catch return &.{}; + return tmp.char_at(root, metrics); +} + pub fn test_at(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, metrics: Metrics) bool { return root.test_at(pred, self.row, self.col, metrics); } +pub fn test_left(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, metrics: Metrics) bool { + var tmp = self.*; + tmp.move_left(root, metrics) catch return false; + return root.test_at(pred, tmp.row, tmp.col, metrics); +} + +pub fn test_right(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, metrics: Metrics) bool { + var tmp = self.*; + tmp.move_right(root, metrics) catch return false; + return root.test_at(pred, tmp.row, tmp.col, metrics); +} + pub fn write(self: *const Self, writer: *std.Io.Writer) !void { try cbor.writeValue(writer, .{ self.row,