refactor(win_close): remove "force", don't pass on "free_buf" (#21921)

Problem:
The "force" flag of win_close() complicates the code and adds edge cases
where it is not clear what the correct behavior should be.
The "free_buf" flag of win_close() is passed on to float windows when
closing the last window of a tabpage, which doesn't make much sense.

Solution:
Remove the "force" flag and always close float windows as if :close! is
used when closing the last window of a tabpage, and set the "free_buf"
flag for a float window based on whether its buffer can be freed.
As 'hidden' is on by default, this change shouldn't affect many people.
This commit is contained in:
zeertzjq
2023-01-23 18:55:11 +08:00
committed by GitHub
parent d58bf4ff30
commit 0371d0f7af
11 changed files with 59 additions and 79 deletions

View File

@@ -462,6 +462,9 @@ describe('float window', function()
end)
end)
describe("deleting the last non-floating window's buffer", function()
after_each(function()
eq(false, meths.buf_is_valid(old_buf))
end)
describe('leaves one window with an empty buffer when there is only one buffer', function()
local same_buf_float
before_each(function()
@@ -552,6 +555,9 @@ describe('float window', function()
end)
end)
describe('with splits, deleting the last listed buffer creates an empty buffer', function()
after_each(function()
eq(false, meths.buf_is_valid(old_buf))
end)
describe('when a non-floating window has an unlisted buffer', function()
local same_buf_float
before_each(function()
@@ -597,6 +603,7 @@ describe('float window', function()
same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
eq(false, meths.buf_is_valid(old_buf))
expect('')
eq(2, #meths.list_wins())
eq(2, #meths.list_tabpages())
@@ -629,6 +636,7 @@ describe('float window', function()
same_buf_float = meths.open_win(old_buf, false, float_opts).id
end)
after_each(function()
eq(false, meths.buf_is_valid(old_buf))
expect('')
eq(3, #meths.list_wins())
eq(2, #meths.list_tabpages())
@@ -656,12 +664,18 @@ describe('float window', function()
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
describe('closes the tabpage force-closing floating windows', function()
local same_buf_float, other_buf, other_buf_float
before_each(function()
command('set nohidden')
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('foo')
meths.set_current_win(old_win)
end)
after_each(function()
eq(true, meths.buf_is_valid(other_buf))
eq(old_tabpage, curtab().id)
expect('oldtab')
eq(1, #meths.list_tabpages())
@@ -669,41 +683,30 @@ describe('float window', function()
it('if called from non-floating window', function()
meths.win_close(old_win, false)
end)
it('if called from floating window', function()
it('if called from floating window with the same buffer', 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).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()
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()
it('if called from floating window with another buffer', 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))
meths.win_close(old_win, false)
end)
end)
end)
describe("deleting the last non-floating window's buffer", function()
describe('closes the tabpage when all floating windows are closeable', function()
describe('closes the tabpage force-closing floating windows', function()
local same_buf_float, other_buf, other_buf_float
before_each(function()
command('set nohidden')
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('foo')
meths.set_current_win(old_win)
end)
after_each(function()
eq(false, meths.buf_is_valid(old_buf))
eq(true, meths.buf_is_valid(other_buf))
eq(old_tabpage, curtab().id)
expect('oldtab')
eq(1, #meths.list_tabpages())
@@ -721,7 +724,6 @@ describe('float window', function()
meths.buf_delete(old_buf, {force = false})
end)
end)
-- TODO: what to do when there are non-closeable floating windows?
end)
end)