Files
neovim/test/functional/treesitter/testutil.lua
Riley Bruins 1d5b3b5b4c feat(treesitter)!: apply offset! directive to all captures #34383
This commit changes the `offset!` directive so that instead of setting a
`metadata.range` value for the entire pattern, it will set a
`metadata.offset` value. This offset will be applied to the range only
in `vim.treesitter.get_range()`, rather than at directive application
time. This allows the offset to be applied to any and all nodes captured
by the given pattern, and removes the requirement that `#offset!` be
applied to only a single node.

The downside of this change is that plugins which read from
`metadata.range` may be thrown off course, but such plugins should
prefer `vim.treesitter.get_range()` when retrieving ranges anyway.

Note that `#trim!` still sets `metadata.range`, and
`vim.treesitter.get_range()` still reads from `metadata.range`, if it
exists.
2025-06-13 06:42:10 -07:00

27 lines
727 B
Lua

local n = require('test.functional.testnvim')()
local exec_lua = n.exec_lua
local M = {}
---@param language string
---@param query_string string
function M.run_query(language, query_string)
return exec_lua(function(lang, query_str)
local query = vim.treesitter.query.parse(lang, query_str)
local parser = vim.treesitter.get_parser()
local tree = parser:parse()[1]
local Range = require('vim.treesitter._range')
local res = {}
for id, node, metadata in query:iter_captures(tree:root(), 0) do
table.insert(res, {
query.captures[id],
{ Range.unpack4(vim.treesitter.get_range(node, 0, metadata[id])) },
})
end
return res
end, language, query_string)
end
return M