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:116
Constructors
init(rules: [HighlightRule])
Not documented.
Sources/LavaText/Highlighting.swift:121
init<L>(lexer: L) where L : StatefulLexer
Not documented.
Sources/LavaText/Highlighting.swift:134
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:154
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:149
var isStateful: Bool { get }
Not documented.
Sources/LavaText/Highlighting.swift:140
var rules: [HighlightRule]
Not documented.
Sources/LavaText/Highlighting.swift:117
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:170
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:161
Types
SyntaxHighlighter.Cache struct
Per-line spans plus the state each line started in, updated incrementally as the buffer edits rather than re-lexed whole on every keystroke — the property a stateful lexer needs and a rule list already got for free from being stateless.