fix(api): nvim_create_buf assert fails if autocmds set &swapfile

Problem: assertion failure in nvim_create_buf if buflist_new autocommands open
a swapfile when "scratch" is set.

Solution: block autocommands when setting up the buffer; fire them later
instead.

Note that, unlike buflist_new, I don't check if autocommands aborted script
processing; the buffer is already created and configured at that point, so might
as well return the handle anyway.

Rather than repeat try_{start,end} and {un}block_autocmds for each relevant
operation, just do it at the start and near the end. This means that, if
TermResponse fires from unblock_autocmds for whatever reason, it can see the
buffer in an already configured state if we didn't bail due to an error (plus
it's probably a bit cleaner this way).
This commit is contained in:
Sean Dewar
2024-03-19 12:59:44 +00:00
parent d5c23d72a5
commit 6091df6b7a
2 changed files with 66 additions and 11 deletions

View File

@@ -3144,6 +3144,47 @@ describe('API', function()
api.nvim_create_buf(false, true)
eq(1, eval('g:loaded'))
end)
it('creating scratch buffer where autocommands set &swapfile works', function()
exec([[
autocmd BufNew * ++once execute expand("<abuf>") "buffer"
\| file foobar
\| setlocal swapfile
]])
local new_buf = api.nvim_create_buf(false, true)
neq('', fn.swapname(new_buf))
end)
it('fires expected autocommands', function()
exec([=[
" Append the &buftype to check autocommands trigger *after* the buffer was configured to be
" scratch, if applicable.
autocmd BufNew * let fired += [["BufNew", expand("<abuf>")->str2nr(),
\ getbufvar(expand("<abuf>")->str2nr(), "&buftype")]]
autocmd BufAdd * let fired += [["BufAdd", expand("<abuf>")->str2nr(),
\ getbufvar(expand("<abuf>")->str2nr(), "&buftype")]]
" Don't want to see OptionSet; buffer options set from passing true for "scratch", etc.
" should be configured invisibly, and before autocommands.
autocmd OptionSet * let fired += [["OptionSet", expand("<amatch>")]]
let fired = []
]=])
local new_buf = api.nvim_create_buf(false, false)
eq({ { 'BufNew', new_buf, '' } }, eval('g:fired'))
command('let fired = []')
new_buf = api.nvim_create_buf(false, true)
eq({ { 'BufNew', new_buf, 'nofile' } }, eval('g:fired'))
command('let fired = []')
new_buf = api.nvim_create_buf(true, false)
eq({ { 'BufNew', new_buf, '' }, { 'BufAdd', new_buf, '' } }, eval('g:fired'))
command('let fired = []')
new_buf = api.nvim_create_buf(true, true)
eq({ { 'BufNew', new_buf, 'nofile' }, { 'BufAdd', new_buf, 'nofile' } }, eval('g:fired'))
end)
end)
describe('nvim_get_runtime_file', function()