shiki_flutter library

A TextMate-grammar based syntax highlighter for Flutter, ported from Shiki.

Load a language grammar and a Shiki theme into a ShikiHighlighter, then either read the ThemedTokens directly via ShikiHighlighter.codeToTokens or render them with codeToTextSpan / ShikiCodeView.

import 'package:shiki_flutter/shiki_flutter.dart';
import 'package:shiki_flutter/langs.dart';
import 'package:shiki_flutter/themes.dart';

final highlighter = ShikiHighlighter();
// lang/theme are objects and are loaded on demand.
final span = codeToTextSpan(highlighter, sourceCode,
    lang: CodeLanguages.dart, theme: ShikiThemes.githubDark);

Classes

CodeLanguage
A bundled TextMate grammar and its metadata (id, aliases, display name, classification, and the embedded grammars it depends on).
CodeLanguages
Typesafe, tree-shakeable access to every language grammar bundled with the package. Referencing a member (e.g. abap) pulls in only that grammar and its embedded dependencies; grammars you never reference are tree-shaken out of the build.
FontStyle
Font-style bit flags. notSet (-1) means "inherit".
GutterStyle
Styling for the line-number gutter: the numbers themselves, the gap between the gutter and the code, and an optional divider between them.
OnigCaptureIndex
A captured range within a match. Mirrors IOnigCaptureIndex.
OnigMatch
The result of OnigScanner.findNextMatch. Mirrors IOnigMatch.
OnigScanner
Scans a line for the earliest match among a set of patterns.
OnigString
Wraps a string being scanned. Mirrors OnigString.
PierreThemes
Typesafe, tree-shakeable access to the 10 opt-in Pierre themes. Separate from ShikiThemes and not counted among the bundled Shiki themes. Referencing a member (e.g. pierreDark) pulls in only that theme.
ShikiCodeListView
Displays code highlighted with lang/theme, rendering one line per row via a lazily-built ListView so large files stay smooth.
ShikiCodeView
Displays code highlighted with the given lang and theme.
ShikiDualTheme
A pair of themes that resolve switches between based on brightness, letting a highlighter track light/dark mode without being rebuilt.
ShikiHighlighter
A synchronous, TextMate-grammar based syntax highlighter.
ShikiHighlighterConfig
Global defaults for ShikiHighlighter, split by platform so IO and web can be configured independently.
ShikiHighlighterEmbeddedEngine
The default engine: the pure-Dart DartOnigScanner backed by OnigRegex.
ShikiHighlighterEngine
Factory for scanners and strings: the pluggable engine seam. Mirrors vscode-textmate's IOnigLib.
ShikiTextEditingController
A TextEditingController that supports syntax highlighting using Shiki.
ShikiTheme
A single bundled theme description.
ShikiThemeBase
A theme, or a light/dark pair of themes, that a ShikiTheme highlighter can be configured with. Implemented by ShikiTheme (a single theme) and ShikiDualTheme (a light/dark pair resolved at highlight time).
ShikiThemes
Typesafe, tree-shakeable access to every theme bundled with the package. Referencing a member (e.g. andromeeda) pulls in only that theme; themes you never reference are tree-shaken out of the build.
ThemedToken
A single highlighted token: a run of characters sharing one style.
ThemeRegistration
A resolved Shiki theme: normalized settings, resolved fg/bg, and color replacements for any non-hex colors that vscode-textmate cannot represent.
TokenCache
A bounded least-recently-used cache from a tokenize key to its List<List<ThemedToken>> result.
TokenizeOptions
Options for ShikiHighlighter.codeToTokens.

Enums

GrammarCategory
The classification tags a CodeLanguage can carry, mirroring the GrammarCategory union from tm-grammars.

Constants

kUnmatchedOffset → const int
Sentinel used for capture groups that did not participate in a match.

Functions

applyColorReplacements(String? color, Map<String, String>? replacements) String?
Applies color replacements to a resolved color (case-insensitive key).
codeToLineSpans(ShikiHighlighter highlighter, String code, {required CodeLanguage lang, required ShikiTheme theme, TextStyle? baseStyle}) List<List<TextSpan>>
Highlights code and returns its styled spans grouped by line.
codeToTextSpan(ShikiHighlighter highlighter, String code, {required CodeLanguage lang, required ShikiTheme theme, TextStyle? baseStyle}) TextSpan
Highlights code and returns a ready-to-use TextSpan.
lineToTextSpan(List<InlineSpan> spans) TextSpan
Assembles one line's spans into a single TextSpan for Text.rich.
normalizeTheme(ThemeRegistration theme) ThemeRegistration
Normalizes a raw theme: fills in defaults, guesses fg/bg, and moves non-hex colors into ThemeRegistration.colorReplacements.
parseColor(String? input) Color?
Parses a CSS color string into a Flutter Color.
parseHexColor(String? hex) Color?
Alias for parseColor, kept for source compatibility. Despite the name it now also parses color(display-p3 ...) and color(srgb ...).
splitLines(String code) List<({String content, int offset})>
Splits code into (lineContent, offset) pairs, preserving offsets into the original string and excluding the newline characters from the content.
themedTokenStyle(ThemedToken token, {TextStyle? baseStyle}) TextStyle
Builds a TextStyle for a single ThemedToken, layered over baseStyle.
tokensToLineSpans(List<List<ThemedToken>> lines, {TextStyle? baseStyle}) List<List<TextSpan>>
Converts a grid of themed tokens into styled spans grouped by line.
tokensToTextSpan(List<List<ThemedToken>> lines, {TextStyle? baseStyle}) TextSpan
Converts a grid of themed tokens (lines of tokens) into a single TextSpan, inserting newlines between lines.

Exceptions / Errors

ShikiError
Thrown for highlighter usage errors (unknown language/theme, etc.).