feat(treesitter): add node_for_range function

This is identical to `named_node_for_range` except that it includes
anonymous nodes. This maintains consistency in the API because we
already have `descendant_for_range` and `named_descendant_for_range`.
This commit is contained in:
Riley Bruins
2024-07-28 13:23:40 -07:00
committed by Christian Clason
parent 01a56a056c
commit bd3b6ec836
4 changed files with 37 additions and 1 deletions

View File

@@ -1133,6 +1133,18 @@ function LanguageTree:tree_for_range(range, opts)
return nil
end
--- Gets the smallest node that contains {range}.
---
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
---@param opts? vim.treesitter.LanguageTree.tree_for_range.Opts
---@return TSNode?
function LanguageTree:node_for_range(range, opts)
local tree = self:tree_for_range(range, opts)
if tree then
return tree:root():descendant_for_range(unpack(range))
end
end
--- Gets the smallest named node that contains {range}.
---
---@param range Range4 `{ start_line, start_col, end_line, end_col }`