mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
feat: added debug
This commit is contained in:
parent
f4e0130ec3
commit
6cb43f6033
2 changed files with 40 additions and 0 deletions
39
lua/snacks/debug.lua
Normal file
39
lua/snacks/debug.lua
Normal 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
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
---@field git snacks.git
|
||||
---@field gitbrowse snacks.gitbrowse
|
||||
---@field notify snacks.notify
|
||||
---@field debug snacks.debug
|
||||
local M = {}
|
||||
|
||||
setmetatable(M, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue