syntax

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorScheme

type ColorScheme struct {
	// Name is the name of the color scheme.
	Name string
	color.ColorPalette
	// contains filtered or unexported fields
}

ColorScheme defines the token types and their styles used for syntax highlighting.

func (*ColorScheme) AddStyle

func (cs *ColorScheme) AddStyle(scope StyleScope, textStyle TextStyle, fg, bg color.Color)

func (*ColorScheme) GetStyleByID

func (cs *ColorScheme) GetStyleByID(scopeID int) StyleMeta

func (*ColorScheme) GetTokenStyle

func (cs *ColorScheme) GetTokenStyle(scope StyleScope) StyleMeta

GetTokenStyle finds a proper StyleMeta for the requested scope. When the scope has no registered style, search upwards using the parent scope. If everything has tried but still failed, it returns an empty style.

func (*ColorScheme) Scopes

func (cs *ColorScheme) Scopes() []StyleScope

Scopes returns all the registered style scopes.

type StyleMeta

type StyleMeta uint32

StyleMeta applies a bit packed binary format to encode tokens from syntax lexer, to be used as styles for text rendering. This is like TokenMetadata in Monaco/vscode.

It uses 4 bytes to hold the metadata, and the layout is as follows: Bits: 31 ... 0 [3][7][8][8][6] = 32 | | | | | | | | | └── Text style flags (6bits, bold, italic, underline, Squiggle, strikethrough, border) | | | └───── Background color ID (8bits, 0–255) | | └──────── Foreground color ID (8bits, 0–255) | └─────────── Token type (7bits, 0–127) └────────────── reserved bits (3bits)

The color IDs are mapped to indices of color palette.

func (StyleMeta) Background

func (t StyleMeta) Background() int

func (StyleMeta) Foreground

func (t StyleMeta) Foreground() int

func (StyleMeta) String

func (t StyleMeta) String() string

func (StyleMeta) TextStyle

func (t StyleMeta) TextStyle() TextStyle

func (StyleMeta) TokenType

func (t StyleMeta) TokenType() int

type StyleScope

type StyleScope string

StyleScope is a TextMate style scope notation, eg, 'keyword.control.if', 'entity.name.function'

func (StyleScope) IsChild

func (s StyleScope) IsChild(other StyleScope) bool

IsChild checks if other is a sub scope of s.

func (StyleScope) IsValid

func (s StyleScope) IsValid() bool

IsValid checks if s has a valid notation.

func (StyleScope) Parent

func (s StyleScope) Parent() StyleScope

Parent returns the parent of scope s. If s is invalid, it returns an invalid empty scope.

type TextStyle

type TextStyle uint8
const (
	Bold TextStyle = 1 << iota
	Italic
	Underline
	Squiggle
	Strikethrough
	Border
)

func (TextStyle) HasStyle

func (s TextStyle) HasStyle(mask TextStyle) bool

type TextTokens

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

func NewTextTokens

func NewTextTokens(scheme *ColorScheme) *TextTokens

func (*TextTokens) Clear

func (t *TextTokens) Clear()

Clear the tokens for reuse.

func (*TextTokens) GetColor

func (t *TextTokens) GetColor(colorID int) color.Color

func (*TextTokens) QueryRange

func (t *TextTokens) QueryRange(start, end int) []TokenStyle

Query tokens for rune range. start and end are in runes. start is inclusive and end is exclusive. This method assumes the tokens are sorted by start or end in ascending order.

func (*TextTokens) Set

func (t *TextTokens) Set(tokens ...Token)

Set adds all the tokens, replacing the existing ones. Caller should insures the tokens are sorted by the range in ascending order .

func (*TextTokens) Split

func (t *TextTokens) Split(line layout.Line, runs *[]painter.RenderRun)

Split implements painter.LineSplitter

type Token

type Token struct {
	// offset of the start rune in the document.
	Start, End int
	// Scope registered in the color scheme.
	Scope StyleScope
}

type TokenStyle

type TokenStyle struct {
	// offset of the start rune in the document.
	Start int
	// offset of the end rune in the document.
	End   int
	Style StyleMeta
}

Jump to

Keyboard shortcuts

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