LavaUI docs GitHub

struct SyntaxHighlighter

Swift

struct SyntaxHighlighter

Applies rules to a line and resolves overlaps by priority — or, if built from a StatefulLexer instead, threads that lexer's state line to line.

The rule-list form stays exactly what it was: independent of scroll position, correct one line at a time, no notion of state at all. Most syntax highlighting is that simple and should stay that simple — isStateful is false for it, and every stateful-only entry point degrades to the obvious single-line answer when called on it anyway.

Sources/LavaText/Highlighting.swift:127

Constructors

init(rules: [HighlightRule])

Creates a highlighter that applies rules to each line independently.

Sources/LavaText/Highlighting.swift:134

init<L>(lexer: L) where L : StatefulLexer

Creates a highlighter that runs lexer, carrying its state line to line.

Sources/LavaText/Highlighting.swift:148

Properties

var initialState: AnyHashable { get }

AnyHashable(()) for the rule-list form — it has no real state, but giving every highlighter a value here means a cache can seed the first line uniformly without special-casing which kind it holds.

Sources/LavaText/Highlighting.swift:169

var isEmpty: Bool { get }

True when this highlighter cannot produce a span for anything.

A plain-text document still gets a highlighter — EditorView always installs one — and asking it about a line means cutting that line out of the buffer to hand it over. For a wrapped editor that is a String.Index walk and a copy per visible logical line, every frame, to be told there is nothing to colour.

Sources/LavaText/Highlighting.swift:164

var isStateful: Bool { get }

Whether this highlighter was built from a StatefulLexer.

Sources/LavaText/Highlighting.swift:155

var rules: [HighlightRule]

The rules this highlighter applies. Empty for one built from a lexer.

Sources/LavaText/Highlighting.swift:129

Methods

func highlight(_ line: String, state: AnyHashable) -> (spans: [HighlightSpan], nextState: AnyHashable)

state is whatever the previous line's nextState was. The rule-list form ignores it and hands it back unchanged, so a caller that always goes through this entry point does not need to know which kind of highlighter it has.

Sources/LavaText/Highlighting.swift:185

func spans(in line: String) -> [HighlightSpan]

Spans for line alone, sorted by position, non-overlapping. For a stateful lexer this uses initialState regardless of what actually precedes line — correct only for a line with nothing meaningful above it. Real multi-line callers want highlight(_:state:), chained through SyntaxHighlighter.Cache — see EditorView.

Sources/LavaText/Highlighting.swift:176

Types