From d479a4bd31281b6f2cefbc3fa90afd32633b46f1 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 5 Mar 2012 17:49:48 -0500 Subject: [PATCH] nitc: move line coloration to Location Signed-off-by: Jean Privat --- src/location.nit | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/mmloader.nit | 33 +-------------------------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/location.nit b/src/location.nit index 0603050..a643c7e 100644 --- a/src/location.nit +++ b/src/location.nit @@ -128,5 +128,51 @@ class Location return column_end < other.column_end end + + # Return the associated line with the location highlihted with color and a carret under the starting position + # `color' must be and terminal escape sequence used as "{escape}[{color}m;" + # "0;31" for red + # "1;31" for bright red + # "0;32" for green + fun colored_line(color: String): String + do + var esc = 27.ascii + var def = "{esc}[0m" + var col = "{esc}[{color}m" + + var l = self + var i = l.line_start + var line_start = l.file.line_starts[i-1] + var line_end = line_start + var string = l.file.string + while line_end+1 < string.length and string[line_end+1] != '\n' and string[line_end+1] != '\r' do + line_end += 1 + end + var lstart = string.substring(line_start, l.column_start - 1) + var cend + if i != l.line_end then + cend = line_end - line_start + 1 + else + cend = l.column_end + end + var lmid + var lend + if line_start + cend <= string.length then + lmid = string.substring(line_start + l.column_start - 1, cend - l.column_start + 1) + lend = string.substring(line_start + cend, line_end - line_start - cend + 1) + else + lmid = "" + lend = "" + end + var indent = new Buffer + for j in [line_start..line_start+l.column_start-1[ do + if string[j] == '\t' then + indent.add '\t' + else + indent.add ' ' + end + end + return "\t{lstart}{col}{lmid}{def}{lend}\n\t{indent}^" + end end diff --git a/src/mmloader.nit b/src/mmloader.nit index 6bd2059..576ceb8 100644 --- a/src/mmloader.nit +++ b/src/mmloader.nit @@ -62,38 +62,7 @@ class Message else if l.file == null then return "{yellow}{l}{def}: {text}" else - var i = location.line_start - var line_start = l.file.line_starts[i-1] - var line_end = line_start - var string = l.file.string - while line_end+1 < string.length and string[line_end+1] != '\n' and string[line_end+1] != '\r' do - line_end += 1 - end - var lstart = string.substring(line_start, location.column_start - 1) - var cend - if i != location.line_end then - cend = line_end - line_start + 1 - else - cend = location.column_end - end - var lmid - var lend - if line_start + cend <= string.length then - lmid = string.substring(line_start + location.column_start - 1, cend - location.column_start + 1) - lend = string.substring(line_start + cend, line_end - line_start - cend + 1) - else - lmid = "" - lend = "" - end - var indent = new Buffer - for j in [line_start..line_start+location.column_start-1[ do - if string[j] == '\t' then - indent.add '\t' - else - indent.add ' ' - end - end - return "{yellow}{l}{def}: {text}\n\t{lstart}{bred}{lmid}{def}{lend}\n\t{indent}^" + return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}" end end end -- 1.7.9.5