dartLine method
Highlights a line of Dart code.
Implementation
String dartLine(String line) {
var out = line;
// Line comments
final commentIdx = out.indexOf('//');
String? commentPart;
if (commentIdx >= 0) {
commentPart = out.substring(commentIdx);
out = out.substring(0, commentIdx);
}
// Strings
out = out.replaceAllMapped(
RegExp(r'"[^"]*"'),
(m) => '${theme.highlight}${m[0]}${theme.reset}',
);
out = out.replaceAllMapped(
RegExp(r"'[^']*'"),
(m) => '${theme.highlight}${m[0]}${theme.reset}',
);
// Numbers
out = out.replaceAllMapped(
RegExp(r'\b\d+(?:\.\d+)?\b'),
(m) => '${theme.selection}${m[0]}${theme.reset}',
);
// Keywords
const keywords = [
'class',
'enum',
'import',
'as',
'show',
'hide',
'void',
'final',
'const',
'var',
'return',
'if',
'else',
'for',
'while',
'switch',
'case',
'break',
'continue',
'try',
'catch',
'on',
'throw',
'new',
'this',
'super',
'extends',
'with',
'implements',
'static',
'get',
'set',
'async',
'await',
'yield',
'true',
'false',
'null'
];
final kwPattern = RegExp(r'\b(' + keywords.join('|') + r')\b');
out = out.replaceAllMapped(
kwPattern,
(m) => '${theme.accent}${theme.bold}${m[0]}${theme.reset}',
);
// Punctuation
out = out.replaceAllMapped(
RegExp(r'[\[\]\{\}\(\)\,\;\:]'),
(m) => '${theme.dim}${m[0]}${theme.reset}',
);
if (commentPart != null) {
out = '$out ${theme.gray}$commentPart${theme.reset}';
}
return out;
}