From 8f8d4b61456373d3689850dee9da0c75aebf079f Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 31 Mar 2024 19:04:39 +0200 Subject: [PATCH] feat: add command to add cursors to all matches Also, move inspector view to Ctrl-Shift-i --- help.md | 9 ++++++--- src/tui/editor.zig | 25 ++++++++++++++++++++++++- src/tui/mode/input/flow.zig | 4 ++-- src/tui/mode/input/vim/insert.zig | 4 ++-- src/tui/mode/input/vim/normal.zig | 4 ++-- src/tui/mode/input/vim/visual.zig | 4 ++-- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/help.md b/help.md index 878abbd..4515754 100644 --- a/help.md +++ b/help.md @@ -139,6 +139,9 @@ cycle style of editing. - Alt-Shift-Down, Alt-Shift-Up => Add cursor on the previous/next line +- Ctrl-Shift-l => + Add cursors to all matches + - Ctrl-MouseLeft => Add cursor at mouse click @@ -237,7 +240,7 @@ cycle style of editing. ### Debugging Commands -- F5 => +- F5, Ctrl-Shift-i => Toggle inspector view - F6 => @@ -246,10 +249,10 @@ cycle style of editing. - F7 => Dump current line to log view -- F11, Ctrl-J, Ctrl-Shift-l => +- F11, Ctrl-J, Alt-l => Toggle log view -- F12, Ctrl-Shift-i => +- F12, Alt-i => Toggle input view - Ctrl-Shift-/ => diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 82f3f1d..cf0c6ae 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2083,6 +2083,21 @@ pub const Editor = struct { try self.send_editor_jump_destination(); } + pub fn add_cursor_all_matches(self: *Self, _: command.Context) tp.result { + if (self.matches.items.len == 0) return; + try self.send_editor_jump_source(); + while (self.get_next_match(self.get_primary().cursor)) |match| { + self.push_cursor() catch |e| return tp.exit_error(e); + const primary = self.get_primary(); + const root = self.buf_root() catch return; + primary.selection = match.to_selection(); + match.has_selection = true; + primary.cursor.move_to(root, match.end.row, match.end.col) catch return; + } + self.clamp(); + try self.send_editor_jump_destination(); + } + fn pull_cursel_up(self: *Self, root_: Buffer.Root, cursel: *CurSel, a: Allocator) error{Stop}!Buffer.Root { var root = root_; const saved = cursel.*; @@ -2954,6 +2969,14 @@ pub const Editor = struct { (self.matches.addOne() catch return).* = match; } + fn scan_first_match(self: *const Self) ?*Match { + for (self.matches.items) |*match_| if (match_.*) |*match| { + if (match.has_selection) continue; + return match; + }; + return null; + } + fn scan_next_match(self: *const Self, cursor: Cursor) ?*Match { const row = cursor.row; const col = cursor.col; @@ -2968,7 +2991,7 @@ pub const Editor = struct { if (self.scan_next_match(cursor)) |match| return match; var cursor_ = cursor; cursor_.move_buffer_begin(); - return self.scan_next_match(cursor_); + return self.scan_first_match(); } fn scan_prev_match(self: *const Self, cursor: Cursor) ?*Match { diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index 184b2ce..447eb35 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -116,8 +116,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'Q' => self.cmd("quit_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), - 'L' => self.cmd_async("toggle_logview"), - 'I' => self.cmd_async("toggle_inputview"), + 'L' => self.cmd_async("add_cursor_all_matches"), + 'I' => self.cmd_async("toggle_inspector_view"), '/' => self.cmd("log_widgets", .{}), key.ENTER => self.cmd("smart_insert_line_before", .{}), key.END => self.cmd("select_buffer_end", .{}), diff --git a/src/tui/mode/input/vim/insert.zig b/src/tui/mode/input/vim/insert.zig index 0acb93b..7327cc1 100644 --- a/src/tui/mode/input/vim/insert.zig +++ b/src/tui/mode/input/vim/insert.zig @@ -118,8 +118,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'Q' => self.cmd("quit_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), - 'L' => self.cmd_async("toggle_logview"), - 'I' => self.cmd_async("toggle_inputview"), + 'L' => self.cmd_async("add_cursor_all_matches"), + 'I' => self.cmd_async("toggle_inspector_view"), '/' => self.cmd("log_widgets", .{}), key.ENTER => self.cmd("smart_insert_line_before", .{}), key.END => self.cmd("select_buffer_end", .{}), diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index 3f95f2f..de9db74 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -121,8 +121,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'Q' => self.cmd("quit_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), - 'L' => self.cmd_async("toggle_logview"), - 'I' => self.cmd_async("toggle_inputview"), + 'L' => self.cmd_async("add_cursor_all_matches"), + 'I' => self.cmd_async("toggle_inspector_view"), '/' => self.cmd("log_widgets", .{}), key.ENTER => self.cmd("smart_insert_line_before", .{}), key.END => self.cmd("select_buffer_end", .{}), diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 7796507..8d84902 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -121,8 +121,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'Q' => self.cmd("quit_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), - 'L' => self.cmd_async("toggle_logview"), - 'I' => self.cmd_async("toggle_inputview"), + 'L' => self.cmd_async("add_cursor_all_matches"), + 'I' => self.cmd_async("toggle_inspector_view"), '/' => self.cmd("log_widgets", .{}), key.ENTER => self.cmd("smart_insert_line_before", .{}), key.END => self.cmd("select_buffer_end", .{}),