LavaUI docs GitHub

protocol StatefulLexer

Swift

protocol StatefulLexer

A lexer whose highlighting for line N depends on what came before it — the gap a per-line HighlightRule list cannot close: a block comment or a string that opens on one line and closes on another.

State is the lexer's own notion of context ("inside a /* */ comment", "inside a string opened with """, unterminated"); initialState is what an empty document — or the very first line — starts with. highlight takes the state the previous line ended in and returns both this line's spans and the state the next line should start with. Chaining that output forward, line after line, is what lets a construct span lines at all; SyntaxHighlighter's cache (see EditorView/DrawList.emitEditor) is what keeps re-chaining it on every keystroke from costing the whole file instead of just the lines a change actually invalidated.

Sources/LavaText/Highlighting.swift:81

Properties

static var initialState: Self.State { get }

The state the first line of a document starts in.

Sources/LavaText/Highlighting.swift:85

Methods

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

Highlights one line, given the state the previous line ended in. Returns the line's spans and the state the next line starts in.

Sources/LavaText/Highlighting.swift:88

Type aliases

associatedtype State : Hashable

The lexer's context carried from one line to the next.

Sources/LavaText/Highlighting.swift:83