fix: merge selections when collapsing cursors

closes #318
This commit is contained in:
CJ van den Berg 2025-11-03 18:31:51 +01:00
parent 79369bf2ca
commit 2d65864e74
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -1837,10 +1837,26 @@ pub const Editor = struct {
var old = self.cursels; var old = self.cursels;
defer old.deinit(self.allocator); defer old.deinit(self.allocator);
self.cursels = CurSel.List.initCapacity(self.allocator, old.items.len) catch return; self.cursels = CurSel.List.initCapacity(self.allocator, old.items.len) catch return;
for (old.items[0 .. old.items.len - 1], 0..) |*a_, i| if (a_.*) |*a| { var a_idx = old.items.len - 1;
for (old.items[i + 1 ..], i + 1..) |*b_, j| if (b_.*) |*b| { while (a_idx > 0) : (a_idx -= 1) if (old.items[a_idx]) |*a| {
if (a.cursor.eql(b.cursor)) var b_idx = a_idx - 1;
old.items[j] = null; while (true) : ({
if (b_idx == 0) break else b_idx -= 1;
}) if (old.items[b_idx]) |*b| {
const b_cursor = b.cursor;
if (b.selection) |b_sel| {
if (a.merge(b_sel)) {
old.items[b_idx] = null;
continue;
}
}
if (a.selection) |*a_sel| if (b_cursor.within(a_sel.*)) {
if (b.selection) |b_sel| a_sel.expand(b_sel);
old.items[b_idx] = null;
continue;
};
if (a.cursor.eql(b_cursor))
old.items[b_idx] = null;
}; };
}; };
for (old.items) |*item_| if (item_.*) |*item| { for (old.items) |*item_| if (item_.*) |*item| {