Skip to content

Commit

Permalink
refactor(lsp): call hierarchy functions
Browse files Browse the repository at this point in the history
Add warning message when there are no hierarchy (return of
`prepareCallHierarchy` is nil or empty list).
  • Loading branch information
jamestrew committed Sep 12, 2024
1 parent 1398e11 commit 292619c
Showing 1 changed file with 29 additions and 12 deletions.
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

0 comments on commit 292619c

Please sign in to comment.