2021-12-12 23:42:53 +00:00
|
|
|
local util = require('navigator.util')
|
2021-09-05 22:34:26 +00:00
|
|
|
local mk_handler = util.mk_handler
|
2021-04-26 11:45:45 +00:00
|
|
|
local log = util.log
|
2021-12-12 23:42:53 +00:00
|
|
|
local lsphelper = require('navigator.lspwrapper')
|
|
|
|
local gui = require('navigator.gui')
|
|
|
|
local lsp = require('navigator.lspwrapper')
|
|
|
|
local trace = require('navigator.util').trace
|
2021-09-13 13:18:54 +00:00
|
|
|
ListViewCtrl = ListViewCtrl or require('guihua.listviewctrl').ListViewCtrl
|
2021-04-19 02:56:32 +00:00
|
|
|
-- local partial = util.partial
|
2021-09-09 02:59:58 +00:00
|
|
|
-- local cwd = vim.loop.cwd()
|
2021-04-19 02:56:32 +00:00
|
|
|
-- local lsphelper = require "navigator.lspwrapper"
|
|
|
|
local locations_to_items = lsphelper.locations_to_items
|
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
local M = {}
|
2021-09-09 02:59:58 +00:00
|
|
|
local ref_view = function(err, locations, ctx, cfg)
|
2021-09-13 13:18:54 +00:00
|
|
|
local truncate = cfg and cfg.truncate or 20
|
2021-04-19 02:56:32 +00:00
|
|
|
local opts = {}
|
2021-12-12 23:42:53 +00:00
|
|
|
trace('arg1', err, ctx, locations)
|
2021-05-21 11:39:46 +00:00
|
|
|
trace(locations)
|
2021-12-23 08:17:48 +00:00
|
|
|
if ctx.combine then
|
|
|
|
-- wait for both request
|
|
|
|
if #ctx.results.definitions.result == nil or ctx.results.references.result == nil then
|
|
|
|
log('not all requests returned')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local definitions = ctx.results.definitions
|
|
|
|
local references = ctx.results.references
|
|
|
|
if definitions.error and references.error then
|
|
|
|
vim.notify('lsp ref callback error', vim.inspect(ctx.result), vim.lsp.log_levels.WARN)
|
|
|
|
end
|
|
|
|
locations = {}
|
|
|
|
if definitions.result then
|
|
|
|
for i, _ in ipairs(definitions.result) do
|
|
|
|
definitions.result[i].definition = true
|
|
|
|
end
|
|
|
|
vim.list_extend(locations, definitions.result)
|
|
|
|
end
|
|
|
|
if references.result then
|
|
|
|
vim.list_extend(locations, references.result)
|
|
|
|
end
|
|
|
|
ctx = references.ctx or definitions.ctx
|
|
|
|
err = nil
|
|
|
|
cfg = references.config or definitions.config
|
|
|
|
trace(ctx, locations)
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
-- log("num", num)
|
|
|
|
-- log("bfnr", bufnr)
|
2021-05-17 03:15:15 +00:00
|
|
|
if err ~= nil then
|
2021-12-23 05:37:39 +00:00
|
|
|
vim.notify('lsp ref callback error', err, ctx, vim.inspect(locations), vim.lsp.log_levels.WARN)
|
2021-09-05 22:34:26 +00:00
|
|
|
log('ref callback error, lsp may not ready', err, ctx, vim.inspect(locations))
|
2021-05-17 03:15:15 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if type(locations) ~= 'table' then
|
|
|
|
log(locations)
|
2021-12-12 23:42:53 +00:00
|
|
|
log('ctx', ctx)
|
2021-12-23 05:37:39 +00:00
|
|
|
vim.notify('incorrect setup', locations, vim.lsp.log_levels.WARN)
|
2021-09-05 22:34:26 +00:00
|
|
|
return
|
2021-05-17 03:15:15 +00:00
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
if locations == nil or vim.tbl_isempty(locations) then
|
2021-12-23 05:37:39 +00:00
|
|
|
vim.notify('References not found', vim.lsp.log_levels.WARN)
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
2021-09-13 13:18:54 +00:00
|
|
|
|
|
|
|
local items, width, second_part = locations_to_items(locations, truncate)
|
|
|
|
local thread_items = vim.deepcopy(items)
|
2021-12-12 23:42:53 +00:00
|
|
|
log('splits: ', #items, #second_part)
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
local wwidth = vim.api.nvim_get_option('columns')
|
2021-06-25 10:18:55 +00:00
|
|
|
local mwidth = _NgConfigValues.width
|
|
|
|
width = math.min(width + 30, 120, math.floor(wwidth * mwidth))
|
2021-08-24 07:14:43 +00:00
|
|
|
-- log(items)
|
|
|
|
-- log(width)
|
2021-09-13 13:18:54 +00:00
|
|
|
opts = {
|
|
|
|
total = #locations,
|
2021-06-13 04:03:01 +00:00
|
|
|
items = items,
|
2021-12-12 23:42:53 +00:00
|
|
|
rawdata = false,
|
2021-06-13 04:03:01 +00:00
|
|
|
ft = ft,
|
|
|
|
width = width,
|
2021-12-12 23:42:53 +00:00
|
|
|
api = 'Reference',
|
|
|
|
enable_preview_edit = true,
|
2021-09-13 13:18:54 +00:00
|
|
|
}
|
|
|
|
local listview = gui.new_list_view(opts)
|
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
if listview == nil then
|
2021-12-23 05:37:39 +00:00
|
|
|
vim.notify('failed to create preview windows', vim.lsp.log_levels.WARN)
|
2021-12-12 23:42:53 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- trace("update items", listview.ctrl.class)
|
2021-09-13 13:18:54 +00:00
|
|
|
local nv_ref_async
|
|
|
|
nv_ref_async = vim.loop.new_async(vim.schedule_wrap(function()
|
|
|
|
log('$$$$$$$$ seperate thread... $$$$$$$$')
|
|
|
|
if vim.tbl_isempty(second_part) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local items2 = locations_to_items(second_part)
|
|
|
|
|
|
|
|
vim.list_extend(thread_items, items2)
|
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
local data = require('navigator.render').prepare_for_render(thread_items, opts)
|
2021-09-13 13:18:54 +00:00
|
|
|
listview.ctrl:on_data_update(data)
|
|
|
|
if nv_ref_async then
|
|
|
|
vim.loop.close(nv_ref_async)
|
|
|
|
else
|
2021-12-12 23:42:53 +00:00
|
|
|
log('invalid asy', nv_ref_async)
|
2021-09-13 13:18:54 +00:00
|
|
|
end
|
|
|
|
end))
|
|
|
|
|
|
|
|
vim.defer_fn(function()
|
|
|
|
vim.loop.new_thread(function(asy)
|
|
|
|
asy:send()
|
|
|
|
end, nv_ref_async)
|
|
|
|
end, 100)
|
|
|
|
|
2021-08-24 07:14:43 +00:00
|
|
|
return listview, items, width
|
2021-09-09 02:59:58 +00:00
|
|
|
end
|
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
local ref_hdlr = mk_handler(function(err, locations, ctx, cfg)
|
2021-10-13 09:27:46 +00:00
|
|
|
_NgConfigValues.closer = nil
|
2021-09-09 02:59:58 +00:00
|
|
|
trace(err, locations, ctx, cfg)
|
|
|
|
M.async_hdlr = vim.loop.new_async(vim.schedule_wrap(function()
|
|
|
|
ref_view(err, locations, ctx, cfg)
|
|
|
|
M.async_hdlr:close()
|
|
|
|
end))
|
|
|
|
M.async_hdlr:send()
|
|
|
|
end)
|
2021-10-13 09:27:46 +00:00
|
|
|
|
2021-12-23 08:17:48 +00:00
|
|
|
local async_ref = function()
|
|
|
|
local ref_params = vim.lsp.util.make_position_params()
|
|
|
|
local results = { definitions = {}, references = {} }
|
|
|
|
ref_params.context = { includeDeclaration = false }
|
|
|
|
lsp.call_async('textDocument/definition', ref_params, function(err, result, ctx, config)
|
|
|
|
trace(err, result, ctx, config)
|
|
|
|
results.definitions = {
|
|
|
|
error = err,
|
|
|
|
result = result,
|
|
|
|
ctx = ctx,
|
|
|
|
config = config,
|
|
|
|
}
|
|
|
|
ctx = ctx or {}
|
|
|
|
ctx.results = results
|
|
|
|
ctx.combine = true
|
|
|
|
ref_view(err, result, ctx, config)
|
|
|
|
end) -- return asyncresult, canceller
|
|
|
|
lsp.call_async('textDocument/references', ref_params, function(err, result, ctx, config)
|
|
|
|
trace(err, result, ctx, config)
|
|
|
|
results.references = {
|
|
|
|
error = err,
|
|
|
|
result = result,
|
|
|
|
ctx = ctx,
|
|
|
|
config = config,
|
|
|
|
}
|
|
|
|
ctx = ctx or {}
|
|
|
|
ctx.results = results
|
|
|
|
ctx.combine = true
|
|
|
|
ref_view(err, result, ctx, config)
|
|
|
|
end) -- return asyncresult, canceller
|
|
|
|
end
|
2021-10-13 09:27:46 +00:00
|
|
|
|
|
|
|
local ref_req = function()
|
|
|
|
if _NgConfigValues.closer ~= nil then
|
|
|
|
-- do not call it twice
|
|
|
|
_NgConfigValues.closer()
|
|
|
|
end
|
2021-04-26 11:45:45 +00:00
|
|
|
local ref_params = vim.lsp.util.make_position_params()
|
2021-12-12 23:42:53 +00:00
|
|
|
ref_params.context = { includeDeclaration = true }
|
2021-08-14 03:05:40 +00:00
|
|
|
-- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller
|
2021-10-13 09:27:46 +00:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
2021-12-12 23:42:53 +00:00
|
|
|
log('bufnr', bufnr)
|
|
|
|
local ids, closer = vim.lsp.buf_request(bufnr, 'textDocument/references', ref_params, ref_hdlr)
|
2021-10-13 09:27:46 +00:00
|
|
|
log(ids)
|
|
|
|
|
|
|
|
_NgConfigValues.closer = closer
|
|
|
|
return ids, closer
|
|
|
|
end
|
|
|
|
|
|
|
|
local ref = function()
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
|
|
|
|
local ref_params = vim.lsp.util.make_position_params()
|
|
|
|
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, bufnr)
|
|
|
|
if client.resolved_capabilities.find_references then
|
2021-12-12 23:42:53 +00:00
|
|
|
client.request('textDocument/references', ref_params, ref_hdlr, bufnr)
|
2021-10-13 09:27:46 +00:00
|
|
|
end
|
|
|
|
end)
|
2021-04-26 11:45:45 +00:00
|
|
|
end
|
|
|
|
|
2021-12-23 08:17:48 +00:00
|
|
|
return { reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view, async_ref = async_ref }
|