Merge branch 'master' into zig-0.14

This commit is contained in:
CJ van den Berg 2025-03-05 21:28:38 +01:00
commit 4cdd439b4a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -4003,20 +4003,39 @@ pub const Editor = struct {
} }
pub const insert_line_meta: Meta = .{ .description = "Insert line" }; pub const insert_line_meta: Meta = .{ .description = "Insert line" };
fn cursel_smart_insert_line(self: *Self, root: Buffer.Root, cursel: *CurSel, b_allocator: std.mem.Allocator) !Buffer.Root {
var leading_ws = @min(find_first_non_ws(root, cursel.cursor.row, self.metrics), cursel.cursor.col);
var sfa = std.heap.stackFallback(512, self.allocator);
const allocator = sfa.get();
var stream = std.ArrayList(u8).init(allocator);
defer stream.deinit();
var writer = stream.writer();
_ = try writer.write("\n");
while (leading_ws > 0) : (leading_ws -= 1)
_ = try writer.write(" ");
return self.insert(root, cursel, stream.items, b_allocator);
}
pub fn smart_insert_line(self: *Self, _: Context) Result { pub fn smart_insert_line(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
var root = b.root; var root = b.root;
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
var leading_ws = @min(find_first_non_ws(root, cursel.cursor.row, self.metrics), cursel.cursor.col); const smart_brace_indent = blk: {
var sfa = std.heap.stackFallback(512, self.allocator); var sel = Selection.from_cursor(&cursel.cursor);
const allocator = sfa.get(); move_cursor_left(root, &sel.begin, self.metrics) catch break :blk false;
var stream = std.ArrayList(u8).init(allocator); const egc_left, _, _ = sel.end.egc_at(root, self.metrics) catch break :blk false;
defer stream.deinit(); const egc_right, _, _ = sel.begin.egc_at(root, self.metrics) catch break :blk false;
var writer = stream.writer(); break :blk std.mem.eql(u8, egc_right, "{") and std.mem.eql(u8, egc_left, "}");
_ = try writer.write("\n"); };
while (leading_ws > 0) : (leading_ws -= 1)
_ = try writer.write(" "); root = try self.cursel_smart_insert_line(root, cursel, b.allocator);
root = try self.insert(root, cursel, stream.items, b.allocator);
if (smart_brace_indent) {
const cursor = cursel.cursor;
root = try self.cursel_smart_insert_line(root, cursel, b.allocator);
cursel.cursor = cursor;
root = try self.indent_cursel(root, cursel, b.allocator);
}
}; };
try self.update_buf(root); try self.update_buf(root);
self.clamp(); self.clamp();