refactor: re-write all helix commands to use bound self parameter

This makes implementing commands in helix mode almost identical to
implementing them inside the editor directly.
This commit is contained in:
CJ van den Berg 2025-11-27 20:11:22 +01:00
parent fb6e47fd77
commit 71387aaa43
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 157 additions and 164 deletions

View file

@ -229,59 +229,57 @@ const cmds_ = struct {
}
pub const select_half_page_down_meta: Meta = .{ .description = "Select half a page down" };
pub fn save_selection(_: *Self, _: Context) Result {
pub fn save_selection(self: *Self, _: Context) Result {
const logger = log.logger("helix-mode");
defer logger.deinit();
logger.print("saved location", .{});
const mv = tui.mainview() orelse return;
const file_path = mv.get_active_file_path() orelse return;
const primary = (mv.get_active_editor() orelse return).get_primary();
const primary = self.get_primary();
const sel: ?location_history.Selection = if (primary.selection) |sel| .{
.begin = .{ .row = sel.begin.row, .col = sel.begin.col },
.end = .{ .row = sel.end.row, .col = sel.end.col },
} else null;
mv.location_history_.update(file_path, .{
mv.location_history_.update(self.file_path orelse return, .{
.row = primary.cursor.row + 1,
.col = primary.cursor.col + 1,
}, sel);
}
pub const save_selection_meta: Meta = .{ .description = "Save current selection to location history" };
pub fn split_selection_on_newline(_: *Self, _: Context) Result {
const ed, const root = get_buf() orelse return;
const cursels = try ed.cursels.toOwnedSlice(ed.allocator);
defer ed.allocator.free(cursels);
for (cursels) |*cursel_| if (cursel_.*) |*cursel| {
try add_cursors_to_cursel_line_ends_helix(ed, root, cursel);
};
ed.clamp();
pub fn split_selection_on_newline(self: *Self, _: Context) Result {
const root = try self.buf_root();
const cursels = try self.cursels.toOwnedSlice(self.allocator);
defer self.allocator.free(cursels);
for (cursels) |*cursel_| if (cursel_.*) |*cursel|
try add_cursors_to_cursel_line_ends(self, root, cursel);
self.clamp();
}
pub const split_selection_on_newline_meta: Meta = .{ .description = "Add cursor to each line in selection helix" };
pub fn match_brackets(_: *Self, ctx: Context) Result {
const ed, const root = get_buf() orelse return;
try ed.with_cursels_const_once_arg(root, &match_bracket, ctx);
ed.clamp();
pub fn match_brackets(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
try self.with_cursels_const_once_arg(root, &match_bracket, ctx);
self.clamp();
}
pub const match_brackets_meta: Meta = .{ .description = "Goto matching bracket" };
pub fn extend_line_below(_: *Self, ctx: Context) Result {
const ed, const root = get_buf() orelse return;
pub fn extend_line_below(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
while (repeat > 0) : (repeat -= 1) {
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
const sel = cursel.enable_selection();
sel.normalize();
try Editor.move_cursor_begin(root, &sel.begin, ed.metrics);
try Editor.move_cursor_end(root, &sel.end, ed.metrics);
try Editor.move_cursor_right(root, &sel.end, ed.metrics);
try move_cursor_begin(root, &sel.begin, self.metrics);
try move_cursor_end(root, &sel.end, self.metrics);
try move_cursor_right(root, &sel.end, self.metrics);
cursel.cursor = sel.end;
};
}
ed.clamp();
self.clamp();
}
pub const extend_line_below_meta: Meta = .{ .arguments = &.{.integer}, .description = "Select current line, if already selected, extend to next line" };
@ -294,117 +292,117 @@ const cmds_ = struct {
}
pub const init_helix_select_mode_meta: Meta = .{};
pub fn move_next_word_start(_: *Self, ctx: Context) Result {
try move_to_word(ctx, Editor.move_cursor_word_right_vim, .forwards);
pub fn move_next_word_start(self: *Self, ctx: Context) Result {
try move_to_word(self, ctx, move_cursor_word_right_vim, .forwards);
}
pub const move_next_word_start_meta: Meta = .{ .description = "Move next word start", .arguments = &.{.integer} };
pub fn extend_next_word_start(_: *Self, ctx: Context) Result {
try extend_to_word(ctx, Editor.move_cursor_word_right_vim, .forwards);
pub fn extend_next_word_start(self: *Self, ctx: Context) Result {
try extend_to_word(self, ctx, move_cursor_word_right_vim, .forwards);
}
pub const extend_next_word_start_meta: Meta = .{ .description = "Extend next word start", .arguments = &.{.integer} };
pub fn move_next_long_word_start(_: *Self, ctx: Context) Result {
try move_to_word(ctx, move_cursor_long_word_right, .forwards);
pub fn move_next_long_word_start(self: *Self, ctx: Context) Result {
try move_to_word(self, ctx, move_cursor_long_word_right, .forwards);
}
pub const move_next_long_word_start_meta: Meta = .{ .description = "Move next long word start", .arguments = &.{.integer} };
pub fn extend_next_long_word_start(_: *Self, ctx: Context) Result {
try extend_to_word(ctx, move_cursor_long_word_right, .forwards);
pub fn extend_next_long_word_start(self: *Self, ctx: Context) Result {
try extend_to_word(self, ctx, move_cursor_long_word_right, .forwards);
}
pub const extend_next_long_word_start_meta: Meta = .{ .description = "Extend next long word start", .arguments = &.{.integer} };
pub fn move_prev_word_start(_: *Self, ctx: Context) Result {
try move_cursels_const_repeat(move_cursor_prev_word_start, ctx);
pub fn move_prev_word_start(self: *Self, ctx: Context) Result {
try move_cursels_const_repeat(self, move_cursor_prev_word_start, ctx);
}
pub const move_prev_word_start_meta: Meta = .{ .description = "Move previous word start", .arguments = &.{.integer} };
pub fn extend_prev_word_start(_: *Self, ctx: Context) Result {
try move_cursels_const_repeat(move_cursor_prev_word_start_extend, ctx);
pub fn extend_prev_word_start(self: *Self, ctx: Context) Result {
try move_cursels_const_repeat(self, move_cursor_prev_word_start_extend, ctx);
}
pub const extend_prev_word_start_meta: Meta = .{ .description = "Extend previous word start", .arguments = &.{.integer} };
pub fn move_prev_long_word_start(_: *Self, ctx: Context) Result {
try move_to_word(ctx, move_cursor_long_word_left, .backwards);
pub fn move_prev_long_word_start(self: *Self, ctx: Context) Result {
try move_to_word(self, ctx, move_cursor_long_word_left, .backwards);
}
pub const move_prev_long_word_start_meta: Meta = .{ .description = "Move previous long word start", .arguments = &.{.integer} };
pub fn extend_prev_long_word_start(_: *Self, ctx: Context) Result {
try extend_to_word(ctx, move_cursor_long_word_left, .backwards);
pub fn extend_prev_long_word_start(self: *Self, ctx: Context) Result {
try extend_to_word(self, ctx, move_cursor_long_word_left, .backwards);
}
pub const extend_prev_long_word_start_meta: Meta = .{ .description = "Extend previous word start", .arguments = &.{.integer} };
pub fn move_next_word_end(_: *Self, ctx: Context) Result {
try move_to_word(ctx, move_cursor_word_right_end_helix, .forwards);
pub fn move_next_word_end(self: *Self, ctx: Context) Result {
try move_to_word(self, ctx, move_cursor_word_right_end_helix, .forwards);
}
pub const move_next_word_end_meta: Meta = .{ .description = "Move next word end", .arguments = &.{.integer} };
pub fn extend_next_word_end(_: *Self, ctx: Context) Result {
try extend_to_word(ctx, move_cursor_word_right_end_helix, .forwards);
pub fn extend_next_word_end(self: *Self, ctx: Context) Result {
try extend_to_word(self, ctx, move_cursor_word_right_end_helix, .forwards);
}
pub const extend_next_word_end_meta: Meta = .{ .description = "Extend next word end", .arguments = &.{.integer} };
pub fn move_next_long_word_end(_: *Self, ctx: Context) Result {
try move_to_word(ctx, move_cursor_long_word_right_end, .forwards);
pub fn move_next_long_word_end(self: *Self, ctx: Context) Result {
try move_to_word(self, ctx, move_cursor_long_word_right_end, .forwards);
}
pub const move_next_long_word_end_meta: Meta = .{ .description = "Move next long word end", .arguments = &.{.integer} };
pub fn extend_next_long_word_end(_: *Self, ctx: Context) Result {
try extend_to_word(ctx, move_cursor_long_word_right_end, .forwards);
pub fn extend_next_long_word_end(self: *Self, ctx: Context) Result {
try extend_to_word(self, ctx, move_cursor_long_word_right_end, .forwards);
}
pub const extend_next_long_word_end_meta: Meta = .{ .description = "Extend next long word end", .arguments = &.{.integer} };
pub fn cut_forward_internal_inclusive(_: *Self, _: Context) Result {
const ed, const b = get_buf_for_update() orelse return;
pub fn cut_forward_internal_inclusive(self: *Self, _: Context) Result {
const b = try self.buf_for_update();
tui.clipboard_start_group();
const root = try ed.cut_to(move_noop, b.root);
try ed.update_buf(root);
ed.clamp();
const root = try self.cut_to(move_noop, b.root);
try self.update_buf(root);
self.clamp();
}
pub const cut_forward_internal_inclusive_meta: Meta = .{ .description = "Cut next character to internal clipboard (inclusive)" };
pub fn select_right_helix(_: *Self, ctx: Context) Result {
const ed, const root = get_buf() orelse return;
pub fn select_right_helix(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
while (repeat > 0) : (repeat -= 1) {
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
const sel = cursel.enable_selection();
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
const sel = cursel.enable_selection_inclusive(root, self.metrics);
// handling left to right transition
const sel_begin: i32 = @intCast(sel.begin.col);
const sel_end: i32 = @intCast(sel.end.col);
if ((sel_begin - sel_end) == 1 and sel.begin.row == sel.end.row) {
try Editor.move_cursor_right(root, &sel.end, ed.metrics);
try move_cursor_right(root, &sel.end, self.metrics);
sel.begin.col -= 1;
}
try Editor.move_cursor_right(root, &sel.end, ed.metrics);
try move_cursor_right(root, &sel.end, self.metrics);
cursel.cursor = sel.end;
cursel.check_selection();
};
}
ed.clamp();
self.clamp();
}
pub const select_right_helix_meta: Meta = .{ .description = "Select right", .arguments = &.{.integer} };
pub fn select_left_helix(_: *Self, ctx: Context) Result {
const ed, const root = get_buf() orelse return;
pub fn select_left_helix(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
while (repeat > 0) : (repeat -= 1) {
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
if (cursel.selection == null) {
cursel.selection = Selection.from_cursor(&cursel.cursor);
try cursel.selection.?.begin.move_right(root, ed.metrics);
try cursel.selection.?.begin.move_right(root, self.metrics);
}
if (cursel.selection) |*sel| {
try Editor.move_cursor_left(root, &sel.end, ed.metrics);
try move_cursor_left(root, &sel.end, self.metrics);
cursel.cursor = sel.end;
if (sel.begin.col == sel.end.col and sel.begin.row == sel.end.row) {
try sel.begin.move_right(root, ed.metrics);
try Editor.move_cursor_left(root, &sel.end, ed.metrics);
try sel.begin.move_right(root, self.metrics);
try move_cursor_left(root, &sel.end, self.metrics);
cursel.cursor = sel.end;
}
}
@ -412,117 +410,117 @@ const cmds_ = struct {
cursel.check_selection();
};
}
ed.clamp();
self.clamp();
}
pub const select_left_helix_meta: Meta = .{ .description = "Select left", .arguments = &.{.integer} };
pub fn select_to_char_left_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &select_cursel_to_char_left_helix);
pub fn select_to_char_left_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &select_cursel_to_char_left_helix);
}
pub const select_to_char_left_helix_meta: Meta = .{ .description = "Select to char left" };
pub fn select_till_char_left_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &select_cursel_till_char_left_helix);
pub fn select_till_char_left_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &select_cursel_till_char_left_helix);
}
pub const select_till_char_left_helix_meta: Meta = .{ .description = "Select until char left" };
pub fn extend_to_char_left_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &extend_cursel_to_char_left_helix);
pub fn extend_to_char_left_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &extend_cursel_to_char_left_helix);
}
pub const extend_to_char_left_helix_meta: Meta = .{ .description = "Extend Selection to char left" };
pub fn extend_till_char_left_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &extend_cursel_till_char_left_helix);
pub fn extend_till_char_left_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &extend_cursel_till_char_left_helix);
}
pub const extend_till_char_left_helix_meta: Meta = .{ .description = "Extend Selection until char left" };
pub fn select_till_char_right_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &select_cursel_till_char_right_helix);
pub fn select_till_char_right_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &select_cursel_till_char_right_helix);
}
pub const select_till_char_right_helix_meta: Meta = .{ .description = "Select until char right" };
pub fn select_to_char_right_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &select_cursel_to_char_right_helix);
pub fn select_to_char_right_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &select_cursel_to_char_right_helix);
}
pub const select_to_char_right_helix_meta: Meta = .{ .description = "Select to char right" };
pub fn extend_till_char_right_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &extend_cursel_till_char_right_helix);
pub fn extend_till_char_right_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &extend_cursel_till_char_right_helix);
}
pub const extend_till_char_right_helix_meta: Meta = .{ .description = "Extend Selection until char right" };
pub fn extend_to_char_right_helix(_: *Self, ctx: Context) Result {
try to_char_helix(ctx, &extend_cursel_to_char_right_helix);
pub fn extend_to_char_right_helix(self: *Self, ctx: Context) Result {
try to_char_helix(self, ctx, &extend_cursel_to_char_right_helix);
}
pub const extend_to_char_right_helix_meta: Meta = .{ .description = "Extend Selection to char right" };
pub fn select_textobject_inner(_: *Self, ctx: Context) Result {
const ed, const root = get_buf() orelse return error.Stop;
pub fn select_textobject_inner(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
var action: []const u8 = "";
if (!try ctx.args.match(.{tp.extract(&action)})) return error.Stop;
if (std.mem.eql(u8, action, "w")) {
try ed.with_cursels_const(root, select_inner_word, ed.metrics);
try self.with_cursels_const(root, select_inner_word, self.metrics);
} else if (std.mem.eql(u8, action, "W")) {
try ed.with_cursels_const(root, select_inner_long_word, ed.metrics);
try self.with_cursels_const(root, select_inner_long_word, self.metrics);
} else {
return;
}
ed.clamp();
self.clamp();
}
pub const select_textobject_inner_meta: Meta = .{ .description = "select inside object helix" };
pub fn select_textobject_around(_: *Self, ctx: Context) Result {
const ed, const root = get_buf() orelse return;
pub fn select_textobject_around(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
var action: []const u8 = "";
if (!try ctx.args.match(.{tp.extract(&action)})) return error.Stop;
if (std.mem.eql(u8, action, "w")) {
try ed.with_cursels_const(root, select_around_word, ed.metrics);
try self.with_cursels_const(root, select_around_word, self.metrics);
} else if (std.mem.eql(u8, action, "W")) {
try ed.with_cursels_const(root, select_inner_long_word, ed.metrics);
try self.with_cursels_const(root, select_inner_long_word, self.metrics);
} else {
return;
}
ed.clamp();
self.clamp();
}
pub const select_textobject_around_meta: Meta = .{ .description = "select around object helix" };
pub fn copy_helix(_: *Self, _: Context) Result {
const ed, const root = get_buf() orelse return;
pub fn copy_helix(self: *Self, _: Context) Result {
const root = try self.buf_root();
tui.clipboard_start_group();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| if (cursel.selection) |sel|
tui.clipboard_add_chunk(try Editor.copy_selection(root, sel, tui.clipboard_allocator(), ed.metrics));
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| if (cursel.selection) |sel|
tui.clipboard_add_chunk(try Editor.copy_selection(root, sel, tui.clipboard_allocator(), self.metrics));
ed.logger.print("copy: {d} selections", .{ed.cursels.items.len});
self.logger.print("copy: {d} selections", .{self.cursels.items.len});
}
pub const copy_helix_meta: Meta = .{ .description = "Copy selection to clipboard (helix)" };
pub fn paste_after(_: *Self, ctx: Context) Result {
try paste_helix(ctx, insert_after);
pub fn paste_after(self: *Self, ctx: Context) Result {
try paste_helix(self, ctx, insert_after);
}
pub const paste_after_meta: Meta = .{ .description = "Paste from clipboard after selection" };
pub fn replace_selections_with_clipboard(_: *Self, ctx: Context) Result {
try paste_helix(ctx, insert_replace_selection);
pub fn replace_selections_with_clipboard(self: *Self, ctx: Context) Result {
try paste_helix(self, ctx, insert_replace_selection);
}
pub const replace_selections_with_clipboard_meta: Meta = .{ .description = "Replace selection from clipboard" };
pub fn paste_clipboard_before(_: *Self, ctx: Context) Result {
try paste_helix(ctx, insert_before);
pub fn paste_clipboard_before(self: *Self, ctx: Context) Result {
try paste_helix(self, ctx, insert_before);
}
pub const paste_clipboard_before_meta: Meta = .{ .description = "Paste from clipboard before selection" };
pub fn replace_with_character_helix(_: *Self, ctx: Context) Result {
const ed, const b = get_buf_for_update() orelse return;
pub fn replace_with_character_helix(self: *Self, ctx: Context) Result {
const b = try self.buf_for_update();
var root = b.root;
root = try ed.with_cursels_mut_once_arg(root, replace_cursel_with_character, ed.allocator, ctx);
try ed.update_buf(root);
ed.clamp();
ed.need_render();
root = try self.with_cursels_mut_once_arg(root, replace_cursel_with_character, self.allocator, ctx);
try self.update_buf(root);
self.clamp();
self.need_render();
}
pub const replace_with_character_helix_meta: Meta = .{ .description = "Replace with character" };
@ -539,22 +537,6 @@ const cmds_ = struct {
pub const goto_line_helix_meta: Meta = .{ .arguments = &.{.integer} };
};
fn get_context() ?struct { *MainView, *Editor } {
const mv = tui.mainview() orelse return null;
const ed = mv.get_active_editor() orelse return null;
return .{ mv, ed };
}
fn get_buf() ?struct { *Editor, Buffer.Root } {
_, const ed = get_context() orelse return null;
return .{ ed, ed.buf_root() catch return null };
}
fn get_buf_for_update() ?struct { *Editor, *const Buffer } {
_, const ed = get_context() orelse return null;
return .{ ed, ed.buf_for_update() catch return null };
}
fn is_eol(c: []const u8) bool {
return char_class(c) == .eol;
}
@ -577,12 +559,18 @@ fn is_word_boundary(root: Buffer.Root, cursor: Cursor, metrics: Buffer.Metrics,
};
}
const move_cursor_begin = Editor.move_cursor_begin;
const move_cursor_end = Editor.move_cursor_end;
const move_cursor_left = Editor.move_cursor_left;
const move_cursor_right = Editor.move_cursor_right;
const move_cursor_word_right_vim = Editor.move_cursor_word_right_vim;
fn move_cursor_prev_word_start(root: Buffer.Root, cursel: *CurSel, metrics: Buffer.Metrics) error{Stop}!void {
var cursor = cursel.cursor;
if (is_word_boundary(root, cursor, metrics, .left))
cursor.move_left(root, metrics) catch {};
var sel = Selection.from_cursor_inclusive(&cursor, root, metrics);
var sel = cursel.to_selection_inclusive(root, metrics);
defer {
sel.begin = cursor;
cursel.cursor = cursor;
@ -614,10 +602,10 @@ fn move_cursor_prev_word_start_extend(root: Buffer.Root, cursel: *CurSel, metric
try move_cursor_prev_word_start(root, cursel, metrics);
}
fn move_cursels_const_repeat(move: Editor.cursel_operator_const, ctx: Context) Result {
const ed, const root = get_buf() orelse return;
try ed.with_cursels_const_repeat(root, move, ctx);
ed.clamp();
fn move_cursels_const_repeat(self: *Self, move: Editor.cursel_operator_const, ctx: Context) Result {
const root = try self.buf_root();
try self.with_cursels_const_repeat(root, move, ctx);
self.clamp();
}
fn match_bracket(root: Buffer.Root, cursel: *CurSel, ctx: command.Context, metrics: Buffer.Metrics) error{Stop}!void {
@ -656,61 +644,61 @@ fn match_bracket(root: Buffer.Root, cursel: *CurSel, ctx: command.Context, metri
}
}
fn move_to_word(ctx: command.Context, move: Editor.cursor_operator_const, direction: Direction) command.Result {
const ed, const root = get_buf() orelse return;
fn move_to_word(self: *Self, ctx: command.Context, move: Editor.cursor_operator_const, direction: Direction) command.Result {
const root = try self.buf_root();
// NOR mode moves n words selecting the last one
var repeat: usize = 0;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
if (repeat > 1) ed.with_cursors_const_repeat(root, move, command.fmt(.{repeat - 1})) catch {};
if (repeat > 1) self.with_cursors_const_repeat(root, move, command.fmt(.{repeat - 1})) catch {};
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
var sel = Selection.from_cursor(&cursel.cursor);
const cur = sel.begin.test_at(root, is_not_whitespace_or_eol, ed.metrics);
const cur = sel.begin.test_at(root, is_not_whitespace_or_eol, self.metrics);
if (direction == .backwards) {
sel.begin.move_left(root, ed.metrics) catch continue;
const prev = sel.begin.test_at(root, Editor.is_not_word_char, ed.metrics);
sel.begin.move_left(root, self.metrics) catch continue;
const prev = sel.begin.test_at(root, Editor.is_not_word_char, self.metrics);
sel.begin = sel.end;
if (!cur or cur != prev)
sel.begin.move_right(root, ed.metrics) catch continue;
sel.begin.move_right(root, self.metrics) catch continue;
} else {
sel.end.move_right(root, ed.metrics) catch continue;
const next = sel.end.test_at(root, Editor.is_not_word_char, ed.metrics);
sel.end.move_right(root, self.metrics) catch continue;
const next = sel.end.test_at(root, Editor.is_not_word_char, self.metrics);
if (!cur and cur != next)
sel.begin = sel.end;
}
cursel.cursor = sel.end;
if (direction == .forwards)
sel.end.move_right(root, ed.metrics) catch {};
sel.end.move_right(root, self.metrics) catch {};
cursel.selection = sel;
};
ed.with_selections_const_repeat(root, move, command.fmt(.{1})) catch {};
ed.clamp();
self.with_selections_const_repeat(root, move, command.fmt(.{1})) catch {};
self.clamp();
}
fn extend_to_word(ctx: command.Context, move: Editor.cursor_operator_const, _: Direction) command.Result {
const ed, const root = get_buf() orelse return;
fn extend_to_word(self: *Self, ctx: command.Context, move: Editor.cursor_operator_const, _: Direction) command.Result {
const root = try self.buf_root();
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
const sel = cursel.enable_selection();
const pivot: usize = if (sel.is_reversed()) sel.begin.col -| 1 else sel.begin.col;
var i: usize = repeat;
while (i > 0) : (i -= 1) {
try move(root, &sel.end, ed.metrics);
try move(root, &sel.end, self.metrics);
}
sel.begin.col = if (sel.is_reversed()) pivot +| 1 else pivot;
cursel.cursor = sel.end;
};
ed.clamp();
self.clamp();
}
fn to_char_helix(ctx: command.Context, move: Editor.cursel_operator_mut_once_arg) command.Result {
const ed, const root = get_buf() orelse return;
try ed.with_cursels_const_once_arg(root, move, ctx);
ed.clamp();
fn to_char_helix(self: *Self, ctx: command.Context, move: Editor.cursel_operator_mut_once_arg) command.Result {
const root = try self.buf_root();
try self.with_cursels_const_once_arg(root, move, ctx);
self.clamp();
}
fn select_inner_word(root: Buffer.Root, cursel: *CurSel, metrics: Buffer.Metrics) !void {
@ -1062,7 +1050,7 @@ fn move_cursor_till_char_right_beyond_eol(root: Buffer.Root, cursor: *Cursor, me
}
}
fn add_cursors_to_cursel_line_ends_helix(ed: *Editor, root: Buffer.Root, cursel: *CurSel) !void {
fn add_cursors_to_cursel_line_ends(ed: *Editor, root: Buffer.Root, cursel: *CurSel) !void {
const original_cursel = cursel.*;
const sel = cursel.enable_selection();
sel.normalize();
@ -1282,8 +1270,8 @@ fn move_cursor_long_word_right_end(root: Buffer.Root, cursor: *Cursor, metrics:
const pasting_function = @TypeOf(insert_before);
const find_char_function = @TypeOf(move_cursor_to_char_left_beyond_eol);
fn paste_helix(ctx: command.Context, do_paste: pasting_function) command.Result {
const ed, const b = get_buf_for_update() orelse return;
fn paste_helix(self: *Self, ctx: command.Context, do_paste: pasting_function) command.Result {
const b = try self.buf_for_update();
var root = b.root;
var text_: []const u8 = undefined;
@ -1294,7 +1282,7 @@ fn paste_helix(ctx: command.Context, do_paste: pasting_function) command.Result
tui.clipboard_get_group(0);
if (clipboard.len == 0) {
ed.logger.print("paste: nothing to paste", .{});
self.logger.print("paste: nothing to paste", .{});
return;
}
@ -1303,20 +1291,20 @@ fn paste_helix(ctx: command.Context, do_paste: pasting_function) command.Result
// use the last chunk in the clipboard
var bytes: usize = 0;
for (ed.cursels.items, 0..) |*cursel_, idx| if (cursel_.*) |*cursel| {
for (self.cursels.items, 0..) |*cursel_, idx| if (cursel_.*) |*cursel| {
if (idx < clipboard.len) {
root = try do_paste(ed, root, cursel, clipboard[idx].text, b.allocator);
root = try do_paste(self, root, cursel, clipboard[idx].text, b.allocator);
bytes += clipboard[idx].text.len;
} else {
bytes += clipboard[clipboard.len - 1].text.len;
root = try do_paste(ed, root, cursel, clipboard[clipboard.len - 1].text, b.allocator);
root = try do_paste(self, root, cursel, clipboard[clipboard.len - 1].text, b.allocator);
}
};
ed.logger.print("paste: {d} bytes", .{bytes});
self.logger.print("paste: {d} bytes", .{bytes});
try ed.update_buf(root);
ed.clamp();
ed.need_render();
try self.update_buf(root);
self.clamp();
self.need_render();
}
fn move_cursor_carriage_return(root: Buffer.Root, cursel: CurSel, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {