Skip to content

Commit 7b84609

Browse files
committed
feat(util): expose pretty stacktraces for notify
1 parent cb3a055 commit 7b84609

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

lua/lazy/core/util.lua

+35-28
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,41 @@ function M.norm(path)
6464
return path:sub(-1) == "/" and path:sub(1, -2) or path
6565
end
6666

67+
---@param opts? {level?: number}
68+
function M.pretty_trace(opts)
69+
opts = opts or {}
70+
local Config = require("lazy.core.config")
71+
local trace = {}
72+
local level = opts.level or 2
73+
while true do
74+
local info = debug.getinfo(level, "Sln")
75+
if not info then
76+
break
77+
end
78+
if info.what ~= "C" and not info.source:find("lazy.nvim") then
79+
local source = info.source:sub(2)
80+
if source:find(Config.options.root, 1, true) == 1 then
81+
source = source:sub(#Config.options.root + 1)
82+
end
83+
source = vim.fn.fnamemodify(source, ":p:~:.") --[[@as string]]
84+
local line = " - " .. source .. ":" .. info.currentline
85+
if info.name then
86+
line = line .. " _in_ **" .. info.name .. "**"
87+
end
88+
table.insert(trace, line)
89+
end
90+
level = level + 1
91+
end
92+
return #trace > 0 and ("\n\n# stacktrace:\n" .. table.concat(trace, "\n")) or ""
93+
end
94+
6795
---@param opts? string|{msg:string, on_error:fun(msg)}
6896
function M.try(fn, opts)
6997
opts = type(opts) == "string" and { msg = opts } or opts or {}
7098
local msg = opts.msg
7199
-- error handler
72100
local error_handler = function(err)
73-
local Config = require("lazy.core.config")
74-
local trace = {}
75-
local level = 1
76-
while true do
77-
local info = debug.getinfo(level, "Sln")
78-
if not info then
79-
break
80-
end
81-
if info.what ~= "C" and not info.source:find("lazy.nvim") then
82-
local source = info.source:sub(2)
83-
if source:find(Config.options.root, 1, true) == 1 then
84-
source = source:sub(#Config.options.root + 1)
85-
end
86-
source = vim.fn.fnamemodify(source, ":p:~:.")
87-
local line = " - " .. source .. ":" .. info.currentline
88-
if info.name then
89-
line = line .. " _in_ **" .. info.name .. "**"
90-
end
91-
table.insert(trace, line)
92-
end
93-
level = level + 1
94-
end
95-
msg = (msg and (msg .. "\n\n") or "") .. err
96-
if #trace > 0 then
97-
msg = msg .. "\n\n# stacktrace:\n" .. table.concat(trace, "\n")
98-
end
101+
msg = (msg and (msg .. "\n\n") or "") .. err .. M.pretty_trace()
99102
if opts.on_error then
100103
opts.on_error(msg)
101104
else
@@ -292,7 +295,7 @@ function M.extend(list, add)
292295
return list
293296
end
294297

295-
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number}
298+
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number, once?:boolean, stacktrace?:boolean, stacklevel?:number}
296299

297300
---@param msg string|string[]
298301
---@param opts? LazyNotifyOpts
@@ -312,8 +315,12 @@ function M.notify(msg, opts)
312315
"\n"
313316
)
314317
end
318+
if opts.stacktrace then
319+
msg = msg .. M.pretty_trace({ level = opts.stacklevel or 2 })
320+
end
315321
local lang = opts.lang or "markdown"
316-
vim.notify(msg, opts.level or vim.log.levels.INFO, {
322+
local n = opts.once and vim.notify_once or vim.notify
323+
n(msg, opts.level or vim.log.levels.INFO, {
317324
on_open = function(win)
318325
local ok = pcall(function()
319326
vim.treesitter.language.add("markdown")

0 commit comments

Comments
 (0)