diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 5dd92b8..57538df 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2259,7 +2259,7 @@ pub const Editor = struct { }; } - fn is_word_char(c: []const u8) bool { + pub fn is_word_char(c: []const u8) bool { return !is_not_word_char(c); } @@ -2271,7 +2271,7 @@ pub const Editor = struct { return cursor.test_at(root, is_not_word_char, metrics); } - fn is_word_boundary_left(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + pub fn is_word_boundary_left(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { if (cursor.col == 0) return true; if (is_non_word_char_at_cursor(root, cursor, metrics)) @@ -2324,7 +2324,7 @@ pub const Editor = struct { return false; } - fn is_word_boundary_right(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { + pub fn is_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) return true; diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index bd3322a..e08e724 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -411,6 +411,28 @@ const cmds_ = struct { } pub const extend_to_char_right_helix_meta: Meta = .{ .description = "Extend Selection to char right" }; + pub fn select_textobject_inner(_: *void, ctx: Ctx) Result { + var action: []const u8 = ""; + + if (!try ctx.args.match(.{tp.extract(&action)})) return error.Stop; + const logger = log.logger("helix-mode"); + defer logger.deinit(); + logger.print("the selection {s}", .{action}); + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + const root = ed.buf_root() catch return; + + if (std.mem.eql(u8, action, "w")) { + try ed.with_cursels_const(root, select_inner_word, ed.metrics); + } else if (std.mem.eql(u8, action, "W")) { + try ed.with_cursels_const(root, select_inner_long_word, ed.metrics); + } else { + return; + } + ed.clamp(); + } + pub const select_textobject_inner_meta: Meta = .{ .description = "select inside object helix" }; + pub fn copy_helix(_: *void, _: Ctx) Result { const mv = tui.mainview() orelse return; const ed = mv.get_active_editor() orelse return; @@ -522,6 +544,32 @@ fn to_char_helix(ctx: command.Context, move: Editor.cursel_operator_mut_once_arg ed.clamp(); } +fn select_inner_word(root: Buffer.Root, cursel: *CurSel, metrics: Buffer.Metrics) !void { + if (!cursel.cursor.test_at(root, Editor.is_word_char, metrics)) return; + var prev = cursel.cursor; + var next = cursel.cursor; + Editor.move_cursor_left_until(root, &prev, Editor.is_word_boundary_left, metrics); + Editor.move_cursor_right_until(root, &next, Editor.is_word_boundary_right, metrics); + try next.move_right(root, metrics); + const sel = try cursel.enable_selection(root, metrics); + sel.begin = prev; + sel.end = next; + cursel.*.cursor = next; +} + +fn select_inner_long_word(root: Buffer.Root, cursel: *CurSel, metrics: Buffer.Metrics) !void { + if (cursel.cursor.test_at(root, Editor.is_whitespace, metrics)) return; + var prev = cursel.cursor; + var next = cursel.cursor; + Editor.move_cursor_left_until(root, &prev, is_long_word_boundary_left, metrics); + Editor.move_cursor_right_until(root, &next, is_long_word_boundary_right, metrics); + try next.move_right(root, metrics); + const sel = try cursel.enable_selection(root, metrics); + sel.begin = prev; + sel.end = next; + cursel.*.cursor = next; +} + fn select_cursel_to_char_left_helix(root: Buffer.Root, cursel: *CurSel, ctx: command.Context, metrics: Buffer.Metrics) error{Stop}!void { var moving_cursor: Cursor = cursel.*.cursor; var begin = cursel.*.cursor;