From d3a963d51c0b5419a1fd84774b24d35a3e6c8996 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 10 Jan 2023 08:42:20 +0100 Subject: [PATCH] refactor(util): improved notify functions --- lua/lazy/core/cache.lua | 13 +++++------ lua/lazy/core/plugin.lua | 2 +- lua/lazy/core/util.lua | 48 +++++++++++++++++++++++++--------------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index 09792bc..e99f79e 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -105,25 +105,24 @@ function M.disable() _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" }) + M.log(M.topmods, { level = vim.log.levels.WARN, title = "topmods" }) end if M.debug and false 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" }) + M.log(stats, { title = "stats" }) end end ---@param msg string|table ----@param level? number ----@param opts? {lang:string, title:string} -function M.log(msg, level, opts) +---@param opts? LazyNotifyOpts +function M.log(msg, opts) if M.debug then msg = vim.deepcopy(msg) vim.schedule(function() - require("lazy.core.util").debug(msg, level, opts) + require("lazy.core.util").debug(msg, opts) end) end end @@ -208,7 +207,7 @@ function M.load(modkey, modpath) entry.hash = hash 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 chunk, err = M._loadfile(entry.modpath) diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index 8156240..135bdd6 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -180,7 +180,7 @@ function Spec:report(level) level = level or vim.log.levels.ERROR for _, notif in ipairs(self.notifs) do if notif.level >= level then - Util.notify(notif.msg, notif.level) + Util.notify(notif.msg, { level = notif.level }) end end end diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 981ad34..b3a9811 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -219,14 +219,15 @@ function M.lsmod(modname, fn) end) end +---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number} + ---@param msg string|string[] ----@param opts? {lang:string, title:string} -function M.notify(msg, level, opts) +---@param opts? LazyNotifyOpts +function M.notify(msg, opts) if vim.in_fast_event() then - vim.schedule(function() - M.notify(msg, level, opts) + return vim.schedule(function() + M.notify(msg, opts) end) - return end opts = opts or {} @@ -239,7 +240,7 @@ function M.notify(msg, level, opts) ) end local lang = opts.lang or "markdown" - vim.notify(msg, level, { + vim.notify(msg, opts.level or vim.log.levels.INFO, { on_open = function(win) pcall(require, "nvim-treesitter") vim.wo[win].conceallevel = 3 @@ -251,38 +252,49 @@ function M.notify(msg, level, opts) vim.bo[buf].syntax = lang end end, - title = "lazy.nvim" .. (opts.title and ": " .. opts.title or ""), + title = opts.title or "lazy.nvim", }) end ---@param msg string|string[] -function M.error(msg) - M.notify(msg, vim.log.levels.ERROR) +---@param opts? LazyNotifyOpts +function M.error(msg, opts) + opts = opts or {} + opts.level = vim.log.levels.ERROR + M.notify(msg, opts) end ---@param msg string|string[] -function M.info(msg) - M.notify(msg, vim.log.levels.INFO) +---@param opts? LazyNotifyOpts +function M.info(msg, opts) + opts = opts or {} + opts.level = vim.log.levels.INFO + M.notify(msg, opts) end ---@param msg string|string[] -function M.warn(msg) - M.notify(msg, vim.log.levels.WARN) +---@param opts? LazyNotifyOpts +function M.warn(msg, opts) + opts = opts or {} + opts.level = vim.log.levels.WARN + M.notify(msg, opts) end ---@param msg string|table ----@param level? number ----@param opts? {lang:string, title:string} -function M.debug(msg, level, opts) +---@param opts? LazyNotifyOpts +function M.debug(msg, opts) if not require("lazy.core.config").options.debug then return end opts = opts or {} + if opts.title then + opts.title = "lazy.nvim: " .. opts.title + end if type(msg) == "string" then - M.notify(msg, level, opts) + M.notify(msg, opts) else opts.lang = "lua" - M.notify(vim.inspect(msg), level, opts) + M.notify(vim.inspect(msg), opts) end end