fix: left an right movements jump to selection boundary

When there is an active selection, basic left and right movements should cancel
the selection and place the cursor on the left/right of where the selection was.

closes #244
This commit is contained in:
CJ van den Berg 2025-05-15 15:52:36 +02:00
parent 1eb36696d2
commit 6618a2d84d
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -3042,17 +3042,41 @@ pub const Editor = struct {
}
pub const join_next_line_meta: Meta = .{ .description = "Join next line", .arguments = &.{.integer} };
pub fn move_left(self: *Self, ctx: Context) Result {
fn move_cursors_or_collapse_selection(
self: *Self,
direction: enum { left, right },
ctx: Context,
) error{Stop}!void {
const root = try self.buf_root();
self.with_cursors_const_repeat(root, move_cursor_left, ctx) catch {};
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
while (repeat > 0) : (repeat -= 1) {
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
if (cursel.selection) |*sel| {
cursel.cursor = switch (direction) {
.left => if (sel.is_reversed()) sel.end else sel.begin,
.right => if (sel.is_reversed()) sel.begin else sel.end,
};
cursel.disable_selection(root, self.metrics);
} else {
try with_cursor_const(root, switch (direction) {
.left => move_cursor_left,
.right => move_cursor_right,
}, cursel, self.metrics);
}
};
self.collapse_cursors();
}
self.clamp();
}
pub fn move_left(self: *Self, ctx: Context) Result {
self.move_cursors_or_collapse_selection(.left, ctx) catch {};
}
pub const move_left_meta: Meta = .{ .description = "Move cursor left", .arguments = &.{.integer} };
pub fn move_right(self: *Self, ctx: Context) Result {
const root = try self.buf_root();
self.with_cursors_const_repeat(root, move_cursor_right, ctx) catch {};
self.clamp();
self.move_cursors_or_collapse_selection(.right, ctx) catch {};
}
pub const move_right_meta: Meta = .{ .description = "Move cursor right", .arguments = &.{.integer} };