refactor: use a match instead of the primary cursor in focus_on_range

This commit is contained in:
CJ van den Berg 2025-11-18 23:26:24 +01:00
parent f3440d4f83
commit f49d6a7423
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -5713,32 +5713,27 @@ pub const Editor = struct {
pub fn focus_on_range(self: *Self, ctx: Context) Result { pub fn focus_on_range(self: *Self, ctx: Context) Result {
var sel: Selection = .{}; var sel: Selection = .{};
var pos_type: PosType = .column;
if (!try ctx.args.match(.{ if (!try ctx.args.match(.{
tp.extract(&sel.begin.row), tp.extract(&sel.begin.row),
tp.extract(&sel.begin.col), tp.extract(&sel.begin.col),
tp.extract(&sel.end.row), tp.extract(&sel.end.row),
tp.extract(&sel.end.col), tp.extract(&sel.end.col),
tp.extract(&pos_type),
})) return error.InvalidFocusOnRangeArgument; })) return error.InvalidFocusOnRangeArgument;
self.cancel_all_selections(); self.cancel_all_matches();
const root = self.buf_root() catch return; self.add_match_internal(sel.begin.row + 1, sel.begin.col, sel.end.row + 1, sel.end.col);
if (pos_type == .byte) const cursor = sel.begin;
sel = sel.from_pos(root, self.metrics) catch return; const range_height = sel.end.row - sel.begin.row + 1;
const primary = self.get_primary(); const view_height = self.view.rows / 2;
try primary.cursor.move_to( const offset = if (range_height > view_height - @min(view_height, scroll_cursor_min_border_distance * 2))
root, scroll_cursor_min_border_distance
sel.begin.row,
sel.begin.col,
self.metrics,
);
primary.selection = sel;
if (self.view.is_visible(&primary.cursor))
self.clamp()
else else
try self.scroll_view_center(.{}); (view_height / 2) - (range_height / 2);
self.need_render(); const row = if (cursor.row > offset)
cursor.row - offset
else
0;
self.update_scroll_dest_abs(row);
} }
pub const focus_on_range_meta: Meta = .{ .arguments = &.{ .integer, .integer, .integer, .integer } }; pub const focus_on_range_meta: Meta = .{ .arguments = &.{ .integer, .integer, .integer, .integer } };