testutil.lua 666 B

1234567891011121314151617181920212223242526
  1. local n = require('test.functional.testnvim')()
  2. local exec_lua = n.exec_lua
  3. local M = {}
  4. ---@param language string
  5. ---@param query_string string
  6. function M.run_query(language, query_string)
  7. return exec_lua(function(lang, query_str)
  8. local query = vim.treesitter.query.parse(lang, query_str)
  9. local parser = vim.treesitter.get_parser()
  10. local tree = parser:parse()[1]
  11. local res = {}
  12. for id, node, metadata in query:iter_captures(tree:root(), 0) do
  13. table.insert(
  14. res,
  15. { query.captures[id], metadata[id] and metadata[id].range or { node:range() } }
  16. )
  17. end
  18. return res
  19. end, language, query_string)
  20. end
  21. return M