Documentation
¶
Overview ¶
Package highlight colors the text of a code editor with @pierre/highlights, a syntax highlighter of the Pierre Computer Company with a lexer for each of 76 languages, which runs in WebAssembly (see codeedit/internal/lexer). Languages are named as code fences name them, such as "go", "python" or "ts"; Languages lists the names.
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. It applies every queued change, relexes the changed lines and the lines after them that the change affects, up to the end of the visible range, and publishes the spans of that range; it relexes the rest of the document afterwards, a slice at a time. Spans reports the latest published spans, and Options.Notify tells the editor when new ones arrive, so it can request a frame.
Highlight colors a text that is not edited, such as a code block, in one call.
Index ¶
Constants ¶
const DefaultMaxBytes = 16 << 20
DefaultMaxBytes is the largest document a Session highlights when Options.MaxBytes is zero. The lexer's copy of a document takes about five times its size in memory.
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.
var ErrUnknownLanguage = errors.New("highlight: unknown language")
ErrUnknownLanguage is returned, wrapped with the name, by NewSession and Highlight for a language that the highlighter does not know.
Functions ¶
func CaptureNames ¶ added in v0.4.0
func CaptureNames() []string
CaptureNames returns the names of the tokens, indexed by Span.Capture: the highlighter's token names, which are the syntax names of Zed's themes, such as "keyword", "string.escape" or "function.method", with a few of its own, such as "selector.class" for CSS. Map them to theme keys with Closest. Every language uses the same names. The caller must not change the slice.
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". When none is in the map, it tries the same for the few names that Zed's themes do not style, such as "selector.class" for CSS, with the name whose style they take instead ("property"). It returns the key it matched, and false when no key matches.
func Languages ¶ added in v0.4.0
func Languages() []string
Languages returns the names that NewSession and Highlight accept, sorted: the names of the languages and their aliases, in lower case, such as "go", "golang", "python", "py", "typescript" and "ts". Names are matched in any case. "plain" (or "text") highlights nothing.
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 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 in the language lang, such as "go" (see Languages), and starts its worker. The worker lexes the text and publishes the first spans in the background. NewSession returns ErrTooLarge for a document over Options.MaxBytes, and an error wrapping ErrUnknownLanguage for a language it does not know.
func (*Session) CaptureNames ¶
CaptureNames returns the names that Span.Capture indexes, the same for every session (see the package's CaptureNames). 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 about a millisecond at most once the first spans are published. 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 it relexes. 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.
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 CaptureNames. The spans of a session do not overlap; for highlighters that make spans that do, a span later in the slice takes precedence over an earlier one that overlaps it.
func Highlight ¶ added in v0.4.0
Highlight returns the spans of code in the language lang, such as "go" (see Languages), lexed as a whole. It is for text that is not edited, such as the code blocks of a Markdown view, and takes about 10 ms per megabyte. It returns an error wrapping ErrUnknownLanguage for a language it does not know. It is safe for concurrent use.