diff options
author | Riley Bruins <ribru17@hotmail.com> | 2024-08-30 19:27:18 -0700 |
---|---|---|
committer | Riley Bruins <ribru17@hotmail.com> | 2024-12-06 08:36:28 -0800 |
commit | f0ea38a4bcb37aa6c68d498864c3c83f2867c4ad (patch) | |
tree | 40112cca07a35de2b78027147cfd6f471558ea93 /test/functional/treesitter/testutil.lua | |
parent | b8c75a31e6f4716f542cd2000e4a7c19c1ae9d70 (diff) | |
download | rneovim-f0ea38a4bcb37aa6c68d498864c3c83f2867c4ad.tar.gz rneovim-f0ea38a4bcb37aa6c68d498864c3c83f2867c4ad.tar.bz2 rneovim-f0ea38a4bcb37aa6c68d498864c3c83f2867c4ad.zip |
test(treesitter): add a simple testutil file
The util file, for now, just abstracts the common `run_query` function.
Diffstat (limited to 'test/functional/treesitter/testutil.lua')
-rw-r--r-- | test/functional/treesitter/testutil.lua | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/functional/treesitter/testutil.lua b/test/functional/treesitter/testutil.lua new file mode 100644 index 0000000000..f8934f06c3 --- /dev/null +++ b/test/functional/treesitter/testutil.lua @@ -0,0 +1,25 @@ +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 res = {} + for id, node, metadata in query:iter_captures(tree:root(), 0) do + table.insert( + res, + { query.captures[id], metadata[id] and metadata[id].range or { node:range() } } + ) + end + return res + end, language, query_string) +end + +return M |