From c3991b8ef4f086b479ae3bc0f65aecaa07271a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Jos=C3=A9=20Solano?= Date: Sun, 20 Jul 2025 13:58:40 -0700 Subject: [PATCH] fix(lsp): show notification with empty hover response (#35014) --- runtime/lua/vim/lsp/buf.lua | 19 ++++++++++++++++--- test/functional/plugin/lsp_spec.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 2d4e1e77b3..c7505ff3b0 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -61,19 +61,32 @@ function M.hover(config) -- Filter errors from results local results1 = {} --- @type table + 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 diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 7d6bc84535..a88d00afaa 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -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)