summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-12 10:56:42 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitf2b02c455f2bcdfb905a015eefbb630a28b4b12e (patch)
treec344c7ad42ec288dd5e1ebd0eb6d25f5aa2fbccc /src/File.zig
parentfc38c73c59f7855a4caad8aa4d3bb8e023309c13 (diff)
downloadpardes-f2b02c455f2bcdfb905a015eefbb630a28b4b12e.tar.gz
pardes-f2b02c455f2bcdfb905a015eefbb630a28b4b12e.zip
addons: an addon can hand the frontend a GPU scene (ABI 3.1), and a snake drawn by shaders does; a commit's patch keeps its own +Diff pane; a real ZLS checked through the window build
ABI 3.1 appends an optional `gpu` callback and its context to pa_paint: one self-contained sketchbook SGPU v1 scene for the pane, at most 4 MiB, copied during the call, the last successful call winning. The SDL frontend runs it on its own GPU through sketchbook's executor and composites its target texture into the pane; replicas carry the scene beside the authoritative pane frame; cells and rasters stay the fallback for terminals, older hosts and frontends without an executor. addons/snake_shader is such an addon (`zig build snake-shader`, the SnakeShader word: a board that follows its pane, buffered turns, a CRT look), with test/gpu_snake.py, test/gpu_snake_interaction.py and test/gpu_upgrade.py. Commit patches open as `+Diff-<short-hash>` and reuse only their own commit's pane, so another paged patch no longer replaces one already open. The language-server client looks in the standard per-user bin directory, which desktop launchers often leave out of PATH, and takes an empty answer that beats the startup progress notification onto the wire; test/real_zls.py checks imports, hover, references, rename previews, diagnostics and completion against a real ZLS from a private home. The dump's schema reader skips two fields older builds persisted, and a replica's shadow of a kind ticks when the authority's does. The reference, the guide, the building and setup notes and the changelog say so. The working copy as it stood on 2026-11-12; message written by Claude from the diff, at the author's request.
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig53
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);
+}