Skip to content

Commit d3a963d

Browse files
committed
refactor(util): improved notify functions
1 parent 1f17bb7 commit d3a963d

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

lua/lazy/core/cache.lua

+6-7
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,24 @@ function M.disable()
105105
_G.loadfile = M._loadfile
106106
M.enabled = false
107107
if M.debug and vim.tbl_count(M.topmods) > 1 then
108-
M.log(M.topmods, vim.log.levels.WARN, { title = "topmods" })
108+
M.log(M.topmods, { level = vim.log.levels.WARN, title = "topmods" })
109109
end
110110
if M.debug and false then
111111
local stats = vim.deepcopy(M.stats)
112112
stats.time = (stats.time or 0) / 1e6
113113
stats.find.time = (stats.find.time or 0) / 1e6
114114
stats.autoload.time = (stats.autoload.time or 0) / 1e6
115-
M.log(stats, nil, { title = "stats" })
115+
M.log(stats, { title = "stats" })
116116
end
117117
end
118118

119119
---@param msg string|table
120-
---@param level? number
121-
---@param opts? {lang:string, title:string}
122-
function M.log(msg, level, opts)
120+
---@param opts? LazyNotifyOpts
121+
function M.log(msg, opts)
123122
if M.debug then
124123
msg = vim.deepcopy(msg)
125124
vim.schedule(function()
126-
require("lazy.core.util").debug(msg, level, opts)
125+
require("lazy.core.util").debug(msg, opts)
127126
end)
128127
end
129128
end
@@ -208,7 +207,7 @@ function M.load(modkey, modpath)
208207
entry.hash = hash
209208

210209
if M.debug then
211-
M.log("`" .. modpath .. "`", vim.log.levels.WARN, { title = "Cache.load" })
210+
M.log("`" .. modpath .. "`", { level = vim.log.levels.WARN, title = "Cache.load" })
212211
end
213212

214213
chunk, err = M._loadfile(entry.modpath)

lua/lazy/core/plugin.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function Spec:report(level)
180180
level = level or vim.log.levels.ERROR
181181
for _, notif in ipairs(self.notifs) do
182182
if notif.level >= level then
183-
Util.notify(notif.msg, notif.level)
183+
Util.notify(notif.msg, { level = notif.level })
184184
end
185185
end
186186
end

lua/lazy/core/util.lua

+30-18
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,15 @@ function M.lsmod(modname, fn)
219219
end)
220220
end
221221

222+
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number}
223+
222224
---@param msg string|string[]
223-
---@param opts? {lang:string, title:string}
224-
function M.notify(msg, level, opts)
225+
---@param opts? LazyNotifyOpts
226+
function M.notify(msg, opts)
225227
if vim.in_fast_event() then
226-
vim.schedule(function()
227-
M.notify(msg, level, opts)
228+
return vim.schedule(function()
229+
M.notify(msg, opts)
228230
end)
229-
return
230231
end
231232

232233
opts = opts or {}
@@ -239,7 +240,7 @@ function M.notify(msg, level, opts)
239240
)
240241
end
241242
local lang = opts.lang or "markdown"
242-
vim.notify(msg, level, {
243+
vim.notify(msg, opts.level or vim.log.levels.INFO, {
243244
on_open = function(win)
244245
pcall(require, "nvim-treesitter")
245246
vim.wo[win].conceallevel = 3
@@ -251,38 +252,49 @@ function M.notify(msg, level, opts)
251252
vim.bo[buf].syntax = lang
252253
end
253254
end,
254-
title = "lazy.nvim" .. (opts.title and ": " .. opts.title or ""),
255+
title = opts.title or "lazy.nvim",
255256
})
256257
end
257258

258259
---@param msg string|string[]
259-
function M.error(msg)
260-
M.notify(msg, vim.log.levels.ERROR)
260+
---@param opts? LazyNotifyOpts
261+
function M.error(msg, opts)
262+
opts = opts or {}
263+
opts.level = vim.log.levels.ERROR
264+
M.notify(msg, opts)
261265
end
262266

263267
---@param msg string|string[]
264-
function M.info(msg)
265-
M.notify(msg, vim.log.levels.INFO)
268+
---@param opts? LazyNotifyOpts
269+
function M.info(msg, opts)
270+
opts = opts or {}
271+
opts.level = vim.log.levels.INFO
272+
M.notify(msg, opts)
266273
end
267274

268275
---@param msg string|string[]
269-
function M.warn(msg)
270-
M.notify(msg, vim.log.levels.WARN)
276+
---@param opts? LazyNotifyOpts
277+
function M.warn(msg, opts)
278+
opts = opts or {}
279+
opts.level = vim.log.levels.WARN
280+
M.notify(msg, opts)
271281
end
272282

273283
---@param msg string|table
274-
---@param level? number
275-
---@param opts? {lang:string, title:string}
276-
function M.debug(msg, level, opts)
284+
---@param opts? LazyNotifyOpts
285+
function M.debug(msg, opts)
277286
if not require("lazy.core.config").options.debug then
278287
return
279288
end
280289
opts = opts or {}
290+
if opts.title then
291+
opts.title = "lazy.nvim: " .. opts.title
292+
end
281293
if type(msg) == "string" then
282-
M.notify(msg, level, opts)
294+
M.notify(msg, opts)
283295
else
284296
opts.lang = "lua"
285-
M.notify(vim.inspect(msg), level, opts)
297+
M.notify(vim.inspect(msg), opts)
286298
end
287299
end
288300

0 commit comments

Comments
 (0)