Skip to content

Commit

Permalink
fix(notify): update refresh() to work inside fast events
Browse files Browse the repository at this point in the history
Resolve #1341
  • Loading branch information
echasnovski committed Nov 18, 2024
1 parent b07157c commit 6714e73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions doc/mini-notify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ Make notification window show relevant data:
update its message.
- Construct content from notifications and show them in a window.

Note: effects are delayed if inside fast event (|vim.in_fast_event()|).

------------------------------------------------------------------------------
*MiniNotify.get()*
`MiniNotify.get`({id})
Expand Down
10 changes: 4 additions & 6 deletions lua/mini/notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ MiniNotify.make_notify = function(opts)
if type(val.hl_group) ~= 'string' then H.error('`hl_group` in level data should be string.') end
end

-- Use `vim.schedule_wrap` for output to be usable inside `vim.uv` callbacks
local notify = function(msg, level)
return function(msg, level)
level = level or vim.log.levels.INFO
local level_name = level_names[level]
if level_name == nil then H.error('Only valid values of `vim.log.levels` are supported.') end
Expand All @@ -310,10 +309,6 @@ MiniNotify.make_notify = function(opts)
local id = MiniNotify.add(msg, level_name, level_data.hl_group)
vim.defer_fn(function() MiniNotify.remove(id) end, level_data.duration)
end
return function(msg, level)
if not vim.in_fast_event() then return notify(msg, level) end
vim.schedule(function() notify(msg, level) end)
end
end

--- Add notification
Expand Down Expand Up @@ -417,7 +412,10 @@ end
--- - Apply `config.content.format` to each element of notification array and
--- update its message.
--- - Construct content from notifications and show them in a window.
---
--- Note: effects are delayed if inside fast event (|vim.in_fast_event()|).
MiniNotify.refresh = function()
if vim.in_fast_event() then return vim.schedule(MiniNotify.refresh) end
if H.is_disabled() or type(vim.v.exiting) == 'number' then return H.window_close() end

-- Prepare array of active notifications
Expand Down
13 changes: 12 additions & 1 deletion tests/test_notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ T['make_notify()']['validates arguments'] = function()
validate_level('OFF')
end

T['make_notify()']['has output working in `libuv` callbacks'] = function()
T['make_notify()']['has output working in fast event'] = function()
child.lua('_G.dur = ' .. small_time)
child.lua([[
vim.notify = MiniNotify.make_notify()
Expand Down Expand Up @@ -519,6 +519,17 @@ T['refresh()']['handles manual buffer/window delete'] = function()
eq(is_notif_window_shown(), true)
end

T['refresh()']['can be used inside fast event'] = function()
add('Hello')
child.lua('_G.dur = ' .. small_time)
child.lua([[
local timer = vim.loop.new_timer()
timer:start(_G.dur, 0, function() MiniNotify.refresh() end)
]])
sleep(small_time + small_time)
eq(child.cmd_capture('messages'), '')
end

T['refresh()']['respects `vim.{g,b}.mininotify_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } } }, {
test = function(var_type)
mock_gettimeofday()
Expand Down

0 comments on commit 6714e73

Please sign in to comment.