Skip to content

Commit

Permalink
fix(autocmds): do not force close unsaved buffers (#349)
Browse files Browse the repository at this point in the history
## 📃 Summary

we were force closing remaining windows on teardown but it's possible
that the buffer isn't yet saved, we therefore need to replicate this
behavior

closes #348
  • Loading branch information
shortcuts committed May 15, 2024
1 parent 0b32996 commit 9a529ec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
70 changes: 23 additions & 47 deletions lua/no-neck-pain/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function N.enable(scope)
and not S.isSideWinValid(S, "curr")
)
then
D.log(s, "one of the NNP side has been closed, disabling...")
D.log(s, "one of the NNP side has been closed")

return N.disable(p.event)
end
Expand All @@ -267,13 +267,15 @@ function N.enable(scope)
-- if we still have a side valid but curr has been deleted (mostly because of a :bd),
-- we will fallback to the first valid side
if p.event == "QuitPre" then
D.log(s, "one of the NNP side has been closed, disabling...")
D.log(s, "curr has been closed")

return N.disable(p.event)
end

D.log(s, "`curr` has been deleted, resetting state")

vim.cmd("new")

N.disable(string.format("%s:reset", s))
N.enable(string.format("%s:reset", s))
end)
Expand Down Expand Up @@ -425,62 +427,36 @@ function N.disable(scope)

pcall(vim.api.nvim_del_augroup_by_name, A.getAugroupName(S.activeTab))

local sides = { left = S.getSideID(S, "left"), right = S.getSideID(S, "right") }
local currID = S.getSideID(S, "curr")
local activeTab = S.activeTab

-- shutdowns gracefully by focusing the stored `curr` buffer
if
currID ~= nil
and vim.api.nvim_win_is_valid(currID)
and not S.isSideTheActiveWin(S, "curr")
then
vim.fn.win_gotoid(currID)

if _G.NoNeckPain.config.killAllBuffersOnDisable then
vim.cmd("only")
end
end

-- determine if we should quit vim or just close the window
for _, side in pairs(Co.SIDES) do
if S.isSideRegistered(S, side) then
local activeWins = vim.api.nvim_tabpage_list_wins(S.activeTab)
local haveOtherWins = false
local sideID = S.getSideID(S, side)

if S.isSideWinValid(S, side) then
S.removeNamespace(S, vim.api.nvim_win_get_buf(sideID), side)
end
if S.refreshTabs(S) == 0 then
pcall(vim.api.nvim_del_augroup_by_name, "NoNeckPainVimEnterAutocmd")

-- if we have other wins active and usable, we won't quit vim
for _, activeWin in pairs(activeWins) do
if sideID ~= activeWin and not A.isRelativeWindow(activeWin) then
haveOtherWins = true
end
end
D.log(scope, "no more active tabs left, reinitializing state")

-- we don't have any window left if we close this one
if not haveOtherWins then
-- either triggered by a :wq or quit event, we can just quit
if scope == "QuitPre" then
return vim.cmd("quit!")
end
S.init(S)
end

-- mostly triggered by :bd or similar
-- we will create a new window and close the other
vim.cmd("new")
for side, id in pairs(sides) do
if vim.api.nvim_win_is_valid(id) then
if #vim.api.nvim_tabpage_list_wins(activeTab) == 1 then
return vim.cmd("quit")
end

-- when we have more than 1 window left, we can just close it
W.close(scope, sideID, side)
S.removeNamespace(S, vim.api.nvim_win_get_buf(id), side)
W.close(scope, id, side)
end
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")
-- shutdowns gracefully by focusing the stored `curr` buffer
if currID ~= nil and vim.api.nvim_win_is_valid(currID) then
vim.fn.win_gotoid(currID)

S.init(S)
if _G.NoNeckPain.config.killAllBuffersOnDisable then
vim.cmd("only")
end
end

S.save(S)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,28 @@ T["disable"]["(multiple tab) resets state"] = function()
Helpers.expect.state(child, "tabs", vim.NIL)
end

T["disable"]["does not close the window if unsaved buffer"] = function()
child.set_size(500, 500)
child.lua([[ require('no-neck-pain').setup({width=50}) ]])
Helpers.toggle(child)

Helpers.expect.state(child, "enabled", true)
Helpers.expect.state(child, "tabs[1].wins.main", {
curr = 1000,
left = 1001,
right = 1002,
})
Helpers.expect.equality(Helpers.listBuffers(child), { 1, 2, 3 })

child.api.nvim_buf_set_lines(1, 0, 1, false, { "foo" })

Helpers.expect.equality(child.lua_get("vim.api.nvim_buf_get_option(1, 'modified')"), true)

child.cmd("quit")

Helpers.expect.error(function()
child.cmd("quit")
end)
end

return T

0 comments on commit 9a529ec

Please sign in to comment.