refactor: add Cursor.from_pos and Seleciton.from_range

And use them to clean-up all root.pos_to_width call sites.
This commit is contained in:
CJ van den Berg 2025-11-25 14:37:05 +01:00
parent be41027d75
commit 4a0150d68f
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 80 additions and 68 deletions

View file

@ -42,6 +42,13 @@ fn follow_target(self: *Self, root: Buffer.Root, metrics: Metrics) void {
self.col = @min(self.target, root.line_width(self.row, metrics) catch 0);
}
pub fn from_pos(self: Self, root: Buffer.Root, metrics: Buffer.Metrics) Self {
return .{
.row = self.row,
.col = root.pos_to_width(self.row, self.col, metrics) catch root.line_width(self.row, metrics) catch 0,
};
}
fn move_right_no_target(self: *Self, root: Buffer.Root, metrics: Metrics) !void {
const lines = root.lines();
if (lines <= self.row) return error.Stop;

View file

@ -32,6 +32,17 @@ pub fn from_pos(sel: Self, root: Buffer.Root, metrics: Buffer.Metrics) Self {
};
}
pub fn from_range(range: anytype, root: Buffer.Root, metrics: Buffer.Metrics) Self {
return from_pos(from_range_raw(range), root, metrics);
}
pub fn from_range_raw(range: anytype) Self {
return .{
.begin = .{ .row = range.start_point.row, .col = range.start_point.column },
.end = .{ .row = range.end_point.row, .col = range.end_point.column },
};
}
pub fn line_from_cursor(cursor: Cursor, root: Buffer.Root, mtrx: Buffer.Metrics) Self {
var begin = cursor;
var end = cursor;