feat(util): expose pretty stacktraces for notify

This commit is contained in:
Folke Lemaitre 2023-10-10 19:12:35 +02:00
parent cb3a0555b9
commit 7b84609a06
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 35 additions and 28 deletions

View File

@ -64,15 +64,12 @@ function M.norm(path)
return path:sub(-1) == "/" and path:sub(1, -2) or path return path:sub(-1) == "/" and path:sub(1, -2) or path
end end
---@param opts? string|{msg:string, on_error:fun(msg)} ---@param opts? {level?: number}
function M.try(fn, opts) function M.pretty_trace(opts)
opts = type(opts) == "string" and { msg = opts } or opts or {} opts = opts or {}
local msg = opts.msg
-- error handler
local error_handler = function(err)
local Config = require("lazy.core.config") local Config = require("lazy.core.config")
local trace = {} local trace = {}
local level = 1 local level = opts.level or 2
while true do while true do
local info = debug.getinfo(level, "Sln") local info = debug.getinfo(level, "Sln")
if not info then if not info then
@ -83,7 +80,7 @@ function M.try(fn, opts)
if source:find(Config.options.root, 1, true) == 1 then if source:find(Config.options.root, 1, true) == 1 then
source = source:sub(#Config.options.root + 1) source = source:sub(#Config.options.root + 1)
end end
source = vim.fn.fnamemodify(source, ":p:~:.") source = vim.fn.fnamemodify(source, ":p:~:.") --[[@as string]]
local line = " - " .. source .. ":" .. info.currentline local line = " - " .. source .. ":" .. info.currentline
if info.name then if info.name then
line = line .. " _in_ **" .. info.name .. "**" line = line .. " _in_ **" .. info.name .. "**"
@ -92,10 +89,16 @@ function M.try(fn, opts)
end end
level = level + 1 level = level + 1
end end
msg = (msg and (msg .. "\n\n") or "") .. err return #trace > 0 and ("\n\n# stacktrace:\n" .. table.concat(trace, "\n")) or ""
if #trace > 0 then
msg = msg .. "\n\n# stacktrace:\n" .. table.concat(trace, "\n")
end end
---@param opts? string|{msg:string, on_error:fun(msg)}
function M.try(fn, opts)
opts = type(opts) == "string" and { msg = opts } or opts or {}
local msg = opts.msg
-- error handler
local error_handler = function(err)
msg = (msg and (msg .. "\n\n") or "") .. err .. M.pretty_trace()
if opts.on_error then if opts.on_error then
opts.on_error(msg) opts.on_error(msg)
else else
@ -292,7 +295,7 @@ function M.extend(list, add)
return list return list
end end
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number} ---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number, once?:boolean, stacktrace?:boolean, stacklevel?:number}
---@param msg string|string[] ---@param msg string|string[]
---@param opts? LazyNotifyOpts ---@param opts? LazyNotifyOpts
@ -312,8 +315,12 @@ function M.notify(msg, opts)
"\n" "\n"
) )
end end
if opts.stacktrace then
msg = msg .. M.pretty_trace({ level = opts.stacklevel or 2 })
end
local lang = opts.lang or "markdown" local lang = opts.lang or "markdown"
vim.notify(msg, opts.level or vim.log.levels.INFO, { local n = opts.once and vim.notify_once or vim.notify
n(msg, opts.level or vim.log.levels.INFO, {
on_open = function(win) on_open = function(win)
local ok = pcall(function() local ok = pcall(function()
vim.treesitter.language.add("markdown") vim.treesitter.language.add("markdown")