From 4585c5af62d706b730c3a13dbedb600ce3138f56 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 31 Mar 2024 19:57:45 +0200 Subject: [PATCH] feat: add command to add cursors to all line ends --- help.md | 3 +++ src/tui/editor.zig | 27 +++++++++++++++++++++++++++ src/tui/mode/input/flow.zig | 1 + src/tui/mode/input/vim/insert.zig | 1 + src/tui/mode/input/vim/normal.zig | 1 + src/tui/mode/input/vim/visual.zig | 1 + 6 files changed, 34 insertions(+) diff --git a/help.md b/help.md index 4515754..95b0b20 100644 --- a/help.md +++ b/help.md @@ -142,6 +142,9 @@ cycle style of editing. - Ctrl-Shift-l => Add cursors to all matches +- Shift-Alt-i => + Add cursors to line ends + - Ctrl-MouseLeft => Add cursor at mouse click diff --git a/src/tui/editor.zig b/src/tui/editor.zig index cf0c6ae..8b5de80 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2098,6 +2098,33 @@ pub const Editor = struct { try self.send_editor_jump_destination(); } + fn add_cursors_to_cursel_line_ends(self: *Self, root: Buffer.Root, cursel: *CurSel) !void { + var sel = cursel.enable_selection(); + sel.normalize(); + var row = sel.begin.row; + while (row <= sel.end.row) : (row += 1) { + const new_cursel = try self.cursels.addOne(); + new_cursel.* = CurSel{ + .selection = null, + .cursor = .{ + .row = row, + .col = 0, + }, + }; + new_cursel.*.?.cursor.move_end(root); + } + } + + pub fn add_cursors_to_line_ends(self: *Self, _: command.Context) tp.result { + const root = self.buf_root() catch |e| return tp.exit_error(e); + const cursels = self.cursels.toOwnedSlice() catch |e| return tp.exit_error(e); + defer self.cursels.allocator.free(cursels); + for (cursels) |*cursel_| if (cursel_.*) |*cursel| + self.add_cursors_to_cursel_line_ends(root, cursel) catch |e| return tp.exit_error(e); + self.collapse_cursors(); + self.clamp(); + } + fn pull_cursel_up(self: *Self, root_: Buffer.Root, cursel: *CurSel, a: Allocator) error{Stop}!Buffer.Root { var root = root_; const saved = cursel.*; diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index 447eb35..3d84b0e 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -152,6 +152,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'F' => self.cmd("filter", command.fmt(.{ "zig", "fmt", "--stdin" })), 'S' => self.cmd("filter", command.fmt(.{ "sort", "-u" })), 'V' => self.cmd("paste", .{}), + 'I' => self.cmd("add_cursors_to_line_ends", .{}), key.LEFT => self.cmd("move_scroll_left", .{}), key.RIGHT => self.cmd("move_scroll_right", .{}), key.UP => self.cmd("add_cursor_up", .{}), diff --git a/src/tui/mode/input/vim/insert.zig b/src/tui/mode/input/vim/insert.zig index 7327cc1..b61ecc3 100644 --- a/src/tui/mode/input/vim/insert.zig +++ b/src/tui/mode/input/vim/insert.zig @@ -152,6 +152,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'F' => self.cmd("filter", command.fmt(.{ "zig", "fmt", "--stdin" })), 'S' => self.cmd("filter", command.fmt(.{ "sort", "-u" })), 'V' => self.cmd("paste", .{}), + 'I' => self.cmd("add_cursors_to_line_ends", .{}), key.LEFT => self.cmd("move_scroll_left", .{}), key.RIGHT => self.cmd("move_scroll_right", .{}), key.UP => self.cmd("add_cursor_up", .{}), diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index de9db74..0217810 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -157,6 +157,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'F' => self.cmd("filter", command.fmt(.{ "zig", "fmt", "--stdin" })), 'S' => self.cmd("filter", command.fmt(.{ "sort", "-u" })), 'V' => self.cmd("paste", .{}), + 'I' => self.cmd("add_cursors_to_line_ends", .{}), key.LEFT => self.cmd("move_scroll_left", .{}), key.RIGHT => self.cmd("move_scroll_right", .{}), key.UP => self.cmd("add_cursor_up", .{}), diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 8d84902..3ce67a8 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -155,6 +155,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'F' => self.cmd("filter", command.fmt(.{ "zig", "fmt", "--stdin" })), 'S' => self.cmd("filter", command.fmt(.{ "sort", "-u" })), 'V' => self.cmd("paste", .{}), + 'I' => self.cmd("add_cursors_to_line_ends", .{}), key.LEFT => self.cmd("move_scroll_left", .{}), key.RIGHT => self.cmd("move_scroll_right", .{}), else => {},