aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-03-24 14:53:20 +0800
committerGitHub <noreply@github.com>2022-03-24 14:53:20 +0800
commita72f338d76c871869712518df862c85d1df25f54 (patch)
treedca9871bc17e0315e94f4302f0409101c3c11228
parentff82b2785f161fc14ff6bd8eae497f37ecd14564 (diff)
downloadrneovim-a72f338d76c871869712518df862c85d1df25f54.tar.gz
rneovim-a72f338d76c871869712518df862c85d1df25f54.tar.bz2
rneovim-a72f338d76c871869712518df862c85d1df25f54.zip
fix(float): do not switch window before deleting last listed buffer (#17840)
Just allow close_windows() to close the current window instead. This fixes wrong working directory or autocommands not being triggered.
-rw-r--r--src/nvim/buffer.c18
-rw-r--r--test/functional/ui/float_spec.lua109
2 files changed, 74 insertions, 53 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
index 7fc880fb41..f200f16a5f 100644
--- a/src/nvim/buffer.c
+++ b/src/nvim/buffer.c
@@ -1042,24 +1042,24 @@ static int empty_curbuf(int close_others, int forceit, int action)
set_bufref(&bufref, buf);
if (close_others) {
+ bool can_close_all_others = true;
if (curwin->w_floating) {
- bool can_close_all_others = false;
+ // Closing all other windows with this buffer may leave only floating windows.
+ can_close_all_others = false;
for (win_T *wp = firstwin; !wp->w_floating; wp = wp->w_next) {
if (wp->w_buffer != curbuf) {
// Found another non-floating window with a different (probably unlisted) buffer.
- // Closing all other windows with the this buffer is fine in this case.
+ // Closing all other windows with this buffer is fine in this case.
can_close_all_others = true;
break;
}
}
- if (!can_close_all_others) {
- // Closing all other windows with this buffer will close all non-floating windows.
- // Move to a non-floating window then.
- curwin = firstwin;
- }
}
- // Close any other windows on this buffer, then make it empty.
- close_windows(buf, true);
+ // If it is fine to close all other windows with this buffer, keep the current window and
+ // close any other windows with this buffer, then make it empty.
+ // Otherwise close_windows() will refuse to close the last non-floating window, so allow it
+ // to close the current window instead.
+ close_windows(buf, can_close_all_others);
}
setpcmark();
diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua
index 886bfa2147..359d1f206a 100644
--- a/test/functional/ui/float_spec.lua
+++ b/test/functional/ui/float_spec.lua
@@ -17,6 +17,7 @@ local funcs = helpers.funcs
local run = helpers.run
local pcall_err = helpers.pcall_err
local tbl_contains = global_helpers.tbl_contains
+local curbuf, curwin, curtab = helpers.curbuf, helpers.curwin, helpers.curtab
describe('float window', function()
before_each(function()
@@ -423,8 +424,8 @@ describe('float window', function()
local old_buf, old_win
before_each(function()
insert('foo')
- old_buf = meths.get_current_buf()
- old_win = meths.get_current_win()
+ old_buf = curbuf().id
+ old_win = curwin().id
end)
describe('closing the last non-floating window gives E444', function()
before_each(function()
@@ -442,44 +443,53 @@ describe('float window', function()
end)
describe("deleting the last non-floating window's buffer", function()
describe('leaves one window with an empty buffer when there is only one buffer', function()
+ local same_buf_float
before_each(function()
- meths.open_win(old_buf, true, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
- eq(old_win, meths.get_current_win())
+ eq(old_win, curwin().id)
expect('')
eq(1, #meths.list_wins())
end)
it('if called from non-floating window', function()
- meths.set_current_win(old_win)
meths.buf_delete(old_buf, {force = true})
end)
it('if called from floating window', function()
+ meths.set_current_win(same_buf_float)
+ command('autocmd WinLeave * let g:win_leave = nvim_get_current_win()')
+ command('autocmd WinEnter * let g:win_enter = nvim_get_current_win()')
meths.buf_delete(old_buf, {force = true})
+ eq(same_buf_float, eval('g:win_leave'))
+ eq(old_win, eval('g:win_enter'))
end)
end)
describe('closes other windows with that buffer when there are other buffers', function()
local same_buf_float, other_buf, other_buf_float
before_each(function()
- same_buf_float = meths.open_win(old_buf, false, float_opts)
- other_buf = meths.create_buf(true, false)
- other_buf_float = meths.open_win(other_buf, true, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
+ other_buf = meths.create_buf(true, false).id
+ other_buf_float = meths.open_win(other_buf, true, float_opts).id
insert('bar')
meths.set_current_win(old_win)
end)
after_each(function()
- eq(other_buf, meths.get_current_buf())
+ eq(other_buf, curbuf().id)
expect('bar')
eq(2, #meths.list_wins())
end)
it('if called from non-floating window', function()
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(old_win, curwin().id)
end)
it('if called from floating window with the same buffer', function()
meths.set_current_win(same_buf_float)
+ command('autocmd WinLeave * let g:win_leave = nvim_get_current_win()')
+ command('autocmd WinEnter * let g:win_enter = nvim_get_current_win()')
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(same_buf_float, eval('g:win_leave'))
+ eq(old_win, eval('g:win_enter'))
+ eq(old_win, curwin().id)
end)
-- TODO: this case is too hard to deal with
pending('if called from floating window with another buffer', function()
@@ -490,9 +500,9 @@ describe('float window', function()
describe('creates an empty buffer when there is only one listed buffer', function()
local same_buf_float, unlisted_buf_float
before_each(function()
- same_buf_float = meths.open_win(old_buf, false, float_opts)
- local unlisted_buf = meths.create_buf(true, false)
- unlisted_buf_float = meths.open_win(unlisted_buf, true, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
+ local unlisted_buf = meths.create_buf(true, false).id
+ unlisted_buf_float = meths.open_win(unlisted_buf, true, float_opts).id
insert('unlisted')
command('set nobuflisted')
meths.set_current_win(old_win)
@@ -503,12 +513,16 @@ describe('float window', function()
end)
it('if called from non-floating window', function()
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(old_win, curwin().id)
end)
it('if called from floating window with the same buffer', function()
meths.set_current_win(same_buf_float)
+ command('autocmd WinLeave * let g:win_leave = nvim_get_current_win()')
+ command('autocmd WinEnter * let g:win_enter = nvim_get_current_win()')
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(same_buf_float, eval('g:win_leave'))
+ eq(old_win, eval('g:win_enter'))
+ eq(old_win, curwin().id)
end)
-- TODO: this case is too hard to deal with
pending('if called from floating window with an unlisted buffer', function()
@@ -525,7 +539,7 @@ describe('float window', function()
insert('unlisted')
command('set nobuflisted')
meths.set_current_win(old_win)
- same_buf_float = meths.open_win(old_buf, false, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
expect('')
@@ -533,12 +547,12 @@ describe('float window', function()
end)
it('if called from non-floating window with the deleted buffer', function()
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(old_win, curwin().id)
end)
it('if called from floating window with the deleted buffer', function()
meths.set_current_win(same_buf_float)
meths.buf_delete(old_buf, {force = true})
- eq(same_buf_float, meths.get_current_win())
+ eq(same_buf_float, curwin().id)
end)
end)
end)
@@ -550,17 +564,17 @@ describe('float window', function()
before_each(function()
insert('unlisted')
command('set nobuflisted')
- unlisted_buf = meths.get_current_buf()
+ unlisted_buf = curbuf().id
command('tabnew')
insert('foo')
- old_buf = meths.get_current_buf()
- old_win = meths.get_current_win()
+ old_buf = curbuf().id
+ old_win = curwin().id
end)
describe('without splits, deleting the last listed buffer creates an empty buffer', function()
local same_buf_float
before_each(function()
meths.set_current_win(old_win)
- same_buf_float = meths.open_win(old_buf, false, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
expect('')
@@ -569,12 +583,16 @@ describe('float window', function()
end)
it('if called from non-floating window', function()
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(old_win, curwin().id)
end)
it('if called from floating window with the same buffer', function()
meths.set_current_win(same_buf_float)
+ command('autocmd WinLeave * let g:win_leave = nvim_get_current_win()')
+ command('autocmd WinEnter * let g:win_enter = nvim_get_current_win()')
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(same_buf_float, eval('g:win_leave'))
+ eq(old_win, eval('g:win_enter'))
+ eq(old_win, curwin().id)
end)
end)
describe('with splits, deleting the last listed buffer creates an empty buffer', function()
@@ -583,7 +601,7 @@ describe('float window', function()
command('botright vsplit')
meths.set_current_buf(unlisted_buf)
meths.set_current_win(old_win)
- same_buf_float = meths.open_win(old_buf, false, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
expect('')
@@ -592,12 +610,12 @@ describe('float window', function()
end)
it('if called from non-floating window with the deleted buffer', function()
meths.buf_delete(old_buf, {force = true})
- eq(old_win, meths.get_current_win())
+ eq(old_win, curwin().id)
end)
it('if called from floating window with the deleted buffer', function()
meths.set_current_win(same_buf_float)
meths.buf_delete(old_buf, {force = true})
- eq(same_buf_float, meths.get_current_win())
+ eq(same_buf_float, curwin().id)
end)
end)
end)
@@ -606,43 +624,46 @@ describe('float window', function()
local float_opts = {relative = 'editor', row = 1, col = 1, width = 1, height = 1}
local old_tabpage, old_buf, old_win
before_each(function()
- old_tabpage = meths.get_current_tabpage()
+ old_tabpage = curtab().id
insert('oldtab')
command('tabnew')
- old_buf = meths.get_current_buf()
- old_win = meths.get_current_win()
+ old_buf = curbuf().id
+ old_win = curwin().id
end)
describe('closing the last non-floating window', function()
describe('closes the tabpage when all floating windows are closeable', function()
+ local same_buf_float
before_each(function()
- meths.open_win(old_buf, true, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
- eq(old_tabpage, meths.get_current_tabpage())
+ eq(old_tabpage, curtab().id)
expect('oldtab')
eq(1, #meths.list_tabpages())
end)
it('if called from non-floating window', function()
- meths.set_current_win(old_win)
meths.win_close(old_win, false)
end)
it('if called from floating window', function()
+ meths.set_current_win(same_buf_float)
meths.win_close(old_win, false)
end)
end)
describe('gives E5601 when there are non-closeable floating windows', function()
+ local other_buf_float
before_each(function()
command('set nohidden')
- local other_buf = meths.create_buf(true, false)
- meths.open_win(other_buf, true, float_opts)
+ local other_buf = meths.create_buf(true, false).id
+ other_buf_float = meths.open_win(other_buf, true, float_opts).id
insert('foo')
+ meths.set_current_win(old_win)
end)
it('if called from non-floating window', function()
- meths.set_current_win(old_win)
eq('Vim:E5601: Cannot close window, only floating window would remain',
pcall_err(meths.win_close, old_win, false))
end)
it('if called from floating window', function()
+ meths.set_current_win(other_buf_float)
eq('Vim:E5601: Cannot close window, only floating window would remain',
pcall_err(meths.win_close, old_win, false))
end)
@@ -650,15 +671,15 @@ describe('float window', function()
end)
describe("deleting the last non-floating window's buffer", function()
describe('closes the tabpage when all floating windows are closeable', function()
- local same_buf_win, other_buf, other_buf_win
+ local same_buf_float, other_buf, other_buf_float
before_each(function()
- same_buf_win = meths.open_win(old_buf, false, float_opts)
- other_buf = meths.create_buf(true, false)
- other_buf_win = meths.open_win(other_buf, true, float_opts)
+ same_buf_float = meths.open_win(old_buf, false, float_opts).id
+ other_buf = meths.create_buf(true, false).id
+ other_buf_float = meths.open_win(other_buf, true, float_opts).id
meths.set_current_win(old_win)
end)
after_each(function()
- eq(old_tabpage, meths.get_current_tabpage())
+ eq(old_tabpage, curtab().id)
expect('oldtab')
eq(1, #meths.list_tabpages())
end)
@@ -666,12 +687,12 @@ describe('float window', function()
meths.buf_delete(old_buf, {force = false})
end)
it('if called from floating window with the same buffer', function()
- meths.set_current_win(same_buf_win)
+ meths.set_current_win(same_buf_float)
meths.buf_delete(old_buf, {force = false})
end)
-- TODO: this case is too hard to deal with
pending('if called from floating window with another buffer', function()
- meths.set_current_win(other_buf_win)
+ meths.set_current_win(other_buf_float)
meths.buf_delete(old_buf, {force = false})
end)
end)