vdoc: improve syntax highlighting (#9323)

pull/9325/head
Swastik Baranwal 2021-03-16 15:35:36 +05:30 committed by GitHub
parent 0547a0a9cd
commit 0b0e96a8cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 12 deletions

View File

@ -89,6 +89,9 @@ enum HighlightTokenTyp {
punctuation
string
symbol
none_
module_
prefix
}
struct SearchModuleResult {

View File

@ -160,9 +160,18 @@ fn color_highlight(code string, tb &table.Table) string {
.function {
term.cyan(tok.lit)
}
.number {
.number, .module_ {
term.bright_blue(tok.lit)
}
.boolean {
term.bright_magenta(tok.lit)
}
.none_ {
term.red(tok.lit)
}
.prefix {
term.magenta(tok.lit)
}
else {
tok.lit
}
@ -170,6 +179,7 @@ fn color_highlight(code string, tb &table.Table) string {
return lit
}
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
mut prev_prev := token.Token{}
mut prev := token.Token{}
mut tok := s.scan()
mut next_tok := s.scan()
@ -181,13 +191,23 @@ fn color_highlight(code string, tb &table.Table) string {
match tok.kind {
.name {
if (tok.lit in builtin || tb.known_type(tok.lit))
&& (next_tok.kind != .lpar || prev.kind != .key_fn) {
&& (next_tok.kind != .lpar || prev.kind !in [.key_fn, .rpar]) {
tok_typ = .builtin
} else if next_tok.kind in [.lcbr, .rpar, .eof]
&& (next_tok.kind != .rpar || prev.kind in [.name, .amp]) {
} else if
next_tok.kind in [.lcbr, .rpar, .eof, .comma, .pipe, .name, .rcbr, .assign, .key_pub, .key_mut, .pipe, .comma]
&& prev.kind in [.name, .amp, .rsbr, .key_type, .assign, .dot, .question, .rpar, .key_struct, .key_enum, .pipe, .key_interface]
&& (tok.lit[0].ascii_str().is_upper() || prev_prev.lit in ['C', 'JS']) {
tok_typ = .symbol
} else if next_tok.kind in [.lpar, .lt] {
tok_typ = .function
} else if next_tok.kind == .dot {
if tok.lit in ['C', 'JS'] {
tok_typ = .prefix
} else {
tok_typ = .module_
}
} else if tok.lit in ['r', 'c'] && next_tok.kind == .string {
tok_typ = .prefix
} else {
tok_typ = .name
}
@ -210,6 +230,9 @@ fn color_highlight(code string, tb &table.Table) string {
.lpar, .lcbr, .rpar, .rcbr, .lsbr, .rsbr, .semicolon, .colon, .comma, .dot {
tok_typ = .punctuation
}
.key_none {
tok_typ = .none_
}
else {
if token.is_key(tok.lit) || token.is_decl(tok.kind) {
tok_typ = .keyword
@ -220,18 +243,20 @@ fn color_highlight(code string, tb &table.Table) string {
}
}
buf.write_string(highlight_code(tok, tok_typ))
if prev.kind != .eof {
prev = tok
} else {
if prev_prev.kind == .eof {
break
}
if next_tok.kind != .eof {
i = tok.pos + tok.len
tok = next_tok
next_tok = s.scan()
} else {
prev_prev = prev
if prev.kind == .eof {
break
}
prev = tok
if next_tok.kind == .eof {
break
}
i = tok.pos + tok.len
tok = next_tok
next_tok = s.scan()
} else {
buf.write_b(code[i])
i++