From 7778512c35a5af18af8b2d99f9c306e741aa7b1d Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Wed, 9 Apr 2025 21:16:41 -0300 Subject: [PATCH] Correcting selection after paste --- src/tui/editor.zig | 4 ++-- src/tui/mode/helix.zig | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 23b7837..fbd5740 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1963,7 +1963,7 @@ pub const Editor = struct { }; } - fn delete_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { + pub fn delete_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { var sel: Selection = cursel.selection orelse return error.Stop; sel.normalize(); cursel.cursor = sel.begin; @@ -3993,7 +3993,7 @@ pub const Editor = struct { cursel.cursor = sel.end; } - fn select_line_around_cursor(self: *Self, cursel: *CurSel) !void { + pub fn select_line_around_cursor(self: *Self, cursel: *CurSel) !void { const root = try self.buf_root(); const sel = try cursel.enable_selection(root, self.metrics); sel.normalize(); diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 63e7589..4588a2c 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -93,9 +93,8 @@ const cmds_ = struct { 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); cursel.cursor = sel.end; - try cursel.selection.?.end.move_right(root, ed.metrics); - try cursel.cursor.move_right(root, ed.metrics); }; } @@ -244,8 +243,6 @@ const cmds_ = struct { const b = try ed.buf_for_update(); var root = b.root; - if (std.mem.eql(u8, text[text.len - 1 ..], "\n")) text = text[0 .. text.len - 1]; - if (std.mem.indexOfScalar(u8, text, '\n')) |idx| { if (idx == 0) { for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { @@ -256,10 +253,10 @@ const cmds_ = struct { } if (ed.cursels.items.len == 1) { const primary = ed.get_primary(); - root = try ed.insert_line_vim(root, primary, text, b.allocator); + root = try insert_line(ed, root, primary, text, b.allocator); } else { for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { - root = try ed.insert_line_vim(root, cursel, text, b.allocator); + root = try insert_line(ed, root, cursel, text, b.allocator); }; } } else { @@ -319,5 +316,17 @@ fn insert(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, alloca cursor.row, cursor.col, root_ = try root_.insert_chars(cursor.row, cursor.col, s, allocator, ed.metrics); cursor.target = cursor.col; ed.nudge_insert(.{ .begin = begin, .end = cursor.* }, cursel, s.len); + cursel.selection = Selection{ .begin = begin, .end = cursor.* }; + return root_; +} + +fn insert_line(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: std.mem.Allocator) !Buffer.Root { + var root_ = root; + const cursor = &cursel.cursor; + const begin = cursel.cursor; + _, _, root_ = try root_.insert_chars(cursor.row, cursor.col, s, allocator, ed.metrics); + cursor.target = cursor.col; + ed.nudge_insert(.{ .begin = begin, .end = cursor.* }, cursel, s.len); + cursel.selection = Selection{ .begin = begin, .end = cursor.* }; return root_; }