diff options
author | Lewis Russell <lewis6991@gmail.com> | 2024-03-18 23:19:01 +0000 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-03-19 14:24:59 +0000 |
commit | aca2048bcd57937ea1c7b7f0325f25d5b82588db (patch) | |
tree | 74f26cf464e6ce345cb296c07477cb38c088f613 /runtime/lua/vim/treesitter/_meta.lua | |
parent | 16a416cb3c17ed3a7f21d35da5d211fcad947768 (diff) | |
download | rneovim-aca2048bcd57937ea1c7b7f0325f25d5b82588db.tar.gz rneovim-aca2048bcd57937ea1c7b7f0325f25d5b82588db.tar.bz2 rneovim-aca2048bcd57937ea1c7b7f0325f25d5b82588db.zip |
refactor(treesitter): redesign query iterating
Problem:
`TSNode:_rawquery()` is complicated, has known issues and the Lua and
C code is awkwardly coupled (see logic with `active`).
Solution:
- Add `TSQueryCursor` and `TSQueryMatch` bindings.
- Replace `TSNode:_rawquery()` with `TSQueryCursor:next_capture()` and `TSQueryCursor:next_match()`
- Do more stuff in Lua
- API for `Query:iter_captures()` and `Query:iter_matches()` remains the same.
- `treesitter.c` no longer contains any logic related to predicates.
- Add `match_limit` option to `iter_matches()`. Default is still 256.
Diffstat (limited to 'runtime/lua/vim/treesitter/_meta.lua')
-rw-r--r-- | runtime/lua/vim/treesitter/_meta.lua | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/runtime/lua/vim/treesitter/_meta.lua b/runtime/lua/vim/treesitter/_meta.lua index 19d97d2820..e2768d4b06 100644 --- a/runtime/lua/vim/treesitter/_meta.lua +++ b/runtime/lua/vim/treesitter/_meta.lua @@ -34,22 +34,6 @@ error('Cannot require a meta file') ---@field byte_length fun(self: TSNode): integer local TSNode = {} ----@param query TSQuery ----@param captures true ----@param start? integer ----@param end_? integer ----@param opts? table ----@return fun(): integer, TSNode, vim.treesitter.query.TSMatch -function TSNode:_rawquery(query, captures, start, end_, opts) end - ----@param query TSQuery ----@param captures false ----@param start? integer ----@param end_? integer ----@param opts? table ----@return fun(): integer, vim.treesitter.query.TSMatch -function TSNode:_rawquery(query, captures, start, end_, opts) end - ---@alias TSLoggerCallback fun(logtype: 'parse'|'lex', msg: string) ---@class TSParser: userdata @@ -90,3 +74,31 @@ vim._ts_parse_query = function(lang, query) end ---@param lang string ---@return TSParser vim._create_ts_parser = function(lang) end + +--- @class TSQueryMatch: userdata +--- @field captures fun(self: TSQueryMatch): table<integer,TSNode[]> +local TSQueryMatch = {} + +--- @return integer match_id +--- @return integer pattern_index +function TSQueryMatch:info() end + +--- @class TSQueryCursor: userdata +--- @field remove_match fun(self: TSQueryCursor, id: integer) +local TSQueryCursor = {} + +--- @return integer capture +--- @return TSNode captured_node +--- @return TSQueryMatch match +function TSQueryCursor:next_capture() end + +--- @return TSQueryMatch match +function TSQueryCursor:next_match() end + +--- @param node TSNode +--- @param query TSQuery +--- @param start integer? +--- @param stop integer? +--- @param opts? { max_start_depth?: integer, match_limit?: integer} +--- @return TSQueryCursor +function vim._create_ts_querycursor(node, query, start, stop, opts) end |