Skip to content

Commit

Permalink
fix(tabs): do not close if tabs are left (#371)
Browse files Browse the repository at this point in the history
## 📃 Summary

close #370

we do not account for remaining tabs when determining if we should quit
nvim or just disable the plugin
  • Loading branch information
shortcuts committed Jun 26, 2024
1 parent fe3f554 commit 22ba867
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lua/no-neck-pain/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function N.enable(scope)
callback = function(p)
vim.schedule(function()
p.event = string.format("%s:split", p.event)
if not S.hasTabs(S) or E.skip(S.getTab(S)) then
if not S.isActiveTabRegistered(S) or E.skip(S.getTab(S)) then
return D.log(p.event, "skip")
end

Expand Down Expand Up @@ -235,7 +235,11 @@ function N.enable(scope)
callback = function(p)
vim.schedule(function()
local s = string.format("%s:quit", p.event)
if E.skip(nil) or not S.isActiveTabRegistered(S) then
if
not S.isActiveTabRegistered(S)
or E.skip(nil)
or not S.isActiveTabRegistered(S)
then
return
end

Expand All @@ -249,7 +253,8 @@ function N.enable(scope)
or (S.isSideRegistered(S, "right") and not S.isSideWinValid(S, "right"))
)
or (
not _G.NoNeckPain.config.fallbackOnBufferDelete
p.event == "BufDelete"
and not _G.NoNeckPain.config.fallbackOnBufferDelete
and not S.isSideWinValid(S, "curr")
)
then
Expand Down Expand Up @@ -287,7 +292,12 @@ function N.enable(scope)
vim.api.nvim_create_autocmd({ "WinClosed", "BufDelete" }, {
callback = function(p)
vim.schedule(function()
if E.skip(nil) or not S.hasSplits(S) or W.stateWinsActive(true) then
if
not S.isActiveTabRegistered(S)
or E.skip(nil)
or not S.hasSplits(S)
or W.stateWinsActive(true)
then
return
end

Expand Down Expand Up @@ -337,7 +347,11 @@ function N.enable(scope)
callback = function(p)
vim.schedule(function()
local s = string.format("%s:integration", p.event)
if not S.hasTabs(S) or not S.isActiveTabRegistered(S) or E.skip(S.getTab(S)) then
if
not S.isActiveTabRegistered(S)
or not S.isActiveTabRegistered(S)
or E.skip(S.getTab(S))
then
return D.log(s, "skip")
end

Expand Down Expand Up @@ -375,7 +389,7 @@ function N.enable(scope)
vim.schedule(function()
p.event = string.format("%s:skipEnteringNoNeckPainBuffer", p.event)
if
not S.hasTabs(S)
not S.isActiveTabRegistered(S)
or not S.isActiveTabRegistered(S)
or E.skip()
or S.getScratchPad(S)
Expand Down Expand Up @@ -433,7 +447,7 @@ function N.disable(scope)
and not A.isRelativeWindow(win)
end, vim.api.nvim_tabpage_list_wins(activeTab))

if #wins == 0 then
if #vim.api.nvim_list_tabpages() == 1 and #wins == 0 then
for name, modified in pairs(A.getOpenedBuffers()) do
if modified then
local bufname = name
Expand Down
69 changes: 69 additions & 0 deletions tests/test_tabs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,73 @@ T["tabnew/tabclose"]["keep state synchronized on second tab"] = function()
Helpers.expect.state(child, "tabs", vim.NIL)
end

T["tabnew/tabclose"]["does not close nvim when quitting tab if some are left"] = function()
child.lua([[require('no-neck-pain').setup({width=50})]])

Helpers.expect.equality(child.api.nvim_get_current_tabpage(), 1)
Helpers.toggle(child)
Helpers.expect.equality(Helpers.winsInTab(child), { 1001, 1000, 1002 })

child.cmd("tabnew")
Helpers.expect.equality(child.api.nvim_get_current_tabpage(), 2)
Helpers.toggle(child)
Helpers.expect.equality(Helpers.winsInTab(child), { 1004, 1003, 1005 })

Helpers.expect.state(child, "enabled", true)
Helpers.expect.state(child, "tabs[1]", {
id = 1,
layers = {
split = 1,
vsplit = 1,
},
scratchPadEnabled = false,
wins = {
integrations = Co.INTEGRATIONS,
main = {
curr = 1000,
left = 1001,
right = 1002,
},
},
})
Helpers.expect.state(child, "activeTab", 2)
Helpers.expect.state(child, "tabs[2]", {
id = 2,
layers = {
split = 1,
vsplit = 1,
},
scratchPadEnabled = false,
wins = {
integrations = Co.INTEGRATIONS,
main = {
curr = 1003,
left = 1004,
right = 1005,
},
},
})

child.cmd("q")
Helpers.expect.equality(child.api.nvim_get_current_tabpage(), 1)
Helpers.expect.state(child, "activeTab", 1)
Helpers.expect.state(child, "tabs[1]", {
id = 1,
layers = {
split = 1,
vsplit = 1,
},
scratchPadEnabled = false,
wins = {
integrations = Co.INTEGRATIONS,
main = {
curr = 1000,
left = 1001,
right = 1002,
},
},
})
Helpers.expect.state(child, "tabs[2]", vim.NIL)
end

return T

0 comments on commit 22ba867

Please sign in to comment.