Incremental highlighting
Keep a document highlighted as it is edited, re-scanning only the lines an edit can affect.
An editor highlights the same document after every keystroke.
IncrementalHighlighter keeps it highlighted, and after an edit scans again
only what the edit can affect. CodeField and HighlightTextController are
built on it.
final document = IncrementalHighlighter(language: dartLanguage, text: source);
// After each edit, pass the whole new text.final result = document.update(editedSource);update takes the full new text and works out the edit itself, from the
part the old and new text have in common at each end. It returns the new
HighlightResult, which is also document.result. The result is always the
same as highlighting the new text from scratch.
What gets scanned again
Section titled “What gets scanned again”The scanner’s state is saved at the start of every line. After an edit, scanning resumes a line before the first change, and stops at the first line after the edit whose saved state matches the old one; the old tokens are reused from there. So typing inside a function scans a line or two, while opening a block comment scans until the comment closes — or to the end of the document.
lastRescannedLines reports how many lines the last update scanned. In a
2,000-line Dart file:
final source = [for (var i = 0; i < 2000; i++) 'int f$i() => $i;'].join('\n');final document = IncrementalHighlighter(language: dartLanguage, text: source);document.lastRescannedLines; // 2000: the first highlight scans everything
document.update(source.replaceFirst('f1000()', 'f1000x()'));document.lastRescannedLines; // 2
document.update(source.replaceFirst('int f1000', '/* int f1000'));document.lastRescannedLines; // 1001: the comment never closesSetting document.language highlights everything again in the new
language. Pass registry to the constructor for languages named inside the
code, such as Markdown fences.
A grammar whose rules read more than one line ahead tells the highlighter
where it can safely resume, with Grammar.restartLine; see
writing a grammar.
API reference
Section titled “API reference”IncrementalHighlighter
Section titled “IncrementalHighlighter”Keeps a document highlighted as it is edited, re-scanning only what an edit can affect.
The scanner state is saved at every line start. After an edit, scanning resumes a line before the first change and stops at the first line after the change whose saved state matches, then reuses the old tokens for the rest of the document. Typing inside a function re-scans a line or two; opening a block comment re-scans until the comment closes.
final doc = IncrementalHighlighter(language: dartLanguage, text: source);final result = doc.update(editedSource);IncrementalHighlighter({ required Grammar language, String text = '', LanguageRegistry? registry,})Creates a highlighter for text in language. registry resolves
languages named inside the code, such as Markdown code fences.
| Field | Type | Description |
|---|---|---|
registry |
LanguageRegistry? |
Languages for embedded regions named in the code. |
lastRescannedLines |
int |
Lines re-scanned by the last update, for diagnostics. |
| Member | Type | Description |
|---|---|---|
result |
HighlightResult |
The current result. |
language |
Grammar |
The current language. Setting it re-highlights everything. Settable. |
update(String text) |
HighlightResult |
Updates the document to text and returns the new result. |