refactor: adapt editor module to Buffer text caching API

This commit is contained in:
CJ van den Berg 2026-02-02 21:34:59 +01:00
parent b25e099a84
commit 23504b62cd
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -730,15 +730,9 @@ pub const Editor = struct {
self.syntax_no_render = true; self.syntax_no_render = true;
} }
var content: std.Io.Writer.Allocating = .init(std.heap.c_allocator); const content = new_buf.store_to_string_cached(new_buf.root, new_buf.file_eol_mode);
defer content.deinit();
{
const frame_ = tracy.initZone(@src(), .{ .name = "store" });
defer frame_.deinit();
try new_buf.root.store(&content.writer, new_buf.file_eol_mode);
}
if (self.indent_mode == .auto) if (self.indent_mode == .auto)
self.detect_indent_mode(content.written()); self.detect_indent_mode(content);
self.syntax = syntax: { self.syntax = syntax: {
const lang_override = file_type orelse tp.env.get().str("language"); const lang_override = file_type orelse tp.env.get().str("language");
@ -749,7 +743,7 @@ pub const Editor = struct {
break :blk if (lang_override.len > 0) break :blk if (lang_override.len > 0)
try file_type_config.get(lang_override) try file_type_config.get(lang_override)
else else
file_type_config.guess_file_type(self.file_path, content.written()); file_type_config.guess_file_type(self.file_path, content);
}; };
self.checked_formatter = false; self.checked_formatter = false;
self.formatter = null; self.formatter = null;
@ -770,7 +764,7 @@ pub const Editor = struct {
file_path, file_path,
ft, ft,
new_buf.lsp_version, new_buf.lsp_version,
try content.toOwnedSlice(), content,
new_buf.is_ephemeral(), new_buf.is_ephemeral(),
) catch |e| ) catch |e|
self.logger.print("project_manager.did_open failed: {any}", .{e}); self.logger.print("project_manager.did_open failed: {any}", .{e});
@ -2020,7 +2014,7 @@ pub const Editor = struct {
const root = root_ orelse return &.{}; const root = root_ orelse return &.{};
var text: std.Io.Writer.Allocating = .init(std.heap.c_allocator); var text: std.Io.Writer.Allocating = .init(std.heap.c_allocator);
defer text.deinit(); defer text.deinit();
try root.store(&text.writer, eol_mode); try root.store_node(&text.writer, eol_mode);
return text.toOwnedSlice(); return text.toOwnedSlice();
} }
@ -5303,7 +5297,8 @@ pub const Editor = struct {
pub const smart_insert_pair_close_meta: Meta = .{ .arguments = &.{ .string, .string } }; pub const smart_insert_pair_close_meta: Meta = .{ .arguments = &.{ .string, .string } };
fn update_syntax(self: *Self) !void { fn update_syntax(self: *Self) !void {
const root = try self.buf_root(); const buffer = self.buffer orelse return error.Stop;
const root = buffer.root;
const eol_mode = try self.buf_eol_mode(); const eol_mode = try self.buf_eol_mode();
if (!self.syntax_refresh_full and self.syntax_last_rendered_root == root) if (!self.syntax_refresh_full and self.syntax_last_rendered_root == root)
return; return;
@ -5323,15 +5318,7 @@ pub const Editor = struct {
self.syntax_refresh_full = true; self.syntax_refresh_full = true;
if (self.syntax_last_rendered_root == null) if (self.syntax_last_rendered_root == null)
self.syntax_refresh_full = true; self.syntax_refresh_full = true;
var content_: std.Io.Writer.Allocating = .init(self.allocator); const content = buffer.store_to_string_cached(root, eol_mode);
defer content_.deinit();
{
const frame = tracy.initZone(@src(), .{ .name = "editor store syntax" });
defer frame.deinit();
try root.store(&content_.writer, eol_mode);
}
const content = try content_.toOwnedSliceSentinel(0);
defer self.allocator.free(content);
if (self.syntax_refresh_full) { if (self.syntax_refresh_full) {
{ {
const frame = tracy.initZone(@src(), .{ .name = "editor reset syntax" }); const frame = tracy.initZone(@src(), .{ .name = "editor reset syntax" });
@ -5354,7 +5341,7 @@ pub const Editor = struct {
{ {
const frame = tracy.initZone(@src(), .{ .name = "editor store syntax" }); const frame = tracy.initZone(@src(), .{ .name = "editor store syntax" });
defer frame.deinit(); defer frame.deinit();
try root_src.store(&old_content.writer, eol_mode); try root_src.store_node(&old_content.writer, eol_mode);
} }
{ {
const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" }); const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" });
@ -5381,10 +5368,8 @@ pub const Editor = struct {
} }
} }
} else { } else {
var content: std.Io.Writer.Allocating = .init(self.allocator); const content = buffer.store_to_string_cached(root, eol_mode);
defer content.deinit(); self.syntax = file_type_config.create_syntax_guess_file_type(self.allocator, content, self.file_path, tui.query_cache()) catch |e| switch (e) {
try root.store(&content.writer, eol_mode);
self.syntax = file_type_config.create_syntax_guess_file_type(self.allocator, content.written(), self.file_path, tui.query_cache()) catch |e| switch (e) {
error.NotFound => null, error.NotFound => null,
else => return e, else => return e,
}; };
@ -5392,7 +5377,7 @@ pub const Editor = struct {
if (self.syntax) |syn| { if (self.syntax) |syn| {
const frame = tracy.initZone(@src(), .{ .name = "editor parse syntax" }); const frame = tracy.initZone(@src(), .{ .name = "editor parse syntax" });
defer frame.deinit(); defer frame.deinit();
try syn.refresh_full(content.written()); try syn.refresh_full(content);
self.syntax_last_rendered_root = root; self.syntax_last_rendered_root = root;
} }
} }
@ -6340,12 +6325,10 @@ pub const Editor = struct {
const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" }); const frame = tracy.initZone(@src(), .{ .name = "editor diff syntax" });
defer frame.deinit(); defer frame.deinit();
var content_: std.Io.Writer.Allocating = .init(self.allocator); const buffer = self.buffer orelse return;
defer content_.deinit(); const root = buffer.root;
const root = self.buf_root() catch return;
const eol_mode = self.buf_eol_mode() catch return; const eol_mode = self.buf_eol_mode() catch return;
try root.store(&content_.writer, eol_mode); const content = buffer.store_to_string_cached(root, eol_mode);
const content = content_.written();
var last_begin_row: usize = 0; var last_begin_row: usize = 0;
var last_begin_col_pos: usize = 0; var last_begin_col_pos: usize = 0;
var last_end_row: usize = 0; var last_end_row: usize = 0;
@ -6785,7 +6768,8 @@ pub const Editor = struct {
fn filter_done(self: *Self) !void { fn filter_done(self: *Self) !void {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const root = self.buf_root() catch return; const buffer = self.buffer orelse return;
const root = b.root;
const state = if (self.filter_) |*s| s else return error.Stop; const state = if (self.filter_) |*s| s else return error.Stop;
if (state.before_root != root) return error.Stop; if (state.before_root != root) return error.Stop;
defer self.filter_deinit(); defer self.filter_deinit();
@ -6796,10 +6780,8 @@ pub const Editor = struct {
return; return;
} }
const old_hash = blk: { const old_hash = blk: {
var content: std.Io.Writer.Allocating = .init(self.allocator); const content = buffer.store_to_string_cached(root, try self.buf_eol_mode());
defer content.deinit(); break :blk std.hash.XxHash3.hash(0, content);
try root.store(&content.writer, try self.buf_eol_mode());
break :blk std.hash.XxHash3.hash(0, content.written());
}; };
const new_hash = std.hash.XxHash3.hash(0, buf.items); const new_hash = std.hash.XxHash3.hash(0, buf.items);
if (old_hash == new_hash) { if (old_hash == new_hash) {
@ -7029,17 +7011,15 @@ pub const Editor = struct {
}; };
if (self.file_type) |ft| { if (self.file_type) |ft| {
var content: std.Io.Writer.Allocating = .init(self.allocator); const buffer = self.buffer orelse return error.Stop;
defer content.deinit(); const content = buffer.store_to_string_cached(buffer.root, try self.buf_eol_mode());
const root = try self.buf_root();
try root.store(&content.writer, try self.buf_eol_mode());
if (self.buffer) |buffer| if (self.file_path) |file_path| if (self.file_path) |file_path|
project_manager.did_open( project_manager.did_open(
file_path, file_path,
ft, ft,
buffer.lsp_version, buffer.lsp_version,
try content.toOwnedSlice(), content,
if (self.buffer) |p| p.is_ephemeral() else true, if (self.buffer) |p| p.is_ephemeral() else true,
) catch |e| ) catch |e|
self.logger.print("project_manager.did_open failed: {any}", .{e}); self.logger.print("project_manager.did_open failed: {any}", .{e});