diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index 76ca303..c90bf76 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -14,7 +14,7 @@ pub const Manager = @import("Manager.zig"); pub const Cursor = @import("Cursor.zig"); pub const View = @import("View.zig"); pub const Selection = @import("Selection.zig"); -pub const MetaWriter = std.ArrayList(u8).Writer; +pub const MetaWriter = std.ArrayListUnmanaged(u8).Writer; pub const Metrics = struct { ctx: *const anyopaque, @@ -1030,7 +1030,9 @@ const Node = union(enum) { return error.NotFound; } - pub fn debug_render_chunks(self: *const Node, line: usize, output: *ArrayList(u8), metrics_: Metrics) !void { + pub fn debug_render_chunks(self: *const Node, allocator: std.mem.Allocator, line: usize, metrics_: Metrics) ![]const u8 { + var output = std.ArrayList(u8).init(allocator); + defer output.deinit(); const ctx_ = struct { l: *ArrayList(u8), wcwidth: usize = 0, @@ -1041,17 +1043,23 @@ const Node = union(enum) { return if (!leaf.eol) Walker.keep_walking else Walker.stop; } }; - var ctx: ctx_ = .{ .l = output }; + var ctx: ctx_ = .{ .l = &output }; const found = self.walk_from_line_begin_const(line, ctx_.walker, &ctx, metrics_) catch true; if (!found) return error.NotFound; var buf: [16]u8 = undefined; const wcwidth = try std.fmt.bufPrint(&buf, "{d}", .{ctx.wcwidth}); try output.appendSlice(wcwidth); + return output.toOwnedSlice(); } - pub fn debug_line_render_tree(self: *const Node, line: usize, l: *ArrayList(u8)) !void { - return if (self.find_line_node(line)) |n| n.debug_render_tree(l, 0) else error.NotFound; + pub fn debug_line_render_tree(self: *const Node, allocator: std.mem.Allocator, line: usize) ![]const u8 { + return if (self.find_line_node(line)) |n| blk: { + var l = std.ArrayList(u8).init(allocator); + defer l.deinit(); + n.debug_render_tree(&l, 0); + break :blk l.toOwnedSlice(); + } else error.NotFound; } }; diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 64a5f7d..ace049b 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -59,7 +59,7 @@ pub const Match = struct { has_selection: bool = false, style: ?Widget.Theme.Style = null, - const List = std.ArrayList(?Self); + const List = std.ArrayListUnmanaged(?Self); const Self = @This(); pub fn from_selection(sel: Selection) Self { @@ -86,7 +86,7 @@ pub const CurSel = struct { cursor: Cursor = Cursor{}, selection: ?Selection = null, - const List = std.ArrayList(?Self); + const List = std.ArrayListUnmanaged(?Self); const Self = @This(); pub inline fn invalid() Self { @@ -285,7 +285,7 @@ pub const Editor = struct { pos: CurSel, old_primary: CurSel, old_primary_reversed: bool, - whole_file: ?std.ArrayList(u8), + whole_file: ?std.ArrayListUnmanaged(u8), bytes: usize = 0, chunks: usize = 0, eol_mode: Buffer.EolMode = .lf, @@ -295,7 +295,7 @@ pub const Editor = struct { match_token: usize = 0, match_done_token: usize = 0, last_find_query: ?[]const u8 = null, - find_history: ?std.ArrayList([]const u8) = null, + find_history: ?std.ArrayListUnmanaged([]const u8) = null, find_operation: ?enum { goto_next_match, goto_prev_match } = null, prefix_buf: [8]u8 = undefined, @@ -339,7 +339,7 @@ pub const Editor = struct { style_cache: ?StyleCache = null, style_cache_theme: []const u8 = "", - diagnostics: std.ArrayList(Diagnostic), + diagnostics: std.ArrayListUnmanaged(Diagnostic), diag_errors: usize = 0, diag_warnings: usize = 0, diag_info: usize = 0, @@ -420,7 +420,7 @@ pub const Editor = struct { while (len > 0) : (len -= 1) { var cursel: CurSel = .{}; if (!(cursel.extract(&iter) catch false)) break; - (try self.cursels.addOne()).* = cursel; + (try self.cursels.addOne(self.allocator)).* = cursel; } len = cbor.decodeArrayHeader(&find_history) catch return error.RestoreFindHistory; @@ -452,24 +452,24 @@ pub const Editor = struct { .animation_lag = get_animation_max_lag(), .animation_frame_rate = frame_rate, .animation_last_time = time.microTimestamp(), - .cursels = CurSel.List.init(allocator), - .cursels_saved = CurSel.List.init(allocator), - .matches = Match.List.init(allocator), + .cursels = .empty, + .cursels_saved = .empty, + .matches = .empty, .enable_terminal_cursor = tui.config().enable_terminal_cursor, .render_whitespace = from_whitespace_mode(tui.config().whitespace_mode), - .diagnostics = std.ArrayList(Diagnostic).init(allocator), + .diagnostics = .empty, }; } fn deinit(self: *Self) void { - var meta = std.ArrayList(u8).init(self.allocator); - defer meta.deinit(); - if (self.buffer) |_| self.write_state(meta.writer()) catch {}; - for (self.diagnostics.items) |*d| d.deinit(self.diagnostics.allocator); - self.diagnostics.deinit(); + var meta = std.ArrayListUnmanaged(u8).empty; + defer meta.deinit(self.allocator); + if (self.buffer) |_| self.write_state(meta.writer(self.allocator)) catch {}; + for (self.diagnostics.items) |*d| d.deinit(self.allocator); + self.diagnostics.deinit(self.allocator); if (self.syntax) |syn| syn.destroy(tui.query_cache()); - self.cursels.deinit(); - self.matches.deinit(); + self.cursels.deinit(self.allocator); + self.matches.deinit(self.allocator); self.handlers.deinit(); self.logger.deinit(); if (self.buffer) |p| self.buffer_manager.retire(p, meta.items); @@ -498,8 +498,8 @@ pub const Editor = struct { pub fn buf_for_update(self: *Self) !*const Buffer { if (!self.pause_undo) { - self.cursels_saved.clearAndFree(); - self.cursels_saved = try self.cursels.clone(); + self.cursels_saved.clearAndFree(self.allocator); + self.cursels_saved = try self.cursels.clone(self.allocator); } return self.buffer orelse error.Stop; } @@ -570,12 +570,12 @@ pub const Editor = struct { } self.syntax = syntax: { const lang_override = file_type orelse tp.env.get().str("language"); - var content = std.ArrayList(u8).init(self.allocator); - defer content.deinit(); + var content = std.ArrayListUnmanaged(u8).empty; + defer content.deinit(self.allocator); { const frame_ = tracy.initZone(@src(), .{ .name = "store" }); defer frame_.deinit(); - try new_buf.root.store(content.writer(), new_buf.file_eol_mode); + try new_buf.root.store(content.writer(self.allocator), new_buf.file_eol_mode); } const syn_file_type = blk: { @@ -603,7 +603,7 @@ pub const Editor = struct { file_path, syn_.file_type, self.lsp_version, - try content.toOwnedSlice(), + try content.toOwnedSlice(self.allocator), new_buf.is_ephemeral(), ) catch |e| self.logger.print("project_manager.did_open failed: {any}", .{e}); @@ -631,9 +631,9 @@ pub const Editor = struct { } fn close(self: *Self) !void { - var meta = std.ArrayList(u8).init(self.allocator); - defer meta.deinit(); - self.write_state(meta.writer()) catch {}; + var meta = std.ArrayListUnmanaged(u8).empty; + defer meta.deinit(self.allocator); + self.write_state(meta.writer(self.allocator)) catch {}; if (self.buffer) |b_mut| self.buffer_manager.retire(b_mut, meta.items); self.cancel_all_selections(); self.buffer = null; @@ -668,7 +668,7 @@ pub const Editor = struct { pub fn push_cursor(self: *Self) !void { const primary = self.cursels.getLastOrNull() orelse CurSel{} orelse CurSel{}; - (try self.cursels.addOne()).* = primary; + (try self.cursels.addOne(self.allocator)).* = primary; } pub fn pop_cursor(self: *Self, _: Context) Result { @@ -689,7 +689,7 @@ pub const Editor = struct { return primary; if (idx == 0) { self.logger.print("ERROR: no more cursors", .{}); - (@constCast(self).cursels.addOne() catch |e| switch (e) { + (@constCast(self).cursels.addOne(self.allocator) catch |e| switch (e) { error.OutOfMemory => @panic("get_primary error.OutOfMemory"), }).* = CurSel{}; } @@ -697,19 +697,19 @@ pub const Editor = struct { } fn store_undo_meta(self: *Self, allocator: Allocator) ![]u8 { - var meta = std.ArrayList(u8).init(allocator); - const writer = meta.writer(); + var meta = std.ArrayListUnmanaged(u8).empty; + const writer = meta.writer(allocator); for (self.cursels_saved.items) |*cursel_| if (cursel_.*) |*cursel| try cursel.write(writer); - return meta.toOwnedSlice(); + return meta.toOwnedSlice(allocator); } fn store_current_undo_meta(self: *Self, allocator: Allocator) ![]u8 { - var meta = std.ArrayList(u8).init(allocator); - const writer = meta.writer(); + var meta = std.ArrayListUnmanaged(u8).empty; + const writer = meta.writer(allocator); for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| try cursel.write(writer); - return meta.toOwnedSlice(); + return meta.toOwnedSlice(allocator); } pub fn update_buf(self: *Self, root: Buffer.Root) !void { @@ -739,7 +739,7 @@ pub const Editor = struct { while (iter.len > 0) { var cursel: CurSel = .{}; if (!try cursel.extract(&iter)) return error.SyntaxError; - (try self.cursels.addOne()).* = cursel; + (try self.cursels.addOne(self.allocator)).* = cursel; } } @@ -784,8 +784,8 @@ pub const Editor = struct { pub fn pause_undo_history(self: *Self, _: Context) Result { self.pause_undo = true; self.pause_undo_root = self.buf_root() catch return; - self.cursels_saved.clearAndFree(); - self.cursels_saved = try self.cursels.clone(); + self.cursels_saved.clearAndFree(self.allocator); + self.cursels_saved = try self.cursels.clone(self.allocator); } pub const pause_undo_history_meta: Meta = .{ .description = "Pause undo history" }; @@ -1725,7 +1725,7 @@ pub const Editor = struct { const frame = tracy.initZone(@src(), .{ .name = "collapse cursors" }); defer frame.deinit(); var old = self.cursels; - defer old.deinit(); + defer old.deinit(self.allocator); 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| { for (old.items[i + 1 ..], i + 1..) |*b_, j| if (b_.*) |*b| { @@ -1734,7 +1734,7 @@ pub const Editor = struct { }; }; for (old.items) |*item_| if (item_.*) |*item| { - (self.cursels.addOne() catch return).* = item.*; + (self.cursels.addOne(self.allocator) catch return).* = item.*; }; } @@ -1749,7 +1749,7 @@ pub const Editor = struct { } fn cancel_all_matches(self: *Self) void { - self.matches.clearAndFree(); + self.matches.clearAndFree(self.allocator); } pub fn clear_matches(self: *Self) void { @@ -2545,7 +2545,8 @@ pub const Editor = struct { var all_stop = true; var root = root_; - var text = std.ArrayList(u8).init(self.allocator); + var text = std.ArrayListUnmanaged(u8).empty; + defer text.deinit(self.allocator); var first = true; for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { if (cursel.selection) |_| { @@ -2554,9 +2555,9 @@ pub const Editor = struct { if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(cut_text); + try text.appendSlice(self.allocator, cut_text); continue; } @@ -2566,25 +2567,26 @@ pub const Editor = struct { if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(cut_text); + try text.appendSlice(self.allocator, cut_text); all_stop = false; }; if (all_stop) return error.Stop; - return .{ text.items, root }; + return .{ try text.toOwnedSlice(self.allocator), root }; } pub fn cut_internal_vim(self: *Self, _: Context) Result { const primary = self.get_primary(); const b = self.buf_for_update() catch return; var root = b.root; - var text = std.ArrayList(u8).init(self.allocator); + var text = std.ArrayListUnmanaged(u8).empty; + defer text.deinit(self.allocator); if (self.cursels.items.len == 1) if (primary.selection) |_| {} else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); const sel = primary.enable_selection(root, self.metrics) catch return; try move_cursor_begin(root, &sel.begin, self.metrics); try move_cursor_end(root, &sel.end, self.metrics); @@ -2596,12 +2598,12 @@ pub const Editor = struct { if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(cut_text); + try text.appendSlice(self.allocator, cut_text); }; try self.update_buf(root); - self.set_clipboard_internal(text.items); + self.set_clipboard_internal(try text.toOwnedSlice(self.allocator)); self.clamp(); } pub const cut_internal_vim_meta: Meta = .{ .description = "Cut selection or current line to internal clipboard (vim)" }; @@ -2624,18 +2626,19 @@ pub const Editor = struct { }; }; var first = true; - var text = std.ArrayList(u8).init(self.allocator); + var text = std.ArrayListUnmanaged(u8).empty; + defer text.deinit(self.allocator); for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { const cut_text, root = try self.cut_selection(root, cursel); if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(cut_text); + try text.appendSlice(self.allocator, cut_text); }; try self.update_buf(root); - self.set_clipboard(text.items); + self.set_clipboard(try text.toOwnedSlice(self.allocator)); self.clamp(); } pub const cut_meta: Meta = .{ .description = "Cut selection or current line to clipboard" }; @@ -2644,7 +2647,8 @@ pub const Editor = struct { const primary = self.get_primary(); const root = self.buf_root() catch return; var first = true; - var text = std.ArrayList(u8).init(self.allocator); + var text = std.ArrayListUnmanaged(u8).empty; + defer text.deinit(self.allocator); if (self.cursels.items.len == 1) if (primary.selection) |_| {} else { const sel = primary.enable_selection(root, self.metrics) catch return; @@ -2658,9 +2662,9 @@ pub const Editor = struct { if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(copy_text); + try text.appendSlice(self.allocator, copy_text); } }; if (text.items.len > 0) { @@ -2669,7 +2673,7 @@ pub const Editor = struct { } else { self.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)}); } - self.set_clipboard(text.items); + self.set_clipboard(try text.toOwnedSlice(self.allocator)); } } pub const copy_meta: Meta = .{ .description = "Copy selection to clipboard" }; @@ -2748,16 +2752,17 @@ pub const Editor = struct { pub fn copy_internal_vim(self: *Self, _: Context) Result { const root = self.buf_root() catch return; var first = true; - var text = std.ArrayList(u8).init(self.allocator); + var text = std.ArrayListUnmanaged(u8).empty; + defer text.deinit(self.allocator); for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { if (cursel.selection) |sel| { const copy_text = try copy_selection(root, sel, self.allocator, self.metrics); if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(copy_text); + try text.appendSlice(self.allocator, copy_text); } }; if (text.items.len > 0) { @@ -2766,7 +2771,7 @@ pub const Editor = struct { } else { self.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)}); } - self.set_clipboard_internal(text.items); + self.set_clipboard_internal(try text.toOwnedSlice(self.allocator)); } } pub const copy_internal_vim_meta: Meta = .{ .description = "Copy selection to internal clipboard (vim)" }; @@ -2775,8 +2780,9 @@ pub const Editor = struct { const primary = self.get_primary(); const root = self.buf_root() catch return; var first = true; - var text = std.ArrayList(u8).init(self.allocator); - try text.appendSlice("\n"); + var text = std.ArrayListUnmanaged(u8).empty; + defer text.deinit(self.allocator); + try text.appendSlice(self.allocator, "\n"); if (primary.selection) |_| {} else { const sel = primary.enable_selection(root, self.metrics) catch return; try move_cursor_begin(root, &sel.begin, self.metrics); @@ -2789,9 +2795,9 @@ pub const Editor = struct { if (first) { first = false; } else { - try text.appendSlice("\n"); + try text.appendSlice(self.allocator, "\n"); } - try text.appendSlice(copy_text); + try text.appendSlice(self.allocator, copy_text); } }; if (text.items.len > 0) { @@ -2800,7 +2806,7 @@ pub const Editor = struct { } else { self.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)}); } - self.set_clipboard_internal(text.items); + self.set_clipboard_internal(try text.toOwnedSlice(self.allocator)); } } pub const copy_line_internal_vim_meta: Meta = .{ .description = "Copy line to internal clipboard (vim)" }; @@ -3414,7 +3420,7 @@ pub const Editor = struct { sel.normalize(); var row = sel.begin.row; while (row <= sel.end.row) : (row += 1) { - const new_cursel = try self.cursels.addOne(); + const new_cursel = try self.cursels.addOne(self.allocator); new_cursel.* = CurSel{ .selection = null, .cursor = .{ @@ -3428,8 +3434,8 @@ pub const Editor = struct { pub fn add_cursors_to_line_ends(self: *Self, _: Context) Result { const root = try self.buf_root(); - const cursels = try self.cursels.toOwnedSlice(); - defer self.cursels.allocator.free(cursels); + const cursels = try self.cursels.toOwnedSlice(self.allocator); + defer self.allocator.free(cursels); for (cursels) |*cursel_| if (cursel_.*) |*cursel| try self.add_cursors_to_cursel_line_ends(root, cursel); self.collapse_cursors(); @@ -3638,8 +3644,8 @@ pub const Editor = struct { } fn restore_cursels(self: *Self) void { - self.cursels.clearAndFree(); - self.cursels = self.cursels_saved.clone() catch return; + self.cursels.clearAndFree(self.allocator); + self.cursels = self.cursels_saved.clone(self.allocator) catch return; } pub fn unindent(self: *Self, ctx: Context) Result { @@ -4293,9 +4299,9 @@ pub const Editor = struct { 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(); + var stream = std.ArrayListUnmanaged(u8).empty; + defer stream.deinit(allocator); + var writer = stream.writer(allocator); _ = try writer.write("\n"); while (leading_ws > 0) : (leading_ws -= 1) _ = try writer.write(" "); @@ -4358,9 +4364,9 @@ pub const Editor = struct { try move_cursor_left(root, &cursel.cursor, self.metrics); 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(); + var stream = std.ArrayListUnmanaged(u8).empty; + defer stream.deinit(allocator); + var writer = stream.writer(self.allocator); while (leading_ws > 0) : (leading_ws -= 1) _ = try writer.write(" "); if (stream.items.len > 0) @@ -4391,9 +4397,9 @@ pub const Editor = struct { try move_cursor_end(root, &cursel.cursor, self.metrics); 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(); + var stream = std.ArrayListUnmanaged(u8).empty; + defer stream.deinit(allocator); + var writer = stream.writer(allocator); _ = try writer.write("\n"); while (leading_ws > 0) : (leading_ws -= 1) _ = try writer.write(" "); @@ -4541,14 +4547,14 @@ pub const Editor = struct { self.syntax_refresh_full = true; if (self.syntax_last_rendered_root == null) self.syntax_refresh_full = true; - var content_ = std.ArrayList(u8).init(self.allocator); - defer content_.deinit(); + var content_ = std.ArrayListUnmanaged(u8).empty; + defer content_.deinit(self.allocator); { const frame = tracy.initZone(@src(), .{ .name = "editor store syntax" }); defer frame.deinit(); - try root.store(content_.writer(), eol_mode); + try root.store(content_.writer(self.allocator), eol_mode); } - const content = try content_.toOwnedSliceSentinel(0); + const content = try content_.toOwnedSliceSentinel(self.allocator, 0); defer self.allocator.free(content); if (self.syntax_refresh_full) { { @@ -4567,12 +4573,12 @@ pub const Editor = struct { } else { if (self.syntax_last_rendered_root) |root_src| { self.syntax_last_rendered_root = null; - var old_content = std.ArrayList(u8).init(self.allocator); - defer old_content.deinit(); + var old_content = std.ArrayListUnmanaged(u8).empty; + defer old_content.deinit(self.allocator); { const frame = tracy.initZone(@src(), .{ .name = "editor store syntax" }); defer frame.deinit(); - try root_src.store(old_content.writer(), eol_mode); + try root_src.store(old_content.writer(self.allocator), eol_mode); } { const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" }); @@ -4599,9 +4605,9 @@ pub const Editor = struct { } } } else { - var content = std.ArrayList(u8).init(self.allocator); - defer content.deinit(); - try root.store(content.writer(), eol_mode); + var content = std.ArrayListUnmanaged(u8).empty; + defer content.deinit(self.allocator); + try root.store(content.writer(self.allocator), eol_mode); self.syntax = syntax.create_guess_file_type(self.allocator, content.items, self.file_path, tui.query_cache()) catch |e| switch (e) { error.NotFound => null, else => return e, @@ -4652,22 +4658,20 @@ pub const Editor = struct { pub fn dump_current_line(self: *Self, _: Context) Result { const root = self.buf_root() catch return; const primary = self.get_primary(); - var tree = std.ArrayList(u8).init(self.allocator); - defer tree.deinit(); - root.debug_render_chunks(primary.cursor.row, &tree, self.metrics) catch |e| + const tree = root.debug_render_chunks(self.allocator, primary.cursor.row, self.metrics) catch |e| return self.logger.print("line {d}: {any}", .{ primary.cursor.row, e }); - self.logger.print("line {d}:{s}", .{ primary.cursor.row, std.fmt.fmtSliceEscapeLower(tree.items) }); + defer self.allocator.free(tree); + self.logger.print("line {d}:{s}", .{ primary.cursor.row, std.fmt.fmtSliceEscapeLower(tree) }); } pub const dump_current_line_meta: Meta = .{ .description = "Debug: dump current line" }; pub fn dump_current_line_tree(self: *Self, _: Context) Result { const root = self.buf_root() catch return; const primary = self.get_primary(); - var tree = std.ArrayList(u8).init(self.allocator); - defer tree.deinit(); - root.debug_line_render_tree(primary.cursor.row, &tree) catch |e| + const tree = root.debug_line_render_tree(self.allocator, primary.cursor.row) catch |e| return self.logger.print("line {d} ast: {any}", .{ primary.cursor.row, e }); - self.logger.print("line {d} ast:{s}", .{ primary.cursor.row, std.fmt.fmtSliceEscapeLower(tree.items) }); + defer self.allocator.free(tree); + self.logger.print("line {d} ast:{s}", .{ primary.cursor.row, std.fmt.fmtSliceEscapeLower(tree) }); } pub const dump_current_line_tree_meta: Meta = .{ .description = "Debug: dump current line (tree)" }; @@ -4821,14 +4825,14 @@ pub const Editor = struct { pub fn push_find_history(self: *Self, query: []const u8) void { if (query.len == 0) return; const history = if (self.find_history) |*hist| hist else ret: { - self.find_history = std.ArrayList([]const u8).init(self.allocator); + self.find_history = .empty; break :ret &self.find_history.?; }; for (history.items, 0..) |entry, i| if (std.mem.eql(u8, entry, query)) self.allocator.free(history.orderedRemove(i)); const new = self.allocator.dupe(u8, query) catch return; - (history.addOne() catch return).* = new; + (history.addOne(self.allocator) catch return).* = new; } fn set_last_find_query(self: *Self, query: []const u8) void { @@ -4993,7 +4997,7 @@ pub const Editor = struct { var match: Match = .{ .begin = .{ .row = begin_line, .col = begin_pos }, .end = .{ .row = end_line, .col = end_pos } }; if (match.end.eql(self.get_primary().cursor)) match.has_selection = true; - (self.matches.addOne() catch return).* = match; + (self.matches.addOne(self.allocator) catch return).* = match; } fn find_selection_match(self: *const Self, sel: Selection) ?*Match { @@ -5319,11 +5323,11 @@ pub const Editor = struct { const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" }); defer frame.deinit(); - var content_ = std.ArrayList(u8).init(self.allocator); - defer content_.deinit(); + var content_ = std.ArrayListUnmanaged(u8).empty; + defer content_.deinit(self.allocator); const root = self.buf_root() catch return; const eol_mode = self.buf_eol_mode() catch return; - try root.store(content_.writer(), eol_mode); + try root.store(content_.writer(self.allocator), eol_mode); const content = content_.items; var last_begin_row: usize = 0; var last_begin_col_pos: usize = 0; @@ -5415,7 +5419,7 @@ pub const Editor = struct { }; switch (self.matches.items.len) { 0 => { - (self.matches.addOne() catch return).* = match; + (self.matches.addOne(self.allocator) catch return).* = match; }, 1 => { self.matches.items[0] = match; @@ -5448,10 +5452,10 @@ pub const Editor = struct { }, }; - (try self.diagnostics.addOne()).* = .{ - .source = try self.diagnostics.allocator.dupe(u8, source), - .code = try self.diagnostics.allocator.dupe(u8, code), - .message = try self.diagnostics.allocator.dupe(u8, message), + (try self.diagnostics.addOne(self.allocator)).* = .{ + .source = try self.allocator.dupe(u8, source), + .code = try self.allocator.dupe(u8, code), + .message = try self.allocator.dupe(u8, message), .severity = severity, .sel = sel, }; @@ -5503,11 +5507,12 @@ pub const Editor = struct { return; } if (self.get_formatter()) |fmtr| { - var args = std.ArrayList(u8).init(self.allocator); - const writer = args.writer(); + var args = std.ArrayListUnmanaged(u8).empty; + defer args.deinit(self.allocator); + const writer = args.writer(self.allocator); try cbor.writeArrayHeader(writer, fmtr.len); for (fmtr) |arg| try cbor.writeValue(writer, arg); - try self.filter_cmd(.{ .buf = args.items }); + try self.filter_cmd(.{ .buf = try args.toOwnedSlice(self.allocator) }); return; } return tp.exit("no formatter"); @@ -5540,7 +5545,7 @@ pub const Editor = struct { .pos = .{ .cursor = sel.begin }, .old_primary = primary.*, .old_primary_reversed = reversed, - .whole_file = if (primary.selection) |_| null else std.ArrayList(u8).init(self.allocator), + .whole_file = if (primary.selection) |_| null else .empty, }; errdefer self.filter_deinit(); const state = &self.filter_.?; @@ -5564,7 +5569,7 @@ pub const Editor = struct { errdefer self.filter_deinit(); const buf_a_ = try self.buf_a(); if (state.whole_file) |*buf| { - try buf.appendSlice(bytes); + try buf.appendSlice(self.allocator, bytes); } else { const cursor = &state.pos.cursor; cursor.row, cursor.col, state.work_root = try state.work_root.insert_chars(cursor.row, cursor.col, bytes, buf_a_, self.metrics); @@ -5629,7 +5634,7 @@ pub const Editor = struct { fn filter_deinit(self: *Self) void { const state = if (self.filter_) |*s| s else return; - if (state.whole_file) |*buf| buf.deinit(); + if (state.whole_file) |*buf| buf.deinit(self.allocator); self.filter_ = null; } @@ -5698,11 +5703,12 @@ pub const Editor = struct { saved.cursor = sel.end; break :ret sel; }; - var result = std.ArrayList(u8).init(self.allocator); - defer result.deinit(); + var result = std.ArrayListUnmanaged(u8).empty; + defer result.deinit(self.allocator); const writer: struct { self_: *Self, - result: *std.ArrayList(u8), + result: *std.ArrayListUnmanaged(u8), + allocator: std.mem.Allocator, const Error = @typeInfo(@typeInfo(@TypeOf(Buffer.unicode.CaseData.toUpperStr)).@"fn".return_type.?).error_union.error_set; pub fn write(writer: *@This(), bytes: []const u8) Error!void { @@ -5712,7 +5718,7 @@ pub const Editor = struct { else try cd.toLowerStr(writer.self_.allocator, bytes); defer writer.self_.allocator.free(flipped); - return writer.result.appendSlice(flipped); + return writer.result.appendSlice(writer.allocator, flipped); } fn map_error(e: anyerror, _: ?*std.builtin.StackTrace) Error { return @errorCast(e); @@ -5720,6 +5726,7 @@ pub const Editor = struct { } = .{ .self_ = self, .result = &result, + .allocator = allocator, }; self.write_range(root, sel.*, writer, @TypeOf(writer).map_error, null) catch return error.Stop; root = try self.delete_selection(root, cursel, allocator); @@ -5787,17 +5794,17 @@ pub const Editor = struct { self.syntax_incremental_reparse = false; self.syntax = syntax: { - var content = std.ArrayList(u8).init(self.allocator); - defer content.deinit(); + var content = std.ArrayListUnmanaged(u8).empty; + defer content.deinit(self.allocator); const root = try self.buf_root(); - try root.store(content.writer(), try self.buf_eol_mode()); + try root.store(content.writer(self.allocator), try self.buf_eol_mode()); const syn = syntax.create_file_type(self.allocator, file_type, tui.query_cache()) catch null; if (syn) |syn_| if (self.file_path) |file_path| project_manager.did_open( file_path, syn_.file_type, self.lsp_version, - try content.toOwnedSlice(), + try content.toOwnedSlice(self.allocator), if (self.buffer) |p| p.is_ephemeral() else true, ) catch |e| self.logger.print("project_manager.did_open failed: {any}", .{e}); @@ -6066,7 +6073,7 @@ pub const PosToWidthCache = struct { pub fn init(allocator: Allocator) !Self { return .{ - .cache = try std.ArrayList(usize).initCapacity(allocator, 2048), + .cache = try .initCapacity(allocator, 2048), }; } diff --git a/src/tui/inspector_view.zig b/src/tui/inspector_view.zig index e7590fd..967648e 100644 --- a/src/tui/inspector_view.zig +++ b/src/tui/inspector_view.zig @@ -88,7 +88,7 @@ fn dump_highlight(self: *Self, range: syntax.Range, scope: []const u8, id: u32, if (self.theme) |theme| match.style = .{ .bg = theme.editor_gutter_modified.fg }; switch (self.editor.matches.items.len) { 0 => { - (self.editor.matches.addOne() catch return).* = match; + (self.editor.matches.addOne(self.editor.allocator) catch return).* = match; update_match = .add; }, 1 => { @@ -116,7 +116,7 @@ fn dump_highlight(self: *Self, range: syntax.Range, scope: []const u8, id: u32, var match_parent = ed.Match.from_selection(sel_parent); if (self.theme) |theme| match_parent.style = .{ .bg = theme.editor_gutter_added.fg }; switch (update_match) { - .add => (self.editor.matches.addOne() catch return).* = match_parent, + .add => (self.editor.matches.addOne(self.editor.allocator) catch return).* = match_parent, .set => self.editor.matches.items[1] = match_parent, .no => {}, } diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index e0c531e..ed7d1d4 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -250,9 +250,9 @@ fn open_style_config(self: *Self, Style: type) command.Result { break :blk .{ style, style_bufs }; } else .{ Style{}, &.{} }; defer root.free_config(self.allocator, style_bufs); - var conf = std.ArrayList(u8).init(self.allocator); - defer conf.deinit(); - root.write_config_to_writer(Style, style, conf.writer()) catch {}; + var conf = std.ArrayListUnmanaged(u8).empty; + defer conf.deinit(self.allocator); + root.write_config_to_writer(Style, style, conf.writer(self.allocator)) catch {}; tui.reset_drag_context(); try self.create_editor(); try command.executeName("open_scratch_buffer", command.fmt(.{ @@ -1209,8 +1209,8 @@ pub fn write_restore_info(self: *Self) void { const editor = self.get_active_editor() orelse return; var sfa = std.heap.stackFallback(512, self.allocator); const a = sfa.get(); - var meta = std.ArrayList(u8).init(a); - editor.write_state(meta.writer()) catch return; + var meta = std.ArrayListUnmanaged(u8).empty; + editor.write_state(meta.writer(a)) catch return; const file_name = root.get_restore_file_name() catch return; var file = std.fs.createFileAbsolute(file_name, .{ .truncate = true }) catch return; defer file.close();