aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/treesitter.txt5
-rw-r--r--runtime/lua/vim/treesitter/_meta.lua1
-rw-r--r--runtime/lua/vim/treesitter/query.lua13
3 files changed, 8 insertions, 11 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index e105d06ebb..0b84bb60d4 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -78,6 +78,8 @@ An instance `TSNode` of a treesitter node supports the following methods.
TSNode:parent() *TSNode:parent()*
Get the node's immediate parent.
+ Prefer |TSNode:child_containing_descendant()|
+ for iterating over the node's ancestors.
TSNode:next_sibling() *TSNode:next_sibling()*
Get the node's next sibling.
@@ -114,6 +116,9 @@ TSNode:named_child({index}) *TSNode:named_child()*
Get the node's named child at the given {index}, where zero represents the
first named child.
+TSNode:child_containing_descendant({descendant}) *TSNode:child_containing_descendant()*
+ Get the node's child that contains {descendant}.
+
TSNode:start() *TSNode:start()*
Get the node's start position. Return three values: the row, column and
total byte count (all zero-based).
diff --git a/runtime/lua/vim/treesitter/_meta.lua b/runtime/lua/vim/treesitter/_meta.lua
index 34a51e42f6..177699a207 100644
--- a/runtime/lua/vim/treesitter/_meta.lua
+++ b/runtime/lua/vim/treesitter/_meta.lua
@@ -20,6 +20,7 @@ error('Cannot require a meta file')
---@field descendant_for_range fun(self: TSNode, start_row: integer, start_col: integer, end_row: integer, end_col: integer): TSNode?
---@field named_descendant_for_range fun(self: TSNode, start_row: integer, start_col: integer, end_row: integer, end_col: integer): TSNode?
---@field parent fun(self: TSNode): TSNode?
+---@field child_containing_descendant fun(self: TSNode, descendant: TSNode): TSNode?
---@field next_sibling fun(self: TSNode): TSNode?
---@field prev_sibling fun(self: TSNode): TSNode?
---@field next_named_sibling fun(self: TSNode): TSNode?
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index 36c78b7f1d..ef5c2143a7 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -457,17 +457,8 @@ local predicate_handlers = {
end
for _, node in ipairs(nodes) do
- local ancestor_types = {} --- @type table<string, boolean>
- for _, type in ipairs({ unpack(predicate, 3) }) do
- ancestor_types[type] = true
- end
-
- local cur = node:parent()
- while cur do
- if ancestor_types[cur:type()] then
- return true
- end
- cur = cur:parent()
+ if node:__has_ancestor(predicate) then
+ return true
end
end
return false