Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autocmds): clear after debounce #326

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lua/no-neck-pain/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function NoNeckPain.setup(opts)
or _G.NoNeckPain.config.autocmds.reloadOnColorSchemeChange
then
vim.api.nvim_create_augroup("NoNeckPainAutocmd", { clear = true })
vim.api.nvim_create_augroup("NoNeckPainVimEnterAutocmd", { clear = true })
end

if _G.NoNeckPain.config.autocmds.reloadOnColorSchemeChange then
Expand All @@ -108,17 +109,19 @@ function NoNeckPain.setup(opts)
if _G.NoNeckPain.config.autocmds.enableOnVimEnter then
vim.api.nvim_create_autocmd({ "BufEnter" }, {
pattern = "*",
callback = function(p)
callback = function()
vim.schedule(function()
if _G.NoNeckPain.state ~= nil and _G.NoNeckPain.state.enabled then
return
end

NoNeckPain.enable()

if _G.NoNeckPain.state ~= nil then
vim.api.nvim_del_autocmd(p.id)
end
A.debounce("enableOnVimEnter", function()
if _G.NoNeckPain.state ~= nil then
pcall(vim.api.nvim_del_augroup_by_name, "NoNeckPainVimEnterAutocmd")
end
end, 20)
end)
end,
group = "NoNeckPainAutocmd",
Expand Down
9 changes: 8 additions & 1 deletion lua/no-neck-pain/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,12 @@ function N.enable(scope)
callback = function(p)
vim.schedule(function()
p.event = string.format("%s:skipEnteringNoNeckPainBuffer", p.event)
if not S.hasTabs(S) or not S.isActiveTabRegistered(S) or E.skip() or S.hasScratchPadEnabled(S) then
if
not S.hasTabs(S)
or not S.isActiveTabRegistered(S)
or E.skip()
or S.hasScratchPadEnabled(S)
then
return D.log(p.event, "skip")
end

Expand Down Expand Up @@ -458,6 +463,8 @@ function N.disable(scope)
end

if S.refreshTabs(S) == 0 then
pcall(vim.api.nvim_del_augroup_by_name, "NoNeckPainVimEnterAutocmd")

D.log(scope, "no more active tabs left, reinitializing state")

S.init(S)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ T["auto command"]["does not create side buffers window's width < options.width"]
})
end

T["auto command"]["disabling clears VimEnter autocmd"] = function()
child.restart({ "-u", "scripts/init_auto_open.lua" })
Helpers.toggle(child)

Helpers.expect.equality(
child.api.nvim_get_autocmds({ group = "NoNeckPainVimEnterAutocmd" }),
{}
)
end

T["auto command"]["does not shift when opening/closing float window"] = function()
child.set_size(5, 200)
child.lua([[ require('no-neck-pain').setup({width=50}) ]])
Expand Down