Helix mode: move_next_word

This commit is contained in:
ivel.santos 2025-04-02 12:24:50 -03:00 committed by CJ van den Berg
parent d8553dd9d7
commit bef8549afa
2 changed files with 40 additions and 4 deletions

View file

@ -1775,7 +1775,7 @@ pub const Editor = struct {
cursel.check_selection(root, metrics); 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; var someone_stopped = false;
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel|
with_selection_const(root, move, cursel, self.metrics) catch { 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); 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; if (is_whitespace_at_cursor(root, cursor, metrics)) return false;
var next = cursor.*; var next = cursor.*;
next.move_left(root, metrics) catch return true; next.move_left(root, metrics) catch return true;
@ -2087,11 +2087,11 @@ pub const Editor = struct {
return false; 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); 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)) while (!pred(root, cursor, metrics))
move_cursor_left(root, cursor, metrics) catch return; move_cursor_left(root, cursor, metrics) catch return;
} }

View file

@ -6,6 +6,8 @@ const cmd = command.executeName;
const tui = @import("../tui.zig"); const tui = @import("../tui.zig");
const Editor = @import("../editor.zig").Editor; const Editor = @import("../editor.zig").Editor;
const Buffer = @import("Buffer");
const Cursor = Buffer.Cursor;
var commands: Commands = undefined; var commands: Commands = undefined;
@ -93,4 +95,38 @@ const cmds_ = struct {
ed.clamp(); ed.clamp();
} }
pub const extend_line_below_meta: Meta = .{ .description = "Select current line, if already selected, extend to next line" }; 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);
}