Skip to content

Commit

Permalink
feat(lsp): add tcp support
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Aug 24, 2022
1 parent b1ded9c commit a2e53e0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 37 deletions.
26 changes: 22 additions & 4 deletions runtime/doc/lsp.txt
Expand Up @@ -825,7 +825,7 @@ start({config}, {opts}) *vim.lsp.start()*
re-uses a client if name and root_dir matches.

Return: ~
(number) client_id
(number|nil) client_id

start_client({config}) *vim.lsp.start_client()*
Starts and initializes a client with the given configuration.
Expand All @@ -835,9 +835,16 @@ start_client({config}) *vim.lsp.start_client()*
The following parameters describe fields in the {config} table.

Parameters: ~
{cmd} (required, string or list treated like
|jobstart()|) Base command that initiates the LSP
client.
{cmd} (table|string|fun(dispatchers: table):table)
command string or 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|
{cmd_cwd} (string, default=|getcwd()|) Directory to launch
the `cmd` process. Not related to `root_dir`.
{cmd_env} (table) Environment flags to pass to the LSP on
Expand Down Expand Up @@ -1890,6 +1897,17 @@ should_log({level}) *vim.lsp.log.should_log()*
==============================================================================
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()*
Constructs an error message from an LSP error object.

Expand Down
38 changes: 28 additions & 10 deletions runtime/lua/vim/lsp.lua
Expand Up @@ -289,7 +289,12 @@ local function validate_client_config(config)
'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
if config.offset_encoding then
offset_encoding = validate_encoding(config.offset_encoding)
Expand Down Expand Up @@ -855,14 +860,17 @@ end
--- Used on all running clients.
--- The default implementation re-uses a client if name
--- and root_dir matches.
---@return number client_id
---@return number|nil client_id
function lsp.start(config, opts)
opts = opts or {}
local reuse_client = opts.reuse_client
or function(client, conf)
return client.config.root_dir == conf.root_dir and client.name == conf.name
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()
for _, clients in ipairs({ uninitialized_clients, lsp.get_active_clients() }) do
for _, client in pairs(clients) do
Expand Down Expand Up @@ -893,8 +901,13 @@ end
--- The following parameters describe fields in the {config} table.
---
---
---@param cmd: (required, string or list treated like |jobstart()|) Base command
--- that initiates the LSP client.
---@param cmd: (table|string|fun(dispatchers: table):table) command string or
--- 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
--- the `cmd` process. Not related to `root_dir`.
Expand Down Expand Up @@ -1164,11 +1177,16 @@ function lsp.start_client(config)
end

-- Start the RPC client.
local rpc = lsp_rpc.start(cmd, cmd_args, dispatch, {
cwd = config.cmd_cwd,
env = config.cmd_env,
detached = config.detached,
})
local rpc
if type(cmd) == 'function' then
rpc = cmd(dispatch)
else
rpc = lsp_rpc.start(cmd, cmd_args, dispatch, {
cwd = config.cmd_cwd,
env = config.cmd_env,
detached = config.detached,
})
end

-- Return nil if client fails to start
if not rpc then
Expand Down
102 changes: 79 additions & 23 deletions runtime/lua/vim/lsp/rpc.lua
Expand Up @@ -556,6 +556,83 @@ local function public_client(client)
return result
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()
end
end,
}
local client = new_client(dispatchers, transport)
tcp:connect(host, port, function(err)
if err then
vim.notify(
string.format('Could not connect to %s:%s, reason: %s', host, port, vim.inspect(err)),
vim.log.levels.WARN
)
return
end
local request_parser = coroutine.wrap(request_parser_loop)
request_parser()
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
--- interact with it. Communication with the server is currently limited to stdio.
---
Expand Down Expand Up @@ -593,30 +670,8 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
if extra_spawn_params and extra_spawn_params.cwd then
assert(is_dir(extra_spawn_params.cwd), 'cwd must be a directory')
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 stdout = uv.new_pipe(false)
local stderr = uv.new_pipe(false)
Expand Down Expand Up @@ -693,6 +748,7 @@ end

return {
start = start,
connect = connect,
rpc_response_error = rpc_response_error,
format_rpc_error = format_rpc_error,
client_errors = client_errors,
Expand Down

0 comments on commit a2e53e0

Please sign in to comment.