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
|
2022-06-28 04:40:19 +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()
|
2023-11-07 12:00:36 +00:00
|
|
|
local uv = vim.uv or vim.loop
|
|
|
|
local cwd = uv.cwd()
|
2022-06-28 04:40:19 +00:00
|
|
|
local in_method = 'callHierarchy/incomingCalls'
|
|
|
|
local out_method = 'callHierarchy/outgoingCalls'
|
|
|
|
|
|
|
|
local lsp_method = { to = out_method, from = in_method }
|
|
|
|
local panel_method = { to = out_method, from = in_method }
|
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
local M = {}
|
2022-06-21 15:56:17 +00:00
|
|
|
local outgoing_calls_handler
|
|
|
|
local incoming_calls_handler
|
2022-06-28 04:40:19 +00:00
|
|
|
local hierarchy_handler
|
|
|
|
|
|
|
|
local call_hierarchy
|
|
|
|
|
|
|
|
local function pick_call_hierarchy_item(call_hierarchy_items)
|
|
|
|
if not call_hierarchy_items then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if #call_hierarchy_items == 1 then
|
|
|
|
return call_hierarchy_items[1]
|
|
|
|
end
|
|
|
|
local items = {}
|
|
|
|
for i, item in pairs(call_hierarchy_items) do
|
|
|
|
local entry = item.detail or item.name
|
|
|
|
table.insert(items, string.format('%d. %s', i, entry))
|
|
|
|
end
|
|
|
|
local choice = vim.fn.inputlist(items)
|
|
|
|
if choice < 1 or choice > #items then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return choice
|
|
|
|
end
|
2022-06-21 15:56:17 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
-- convert lsp result to navigator items
|
|
|
|
local function call_hierarchy_result_procesor(direction, err, result, ctx, config)
|
|
|
|
math.randomseed(os.clock() * 100000000000)
|
|
|
|
trace(direction, err, ctx, config)
|
|
|
|
trace(result)
|
2021-10-17 06:36:07 +00:00
|
|
|
if not result then
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.notify('No call hierarchy items found', vim.log.levels.WARN)
|
2021-10-17 06:36:07 +00:00
|
|
|
return
|
|
|
|
end
|
2022-06-21 15:56:17 +00:00
|
|
|
-- trace('call_hierarchy', result)
|
2022-04-08 07:01:40 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local bufnr = ctx.bufnr or vim.api.nvim_get_current_buf()
|
2022-10-26 05:03:48 +00:00
|
|
|
assert(next(vim.lsp.get_active_clients({buffer = bufnr})), 'Must have a client running to use call hierarchy')
|
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)
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.notify('ERROR: ' .. err, vim.log.levels.WARN)
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-06-21 15:56:17 +00:00
|
|
|
local items = ctx.items or {}
|
2021-06-23 14:09:33 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local kind = ' '
|
2022-06-21 15:56:17 +00:00
|
|
|
for _, call_hierarchy_result in pairs(result) do
|
|
|
|
local call_hierarchy_item = call_hierarchy_result[direction]
|
2021-04-19 02:56:32 +00:00
|
|
|
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
|
|
|
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', ' ↳ ')
|
2022-06-28 04:40:19 +00:00
|
|
|
trace(call_hierarchy_item)
|
2021-10-17 04:29:17 +00:00
|
|
|
|
2022-06-21 15:56:17 +00:00
|
|
|
local disp_item = vim.tbl_deep_extend('force', {}, call_hierarchy_item)
|
|
|
|
disp_item = vim.tbl_deep_extend('force', disp_item, {
|
2021-10-17 04:29:17 +00:00
|
|
|
filename = filename,
|
|
|
|
display_filename = display_filename,
|
2022-06-28 04:40:19 +00:00
|
|
|
indent_level = ctx.depth or 1,
|
|
|
|
method = lsp_method[direction],
|
|
|
|
node_text = call_hierarchy_item.name,
|
|
|
|
type = kind,
|
|
|
|
id = math.random(1, 100000),
|
2021-10-17 04:29:17 +00:00
|
|
|
text = kind .. call_hierarchy_item.name .. ' ﰲ ' .. call_hierarchy_item.detail,
|
2022-06-21 15:56:17 +00:00
|
|
|
lnum = call_hierarchy_item.selectionRange.start.line + 1,
|
|
|
|
col = call_hierarchy_item.selectionRange.start.character,
|
|
|
|
})
|
2021-10-17 04:29:17 +00:00
|
|
|
table.insert(items, disp_item)
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
2022-06-28 04:40:19 +00:00
|
|
|
trace(items)
|
2021-04-19 02:56:32 +00:00
|
|
|
return items
|
|
|
|
end
|
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local call_hierarchy_handler_from = partial(call_hierarchy_result_procesor, 'from')
|
|
|
|
local call_hierarchy_handler_to = partial(call_hierarchy_result_procesor, 'to')
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
-- the handler that deal all lsp request
|
|
|
|
hierarchy_handler = function(dir, handler, show, api, err, result, ctx, cfg)
|
|
|
|
trace(dir, handler, api, show, err, result, ctx, cfg)
|
|
|
|
ctx = ctx or {} -- can be nil if it is async call
|
|
|
|
cfg = cfg or {}
|
2022-07-26 14:24:40 +00:00
|
|
|
local opts = ctx.opts or {}
|
2022-06-28 04:40:19 +00:00
|
|
|
vim.validate({ handler = { handler, 'function' }, show = { show, 'function' }, api = { api, 'string' } })
|
|
|
|
local bufnr = ctx.bufnr or vim.api.nvim_get_current_buf()
|
2022-10-26 05:03:48 +00:00
|
|
|
assert(next(vim.lsp.get_active_clients({buffer = bufnr})), 'Must have a client running to use lsp hierarchy')
|
2022-06-28 04:40:19 +00:00
|
|
|
|
|
|
|
local results = handler(err, result, ctx, cfg, 'Incoming calls not found')
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2022-06-21 15:56:17 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr or vim.api.nvim_get_current_buf(), 'ft')
|
|
|
|
if ctx.no_show then
|
|
|
|
return results
|
|
|
|
end
|
2022-06-28 04:40:19 +00:00
|
|
|
-- local panel = args.panel
|
|
|
|
-- local items = args.items
|
|
|
|
-- local parent_node = args.node
|
|
|
|
-- local section_id = args.section_id or 1
|
|
|
|
local show_args = {
|
|
|
|
items = results,
|
|
|
|
ft = ft,
|
|
|
|
api = api,
|
|
|
|
bufnr = bufnr,
|
|
|
|
panel = opts.panel,
|
|
|
|
parent_node = opts.parent_node,
|
2023-02-14 12:58:38 +00:00
|
|
|
title = 'Call Hierarchy',
|
2022-06-28 04:40:19 +00:00
|
|
|
}
|
|
|
|
local win = show(show_args)
|
2022-06-21 15:56:17 +00:00
|
|
|
return results, win
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local make_params = function(uri, pos)
|
|
|
|
return {
|
|
|
|
textDocument = {
|
|
|
|
uri = uri,
|
|
|
|
},
|
|
|
|
position = pos,
|
|
|
|
}
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local function display_panel(args)
|
|
|
|
-- args = {items=results, ft=ft, api=api}
|
|
|
|
log(args)
|
|
|
|
|
|
|
|
local Panel = require('guihua.panel')
|
|
|
|
local bufnr = args.bufnr or vim.api.nvim_get_current_buf()
|
2022-07-26 14:24:40 +00:00
|
|
|
-- local ft = args.ft or vim.api.nvim_buf_get_option(bufnr, 'buftype')
|
2022-06-28 04:40:19 +00:00
|
|
|
local items = args.items
|
|
|
|
local p = Panel:new({
|
|
|
|
header = args.header or 'Call Hierarchy',
|
2022-07-26 14:24:40 +00:00
|
|
|
render = function(buf)
|
|
|
|
log(buf)
|
2022-06-28 04:40:19 +00:00
|
|
|
return items
|
|
|
|
end,
|
|
|
|
fold = function(panel, node)
|
|
|
|
if node.expanded ~= nil then
|
|
|
|
node.expanded = not node.expanded
|
|
|
|
vim.cmd('normal! za')
|
|
|
|
else
|
|
|
|
expand(panel, node)
|
|
|
|
node.expanded = true
|
|
|
|
end
|
|
|
|
log('fold')
|
|
|
|
return node
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
p:open(true)
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local function expand_item(args)
|
|
|
|
-- args = {items=results, ft=ft, api=api}
|
|
|
|
print('dispaly panel')
|
|
|
|
trace(args, args.parent_node)
|
|
|
|
local panel = args.panel
|
|
|
|
local items = args.items
|
|
|
|
local parent_node = args.parent_node
|
|
|
|
local section_id = args.section_id or 1
|
|
|
|
|
|
|
|
local sect
|
|
|
|
local sectid = 1
|
|
|
|
for i, s in pairs(panel.sections) do
|
|
|
|
if s.id == section_id then
|
|
|
|
sectid = i
|
|
|
|
break
|
|
|
|
end
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
2022-06-28 04:40:19 +00:00
|
|
|
sect = panel.sections[sectid]
|
|
|
|
for i, node in pairs(sect.nodes) do
|
|
|
|
if node.id == parent_node.id then
|
|
|
|
for j in ipairs(items) do
|
|
|
|
items[j].indent_level = parent_node.indent_level + 1
|
|
|
|
table.insert(sect.nodes, i + j, args.items[j])
|
|
|
|
end
|
|
|
|
sect.nodes[i].expanded = true
|
|
|
|
sect.nodes[i].expandable = false
|
|
|
|
break
|
|
|
|
end
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
2022-06-28 04:40:19 +00:00
|
|
|
trace(panel.sections[sectid])
|
|
|
|
-- render the panel again
|
|
|
|
panel:redraw(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
incoming_calls_handler = util.partial4(
|
|
|
|
hierarchy_handler,
|
|
|
|
'from',
|
|
|
|
call_hierarchy_handler_from,
|
|
|
|
gui.new_list_view,
|
|
|
|
' '
|
|
|
|
)
|
|
|
|
outgoing_calls_handler = util.partial4(hierarchy_handler, 'to', call_hierarchy_handler_to, gui.new_list_view, ' ')
|
|
|
|
|
|
|
|
local incoming_calls_panel = util.partial4(
|
|
|
|
hierarchy_handler,
|
|
|
|
'from',
|
|
|
|
call_hierarchy_handler_from,
|
|
|
|
display_panel,
|
|
|
|
' '
|
|
|
|
)
|
|
|
|
local outgoing_calls_panel = util.partial4(hierarchy_handler, 'to', call_hierarchy_handler_to, display_panel, ' ')
|
|
|
|
|
|
|
|
local incoming_calls_expand = util.partial4(hierarchy_handler, 'from', call_hierarchy_handler_from, expand_item, ' ')
|
|
|
|
local outgoing_calls_expand = util.partial4(hierarchy_handler, 'to', call_hierarchy_handler_to, expand_item, ' ')
|
|
|
|
|
|
|
|
function expand(panel, node)
|
|
|
|
trace(panel, node)
|
|
|
|
local params = make_params(node.uri, {
|
|
|
|
line = node.range.start.line,
|
|
|
|
character = node.range.start.character,
|
|
|
|
})
|
|
|
|
local handler = incoming_calls_expand
|
|
|
|
if node.api == out_method then
|
|
|
|
handler = outgoing_calls_expand
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
2022-06-28 04:40:19 +00:00
|
|
|
|
|
|
|
local bufnr = vim.uri_to_bufnr(node.uri)
|
|
|
|
call_hierarchy(node.method, {
|
|
|
|
params = params,
|
|
|
|
panel = panel,
|
|
|
|
parent_node = node,
|
|
|
|
handler = handler,
|
|
|
|
bufnr = bufnr,
|
|
|
|
})
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
local request = vim.lsp.buf_request
|
|
|
|
|
|
|
|
-- call_hierarchy with floating window
|
|
|
|
call_hierarchy = function(method, opts)
|
|
|
|
trace(method, opts)
|
2022-06-21 15:56:17 +00:00
|
|
|
opts = opts or {}
|
2022-06-28 04:40:19 +00:00
|
|
|
local params = opts.params or vim.lsp.util.make_position_params()
|
|
|
|
local bufnr = opts.bufnr
|
|
|
|
local handler = function(err, result, ctx, cfg)
|
|
|
|
ctx.opts = opts
|
|
|
|
return opts.handler(err, result, ctx, cfg)
|
|
|
|
end
|
|
|
|
-- log(opts, params)
|
2022-07-03 11:29:00 +00:00
|
|
|
return request(
|
2022-06-28 04:40:19 +00:00
|
|
|
bufnr,
|
2022-06-21 15:56:17 +00:00
|
|
|
'textDocument/prepareCallHierarchy',
|
|
|
|
params,
|
|
|
|
vim.lsp.with(function(err, result, ctx)
|
|
|
|
if err then
|
|
|
|
vim.notify(err.message, vim.log.levels.WARN)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local call_hierarchy_item = pick_call_hierarchy_item(result)
|
|
|
|
local client = vim.lsp.get_client_by_id(ctx.client_id)
|
|
|
|
if client then
|
2022-06-28 04:40:19 +00:00
|
|
|
trace('result', result, 'items', call_hierarchy_item, method, ctx, client.name)
|
|
|
|
client.request(method, {
|
|
|
|
item = call_hierarchy_item,
|
|
|
|
args = {
|
|
|
|
method = method,
|
|
|
|
},
|
|
|
|
}, handler, ctx.bufnr)
|
2022-06-21 15:56:17 +00:00
|
|
|
else
|
2022-06-28 04:40:19 +00:00
|
|
|
vim.notify(string.format('Client with id=%d stopped', ctx.client_id), vim.log.levels.WARN)
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
|
|
|
end, { direction = method, depth = opts.depth })
|
|
|
|
)
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2022-06-21 15:56:17 +00:00
|
|
|
function M.incoming_calls(opts)
|
2022-06-28 04:40:19 +00:00
|
|
|
call_hierarchy(in_method, opts)
|
2022-06-21 15:56:17 +00:00
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-06-21 15:56:17 +00:00
|
|
|
function M.outgoing_calls(opts)
|
2022-06-28 04:40:19 +00:00
|
|
|
call_hierarchy(out_method, opts)
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
function M.incoming_calls_panel(opts)
|
|
|
|
opts = vim.tbl_extend('force', { handler = incoming_calls_panel }, opts or {})
|
|
|
|
call_hierarchy(in_method, opts)
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
function M.outgoing_calls_panel(opts)
|
|
|
|
opts = vim.tbl_extend('force', { handler = outgoing_calls_panel }, opts or {})
|
|
|
|
call_hierarchy(out_method, opts)
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2022-06-28 04:40:19 +00:00
|
|
|
M.incoming_calls_handler = incoming_calls_handler
|
|
|
|
M.outgoing_calls_handler = outgoing_calls_handler
|
2022-07-03 11:29:00 +00:00
|
|
|
-- for testing
|
|
|
|
M._call_hierarchy = call_hierarchy
|
2022-06-28 04:40:19 +00:00
|
|
|
|
|
|
|
function M.calltree(args)
|
|
|
|
if args == '-o' then
|
|
|
|
return M.outgoing_calls_panel()
|
|
|
|
end
|
|
|
|
M.incoming_calls_panel()
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
return M
|