From f567f1b99c79c58980ef07747e2ce560710ca9cd Mon Sep 17 00:00:00 2001 From: ray-x Date: Thu, 7 Jul 2022 14:02:14 +1000 Subject: [PATCH] dedup definition for script languages --- lua/navigator/definition.lua | 5 +++++ lua/navigator/reference.lua | 28 +------------------------- lua/navigator/util.lua | 39 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lua/navigator/definition.lua b/lua/navigator/definition.lua index b3b114e..e3c87ed 100644 --- a/lua/navigator/definition.lua +++ b/lua/navigator/definition.lua @@ -3,6 +3,7 @@ local lsphelper = require('navigator.lspwrapper') local locations_to_items = lsphelper.locations_to_items local gui = require('navigator.gui') local log = util.log +local trace = util.trace local TextView = require('guihua.textview') -- callback for lsp definition, implementation and declaration handler local definition_hdlr = function(err, locations, ctx, _) @@ -21,6 +22,10 @@ local definition_hdlr = function(err, locations, ctx, _) end local oe = require('navigator.util').encoding(ctx.client_id) + + locations = util.dedup(locations) + log(locations) + log("found " .. #locations .. " locations") if vim.tbl_islist(locations) then if #locations > 1 then local items = locations_to_items(locations) diff --git a/lua/navigator/reference.lua b/lua/navigator/reference.lua index 5686387..8d495be 100644 --- a/lua/navigator/reference.lua +++ b/lua/navigator/reference.lua @@ -60,34 +60,8 @@ local ref_view = function(err, locations, ctx, cfg) err = nil trace(locations) -- lets de-dup first 10 elements. some lsp does not recognize definition and reference difference - local m = 10 - if 10 > #locations then - m = #locations - end + locations = util.dedup(locations) - local dict = {} - local del = {} - for i = 1, m, 1 do - local value = locations[i] - if not value.range then - break - end - local key = (value.range.uri or value.targetUri or "") .. ':' .. tostring(value.range.start.line) .. ':' .. tostring(value.range.start.character) - .. ':' .. tostring(value.range['end'].line) .. ':' .. tostring(value.range['end'].character) - if dict[key] == nil then - dict[key] = i - else - local j = dict[key] - if not locations[j].definition then - del[#del] = i - else - del[#del] = j - end - end - end - for i = #del, 1, -1 do - table.remove(locations, del[i]) - end end -- log("num", num) -- log("bfnr", bufnr) diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 2023864..174a260 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -491,6 +491,45 @@ function M.info(msg) vim.notify('INF: ' .. msg, vim.lsp.log_levels.INFO) end +function M.dedup(locations) + local m = 10 + if m > #locations then + m = #locations + end + local dict = {} + local del = {} + for i = 1, m, 1 do + local value = locations[i] + local range = value.range or value.originSelectionRange or value.targetRange + if not range then + break + end + local key = (range.uri or value.targetUri or '') + .. ':' + .. tostring(range.start.line) + .. ':' + .. tostring(range.start.character) + .. ':' + .. tostring(range['end'].line) + .. ':' + .. tostring(range['end'].character) + if dict[key] == nil then + dict[key] = i + else + local j = dict[key] + if not locations[j].definition then + table.insert(del, i) + else + table.insert(del, j) + end + end + end + for i = #del, 1, -1 do + locations = table.remove(locations, del[i]) + end + return locations +end + function M.range_inside(outer, inner) if outer == nil or inner == nil then return false