refactor(util): improved notify functions

This commit is contained in:
Folke Lemaitre 2023-01-10 08:42:20 +01:00
parent 1f17bb79b7
commit d3a963d51c
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 37 additions and 26 deletions

View File

@ -105,25 +105,24 @@ function M.disable()
_G.loadfile = M._loadfile _G.loadfile = M._loadfile
M.enabled = false M.enabled = false
if M.debug and vim.tbl_count(M.topmods) > 1 then if M.debug and vim.tbl_count(M.topmods) > 1 then
M.log(M.topmods, vim.log.levels.WARN, { title = "topmods" }) M.log(M.topmods, { level = vim.log.levels.WARN, title = "topmods" })
end end
if M.debug and false then if M.debug and false then
local stats = vim.deepcopy(M.stats) local stats = vim.deepcopy(M.stats)
stats.time = (stats.time or 0) / 1e6 stats.time = (stats.time or 0) / 1e6
stats.find.time = (stats.find.time or 0) / 1e6 stats.find.time = (stats.find.time or 0) / 1e6
stats.autoload.time = (stats.autoload.time or 0) / 1e6 stats.autoload.time = (stats.autoload.time or 0) / 1e6
M.log(stats, nil, { title = "stats" }) M.log(stats, { title = "stats" })
end end
end end
---@param msg string|table ---@param msg string|table
---@param level? number ---@param opts? LazyNotifyOpts
---@param opts? {lang:string, title:string} function M.log(msg, opts)
function M.log(msg, level, opts)
if M.debug then if M.debug then
msg = vim.deepcopy(msg) msg = vim.deepcopy(msg)
vim.schedule(function() vim.schedule(function()
require("lazy.core.util").debug(msg, level, opts) require("lazy.core.util").debug(msg, opts)
end) end)
end end
end end
@ -208,7 +207,7 @@ function M.load(modkey, modpath)
entry.hash = hash entry.hash = hash
if M.debug then if M.debug then
M.log("`" .. modpath .. "`", vim.log.levels.WARN, { title = "Cache.load" }) M.log("`" .. modpath .. "`", { level = vim.log.levels.WARN, title = "Cache.load" })
end end
chunk, err = M._loadfile(entry.modpath) chunk, err = M._loadfile(entry.modpath)

View File

@ -180,7 +180,7 @@ function Spec:report(level)
level = level or vim.log.levels.ERROR level = level or vim.log.levels.ERROR
for _, notif in ipairs(self.notifs) do for _, notif in ipairs(self.notifs) do
if notif.level >= level then if notif.level >= level then
Util.notify(notif.msg, notif.level) Util.notify(notif.msg, { level = notif.level })
end end
end end
end end

View File

@ -219,14 +219,15 @@ function M.lsmod(modname, fn)
end) end)
end end
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number}
---@param msg string|string[] ---@param msg string|string[]
---@param opts? {lang:string, title:string} ---@param opts? LazyNotifyOpts
function M.notify(msg, level, opts) function M.notify(msg, opts)
if vim.in_fast_event() then if vim.in_fast_event() then
vim.schedule(function() return vim.schedule(function()
M.notify(msg, level, opts) M.notify(msg, opts)
end) end)
return
end end
opts = opts or {} opts = opts or {}
@ -239,7 +240,7 @@ function M.notify(msg, level, opts)
) )
end end
local lang = opts.lang or "markdown" local lang = opts.lang or "markdown"
vim.notify(msg, level, { vim.notify(msg, opts.level or vim.log.levels.INFO, {
on_open = function(win) on_open = function(win)
pcall(require, "nvim-treesitter") pcall(require, "nvim-treesitter")
vim.wo[win].conceallevel = 3 vim.wo[win].conceallevel = 3
@ -251,38 +252,49 @@ function M.notify(msg, level, opts)
vim.bo[buf].syntax = lang vim.bo[buf].syntax = lang
end end
end, end,
title = "lazy.nvim" .. (opts.title and ": " .. opts.title or ""), title = opts.title or "lazy.nvim",
}) })
end end
---@param msg string|string[] ---@param msg string|string[]
function M.error(msg) ---@param opts? LazyNotifyOpts
M.notify(msg, vim.log.levels.ERROR) function M.error(msg, opts)
opts = opts or {}
opts.level = vim.log.levels.ERROR
M.notify(msg, opts)
end end
---@param msg string|string[] ---@param msg string|string[]
function M.info(msg) ---@param opts? LazyNotifyOpts
M.notify(msg, vim.log.levels.INFO) function M.info(msg, opts)
opts = opts or {}
opts.level = vim.log.levels.INFO
M.notify(msg, opts)
end end
---@param msg string|string[] ---@param msg string|string[]
function M.warn(msg) ---@param opts? LazyNotifyOpts
M.notify(msg, vim.log.levels.WARN) function M.warn(msg, opts)
opts = opts or {}
opts.level = vim.log.levels.WARN
M.notify(msg, opts)
end end
---@param msg string|table ---@param msg string|table
---@param level? number ---@param opts? LazyNotifyOpts
---@param opts? {lang:string, title:string} function M.debug(msg, opts)
function M.debug(msg, level, opts)
if not require("lazy.core.config").options.debug then if not require("lazy.core.config").options.debug then
return return
end end
opts = opts or {} opts = opts or {}
if opts.title then
opts.title = "lazy.nvim: " .. opts.title
end
if type(msg) == "string" then if type(msg) == "string" then
M.notify(msg, level, opts) M.notify(msg, opts)
else else
opts.lang = "lua" opts.lang = "lua"
M.notify(vim.inspect(msg), level, opts) M.notify(vim.inspect(msg), opts)
end end
end end