aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/query.lua
diff options
context:
space:
mode:
authorRiley Bruins <ribru17@hotmail.com>2025-03-11 08:29:51 -0700
committerChristian Clason <ch.clason+github@icloud.com>2025-03-12 09:41:40 +0100
commit44ccd9ca24dd804afbc8a00eb1a4789975a51362 (patch)
tree497ab9e4b659261dca33b526099660e9ed73c7b1 /runtime/lua/vim/treesitter/query.lua
parent55fe49cadfbaa42602faf37d5092c044b58959a3 (diff)
downloadrneovim-44ccd9ca24dd804afbc8a00eb1a4789975a51362.tar.gz
rneovim-44ccd9ca24dd804afbc8a00eb1a4789975a51362.tar.bz2
rneovim-44ccd9ca24dd804afbc8a00eb1a4789975a51362.zip
feat(treesitter): allow `iter_captures` to accept `opts`
This matches the `iter_captures` functionality to the `iter_matches` functionality, allowing it to specify a match limit and start depth for the query iterator.
Diffstat (limited to 'runtime/lua/vim/treesitter/query.lua')
-rw-r--r--runtime/lua/vim/treesitter/query.lua11
1 files changed, 9 insertions, 2 deletions
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index 5830cc12e0..c0106c2da7 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -960,12 +960,19 @@ end
---@param source (integer|string) Source buffer or string to extract text from
---@param start? integer Starting line for the search. Defaults to `node:start()`.
---@param stop? integer Stopping line for the search (end-exclusive). Defaults to `node:end_()`.
+---@param opts? table Optional keyword arguments:
+--- - max_start_depth (integer) if non-zero, sets the maximum start depth
+--- for each match. This is used to prevent traversing too deep into a tree.
+--- - match_limit (integer) Set the maximum number of in-progress matches (Default: 256).
---
---@return (fun(end_line: integer|nil): integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch, TSTree):
--- capture id, capture node, metadata, match, tree
---
---@note Captures are only returned if the query pattern of a specific capture contained predicates.
-function Query:iter_captures(node, source, start, stop)
+function Query:iter_captures(node, source, start, stop, opts)
+ opts = opts or {}
+ opts.match_limit = opts.match_limit or 256
+
if type(source) == 'number' and source == 0 then
source = api.nvim_get_current_buf()
end
@@ -974,7 +981,7 @@ function Query:iter_captures(node, source, start, stop)
-- Copy the tree to ensure it is valid during the entire lifetime of the iterator
local tree = node:tree():copy()
- local cursor = vim._create_ts_querycursor(node, self.query, start, stop, { match_limit = 256 })
+ local cursor = vim._create_ts_querycursor(node, self.query, start, stop, opts)
-- For faster checks that a match is not in the cache.
local highest_cached_match_id = -1