fix: make lsp_* async (#1506)

This commit is contained in:
Chinmay Dalal
2021-12-02 12:21:13 +05:30
committed by GitHub
parent aa41912ef8
commit 7462999202

View File

@@ -16,17 +16,15 @@ lsp.references = function(opts)
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
params.context = { includeDeclaration = true } params.context = { includeDeclaration = true }
local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/references", params, opts.timeout or 10000) vim.lsp.buf_request(0, "textDocument/references", params, function(err, result, _ctx, _config)
if err then if err then
vim.api.nvim_err_writeln("Error when finding references: " .. err) vim.api.nvim_err_writeln("Error when finding references: " .. err)
return return
end end
local locations = {} local locations = {}
for _, server_results in pairs(results_lsp) do if result then
if server_results.result then vim.list_extend(locations, vim.lsp.util.locations_to_items(result) or {})
vim.list_extend(locations, vim.lsp.util.locations_to_items(server_results.result) or {})
end
end end
if vim.tbl_isempty(locations) then if vim.tbl_isempty(locations) then
@@ -42,28 +40,26 @@ lsp.references = function(opts)
previewer = conf.qflist_previewer(opts), previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
}):find() }):find()
end)
end end
local function list_or_jump(action, title, opts) local function list_or_jump(action, title, opts)
opts = opts or {} opts = opts or {}
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
local result, err = vim.lsp.buf_request_sync(0, action, params, opts.timeout or 10000) vim.lsp.buf_request(0, action, params, function(err, result, _ctx, _config)
if err then if err then
vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err) vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err)
return return
end end
local flattened_results = {} local flattened_results = {}
for _, server_results in pairs(result) do if result then
if server_results.result then
-- textDocument/definition can return Location or Location[] -- textDocument/definition can return Location or Location[]
if not vim.tbl_islist(server_results.result) then if not vim.tbl_islist(result) then
flattened_results = { server_results.result } flattened_results = { result }
break
end end
vim.list_extend(flattened_results, server_results.result) vim.list_extend(flattened_results, result)
end
end end
if #flattened_results == 0 then if #flattened_results == 0 then
@@ -89,6 +85,7 @@ local function list_or_jump(action, title, opts)
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
}):find() }):find()
end end
end)
end end
lsp.definitions = function(opts) lsp.definitions = function(opts)
@@ -105,21 +102,19 @@ end
lsp.document_symbols = function(opts) lsp.document_symbols = function(opts)
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, opts.timeout or 10000) vim.lsp.buf_request(0, "textDocument/documentSymbol", params, function(err, result, _ctx, _config)
if err then if err then
vim.api.nvim_err_writeln("Error when finding document symbols: " .. err) vim.api.nvim_err_writeln("Error when finding document symbols: " .. err)
return return
end end
if not results_lsp or vim.tbl_isempty(results_lsp) then if not result or vim.tbl_isempty(result) then
print "No results from textDocument/documentSymbol" print "No results from textDocument/documentSymbol"
return return
end end
local locations = {} local locations = {}
for _, server_results in pairs(results_lsp) do vim.list_extend(locations, vim.lsp.util.symbols_to_items(result, 0) or {})
vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {})
end
locations = utils.filter_symbols(locations, opts) locations = utils.filter_symbols(locations, opts)
if locations == nil then if locations == nil then
@@ -144,6 +139,7 @@ lsp.document_symbols = function(opts)
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
}, },
}):find() }):find()
end)
end end
lsp.code_actions = function(opts) lsp.code_actions = function(opts)
@@ -337,7 +333,7 @@ end
lsp.workspace_symbols = function(opts) lsp.workspace_symbols = function(opts)
local params = { query = opts.query or "" } local params = { query = opts.query or "" }
local results_lsp, err = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 10000) vim.lsp.buf_request(0, "workspace/symbol", params, function(err, server_result, _ctx, _config)
if err then if err then
vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. err) vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. err)
return return
@@ -345,12 +341,10 @@ lsp.workspace_symbols = function(opts)
local locations = {} local locations = {}
if results_lsp and not vim.tbl_isempty(results_lsp) then if server_result and not vim.tbl_isempty(server_result) then
for _, server_results in pairs(results_lsp) do
-- Some LSPs (like Clangd and intelephense) might return { { result = {} } }, so make sure we have result -- Some LSPs (like Clangd and intelephense) might return { { result = {} } }, so make sure we have result
if server_results and server_results.result and not vim.tbl_isempty(server_results.result) then if server_result and server_result.result and not vim.tbl_isempty(server_result.result) then
vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_result.result, 0) or {})
end
end end
end end
@@ -382,6 +376,7 @@ lsp.workspace_symbols = function(opts)
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
}, },
}):find() }):find()
end)
end end
local function get_workspace_symbols_requester(bufnr, opts) local function get_workspace_symbols_requester(bufnr, opts)