From c40b95b8f938e01b7e8846bae53f16b0c1cd5654 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 03:35:02 -0300 Subject: 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/File.zig | 13 ++++++++++- src/indent.zig | 2 +- src/normal.zig | 6 +++-- src/syntax.zig | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/File.zig b/src/File.zig index 90711c9a..05e5271c 100644 --- a/src/File.zig +++ b/src/File.zig @@ -64,7 +64,7 @@ pub const State = struct { /// What the replay digest leaves out (src/walk.zig): what is made again /// from the text whenever it is wanted. pub const replica = .{ - .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" }, + .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "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 @@ -143,6 +143,10 @@ pub const State = struct { /// one: `syntax.parseTree`'s, opaque here. node_tree: ?*anyopaque = null, node_tree_revision: u32 = 0, + /// The revision the tree's edits reach (`syntax.editTree`, told by + /// `edit` below): at `revision`, the next parse reuses it; else, a + /// revision it was not told of (a twin's edit, a reload), a parse anew. + node_tree_edits: u32 = 0, /// helix's object_selections: what Alt-o grew from, for Alt-i to go back /// to; gpa-owned ranges and the primary's index. node_history: std.ArrayList(NodeStep) = .empty, @@ -686,6 +690,13 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who f.saved_hash = std.hash.Wyhash.hash(0, f.content); f.saved_len = f.content.len; } + // The kept tree follows the edit (normal.fileTree parses with it), but + // not while a rollback copy holds it: an edit in place would change the + // copy's tree under it, so that one is left as it is and parsed anew. + if (f.node_tree) |tree| if (syntax.held_trees == null and f.node_tree_edits == f.revision) { + syntax.editTree(tree, f.content[0..off], f.content[off..][0..len], bytes); + f.node_tree_edits = f.revision +% 1; + }; var undo_gone = false; if (fresh) |new| { const old = textRoom(f); diff --git a/src/indent.zig b/src/indent.zig index fc2df450..bf52800c 100644 --- a/src/indent.zig +++ b/src/indent.zig @@ -335,7 +335,7 @@ fn expectIndent(path: []const u8, marked: []const u8, want: []const u8) !void { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const text = try std.mem.concat(arena.allocator(), u8, &.{ marked[0..cut], marked[cut + 1 ..] }); - const tree = syntax.parseTree(path, text) orelse return error.NoTree; + const tree = syntax.parseTree(path, text, null) orelse return error.NoTree; defer syntax.destroyTree(tree); const got = try forNewline(arena.allocator(), l, tree, text, std.mem.count(u8, text[0..cut], "\n"), cut); try std.testing.expectEqualStrings(want, got orelse return error.NoIndent); diff --git a/src/normal.zig b/src/normal.zig index fc6da633..e95a5798 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -207,9 +207,11 @@ pub fn appendSearchLiteral(out: *std.ArrayList(u8), arena: std.mem.Allocator, te /// kept until the next: never made again for a key that changed nothing. pub fn fileTree(p: *Pardes, f: *panes.File.State) ?*anyopaque { if (f.node_tree == null or f.node_tree_revision != f.revision) { - if (f.node_tree) |old| syntax.freeTree(old); - f.node_tree = syntax.parseTree(f.path, f.content); + const old = f.node_tree; + f.node_tree = syntax.parseTree(f.path, f.content, if (f.node_tree_edits == f.revision) old else null); + if (old) |o| syntax.freeTree(o); f.node_tree_revision = f.revision; + f.node_tree_edits = f.revision; for (f.node_history.items) |step| p.gpa.free(step.ranges); f.node_history.clearRetainingCapacity(); } 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()); +} -- cgit v1.3