fix: make lsp_* async (#1506)
This commit is contained in:
@@ -16,71 +16,23 @@ 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
|
|
||||||
|
|
||||||
local locations = {}
|
|
||||||
for _, server_results in pairs(results_lsp) do
|
|
||||||
if server_results.result then
|
|
||||||
vim.list_extend(locations, vim.lsp.util.locations_to_items(server_results.result) or {})
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if vim.tbl_isempty(locations) then
|
local locations = {}
|
||||||
return
|
if result then
|
||||||
end
|
vim.list_extend(locations, vim.lsp.util.locations_to_items(result) or {})
|
||||||
|
|
||||||
pickers.new(opts, {
|
|
||||||
prompt_title = "LSP References",
|
|
||||||
finder = finders.new_table {
|
|
||||||
results = locations,
|
|
||||||
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
|
|
||||||
},
|
|
||||||
previewer = conf.qflist_previewer(opts),
|
|
||||||
sorter = conf.generic_sorter(opts),
|
|
||||||
}):find()
|
|
||||||
end
|
|
||||||
|
|
||||||
local function list_or_jump(action, title, opts)
|
|
||||||
opts = opts or {}
|
|
||||||
|
|
||||||
local params = vim.lsp.util.make_position_params()
|
|
||||||
local result, err = vim.lsp.buf_request_sync(0, action, params, opts.timeout or 10000)
|
|
||||||
if err then
|
|
||||||
vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local flattened_results = {}
|
|
||||||
for _, server_results in pairs(result) do
|
|
||||||
if server_results.result then
|
|
||||||
-- textDocument/definition can return Location or Location[]
|
|
||||||
if not vim.tbl_islist(server_results.result) then
|
|
||||||
flattened_results = { server_results.result }
|
|
||||||
break
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.list_extend(flattened_results, server_results.result)
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if #flattened_results == 0 then
|
if vim.tbl_isempty(locations) then
|
||||||
return
|
return
|
||||||
elseif #flattened_results == 1 and opts.jump_type ~= "never" then
|
|
||||||
if opts.jump_type == "tab" then
|
|
||||||
vim.cmd "tabedit"
|
|
||||||
elseif opts.jump_type == "split" then
|
|
||||||
vim.cmd "new"
|
|
||||||
elseif opts.jump_type == "vsplit" then
|
|
||||||
vim.cmd "vnew"
|
|
||||||
end
|
end
|
||||||
vim.lsp.util.jump_to_location(flattened_results[1])
|
|
||||||
else
|
|
||||||
local locations = vim.lsp.util.locations_to_items(flattened_results)
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = title,
|
prompt_title = "LSP References",
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
results = locations,
|
results = locations,
|
||||||
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
|
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
|
||||||
@@ -88,7 +40,52 @@ local function list_or_jump(action, title, 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)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
local params = vim.lsp.util.make_position_params()
|
||||||
|
vim.lsp.buf_request(0, action, params, function(err, result, _ctx, _config)
|
||||||
|
if err then
|
||||||
|
vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local flattened_results = {}
|
||||||
|
if result then
|
||||||
|
-- textDocument/definition can return Location or Location[]
|
||||||
|
if not vim.tbl_islist(result) then
|
||||||
|
flattened_results = { result }
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.list_extend(flattened_results, result)
|
||||||
|
end
|
||||||
|
|
||||||
|
if #flattened_results == 0 then
|
||||||
|
return
|
||||||
|
elseif #flattened_results == 1 and opts.jump_type ~= "never" then
|
||||||
|
if opts.jump_type == "tab" then
|
||||||
|
vim.cmd "tabedit"
|
||||||
|
elseif opts.jump_type == "split" then
|
||||||
|
vim.cmd "new"
|
||||||
|
elseif opts.jump_type == "vsplit" then
|
||||||
|
vim.cmd "vnew"
|
||||||
|
end
|
||||||
|
vim.lsp.util.jump_to_location(flattened_results[1])
|
||||||
|
else
|
||||||
|
local locations = vim.lsp.util.locations_to_items(flattened_results)
|
||||||
|
pickers.new(opts, {
|
||||||
|
prompt_title = title,
|
||||||
|
finder = finders.new_table {
|
||||||
|
results = locations,
|
||||||
|
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
|
||||||
|
},
|
||||||
|
previewer = conf.qflist_previewer(opts),
|
||||||
|
sorter = conf.generic_sorter(opts),
|
||||||
|
}):find()
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
lsp.definitions = function(opts)
|
lsp.definitions = function(opts)
|
||||||
@@ -105,45 +102,44 @@ 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
|
||||||
-- error message already printed in `utils.filter_symbols`
|
-- error message already printed in `utils.filter_symbols`
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.tbl_isempty(locations) then
|
if vim.tbl_isempty(locations) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
opts.ignore_filename = opts.ignore_filename or true
|
opts.ignore_filename = opts.ignore_filename or true
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = "LSP Document Symbols",
|
prompt_title = "LSP Document Symbols",
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
results = locations,
|
results = locations,
|
||||||
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts),
|
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts),
|
||||||
},
|
},
|
||||||
previewer = conf.qflist_previewer(opts),
|
previewer = conf.qflist_previewer(opts),
|
||||||
sorter = conf.prefilter_sorter {
|
sorter = conf.prefilter_sorter {
|
||||||
tag = "symbol_type",
|
tag = "symbol_type",
|
||||||
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,51 +333,50 @@ 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
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
locations = utils.filter_symbols(locations, opts)
|
locations = utils.filter_symbols(locations, opts)
|
||||||
if locations == nil then
|
if locations == nil then
|
||||||
-- error message already printed in `utils.filter_symbols`
|
-- error message already printed in `utils.filter_symbols`
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.tbl_isempty(locations) then
|
if vim.tbl_isempty(locations) then
|
||||||
print(
|
print(
|
||||||
"No results from workspace/symbol. Maybe try a different query: "
|
"No results from workspace/symbol. Maybe try a different query: "
|
||||||
.. "Telescope lsp_workspace_symbols query=example"
|
.. "Telescope lsp_workspace_symbols query=example"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
opts.ignore_filename = utils.get_default(opts.ignore_filename, false)
|
opts.ignore_filename = utils.get_default(opts.ignore_filename, false)
|
||||||
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = "LSP Workspace Symbols",
|
prompt_title = "LSP Workspace Symbols",
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
results = locations,
|
results = locations,
|
||||||
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts),
|
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts),
|
||||||
},
|
},
|
||||||
previewer = conf.qflist_previewer(opts),
|
previewer = conf.qflist_previewer(opts),
|
||||||
sorter = conf.prefilter_sorter {
|
sorter = conf.prefilter_sorter {
|
||||||
tag = "symbol_type",
|
tag = "symbol_type",
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user