Helix mode: move_cursor_word_left

This commit is contained in:
ivel.santos 2025-04-03 00:14:13 -03:00 committed by CJ van den Berg
parent bef8549afa
commit d2d6508ed8
2 changed files with 23 additions and 5 deletions

View file

@ -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);
}

View file

@ -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);
}
}