utils_spec.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local insert = n.insert
  5. local eq = t.eq
  6. local exec_lua = n.exec_lua
  7. before_each(clear)
  8. describe('treesitter utils', function()
  9. before_each(clear)
  10. it('can find an ancestor', function()
  11. insert([[
  12. int main() {
  13. int x = 3;
  14. }]])
  15. exec_lua(function()
  16. local parser = vim.treesitter.get_parser(0, 'c')
  17. local tree = parser:parse()[1]
  18. local root = tree:root()
  19. _G.ancestor = assert(root:child(0))
  20. _G.child = assert(_G.ancestor:named_child(1))
  21. _G.child_sibling = assert(_G.ancestor:named_child(2))
  22. _G.grandchild = assert(_G.child:named_child(0))
  23. end)
  24. eq(true, exec_lua('return vim.treesitter.is_ancestor(_G.ancestor, _G.child)'))
  25. eq(true, exec_lua('return vim.treesitter.is_ancestor(_G.ancestor, _G.grandchild)'))
  26. eq(false, exec_lua('return vim.treesitter.is_ancestor(_G.child, _G.ancestor)'))
  27. eq(false, exec_lua('return vim.treesitter.is_ancestor(_G.child, _G.child_sibling)'))
  28. end)
  29. it('can detect if a position is contained in a node', function()
  30. exec_lua(function()
  31. _G.node = {
  32. range = function()
  33. return 0, 4, 0, 8
  34. end,
  35. }
  36. end)
  37. eq(false, exec_lua('return vim.treesitter.is_in_node_range(node, 0, 3)'))
  38. for i = 4, 7 do
  39. eq(true, exec_lua('return vim.treesitter.is_in_node_range(node, 0, ...)', i))
  40. end
  41. -- End column exclusive
  42. eq(false, exec_lua('return vim.treesitter.is_in_node_range(node, 0, 8)'))
  43. end)
  44. end)