fix(lsp): show notification with empty hover response (#35014)

This commit is contained in:
Maria José Solano
2025-07-20 13:58:40 -07:00
committed by GitHub
parent 2495015455
commit c3991b8ef4
2 changed files with 43 additions and 3 deletions

View File

@@ -61,19 +61,32 @@ function M.hover(config)
-- Filter errors from results
local results1 = {} --- @type table<integer,lsp.Hover>
local empty_response = false
for client_id, resp in pairs(results) do
local err, result = resp.err, resp.result
if err then
lsp.log.error(err.code, err.message)
elseif result then
results1[client_id] = result
elseif result and result.contents then
-- Make sure the response is not empty
if
(type(result.contents) == 'table' and #(vim.tbl_get(result.contents, 'value') or '') > 0)
or type(result.contents == 'string') and #result.contents > 0
then
results1[client_id] = result
else
empty_response = true
end
end
end
if vim.tbl_isempty(results1) then
if config.silent ~= true then
vim.notify('No information available', vim.log.levels.INFO)
if empty_response then
vim.notify('Empty hover response', vim.log.levels.INFO)
else
vim.notify('No information available', vim.log.levels.INFO)
end
end
return
end

View File

@@ -7110,4 +7110,31 @@ describe('LSP', function()
)
end)
end)
describe('vim.lsp.buf.hover()', function()
it('handles empty contents', function()
exec_lua(create_server_definition)
exec_lua(function()
local server = _G._create_server({
capabilities = {
hoverProvider = true,
},
handlers = {
['textDocument/hover'] = function(_, _, callback)
local res = {
contents = {
kind = 'markdown',
value = '',
},
}
callback(nil, res)
end,
},
})
vim.lsp.start({ name = 'dummy', cmd = server.cmd })
end)
eq('Empty hover response', n.exec_capture('lua vim.lsp.buf.hover()'))
end)
end)
end)