treesitter

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package treesitter runs tree-sitter language modules in WebAssembly with wazero. A module is the tree-sitter runtime, one grammar and the shim, built by github.com/tyler-builds/jamb-grammars (see its shim/shim.c and its internal/grammarbuild); highlight.ABI is the version of the shim's interface that this package calls. The package compiles each module once per process, keeps the compiled code in a per-user cache directory, and creates an Instance for each document. An Instance holds one document: its text, parser, tree and highlight query. Instances are not safe for concurrent use, so each one has a single owner that calls it from one goroutine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance is one instance of a module, holding one document. It is not safe for concurrent use. After a call returns an error from the module, the instance may be left in an unknown state and should be closed.

func (*Instance) CaptureNames

func (in *Instance) CaptureNames() ([]string, error)

CaptureNames returns the names of the query's captures, indexed by the capture numbers that Spans returns.

func (*Instance) Close

func (in *Instance) Close() error

Close releases the instance and its memory.

func (*Instance) Edit

func (in *Instance) Edit(start, oldEnd int, text []byte) error

Edit replaces the bytes from start to oldEnd with text, and edits the tree to match. The edit is not reparsed until Parse. It returns an error if the range is not inside the document.

func (*Instance) Init

func (in *Instance) Init(text []byte) error

Init sets the document's text and selects the module's grammar. Any earlier tree is discarded, so the next Parse must be a full parse.

func (*Instance) MemorySize

func (in *Instance) MemorySize() uint32

MemorySize returns the size of the instance's linear memory in bytes.

func (*Instance) Parse

func (in *Instance) Parse(full bool) error

Parse parses the document. With full set it parses from scratch, otherwise it reuses the tree that the edits have been applied to.

func (*Instance) SetQuery

func (in *Instance) SetQuery(q []byte) error

SetQuery compiles the highlight query against the grammar. When the query does not match the grammar, the error gives the byte offset of the problem in the query text.

func (*Instance) Spans

func (in *Instance) Spans(lo, hi int, dst []uint32) ([]uint32, error)

Spans appends to dst the highlight captures that intersect bytes lo to hi, as (start, end, capture index) triples in the order the query yields them, and returns the extended slice.

func (*Instance) TreeHasError

func (in *Instance) TreeHasError() (bool, error)

TreeHasError reports whether the current tree contains an error node.

func (*Instance) TreeHash

func (in *Instance) TreeHash(fresh bool) (uint64, error)

TreeHash returns a hash of the current tree. With fresh set it hashes a new parse of the same text, so a caller can check that incremental parsing gives the same tree as parsing from scratch.

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module is a compiled language module. Its instances share the compiled code.

func Compile

func Compile(wasm []byte) (*Module, error)

Compile returns the compiled form of wasm, compiling it on first use. wasm must be a module built as described in the package comment, and must not be changed afterwards. The result is cached per process, keyed by the address of wasm's bytes.

func (*Module) Instantiate

func (m *Module) Instantiate() (*Instance, error)

Instantiate creates a new instance of the module. The instance starts with no document; call Init first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL