diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2015-11-24 15:32:36 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2015-11-24 15:32:36 +0000 |
commit | 0cde41427841ad6f031175e9312f536837e40d35 (patch) | |
tree | 4afdaa2aa73ad8c2a666dacbec8c58a376ee4c67 | |
parent | 21bc46875a0a25bef9c3700442e5189aa9be3f2a (diff) | |
download | lace-0cde41427841ad6f031175e9312f536837e40d35.tar.gz |
lace.error.render: Highlight the subword the error came from
-rw-r--r-- | lib/lace/error.lua | 47 | ||||
-rw-r--r-- | test/test-lace.engine.lua | 4 |
2 files changed, 37 insertions, 14 deletions
diff --git a/lib/lace/error.lua b/lib/lace/error.lua index acc8fb2..6277233 100644 --- a/lib/lace/error.lua +++ b/lib/lace/error.lua @@ -113,20 +113,43 @@ local function _render(err) end end build_wordset(err.words, wordset) - local hlstr = "" - local cpos = 1 - for w, info in ipairs(srcline.content) do - -- Ensure that we're up to the start position of the word - while (cpos < info.spos) do - hlstr = hlstr .. " " - cpos = cpos + 1 - end - -- Highlight this word if appropriate - while (cpos <= info.epos) do - hlstr = hlstr .. (wordset[w] and "^" or " ") - cpos = cpos + 1 + + local function mark_my_words(line, wordset) + local hlstr, cpos = "", 1 + for w, info in ipairs(line) do + -- Ensure that we're up to the start position of the word + while (cpos < info.spos) do + hlstr = hlstr .. " " + cpos = cpos + 1 + end + -- TODO: The subword can be defined in a different line entirely, + -- at which point it's not a subword of word in this line. + -- This is the norm for explicit definitions. + -- Eventually we should trace back to the define's + -- definition and highlight where in that line the problem is. + if type(wordset[w]) == "table" and info.sub then + -- space for [ + hlstr, cpos = hlstr .. " ", cpos + 1 + + -- mark subword + local subhlstr, subcpos = mark_my_words(info.sub, wordset[w]) + hlstr = hlstr .. subhlstr + cpos = cpos + subcpos + + -- space for ] + hlstr, cpos = hlstr .. " ", cpos + 1 + else + -- Highlight this word if appropriate + while (cpos <= info.epos) do + hlstr = hlstr .. (wordset[w] and "^" or " ") + cpos = cpos + 1 + end + end end + return hlstr, cpos end + local hlstr, _ = mark_my_words(srcline.content, wordset) + ret[4] = hlstr -- The rendered error is those four strings joined by newlines return table.concat(ret, "\n") diff --git a/test/test-lace.engine.lua b/test/test-lace.engine.lua index c5f7751..936a2ed 100644 --- a/test/test-lace.engine.lua +++ b/test/test-lace.engine.lua @@ -263,7 +263,7 @@ function suite.subdefine_err_reported() assert(line1 == "woah", "The first line must mention the error") assert(line2 == "real-subdefine-error :: 2", "The second line is where the error happened") assert(line3 == 'allow "Yay" [error]', "The third line is the original line") - assert(line4 == " ^^^^^^^", "The fourth line highlights relevant words") + assert(line4 == " ^^^^^ ", "The fourth line highlights relevant words") end function suite.subsubdefine_works() @@ -286,7 +286,7 @@ function suite.subsubdefine_err_reported() assert(line1 == "woah", "The first line must mention the error") assert(line2 == "real-subsubdefine-error :: 1", "The second line is where the error happened") assert(line3 == 'allow "FAIL" [anyof [equal jeff banana] [error]]', "The third line is the original line") - assert(line4 == " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", "The fourth line highlights relevant words") + assert(line4 == " ^^^^^ ", "The fourth line highlights relevant words") end local count_ok = 0 |