fix(lsp): add nvim_ prefix in augroup name

Problem: internal augroup need nvim_ prefix and test case postion inside wrong function block

Solution: add nvim_ prefix and move the test case to a single block
This commit is contained in:
glepnir 2024-11-26 14:01:39 +08:00
parent 66bb1e577c
commit 4bda502014
2 changed files with 35 additions and 37 deletions

View File

@ -1620,18 +1620,16 @@ function M.open_floating_preview(contents, syntax, opts)
api.nvim_buf_set_var(bufnr, 'lsp_floating_preview', floating_winnr) api.nvim_buf_set_var(bufnr, 'lsp_floating_preview', floating_winnr)
end end
local augroup_name = ('closing_floating_preview_%d'):format(floating_winnr) local augroup_name = 'nvim_closing_floating_preview'
local ok = if vim.fn.exists(('#%s'):format(augroup_name)) == 0 then
pcall(api.nvim_get_autocmds, { group = augroup_name, pattern = tostring(floating_winnr) })
if not ok then
api.nvim_create_autocmd('WinClosed', { api.nvim_create_autocmd('WinClosed', {
group = api.nvim_create_augroup(augroup_name, {}), group = api.nvim_create_augroup(augroup_name, { clear = true }),
pattern = tostring(floating_winnr), callback = function(args)
callback = function() if
if api.nvim_buf_is_valid(bufnr) then tonumber(args.match) == vim.b[bufnr].lsp_floating_preview and api.nvim_buf_is_valid(bufnr)
then
vim.b[bufnr].lsp_floating_preview = nil vim.b[bufnr].lsp_floating_preview = nil
end end
api.nvim_del_augroup_by_name(augroup_name)
end, end,
}) })
end end

View File

@ -267,8 +267,16 @@ describe('vim.lsp.util', function()
eq(56, opts.height) eq(56, opts.height)
end) end)
end)
end)
describe('open_floating_preview', function()
before_each(function()
n.clear()
Screen.new(10, 10)
feed('9i<CR><Esc>G4k')
end)
describe('vim.lsp.util.open_floating_preview', function()
local var_name = 'lsp_floating_preview' local var_name = 'lsp_floating_preview'
local curbuf = api.nvim_get_current_buf() local curbuf = api.nvim_get_current_buf()
@ -278,10 +286,7 @@ describe('vim.lsp.util', function()
end) end)
eq(true, api.nvim_win_is_valid(api.nvim_buf_get_var(curbuf, var_name))) eq(true, api.nvim_win_is_valid(api.nvim_buf_get_var(curbuf, var_name)))
command('fclose') command('fclose')
eq( eq('Key not found: lsp_floating_preview', pcall_err(api.nvim_buf_get_var, curbuf, var_name))
'Key not found: lsp_floating_preview',
pcall_err(api.nvim_buf_get_var, curbuf, var_name)
)
end) end)
it('clean bufvar after CursorMoved', function() it('clean bufvar after CursorMoved', function()
@ -293,12 +298,7 @@ describe('vim.lsp.util', function()
return result return result
end) end)
eq(true, result) eq(true, result)
eq( eq('Key not found: lsp_floating_preview', pcall_err(api.nvim_buf_get_var, curbuf, var_name))
'Key not found: lsp_floating_preview',
pcall_err(api.nvim_buf_get_var, curbuf, var_name)
)
end)
end)
end) end)
end) end)
end) end)