Skip to main content

Overview

The Syntax Highlighting API provides a high-performance system for styling text with colors and attributes (bold, italic, underline, dim) in terminal applications. It’s designed for syntax highlighting but can be used for any text styling needs.

SyntaxStyle Class

SyntaxStyle.create()

Creates a new syntax style instance.
SyntaxStyle
A new SyntaxStyle instance

SyntaxStyle.fromTheme()

Creates a syntax style from a theme definition.
ThemeTokenStyle[]
required
Array of theme token styles
SyntaxStyle
A new SyntaxStyle instance with registered theme styles

SyntaxStyle.fromStyles()

Creates a syntax style from a flat style definition object.
Record<string, StyleDefinition>
required
Object mapping style names to style definitions
SyntaxStyle
A new SyntaxStyle instance with registered styles

Instance Methods

registerStyle()

Registers a new style with a given name.
string
required
Unique name for this style
StyleDefinition
required
Style definition
number
Numeric ID for the registered style

resolveStyleId()

Resolves a style name to its numeric ID.
string
required
Name of the style to resolve
number | null
Numeric style ID, or null if not found

getStyleId()

Gets a style ID by name, with fallback to base scope for dotted names.
string
required
Name of the style (supports dot notation like “keyword.control”)
number | null
Numeric style ID, or null if not found

getStyle()

Retrieves a style definition by name.
string
required
Name of the style to retrieve
StyleDefinition | undefined
The style definition, or undefined if not found

mergeStyles()

Merges multiple styles together, with later styles overriding earlier ones.
string[]
required
Names of styles to merge (later styles override earlier ones)
MergedStyle
Merged style with combined colors and attributes

getAllStyles()

Gets all registered style definitions.
Map<string, StyleDefinition>
Map of all registered styles

getRegisteredNames()

Gets all registered style names.
string[]
Array of all registered style names

getStyleCount()

Gets the total number of registered styles.
number
Number of registered styles

clearCache()

Clears the internal merged styles cache.

clearNameCache()

Clears the name-to-ID resolution cache.

getCacheSize()

Gets the current size of the merged styles cache.
number
Number of cached merged style combinations

destroy()

Destroy the style instance and free resources.

Utility Functions

convertThemeToStyles()

Converts a theme token array to a flat styles object.
ThemeTokenStyle[]
required
Theme definition array
Record<string, StyleDefinition>
Flat object mapping scope names to style definitions

Types

ColorInput

Color can be specified as a hex string or RGBA object:

RGBA

Color object with normalized values (0-1 range):

Example: Syntax Highlighting