Highlighter class final

Tokenizes and themes code with TextMate grammars.

final highlighter = Highlighter(languages: [dartLanguage]);
final lines = highlighter.highlight(code,
    language: 'dart', theme: githubDarkTheme);

Grammars are compiled lazily and cached. Reuse one Highlighter instead of creating one per call.

Available extensions

Constructors

Highlighter({Iterable<Language> languages = const [], RegexEngine? engine, int maxLineLength = 20000})
A highlighter for languages, using engine or DartRegexEngine.

Properties

engine → RegexEngine
The regex engine grammars are compiled for.
final
hashCode → int
The hash code for this object.
no setterinherited
languages → List<String>
Registered language names, without aliases.
no setter
maxLineLength → int
Lines longer than this many UTF-16 code units are not tokenized, as in VS Code, which uses the same default. Each becomes one token in the current scope. Some grammars have lookaheads that scan to the end of the line, so on minified code their time grows faster than line length and a bundle on one line can take seconds. 0 means no limit.
final
revision → int
Changes when registered grammar definitions or dependencies change.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

addLanguage(Language language) → void
Registers language, replacing any language with the same scope name.
clearPathLanguages() → void
Removes every custom filename and extension mapping.
grammar(String language) → Grammar
The compiled grammar for language. Throws ArgumentError if the language is not registered.
highlight(String code, {required String language, required SyntaxTheme theme}) → List<List<ThemedToken>>
Styled tokens per line. Adjacent tokens with the same style are merged.
highlightRanges(String code, {required String language, required SyntaxTheme theme}) → List<StyledRange>
Styled ranges over the whole of code, as UTF-16 offsets into it: the shape renderers that style one string at a time want. To re-highlight as the code changes, keep a TokenizedDocument instead.
language(String nameOrScope) → Language?
Looks up a language by name, alias or scope name.
languageForPath(String path) → Language?
Guesses the language of a file from its extension or name, using filename overrides, language names and aliases. lib/main.dart, pubspec.yaml, deploy.sh, README.md and Dockerfile all resolve with the bundled languages. Both / and \ separate directories.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setLanguageForPath(String nameOrExtension, String language) → void
Registers an exact basename or an extension (with a leading dot).
toAnsi(String code, {required String language, required SyntaxTheme theme, bool background = false}) → String

Available on Highlighter, provided by the HighlighterRendering extension

Highlights code and renders it for a terminal. See renderAnsi.
toHtml(String code, {required String language, required SyntaxTheme theme}) → String

Available on Highlighter, provided by the HighlighterRendering extension

Highlights code and renders it as HTML. See renderHtml.
tokenize(String code, {required String language}) → List<List<Token>>
Raw tokens per line, without theme information.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited