@@ -64,38 +64,41 @@ function M.norm(path)
64
64
return path :sub (- 1 ) == " /" and path :sub (1 , - 2 ) or path
65
65
end
66
66
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
+
67
95
--- @param opts ? string |{ msg : string , on_error : fun ( msg )}
68
96
function M .try (fn , opts )
69
97
opts = type (opts ) == " string" and { msg = opts } or opts or {}
70
98
local msg = opts .msg
71
99
-- error handler
72
100
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 ()
99
102
if opts .on_error then
100
103
opts .on_error (msg )
101
104
else
@@ -292,7 +295,7 @@ function M.extend(list, add)
292
295
return list
293
296
end
294
297
295
- --- @alias LazyNotifyOpts { lang ?: string , title ?: string , level ?: number }
298
+ --- @alias LazyNotifyOpts { lang ?: string , title ?: string , level ?: number , once ?: boolean , stacktrace ?: boolean , stacklevel ?: number }
296
299
297
300
--- @param msg string | string[]
298
301
--- @param opts ? LazyNotifyOpts
@@ -312,8 +315,12 @@ function M.notify(msg, opts)
312
315
" \n "
313
316
)
314
317
end
318
+ if opts .stacktrace then
319
+ msg = msg .. M .pretty_trace ({ level = opts .stacklevel or 2 })
320
+ end
315
321
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 , {
317
324
on_open = function (win )
318
325
local ok = pcall (function ()
319
326
vim .treesitter .language .add (" markdown" )
0 commit comments