feat: added debug

This commit is contained in:
Folke Lemaitre 2024-11-04 18:56:17 +01:00
parent f4e0130ec3
commit 6cb43f6033
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 40 additions and 0 deletions

39
lua/snacks/debug.lua Normal file
View file

@ -0,0 +1,39 @@
---@class snacks.debug
---@overload fun(msg: string|string[], opts?: snacks.notify.Opts)
local M = setmetatable({}, {
__call = function(t, ...)
return t.inspect(...)
end,
})
function M.inspect(...)
local len = select("#", ...) ---@type number
local obj = { ... } ---@type unknown[]
local caller = debug.getinfo(1, "S")
for level = 2, 10 do
local info = debug.getinfo(level, "S")
if info and info.source ~= caller.source and info.what == "Lua" and info.source ~= "lua" then
caller = info
break
end
end
local title = "Debug: " .. vim.fn.fnamemodify(caller.source:sub(2), ":~:.") .. ":" .. caller.linedefined
Snacks.notify.warn(vim.inspect(len == 1 and obj[1] or len > 0 and obj or nil), { title = title, lang = "lua" })
end
function M.backtrace()
local trace = {}
for level = 2, 20 do
local info = debug.getinfo(level, "Sln")
if info and info.what == "Lua" and info.source ~= "lua" then
local line = "- `" .. vim.fn.fnamemodify(info.source:sub(2), ":p:~:.") .. "`:" .. info.currentline
if info.name then
line = line .. " _in_ **" .. info.name .. "**"
end
table.insert(trace, line)
end
end
Snacks.notify.warn(#trace > 0 and (table.concat(trace, "\n")) or "", { title = "Backtrace" })
end
return M

View file

@ -11,6 +11,7 @@
---@field git snacks.git
---@field gitbrowse snacks.gitbrowse
---@field notify snacks.notify
---@field debug snacks.debug
local M = {}
setmetatable(M, {