syntax

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 15 Imported by: ? 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 syntax incrementally tokenizes TextMate grammars and colors their scopes with VS Code themes. Documents retain line states and token buffers.

Index

Constants

This section is empty.

Variables

View Source
var BuiltinThemeIDs = [...]string{"monokai", "dark-plus", "light-plus"}

Functions

func ReleaseGrammarCache

func ReleaseGrammarCache()

ReleaseGrammarCache drops idle compiled grammars. Active documents keep their grammar valid and do not lose line states or captures.

Types

type Capture

type Capture struct {
	Capture, Scopes    string
	StartByte, EndByte uint32
}

Capture is a render-neutral UTF-8 byte range. Scopes preserves the full TextMate scope stack; Capture is its coarse category for editor behavior.

type Color

type Color struct{ R, G, B, A uint8 }

func (Color) Hex

func (c Color) Hex() string

type Definitions

type Definitions struct {
	Captures     map[string]string `json:"captures"`
	SourceScopes map[string]string `json:"sourceScopes"`
}

Definitions maps lexical token categories to TextMate scopes. The longest capture prefix wins, so function.method.call can inherit function.method.

func DefaultDefinitions

func DefaultDefinitions() *Definitions

func ParseDefinitions

func ParseDefinitions(data []byte) (*Definitions, error)

func (*Definitions) Scope

func (d *Definitions) Scope(capture string) string

func (*Definitions) WithOverrides

func (d *Definitions) WithOverrides(overrides *Definitions) *Definitions

type Document

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

Document caches each line's ending state and tokens. Updates tokenize from the first changed line until the unchanged suffix has the same start state. Its buffers are reused; a Document belongs to one worker.

func (*Document) Captures

func (d *Document) Captures() []Capture

Captures borrows the current ranges until the next update or Close.

func (*Document) Close

func (d *Document) Close()

func (*Document) Language

func (d *Document) Language() string

func (*Document) Tokens

func (d *Document) Tokens(theme *Theme, definitions *Definitions) []Token

Tokens recolors cached scopes. Neither theme changes nor identical updates invoke the tokenizer. The unchanged source/theme path allocates no memory.

func (*Document) Update

func (d *Document) Update[V piecetree.Value](filename string, value V) error

Update detects file extensions or language IDs. Unknown languages stay plain.

func (*Document) UpdateRange

func (d *Document) UpdateRange[V piecetree.Value](filename string, value V, start, oldEnd, newEnd int) error

UpdateRange accepts a known UTF-8 edit. Bytes outside the edit must be equal.

func (*Document) UpdateSnapshot

func (d *Document) UpdateSnapshot(filename string, source piecetree.Snapshot) error

UpdateSnapshot is the worker interface for immutable document text.

type FontStyle

type FontStyle uint8
const (
	Italic FontStyle = 1 << iota
	Bold
	Underline
	Strikethrough
)

type Painter

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

Painter resolves lexical scopes locally, independent of the tokenizer.

func (*Painter) Tokens

func (p *Painter) Tokens(language string, captures []Token, theme *Theme, definitions *Definitions) []Token

type Style

type Style struct {
	Foreground Color
	FontStyle  FontStyle
}

func (Style) CSS

func (s Style) CSS() string

type Theme

type Theme struct {
	Name string
	Type string
	// contains filtered or unexported fields
}

Theme is immutable after loading and can be shared by multiple documents.

func BuiltinTheme

func BuiltinTheme(id string) (*Theme, error)

func LoadTheme

func LoadTheme(source fs.FS, filename string) (*Theme, error)

LoadTheme reads a JSON/JSONC color theme and resolves relative includes.

func (*Theme) Color

func (t *Theme) Color(key string) Color

func (*Theme) LookupColor

func (t *Theme) LookupColor(key string) (Color, bool)

LookupColor distinguishes an omitted color from an explicitly transparent one.

func (*Theme) Resolve

func (t *Theme) Resolve(scopes ...string) Style

Resolve applies scope specificity, property inheritance and last-rule wins.

type Token

type Token struct {
	Capture    string
	Scopes     string // Space-separated TextMate scopes, outermost first.
	CSS        string
	Start, End int
	Style      Style
}

Jump to

Keyboard shortcuts

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