fix: improve dupe_cursel_up to correctly handle duplicating at EOF

This commit is contained in:
CJ van den Berg 2025-11-02 19:23:09 +01:00
parent 1c486ccd93
commit e1e82a57eb
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -3568,15 +3568,28 @@ pub const Editor = struct {
fn dupe_cursel_up(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { fn dupe_cursel_up(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root {
var root = root_; var root = root_;
const sel: Selection = if (cursel.selection) |sel_| sel_ else Selection.line_from_cursor(cursel.cursor, root, self.metrics); var sel: Selection = if (cursel.selection) |sel_| sel_ else Selection.line_from_cursor(cursel.cursor, root, self.metrics);
cursel.disable_selection(root, self.metrics);
var sfa = std.heap.stackFallback(4096, self.allocator); var sfa = std.heap.stackFallback(4096, self.allocator);
const sfa_allocator = sfa.get(); const sfa_allocator = sfa.get();
const text = copy_selection(root, sel, sfa_allocator, self.metrics) catch return error.Stop; const text = copy_selection(root, sel, sfa_allocator, self.metrics) catch return error.Stop;
defer sfa_allocator.free(text); defer sfa_allocator.free(text);
cursel.cursor = sel.begin; cursel.cursor = sel.begin;
var add_eol = false;
if (cursel.selection) |_| {
cursel.disable_selection(root, self.metrics);
} else {
var test_eof = sel.end;
test_eof.move_right(root, self.metrics) catch { // test for EOF
add_eol = true;
};
}
root = self.insert(root, cursel, text, allocator) catch return error.Stop; root = self.insert(root, cursel, text, allocator) catch return error.Stop;
cursel.selection = .{ .begin = sel.begin, .end = sel.end }; if (add_eol) {
root = self.insert(root, cursel, "\n", allocator) catch return error.Stop;
sel.end.move_right(root, self.metrics) catch {};
}
cursel.selection = .{ .begin = sel.end, .end = sel.begin };
cursel.cursor = sel.begin; cursel.cursor = sel.begin;
return root; return root;
} }