colorize function
Implementation
String colorize(String content, {int tabWeight = 2}) {
final Lexer lexer = Lexer.tokenize(content);
const newBlockTokens = [
TokenType.kThen,
TokenType.kDo,
TokenType.kWhile,
TokenType.kRepeat,
];
const endBlockTokens = [TokenType.kElseIf, TokenType.kElse, TokenType.kEnd];
bool newLineStart = false;
int tabs = 0;
String prettyContent = '';
for (final Token t in lexer.tokens) {
final TokenType type = t.type;
String nextContent = pretty(t).toString();
if (type == TokenType.kNewLine) {
newLineStart = true;
nextContent = '$nextContent\n';
} else if (newBlockTokens.contains(type)) {
tabs++;
} else if (endBlockTokens.contains(type)) {
tabs = max(0, tabs - 1);
}
if (newLineStart && type != TokenType.kNewLine) {
nextContent = '${indent(tabs * tabWeight)}$nextContent';
newLineStart = false;
}
prettyContent += ' $nextContent';
}
return prettyContent;
}