Files
neogen/lua/neogen/locators/default.lua
2022-01-09 14:14:23 +01:00

31 lines
1.1 KiB
Lua

--- @class node_info
--- @field current userdata the current node from cursor
--- @field root userdata the root node
--- The default locator tries to find one of the nodes to match in the current node
--- If it does not find one, will fetch the parents until he finds one
--- @param node_info node_info a node informations
--- @param nodes_to_match table a list of parent nodes to match
--- @return userdata node one of the nodes to match directly above the given node
neogen.default_locator = function(node_info, nodes_to_match)
if not node_info.current then
if vim.tbl_contains(nodes_to_match, node_info.root:type()) then
return node_info.root
end
return
end
-- If we find one of the wanted nodes in current one, return the current node
if vim.tbl_contains(nodes_to_match, node_info.current:type()) then
return node_info.current
end
-- Else, loop to parents until we find one of the nodes to match
while node_info.current and not vim.tbl_contains(nodes_to_match, node_info.current:type()) do
node_info.current = node_info.current:parent()
end
return node_info.current
end