From bef8549afa2bf8018f197ddc8a1ba10831516b71 Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Wed, 2 Apr 2025 12:24:50 -0300 Subject: [PATCH] Helix mode: move_next_word --- src/tui/editor.zig | 8 ++++---- src/tui/mode/helix.zig | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 4f34ae3..d52fafb 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1775,7 +1775,7 @@ pub const Editor = struct { cursel.check_selection(root, metrics); } - fn with_selections_const(self: *Self, root: Buffer.Root, move: cursor_operator_const) error{Stop}!void { + pub fn with_selections_const(self: *Self, root: Buffer.Root, move: cursor_operator_const) error{Stop}!void { var someone_stopped = false; for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| with_selection_const(root, move, cursel, self.metrics) catch { @@ -1994,7 +1994,7 @@ pub const Editor = struct { return !cursor.test_at(root, is_whitespace_or_eol, metrics); } - fn is_word_boundary_left_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + pub fn is_word_boundary_left_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { if (is_whitespace_at_cursor(root, cursor, metrics)) return false; var next = cursor.*; next.move_left(root, metrics) catch return true; @@ -2087,11 +2087,11 @@ pub const Editor = struct { return false; } - fn move_cursor_left(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { + pub fn move_cursor_left(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { try cursor.move_left(root, metrics); } - fn move_cursor_left_until(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate, metrics: Buffer.Metrics) void { + pub fn move_cursor_left_until(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate, metrics: Buffer.Metrics) void { while (!pred(root, cursor, metrics)) move_cursor_left(root, cursor, metrics) catch return; } diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 6838af5..74e4b8f 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -6,6 +6,8 @@ const cmd = command.executeName; const tui = @import("../tui.zig"); const Editor = @import("../editor.zig").Editor; +const Buffer = @import("Buffer"); +const Cursor = Buffer.Cursor; var commands: Commands = undefined; @@ -93,4 +95,38 @@ const cmds_ = struct { ed.clamp(); } pub const extend_line_below_meta: Meta = .{ .description = "Select current line, if already selected, extend to next line" }; + + pub fn move_next_word_start(_: *void, _: Ctx) Result { + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + const root = try ed.buf_root(); + + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + cursel.selection = null; + }; + + ed.with_selections_const(root, Editor.move_cursor_word_right_vim) catch {}; + ed.clamp(); + } + + pub const move_next_word_start_meta: Meta = .{ .description = "Move next word start" }; + + pub fn move_prev_word_start(_: *void, _: Ctx) Result { + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + const root = try ed.buf_root(); + + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + cursel.selection = null; + }; + + ed.with_selections_const(root, move_cursor_word_left_helix) catch {}; + ed.clamp(); + } + 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 { + try Editor.move_cursor_left(root, cursor, metrics); + Editor.move_cursor_left_until(root, cursor, Editor.is_word_boundary_left_vim, metrics); +}