feat: add smart_insert_pair command

closes #90
This commit is contained in:
CJ van den Berg 2025-02-26 22:03:50 +01:00
parent 0bd01afe53
commit 67f1ab1697
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 36 additions and 1 deletions

View file

@ -161,6 +161,16 @@
["tab", "indent"],
["ctrl+space", "enter_mode", "select"],
["\"", "smart_insert_pair", "\"", "\""],
["'", "smart_insert_pair", "'", "'"],
["(", "smart_insert_pair", "(", ")"],
["[", "smart_insert_pair", "[", "]"],
["{", "smart_insert_pair", "{", "}"],
["", "smart_insert_pair", "", ""],
["“", "smart_insert_pair", "“", "”"],
["", "smart_insert_pair", "", ""],
["«", "smart_insert_pair", "«", "»"],
["left_control", "enable_jump_mode"],
["right_control", "enable_jump_mode"],
["left_alt", "enable_fast_scroll"],

View file

@ -615,7 +615,7 @@ pub const Editor = struct {
}
if (self.buffer) |buffer| if (buffer.get_meta()) |meta| {
const frame_ = tracy.initZone(@src(), .{ .name = "extract_state" });
const frame_ = tracy.initZone(@src(), .{ .name = extract_state });
defer frame_.deinit();
try self.extract_state(meta, .none);
};
@ -4059,6 +4059,31 @@ pub const Editor = struct {
}
pub const smart_buffer_append_meta = .{ .arguments = &.{.string} };
pub fn smart_insert_pair(self: *Self, ctx: Context) Result {
var chars_left: []const u8 = undefined;
var chars_right: []const u8 = undefined;
if (!try ctx.args.match(.{ tp.extract(&chars_left), tp.extract(&chars_right) }))
return error.InvalidSmartInsertPairArguments;
const b = try self.buf_for_update();
var root = b.root;
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
if (cursel.selection) |*sel| {
var begin: CurSel = .{ .cursor = sel.begin };
root = try self.insert(root, &begin, chars_left, b.allocator);
var end: CurSel = .{ .cursor = sel.end };
root = try self.insert(root, &end, chars_right, b.allocator);
sel.end.move_left(root, self.metrics) catch {};
} else {
root = try self.insert(root, cursel, chars_left, b.allocator);
root = try self.insert(root, cursel, chars_right, b.allocator);
}
cursel.cursor.move_left(root, self.metrics) catch {};
};
try self.update_buf(root);
self.clamp();
}
pub const smart_insert_pair_meta = .{ .arguments = &.{.string} };
pub fn enable_fast_scroll(self: *Self, _: Context) Result {
self.fast_scroll = true;
}