navigator.lua/lua/navigator/hierarchy.lua

96 lines
3.5 KiB
Lua
Raw Normal View History

2021-04-19 02:56:32 +00:00
local gui = require "navigator.gui"
local util = require "navigator.util"
local log = util.log
local partial = util.partial
2021-04-19 02:56:32 +00:00
local lsphelper = require "navigator.lspwrapper"
2021-06-25 00:33:04 +00:00
local path_sep = require"navigator.util".path_sep()
local path_cur = require"navigator.util".path_cur()
2021-04-19 02:56:32 +00:00
local cwd = vim.fn.getcwd(0)
local M = {}
local function call_hierarchy_handler(direction, err, _, result, _, _, error_message)
2021-06-23 14:09:33 +00:00
log('call_hierarchy')
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp_tags")
2021-04-19 02:56:32 +00:00
if err ~= nil then
log("dir", direction, "result", result, "err", err)
2021-04-19 02:56:32 +00:00
print("ERROR: " .. error_message)
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
kind = require'navigator.lspclient.lspkind'.symbol_kind(call_hierarchy_item.kind) .. ' '
2021-04-19 02:56:32 +00:00
end
for _, range in pairs(call_hierarchy_call.fromRanges) do
local filename = assert(vim.uri_to_fname(call_hierarchy_item.uri))
2021-06-25 00:33:04 +00:00
local display_filename = filename:gsub(cwd .. path_sep, path_cur, 1)
table.insert(items, {
uri = call_hierarchy_item.uri,
filename = filename,
display_filename = call_hierarchy_item.detail or display_filename,
text = kind .. call_hierarchy_item.name,
range = range,
lnum = range.start.line,
col = range.start.character
})
2021-04-19 02:56:32 +00:00
end
end
return items
end
local call_hierarchy_handler_from = partial(call_hierarchy_handler, "from")
local call_hierarchy_handler_to = partial(call_hierarchy_handler, "to")
local function incoming_calls_handler(bang, err, method, result, client_id, bufnr)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp_tags")
local results = call_hierarchy_handler_from(err, method, result, client_id, bufnr,
"Incoming calls not found")
local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
gui.new_list_view({items = results, ft = ft, api = ''})
2021-04-19 02:56:32 +00:00
end
local function outgoing_calls_handler(bang, err, method, result, client_id, bufnr)
local results = call_hierarchy_handler_to(err, method, result, client_id, bufnr,
"Outgoing calls not found")
2021-04-19 02:56:32 +00:00
local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
gui.new_list_view({items = results, ft = ft, api = ''})
-- fzf_locations(bang, "", "Outgoing Calls", results, false)
2021-04-19 02:56:32 +00:00
end
function M.incoming_calls(bang, opts)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp_tags")
if not lsphelper.check_capabilities("call_hierarchy") then
return
end
2021-04-19 02:56:32 +00:00
local params = vim.lsp.util.make_position_params()
2021-06-23 14:09:33 +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)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp_tags")
if not lsphelper.check_capabilities("call_hierarchy") then
return
end
2021-04-19 02:56:32 +00:00
local params = vim.lsp.util.make_position_params()
2021-06-23 14:09:33 +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