diff options
Diffstat (limited to 'src/File.zig')
| -rw-r--r-- | src/File.zig | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/src/File.zig b/src/File.zig index dc7b4cf8..d1cfe088 100644 --- a/src/File.zig +++ b/src/File.zig @@ -59,12 +59,31 @@ pub const History = struct { } }; +/// Owned syntax results. Excluded from dumps as State.context is derived. +pub const ContextCache = struct { + allocator: ?std.mem.Allocator = null, + declarations: []syntax.ContextDeclaration = &.{}, + revision: ?u32 = null, + + pub fn deinit(self: *ContextCache) void { + if (self.allocator) |gpa| gpa.free(self.declarations); + self.* = .{}; + } + + /// Takes ownership of declarations allocated by gpa. + pub fn replace(self: *ContextCache, gpa: std.mem.Allocator, declarations: []syntax.ContextDeclaration, revision: u32) void { + self.deinit(); + self.* = .{ .allocator = gpa, .declarations = declarations, .revision = revision }; + } +}; + /// A file pane's backing: owned content, its derived caches, and undo history. pub const State = struct { /// What the replay digest leaves out (src/walk.zig): what is made again /// from the text whenever it is wanted. + // Syntax caches must not load through the dump loader's core allocator. pub const replica = .{ - .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_tree_edits", "node_history", "tables" }, + .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "context", "node_tree", "node_tree_revision", "node_tree_edits", "node_history", "tables" }, // Big texts, dumped and sent by their hash (src/walk.zig). The // text is written in place by `replace`, but never while a join // sends it (memory.zig `pin`): then an edit puts a new buffer in @@ -136,8 +155,7 @@ pub const State = struct { highlight_start: usize = 0, syntax_dirty: bool = true, tree_context: bool = false, - context_declarations: []syntax.ContextDeclaration = &.{}, - context_revision: ?u32 = null, + context: ContextCache = .{}, /// The whole file's parse for the keys that walk its nodes (Alt-o and /// kin), made on the first of them after an edit and kept until the next /// one: `syntax.parseTree`'s, opaque here. @@ -576,7 +594,7 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); p.gpa.free(file.pager_spans); locations.freeRows(p.gpa, file.location_rows); - if (file.context_declarations.len > 0) p.tree_sitter_gpa.free(file.context_declarations); + file.context.deinit(); if (file.node_tree) |tree| syntax.freeTree(tree); for (file.node_history.items) |step| p.gpa.free(step.ranges); file.node_history.deinit(p.gpa); @@ -1463,11 +1481,9 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { /// its text as it is now: found in the step (Pardes.sync), never only by a /// render, since the view's geometry is made of them (`geometry`). pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void { - if (!f.tree_context or !supportsContext(pane) or f.context_revision == f.revision) return; + if (!f.tree_context or !supportsContext(pane) or f.context.revision == f.revision) return; const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch return; - if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); - f.context_declarations = declarations; - f.context_revision = f.revision; + f.context.replace(p.tree_sitter_gpa, declarations, f.revision); } pub fn refreshHighlights(p: *Pardes) void { @@ -1585,7 +1601,7 @@ fn prepareContextRows(pane: *Pane, file: *State) void { pane.context_rows = 0; if (!file.tree_context or !supportsContext(pane) or pane.rows > pane.wrap_line.len) return; const capacity = @min(pane.rows -| 1, pane.context_row_limit orelse std.math.maxInt(u16)); // retain an ordinary body row - for (file.context_declarations) |declaration| { + for (file.context.declarations) |declaration| { if (declaration.start_line >= file.scroll) break; if (declaration.end_line < file.scroll) continue; var row = declaration.start_line; @@ -1676,8 +1692,8 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { f.revision, f.content.len, f.scroll, pane.hscroll, pane.rows, pane.cols, pane.tag_rows, pane.collapsed, pane.query != null, f.pager, f.mini != null, f.output != null, - if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0, - f.context_declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len, + if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context.revision orelse 0, + f.context.declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize), pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, pane.view.rect_h, }; @@ -2640,3 +2656,18 @@ test "undo keeps its steps within a byte bound, the newest always" { try std.testing.expect(std.mem.allEqual(u8, f.content[step / 2 ..][0..step], f.content[step / 2])); } } + +test "context cache frees each allocation with its original allocator" { + var first: std.heap.DebugAllocator(.{}) = .init; + defer std.testing.expect(first.deinit() == .ok) catch @panic("first allocator leaked"); + var second: std.heap.DebugAllocator(.{}) = .init; + defer std.testing.expect(second.deinit() == .ok) catch @panic("second allocator leaked"); + var cache: ContextCache = .{}; + defer cache.deinit(); + cache.replace(first.allocator(), try first.allocator().alloc(syntax.ContextDeclaration, 1), 1); + cache.replace(second.allocator(), try second.allocator().alloc(syntax.ContextDeclaration, 2), 2); + try std.testing.expectEqual(@as(?u32, 2), cache.revision); + cache.deinit(); + try std.testing.expect(cache.allocator == null); + try std.testing.expectEqual(@as(usize, 0), cache.declarations.len); +} |
