Skip to content

Commit 1c86e99

Browse files
committed
LuaMacro: enhance detecting macro loading error source
Use debug.getinfo stack information directly (instead of heuristics on traceback text)
1 parent 14ecbc4 commit 1c86e99

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

plugins/luamacro/utils.lua

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ end
632632
-- ...nager\unicode_far\CommonProfile\Macros\scripts\test1.lua:9:
633633
-- attempt to perform arithmetic on a nil value
634634

635-
local function ErrMsgLoad (msg, filename, isMoonScript, mode)
635+
local function ErrMsgLoad (msg, filename, isMoonScript, mode, stacklevelinfo)
636636
local title = isMoonScript and mode=="compile" and "MoonScript" or "LuaMacro"
637637

638638
if type(msg)~="string" and type(msg)~="number" then
@@ -641,37 +641,14 @@ local function ErrMsgLoad (msg, filename, isMoonScript, mode)
641641
end
642642

643643
if mode=="run" then
644-
local fname, line, found
644+
local fname, line
645645
fname = msg:match("^error loading module .- from file '([^\n]+)':")
646646
if fname then
647-
found = true
648647
line = tonumber(msg:match("^.-\n.-:(%d+):"))
649648
else
650-
fname,line = msg:match("^(.-):(%d+):")
651-
if fname then
652-
line = tonumber(line)
653-
if string_sub(fname,1,3) ~= "..." then
654-
found = true
655-
else
656-
fname = string_sub(fname,4)
657-
-- for k=1,5 do
658-
-- if fname:utf8valid() then break end
659-
-- fname = string_sub(fname,2)
660-
-- end
661-
fname = fname:gsub("/", "\\")
662-
local middle = fname:match([=[^[^\\]*\[^\\]+\]=])
663-
if middle then
664-
local from = string_find(filename:lower(), middle:lower(), 1, true)
665-
if from then
666-
fname = string_sub(filename,1,from-1) .. fname
667-
local attr = win.GetFileAttr(fname)
668-
found = attr and not attr:find("d")
669-
end
670-
end
671-
end
672-
end
649+
fname,line = stacklevelinfo.source:match("^@(.+)"), stacklevelinfo.currentline
673650
end
674-
if found then
651+
if fname and win.GetFileAttr(fname) then
675652
if 2 == ErrMsg(msg, title, "OK;Edit", "wl") then
676653
if isMoonScript then line = GetMoonscriptLineNumber(fname,line) end
677654
editor.Editor(fname,nil,nil,nil,nil,nil,nil,line or 1,nil,65001)
@@ -792,14 +769,18 @@ local function LoadMacros (unload, paths)
792769
for _,name in ipairs(FuncList2) do env[name]=DummyFunc; end
793770
setmetatable(env,gmeta)
794771
setfenv(f, env)
795-
local ok, msg = xpcall(function() return f(FullPath, LoadCounter) end, debug.traceback)
772+
local stacklevelinfo
773+
local ok, msg = xpcall(function() return f(FullPath, LoadCounter) end, function (err)
774+
stacklevelinfo = debug.getinfo(2)
775+
return debug.traceback(err)
776+
end)
796777
if ok then
797778
for _,name in ipairs(FuncList1) do env[name]=nil; end
798779
for _,name in ipairs(FuncList2) do env[name]=nil; end
799780
else
800781
numerrors=numerrors+1
801782
msg = string.gsub(msg,"\n\t","\n ")
802-
ErrMsgLoad(msg,FullPath,isMoonScript,"run")
783+
ErrMsgLoad(msg,FullPath,isMoonScript,"run",stacklevelinfo)
803784
end
804785
end
805786

0 commit comments

Comments
 (0)