From 5d21fb07dc40f708265324d719f27a639ef48da7 Mon Sep 17 00:00:00 2001 From: lulvz Date: Sun, 2 Feb 2025 13:16:15 +0000 Subject: [PATCH] feat(vim): add move_word_right_end_vim function (correspondent to e in NORMAL mode) and update keybindings --- src/keybind/builtin/vim.json | 2 +- src/tui/editor.zig | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/keybind/builtin/vim.json b/src/keybind/builtin/vim.json index 1b42dea..b069ed3 100644 --- a/src/keybind/builtin/vim.json +++ b/src/keybind/builtin/vim.json @@ -12,7 +12,7 @@ "press": [ ["b", "move_word_left_vim"], ["w", "move_word_right_vim"], - ["e", "move_word_right"], + ["e", "move_word_right_end_vim"], ["x", "delete_forward"], ["s", ["delete_forward"], ["enter_mode", "insert"]], ["u", "undo"], diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 4d7b5e5..6992ad6 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2019,6 +2019,19 @@ pub const Editor = struct { return false; } + fn is_word_boundary_right_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_right(root, metrics) catch return true; + + const next_is_white_space = is_white_space_at_cursor(root, &next, metrics); + if(next_is_white_space) return true; + + const curr_is_non_word = is_non_word_char_at_cursor_vim(root, cursor, metrics); + const next_is_non_word = is_non_word_char_at_cursor_vim(root, &next, metrics); + return curr_is_non_word != next_is_non_word; + } + fn is_non_word_boundary_right(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { const line_width = root.line_width(cursor.row, metrics) catch return true; if (cursor.col >= line_width) @@ -2638,6 +2651,11 @@ pub const Editor = struct { move_cursor_right_until(root, cursor, is_word_boundary_left_vim, metrics); } + pub fn move_cursor_word_right_end_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_right_vim, metrics); + } + pub fn move_cursor_word_right_space(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { var next = cursor.*; next.move_right(root, metrics) catch { @@ -2681,6 +2699,13 @@ pub const Editor = struct { } pub const move_word_right_vim_meta = .{ .description = "Move cursor right by word (vim)" }; + pub fn move_word_right_end_vim(self: *Self, _: Context) Result { + const root = try self.buf_root(); + self.with_cursors_const(root, move_cursor_word_right_end_vim) catch {}; + self.clamp(); + } + pub const move_word_right_end_vim_meta = .{ .description = "Move cursor right by end of word (vim)" }; + fn move_cursor_to_char_left(root: Buffer.Root, cursor: *Cursor, ctx: Context, metrics: Buffer.Metrics) error{Stop}!void { var egc: []const u8 = undefined; if (!(ctx.args.match(.{tp.extract(&egc)}) catch return error.Stop))