2022-04-08 07:01:40 +00:00
|
|
|
local gui = require('navigator.gui')
|
|
|
|
local util = require('navigator.util')
|
2021-04-19 02:56:32 +00:00
|
|
|
local log = util.log
|
2021-09-24 06:46:17 +00:00
|
|
|
local trace = util.trace
|
2021-05-17 03:15:15 +00:00
|
|
|
local partial = util.partial
|
2022-04-08 07:01:40 +00:00
|
|
|
local lsphelper = require('navigator.lspwrapper')
|
2021-06-25 00:33:04 +00:00
|
|
|
|
2022-04-08 07:01:40 +00:00
|
|
|
local path_sep = require('navigator.util').path_sep()
|
|
|
|
local path_cur = require('navigator.util').path_cur()
|
2021-09-09 02:59:58 +00:00
|
|
|
local cwd = vim.loop.cwd()
|
2021-04-19 02:56:32 +00:00
|
|
|
local M = {}
|
2022-06-01 15:41:26 +00:00
|
|
|
local function call_hierarchy_handler(direction, err, result, ctx, _, error_message)
|
2021-10-17 06:36:07 +00:00
|
|
|
if not result then
|
2022-04-08 07:01:40 +00:00
|
|
|
vim.notify('No call hierarchy items found', vim.lsp.log_levels.WARN)
|
2021-10-17 06:36:07 +00:00
|
|
|
return
|
|
|
|
end
|
2021-09-24 06:46:17 +00:00
|
|
|
trace('call_hierarchy', result)
|
2022-04-08 07:01:40 +00:00
|
|
|
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
|
2021-04-19 02:56:32 +00:00
|
|
|
if err ~= nil then
|
2022-04-08 07:01:40 +00:00
|
|
|
log('dir', direction, 'result', result, 'err', err, ctx)
|
|
|
|
vim.notify('ERROR: ' .. error_message, vim.lsp.log_levels.WARN)
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local items = {}
|
2021-06-23 14:09:33 +00:00
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
for _, call_hierarchy_call in pairs(result) do
|
|
|
|
local call_hierarchy_item = call_hierarchy_call[direction]
|
|
|
|
local kind = ' '
|
|
|
|
if call_hierarchy_item.kind then
|
2022-04-08 07:01:40 +00:00
|
|
|
kind = require('navigator.lspclient.lspkind').symbol_kind(call_hierarchy_item.kind) .. ' '
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
2021-10-17 04:29:17 +00:00
|
|
|
-- for _, range in pairs(call_hierarchy_call.fromRanges) do
|
2022-06-01 15:41:26 +00:00
|
|
|
local range = call_hierarchy_item.range or call_hierarchy_item.selectionRange
|
2021-10-17 04:29:17 +00:00
|
|
|
local filename = assert(vim.uri_to_fname(call_hierarchy_item.uri))
|
|
|
|
local display_filename = filename:gsub(cwd .. path_sep, path_cur, 1)
|
2022-04-08 07:01:40 +00:00
|
|
|
call_hierarchy_item.detail = call_hierarchy_item.detail or ''
|
2022-06-01 15:41:26 +00:00
|
|
|
call_hierarchy_item.detail = string.gsub(call_hierarchy_item.detail, '\n', ' ↳ ')
|
2021-10-17 04:29:17 +00:00
|
|
|
trace(range, call_hierarchy_item)
|
|
|
|
|
|
|
|
local disp_item = {
|
|
|
|
uri = call_hierarchy_item.uri,
|
|
|
|
filename = filename,
|
|
|
|
display_filename = display_filename,
|
|
|
|
text = kind .. call_hierarchy_item.name .. ' ﰲ ' .. call_hierarchy_item.detail,
|
|
|
|
range = range,
|
|
|
|
lnum = range.start.line + 1,
|
2022-04-08 07:01:40 +00:00
|
|
|
col = range.start.character,
|
2021-10-17 04:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table.insert(items, disp_item)
|
|
|
|
-- end
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
return items
|
|
|
|
end
|
|
|
|
|
2022-04-08 07:01:40 +00:00
|
|
|
local call_hierarchy_handler_from = partial(call_hierarchy_handler, 'from')
|
|
|
|
local call_hierarchy_handler_to = partial(call_hierarchy_handler, 'to')
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-06-01 15:41:26 +00:00
|
|
|
-- local function incoming_calls_handler(bang, err, result, ctx, cfg)
|
|
|
|
local function incoming_calls_handler(_, err, result, ctx, cfg)
|
2022-04-08 07:01:40 +00:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp hierarchy')
|
|
|
|
local results = call_hierarchy_handler_from(err, result, ctx, cfg, 'Incoming calls not found')
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2022-04-08 07:01:40 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
|
|
|
|
gui.new_list_view({ items = results, ft = ft, api = ' ' })
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2022-06-01 15:41:26 +00:00
|
|
|
local function outgoing_calls_handler(_, err, result, ctx, cfg)
|
2022-04-08 07:01:40 +00:00
|
|
|
local results = call_hierarchy_handler_to(err, result, ctx, cfg, 'Outgoing calls not found')
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-04-08 07:01:40 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
|
|
|
|
gui.new_list_view({ items = results, ft = ft, api = ' ' })
|
2021-05-17 03:15:15 +00:00
|
|
|
-- fzf_locations(bang, "", "Outgoing Calls", results, false)
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.incoming_calls(bang, opts)
|
2022-04-08 07:01:40 +00:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp hierarchy')
|
2022-05-15 13:13:01 +00:00
|
|
|
if not lsphelper.check_capabilities('callHierarchyProvider') then
|
2021-05-21 11:39:46 +00:00
|
|
|
return
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
|
|
|
local params = vim.lsp.util.make_position_params()
|
2022-04-08 07:01:40 +00:00
|
|
|
lsphelper.call_sync('callHierarchy/incomingCalls', params, opts, partial(incoming_calls_handler, bang))
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.outgoing_calls(bang, opts)
|
2022-04-08 07:01:40 +00:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
|
2022-05-15 13:13:01 +00:00
|
|
|
if not lsphelper.check_capabilities('callHierarchyProvider') then
|
2021-05-21 11:39:46 +00:00
|
|
|
return
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
|
|
|
local params = vim.lsp.util.make_position_params()
|
2022-04-08 07:01:40 +00:00
|
|
|
lsphelper.call_sync('callHierarchy/outgoingCalls', params, opts, partial(outgoing_calls_handler, bang))
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.incoming_calls_call = partial(M.incoming_calls, 0)
|
|
|
|
M.outgoing_calls_call = partial(M.outgoing_calls, 0)
|
|
|
|
|
|
|
|
M.incoming_calls_handler = partial(incoming_calls_handler, 0)
|
|
|
|
M.outgoing_calls_handler = partial(outgoing_calls_handler, 0)
|
|
|
|
|
|
|
|
return M
|