Skip to content

Commit

Permalink
issue #119: use subject instead of rereading file when adding error c…
Browse files Browse the repository at this point in the history
…ontext
  • Loading branch information
mascarenhas committed Aug 27, 2018
1 parent 002f983 commit 1016213
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions typedlua/tlchecker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1962,9 +1962,14 @@ function tlchecker.typecheck (ast, subject, filename, strict, integer, color)
return env.messages
end

local function get_source_line(filename, l)
local function get_source_line(subject, filename, l)
if not subject then
local file = io.open(filename)
subject = file:read("*a")
file:close()
end
local i = 1
for source_line in io.lines(filename) do
for source_line in subject:gmatch("[^\r\n]*") do
if i == l then
return (string.gsub(source_line, "\t", " "))
end
Expand Down Expand Up @@ -1998,15 +2003,15 @@ function tlchecker.error_msgs (messages, warnings, color, line_preview)
local warning_text = color and acolor.magenta .. "warning" .. acolor.reset or "warning"
table.insert(l, string.format(msg, v.filename, v.l, v.c, warning_text, v.msg))
if line_preview then
table.insert(l, get_source_line(v.filename, v.l))
table.insert(l, get_source_line(v.subject, v.filename, v.l))
table.insert(l, get_source_arrow(v.c, color, true))
end
end
else
local error_text = color and acolor.red .. "type error" .. acolor.reset or "type error"
table.insert(l, string.format(msg, v.filename, v.l, v.c, error_text, v.msg))
if line_preview then
table.insert(l, get_source_line(v.filename, v.l))
table.insert(l, get_source_line(v.subject, v.filename, v.l))
table.insert(l, get_source_arrow(v.c, color, false))
end
n = n + 1
Expand Down
2 changes: 1 addition & 1 deletion typedlua/tltype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ function tltype.typeerror (env, tag, msg, pos)
end

local l, c = lineno(env.subject, pos)
local error_msg = { tag = tag, filename = env.filename, msg = msg, l = l, c = c }
local error_msg = { tag = tag, filename = env.filename, subject = env.subject, msg = msg, l = l, c = c }
for i, v in ipairs(env.messages) do
if l < v.l or (l == v.l and c < v.c) then
table.insert(env.messages, i, error_msg)
Expand Down

0 comments on commit 1016213

Please sign in to comment.