Skip to content

Commit

Permalink
feat: always use the global notify instance and set animate=false whe…
Browse files Browse the repository at this point in the history
…n blocking
  • Loading branch information
folke committed Nov 8, 2022
1 parent 40b29cb commit edc8df6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 58 deletions.
27 changes: 0 additions & 27 deletions lua/noice/util/hacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function M.enable()
M.reset_augroup()
M.fix_incsearch()
M.fix_input()
M.fix_notify()
M.fix_nohlsearch()
M.fix_redraw()
M.fix_cmp()
Expand Down Expand Up @@ -191,32 +190,6 @@ function M.fix_input()
end)
end

-- Allow nvim-notify to behave inside instant events
function M.fix_notify()
vim.schedule(Util.protect(function()
if pcall(_G.require, "notify") then
local NotifyService = require("notify.service")
---@type NotificationService
local meta = getmetatable(NotifyService(require("notify")._config()))
local push = meta.push
meta.push = function(self, notif)
---@type buffer
local buf = push(self, notif)

-- run animator and re-render instantly when inside instant events
if Util.is_blocking() then
pcall(self._animator.render, self._animator, self._pending, 1 / self._fps)
self._buffers[notif.id]:render()
end
return buf
end
table.insert(M._disable, function()
meta.push = push
end)
end
end))
end

-- Fixes cmp cmdline position
function M.fix_cmp()
if not Util.module_exists("cmp.utils.api") then
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ end
---@param ... any
function M.notify(msg, level, ...)
if M.module_exists("notify") then
require("noice.view.backend.notify").instance().notify(msg:format(...), level, {
require("notify").notify(msg:format(...), level, {
title = "noice.nvim",
on_open = function(win)
vim.api.nvim_win_set_option(win, "conceallevel", 3)
Expand Down
38 changes: 8 additions & 30 deletions lua/noice/view/backend/notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,13 @@ local defaults = {
---@class NotifyView: NoiceView
---@field win? number
---@field buf? number
---@field notif table<NotifyInstance, notify.Record>
---@field notif notify.Record
---@field super NoiceView
---@diagnostic disable-next-line: undefined-field
local NotifyView = View:extend("NotifyView")

---@return NotifyInstance
function NotifyView.instance()
if Util.is_blocking() then
if not NotifyView._instant_notify then
NotifyView._instant_notify = require("notify").instance({
stages = "static",
}, true)
end
return NotifyView._instant_notify
end
return require("notify")
end

function NotifyView.dismiss()
require("notify").dismiss({ pending = true, silent = true })
if NotifyView._instant_notify then
NotifyView._instant_notify.dismiss({ pending = true, silent = true })
end
end

function NotifyView:init(opts)
NotifyView.super.init(self, opts)
self.notif = {}
end

function NotifyView:is_available()
Expand All @@ -71,7 +50,7 @@ function NotifyView:plain()
end

---@param config notify.Config
---@param render? notify.RenderFun
---@param render? notify.RenderFun|string
---@return notify.RenderFun
function NotifyView:get_render(config, render)
---@type string|notify.RenderFun
Expand All @@ -88,7 +67,7 @@ function NotifyView:get_render(config, render)
end

---@param messages NoiceMessage[]
---@param render? notify.RenderFun
---@param render? notify.RenderFun|string
---@param content? string
function NotifyView:notify_render(messages, render, content)
---@param config notify.Config
Expand Down Expand Up @@ -133,12 +112,11 @@ end
function NotifyView:_notify(msg)
local level = self._opts.level or msg.level

local instance = NotifyView.instance()

local opts = {
title = msg.title or self._opts.title,
animate = not Util.is_blocking(),
timeout = self._opts.timeout,
replace = self._opts.replace and self.notif[instance],
replace = self._opts.replace and self.notif,
keep = function()
return Util.is_blocking()
end,
Expand All @@ -149,7 +127,7 @@ function NotifyView:_notify(msg)
end
end,
on_close = function()
self.notif[instance] = nil
self.notif = nil
for _, m in ipairs(msg.messages) do
m.opts.notify_id = nil
end
Expand All @@ -173,8 +151,8 @@ function NotifyView:_notify(msg)
content = nil
end

local id = instance.notify(content, level, opts)
self.notif[instance] = id
local id = require("notify")(content, level, opts)
self.notif = id
for _, m in ipairs(msg.messages) do
m.opts.notify_id = id
end
Expand Down

0 comments on commit edc8df6

Please sign in to comment.