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.
This commit is contained in:
Riley Bruins
2025-03-11 08:29:51 -07:00
committed by Christian Clason
parent 55fe49cadf
commit 44ccd9ca24
3 changed files with 19 additions and 4 deletions

View File

@ -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