Skip to content

Commit 83b60f2

Browse files
committed
fix: handle vim.notify nil messages for nvim-notify. Fixes #109
1 parent db1628f commit 83b60f2

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

lua/noice/message/manager.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919

2020
---@param message NoiceMessage
2121
function M.add(message)
22-
if not message:is_empty() then
22+
if not (message:is_empty() and vim.tbl_isempty(message.opts)) then
2323
message.tick = next_tick()
2424
message.mtime = vim.fn.localtime()
2525
M._history[message.id] = message
@@ -80,6 +80,10 @@ function M.sort(messages, reverse)
8080
)
8181
end
8282

83+
function M.get_by_id(id)
84+
return M._history[id]
85+
end
86+
8387
---@class NoiceMessageOpts
8488
---@field history? boolean
8589
---@field sort? boolean

lua/noice/source/notify.lua

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function M.notify(msg, level, opts)
5555
message.opts = opts or {}
5656
message.level = level
5757
message.once = true
58+
59+
if msg == nil then
60+
-- special case for some destinations like nvim-notify
61+
message.opts.is_nil = true
62+
end
63+
5864
Msg.check_clear()
5965
Manager.add(message)
6066
if Util.is_blocking() then

lua/noice/view/backend/notify.lua

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local require = require("noice.util.lazy")
22

33
local Util = require("noice.util")
44
local View = require("noice.view")
5+
local Manager = require("noice.message.manager")
56

67
---@class NoiceNotifyOptions
78
---@field title string
@@ -16,7 +17,7 @@ local defaults = {
1617
}
1718

1819
---@class NotifyInstance
19-
---@field notify fun(msg:string, level?:string|number, opts?:table): notify.Record}
20+
---@field notify fun(msg:string?, level?:string|number, opts?:table): notify.Record}
2021

2122
---@alias notify.RenderFun fun(buf:buffer, notif: Notification, hl: NotifyBufHighlights, config: notify.Config)
2223

@@ -25,7 +26,6 @@ local defaults = {
2526
---@field buf? number
2627
---@field notif table<NotifyInstance, notify.Record>
2728
---@field super NoiceView
28-
---@field _notifs table<number, any>
2929
---@diagnostic disable-next-line: undefined-field
3030
local NotifyView = View:extend("NotifyView")
3131

@@ -52,7 +52,6 @@ end
5252
function NotifyView:init(opts)
5353
NotifyView.super.init(self, opts)
5454
self.notif = {}
55-
self._notifs = {}
5655
end
5756

5857
function NotifyView:is_available()
@@ -129,7 +128,7 @@ function NotifyView:_notify(msg)
129128
on_close = function()
130129
self.notif[instance] = nil
131130
for _, m in ipairs(msg.messages) do
132-
self._notifs[m.id] = nil
131+
m.opts.notify_id = nil
133132
end
134133
self.win = nil
135134
end,
@@ -139,14 +138,22 @@ function NotifyView:_notify(msg)
139138
if msg.opts then
140139
opts = vim.tbl_deep_extend("force", opts, msg.opts)
141140
if type(msg.opts.replace) == "table" then
142-
opts.replace = self._notifs[msg.opts.replace.id]
141+
local m = Manager.get_by_id(msg.opts.replace.id)
142+
opts.replace = m and m.opts.notify_id or nil
143143
end
144144
end
145145

146-
local id = instance.notify(msg.content, level, opts)
146+
---@type string?
147+
local content = msg.content
148+
149+
if msg.opts and msg.opts.is_nil then
150+
content = nil
151+
end
152+
153+
local id = instance.notify(content, level, opts)
147154
self.notif[instance] = id
148155
for _, m in ipairs(msg.messages) do
149-
self._notifs[m.id] = id
156+
m.opts.notify_id = id
150157
end
151158
end
152159

0 commit comments

Comments
 (0)