mirror of
https://github.com/neovim/neovim
synced 2025-07-15 16:51:49 +00:00
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:
committed by
Christian Clason
parent
55fe49cadf
commit
44ccd9ca24
@ -433,6 +433,8 @@ TREESITTER
|
||||
a specific pattern or capture in a query.
|
||||
• |vim.treesitter.get_captures_at_pos()| returns the `pattern_id` of the
|
||||
pattern used to match each capture.
|
||||
• |Query:iter_captures()| now accepts an `opts` parameter, similar to
|
||||
|Query:iter_matches()|.
|
||||
|
||||
TUI
|
||||
|
||||
|
@ -1254,7 +1254,7 @@ them to parse text. See |vim.treesitter.query.parse()| for a working example.
|
||||
• {has_combined_injections} (`boolean`) whether the query contains
|
||||
combined injections
|
||||
• {query} (`TSQuery`) userdata query object
|
||||
• {iter_captures} (`fun(self: vim.treesitter.Query, node: TSNode, source: integer|string, start: integer?, stop: integer?): fun(end_line: integer?): integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch, TSTree`)
|
||||
• {iter_captures} (`fun(self: vim.treesitter.Query, node: TSNode, source: integer|string, start: integer?, stop: integer?, opts: table?): fun(end_line: integer?): integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch, TSTree`)
|
||||
See |Query:iter_captures()|.
|
||||
• {iter_matches} (`fun(self: vim.treesitter.Query, node: TSNode, source: integer|string, start: integer?, stop: integer?, opts: table?): fun(): integer, table<integer, TSNode[]>, vim.treesitter.query.TSMetadata, TSTree`)
|
||||
See |Query:iter_matches()|.
|
||||
@ -1424,7 +1424,7 @@ parse({lang}, {query}) *vim.treesitter.query.parse()*
|
||||
• |vim.treesitter.query.get()|
|
||||
|
||||
*Query:iter_captures()*
|
||||
Query:iter_captures({node}, {source}, {start}, {stop})
|
||||
Query:iter_captures({node}, {source}, {start}, {stop}, {opts})
|
||||
Iterates over all captures from all matches in {node}.
|
||||
|
||||
{source} is required if the query contains predicates; then the caller
|
||||
@ -1463,6 +1463,12 @@ Query:iter_captures({node}, {source}, {start}, {stop})
|
||||
`node:start()`.
|
||||
• {stop} (`integer?`) Stopping line for the search (end-exclusive).
|
||||
Defaults to `node:end_()`.
|
||||
• {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?): integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch, TSTree`)
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user