style: better debug

This commit is contained in:
Folke Lemaitre 2023-01-03 16:17:15 +01:00
parent e93f50fd1b
commit 7b9fa284f8
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 56 additions and 12 deletions

View File

@ -101,14 +101,31 @@ function M.disable()
if not M.enabled then
return
end
if M.debug and vim.tbl_count(M.topmods) > 1 then
vim.schedule(function()
vim.notify("topmods:\n" .. vim.inspect(M.topmods), vim.log.levels.WARN, { title = "lazy.nvim" })
end)
end
-- selene:allow(global_usage)
_G.loadfile = M._loadfile
M.enabled = false
if M.debug and vim.tbl_count(M.topmods) > 1 then
M.log(M.topmods, vim.log.levels.WARN, { title = "topmods" })
end
if M.debug then
local stats = vim.deepcopy(M.stats)
stats.time = (stats.time or 0) / 1e6
stats.find.time = (stats.find.time or 0) / 1e6
stats.autoload.time = (stats.autoload.time or 0) / 1e6
M.log(stats, nil, { title = "stats" })
end
end
---@param msg string|table
---@param level? number
---@param opts? {lang?:string, title?:string}
function M.log(msg, level, opts)
if M.debug then
msg = vim.deepcopy(msg)
vim.schedule(function()
require("lazy.core.util").debug(msg, level, opts)
end)
end
end
function M.check_loaded(modname)
@ -191,9 +208,7 @@ function M.load(modkey, modpath)
entry.hash = hash
if M.debug then
vim.schedule(function()
vim.notify("[cache:load] " .. modpath)
end)
M.log("`" .. modpath .. "`", nil, { title = "Cache.load" })
end
chunk, err = M._loadfile(entry.modpath)

View File

@ -216,7 +216,16 @@ function M.lsmod(modname, fn)
end
---@param msg string|string[]
function M.notify(msg, level)
---@param opts? {lang?:string, title?:string}
function M.notify(msg, level, opts)
if vim.in_fast_event() then
vim.schedule(function()
M.notify(msg, level, opts)
end)
return
end
opts = opts or {}
if type(msg) == "table" then
msg = table.concat(
vim.tbl_filter(function(line)
@ -225,17 +234,20 @@ function M.notify(msg, level)
"\n"
)
end
local lang = opts.lang or "markdown"
vim.notify(msg, level, {
on_open = function(win)
pcall(require, "nvim-treesitter")
vim.wo[win].conceallevel = 3
vim.wo[win].concealcursor = ""
vim.wo[win].spell = false
local buf = vim.api.nvim_win_get_buf(win)
if not pcall(vim.treesitter.start, buf, "markdown") then
vim.bo[buf].filetype = "markdown"
if not pcall(vim.treesitter.start, buf, lang) then
vim.bo[buf].filetype = lang
vim.bo[buf].syntax = lang
end
end,
title = "lazy.nvim",
title = "lazy.nvim" .. (opts.title and ": " .. opts.title or ""),
})
end
@ -254,4 +266,20 @@ function M.warn(msg)
M.notify(msg, vim.log.levels.WARN)
end
---@param msg string|table
---@param level? number
---@param opts? {lang?:string, title?:string}
function M.debug(msg, level, opts)
if not require("lazy.core.config").options.debug then
return
end
opts = opts or {}
if type(msg) == "string" then
M.notify(msg, level)
else
opts.lang = "lua"
M.notify(vim.inspect(msg), level, opts)
end
end
return M

View File

@ -110,6 +110,7 @@ function M.open_cmd(cmd, opts)
buffer = float.buf,
callback = function()
float:close()
vim.cmd.checktime()
end,
})
end