summaryrefslogtreecommitdiff
path: root/src/syntax.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 03:35:02 -0300
committerGabriel Schneider <[email protected]>2026-11-10 04:08:16 -0300
commitc40b95b8f938e01b7e8846bae53f16b0c1cd5654 (patch)
tree2d9c0ad034bcbb1ed1b1a27a94c79715bee40331 /src/syntax.zig
parentc0ed5f823a37fb1b9b2300f53b23263bfc97a108 (diff)
downloadpardes-c40b95b8f938e01b7e8846bae53f16b0c1cd5654.tar.gz
pardes-c40b95b8f938e01b7e8846bae53f16b0c1cd5654.zip
syntax: a file's kept tree is told each edit (File.edit, ts_tree_edit) and the next parse starts from it, so a new line's indent in a 9k-line Zig file costs the 0.2 ms of what the edit touched, not the 47 ms of a parse anew (3.5 ms → 0.5 ms for 400 lines); not while a rollback copy holds the tree, which an edit in place would change under the copy: that one is left as it was and parsed anew, as is one whose revision moved without it (a twin's edit), by node_tree_edits, the revision its edits reach
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/syntax.zig')
-rw-r--r--src/syntax.zig73
1 files changed, 70 insertions, 3 deletions
diff --git a/src/syntax.zig b/src/syntax.zig
index 0521966d..2526e113 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -381,15 +381,42 @@ pub fn supportsPath(path: []const u8) bool {
}
/// A whole file's parse, kept by the file for the keys that walk its nodes
-/// (helix's Alt-o and kin). Opaque outside this file; null without a grammar.
-pub fn parseTree(path: []const u8, content: []const u8) ?*anyopaque {
+/// (helix's Alt-o and kin) and for a new line's indent. Opaque outside this
+/// file; null without a grammar. With `old`, the kept tree told every edit
+/// since its parse (`editTree`), the parse reuses what those left alone: a
+/// character typed into a 9k-line file costs a few hundred µs, not 50 ms.
+pub fn parseTree(path: []const u8, content: []const u8, old: ?*anyopaque) ?*anyopaque {
if (!enabled) return null;
const selected = (forExt(std.Io.Dir.path.extension(path)) catch return null) orelse return null;
- const tree = selected.parser.parseString(content, null) orelse return null;
+ const tree = selected.parser.parseString(content, @ptrCast(@alignCast(old))) orelse return null;
if (held_trees) |h| h.born.append(h.meta, @ptrCast(tree)) catch {};
return @ptrCast(tree);
}
+/// The kept tree told of an edit (ts_tree_edit): `old` the bytes taken out
+/// after `before`, the text up to them, `ins` the bytes put in. Its nodes'
+/// places follow the edit; what it says of the edited bytes is stale until
+/// `parseTree` with it.
+pub fn editTree(tree: *anyopaque, before: []const u8, old: []const u8, ins: []const u8) void {
+ if (!enabled) return;
+ const from = advance(.{ .row = 0, .column = 0 }, before);
+ const t: *ts.Tree = @ptrCast(@alignCast(tree));
+ t.edit(.{
+ .start_byte = @intCast(before.len),
+ .old_end_byte = @intCast(before.len + old.len),
+ .new_end_byte = @intCast(before.len + ins.len),
+ .start_point = from,
+ .old_end_point = advance(from, old),
+ .new_end_point = advance(from, ins),
+ });
+}
+
+/// The point `text` ends at, read from `from` (a column is bytes, as tree-sitter's).
+fn advance(from: ts.Point, text: []const u8) ts.Point {
+ const nl = std.mem.lastIndexOfScalar(u8, text, '\n') orelse return .{ .row = from.row, .column = from.column + @as(u32, @intCast(text.len)) };
+ return .{ .row = from.row + @as(u32, @intCast(std.mem.count(u8, text, "\n"))), .column = @intCast(text.len - nl - 1) };
+}
+
/// While a replica's rollback copy is held (src/rollback.zig) a tree parsed
/// is the speculation's, and one freed is put off: trees live on the C
/// heap, which the core's allocator cannot see. Null otherwise.
@@ -2138,3 +2165,43 @@ test "tables are the pipe_tables tree-sitter finds" {
return e;
};
}
+
+test "a kept tree told its edits parses to the fresh parse's tree, places and all" {
+ if (!enabled) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const a = "const std = @import(\"std\");\n\nfn f(x: u32) u32 {\n return x;\n}\n\ntest \"t\" {\n _ = f(1);\n}\n";
+ const old_tree = parseTree("a.zig", a, null) orelse return error.NoTree;
+ // Two edits in a row, as typing makes them: a line opened in f, then a
+ // call typed on it (one of them spans rows, one does not).
+ const Edit = struct { off: usize, len: usize, ins: []const u8 };
+ const edits = [_]Edit{ .{ .off = 47, .len = 0, .ins = "\n " }, .{ .off = 52, .len = 0, .ins = "g(x);" } };
+ var text: []u8 = try gpa.dupe(u8, a);
+ defer gpa.free(text);
+ for (edits) |e| {
+ editTree(old_tree, text[0..e.off], text[e.off..][0..e.len], e.ins);
+ const next = try std.mem.concat(gpa, u8, &.{ text[0..e.off], e.ins, text[e.off + e.len ..] });
+ gpa.free(text);
+ text = next;
+ }
+ try std.testing.expect(std.mem.indexOf(u8, text, "u32 {\n g(x);\n return x;") != null);
+ const reused = parseTree("a.zig", text, old_tree) orelse return error.NoTree;
+ defer destroyTree(reused);
+ destroyTree(old_tree);
+ const fresh = parseTree("a.zig", text, null) orelse return error.NoTree;
+ defer destroyTree(fresh);
+ const r: *ts.Tree = @ptrCast(@alignCast(reused));
+ const f: *ts.Tree = @ptrCast(@alignCast(fresh));
+ const rs = try r.rootNode().toSexp(gpa);
+ defer gpa.free(rs);
+ const fs = try f.rootNode().toSexp(gpa);
+ defer gpa.free(fs);
+ try std.testing.expectEqualStrings(fs, rs);
+ try std.testing.expect(!r.rootNode().hasError());
+ // The places too: the test block's, past both edits.
+ const at: u32 = @intCast(std.mem.indexOf(u8, text, "test").?);
+ const rn = r.rootNode().descendantForByteRange(at, at + 4).?;
+ const fn_ = f.rootNode().descendantForByteRange(at, at + 4).?;
+ try std.testing.expectEqual(fn_.startPoint(), rn.startPoint());
+ try std.testing.expectEqual(fn_.endPoint(), rn.endPoint());
+ try std.testing.expectEqual(fn_.startByte(), rn.startByte());
+}