navigator.lua/lua/navigator/cclshierarchy.lua

103 lines
3.5 KiB
Lua
Raw Normal View History

2022-01-18 05:25:48 +00:00
local gui = require('navigator.gui')
local util = require('navigator.util')
2021-06-23 14:09:33 +00:00
local log = util.log
local partial = util.partial
2022-01-18 05:25:48 +00:00
local lsphelper = require('navigator.lspwrapper')
local cwd = vim.loop.cwd()
2021-06-25 00:33:04 +00:00
2022-01-18 05:25:48 +00:00
local path_sep = require('navigator.util').path_sep()
local path_cur = require('navigator.util').path_cur()
2021-06-23 14:09:33 +00:00
local M = {}
local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_message)
2021-06-23 14:09:33 +00:00
log('call_hierarchy')
log('call_hierarchy', direction, err, result)
2022-01-18 05:25:48 +00:00
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags')
2021-06-23 14:09:33 +00:00
if err ~= nil then
2022-01-18 05:25:48 +00:00
log('hierarchy error', ctx, 'dir', direction, 'result', result, 'err', err)
vim.notify('ERROR: ' .. error_message, vim.lsp.log_levels.WARN)
2021-06-23 14:09:33 +00:00
return
end
-- log(funcs)
local items = {}
for _, call_hierarchy in pairs(result) do
local kind = ''
local range = call_hierarchy.range
2021-06-23 14:09:33 +00:00
local filename = assert(vim.uri_to_fname(call_hierarchy.uri))
2021-06-25 00:33:04 +00:00
local display_filename = filename:gsub(cwd .. path_sep, path_cur, 1)
2021-06-23 14:09:33 +00:00
local bufnr = vim.uri_to_bufnr(call_hierarchy.uri)
local row = range.start.line
2022-01-18 05:25:48 +00:00
local line = (vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false) or { '' })[1]
local fn = ''
2021-06-23 14:09:33 +00:00
if line ~= nil then
fn = line:sub(range.start.character, range['end'].character + 1)
end
table.insert(items, {
uri = call_hierarchy.uri,
filename = filename,
display_filename = display_filename,
text = kind .. fn,
range = range,
lnum = range.start.line + 1,
2022-01-18 05:25:48 +00:00
col = range.start.character,
2021-06-23 14:09:33 +00:00
})
end
return items
end
2022-01-18 05:25:48 +00:00
local call_hierarchy_handler_from = partial(call_hierarchy_handler, 'from')
local call_hierarchy_handler_to = partial(call_hierarchy_handler, 'to')
2021-06-23 14:09:33 +00:00
local function incoming_calls_handler(bang, err, result, ctx, cfg)
2022-01-18 05:25:48 +00:00
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags')
2022-01-18 05:25:48 +00:00
local results = call_hierarchy_handler_from(err, result, ctx, cfg, 'Incoming calls not found')
2021-06-23 14:09:33 +00:00
2022-01-18 05:25:48 +00:00
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
gui.new_list_view({ items = results, ft = ft, api = '' })
2021-06-23 14:09:33 +00:00
end
-- err, method, result, client_id, bufnr
local function outgoing_calls_handler(bang, err, result, ctx, cfg)
2022-01-18 05:25:48 +00:00
local results = call_hierarchy_handler_to(err, result, ctx, cfg, 'Outgoing calls not found')
2021-06-23 14:09:33 +00:00
2022-01-18 05:25:48 +00:00
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
gui.new_list_view({ items = results, ft = ft, api = '' })
2021-06-23 14:09:33 +00:00
-- fzf_locations(bang, "", "Outgoing Calls", results, false)
end
function M.incoming_calls(bang, opts)
2022-01-18 05:25:48 +00:00
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags')
2021-06-23 14:09:33 +00:00
-- if not lsphelper.check_capabilities("call_hierarchy") then
-- return
-- end
local params = vim.lsp.util.make_position_params()
-- params['hierarchy'] = true
params['levels'] = 2
params['callee'] = false
-- params['callee'] = true
log(params)
log(opts)
2022-01-18 05:25:48 +00:00
lsphelper.call_sync('$ccls/call', params, opts, partial(incoming_calls_handler, bang))
2021-06-23 14:09:33 +00:00
end
function M.outgoing_calls(bang, opts)
2022-01-18 05:25:48 +00:00
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags')
2021-06-23 14:09:33 +00:00
local params = vim.lsp.util.make_position_params()
params['levels'] = 2
params['callee'] = true
log(params)
2022-01-18 05:25:48 +00:00
lsphelper.call_sync('$ccls/call', params, opts, partial(outgoing_calls_handler, bang))
2021-06-23 14:09:33 +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