feat(lsp): use cjson for lsp rpc (#15759)

This commit is contained in:
Michael Lingelbach 2021-09-26 13:53:04 -07:00 committed by GitHub
parent 68c65b7732
commit c217766f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 12 deletions

View File

@ -830,7 +830,7 @@ function lsp.start_client(config)
rpc.request('initialize', initialize_params, function(init_err, result) rpc.request('initialize', initialize_params, function(init_err, result)
assert(not init_err, tostring(init_err)) assert(not init_err, tostring(init_err))
assert(result, "server sent empty result") assert(result, "server sent empty result")
rpc.notify('initialized', {[vim.type_idx]=vim.types.dictionary}) rpc.notify('initialized', vim.empty_dict())
client.initialized = true client.initialized = true
uninitialized_clients[client_id] = nil uninitialized_clients[client_id] = nil
client.workspaceFolders = initialize_params.workspaceFolders client.workspaceFolders = initialize_params.workspaceFolders

View File

@ -11,7 +11,7 @@ local validate, schedule, schedule_wrap = vim.validate, vim.schedule, vim.schedu
---@param data (table) Data to encode ---@param data (table) Data to encode
---@returns (string) Encoded object ---@returns (string) Encoded object
local function json_encode(data) local function json_encode(data)
local status, result = pcall(vim.fn.json_encode, data) local status, result = pcall(vim.json.encode, data)
if status then if status then
return result return result
else else
@ -24,7 +24,7 @@ end
---@param data (string) Data to decode ---@param data (string) Data to decode
---@returns (table) Decoded JSON object ---@returns (table) Decoded JSON object
local function json_decode(data) local function json_decode(data)
local status, result = pcall(vim.fn.json_decode, data) local status, result = pcall(vim.json.decode, data)
if status then if status then
return result return result
else else
@ -394,11 +394,8 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
local function encode_and_send(payload) local function encode_and_send(payload)
local _ = log.debug() and log.debug("rpc.send", payload) local _ = log.debug() and log.debug("rpc.send", payload)
if handle == nil or handle:is_closing() then return false end if handle == nil or handle:is_closing() then return false end
-- TODO(ashkan) remove this once we have a Lua json_encode local encoded = assert(json_encode(payload))
schedule(function() stdin:write(format_message_with_content_length(encoded))
local encoded = assert(json_encode(payload))
stdin:write(format_message_with_content_length(encoded))
end)
return true return true
end end
@ -582,8 +579,6 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
on_error(client_errors.INVALID_SERVER_MESSAGE, decoded) on_error(client_errors.INVALID_SERVER_MESSAGE, decoded)
end end
end end
-- TODO(ashkan) remove this once we have a Lua json_decode
handle_body = schedule_wrap(handle_body)
local request_parser = coroutine.wrap(request_parser_loop) local request_parser = coroutine.wrap(request_parser_loop)
request_parser() request_parser()

View File

@ -43,11 +43,11 @@ end
local function read_message() local function read_message()
local line = io.read("*l") local line = io.read("*l")
local length = line:lower():match("content%-length:%s*(%d+)") local length = line:lower():match("content%-length:%s*(%d+)")
return vim.fn.json_decode(io.read(2 + length):sub(2)) return vim.json.decode(io.read(2 + length):sub(2))
end end
local function send(payload) local function send(payload)
io.stdout:write(format_message_with_content_length(vim.fn.json_encode(payload))) io.stdout:write(format_message_with_content_length(vim.json.encode(payload)))
end end
local function respond(id, err, result) local function respond(id, err, result)