From 4d48bf359df98512a2a741d48ea222d055b733c0 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 13 Sep 2018 16:01:57 -0700 Subject: Improvements to indented text parsing and error reporting. --- pretty_errors.lua | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'pretty_errors.lua') diff --git a/pretty_errors.lua b/pretty_errors.lua index f2a608e..7402703 100644 --- a/pretty_errors.lua +++ b/pretty_errors.lua @@ -35,19 +35,37 @@ format_error = function(err) end end if err_line then - local box_width = 60 local before = err_line:sub(1, err_linepos - 1) local during = err_line:sub(err_linepos, err_linepos + err_size - 1) local after = err_line:sub(err_linepos + err_size, -1) err_line = "\027[0m" .. tostring(before) .. "\027[41;30m" .. tostring(during) .. tostring(nl_indicator) .. "\027[0m" .. tostring(after) - err_msg = err_msg .. "\n\027[2m" .. tostring(fmt_str:format(err_linenum)) .. tostring(err_line) .. "\027[0m\n" .. tostring(pointer) - local err_text = "\027[47;31;1m" .. tostring((" " .. err.error):wrap_to_1(box_width):gsub("\n", "\n\027[47;31;1m ")) - if err.hint then - err_text = err_text .. "\n\027[47;30m" .. tostring((" Suggestion: " .. tostring(err.hint)):wrap_to_1(box_width):gsub("\n", "\n\027[47;30m ")) + err_msg = err_msg .. "\n\027[2m" .. tostring(fmt_str:format(err_linenum)) .. tostring(err_line) .. "\027[0m" + end + local _, err_linenum_end, err_linepos_end = string2.line_at(err.source, err.stop) + if err_linenum_end == err_linenum then + err_msg = err_msg .. "\n" .. tostring(pointer) + else + for i = err_linenum + 1, err_linenum_end do + do + local line = string2.line(err.source, i) + if line then + if i == err_linenum_end then + local during, after = line:sub(1, err_linepos_end - 1), line:sub(err_linepos_end, -1) + err_msg = err_msg .. "\n\027[2m" .. tostring(fmt_str:format(i)) .. "\027[0;41;30m" .. tostring(during) .. "\027[0m" .. tostring(after) + else + err_msg = err_msg .. "\n\027[2m" .. tostring(fmt_str:format(i)) .. "\027[0m" .. tostring(line) .. "\027[0m" + end + end + end end - err_msg = err_msg .. ("\n\027[33;1m " .. box(err_text):gsub("\n", "\n ")) end - for i = err_linenum + 1, err_linenum + context do + local box_width = 70 + local err_text = "\027[47;31;1m" .. tostring((" " .. err.error):wrap_to_1(box_width):gsub("\n", "\n\027[47;31;1m ")) + if err.hint then + err_text = err_text .. "\n\027[47;30m" .. tostring((" Suggestion: " .. tostring(err.hint)):wrap_to_1(box_width):gsub("\n", "\n\027[47;30m ")) + end + err_msg = err_msg .. ("\n\027[33;1m " .. box(err_text):gsub("\n", "\n ")) + for i = err_linenum_end + 1, err_linenum_end + context do do local line = string2.line(err.source, i) if line then -- cgit v1.2.3