snacks.nvim/docs/debug.md
Folke Lemaitre 7e5cc9bd53
docs: docgen
2024-11-18 11:53:19 +01:00

2.1 KiB

🍿 debug

Utility functions you can use in your code.

Personally, I have the code below at the top of my init.lua:

_G.dd = function(...)
  Snacks.debug.inspect(...)
end
_G.bt = function()
  Snacks.debug.backtrace()
end
vim.print = _G.dd

What this does:

  • Add a global dd(...) you can use anywhere to quickly show a notification with a pretty printed dump of the object(s) with lua treesitter highlighting
  • Add a global bt() to show a notification with a pretty backtrace.
  • Override Neovim's vim.print, which is also used by := {something = 123}

image

📚 Types

---@alias snacks.debug.Trace {name: string, time: number, [number]:snacks.debug.Trace}

📦 Module

Snacks.debug()

---@type fun(...)
Snacks.debug()

Snacks.debug.backtrace()

Show a notification with a pretty backtrace

Snacks.debug.backtrace()

Snacks.debug.inspect()

Show a notification with a pretty printed dump of the object(s) with lua treesitter highlighting and the location of the caller

Snacks.debug.inspect(...)

Snacks.debug.log()

Log a message to the file ./debug.log.

  • a timestamp will be added to every message.
  • accepts multiple arguments and pretty prints them.
  • if the argument is not a string, it will be printed using vim.inspect.
  • if the message is smaller than 120 characters, it will be printed on a single line.
Snacks.debug.log("Hello", { foo = "bar" }, 42)
-- 2024-11-08 08:56:52 Hello { foo = "bar" } 42
Snacks.debug.log(...)

Snacks.debug.profile()

Very simple function to profile a lua function.

  • flush: set to true to use jit.flush in every iteration.
  • count: defaults to 100
---@param fn fun()
---@param opts? {count?: number, flush?: boolean}
Snacks.debug.profile(fn, opts)

Snacks.debug.stats()

---@param opts? {min?: number}
Snacks.debug.stats(opts)

Snacks.debug.trace()

---@param name string?
Snacks.debug.trace(name)