refactor: move more mode specific commands to helix & vim

This commit is contained in:
CJ van den Berg 2025-11-27 18:03:53 +01:00
parent 8d8f4b82cb
commit 66f8819a19
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 274 additions and 262 deletions

View file

@ -1628,7 +1628,7 @@ pub const Editor = struct {
return row < sel.begin.row or (row == sel.begin.row and col < sel.begin.col);
}
inline fn screen_cursor(self: *const Self, cursor: *const Cursor) ?Cursor {
pub inline fn screen_cursor(self: *const Self, cursor: *const Cursor) ?Cursor {
return if (self.view.is_visible(cursor)) .{
.row = cursor.row - self.view.row,
.col = cursor.col - self.view.col,
@ -1741,7 +1741,7 @@ pub const Editor = struct {
try self.send_editor_cursel_msg("jump_source", self.get_primary());
}
fn send_editor_jump_destination(self: *Self) !void {
pub fn send_editor_jump_destination(self: *Self) !void {
try self.send_editor_cursel_msg("jump_destination", self.get_primary());
}
@ -1959,7 +1959,7 @@ pub const Editor = struct {
try move(root, &cursel.cursor, view, metrics);
}
fn with_cursors_and_view_const(self: *Self, root: Buffer.Root, move: cursor_view_operator_const, view: *const View) error{Stop}!void {
pub fn with_cursors_and_view_const(self: *Self, root: Buffer.Root, move: cursor_view_operator_const, view: *const View) error{Stop}!void {
var someone_stopped = false;
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel|
with_cursor_and_view_const(root, move, cursel, view, self.metrics) catch {
@ -2028,7 +2028,7 @@ pub const Editor = struct {
cursel.cursor = sel.end;
}
fn with_selections_and_view_const(self: *Self, root: Buffer.Root, move: cursor_view_operator_const, view: *const View) error{Stop}!void {
pub fn with_selections_and_view_const(self: *Self, root: Buffer.Root, move: cursor_view_operator_const, view: *const View) error{Stop}!void {
var someone_stopped = false;
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel|
with_selection_and_view_const(root, move, cursel, view, self.metrics) catch {
@ -2346,7 +2346,7 @@ pub const Editor = struct {
return false;
}
fn is_eol_left(_: Buffer.Root, cursor: *const Cursor, _: Buffer.Metrics) bool {
pub fn is_eol_left(_: Buffer.Root, cursor: *const Cursor, _: Buffer.Metrics) bool {
if (cursor.col == 0)
return true;
return false;
@ -2359,22 +2359,6 @@ pub const Editor = struct {
return false;
}
fn is_eol_right_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool {
const line_width = root.line_width(cursor.row, metrics) catch return true;
if (line_width == 0) return true;
if (cursor.col >= line_width - 1)
return true;
return false;
}
fn is_eol_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool {
const line_width = root.line_width(cursor.row, metrics) catch return true;
if (line_width == 0) return true;
if (cursor.col >= line_width)
return true;
return false;
}
pub fn move_cursor_left(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
try cursor.move_left(root, metrics);
}
@ -2384,7 +2368,7 @@ pub const Editor = struct {
move_cursor_left(root, cursor, metrics) catch return;
}
fn move_cursor_left_unless(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate, metrics: Buffer.Metrics) void {
pub fn move_cursor_left_unless(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate, metrics: Buffer.Metrics) void {
if (!pred(root, cursor, metrics))
move_cursor_left(root, cursor, metrics) catch return;
}
@ -2407,7 +2391,7 @@ pub const Editor = struct {
move_cursor_right(root, cursor, metrics) catch return;
}
fn move_cursor_right_unless(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate, metrics: Buffer.Metrics) void {
pub fn move_cursor_right_unless(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate, metrics: Buffer.Metrics) void {
if (!pred(root, cursor, metrics))
move_cursor_right(root, cursor, metrics) catch return;
}
@ -2416,32 +2400,18 @@ pub const Editor = struct {
cursor.move_end(root, metrics);
}
fn move_cursor_end_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void {
move_cursor_right_until(root, cursor, is_eol_vim, metrics);
}
fn move_cursor_up(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void {
cursor.move_up(root, metrics) catch |e| switch (e) {
error.Stop => cursor.move_begin(),
};
}
fn move_cursor_up_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void {
try cursor.move_up(root, metrics);
if (is_eol_vim(root, cursor, metrics)) try move_cursor_left_vim(root, cursor, metrics);
}
pub fn move_cursor_down(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void {
cursor.move_down(root, metrics) catch |e| switch (e) {
error.Stop => cursor.move_end(root, metrics),
};
}
fn move_cursor_down_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void {
try cursor.move_down(root, metrics);
if (is_eol_vim(root, cursor, metrics)) try move_cursor_left_vim(root, cursor, metrics);
}
fn move_cursor_buffer_begin(_: Buffer.Root, cursor: *Cursor, _: Buffer.Metrics) !void {
cursor.move_buffer_begin();
}
@ -2458,24 +2428,6 @@ pub const Editor = struct {
cursor.move_page_down(root, view, metrics);
}
fn move_cursor_half_page_up(root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) !void {
cursor.move_half_page_up(root, view, metrics);
}
fn move_cursor_half_page_up_vim(root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) !void {
cursor.move_half_page_up(root, view, metrics);
if (is_eol_vim(root, cursor, metrics)) try move_cursor_left_vim(root, cursor, metrics);
}
fn move_cursor_half_page_down(root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) !void {
cursor.move_half_page_down(root, view, metrics);
}
fn move_cursor_half_page_down_vim(root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) !void {
cursor.move_half_page_down(root, view, metrics);
if (is_eol_vim(root, cursor, metrics)) try move_cursor_left_vim(root, cursor, metrics);
}
pub fn primary_click(self: *Self, y: c_int, x: c_int) !void {
const root = self.buf_root() catch return;
if (self.fast_scroll) {
@ -3190,14 +3142,6 @@ pub const Editor = struct {
}
pub const delete_line_meta: Meta = .{ .description = "Delete current line", .arguments = &.{.integer} };
pub fn cut_to_end_vim(self: *Self, _: Context) Result {
const b = try self.buf_for_update();
const root = try self.cut_to(move_cursor_end_vim, b.root);
try self.update_buf(root);
self.clamp();
}
pub const cut_to_end_vim_meta: Meta = .{ .description = "Cut to end of line (vim)" };
pub fn join_next_line(self: *Self, ctx: Context) Result {
const b = try self.buf_for_update();
try self.with_cursors_const_repeat(b.root, move_cursor_end, ctx);
@ -3248,28 +3192,6 @@ pub const Editor = struct {
}
pub const move_right_meta: Meta = .{ .description = "Move cursor right", .arguments = &.{.integer} };
fn move_cursor_left_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
move_cursor_left_unless(root, cursor, is_eol_left, metrics);
}
fn move_cursor_right_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
move_cursor_right_unless(root, cursor, is_eol_right_vim, metrics);
}
pub fn move_left_vim(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
self.with_cursors_const_repeat(root, move_cursor_left_vim, ctx) catch {};
self.clamp();
}
pub const move_left_vim_meta: Meta = .{ .description = "Move cursor left (vim)", .arguments = &.{.integer} };
pub fn move_right_vim(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
self.with_cursors_const_repeat(root, move_cursor_right_vim, ctx) catch {};
self.clamp();
}
pub const move_right_vim_meta: Meta = .{ .description = "Move cursor right (vim)", .arguments = &.{.integer} };
fn move_cursor_word_begin(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
if (is_non_word_char_at_cursor(root, cursor, metrics)) {
move_cursor_left_until(root, cursor, is_word_boundary_right, metrics);
@ -3556,13 +3478,6 @@ pub const Editor = struct {
}
pub const move_up_meta: Meta = .{ .description = "Move cursor up", .arguments = &.{.integer} };
pub fn move_up_vim(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
self.with_cursors_const_repeat(root, move_cursor_up_vim, ctx) catch {};
self.clamp();
}
pub const move_up_vim_meta: Meta = .{ .description = "Move cursor up (vim)", .arguments = &.{.integer} };
pub fn add_cursor_up(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
var repeat: usize = 1;
@ -3583,13 +3498,6 @@ pub const Editor = struct {
}
pub const move_down_meta: Meta = .{ .description = "Move cursor down", .arguments = &.{.integer} };
pub fn move_down_vim(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
self.with_cursors_const_repeat(root, move_cursor_down_vim, ctx) catch {};
self.clamp();
}
pub const move_down_vim_meta: Meta = .{ .description = "Move cursor down (vim)", .arguments = &.{.integer} };
pub fn add_cursor_down(self: *Self, ctx: Context) Result {
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
@ -4027,54 +3935,6 @@ pub const Editor = struct {
}
pub const move_scroll_page_down_meta: Meta = .{ .description = "Move and scroll page down" };
pub fn move_scroll_half_page_up(self: *Self, _: Context) Result {
if (self.screen_cursor(&self.get_primary().cursor)) |cursor| {
const root = try self.buf_root();
self.with_cursors_and_view_const(root, move_cursor_half_page_up, &self.view) catch {};
const new_cursor_row = self.get_primary().cursor.row;
self.update_scroll_dest_abs(if (cursor.row > new_cursor_row) 0 else new_cursor_row - cursor.row);
} else {
return self.move_half_page_up(.{});
}
}
pub const move_scroll_half_page_up_meta: Meta = .{ .description = "Move and scroll half a page up" };
pub fn move_scroll_half_page_up_vim(self: *Self, _: Context) Result {
if (self.screen_cursor(&self.get_primary().cursor)) |cursor| {
const root = try self.buf_root();
self.with_cursors_and_view_const(root, move_cursor_half_page_up_vim, &self.view) catch {};
const new_cursor_row = self.get_primary().cursor.row;
self.update_scroll_dest_abs(if (cursor.row > new_cursor_row) 0 else new_cursor_row - cursor.row);
} else {
return self.move_half_page_up(.{});
}
}
pub const move_scroll_half_page_up_vim_meta: Meta = .{ .description = "Move and scroll half a page up (vim)" };
pub fn move_scroll_half_page_down(self: *Self, _: Context) Result {
if (self.screen_cursor(&self.get_primary().cursor)) |cursor| {
const root = try self.buf_root();
self.with_cursors_and_view_const(root, move_cursor_half_page_down, &self.view) catch {};
const new_cursor_row = self.get_primary().cursor.row;
self.update_scroll_dest_abs(if (cursor.row > new_cursor_row) 0 else new_cursor_row - cursor.row);
} else {
return self.move_half_page_down(.{});
}
}
pub const move_scroll_half_page_down_meta: Meta = .{ .description = "Move and scroll half a page down" };
pub fn move_scroll_half_page_down_vim(self: *Self, _: Context) Result {
if (self.screen_cursor(&self.get_primary().cursor)) |cursor| {
const root = try self.buf_root();
self.with_cursors_and_view_const(root, move_cursor_half_page_down_vim, &self.view) catch {};
const new_cursor_row = self.get_primary().cursor.row;
self.update_scroll_dest_abs(if (cursor.row > new_cursor_row) 0 else new_cursor_row - cursor.row);
} else {
return self.move_half_page_down(.{});
}
}
pub const move_scroll_half_page_down_vim_meta: Meta = .{ .description = "Move and scroll half a page down (vim)" };
pub fn smart_move_begin(self: *Self, _: Context) Result {
const root = try self.buf_root();
try self.with_cursors_const_once(root, smart_move_cursor_begin);
@ -4363,24 +4223,6 @@ pub const Editor = struct {
}
pub const select_page_down_meta: Meta = .{ .description = "Select page down" };
pub fn select_half_page_up(self: *Self, _: Context) Result {
try self.send_editor_jump_source();
const root = try self.buf_root();
try self.with_selections_and_view_const(root, move_cursor_half_page_up, &self.view);
self.clamp();
try self.send_editor_jump_destination();
}
pub const select_half_page_up_meta: Meta = .{ .description = "Select half a page up" };
pub fn select_half_page_down(self: *Self, _: Context) Result {
try self.send_editor_jump_source();
const root = try self.buf_root();
try self.with_selections_and_view_const(root, move_cursor_half_page_down, &self.view);
self.clamp();
try self.send_editor_jump_destination();
}
pub const select_half_page_down_meta: Meta = .{ .description = "Select half a page down" };
pub fn select_all(self: *Self, _: Context) Result {
try self.send_editor_jump_source();
self.cancel_all_selections();