fix(lsp): delete bufvar inside WinClosed event

Problem: floaing preview window can be closed by some
ex commands like `only` `fclose` which will not clean the bufvar

Solution: use WinClosed event with floating_winnr for clean
bufnr, and add test cases for vim.lsp.util.open_floating_preview
This commit is contained in:
glepnir
2024-11-23 19:11:30 +08:00
committed by Lewis Russell
parent d9b08c58c3
commit 2a1f604c77
2 changed files with 50 additions and 5 deletions

View File

@@ -5,6 +5,8 @@ local Screen = require('test.functional.ui.screen')
local feed = n.feed
local eq = t.eq
local exec_lua = n.exec_lua
local command, api = n.command, n.api
local pcall_err = t.pcall_err
describe('vim.lsp.util', function()
before_each(n.clear)
@@ -265,6 +267,38 @@ describe('vim.lsp.util', function()
eq(56, opts.height)
end)
describe('vim.lsp.util.open_floating_preview', function()
local var_name = 'lsp_floating_preview'
local curbuf = api.nvim_get_current_buf()
it('clean bufvar after fclose', function()
exec_lua(function()
vim.lsp.util.open_floating_preview({ 'test' }, '', { height = 5, width = 2 })
end)
eq(true, api.nvim_win_is_valid(api.nvim_buf_get_var(curbuf, var_name)))
command('fclose')
eq(
'Key not found: lsp_floating_preview',
pcall_err(api.nvim_buf_get_var, curbuf, var_name)
)
end)
it('clean bufvar after CursorMoved', function()
local result = exec_lua(function()
vim.lsp.util.open_floating_preview({ 'test' }, '', { height = 5, width = 2 })
local winnr = vim.b[vim.api.nvim_get_current_buf()].lsp_floating_preview
local result = vim.api.nvim_win_is_valid(winnr)
vim.api.nvim_feedkeys(vim.keycode('G'), 'txn', false)
return result
end)
eq(true, result)
eq(
'Key not found: lsp_floating_preview',
pcall_err(api.nvim_buf_get_var, curbuf, var_name)
)
end)
end)
end)
end)
end)