diff options
author | Gregory Anders <8965202+gpanders@users.noreply.github.com> | 2022-01-06 11:10:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-06 11:10:56 -0700 |
commit | d78e46679d2ff31916091f9368367ccc1539c299 (patch) | |
tree | eab41e8ecf863638324ed85769df5c42451f1fce /src/nvim/lua/vim.lua | |
parent | 2f779e33615d41e4472b3705e68ef9465b5f5d4e (diff) | |
download | rneovim-d78e46679d2ff31916091f9368367ccc1539c299.tar.gz rneovim-d78e46679d2ff31916091f9368367ccc1539c299.tar.bz2 rneovim-d78e46679d2ff31916091f9368367ccc1539c299.zip |
feat(lua): add notify_once() (#16956)
Like vim.notify(), but only displays the notification once.
Diffstat (limited to 'src/nvim/lua/vim.lua')
-rw-r--r-- | src/nvim/lua/vim.lua | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index abf382484a..e5a5ae799b 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -425,23 +425,43 @@ function vim.defer_fn(fn, timeout) end ---- Notification provider +--- Display a notification to the user. --- ---- Without a runtime, writes to :Messages ----@see :help nvim_notify ----@param msg string Content of the notification to show to the user ----@param log_level number|nil enum from |vim.log.levels| ----@param opts table|nil additional options (timeout, etc) -function vim.notify(msg, log_level, opts) -- luacheck: no unused - if log_level == vim.log.levels.ERROR then +--- This function can be overridden by plugins to display notifications using a +--- custom provider (such as the system notification provider). By default, +--- writes to |:messages|. +--- +---@param msg string Content of the notification to show to the user. +---@param level number|nil One of the values from |vim.log.levels|. +---@param opts table|nil Optional parameters. Unused by default. +function vim.notify(msg, level, opts) -- luacheck: no unused args + if level == vim.log.levels.ERROR then vim.api.nvim_err_writeln(msg) - elseif log_level == vim.log.levels.WARN then + elseif level == vim.log.levels.WARN then vim.api.nvim_echo({{msg, 'WarningMsg'}}, true, {}) else vim.api.nvim_echo({{msg}}, true, {}) end end +do + local notified = {} + + --- Display a notification only one time. + --- + --- Like |vim.notify()|, but subsequent calls with the same message will not + --- display a notification. + --- + ---@param msg string Content of the notification to show to the user. + ---@param level number|nil One of the values from |vim.log.levels|. + ---@param opts table|nil Optional parameters. Unused by default. + function vim.notify_once(msg, level, opts) -- luacheck: no unused args + if not notified[msg] then + vim.notify(msg, level, opts) + notified[msg] = true + end + end +end ---@private function vim.register_keystroke_callback() |