feat(refactor): add definition navigation module

This commit is contained in:
Steven Sojka 2020-06-26 13:57:23 -05:00 committed by Kiyan Yazdani
parent 64838e51c0
commit 6f8e4c97a4
3 changed files with 123 additions and 7 deletions

View file

@ -252,16 +252,36 @@ end
-- @param local_def the local list result
-- @returns a list of nodes
function M.get_local_nodes(local_def)
local result = {}
M.recurse_local_nodes(local_def, function(_, node)
table.insert(result, node)
end)
return result
end
-- Recurse locals results until a node is found.
-- The accumulator function is given
-- * The table of the node
-- * The node
-- * The full definition match `@definition.var.something` -> 'var.something'
-- * The last definition match `@definition.var.something` -> 'something'
-- @param The locals result
-- @param The accumulator function
-- @param The full match path to append to
-- @param The last match
function M.recurse_local_nodes(local_def, accumulator, full_match, last_match)
if local_def.node then
return { local_def.node }
accumulator(local_def, local_def.node, full_match, last_match)
else
local result = {}
for _, def in pairs(local_def) do
vim.list_extend(result, M.get_local_nodes(def))
for match_key, def in pairs(local_def) do
M.recurse_local_nodes(
def,
accumulator,
full_match and (full_match..'.'..match_key) or match_key,
match_key)
end
return result
end
end