Expand description
TS.1 host backing for the tree-snapshot / node WIT resources
(plugin-treesitter-seam.md §3). The host owns the parse tree; a plugin gets
read-only handles and calls back for the structure it needs — the tree never
crosses the boundary (the document-handle model, applied to structure).
Snapshot backing. A tree-snapshot wraps an Arc<SyntaxSnapshot> (an
O(1) ArcSwap bump; lattice-syntax already runs reparses off-thread). It
is immutable, so a handle stays coherent with its own point-in-time tree even
as edits land underneath (§7). The trampoline mints it only when the snapshot
actually has a tree() — so a handed-over resource always resolves.
Node backing = a path of child indices from the root. A NodeResource
is (Arc<SyntaxSnapshot>, Vec<u32>) where the vec is Node::child indices
(ALL children, incl. anonymous) from root to the node. Every method
re-resolves the node against the snapshot’s tree. This is deliberately NOT a
stored tree_sitter::Node<'tree> (which borrows the Tree and can’t be a
'static resource) and NOT a byte-range + kind (which collide on wrapper
nodes sharing a span). The path is safe and unambiguous.
Resolution is NOT O(depth), which is what made this quadratic (OA.0a).
The final step of a path walk is Node::child(i), and tree-sitter walks the
sibling list to reach index i — so resolving the i-th child is O(i), not
O(1). Re-resolving on every accessor therefore made a full pass over one
node’s children O(k²). NodeResource now memoises both its own facts and one
cursor pass over its children; see the type’s own docs.
Structs§
- Cursor
Resource - TS.2 backing for the
tree-cursorWIT resource: a mutable position in the tree, represented as achild-index path (theNodeResourcescheme) so the cursor stays a safe(snapshot, path)pair — no self-referentialtree_sitter::TreeCursor<'tree>. - Node
Resource - Backing for a
nodeWIT resource: a path ofNode::childindices from the tree root (empty = root), re-resolved againstsnapshoton each call. - Query
Resource - TS.2 backing for the
queryWIT resource: a compiled tree-sitter query plus the language it was compiled against (sorun-querycan guard a mismatched snapshot). Owned by the guest; reusable across snapshots of the same language. - Tree
Snapshot Resource - Backing for the
tree-snapshotWIT resource: a point-in-time parse tree.