Documentation
¶
Overview ¶
Package highlight colors the text of a code editor with tree-sitter.
A Session holds one open document. The typing thread calls Edit for every change and SetViewport when the visible range moves. Edit returns at once: it shifts the spans it has, so colors stay in place, and queues the change for a background worker. The worker owns the document's WebAssembly instance (see codeedit/internal/treesitter). It applies every queued change, reparses once, computes the spans of the visible range, and publishes them. Spans reports the latest published spans, and Options.Notify tells the editor when new ones arrive, so it can request a frame.
Grammars come from the packages of github.com/tyler-builds/jamb-grammars. Each one embeds its module and highlight query.
Index ¶
Constants ¶
const ABI = 1
ABI is the version of the interface between the editor and a grammar's module: the functions that the module's shim exports, and the host function it imports. A grammar reports the version it was built with, and NewSession refuses another.
const DefaultMaxBytes = 5 << 20
DefaultMaxBytes is the largest document a Session highlights when Options.MaxBytes is zero.
Variables ¶
var ErrClosed = errors.New("highlight: session is closed")
ErrClosed is returned by Edit and SetViewport after Close.
var ErrTooLarge = errors.New("highlight: document is larger than the highlighting limit")
ErrTooLarge is returned by NewSession for a document over the size limit. The editor then shows the document without highlighting.
Functions ¶
func Closest ¶
Closest finds the style for a capture name in a theme's syntax map. It tries the name itself, then the name with its last dotted part removed, and so on, so "function.method.builtin" falls back to "function.method" and then to "function". It returns the key it matched, and false when no key matches.
Types ¶
type EditRecord ¶
type EditRecord struct {
// Start is the byte offset where the edit begins, and OldEnd is the end of the replaced bytes before the edit.
Start, OldEnd int
// Text is the new bytes that replace the old range.
Text []byte
}
EditRecord replaces the bytes [Start, OldEnd) of the document with Text. An insertion has Start equal to OldEnd, and a deletion has empty Text. Offsets are bytes in the document as it is before the edit.
type Grammar ¶
type Grammar interface {
Name() string // a short name for diagnostics, such as "go"
Wasm() []byte // the module
Query() []byte // the highlight query, with inherited queries already merged
ABI() int // the interface version the module was built with (see ABI)
}
Grammar is a tree-sitter grammar compiled for the editor: a WebAssembly module with the tree-sitter runtime, the grammar and the shim, and the highlight query for that grammar. The packages of github.com/tyler-builds/jamb-grammars provide them, such as golang.Grammar, and NewGrammar makes one from a module of your own. Grammars are compared by identity, so an implementation should be a pointer. Methods may be added to Grammar in later versions.
func NewGrammar ¶
NewGrammar returns the Grammar of a module built for this version of the interface, such as one built with the build command of jamb-grammars.
type Options ¶
type Options struct {
// MaxBytes is the largest document that is highlighted. Zero means
// DefaultMaxBytes, and a negative value means no limit. A larger document
// gets ErrTooLarge from NewSession.
MaxBytes int
// Notify is called from the session's worker goroutine each time new spans
// are published. It must not block, and it must not call Close on the
// session, which waits for the worker. A typical Notify requests a repaint.
Notify func()
}
Options configures a Session.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session highlights one document. Its methods are safe for concurrent use, although Edit and SetViewport are meant for the typing thread.
func NewSession ¶
NewSession opens a document with the given grammar and starts its worker. The worker parses the text and publishes the first spans in the background. NewSession returns ErrTooLarge for a document over Options.MaxBytes, and an error for a grammar built for another version of the interface (see ABI).
func (*Session) CaptureNames ¶
CaptureNames returns the capture names of the language's query, indexed by Span.Capture. Map them to theme keys with Closest.
func (*Session) Close ¶
Close stops the worker and releases the document's instance. It waits for the worker to finish its current step, which takes a few milliseconds at most. Calls after Close return ErrClosed. Close is safe to call more than once.
func (*Session) Edit ¶
func (s *Session) Edit(e EditRecord) error
Edit applies a change to the document. It never waits for the worker: the existing spans are shifted at once, and the change is queued for the worker, which applies every queued change before one reparse. Edit returns an error, without changing anything, when the range is not inside the document.
func (*Session) Err ¶
Err returns the error that stopped highlighting, or nil. After an error the session keeps accepting calls, but it publishes no more spans.
func (*Session) SetViewport ¶
SetViewport tells the session which bytes are visible, [lo, hi). The worker computes spans for that range with a margin on each side. Until it publishes them, Spans shows the old spans, shifted by the edits. The cost of a round grows with the range, so pass the visible lines, not the whole document: spans for half of a 2.5 MB Go file take about a second.
func (*Session) Spans ¶
Spans returns the spans to paint and their revision. The revision changes whenever the spans do: when an edit shifts them, and when the worker publishes new ones, so a caller that keeps an index of the spans rebuilds it when the revision differs. The slice must not be modified. It is replaced, not changed, so a slice held by the caller stays valid.
type Span ¶
type Span struct {
// Start and End are the byte offsets of the span.
Start, End int
// Capture is the index of the capture name, in the names from CaptureNames.
Capture int
}
Span is a highlighted byte range [Start, End) and the index of its capture name in Session.CaptureNames. Spans may overlap, when several patterns capture the same text. A span later in the slice takes precedence over an earlier one that overlaps it.