diff --git a/lua/navigator.lua b/lua/navigator.lua index f707bf0..45816fa 100644 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -91,7 +91,7 @@ _NgConfigValues = { diagnostic_file = '🚑', -- Values value_changed = '📝', - value_definition = '🐶', -- it is easier to see than 🦕 + value_definition = '🐶🍡', -- it is easier to see than 🦕 -- Treesitter match_kinds = { var = ' ', -- "👹", -- Vampaire diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index 6476483..d8add08 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -13,6 +13,7 @@ local single = { '╭', '─', '╮', '│', '╯', '─', '╰', '│' } -- LuaFormatter off local key_maps = { { key = 'gr', func = "require('navigator.reference').reference()" }, + { key = 'Gr', func = "require('navigator.reference').async_ref()" }, { mode = 'i', key = '', func = 'signature_help()' }, { key = '', func = 'signature_help()' }, { key = 'g0', func = "require('navigator.symbols').document_symbols()" }, diff --git a/lua/navigator/lspwrapper.lua b/lua/navigator/lspwrapper.lua index dd16e6c..3ac0d0d 100644 --- a/lua/navigator/lspwrapper.lua +++ b/lua/navigator/lspwrapper.lua @@ -361,8 +361,10 @@ function M.locations_to_items(locations, max_items) uri_def[item.uri] = def if def.start then -- find for the 1st time for j = 1, #items do - if items[j].uri == item.uri and items[j].range.start.line == def.start.line then - items[j].definition = true + if items[j].definition ~= nil then + if items[j].uri == item.uri and items[j].range.start.line == def.start.line then + items[j].definition = true + end end end end @@ -379,9 +381,9 @@ function M.locations_to_items(locations, max_items) end end trace(uri_def[item.uri], item.range) -- set to log if need to get all in rnge - local def = uri_def[item.uri] - if def and def.start and item.range then - if def.start.line == item.range.start.line then + local def1 = uri_def[item.uri] + if def1 and def1.start and item.range then + if def1.start.line == item.range.start.line then log('ts def in current line') item.definition = true end @@ -417,7 +419,6 @@ function M.locations_to_items(locations, max_items) return items, width + 24, second_part -- TODO handle long line? end - function M.symbol_to_items(locations) if not locations or vim.tbl_isempty(locations) then vim.notify('list not avalible', vim.lsp.log_levels.WARN) @@ -427,6 +428,9 @@ function M.symbol_to_items(locations) local items = {} -- lsp.util.locations_to_items(locations) -- items and locations may not matching table.sort(locations, function(i, j) + if i.definition then + return true + end if i.uri == j.uri then if i.range and i.range.start then return i.range.start.line < j.range.start.line diff --git a/lua/navigator/reference.lua b/lua/navigator/reference.lua index 18cf215..0bc3fe2 100644 --- a/lua/navigator/reference.lua +++ b/lua/navigator/reference.lua @@ -17,6 +17,32 @@ local ref_view = function(err, locations, ctx, cfg) local opts = {} trace('arg1', err, ctx, locations) trace(locations) + 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 -- log("num", num) -- log("bfnr", bufnr) if err ~= nil then @@ -100,12 +126,37 @@ local ref_hdlr = mk_handler(function(err, locations, ctx, cfg) M.async_hdlr:send() end) --- local async_reference_request = function() --- local ref_params = vim.lsp.util.make_position_params() --- ref_params.context = {includeDeclaration = true} --- -- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller --- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller --- end +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 local ref_req = function() if _NgConfigValues.closer ~= nil then @@ -135,4 +186,4 @@ local ref = function() end) end -return { reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view } +return { reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view, async_ref = async_ref }