highlight

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 8 Imported by: 2 modules, 1 package

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

View Source
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.

View Source
const DefaultMaxBytes = 5 << 20

DefaultMaxBytes is the largest document a Session highlights when Options.MaxBytes is zero.

Variables

View Source
var ErrClosed = errors.New("highlight: session is closed")

ErrClosed is returned by Edit and SetViewport after Close.

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

func Closest[V any](styles map[string]V, capture string) (string, V, bool)

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

func NewGrammar(name string, wasm, query []byte) Grammar

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

func NewSession(lang Grammar, text []byte, opts Options) (*Session, error)

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

func (s *Session) CaptureNames() []string

CaptureNames returns the capture names of the language's query, indexed by Span.Capture. Map them to theme keys with Closest.

func (*Session) Close

func (s *Session) Close() error

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

func (s *Session) Err() error

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

func (s *Session) SetViewport(lo, hi int) error

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

func (s *Session) Spans() ([]Span, uint64)

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.

func (*Session) Synced

func (s *Session) Synced() uint64

Synced returns the number of edits that the published spans reflect. Until it reaches the number of edits made, the spans of the latest edits are the earlier spans shifted into place.

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.

Jump to

Keyboard shortcuts

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