refactor: add some more explicit error handling to Editor.copy_selection

This removes NoSpaceLeft as a possible error case, which should be impossible.
This commit is contained in:
CJ van den Berg 2026-01-30 09:14:28 +01:00
parent 94aa462a8d
commit 211648b2c9
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -2966,14 +2966,20 @@ pub const Editor = struct {
} }
pub const scroll_view_bottom_meta: Meta = .{}; pub const scroll_view_bottom_meta: Meta = .{};
pub fn copy_selection(root: Buffer.Root, sel: Selection, text_allocator: Allocator, metrics: Buffer.Metrics) ![]u8 { pub fn copy_selection(root: Buffer.Root, sel: Selection, text_allocator: Allocator, metrics: Buffer.Metrics) error{ Stop, OutOfMemory }![]u8 {
var size: usize = 0; var size: usize = 0;
_ = try root.get_range(sel, null, &size, null, metrics); _ = root.get_range(sel, null, &size, null, metrics) catch |e| switch (e) {
error.NoSpaceLeft => @panic("copy_selection:size"), // get_range can only return NoSpaceLeft if passed a copy_buf
else => |e_| return e_,
};
const buf__ = try text_allocator.alloc(u8, size); const buf__ = try text_allocator.alloc(u8, size);
return (try root.get_range(sel, buf__, null, null, metrics)).?; return (root.get_range(sel, buf__, null, null, metrics) catch |e| switch (e) {
error.NoSpaceLeft => @panic("copy_selection:buf__"), // buf__.len does not match &size, which should be impossible
else => |e_| return e_,
}) orelse @panic("copy_selection:null");
} }
pub fn get_selection(self: *const Self, sel: Selection, text_allocator: Allocator) ![]u8 { pub fn get_selection(self: *const Self, sel: Selection, text_allocator: Allocator) error{ Stop, OutOfMemory }![]u8 {
return copy_selection(try self.buf_root(), sel, text_allocator, self.metrics); return copy_selection(try self.buf_root(), sel, text_allocator, self.metrics);
} }