From 21540af8efe299caf071e1f0fac1bc91d3bdc129 Mon Sep 17 00:00:00 2001 From: lulvz Date: Sun, 2 Feb 2025 02:04:44 +0000 Subject: [PATCH] feat(editor): start implementing is_word_boundary_left_vim for vim-like word detection --- src/tui/editor.zig | 64 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index b537f55..a2a47c2 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1939,6 +1939,68 @@ pub const Editor = struct { return false; } + fn is_not_word_char_vim(c: []const u8) bool { + if (c.len == 0) return true; + return switch (c[0]) { + '=' => true, + '"' => true, + '\'' => true, + '\t' => true, + '\n' => true, + '/' => true, + '\\' => true, + '*' => true, + ':' => true, + '.' => true, + ',' => true, + '(' => true, + ')' => true, + '{' => true, + '}' => true, + '[' => true, + ']' => true, + ';' => true, + '|' => true, + '!' => true, + '?' => true, + '&' => true, + '-' => true, + '<' => true, + '>' => true, + else => false, + }; + } + + fn is_non_word_char_at_cursor_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + return cursor.test_at(root, is_not_word_char_vim, metrics); + } + + fn is_white_space(c: []const u8) bool { + if (c.len == 0) return true; + return switch (c[0]) { + ' ' => true, + else => false + }; + } + + fn is_white_space_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + return cursor.test_at(root, is_white_space, metrics); + } + + fn is_word_boundary_left_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + if(is_white_space_at_cursor(root, cursor, metrics)) return false; + var next = cursor.*; + next.move_left(root, metrics) catch return true; + + // right now this doesn't work + // ["enter_vim_mode"], abc + // jumping from the last " forward once using this command goes to the end of the line + // it should go to the start of abc + const curr_is_non_word = is_non_word_char_at_cursor_vim(root, cursor, metrics); + const prev_is_non_word = is_non_word_char_at_cursor_vim(root, &next, metrics); + return curr_is_non_word != prev_is_non_word; + } + fn is_non_word_boundary_left(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { if (cursor.col == 0) return true; @@ -2575,7 +2637,7 @@ pub const Editor = struct { pub fn move_cursor_word_right_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { try move_cursor_right(root, cursor, metrics); - move_cursor_right_until(root, cursor, is_word_boundary_left, metrics); + move_cursor_right_until(root, cursor, is_word_boundary_left_vim, metrics); } pub fn move_cursor_word_right_space(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {