runtime: Add vim.lsp.get_client_by_name (#11603)

Since the client name is more obvious than the client id for the user, add an
API to get the lsp client by the client name.
This commit is contained in:
Hirokazu Hata
2019-12-29 02:28:00 +09:00
committed by Ashkan Kiani
parent 4f4c06a7a4
commit 680693e263
3 changed files with 22 additions and 2 deletions

View File

@@ -72,8 +72,9 @@ the option in an |after-directory| ftplugin, e.g. "after/ftplugin/python.vim".
*lsp-core-api* *lsp-core-api*
These are the core api functions for working with clients. You will mainly be These are the core api functions for working with clients. You will mainly be
using |vim.lsp.start_client()| and |vim.lsp.buf_attach_client()| for operations using |vim.lsp.start_client()| and |vim.lsp.buf_attach_client()| for operations
and |vim.lsp.get_client_by_id()| to retrieve a client by its id after it has and |vim.lsp.get_client_by_id()| and |vim.lsp.get_client_by_name()| to retrieve
initialized (or {config.on_init}. see below) a client by its id or name after it has initialized (or {config.on_init}. see
below)
*vim.lsp.start_client()* *vim.lsp.start_client()*
@@ -265,6 +266,11 @@ vim.lsp.get_client_by_id({client_id})
Look up an active client by its id, returns nil if it is not yet initialized Look up an active client by its id, returns nil if it is not yet initialized
or is not a valid id. Returns |lsp-client| or is not a valid id. Returns |lsp-client|
*vim.lsp.get_client_by_name()*
vim.lsp.get_client_by_name({client_name})
Look up an active client by its name, returns nil if it is not yet initialized
or is not a valid name. Returns |lsp-client|
*vim.lsp.stop_client()* *vim.lsp.stop_client()*
vim.lsp.stop_client({client_id}, [{force}]) vim.lsp.stop_client({client_id}, [{force}])

View File

@@ -697,6 +697,16 @@ function lsp.get_client_by_id(client_id)
return active_clients[client_id] return active_clients[client_id]
end end
-- Look up an active client by its name, returns nil if it is not yet initialized
-- or is not a valid name.
-- @param client_name string the client name.
function lsp.get_client_by_name(client_name)
for _, client in pairs(active_clients) do
if client.name == client_name then return client end
end
return nil
end
-- Stop a client by its id, optionally with force. -- Stop a client by its id, optionally with force.
-- You can also use the `stop()` function on a client if you already have -- You can also use the `stop()` function on a client if you already have
-- access to it. -- access to it.

View File

@@ -111,6 +111,7 @@ describe('Language Client API', function()
exec_lua([=[ exec_lua([=[
lsp = require('vim.lsp') lsp = require('vim.lsp')
local test_name, fixture_filename = ... local test_name, fixture_filename = ...
TEST_NAME = test_name
TEST_RPC_CLIENT_ID = lsp.start_client { TEST_RPC_CLIENT_ID = lsp.start_client {
cmd = { cmd = {
vim.api.nvim_get_vvar("progpath"), '-Es', '-u', 'NONE', '--headless', vim.api.nvim_get_vvar("progpath"), '-Es', '-u', 'NONE', '--headless',
@@ -118,6 +119,7 @@ describe('Language Client API', function()
"-c", "luafile "..fixture_filename; "-c", "luafile "..fixture_filename;
}; };
root_dir = vim.loop.cwd(); root_dir = vim.loop.cwd();
name = test_name;
} }
]=], test_name, lsp_test_rpc_server_file) ]=], test_name, lsp_test_rpc_server_file)
end) end)
@@ -137,6 +139,7 @@ describe('Language Client API', function()
end end
eq(1, exec_lua("return #lsp.get_active_clients()")) eq(1, exec_lua("return #lsp.get_active_clients()"))
eq(false, exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID) == nil")) eq(false, exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID) == nil"))
eq(false, exec_lua("return lsp.get_client_by_name(TEST_NAME) == nil"))
eq(false, exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID).is_stopped()")) eq(false, exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID).is_stopped()"))
exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID).stop()") exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID).stop()")
for _ = 1, 20 do for _ = 1, 20 do
@@ -146,6 +149,7 @@ describe('Language Client API', function()
end end
end end
eq(true, exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID) == nil")) eq(true, exec_lua("return lsp.get_client_by_id(TEST_RPC_CLIENT_ID) == nil"))
eq(true, exec_lua("return lsp.get_client_by_name(TEST_NAME) == nil"))
end) end)
end) end)
end) end)