diff options
author | Riley Bruins <ribru17@hotmail.com> | 2025-03-11 08:29:51 -0700 |
---|---|---|
committer | Christian Clason <ch.clason+github@icloud.com> | 2025-03-12 09:41:40 +0100 |
commit | 44ccd9ca24dd804afbc8a00eb1a4789975a51362 (patch) | |
tree | 497ab9e4b659261dca33b526099660e9ed73c7b1 | |
parent | 55fe49cadfbaa42602faf37d5092c044b58959a3 (diff) | |
download | rneovim-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.
-rw-r--r-- | runtime/doc/news.txt | 2 | ||||
-rw-r--r-- | runtime/doc/treesitter.txt | 10 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/query.lua | 11 |
3 files changed, 19 insertions, 4 deletions
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 3204df6af1..cfb40f1ca6 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -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 diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index b5f64921f6..6c98d46778 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -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`) 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 |