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,16 +65,17 @@ 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]
mut cline := '${iline+1:5d}| ' + sline.replace('\t', tab_spaces)
if iline == pos.line_nr && emanager.support_color { if iline == pos.line_nr && emanager.support_color {
cline = term.red( cline ) cline = term.red( cline )
} }
@ -86,17 +87,21 @@ pub fn formated_error(kind string /*error or warn*/, emsg string, filepath strin
// where it is needed. That is the reason we can not just // where it is needed. That is the reason we can not just
// use strings.repeat(` `, col) to form it. // use strings.repeat(` `, col) to form it.
mut pointerline := []string mut pointerline := []string
for i, c in cline { for i, c in sline {
if i < column { if i < column {
x := if c.is_space() { c } else { ` ` } mut x := c
if x == `\t` {
pointerline << tab_spaces
}else{
x = if x.is_space() { c } else { ` ` }
pointerline << x.str() pointerline << x.str()
}
continue continue
} }
pointerline << if emanager.support_color { term.bold(term.blue('^')) } else { '^' } pointerline << if emanager.support_color { term.bold(term.blue('^')) } else { '^' }
break break
} }
clines << ' ' + pointerline.join('') 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 }
}