Skip to content

Commit

Permalink
Make list_or_jump listen to multiple lsps at once
Browse files Browse the repository at this point in the history
  • Loading branch information
enriclluelles committed Jul 22, 2024
1 parent d23f376 commit dff296d
Showing 1 changed file with 17 additions and 97 deletions.
114 changes: 17 additions & 97 deletions lua/telescope/builtin/__lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ end
---@param funname string: name of the calling function
---@param params lsp.TextDocumentPositionParams
---@param opts table
local function list_or_jump_all(action, title, funname, params, opts)
local function list_or_jump(action, title, funname, params, opts)
opts.reuse_win = vim.F.if_nil(opts.reuse_win, false)
opts.curr_filepath = vim.api.nvim_buf_get_name(opts.bufnr)

Expand All @@ -188,25 +188,23 @@ local function list_or_jump_all(action, title, funname, params, opts)
return
end

if result == nil then
return
end
if result ~= nil then
local locations = {}

local locations = {}
if not utils.islist(result) then
vim.list_extend(locations, { result })
else
vim.list_extend(locations, result)
end

if not utils.islist(result) then
vim.list_extend(locations, { result })
else
vim.list_extend(locations, result)
end
local offset_encoding = vim.lsp.get_client_by_id(client_id).offset_encoding

local offset_encoding = vim.lsp.get_client_by_id(client_id).offset_encoding
if not vim.tbl_isempty(result) then
first_encoding = offset_encoding
end

if not vim.tbl_isempty(result) then
first_encoding = offset_encoding
vim.list_extend(items, vim.lsp.util.locations_to_items(locations, offset_encoding))
end

vim.list_extend(items, vim.lsp.util.locations_to_items(locations, offset_encoding))
end

items = apply_action_handler(action, items, opts)
Expand Down Expand Up @@ -241,83 +239,6 @@ local function list_or_jump_all(action, title, funname, params, opts)

local location = item_to_location(item, first_encoding)
vim.lsp.util.jump_to_location(location, first_encoding, opts.reuse_win)
else
pickers
.new(opts, {
prompt_title = title,
finder = finders.new_table {
results = items,
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts),
push_cursor_on_edit = true,
push_tagstack_on_edit = true,
})
:find()
end
end)
end

---@param action telescope.lsp.list_or_jump_action
---@param title string prompt title
---@param funname string: name of the calling function
---@param params lsp.TextDocumentPositionParams
---@param opts table
local function list_or_jump(action, title, funname, params, opts)
opts.reuse_win = vim.F.if_nil(opts.reuse_win, false)
opts.curr_filepath = vim.api.nvim_buf_get_name(opts.bufnr)

vim.lsp.buf_request(opts.bufnr, action, params, function(err, result, ctx, _)
if err then
vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err.message)
return
end

if result == nil then
return
end

local locations = {}
if not utils.islist(result) then
locations = { result }
end
vim.list_extend(locations, result)

local offset_encoding = vim.lsp.get_client_by_id(ctx.client_id).offset_encoding
local items = vim.lsp.util.locations_to_items(locations, offset_encoding)
items = apply_action_handler(action, items, opts)
items = filter_file_ignore_patters(items, opts)

if vim.tbl_isempty(items) then
utils.notify(funname, {
msg = string.format("No %s found", title),
level = "INFO",
})
return
end

if #items == 1 and opts.jump_type ~= "never" then
local item = items[1]
if opts.curr_filepath ~= item.filename then
local cmd
if opts.jump_type == "tab" then
cmd = "tabedit"
elseif opts.jump_type == "split" then
cmd = "new"
elseif opts.jump_type == "vsplit" then
cmd = "vnew"
elseif opts.jump_type == "tab drop" then
cmd = "tab drop"
end

if cmd then
vim.cmd(string.format("%s %s", cmd, item.filename))
end
end

local location = item_to_location(item, offset_encoding)
vim.lsp.util.jump_to_location(location, offset_encoding, opts.reuse_win)
else
pickers
.new(opts, {
Expand All @@ -340,17 +261,17 @@ lsp.references = function(opts)
opts.include_current_line = vim.F.if_nil(opts.include_current_line, false)
local params = vim.lsp.util.make_position_params(opts.winnr)
params.context = { includeDeclaration = vim.F.if_nil(opts.include_declaration, true) }
return list_or_jump_all("textDocument/references", "LSP References", "builtin.lsp_references", params, opts)
return list_or_jump("textDocument/references", "LSP References", "builtin.lsp_references", params, opts)
end

lsp.definitions = function(opts)
local params = vim.lsp.util.make_position_params(opts.winnr)
return list_or_jump_all("textDocument/definition", "LSP Definitions", "builtin.lsp_definitions", params, opts)
return list_or_jump("textDocument/definition", "LSP Definitions", "builtin.lsp_definitions", params, opts)
end

lsp.type_definitions = function(opts)
local params = vim.lsp.util.make_position_params(opts.winnr)
return list_or_jump_all(
return list_or_jump(
"textDocument/typeDefinition",
"LSP Type Definitions",
"builtin.lsp_type_definitions",
Expand All @@ -361,8 +282,7 @@ end

lsp.implementations = function(opts)
local params = vim.lsp.util.make_position_params(opts.winnr)
return list_or_jump_all("textDocument/implementation", "LSP Implementations", "builtin.lsp_implementations", params,
opts)
return list_or_jump("textDocument/implementation", "LSP Implementations", "builtin.lsp_implementations", params, opts)
end

local symbols_sorter = function(symbols)
Expand Down

0 comments on commit dff296d

Please sign in to comment.