aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/deprecated.txt5
-rw-r--r--runtime/doc/news.txt3
-rw-r--r--runtime/doc/treesitter.txt31
-rw-r--r--runtime/lua/vim/treesitter.lua3
-rw-r--r--runtime/lua/vim/treesitter/_meta/tsnode.lua18
5 files changed, 45 insertions, 15 deletions
diff --git a/runtime/doc/deprecated.txt b/runtime/doc/deprecated.txt
index 72d2faca02..6895348d05 100644
--- a/runtime/doc/deprecated.txt
+++ b/runtime/doc/deprecated.txt
@@ -36,6 +36,11 @@ DIAGNOSTICS
- The "cursor_position" parameter of |vim.diagnostic.JumpOpts| is renamed to
"pos"
+TREESITTER
+• *TSNode:child_containing_descendant()* Use
+ |TSNode:child_with_descendant()| instead; it is identical except that it can
+ return the descendant itself.
+
------------------------------------------------------------------------------
DEPRECATED IN 0.10 *deprecated-0.10*
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
index 9bc20c3827..1d364ba29a 100644
--- a/runtime/doc/news.txt
+++ b/runtime/doc/news.txt
@@ -108,6 +108,9 @@ TREESITTER
if no languages are explicitly registered.
• |vim.treesitter.language.add()| returns `true` if a parser was loaded
successfully and `nil,errmsg` otherwise instead of throwing an error.
+• New |TSNode:child_with_descendant()|, which is nearly identical to
+ |TSNode:child_containing_descendant()| except that it can return the
+ descendant itself.
TUI
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index 35192cc43d..805876172d 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -622,9 +622,23 @@ TSNode:child({index}) *TSNode:child()*
Return: ~
(`TSNode?`)
- *TSNode:child_containing_descendant()*
-TSNode:child_containing_descendant({descendant})
- Get the node's child that contains {descendant}.
+TSNode:child_count() *TSNode:child_count()*
+ Get the node's number of children.
+
+ Return: ~
+ (`integer`)
+
+ *TSNode:child_with_descendant()*
+TSNode:child_with_descendant({descendant})
+ Get the node's child that contains {descendant} (includes {descendant}).
+
+ For example, with the following node hierarchy: >
+ a -> b -> c
+
+ a:child_with_descendant(c) == b
+ a:child_with_descendant(b) == b
+ a:child_with_descendant(a) == nil
+<
Parameters: ~
• {descendant} (`TSNode`)
@@ -632,12 +646,6 @@ TSNode:child_containing_descendant({descendant})
Return: ~
(`TSNode?`)
-TSNode:child_count() *TSNode:child_count()*
- Get the node's number of children.
-
- Return: ~
- (`integer`)
-
*TSNode:descendant_for_range()*
TSNode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
Get the smallest node within this node that spans the given range of (row,
@@ -778,9 +786,8 @@ TSNode:next_sibling() *TSNode:next_sibling()*
(`TSNode?`)
TSNode:parent() *TSNode:parent()*
- Get the node's immediate parent. Prefer
- |TSNode:child_containing_descendant()| for iterating over the node's
- ancestors.
+ Get the node's immediate parent. Prefer |TSNode:child_with_descendant()|
+ for iterating over the node's ancestors.
Return: ~
(`TSNode?`)
diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua
index ed7d31e1f7..4727c0d61d 100644
--- a/runtime/lua/vim/treesitter.lua
+++ b/runtime/lua/vim/treesitter.lua
@@ -152,8 +152,7 @@ function M.is_ancestor(dest, source)
return false
end
- -- child_containing_descendant returns nil if dest is a direct parent
- return source:parent() == dest or dest:child_containing_descendant(source) ~= nil
+ return dest:child_with_descendant(source) ~= nil
end
--- Returns the node's range or an unpacked range table
diff --git a/runtime/lua/vim/treesitter/_meta/tsnode.lua b/runtime/lua/vim/treesitter/_meta/tsnode.lua
index acc9f8d24e..d982b6a505 100644
--- a/runtime/lua/vim/treesitter/_meta/tsnode.lua
+++ b/runtime/lua/vim/treesitter/_meta/tsnode.lua
@@ -15,7 +15,7 @@ error('Cannot require a meta file')
local TSNode = {} -- luacheck: no unused
--- Get the node's immediate parent.
---- Prefer |TSNode:child_containing_descendant()|
+--- Prefer |TSNode:child_with_descendant()|
--- for iterating over the node's ancestors.
--- @return TSNode?
function TSNode:parent() end
@@ -71,8 +71,24 @@ function TSNode:named_child(index) end
--- Get the node's child that contains {descendant}.
--- @param descendant TSNode
--- @return TSNode?
+--- @deprecated
function TSNode:child_containing_descendant(descendant) end
+--- Get the node's child that contains {descendant} (includes {descendant}).
+---
+--- For example, with the following node hierarchy:
+---
+--- ```
+--- a -> b -> c
+---
+--- a:child_with_descendant(c) == b
+--- a:child_with_descendant(b) == b
+--- a:child_with_descendant(a) == nil
+--- ```
+--- @param descendant TSNode
+--- @return TSNode?
+function TSNode:child_with_descendant(descendant) end
+
--- Get the node's start position. Return three values: the row, column and
--- total byte count (all zero-based).
--- @return integer, integer, integer