fix(api): nvim_get_option_value does not clean up on FileType error #31219

Problem:
If there's an error in `FileType` autocmd, the filetype get-opt buffer
doesn't get cleaned up.

Solution:
Call `aucmd_restbuf`.
This commit is contained in:
altermo
2024-11-18 15:40:51 +01:00
committed by GitHub
parent 40347f6e27
commit e2ad251c8d
2 changed files with 20 additions and 0 deletions

View File

@@ -165,6 +165,14 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
buf_T *ftbuf = do_ft_buf(filetype, &aco, err); buf_T *ftbuf = do_ft_buf(filetype, &aco, err);
if (ERROR_SET(err)) { if (ERROR_SET(err)) {
if (ftbuf != NULL) {
// restore curwin/curbuf and a few other things
aucmd_restbuf(&aco);
assert(curbuf != ftbuf); // safety check
wipe_buffer(ftbuf, false);
}
return (Object)OBJECT_INIT; return (Object)OBJECT_INIT;
} }

View File

@@ -161,6 +161,18 @@ describe('vim.filetype', function()
end end
end end
end) end)
it('.get_option() cleans up buffer on error', function()
api.nvim_create_autocmd('FileType', { pattern = 'foo', command = 'lua error()' })
local buf = api.nvim_get_current_buf()
exec_lua(function()
pcall(vim.filetype.get_option, 'foo', 'lisp')
end)
eq(buf, api.nvim_get_current_buf())
end)
end) end)
describe('filetype.lua', function() describe('filetype.lua', function()