From d7e162892a89ced0a7a2c5456dc83478eaf266b3 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 14 Feb 2025 20:23:19 +0100 Subject: [PATCH 1/4] refactor: use an enum to select move_to_char direction instead of a bool --- src/keybind/builtin/flow.json | 4 ++-- src/tui/mode/mini/move_to_char.zig | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/keybind/builtin/flow.json b/src/keybind/builtin/flow.json index 79439e2..14d8500 100644 --- a/src/keybind/builtin/flow.json +++ b/src/keybind/builtin/flow.json @@ -30,8 +30,8 @@ ["ctrl+l", "scroll_view_center_cycle"], ["ctrl+n", "goto_next_match"], ["ctrl+p", "goto_prev_match"], - ["ctrl+b", "move_to_char", false], - ["ctrl+t", "move_to_char", true], + ["ctrl+b", "move_to_char", "left"], + ["ctrl+t", "move_to_char", "right"], ["ctrl+x", "cut"], ["ctrl+c", "copy"], ["ctrl+v", "system_paste"], diff --git a/src/tui/mode/mini/move_to_char.zig b/src/tui/mode/mini/move_to_char.zig index ad90339..3cc5341 100644 --- a/src/tui/mode/mini/move_to_char.zig +++ b/src/tui/mode/mini/move_to_char.zig @@ -30,13 +30,13 @@ const Operation = enum { }; pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } { - var right: bool = true; + var direction: Direction = undefined; const select = if (tui.get_active_editor()) |editor| if (editor.get_primary().selection) |_| true else false else false; - _ = ctx.args.match(.{tp.extract(&right)}) catch return error.InvalidMoveToCharArgument; + _ = ctx.args.match(.{tp.extract(&direction)}) catch return error.InvalidMoveToCharArgument; const self: *Self = try allocator.create(Self); self.* = .{ .allocator = allocator, - .direction = if (right) .right else .left, + .direction = direction, .operation = if (select) .select else .move, }; try self.commands.init(self); From 1eb6a773e5a0369bcdd38cdc4cf94077156d9753 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 14 Feb 2025 20:24:04 +0100 Subject: [PATCH 2/4] feat(vim): add f and F keybindings for move_to_char --- src/keybind/builtin/vim.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/keybind/builtin/vim.json b/src/keybind/builtin/vim.json index a3ba41f..3871c22 100644 --- a/src/keybind/builtin/vim.json +++ b/src/keybind/builtin/vim.json @@ -85,6 +85,9 @@ ["", "TODO"], + ["F", "move_to_char", "left"], + ["f", "move_to_char", "right"], + ["", ["move_down"], ["move_begin"]], ["", ["move_down"], ["move_begin"]] ] From fdabe03e91b49253ccaa0b5697a4c7a938a6f9ed Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 19 Feb 2025 18:41:46 +0100 Subject: [PATCH 3/4] refactor: run zig fmt --- src/tui/editor.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 7e248cd..60649c6 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2088,7 +2088,7 @@ pub const Editor = struct { 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); + if (is_eol_vim(root, cursor, metrics)) try move_cursor_left_vim(root, cursor, metrics); } fn move_cursor_down(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void { @@ -2097,7 +2097,7 @@ pub const Editor = struct { 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); + 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 { @@ -2122,7 +2122,7 @@ pub const Editor = struct { 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); + 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 { @@ -2131,7 +2131,7 @@ pub const Editor = struct { 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); + 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 { @@ -3480,7 +3480,7 @@ pub const Editor = struct { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| try self.select_line_around_cursor(cursel); self.collapse_cursors(); - + self.clamp(); } pub const select_line_vim_meta = .{ .description = "Select the line around the cursor (vim)" }; From c7cca545b9ceaf0a66885544caf020ecb63e66c5 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 19 Feb 2025 18:42:14 +0100 Subject: [PATCH 4/4] fix: make move_cursor_up/_down fallback to move_begin/_end closes #185 --- src/tui/editor.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 60649c6..158c9d5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2083,7 +2083,9 @@ pub const Editor = struct { } fn move_cursor_up(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void { - try cursor.move_up(root, metrics); + 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 { @@ -2092,7 +2094,9 @@ pub const Editor = struct { } fn move_cursor_down(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) !void { - try cursor.move_down(root, metrics); + 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 {