Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lsp): call hierarchy functions #3290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions lua/telescope/builtin/__lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,28 @@ local utils = require "telescope.utils"

local lsp = {}

local function call_hierarchy(opts, method, title, direction, item)
local function no_hierarchy_warning(funname, title)
utils.notify(funname, {
msg = string.format("No %s found", title),
level = "INFO",
})
end

---@param method string
---@param title string
---@param direction "from"|"to
---@param funname string
---@param item table
---@param opts table
local function call_hierarchy(method, title, direction, funname, item, opts)
vim.lsp.buf_request(opts.bufnr, method, { item = item }, function(err, result)
if err then
vim.api.nvim_err_writeln("Error handling " .. title .. ": " .. err.message)
return
end

if not result or vim.tbl_isempty(result) then
no_hierarchy_warning(funname, title)
return
end

Expand Down Expand Up @@ -50,9 +64,6 @@ local function call_hierarchy(opts, method, title, direction, item)
end

local function pick_call_hierarchy_item(call_hierarchy_items)
if not call_hierarchy_items or vim.tbl_isempty(call_hierarchy_items) then
return
end
if #call_hierarchy_items == 1 then
return call_hierarchy_items[1]
end
Expand All @@ -68,33 +79,39 @@ local function pick_call_hierarchy_item(call_hierarchy_items)
return call_hierarchy_items[choice]
end

local function calls(opts, direction)
---@param method string
---@param title string
---@param direction "from"|"to
---@param funname string
---@param opts table
local function calls(method, title, direction, funname, opts)
local params = vim.lsp.util.make_position_params()
vim.lsp.buf_request(opts.bufnr, "textDocument/prepareCallHierarchy", params, function(err, result)
if err then
vim.api.nvim_err_writeln("Error when preparing call hierarchy: " .. err)
return
end

if not result or vim.tbl_isempty(result) then
no_hierarchy_warning(funname, title)
return
end

local call_hierarchy_item = pick_call_hierarchy_item(result)
if not call_hierarchy_item then
return
end

if direction == "from" then
call_hierarchy(opts, "callHierarchy/incomingCalls", "LSP Incoming Calls", direction, call_hierarchy_item)
else
call_hierarchy(opts, "callHierarchy/outgoingCalls", "LSP Outgoing Calls", direction, call_hierarchy_item)
end
call_hierarchy(method, title, direction, funname, call_hierarchy_item, opts)
end)
end

lsp.incoming_calls = function(opts)
calls(opts, "from")
calls("callHierarchy/incomingCalls", "LSP Incoming Calls", "from", "builtin.lsp_incoming_calls", opts)
end

lsp.outgoing_calls = function(opts)
calls(opts, "to")
calls("callHierarchy/outgoingCalls", "LSP Outgoing Calls", "to", "builtin.lsp_outgoing_calls", opts)
end

--- convert `item` type back to something we can pass to `vim.lsp.util.jump_to_location`
Expand Down