feat(lsp): add tcp support

This commit is contained in:
Mathias Fussenegger 2022-08-24 20:25:34 +02:00
parent 46bb34e26b
commit 60ec6e34d5
4 changed files with 159 additions and 37 deletions

View File

@ -825,7 +825,7 @@ start({config}, {opts}) *vim.lsp.start()*
re-uses a client if name and root_dir matches. re-uses a client if name and root_dir matches.
Return: ~ Return: ~
(number) client_id (number|nil) client_id
start_client({config}) *vim.lsp.start_client()* start_client({config}) *vim.lsp.start_client()*
Starts and initializes a client with the given configuration. Starts and initializes a client with the given configuration.
@ -835,9 +835,16 @@ start_client({config}) *vim.lsp.start_client()*
The following parameters describe fields in the {config} table. The following parameters describe fields in the {config} table.
Parameters: ~ Parameters: ~
{cmd} (required, string or list treated like {cmd} (table|string|fun(dispatchers: table):table)
|jobstart()|) Base command that initiates the LSP command string or list treated like |jobstart|.
client. The command must launch the language server
process. `cmd` can also be a function that
creates an RPC client. The function receives a
dispatchers table and must return a table with
the functions `request`, `notify`, `is_closing`
and `terminate` See |vim.lsp.rpc.request| and
|vim.lsp.rpc.notify| For TCP there is a built-in
rpc client factory: |vim.lsp.rpc.connect|
{cmd_cwd} (string, default=|getcwd()|) Directory to launch {cmd_cwd} (string, default=|getcwd()|) Directory to launch
the `cmd` process. Not related to `root_dir`. the `cmd` process. Not related to `root_dir`.
{cmd_env} (table) Environment flags to pass to the LSP on {cmd_env} (table) Environment flags to pass to the LSP on
@ -1890,6 +1897,17 @@ should_log({level}) *vim.lsp.log.should_log()*
============================================================================== ==============================================================================
Lua module: vim.lsp.rpc *lsp-rpc* Lua module: vim.lsp.rpc *lsp-rpc*
connect({host}, {port}) *vim.lsp.rpc.connect()*
Create a LSP RPC client factory that connects via TCP to the given host
and port
Parameters: ~
{host} (string)
{port} (number)
Return: ~
(function)
format_rpc_error({err}) *vim.lsp.rpc.format_rpc_error()* format_rpc_error({err}) *vim.lsp.rpc.format_rpc_error()*
Constructs an error message from an LSP error object. Constructs an error message from an LSP error object.

View File

@ -289,7 +289,12 @@ local function validate_client_config(config)
'flags.debounce_text_changes must be a number with the debounce time in milliseconds' 'flags.debounce_text_changes must be a number with the debounce time in milliseconds'
) )
local cmd, cmd_args = lsp._cmd_parts(config.cmd) local cmd, cmd_args
if type(config.cmd) == 'function' then
cmd = config.cmd
else
cmd, cmd_args = lsp._cmd_parts(config.cmd)
end
local offset_encoding = valid_encodings.UTF16 local offset_encoding = valid_encodings.UTF16
if config.offset_encoding then if config.offset_encoding then
offset_encoding = validate_encoding(config.offset_encoding) offset_encoding = validate_encoding(config.offset_encoding)
@ -855,14 +860,17 @@ end
--- Used on all running clients. --- Used on all running clients.
--- The default implementation re-uses a client if name --- The default implementation re-uses a client if name
--- and root_dir matches. --- and root_dir matches.
---@return number client_id ---@return number|nil client_id
function lsp.start(config, opts) function lsp.start(config, opts)
opts = opts or {} opts = opts or {}
local reuse_client = opts.reuse_client local reuse_client = opts.reuse_client
or function(client, conf) or function(client, conf)
return client.config.root_dir == conf.root_dir and client.name == conf.name return client.config.root_dir == conf.root_dir and client.name == conf.name
end end
config.name = config.name or (config.cmd[1] and vim.fs.basename(config.cmd[1])) or nil config.name = config.name
if not config.name and type(config.cmd) == 'table' then
config.name = config.cmd[1] and vim.fs.basename(config.cmd[1]) or nil
end
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
for _, clients in ipairs({ uninitialized_clients, lsp.get_active_clients() }) do for _, clients in ipairs({ uninitialized_clients, lsp.get_active_clients() }) do
for _, client in pairs(clients) do for _, client in pairs(clients) do
@ -893,8 +901,13 @@ end
--- The following parameters describe fields in the {config} table. --- The following parameters describe fields in the {config} table.
--- ---
--- ---
---@param cmd: (required, string or list treated like |jobstart()|) Base command ---@param cmd: (table|string|fun(dispatchers: table):table) command string or
--- that initiates the LSP client. --- list treated like |jobstart|. The command must launch the language server
--- process. `cmd` can also be a function that creates an RPC client.
--- The function receives a dispatchers table and must return a table with the
--- functions `request`, `notify`, `is_closing` and `terminate`
--- See |vim.lsp.rpc.request| and |vim.lsp.rpc.notify|
--- For TCP there is a built-in rpc client factory: |vim.lsp.rpc.connect|
--- ---
---@param cmd_cwd: (string, default=|getcwd()|) Directory to launch ---@param cmd_cwd: (string, default=|getcwd()|) Directory to launch
--- the `cmd` process. Not related to `root_dir`. --- the `cmd` process. Not related to `root_dir`.
@ -1164,11 +1177,16 @@ function lsp.start_client(config)
end end
-- Start the RPC client. -- Start the RPC client.
local rpc = lsp_rpc.start(cmd, cmd_args, dispatch, { local rpc
if type(cmd) == 'function' then
rpc = cmd(dispatch)
else
rpc = lsp_rpc.start(cmd, cmd_args, dispatch, {
cwd = config.cmd_cwd, cwd = config.cmd_cwd,
env = config.cmd_env, env = config.cmd_env,
detached = config.detached, detached = config.detached,
}) })
end
-- Return nil if client fails to start -- Return nil if client fails to start
if not rpc then if not rpc then

