feat: add smart_delete_backward command for improved smart pair handling

This commit is contained in:
CJ van den Berg 2025-03-04 19:51:31 +01:00
parent e34aed4a95
commit c984f3c392
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 63 additions and 1 deletions

View file

@ -38,6 +38,25 @@ pub fn control_code_to_unicode(code: u8) [:0]const u8 {
}; };
} }
pub const char_pairs = [_]struct { []const u8, []const u8 }{
.{ "\"", "\"" },
.{ "'", "'" },
.{ "(", ")" },
.{ "(", ")" },
.{ "[", "]" },
.{ "[", "]" },
.{ "{", "}" },
.{ "{", "}" },
.{ "", "" },
.{ "", "" },
.{ "", "" },
.{ "", "" },
.{ "", "" },
.{ "", "" },
.{ "«", "»" },
.{ "«", "»" },
};
fn raw_byte_to_utf8(cp: u8, buf: []u8) ![]const u8 { fn raw_byte_to_utf8(cp: u8, buf: []u8) ![]const u8 {
var utf16le: [1]u16 = undefined; var utf16le: [1]u16 = undefined;
const utf16le_as_bytes = std.mem.sliceAsBytes(utf16le[0..]); const utf16le_as_bytes = std.mem.sliceAsBytes(utf16le[0..]);

View file

@ -149,7 +149,7 @@
["escape", "cancel"], ["escape", "cancel"],
["enter", "smart_insert_line"], ["enter", "smart_insert_line"],
["delete", "delete_forward"], ["delete", "delete_forward"],
["backspace", "delete_backward"], ["backspace", "smart_delete_backward"],
["left", "move_left"], ["left", "move_left"],
["right", "move_right"], ["right", "move_right"],
["up", "move_up"], ["up", "move_up"],

View file

@ -2773,6 +2773,49 @@ pub const Editor = struct {
} }
pub const delete_backward_meta = .{ .description = "Delete previous character" }; pub const delete_backward_meta = .{ .description = "Delete previous character" };
pub fn smart_delete_backward(self: *Self, _: Context) Result {
const b = try self.buf_for_update();
var all_stop = true;
var root = b.root;
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
if (cursel.selection) |_| {
root = self.delete_selection(root, cursel, b.allocator) catch continue;
all_stop = false;
continue;
}
with_selection_const(root, move_cursor_left, cursel, self.metrics) catch continue;
if (cursel.selection) |*sel| {
const egc_left, _, _ = sel.end.egc_at(root, self.metrics) catch {
root = self.delete_selection(root, cursel, b.allocator) catch continue;
all_stop = false;
continue;
};
const egc_right, _, _ = sel.begin.egc_at(root, self.metrics) catch {
root = self.delete_selection(root, cursel, b.allocator) catch continue;
all_stop = false;
continue;
};
for (Buffer.unicode.char_pairs) |pair| if (std.mem.eql(u8, egc_left, pair[0]) and std.mem.eql(u8, egc_right, pair[1])) {
sel.begin.move_right(root, self.metrics) catch {};
break;
};
}
root = self.delete_selection(root, cursel, b.allocator) catch continue;
all_stop = false;
};
if (all_stop)
return error.Stop;
try self.update_buf(root);
self.clamp();
}
pub const smart_delete_backward_meta = .{ .description = "Delete previous character (smart)" };
pub fn delete_word_left(self: *Self, _: Context) Result { pub fn delete_word_left(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const root = try self.delete_to(move_cursor_word_left_space, b.root, b.allocator); const root = try self.delete_to(move_cursor_word_left_space, b.root, b.allocator);