1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cmd/tools/vdoc: highlight nested string quotes properly (#9341)

This commit is contained in:
Swastik Baranwal 2021-03-17 13:20:22 +05:30 committed by GitHub
parent ae401bd930
commit 2e84773ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -141,39 +141,50 @@ fn color_highlight(code string, tb &table.Table) string {
builtin := ['bool', 'string', 'i8', 'i16', 'int', 'i64', 'i128', 'byte', 'u16', 'u32', 'u64',
'u128', 'rune', 'f32', 'f64', 'int_literal', 'float_literal', 'byteptr', 'voidptr', 'any']
highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string {
lit := match typ {
mut lit := ''
match typ {
.unone, .operator, .punctuation {
tok.kind.str()
lit = tok.kind.str()
}
.string {
term.yellow("'$tok.lit'")
use_double_quote := tok.lit.contains("'") && !tok.lit.contains('"')
unescaped_val := tok.lit.replace('\\\\', '\x01').replace_each(["\\'", "'", '\\"',
'"',
])
if use_double_quote {
s := unescaped_val.replace_each(['\x01', '\\\\', '"', '\\"'])
lit = term.yellow('"$s"')
} else {
s := unescaped_val.replace_each(['\x01', '\\\\', "'", "\\'"])
lit = term.yellow("'$s'")
}
}
.char {
term.yellow('`$tok.lit`')
lit = term.yellow('`$tok.lit`')
}
.keyword {
term.bright_blue(tok.lit)
lit = term.bright_blue(tok.lit)
}
.builtin, .symbol {
term.green(tok.lit)
lit = term.green(tok.lit)
}
.function {
term.cyan(tok.lit)
lit = term.cyan(tok.lit)
}
.number, .module_ {
term.bright_blue(tok.lit)
lit = term.bright_blue(tok.lit)
}
.boolean {
term.bright_magenta(tok.lit)
lit = term.bright_magenta(tok.lit)
}
.none_ {
term.red(tok.lit)
lit = term.red(tok.lit)
}
.prefix {
term.magenta(tok.lit)
lit = term.magenta(tok.lit)
}
else {
tok.lit
lit = tok.lit
}
}
return lit