mirror of
https://github.com/neovim/neovim.git
synced 2026-03-31 21:02:11 +00:00
refactor(treesitter)!: remove "all" option of Query:iter_matches #33070
This option was introduced to help with transitioning to the new behavior during the 0.11 release cycle with the intention of removing in 0.12.
This commit is contained in:
@@ -136,6 +136,8 @@ TREESITTER
|
||||
`metadata[capture_id].offset`. The offset will be applied in
|
||||
|vim.treesitter.get_range()|, which should be preferred over reading
|
||||
metadata directly for retrieving node ranges.
|
||||
• The "all" option to |Query:iter_matches()|, which was introduced in Nvim
|
||||
0.11 to aid in transitioning to the new behavior, has been removed.
|
||||
|
||||
TUI
|
||||
|
||||
|
||||
@@ -1571,10 +1571,6 @@ add_directive({name}, {handler}, {opts})
|
||||
• {opts} (`table`) A table with the following fields:
|
||||
• {force}? (`boolean`) Override an existing predicate of
|
||||
the same name
|
||||
• {all}? (`boolean`) Use the correct implementation of the
|
||||
match table where capture IDs map to a list of nodes
|
||||
instead of a single node. Defaults to true. This option
|
||||
will be removed in a future release.
|
||||
|
||||
*vim.treesitter.query.add_predicate()*
|
||||
add_predicate({name}, {handler}, {opts})
|
||||
@@ -1588,10 +1584,6 @@ add_predicate({name}, {handler}, {opts})
|
||||
• {opts} (`table?`) A table with the following fields:
|
||||
• {force}? (`boolean`) Override an existing predicate of
|
||||
the same name
|
||||
• {all}? (`boolean`) Use the correct implementation of the
|
||||
match table where capture IDs map to a list of nodes
|
||||
instead of a single node. Defaults to true. This option
|
||||
will be removed in a future release.
|
||||
|
||||
edit({lang}) *vim.treesitter.query.edit()*
|
||||
Opens a live editor to query the buffer you started from.
|
||||
@@ -1808,11 +1800,6 @@ Query:iter_matches({node}, {source}, {start}, {stop}, {opts})
|
||||
traversing too deep into a tree.
|
||||
• match_limit (integer) Set the maximum number of
|
||||
in-progress matches (Default: 256).
|
||||
• all (boolean) When `false` (default `true`), the returned
|
||||
table maps capture IDs to a single (last) node instead of
|
||||
the full list of matching nodes. This option is only for
|
||||
backward compatibility and will be removed in a future
|
||||
release.
|
||||
|
||||
Return: ~
|
||||
(`fun(): integer, table<integer, TSNode[]>, vim.treesitter.query.TSMetadata, TSTree`)
|
||||
|
||||
@@ -759,11 +759,6 @@ local directive_handlers = {
|
||||
---
|
||||
--- Override an existing predicate of the same name
|
||||
--- @field force? boolean
|
||||
---
|
||||
--- Use the correct implementation of the match table where capture IDs map to
|
||||
--- a list of nodes instead of a single node. Defaults to true. This option will
|
||||
--- be removed in a future release.
|
||||
--- @field all? boolean
|
||||
|
||||
--- Adds a new predicate to be used in queries
|
||||
---
|
||||
@@ -783,21 +778,7 @@ function M.add_predicate(name, handler, opts)
|
||||
error(string.format('Overriding existing predicate %s', name))
|
||||
end
|
||||
|
||||
if opts.all ~= false then
|
||||
predicate_handlers[name] = handler
|
||||
else
|
||||
--- @param match table<integer, TSNode[]>
|
||||
local function wrapper(match, ...)
|
||||
local m = {} ---@type table<integer, TSNode>
|
||||
for k, v in pairs(match) do
|
||||
if type(k) == 'number' then
|
||||
m[k] = v[#v]
|
||||
end
|
||||
end
|
||||
return handler(m, ...)
|
||||
end
|
||||
predicate_handlers[name] = wrapper
|
||||
end
|
||||
predicate_handlers[name] = handler
|
||||
end
|
||||
|
||||
--- Adds a new directive to be used in queries
|
||||
@@ -826,19 +807,7 @@ function M.add_directive(name, handler, opts)
|
||||
error(string.format('Overriding existing directive %s', name))
|
||||
end
|
||||
|
||||
if opts.all then
|
||||
directive_handlers[name] = handler
|
||||
else
|
||||
--- @param match table<integer, TSNode[]>
|
||||
local function wrapper(match, ...)
|
||||
local m = {} ---@type table<integer, TSNode>
|
||||
for k, v in pairs(match) do
|
||||
m[k] = v[#v]
|
||||
end
|
||||
handler(m, ...)
|
||||
end
|
||||
directive_handlers[name] = wrapper
|
||||
end
|
||||
directive_handlers[name] = handler
|
||||
end
|
||||
|
||||
--- Lists the currently available directives to use in queries.
|
||||
@@ -1075,9 +1044,6 @@ end
|
||||
--- - 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).
|
||||
--- - all (boolean) When `false` (default `true`), the returned table maps capture IDs to a single
|
||||
--- (last) node instead of the full list of matching nodes. This option is only for backward
|
||||
--- compatibility and will be removed in a future release.
|
||||
---
|
||||
---@return (fun(): integer, table<integer, TSNode[]>, vim.treesitter.query.TSMetadata, TSTree): pattern id, match, metadata, tree
|
||||
function Query:iter_matches(node, source, start, stop, opts)
|
||||
@@ -1123,17 +1089,6 @@ function Query:iter_matches(node, source, start, stop, opts)
|
||||
metadata = self:_apply_directives(directives, pattern_i, captures, source)
|
||||
end
|
||||
|
||||
if opts.all == false then
|
||||
-- Convert the match table into the old buggy version for backward
|
||||
-- compatibility. This is slow, but we only do it when the caller explicitly opted into it by
|
||||
-- setting `all` to `false`.
|
||||
local old_match = {} ---@type table<integer, TSNode>
|
||||
for k, v in pairs(captures or {}) do
|
||||
old_match[k] = v[#v]
|
||||
end
|
||||
return pattern_i, old_match, metadata
|
||||
end
|
||||
|
||||
-- TODO(lewis6991): create a new function that returns {match, metadata}
|
||||
return pattern_i, captures, metadata, tree
|
||||
end
|
||||
|
||||
@@ -523,36 +523,6 @@ void ui_refresh(void)
|
||||
eq({ { 0, 4, 0, 8 } }, res)
|
||||
end
|
||||
|
||||
-- Once with the old API. Remove this whole 'do' block in 0.12
|
||||
do
|
||||
local res = exec_lua(function()
|
||||
local query = vim.treesitter.query
|
||||
|
||||
local function is_main(match, _pattern, bufnr, predicate)
|
||||
local node = match[predicate[2]]
|
||||
|
||||
return vim.treesitter.get_node_text(node, bufnr) == 'main'
|
||||
end
|
||||
|
||||
local parser = vim.treesitter.get_parser(0, 'c')
|
||||
|
||||
query.add_predicate('is-main?', is_main, { all = false, force = true })
|
||||
|
||||
local query0 = query.parse('c', custom_query)
|
||||
|
||||
local nodes = {}
|
||||
for _, node in query0:iter_captures(parser:parse()[1]:root(), 0) do
|
||||
table.insert(nodes, { node:range() })
|
||||
end
|
||||
|
||||
return nodes
|
||||
end)
|
||||
|
||||
-- Remove this 'do' block in 0.12
|
||||
-- eq(0, n.fn.has('nvim-0.12'))
|
||||
eq({ { 0, 4, 0, 8 } }, res)
|
||||
end
|
||||
|
||||
do
|
||||
local res = exec_lua(function()
|
||||
local query = vim.treesitter.query
|
||||
@@ -680,50 +650,6 @@ void ui_refresh(void)
|
||||
}, result)
|
||||
end)
|
||||
|
||||
it('supports the old broken version of iter_matches #24738', function()
|
||||
-- Delete this test in 0.12 when iter_matches is removed
|
||||
-- eq(0, n.fn.has('nvim-0.12'))
|
||||
|
||||
insert(test_text)
|
||||
local res = exec_lua(function()
|
||||
local cquery = vim.treesitter.query.parse('c', test_query)
|
||||
local parser = vim.treesitter.get_parser(0, 'c')
|
||||
local tree = parser:parse()[1]
|
||||
local res = {}
|
||||
for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = false }) do
|
||||
local mrepr = {}
|
||||
for cid, node in pairs(match) do
|
||||
table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() })
|
||||
end
|
||||
table.insert(res, { pattern, mrepr })
|
||||
end
|
||||
return res
|
||||
end)
|
||||
|
||||
eq({
|
||||
{ 3, { { '@type', 'primitive_type', 8, 2, 8, 6 } } },
|
||||
{ 2, { { '@keyword', 'for', 9, 2, 9, 5 } } },
|
||||
{ 3, { { '@type', 'primitive_type', 9, 7, 9, 13 } } },
|
||||
{ 4, { { '@fieldarg', 'identifier', 11, 16, 11, 18 } } },
|
||||
{
|
||||
1,
|
||||
{
|
||||
{ '@minfunc', 'identifier', 11, 12, 11, 15 },
|
||||
{ '@min_id', 'identifier', 11, 27, 11, 32 },
|
||||
},
|
||||
},
|
||||
{ 4, { { '@fieldarg', 'identifier', 12, 17, 12, 19 } } },
|
||||
{
|
||||
1,
|
||||
{
|
||||
{ '@minfunc', 'identifier', 12, 13, 12, 16 },
|
||||
{ '@min_id', 'identifier', 12, 29, 12, 35 },
|
||||
},
|
||||
},
|
||||
{ 4, { { '@fieldarg', 'identifier', 13, 14, 13, 16 } } },
|
||||
}, res)
|
||||
end)
|
||||
|
||||
it('should use node range when omitted', function()
|
||||
local txt = [[
|
||||
int foo = 42;
|
||||
|
||||
Reference in New Issue
Block a user