feat: add caching of tree-sitter query objects

This commit is contained in:
CJ van den Berg 2025-03-18 14:26:31 +01:00
parent 7b133c04fb
commit 47a6024c80
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 152 additions and 15 deletions

View file

@ -464,7 +464,7 @@ pub const Editor = struct {
if (self.buffer) |_| self.write_state(meta.writer()) catch {};
for (self.diagnostics.items) |*d| d.deinit(self.diagnostics.allocator);
self.diagnostics.deinit();
if (self.syntax) |syn| syn.destroy();
if (self.syntax) |syn| syn.destroy(tui.query_cache());
self.cursels.deinit();
self.matches.deinit();
self.handlers.deinit();
@ -588,7 +588,7 @@ pub const Editor = struct {
const frame_ = tracy.initZone(@src(), .{ .name = "create" });
defer frame_.deinit();
break :blk if (syn_file_type) |ft|
syntax.create(ft, self.allocator) catch null
syntax.create(ft, self.allocator, tui.query_cache()) catch null
else
null;
};
@ -4301,7 +4301,7 @@ pub const Editor = struct {
var content = std.ArrayList(u8).init(self.allocator);
defer content.deinit();
try root.store(content.writer(), eol_mode);
self.syntax = syntax.create_guess_file_type(self.allocator, content.items, self.file_path) catch |e| switch (e) {
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,
};
@ -5447,7 +5447,7 @@ pub const Editor = struct {
if (!try ctx.args.match(.{tp.extract(&file_type)}))
return error.InvalidSetFileTypeArgument;
if (self.syntax) |syn| syn.destroy();
if (self.syntax) |syn| syn.destroy(tui.query_cache());
self.syntax_last_rendered_root = null;
self.syntax_refresh_full = true;
self.syntax_incremental_reparse = false;
@ -5457,7 +5457,7 @@ pub const Editor = struct {
defer content.deinit();
const root = try self.buf_root();
try root.store(content.writer(), try self.buf_eol_mode());
const syn = syntax.create_file_type(self.allocator, file_type) catch null;
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,

View file

@ -12,6 +12,7 @@ pub const renderer = @import("renderer");
const command = @import("command");
const EventHandler = @import("EventHandler");
const keybind = @import("keybind");
const syntax = @import("syntax");
const Widget = @import("Widget.zig");
const MessageFilter = @import("MessageFilter.zig");
@ -56,6 +57,7 @@ default_cursor: keybind.CursorShape = .default,
fontface_: []const u8 = "",
fontfaces_: std.ArrayListUnmanaged([]const u8) = .{},
enable_mouse_idle_timer: bool = false,
query_cache_: *syntax.QueryCache,
const keepalive = std.time.us_per_day * 365; // one year
const idle_frames = 0;
@ -120,6 +122,7 @@ fn init(allocator: Allocator) !*Self {
)),
.theme_ = theme_,
.no_sleep = tp.env.get().is("no-sleep"),
.query_cache_ = try syntax.QueryCache.create(allocator, .{}),
};
instance_ = self;
defer instance_ = null;
@ -211,6 +214,7 @@ fn deinit(self: *Self) void {
self.rdr_.stop();
self.rdr_.deinit();
self.logger.deinit();
self.query_cache_.deinit();
self.allocator.destroy(self);
}
@ -1052,6 +1056,10 @@ pub fn mini_mode() ?*MiniMode {
return if (current().mini_mode_) |*p| p else null;
}
pub fn query_cache() *syntax.QueryCache {
return current().query_cache_;
}
pub fn config() *const @import("config") {
return &current().config_;
}