2021-12-12 23:42:53 +00:00
|
|
|
local util = require('navigator.util')
|
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-04-19 02:56:32 +00:00
|
|
|
-- local partial = util.partial
|
2021-09-09 02:59:58 +00:00
|
|
|
-- local cwd = vim.loop.cwd()
|
2024-02-02 04:02:51 +00:00
|
|
|
local uv = vim.uv or vim.loop
|
2021-04-19 02:56:32 +00:00
|
|
|
-- local lsphelper = require "navigator.lspwrapper"
|
|
|
|
local locations_to_items = lsphelper.locations_to_items
|
|
|
|
|
2023-11-07 12:00:36 +00:00
|
|
|
local function order_locations(locations)
|
|
|
|
table.sort(locations, function(i, j)
|
|
|
|
if i == nil or j == nil or i.uri == nil or j.uri == nil then
|
|
|
|
-- log(i, j)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if i.uri == j.uri then
|
|
|
|
if i.range and i.range.start then
|
|
|
|
return i.range.start.line < j.range.start.line
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return i.uri < j.uri
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
return locations
|
|
|
|
end
|
|
|
|
|
2024-02-02 04:02:51 +00:00
|
|
|
local function warmup_treesitter()
|
2023-11-07 12:00:36 +00:00
|
|
|
local parsers = require('nvim-treesitter.parsers')
|
2024-02-02 04:02:51 +00:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
2023-11-07 12:00:36 +00:00
|
|
|
local parser = parsers.get_parser(bufnr)
|
|
|
|
if not parser then
|
|
|
|
log('err: ts not loaded ' .. vim.o.ft)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
2022-09-16 11:21:20 +00:00
|
|
|
cfg = cfg or {}
|
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 = {}
|
2023-11-07 12:00:36 +00:00
|
|
|
trace('ref_view', err, ctx, #locations, cfg, locations)
|
2022-08-01 12:32:15 +00:00
|
|
|
-- log(#locations, locations[1])
|
2021-12-23 08:17:48 +00:00
|
|
|
if ctx.combine then
|
2022-06-29 04:51:28 +00:00
|
|
|
-- wait for both reference and definition LSP request
|
2021-12-24 06:48:54 +00:00
|
|
|
if ctx.results == nil then
|
|
|
|
return
|
|
|
|
end
|
2022-02-17 11:21:34 +00:00
|
|
|
if (ctx.results.definitions == nil) or (ctx.results.references == nil) then
|
2021-12-23 08:17:48 +00:00
|
|
|
log('not all requests returned')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local definitions = ctx.results.definitions
|
|
|
|
local references = ctx.results.references
|
2022-06-11 05:03:28 +00:00
|
|
|
if _NgConfigValues.debug then
|
|
|
|
local logctx = { results = {} }
|
|
|
|
logctx = vim.tbl_extend('keep', logctx, ctx)
|
2022-06-29 04:51:28 +00:00
|
|
|
log(logctx, 'result size', 'def', #ctx.results.definitions, 'ref', #ctx.results.references)
|
2022-06-11 05:03:28 +00:00
|
|
|
end
|
2021-12-23 08:17:48 +00:00
|
|
|
if definitions.error and references.error then
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.notify('lsp ref callback error' .. vim.inspect(ctx.result), vim.log.levels.WARN)
|
2021-12-23 08:17:48 +00:00
|
|
|
end
|
|
|
|
locations = {}
|
2022-02-17 11:21:34 +00:00
|
|
|
if definitions and definitions.result then
|
2021-12-23 08:17:48 +00:00
|
|
|
for i, _ in ipairs(definitions.result) do
|
|
|
|
definitions.result[i].definition = true
|
|
|
|
end
|
|
|
|
vim.list_extend(locations, definitions.result)
|
|
|
|
end
|
2022-07-31 13:17:03 +00:00
|
|
|
|
2022-02-17 11:21:34 +00:00
|
|
|
if references and references.result and #references.result > 0 then
|
|
|
|
local refs = references.result
|
2023-11-07 12:00:36 +00:00
|
|
|
|
|
|
|
order_locations(refs)
|
2022-07-31 13:17:03 +00:00
|
|
|
vim.list_extend(locations, refs)
|
2021-12-23 08:17:48 +00:00
|
|
|
end
|
|
|
|
err = nil
|
2022-07-07 00:18:15 +00:00
|
|
|
-- lets de-dup first 10 elements. some lsp does not recognize definition and reference difference
|
2022-07-07 04:02:14 +00:00
|
|
|
locations = util.dedup(locations)
|
2022-08-01 12:32:15 +00:00
|
|
|
trace(locations)
|
2021-12-23 08:17:48 +00:00
|
|
|
end
|
2023-11-07 12:00:36 +00:00
|
|
|
-- log('num bufnr: ', num, bufnr)
|
2021-05-17 03:15:15 +00:00
|
|
|
if err ~= nil then
|
2021-12-29 04:50:41 +00:00
|
|
|
vim.notify(
|
|
|
|
'lsp ref callback error' .. vim.inspect(err) .. vim.inspect(ctx) .. vim.inspect(locations),
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.log.levels.WARN
|
2021-12-29 04:50:41 +00:00
|
|
|
)
|
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
|
2021-04-19 02:56:32 +00:00
|
|
|
if locations == nil or vim.tbl_isempty(locations) then
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.notify('References not found', vim.log.levels.INFO)
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
2021-09-13 13:18:54 +00:00
|
|
|
|
2022-01-18 05:25:48 +00:00
|
|
|
ctx.max_items = truncate
|
|
|
|
local items, width, second_part = locations_to_items(locations, ctx)
|
2023-11-07 12:00:36 +00:00
|
|
|
local thread_items = {}
|
|
|
|
if vim.fn.empty(second_part) == 0 then
|
|
|
|
thread_items = vim.deepcopy(items)
|
|
|
|
end
|
|
|
|
log('splits: ', #locations, #items, #second_part)
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2022-09-16 11:21:20 +00:00
|
|
|
local ft = vim.api.nvim_buf_get_option(ctx.bufnr or 0, '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
|
2022-09-20 07:08:56 +00:00
|
|
|
width = math.min(width + 30, 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',
|
2021-12-29 04:50:41 +00:00
|
|
|
enable_preview_edit = true,
|
2023-02-14 12:58:38 +00:00
|
|
|
title = 'References ' .. vim.fn.expand('<cword>'),
|
2021-09-13 13:18:54 +00:00
|
|
|
}
|
2022-09-16 11:21:20 +00:00
|
|
|
local listview
|
|
|
|
if not ctx.no_show then
|
|
|
|
listview = gui.new_list_view(opts)
|
2021-09-13 13:18:54 +00:00
|
|
|
|
2022-09-16 11:21:20 +00:00
|
|
|
if listview == nil then
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.notify('failed to create preview windows', vim.log.levels.INFO)
|
2022-09-16 11:21:20 +00:00
|
|
|
return
|
|
|
|
end
|
2021-12-12 23:42:53 +00:00
|
|
|
end
|
2022-09-16 11:21:20 +00:00
|
|
|
|
|
|
|
if ctx.no_show then
|
|
|
|
opts.side_panel = true
|
|
|
|
local data = require('navigator.render').prepare_for_render(items, opts)
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
2021-12-12 23:42:53 +00:00
|
|
|
-- trace("update items", listview.ctrl.class)
|
2021-09-13 13:18:54 +00:00
|
|
|
local nv_ref_async
|
2023-11-07 12:00:36 +00:00
|
|
|
nv_ref_async = uv.new_async(vim.schedule_wrap(function()
|
2021-09-13 13:18:54 +00:00
|
|
|
if vim.tbl_isempty(second_part) then
|
|
|
|
return
|
|
|
|
end
|
2023-11-07 12:00:36 +00:00
|
|
|
log('$$$$$$$$ --- seperate thread... --- $$$$$$$$')
|
|
|
|
ctx.max_items = #second_part -- proccess all the rest
|
2022-01-18 05:25:48 +00:00
|
|
|
local items2 = locations_to_items(second_part, ctx)
|
2021-09-13 13:18:54 +00:00
|
|
|
|
|
|
|
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)
|
2022-06-29 04:51:28 +00:00
|
|
|
log('thread data size', #data)
|
2021-09-13 13:18:54 +00:00
|
|
|
listview.ctrl:on_data_update(data)
|
|
|
|
if nv_ref_async then
|
2023-11-07 12:00:36 +00:00
|
|
|
uv.close(nv_ref_async)
|
2021-09-13 13:18:54 +00:00
|
|
|
else
|
2023-11-07 12:00:36 +00:00
|
|
|
log('invalid ref_async')
|
2021-09-13 13:18:54 +00:00
|
|
|
end
|
|
|
|
end))
|
|
|
|
|
|
|
|
vim.defer_fn(function()
|
2023-11-07 12:00:36 +00:00
|
|
|
uv.new_thread(function(asy)
|
2021-09-13 13:18:54 +00:00
|
|
|
asy:send()
|
|
|
|
end, nv_ref_async)
|
2023-11-07 12:00:36 +00:00
|
|
|
end, 10)
|
2021-09-13 13:18:54 +00:00
|
|
|
|
2021-08-24 07:14:43 +00:00
|
|
|
return listview, items, width
|
2021-09-09 02:59:58 +00:00
|
|
|
end
|
|
|
|
|
2022-05-15 13:13:01 +00:00
|
|
|
local ref_hdlr = 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)
|
2022-09-16 11:21:20 +00:00
|
|
|
if ctx.no_show then
|
|
|
|
return ref_view(err, locations, ctx, cfg)
|
|
|
|
end
|
2023-11-07 12:00:36 +00:00
|
|
|
M.async_hdlr = uv.new_async(vim.schedule_wrap(function()
|
2021-09-09 02:59:58 +00:00
|
|
|
ref_view(err, locations, ctx, cfg)
|
2022-01-09 14:52:32 +00:00
|
|
|
if M.async_hdlr:is_active() then
|
|
|
|
M.async_hdlr:close()
|
|
|
|
end
|
2021-09-09 02:59:58 +00:00
|
|
|
end))
|
|
|
|
M.async_hdlr:send()
|
2022-05-15 13:13:01 +00:00
|
|
|
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()
|
2022-02-17 11:21:34 +00:00
|
|
|
local results = {}
|
2021-12-23 08:17:48 +00:00
|
|
|
lsp.call_async('textDocument/definition', ref_params, function(err, result, ctx, config)
|
|
|
|
trace(err, result, ctx, config)
|
2022-04-05 20:47:47 +00:00
|
|
|
if err ~= nil or result == nil then
|
|
|
|
log('failed to get def', err, result, ctx, config)
|
|
|
|
result = {}
|
|
|
|
end
|
2022-02-17 11:21:34 +00:00
|
|
|
for i = 1, #result do
|
|
|
|
if result[i].range == nil and result[i].targetRange then
|
|
|
|
result[i].range = result[i].targetRange
|
|
|
|
end
|
|
|
|
end
|
2021-12-29 04:50:41 +00:00
|
|
|
results.definitions = { error = err, result = result, ctx = ctx, config = config }
|
2023-11-07 12:00:36 +00:00
|
|
|
log('number of result', #result)
|
2021-12-23 08:17:48 +00:00
|
|
|
ctx = ctx or {}
|
|
|
|
ctx.results = results
|
|
|
|
ctx.combine = true
|
|
|
|
ref_view(err, result, ctx, config)
|
|
|
|
end) -- return asyncresult, canceller
|
2022-04-10 08:59:43 +00:00
|
|
|
|
|
|
|
ref_params.context = { includeDeclaration = false }
|
2021-12-23 08:17:48 +00:00
|
|
|
lsp.call_async('textDocument/references', ref_params, function(err, result, ctx, config)
|
2022-04-05 20:47:47 +00:00
|
|
|
if err ~= nil or result == nil then
|
|
|
|
log('failed to get ref', err, result, ctx, config)
|
|
|
|
result = {}
|
|
|
|
end
|
2021-12-23 08:17:48 +00:00
|
|
|
trace(err, result, ctx, config)
|
2021-12-29 04:50:41 +00:00
|
|
|
results.references = { error = err, result = result, ctx = ctx, config = config }
|
2021-12-23 08:17:48 +00:00
|
|
|
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
|
|
|
|
2023-01-11 10:39:56 +00:00
|
|
|
-- Get positions of LSP reference symbols
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
-- https://github.com/smjonas/inc-rename.nvim/blob/main/lua/inc_rename/init.lua
|
2023-02-17 21:20:40 +00:00
|
|
|
local function fetch_lsp_references(bufnr, params, callback)
|
2023-11-08 04:18:13 +00:00
|
|
|
if not vim.lsp.get_clients then
|
|
|
|
vim.lsp.get_clients = vim.lsp.get_active_clients
|
|
|
|
end
|
2023-11-07 12:00:36 +00:00
|
|
|
local clients = vim.lsp.get_clients({
|
2023-01-11 10:39:56 +00:00
|
|
|
bufnr = bufnr,
|
|
|
|
})
|
|
|
|
clients = vim.tbl_filter(function(client)
|
|
|
|
return client.supports_method('textDocument/rename')
|
|
|
|
end, clients)
|
|
|
|
|
|
|
|
if #clients == 0 then
|
2023-02-17 21:20:40 +00:00
|
|
|
log('[nav-rename] No active language server with rename capability')
|
|
|
|
vim.notify('No active language server with reference capability')
|
2023-01-11 10:39:56 +00:00
|
|
|
end
|
2023-02-17 21:20:40 +00:00
|
|
|
if not params then
|
|
|
|
log('[nav-rename] No params provided')
|
|
|
|
vim.notify('No params provided')
|
|
|
|
end
|
|
|
|
params.context = params.context or { includeDeclaration = true }
|
2023-01-11 10:39:56 +00:00
|
|
|
|
|
|
|
log(bufnr, params)
|
|
|
|
|
|
|
|
-- return id, closer
|
2023-02-14 12:58:38 +00:00
|
|
|
return vim.lsp.buf_request(
|
|
|
|
bufnr,
|
|
|
|
'textDocument/references',
|
|
|
|
params,
|
|
|
|
function(err, result, ctx, cfg)
|
2023-11-07 12:00:36 +00:00
|
|
|
trace(result)
|
2023-02-14 12:58:38 +00:00
|
|
|
if err then
|
|
|
|
log('[nav-rename] Error while finding references: ' .. err.message)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not result or vim.tbl_isempty(result) then
|
|
|
|
log('[nav-rename] Nothing to rename', result)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if callback then
|
|
|
|
callback(err, result, ctx, cfg)
|
|
|
|
end
|
2023-01-11 10:39:56 +00:00
|
|
|
end
|
2023-02-14 12:58:38 +00:00
|
|
|
)
|
2023-01-11 10:39:56 +00:00
|
|
|
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
|
2023-11-07 12:00:36 +00:00
|
|
|
|
|
|
|
if _NgConfigValues.treesitter_analysis then
|
2024-02-02 04:02:51 +00:00
|
|
|
warmup_treesitter()
|
2023-11-07 12:00:36 +00:00
|
|
|
end
|
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()
|
2023-02-17 21:20:40 +00:00
|
|
|
local ref_params = vim.lsp.util.make_position_params()
|
2021-12-12 23:42:53 +00:00
|
|
|
log('bufnr', bufnr)
|
2023-02-17 21:20:40 +00:00
|
|
|
local ids, closer = fetch_lsp_references(bufnr, 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()
|
2022-06-01 15:41:26 +00:00
|
|
|
vim.lsp.for_each_buffer_client(bufnr, function(client, _, _)
|
2022-05-15 13:13:01 +00:00
|
|
|
if client.server_capabilities.referencesProvider 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
|
|
|
|
|
2022-09-16 11:21:20 +00:00
|
|
|
local function side_panel()
|
|
|
|
local Panel = require('guihua.panel')
|
|
|
|
|
|
|
|
local currentWord = vim.fn.expand('<cword>')
|
|
|
|
local p = Panel:new({
|
|
|
|
scope = 'range',
|
|
|
|
header = ' ' .. currentWord .. ' ref ',
|
|
|
|
render = function(bufnr)
|
|
|
|
local ft = vim.api.nvim_buf_get_option(bufnr, 'buftype')
|
|
|
|
if ft == 'nofile' or ft == 'guihua' or ft == 'prompt' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local ref_params = vim.lsp.util.make_position_params()
|
|
|
|
local sync_req = require('navigator.lspwrapper').call_sync
|
|
|
|
return sync_req(
|
|
|
|
'textDocument/references',
|
|
|
|
ref_params,
|
|
|
|
{ timeout = 1000, bufnr = bufnr, no_show = true },
|
|
|
|
vim.lsp.with(function(err, locations, ctx, cfg)
|
|
|
|
cfg.side_panel = true
|
|
|
|
return ref_hdlr(err, locations, ctx, cfg)
|
|
|
|
end, { no_show = true })
|
|
|
|
)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
p:open(true)
|
|
|
|
end
|
|
|
|
|
2021-12-24 06:48:54 +00:00
|
|
|
return {
|
2022-09-16 11:21:20 +00:00
|
|
|
side_panel = side_panel,
|
2023-01-11 10:39:56 +00:00
|
|
|
fetch_lsp_references = fetch_lsp_references,
|
2021-12-24 06:48:54 +00:00
|
|
|
reference_handler = ref_hdlr,
|
|
|
|
reference = ref_req,
|
|
|
|
ref_view = ref_view,
|
2021-12-29 04:50:41 +00:00
|
|
|
async_ref = async_ref,
|
2022-06-01 15:41:26 +00:00
|
|
|
all_ref = ref,
|
2021-12-24 06:48:54 +00:00
|
|
|
}
|