dedup definition for script languages

bugfix171
ray-x 2 years ago
parent a93f7cb20f
commit f567f1b99c

@ -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)

@ -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)

@ -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

Loading…
Cancel
Save