Skip to main content

Module tree_resource

Module tree_resource 

Source
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§

CursorResource
TS.2 backing for the tree-cursor WIT resource: a mutable position in the tree, represented as a child-index path (the NodeResource scheme) so the cursor stays a safe (snapshot, path) pair — no self-referential tree_sitter::TreeCursor<'tree>.
NodeResource
Backing for a node WIT resource: a path of Node::child indices from the tree root (empty = root), re-resolved against snapshot on each call.
QueryResource
TS.2 backing for the query WIT resource: a compiled tree-sitter query plus the language it was compiled against (so run-query can guard a mismatched snapshot). Owned by the guest; reusable across snapshots of the same language.
TreeSnapshotResource
Backing for the tree-snapshot WIT resource: a point-in-time parse tree.