From 2e84773ef1b2a786a401e6347f122664fb64c329 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Wed, 17 Mar 2021 13:20:22 +0530 Subject: [PATCH] cmd/tools/vdoc: highlight nested string quotes properly (#9341) --- cmd/tools/vdoc/utils.v | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/cmd/tools/vdoc/utils.v b/cmd/tools/vdoc/utils.v index 0ab7d2041c..e97fca7c69 100644 --- a/cmd/tools/vdoc/utils.v +++ b/cmd/tools/vdoc/utils.v @@ -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