feat(treesitter): upstream is_parent()

Util from the nvim-treesitter project.
Renamed is_parent to is_ancestor for clarity.
This commit is contained in:
Quentin Rasmont
2022-04-22 21:50:52 +02:00
committed by bfredl
parent 22f9200302
commit 3aba4ba378
2 changed files with 52 additions and 0 deletions

View File

@@ -118,4 +118,27 @@ function M.get_string_parser(str, lang, opts)
return LanguageTree.new(str, lang, opts)
end
--- Determines whether a node is the ancestor of another
---
---@param dest table the possible ancestor
---@param source table the possible descendant node
---
---@returns (boolean) True if dest is an ancestor of source
function M.is_ancestor(dest, source)
if not (dest and source) then
return false
end
local current = source
while current ~= nil do
if current == dest then
return true
end
current = current:parent()
end
return false
end
return M