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