From d2d6508ed8cacad2297282cc30b1060d6beb0d0e Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Thu, 3 Apr 2025 00:14:13 -0300 Subject: [PATCH] Helix mode: move_cursor_word_left --- src/tui/editor.zig | 6 +++--- src/tui/mode/helix.zig | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index d52fafb..ec581e5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1921,7 +1921,7 @@ pub const Editor = struct { const cursel_operator = *const fn (root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root; const cursel_operator_mut = *const fn (self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root; - fn is_not_word_char(c: []const u8) bool { + pub fn is_not_word_char(c: []const u8) bool { if (c.len == 0) return true; return switch (c[0]) { ' ' => true, @@ -1962,7 +1962,7 @@ pub const Editor = struct { return cursor.test_at(root, is_word_char, metrics); } - fn is_non_word_char_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + pub fn is_non_word_char_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { return cursor.test_at(root, is_not_word_char, metrics); } @@ -1986,7 +1986,7 @@ pub const Editor = struct { return is_whitespace(c) or c[0] == '\n'; } - fn is_whitespace_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + pub fn is_whitespace_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { return cursor.test_at(root, is_whitespace, metrics); } diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 74e4b8f..f4efddb 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -126,7 +126,25 @@ const cmds_ = struct { pub const move_prev_word_start_meta: Meta = .{ .description = "Move previous word start" }; }; -pub fn move_cursor_word_left_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { +fn move_cursor_word_left_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { try Editor.move_cursor_left(root, cursor, metrics); - Editor.move_cursor_left_until(root, cursor, Editor.is_word_boundary_left_vim, metrics); + + // Consume " " + while (Editor.is_whitespace_at_cursor(root, cursor, metrics)) { + try Editor.move_cursor_left(root, cursor, metrics); + } + + var next = cursor.*; + next.move_left(root, metrics) catch return; + var next_next = next; + next_next.move_left(root, metrics) catch return; + + const cur = next.test_at(root, Editor.is_not_word_char, metrics); + const nxt = next_next.test_at(root, Editor.is_not_word_char, metrics); + if (cur != nxt) { + try Editor.move_cursor_left(root, cursor, metrics); + return; + } else { + try move_cursor_word_left_helix(root, cursor, metrics); + } }