View File

@ -556,6 +556,84 @@ local function public_client(client)
return result return result
end end
---@private
local function merge_dispatchers(dispatchers)
if dispatchers then
local user_dispatchers = dispatchers
dispatchers = {}
for dispatch_name, default_dispatch in pairs(default_dispatchers) do
local user_dispatcher = user_dispatchers[dispatch_name]
if user_dispatcher then
if type(user_dispatcher) ~= 'function' then
error(string.format('dispatcher.%s must be a function', dispatch_name))
end
-- server_request is wrapped elsewhere.
if
not (dispatch_name == 'server_request' or dispatch_name == 'on_exit') -- TODO this blocks the loop exiting for some reason.
then
user_dispatcher = schedule_wrap(user_dispatcher)
end
dispatchers[dispatch_name] = user_dispatcher
else
dispatchers[dispatch_name] = default_dispatch
end
end
else
dispatchers = default_dispatchers
end
return dispatchers
end
--- Create a LSP RPC client factory that connects via TCP to the given host
--- and port
---
---@param host string
---@param port number
---@return function
local function connect(host, port)
return function(dispatchers)
dispatchers = merge_dispatchers(dispatchers)
local tcp = uv.new_tcp()
local closing = false
local transport = {
write = function(msg)
tcp:write(msg)
end,
is_closing = function()
return closing
end,
terminate = function()
if not closing then
closing = true
tcp:shutdown()
tcp:close()
dispatchers.on_exit(0, 0)
end
end,
}
local client = new_client(dispatchers, transport)
tcp:connect(host, port, function(err)
if err then
vim.schedule(function()
vim.notify(
string.format('Could not connect to %s:%s, reason: %s', host, port, vim.inspect(err)),
vim.log.levels.WARN
)
end)
return
end
local handle_body = function(body)
client:handle_body(body)
end
tcp:read_start(create_read_loop(handle_body, transport.terminate, function(read_err)
client:on_error(client_errors.READ_ERROR, read_err)
end))
end)
return public_client(client)
end
end
--- Starts an LSP server process and create an LSP RPC client object to --- Starts an LSP server process and create an LSP RPC client object to
--- interact with it. Communication with the server is currently limited to stdio. --- interact with it. Communication with the server is currently limited to stdio.
--- ---
@ -593,30 +671,8 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
if extra_spawn_params and extra_spawn_params.cwd then if extra_spawn_params and extra_spawn_params.cwd then
assert(is_dir(extra_spawn_params.cwd), 'cwd must be a directory') assert(is_dir(extra_spawn_params.cwd), 'cwd must be a directory')
end end
if dispatchers then
local user_dispatchers = dispatchers
dispatchers = {}
for dispatch_name, default_dispatch in pairs(default_dispatchers) do
local user_dispatcher = user_dispatchers[dispatch_name]
if user_dispatcher then
if type(user_dispatcher) ~= 'function' then
error(string.format('dispatcher.%s must be a function', dispatch_name))
end
-- server_request is wrapped elsewhere.
if
not (dispatch_name == 'server_request' or dispatch_name == 'on_exit') -- TODO this blocks the loop exiting for some reason.
then
user_dispatcher = schedule_wrap(user_dispatcher)
end
dispatchers[dispatch_name] = user_dispatcher
else
dispatchers[dispatch_name] = default_dispatch
end
end
else
dispatchers = default_dispatchers
end
dispatchers = merge_dispatchers(dispatchers)
local stdin = uv.new_pipe(false) local stdin = uv.new_pipe(false)
local stdout = uv.new_pipe(false) local stdout = uv.new_pipe(false)
local stderr = uv.new_pipe(false) local stderr = uv.new_pipe(false)
@ -693,8 +749,10 @@ end
return { return {
start = start, start = start,
connect = connect,
rpc_response_error = rpc_response_error, rpc_response_error = rpc_response_error,
format_rpc_error = format_rpc_error, format_rpc_error = format_rpc_error,
client_errors = client_errors, client_errors = client_errors,
create_read_loop = create_read_loop,
} }
-- vim:sw=2 ts=2 et -- vim:sw=2 ts=2 et

View File

@ -3181,4 +3181,32 @@ describe('LSP', function()
} }
end) end)
end) end)
describe('cmd', function()
it('can connect to lsp server via rpc.connect', function()
local result = exec_lua [[
local uv = vim.loop
local server = uv.new_tcp()
local init = nil
server:bind('127.0.0.1', 0)
server:listen(127, function(err)
assert(not err, err)
local socket = uv.new_tcp()
server:accept(socket)
socket:read_start(require('vim.lsp.rpc').create_read_loop(function(body)
init = body
socket:close()
end))
end)
local port = server:getsockname().port
vim.lsp.start({ name = 'dummy', cmd = vim.lsp.rpc.connect('127.0.0.1', port) })
vim.wait(1000, function() return init ~= nil end)
assert(init, "server must receive `initialize` request")
server:close()
server:shutdown()
return vim.json.decode(init)
]]
eq(result.method, "initialize")
end)
end)
end) end)