lsp: Fix "unsupported_method" error when the buffer does not have an LSP Server (#13175)

This commit is contained in:
eightpigs
2020-12-03 14:00:54 +08:00
committed by GitHub
parent 8fb786e415
commit fa73bb70fc
2 changed files with 44 additions and 6 deletions

View File

@@ -1032,9 +1032,9 @@ function lsp.buf_request(bufnr, method, params, handler)
end
end)
-- if no clients support the given method, call the handler with the proper
-- if has client but no clients support the given method, call the callback with the proper
-- error message.
if not method_supported then
if not tbl_isempty(all_buffer_active_clients[resolve_bufnr(bufnr)] or {}) and not method_supported then
local unsupported_err = lsp._unsupported_method(method)
handler = handler or lsp.handlers[method]
if handler then

View File

@@ -323,23 +323,61 @@ describe('LSP', function()
test_name = "capabilities_for_client_supports_method";
on_setup = function()
exec_lua([=[
vim.lsp.handlers['textDocument/hover'] = function(err, method)
BUFFER = vim.api.nvim_get_current_buf()
lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)
vim.lsp.callbacks['textDocument/typeDefinition'] = function(err, method)
vim.lsp._last_lsp_callback = { err = err; method = method }
end
vim.lsp._unsupported_method = function(method)
vim.lsp._last_unsupported_method = method
return 'fake-error'
end
vim.lsp.buf.hover()
vim.lsp.buf.type_definition()
]=])
end;
on_init = function(client)
client.stop()
local method = exec_lua("return vim.lsp._last_unsupported_method")
eq("textDocument/hover", method)
eq("textDocument/typeDefinition", method)
local lsp_cb_call = exec_lua("return vim.lsp._last_lsp_callback")
eq("fake-error", lsp_cb_call.err)
eq("textDocument/hover", lsp_cb_call.method)
eq("textDocument/typeDefinition", lsp_cb_call.method)
exec_lua [[
vim.api.nvim_command(BUFFER.."bwipeout")
]]
end;
on_exit = function(code, signal)
eq(0, code, "exit code", fake_lsp_logfile)
eq(0, signal, "exit signal", fake_lsp_logfile)
end;
on_callback = function(...)
eq(table.remove(expected_callbacks), {...}, "expected callback")
end;
}
end)
it('shouldn\'t call unsupported_method when no client and trying to call an unsupported method', function()
local expected_callbacks = {
{NIL, "shutdown", {}, 1};
}
test_rpc_server {
test_name = "capabilities_for_client_supports_method";
on_setup = function()
exec_lua([=[
vim.lsp.callbacks['textDocument/typeDefinition'] = function(err, method)
vim.lsp._last_lsp_callback = { err = err; method = method }
end
vim.lsp._unsupported_method = function(method)
vim.lsp._last_unsupported_method = method
return 'fake-error'
end
vim.lsp.buf.type_definition()
]=])
end;
on_init = function(client)
client.stop()
eq(NIL, exec_lua("return vim.lsp._last_unsupported_method"))
eq(NIL, exec_lua("return vim.lsp._last_lsp_callback"))
end;
on_exit = function(code, signal)
eq(0, code, "exit code", fake_lsp_logfile)