Skip to content

Commit

Permalink
Refactor lsp.references to use the list_or_jump fn
Browse files Browse the repository at this point in the history
  • Loading branch information
jls83 committed Dec 22, 2022
1 parent e960efa commit 6f16402
Showing 1 changed file with 20 additions and 65 deletions.
85 changes: 20 additions & 65 deletions lua/telescope/builtin/__lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,6 @@ local utils = require "telescope.utils"

local lsp = {}

lsp.references = function(opts)
local filepath = vim.api.nvim_buf_get_name(opts.bufnr)
local lnum = vim.api.nvim_win_get_cursor(opts.winnr)[1]
local params = vim.lsp.util.make_position_params(opts.winnr)
local include_current_line = vim.F.if_nil(opts.include_current_line, false)
params.context = { includeDeclaration = vim.F.if_nil(opts.include_declaration, true) }

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

local locations = {}
if result then
local results = vim.lsp.util.locations_to_items(result, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding)
if not include_current_line then
locations = vim.tbl_filter(function(v)
-- Remove current line from result
return not (v.filename == filepath and v.lnum == lnum)
end, vim.F.if_nil(results, {}))
else
locations = vim.F.if_nil(results, {})
end
end

if vim.tbl_isempty(locations) then
return
end

if #locations == 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
-- jump to location
local location = locations[1]
local bufnr = opts.bufnr
if location.filename then
bufnr = vim.uri_to_bufnr(vim.uri_from_fname(location.filename))
end
vim.api.nvim_win_set_buf(0, bufnr)
vim.api.nvim_win_set_cursor(0, { location.lnum, location.col - 1 })
return
end

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),
push_cursor_on_edit = true,
push_tagstack_on_edit = true,
})
:find()
end)
end

local function call_hierarchy(opts, method, title, direction, item)
vim.lsp.buf_request(opts.bufnr, method, { item = item }, function(err, result)
if err then
Expand Down Expand Up @@ -179,6 +114,10 @@ local function list_or_jump(action, title, opts)
vim.list_extend(flattened_results, result)
end

if not vim.F.if_nil(opts.results_filter) then
flattened_results = vim.tbl_filter(opts.results_filter, flattened_results)
end

local offset_encoding = vim.lsp.get_client_by_id(ctx.client_id).offset_encoding

if #flattened_results == 0 then
Expand Down Expand Up @@ -211,6 +150,22 @@ local function list_or_jump(action, title, opts)
end)
end

lsp.references = function(opts)
local filepath = vim.api.nvim_buf_get_name(opts.bufnr)
local lnum = vim.api.nvim_win_get_cursor(opts.winnr)[1]
local include_current_line = vim.F.if_nil(opts.include_current_line, false)

-- If we shouldn't include the current line, add a function to filter it out
-- from the LSP results.
if not include_current_line then
opts.results_filter = function(v)
return not (v.filename == filepath and v.lnum == lnum)
end
end

return list_or_jump("textDocument/references", "LSP References", opts)
end

lsp.definitions = function(opts)
return list_or_jump("textDocument/definition", "LSP Definitions", opts)
end
Expand Down

0 comments on commit 6f16402

Please sign in to comment.