|
|
local log = require('vim.lsp.log') |
|
|
local protocol = require('vim.lsp.protocol') |
|
|
local lsp_transport = require('vim.lsp._transport') |
|
|
local validate, schedule_wrap = vim.validate, vim.schedule_wrap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function format_message_with_content_length(message) |
|
|
return table.concat({ |
|
|
'Content-Length: ', |
|
|
tostring(#message), |
|
|
'\r\n\r\n', |
|
|
message, |
|
|
}) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function parse_headers(header) |
|
|
assert(type(header) == 'string', 'header must be a string') |
|
|
|
|
|
local headers = {} |
|
|
for line in vim.gsplit(header, '\r\n', { plain = true }) do |
|
|
if line == '' then |
|
|
break |
|
|
end |
|
|
|
|
|
local key, value = line:match('^%s*(%S+)%s*:%s*(.+)%s*$') |
|
|
if key then |
|
|
key = key:lower():gsub('%-', '_') |
|
|
headers[key] = value |
|
|
else |
|
|
log.error('invalid header line %q', line) |
|
|
error(string.format('invalid header line %q', line)) |
|
|
end |
|
|
end |
|
|
headers.content_length = tonumber(headers.content_length) |
|
|
or error(string.format('Content-Length not found in headers. %q', header)) |
|
|
return headers |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local header_start_pattern = ('content'):gsub('%w', function(c) |
|
|
return '[' .. c .. c:upper() .. ']' |
|
|
end) |
|
|
|
|
|
|
|
|
local function request_parser_loop() |
|
|
local buffer = '' |
|
|
while true do |
|
|
|
|
|
|
|
|
local start, finish = buffer:find('\r\n\r\n', 1, true) |
|
|
|
|
|
if start then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local buffer_start = buffer:find(header_start_pattern) |
|
|
if not buffer_start then |
|
|
error( |
|
|
string.format( |
|
|
"Headers were expected, a different response was received. The server response was '%s'.", |
|
|
buffer |
|
|
) |
|
|
) |
|
|
end |
|
|
local headers = parse_headers(buffer:sub(buffer_start, start - 1)) |
|
|
local content_length = headers.content_length |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local body_chunks = { buffer:sub(finish + 1) } |
|
|
local body_length = #body_chunks[1] |
|
|
|
|
|
while body_length < content_length do |
|
|
|
|
|
local chunk = coroutine.yield() |
|
|
or error('Expected more data for the body. The server may have died.') |
|
|
table.insert(body_chunks, chunk) |
|
|
body_length = body_length + #chunk |
|
|
end |
|
|
local last_chunk = body_chunks[#body_chunks] |
|
|
|
|
|
body_chunks[#body_chunks] = last_chunk:sub(1, content_length - body_length - 1) |
|
|
local rest = '' |
|
|
if body_length > content_length then |
|
|
rest = last_chunk:sub(content_length - body_length) |
|
|
end |
|
|
local body = table.concat(body_chunks) |
|
|
|
|
|
|
|
|
|
|
|
local data = coroutine.yield(headers, body) |
|
|
or error('Expected more data for the body. The server may have died.') |
|
|
buffer = rest .. data |
|
|
else |
|
|
|
|
|
|
|
|
local data = coroutine.yield() |
|
|
or error('Expected more data for the header. The server may have died.') |
|
|
buffer = buffer .. data |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
local M = {} |
|
|
|
|
|
|
|
|
|
|
|
local client_errors = { |
|
|
INVALID_SERVER_MESSAGE = 1, |
|
|
INVALID_SERVER_JSON = 2, |
|
|
NO_RESULT_CALLBACK_FOUND = 3, |
|
|
READ_ERROR = 4, |
|
|
NOTIFICATION_HANDLER_ERROR = 5, |
|
|
SERVER_REQUEST_HANDLER_ERROR = 6, |
|
|
SERVER_RESULT_CALLBACK_ERROR = 7, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
M.client_errors = vim.deepcopy(client_errors) |
|
|
for k, v in pairs(client_errors) do |
|
|
M.client_errors[v] = k |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.format_rpc_error(err) |
|
|
validate('err', err, 'table') |
|
|
|
|
|
|
|
|
|
|
|
local code |
|
|
if protocol.ErrorCodes[err.code] then |
|
|
code = string.format('code_name = %s,', protocol.ErrorCodes[err.code]) |
|
|
else |
|
|
code = string.format('code_name = unknown, code = %s,', err.code) |
|
|
end |
|
|
|
|
|
local message_parts = { 'RPC[Error]', code } |
|
|
if err.message then |
|
|
table.insert(message_parts, 'message =') |
|
|
table.insert(message_parts, string.format('%q', err.message)) |
|
|
end |
|
|
if err.data then |
|
|
table.insert(message_parts, 'data =') |
|
|
table.insert(message_parts, vim.inspect(err.data)) |
|
|
end |
|
|
return table.concat(message_parts, ' ') |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.rpc_response_error(code, message, data) |
|
|
|
|
|
|
|
|
local code_name = assert(protocol.ErrorCodes[code], 'Invalid RPC error code') |
|
|
return setmetatable({ |
|
|
code = code, |
|
|
message = message or code_name, |
|
|
data = data, |
|
|
}, { |
|
|
__tostring = M.format_rpc_error, |
|
|
}) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local default_dispatchers = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notification = function(method, params) |
|
|
log.debug('notification', method, params) |
|
|
end, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server_request = function(method, params) |
|
|
log.debug('server_request', method, params) |
|
|
return nil, M.rpc_response_error(protocol.ErrorCodes.MethodNotFound) |
|
|
end, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
on_exit = function(code, signal) |
|
|
log.info('client_exit', { code = code, signal = signal }) |
|
|
end, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
on_error = function(code, err) |
|
|
log.error('client_error:', M.client_errors[code], err) |
|
|
end, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.create_read_loop(handle_body, on_exit, on_error) |
|
|
local parse_chunk = coroutine.wrap(request_parser_loop) |
|
|
parse_chunk() |
|
|
return function(err, chunk) |
|
|
if err then |
|
|
on_error(err) |
|
|
return |
|
|
end |
|
|
|
|
|
if not chunk then |
|
|
if on_exit then |
|
|
on_exit() |
|
|
end |
|
|
return |
|
|
end |
|
|
|
|
|
while true do |
|
|
local headers, body = parse_chunk(chunk) |
|
|
if headers then |
|
|
handle_body(assert(body)) |
|
|
chunk = '' |
|
|
else |
|
|
break |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local Client = {} |
|
|
|
|
|
|
|
|
function Client:encode_and_send(payload) |
|
|
log.debug('rpc.send', payload) |
|
|
if self.transport:is_closing() then |
|
|
return false |
|
|
end |
|
|
local jsonstr = vim.json.encode(payload) |
|
|
|
|
|
self.transport:write(format_message_with_content_length(jsonstr)) |
|
|
return true |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client:notify(method, params) |
|
|
return self:encode_and_send({ |
|
|
jsonrpc = '2.0', |
|
|
method = method, |
|
|
params = params, |
|
|
}) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
function Client:send_response(request_id, err, result) |
|
|
return self:encode_and_send({ |
|
|
id = request_id, |
|
|
jsonrpc = '2.0', |
|
|
error = err, |
|
|
result = result, |
|
|
}) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client:request(method, params, callback, notify_reply_callback) |
|
|
validate('callback', callback, 'function') |
|
|
validate('notify_reply_callback', notify_reply_callback, 'function', true) |
|
|
self.message_index = self.message_index + 1 |
|
|
local message_id = self.message_index |
|
|
local result = self:encode_and_send({ |
|
|
id = message_id, |
|
|
jsonrpc = '2.0', |
|
|
method = method, |
|
|
params = params, |
|
|
}) |
|
|
|
|
|
if not result then |
|
|
return false |
|
|
end |
|
|
|
|
|
self.message_callbacks[message_id] = schedule_wrap(callback) |
|
|
if notify_reply_callback then |
|
|
self.notify_reply_callbacks[message_id] = schedule_wrap(notify_reply_callback) |
|
|
end |
|
|
return result, message_id |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client:on_error(errkind, ...) |
|
|
assert(M.client_errors[errkind]) |
|
|
|
|
|
pcall(self.dispatchers.on_error, errkind, ...) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client:pcall_handler(errkind, status, head, ...) |
|
|
if not status then |
|
|
self:on_error(errkind, head, ...) |
|
|
return status, head |
|
|
end |
|
|
return status, head, ... |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client:try_call(errkind, fn, ...) |
|
|
return self:pcall_handler(errkind, pcall(fn, ...)) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client:handle_body(body) |
|
|
local ok, decoded = pcall(vim.json.decode, body, { luanil = { object = true } }) |
|
|
if not ok then |
|
|
self:on_error(M.client_errors.INVALID_SERVER_JSON, decoded) |
|
|
return |
|
|
end |
|
|
log.debug('rpc.receive', decoded) |
|
|
|
|
|
if type(decoded) ~= 'table' then |
|
|
self:on_error(M.client_errors.INVALID_SERVER_MESSAGE, decoded) |
|
|
elseif type(decoded.method) == 'string' and decoded.id then |
|
|
local err |
|
|
|
|
|
|
|
|
vim.schedule(coroutine.wrap(function() |
|
|
local status, result |
|
|
status, result, err = self:try_call( |
|
|
M.client_errors.SERVER_REQUEST_HANDLER_ERROR, |
|
|
self.dispatchers.server_request, |
|
|
decoded.method, |
|
|
decoded.params |
|
|
) |
|
|
log.debug('server_request: callback result', { status = status, result = result, err = err }) |
|
|
if status then |
|
|
if result == nil and err == nil then |
|
|
error( |
|
|
string.format( |
|
|
'method %q: either a result or an error must be sent to the server in response', |
|
|
decoded.method |
|
|
) |
|
|
) |
|
|
end |
|
|
if err then |
|
|
|
|
|
assert( |
|
|
type(err) == 'table', |
|
|
'err must be a table. Use rpc_response_error to help format errors.' |
|
|
) |
|
|
|
|
|
local code_name = assert( |
|
|
protocol.ErrorCodes[err.code], |
|
|
'Errors must use protocol.ErrorCodes. Use rpc_response_error to help format errors.' |
|
|
) |
|
|
err.message = err.message or code_name |
|
|
end |
|
|
else |
|
|
|
|
|
err = M.rpc_response_error(protocol.ErrorCodes.InternalError, result) |
|
|
result = nil |
|
|
end |
|
|
self:send_response(decoded.id, err, result) |
|
|
end)) |
|
|
|
|
|
elseif decoded.id and (decoded.result ~= vim.NIL or decoded.error ~= vim.NIL) then |
|
|
|
|
|
local result_id = assert(tonumber(decoded.id), 'response id must be a number') |
|
|
|
|
|
|
|
|
local notify_reply_callback = self.notify_reply_callbacks[result_id] |
|
|
if notify_reply_callback then |
|
|
validate('notify_reply_callback', notify_reply_callback, 'function') |
|
|
notify_reply_callback(result_id) |
|
|
self.notify_reply_callbacks[result_id] = nil |
|
|
end |
|
|
|
|
|
|
|
|
if decoded.error then |
|
|
assert(type(decoded.error) == 'table') |
|
|
if decoded.error.code == protocol.ErrorCodes.RequestCancelled then |
|
|
log.debug('Received cancellation ack', decoded) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if result_id then |
|
|
self.message_callbacks[result_id] = nil |
|
|
end |
|
|
return |
|
|
end |
|
|
end |
|
|
|
|
|
local callback = self.message_callbacks[result_id] |
|
|
if callback then |
|
|
self.message_callbacks[result_id] = nil |
|
|
validate('callback', callback, 'function') |
|
|
if decoded.error then |
|
|
setmetatable(decoded.error, { __tostring = M.format_rpc_error }) |
|
|
end |
|
|
self:try_call( |
|
|
M.client_errors.SERVER_RESULT_CALLBACK_ERROR, |
|
|
callback, |
|
|
decoded.error, |
|
|
decoded.result |
|
|
) |
|
|
else |
|
|
self:on_error(M.client_errors.NO_RESULT_CALLBACK_FOUND, decoded) |
|
|
log.error('No callback found for server response id ' .. result_id) |
|
|
end |
|
|
elseif type(decoded.method) == 'string' then |
|
|
|
|
|
self:try_call( |
|
|
M.client_errors.NOTIFICATION_HANDLER_ERROR, |
|
|
self.dispatchers.notification, |
|
|
decoded.method, |
|
|
decoded.params |
|
|
) |
|
|
else |
|
|
|
|
|
self:on_error(M.client_errors.INVALID_SERVER_MESSAGE, decoded) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function new_client(dispatchers, transport) |
|
|
local state = { |
|
|
message_index = 0, |
|
|
message_callbacks = {}, |
|
|
notify_reply_callbacks = {}, |
|
|
transport = transport, |
|
|
dispatchers = dispatchers, |
|
|
} |
|
|
return setmetatable(state, { __index = Client }) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function public_client(client) |
|
|
|
|
|
|
|
|
local result = {} |
|
|
|
|
|
|
|
|
function result.is_closing() |
|
|
return client.transport:is_closing() |
|
|
end |
|
|
|
|
|
|
|
|
function result.terminate() |
|
|
client.transport:terminate() |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function result.request(method, params, callback, notify_reply_callback) |
|
|
return client:request(method, params, callback, notify_reply_callback) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function result.notify(method, params) |
|
|
return client:notify(method, params) |
|
|
end |
|
|
|
|
|
return result |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function merge_dispatchers(dispatchers) |
|
|
if not dispatchers then |
|
|
return default_dispatchers |
|
|
end |
|
|
|
|
|
for name, fn in pairs(dispatchers) do |
|
|
if type(fn) ~= 'function' then |
|
|
error(string.format('dispatcher.%s must be a function', name)) |
|
|
end |
|
|
end |
|
|
|
|
|
local merged = { |
|
|
notification = ( |
|
|
dispatchers.notification and vim.schedule_wrap(dispatchers.notification) |
|
|
or default_dispatchers.notification |
|
|
), |
|
|
on_error = ( |
|
|
dispatchers.on_error and vim.schedule_wrap(dispatchers.on_error) |
|
|
or default_dispatchers.on_error |
|
|
), |
|
|
on_exit = dispatchers.on_exit or default_dispatchers.on_exit, |
|
|
server_request = dispatchers.server_request or default_dispatchers.server_request, |
|
|
} |
|
|
return merged |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function create_client_read_loop(client, on_exit) |
|
|
|
|
|
local function handle_body(body) |
|
|
client:handle_body(body) |
|
|
end |
|
|
|
|
|
local function on_error(err) |
|
|
client:on_error(M.client_errors.READ_ERROR, err) |
|
|
end |
|
|
|
|
|
return M.create_read_loop(handle_body, on_exit, on_error) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.connect(host_or_path, port) |
|
|
validate('host_or_path', host_or_path, 'string') |
|
|
validate('port', port, 'number', true) |
|
|
|
|
|
return function(dispatchers) |
|
|
validate('dispatchers', dispatchers, 'table', true) |
|
|
|
|
|
dispatchers = merge_dispatchers(dispatchers) |
|
|
|
|
|
local transport = lsp_transport.TransportConnect.new() |
|
|
local client = new_client(dispatchers, transport) |
|
|
local on_read = create_client_read_loop(client, function() |
|
|
transport:terminate() |
|
|
end) |
|
|
transport:connect(host_or_path, port, on_read, dispatchers.on_exit) |
|
|
|
|
|
return public_client(client) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.start(cmd, dispatchers, extra_spawn_params) |
|
|
log.info('Starting RPC client', { cmd = cmd, extra = extra_spawn_params }) |
|
|
|
|
|
validate('cmd', cmd, 'table') |
|
|
validate('dispatchers', dispatchers, 'table', true) |
|
|
|
|
|
dispatchers = merge_dispatchers(dispatchers) |
|
|
|
|
|
local transport = lsp_transport.TransportRun.new() |
|
|
local client = new_client(dispatchers, transport) |
|
|
local on_read = create_client_read_loop(client) |
|
|
transport:run(cmd, extra_spawn_params, on_read, dispatchers.on_exit) |
|
|
|
|
|
return public_client(client) |
|
|
end |
|
|
|
|
|
return M |
|
|
|