compiler: print underlines in the same color as the error

pull/4815/head
eyelash 2020-05-10 12:27:46 +02:00 committed by GitHub
parent 5f0ad0f562
commit eabc72d4fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 36 deletions

View File

@ -34,16 +34,30 @@ pub mut:
support_color bool support_color bool
} }
pub fn (e &EManager) set_support_color(b bool) {
e.support_color = b
}
pub fn new_error_manager() &EManager { pub fn new_error_manager() &EManager {
return &EManager{ return &EManager{
support_color: term.can_show_color_on_stderr() support_color: term.can_show_color_on_stderr()
} }
} }
fn bold(msg string) string {
if !emanager.support_color {
return msg
}
return term.bold(msg)
}
fn color(kind, msg string) string {
if !emanager.support_color {
return msg
}
if kind.contains('error') {
return term.red(msg)
} else {
return term.magenta(msg)
}
}
// formatted_error - `kind` may be 'error' or 'warn' // formatted_error - `kind` may be 'error' or 'warn'
pub fn formatted_error(kind, emsg, filepath string, pos token.Position) string { pub fn formatted_error(kind, emsg, filepath string, pos token.Position) string {
mut path := filepath mut path := filepath
@ -72,16 +86,9 @@ pub fn formatted_error(kind, emsg, filepath string, pos token.Position) string {
column := imax(0, pos.pos - p - 1) column := imax(0, pos.pos - p - 1)
position := '${path}:${pos.line_nr+1}:${util.imax(1,column+1)}:' position := '${path}:${pos.line_nr+1}:${util.imax(1,column+1)}:'
scontext := source_context(kind, source, column, pos).join('\n') scontext := source_context(kind, source, column, pos).join('\n')
final_position := if emanager.support_color { term.bold(position) } else { position } final_position := bold(position)
mut final_kind := kind final_kind := bold(color(kind, kind))
if emanager.support_color { final_msg := emsg
final_kind = if kind.contains('error') {
term.bold(term.red(kind))
} else {
term.bold(term.magenta(kind))
}
}
final_msg := emsg // if emanager.support_color { term.bold(emsg) } else { emsg }
final_context := if scontext.len > 0 { '\n$scontext' } else { '' } final_context := if scontext.len > 0 { '\n$scontext' } else { '' }
// //
return '$final_position $final_kind $final_msg $final_context'.trim_space() return '$final_position $final_kind $final_msg $final_context'.trim_space()
@ -99,12 +106,8 @@ pub fn source_context(kind, source string, column int, pos token.Position) []str
for iline := bline; iline <= aline; iline++ { for iline := bline; iline <= aline; iline++ {
sline := source_lines[iline] sline := source_lines[iline]
mut cline := sline.replace('\t', tab_spaces) mut cline := sline.replace('\t', tab_spaces)
if iline == pos.line_nr && emanager.support_color { if iline == pos.line_nr {
cline = if kind.contains('error') { cline = color(kind, cline)
term.red(cline)
} else {
term.magenta(cline)
}
} }
clines << '${iline+1:5d} | ' + cline clines << '${iline+1:5d} | ' + cline
// //
@ -131,19 +134,11 @@ pub fn source_context(kind, source string, column int, pos token.Position) []str
} }
if pos.len > 1 { if pos.len > 1 {
max_len := sline.len - pointerline.len // rest of the line max_len := sline.len - pointerline.len // rest of the line
len := if pos.len > max_len { max_len } else { pos.len } len := imin(max_len, pos.len)
underline := '~'.repeat(len) underline := '~'.repeat(len)
pointerline << if emanager.support_color { pointerline << bold(color(kind, underline))
term.bold(term.blue(underline))
} else { } else {
underline pointerline << bold(color(kind, '^'))
}
} else {
pointerline << if emanager.support_color {
term.bold(term.blue('^'))
} else {
'^'
}
} }
break break
} }
@ -154,11 +149,8 @@ pub fn source_context(kind, source string, column int, pos token.Position) []str
} }
pub fn verror(kind, s string) { pub fn verror(kind, s string) {
if emanager.support_color { final_kind := bold(color(kind, kind))
eprintln(term.bold(term.red(kind)) + ': $s') eprintln('${final_kind}: $s')
} else {
eprintln('${kind}: $s')
}
exit(1) exit(1)
} }