errors: fix caret position on lines with tabs

pull/4267/head
Delyan Angelov 2020-04-06 20:35:24 +03:00
parent 3b3d248637
commit bc2ea2f3d4
2 changed files with 40 additions and 24 deletions

View File

@ -65,38 +65,43 @@ pub fn formated_error(kind string /*error or warn*/, emsg string, filepath strin
break break
} }
} }
column = pos.pos - p column = util.imax(0, pos.pos - p - 1)
} }
position := '${path}:${pos.line_nr+1}:$column:' position := '${path}:${pos.line_nr+1}:$column:'
// //
bline := pos.line_nr - error_context_before bline := util.imax(0, pos.line_nr - error_context_before)
aline := pos.line_nr + error_context_after aline := util.imin(source_lines.len-1, pos.line_nr + error_context_after)
mut clines := []string mut clines := []string
for iline, sline in source_lines { tab_spaces := ' '
if iline >= bline && iline <= aline { for iline := bline; iline <= aline; iline++ {
mut cline := '${iline+1:5d}| $sline' sline := source_lines[iline]
if iline == pos.line_nr && emanager.support_color { mut cline := '${iline+1:5d}| ' + sline.replace('\t', tab_spaces)
cline = term.red( cline ) if iline == pos.line_nr && emanager.support_color {
} cline = term.red( cline )
clines << cline }
// clines << cline
if iline == pos.line_nr { //
// The pointerline should have the same spaces/tabs as the offending if iline == pos.line_nr {
// line, so that it prints the ^ character exactly on the *same spot* // The pointerline should have the same spaces/tabs as the offending
// where it is needed. That is the reason we can not just // line, so that it prints the ^ character exactly on the *same spot*
// use strings.repeat(` `, col) to form it. // where it is needed. That is the reason we can not just
mut pointerline := []string // use strings.repeat(` `, col) to form it.
for i, c in cline { mut pointerline := []string
if i < column { for i, c in sline {
x := if c.is_space() { c } else { ` ` } if i < column {
mut x := c
if x == `\t` {
pointerline << tab_spaces
}else{
x = if x.is_space() { c } else { ` ` }
pointerline << x.str() pointerline << x.str()
continue
} }
pointerline << if emanager.support_color { term.bold(term.blue('^')) } else { '^' } continue
break
} }
clines << ' ' + pointerline.join('') pointerline << if emanager.support_color { term.bold(term.blue('^')) } else { '^' }
break
} }
clines << '${0:5d}| ' + pointerline.join('')
} }
} }
source_context += clines.join('\n') source_context += clines.join('\n')

View File

@ -174,3 +174,14 @@ pub fn read_file(file_path string) ?string {
} }
return raw_text return raw_text
} }
[inline]
fn imin(a, b int) int {
return if a < b { a } else { b }
}
[inline]
fn imax(a, b int) int {
return if a > b { a } else { b }
}