refactor: expose CurSel.to_selection method

This commit is contained in:
CJ van den Berg 2025-09-22 12:50:14 +02:00
parent 99dc805817
commit 30af629a1a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -102,37 +102,41 @@ pub const CurSel = struct {
} }
pub fn enable_selection(self: *Self, root: Buffer.Root, metrics: Buffer.Metrics) !*Selection { pub fn enable_selection(self: *Self, root: Buffer.Root, metrics: Buffer.Metrics) !*Selection {
return switch (tui.get_selection_style()) { self.selection = try self.to_selection(root, metrics);
.normal => self.enable_selection_normal(), return if (self.selection) |*sel| sel else unreachable;
.inclusive => try self.enable_selection_inclusive(root, metrics),
};
} }
pub fn enable_selection_normal(self: *Self) *Selection { pub fn enable_selection_normal(self: *Self) *Selection {
return if (self.selection) |*sel| self.selection = self.to_selection_normal();
sel return if (self.selection) |*sel| sel else unreachable;
else cod: { }
self.selection = Selection.from_cursor(&self.cursor);
break :cod &self.selection.?; pub fn to_selection(self: *const Self, root: Buffer.Root, metrics: Buffer.Metrics) !Selection {
return switch (tui.get_selection_style()) {
.normal => self.to_selection_normal(),
.inclusive => try self.to_selection_inclusive(root, metrics),
}; };
} }
fn enable_selection_inclusive(self: *Self, root: Buffer.Root, metrics: Buffer.Metrics) !*Selection { fn to_selection_normal(self: *const Self) Selection {
return if (self.selection) |*sel| return if (self.selection) |sel| sel else Selection.from_cursor(&self.cursor);
}
fn to_selection_inclusive(self: *const Self, root: Buffer.Root, metrics: Buffer.Metrics) !Selection {
return if (self.selection) |sel|
sel sel
else cod: { else cod: {
self.selection = Selection.from_cursor(&self.cursor); var sel = Selection.from_cursor(&self.cursor);
try self.selection.?.end.move_right(root, metrics); try sel.end.move_right(root, metrics);
try self.cursor.move_right(root, metrics); break :cod sel;
break :cod &self.selection.?;
}; };
} }
fn to_inclusive_cursor(self: *const Self, root: Buffer.Root, metrics: Buffer.Metrics) !Cursor { fn to_cursor_inclusive(self: *const Self, root: Buffer.Root, metrics: Buffer.Metrics) !Cursor {
var res = self.cursor; var cursor = self.cursor;
if (self.selection) |sel| if (!sel.is_reversed()) if (self.selection) |sel| if (!sel.is_reversed())
try res.move_left(root, metrics); try cursor.move_left(root, metrics);
return res; return cursor;
} }
pub fn disable_selection(self: *Self, root: Buffer.Root, metrics: Buffer.Metrics) void { pub fn disable_selection(self: *Self, root: Buffer.Root, metrics: Buffer.Metrics) void {
@ -1207,7 +1211,7 @@ pub const Editor = struct {
fn get_rendered_cursor(self: *Self, style: anytype, cursel: anytype) !Cursor { fn get_rendered_cursor(self: *Self, style: anytype, cursel: anytype) !Cursor {
return switch (style) { return switch (style) {
.normal => cursel.cursor, .normal => cursel.cursor,
.inclusive => try cursel.to_inclusive_cursor(try self.buf_root(), self.metrics), .inclusive => try cursel.to_cursor_inclusive(try self.buf_root(), self.metrics),
}; };
} }