Skip to content

Commit

Permalink
fix: vim.schedule QuitPre event callback
Browse files Browse the repository at this point in the history
so that callback attached by consumer gets
the chance to run first
  • Loading branch information
MunifTanjim committed Feb 1, 2023
1 parent b99e6cb commit d147222
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lua/nui/layout/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ local function wire_up_layout_components(layout, box)
autocmd.create({ "BufWipeout", "QuitPre" }, {
group = layout._.augroup.unmount,
buffer = child.component.bufnr,
callback = function()
callback = vim.schedule_wrap(function()
layout:unmount()
end,
end),
}, child.component.bufnr)

autocmd.create("BufWinEnter", {
Expand Down
4 changes: 2 additions & 2 deletions lua/nui/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ function Popup:mount()
autocmd.create("QuitPre", {
group = self._.augroup.unmount,
buffer = self.bufnr,
callback = function()
callback = vim.schedule_wrap(function()
self:unmount()
end,
end),
}, self.bufnr)
autocmd.create("BufWinEnter", {
group = self._.augroup.unmount,
Expand Down
6 changes: 4 additions & 2 deletions lua/nui/split/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ function Split:mount()
buffer = self.bufnr,
callback = function()
self._.pending_quit = true
self:unmount()
self._.pending_quit = nil
vim.schedule(function()
self:unmount()
self._.pending_quit = nil
end)
end,
}, self.bufnr)
autocmd.create("BufWinEnter", {
Expand Down
16 changes: 16 additions & 0 deletions tests/nui/layout/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ describe("nui.layout", function()

p2:unmount()

vim.wait(100, function()
return not layout._.mounted
end, 10)

assert.spy(layout_unmount).was_called()
end)

Expand All @@ -547,6 +551,10 @@ describe("nui.layout", function()
vim.cmd([[quit]])
end)

vim.wait(100, function()
return not layout._.mounted
end, 10)

assert.spy(layout_unmount).was_called()
end)
end)
Expand Down Expand Up @@ -1479,6 +1487,10 @@ describe("nui.layout", function()

s2:unmount()

vim.wait(100, function()
return not layout._.mounted
end, 10)

assert.spy(layout_unmount).was_called()
end)

Expand Down Expand Up @@ -1509,6 +1521,10 @@ describe("nui.layout", function()
vim.cmd([[quit]])
end)

vim.wait(100, function()
return not layout._.mounted
end, 10)

assert.spy(layout_unmount).was_called()
end)
end)
Expand Down
4 changes: 4 additions & 0 deletions tests/nui/popup/init_spec.lua