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 eq =t . eq 7local exec_lua =n . exec_lua 8local insert =n . insert 9local assert_alive =n . assert_alive 10 11before_each ( clear ) 12 13local function lua_eval ( lua_expr ) 14return exec_lua ( 'return ' .. lua_expr ) 15end 16 17describe ( 'treesitter node API' , function () 18it ( 'double free tree' , function () 19insert ( 'F' ) 20exec_lua ( function () 21vim . treesitter . start ( 0 , 'lua' ) 22vim . treesitter . get_parser ( 0 ): parse () 23vim . treesitter . get_node (): tree () 24vim . treesitter . get_node (): tree () 25collectgarbage () 26end ) 27assert_alive () 28end ) 29 30it ( 'double free tree 2' , function () 31exec_lua ( function () 32local parser =vim . treesitter . get_parser ( 0 , 'c' ) 33local x =parser : parse ()[ 1 ]: root (): tree () 34vim . api . nvim_buf_set_text ( 0 , 0 , 0 , 0 , 0 , { 'y' } ) 35parser : parse () 36vim . api . nvim_buf_set_text ( 0 , 0 , 0 , 0 , 1 , { 'z' } ) 37parser : parse () 38collectgarbage () 39x : root () 40end ) 41assert_alive () 42end ) 43 44it ( 'get_node() with lang given' , function () 45-- this buffer doesn't have filetype set! 46insert ( 'local foo = function() end' ) 47exec_lua ( function () 48vim . treesitter . get_parser ( 0 , 'lua' ): parse () 49_G . node =vim . treesitter . get_node ( { 50bufnr =0 , 51pos ={ 0 , 6 } , -- on "foo" 52lang ='lua' , 53} ) 54end ) 55eq ( 'foo' , lua_eval ( 'vim.treesitter.get_node_text(node, 0)' )) 56eq ( 'identifier' , lua_eval ( 'node:type()' )) 57end ) 58 59it ( 'get_node() with anonymous nodes included' , function () 60insert ( [[print('test')]] ) 61 62exec_lua ( function () 63_G . parser =vim . treesitter . get_parser ( 0 , 'lua' ) 64_G . tree =_G . parser : parse ()[ 1 ] 65_G . node =vim . treesitter . get_node ( { 66bufnr =0 , 67pos ={ 0 , 6 } , -- on the first apostrophe 68include_anonymous =true , 69} ) 70end ) 71 72eq ( "'" , lua_eval ( 'node:type()' )) 73eq ( false , lua_eval ( 'node:named()' )) 74end ) 75 76it ( 'can move between siblings' , function () 77insert ( [[ 78int main(int x, int y, int z) { 79return x + y * z 80} 81]] ) 82 83exec_lua ( function () 84local parser =assert ( vim . treesitter . get_parser ( 0 , 'c' )) 85local tree =parser : parse ()[ 1 ] 86_G . root =tree : root () 87vim . treesitter . language . inspect ( 'c' ) 88 89function _G . node_text ( node ) 90return vim . treesitter . get_node_text ( node , 0 ) 91end 92end ) 93 94exec_lua 'node = root:descendant_for_range(0, 9, 0, 14)' 95eq ( 'int x' , lua_eval ( 'node_text(node)' )) 96 97exec_lua 'node = node:next_sibling()' 98eq ( ',' , lua_eval ( 'node_text(node)' )) 99 100exec_lua 'node = node:next_sibling()' 101eq ( 'int y' , lua_eval ( 'node_text(node)' )) 102 103exec_lua 'node = node:prev_sibling()' 104eq ( ',' , lua_eval ( 'node_text(node)' )) 105 106exec_lua 'node = node:prev_sibling()' 107eq ( 'int x' , lua_eval ( 'node_text(node)' )) 108 109exec_lua 'node = node:next_named_sibling()' 110eq ( 'int y' , lua_eval ( 'node_text(node)' )) 111 112exec_lua 'node = node:prev_named_sibling()' 113eq ( 'int x' , lua_eval ( 'node_text(node)' )) 114end ) 115 116it ( 'can retrieve the children of a node' , function () 117insert ( [[ 118int main() { 119int x = 3; 120}]] ) 121 122local len =exec_lua ( function () 123local tree =vim . treesitter . get_parser ( 0 , 'c' ): parse ()[ 1 ] 124local node =assert ( tree : root (): child ( 0 )) 125_G . children =node : named_children () 126 127return # _G . children 128end ) 129 130eq ( 3 , len ) 131eq ( '<node compound_statement>' , lua_eval ( 'tostring(children[3])' )) 132end ) 133 134it ( 'can retrieve the tree root given a node' , function () 135insert ( [[ 136int main() { 137int x = 3; 138}]] ) 139 140exec_lua ( function () 141local tree =vim . treesitter . get_parser ( 0 , 'c' ): parse ()[ 1 ] 142_G . root =tree : root () 143_G . node =_G . root : child ( 0 ): child ( 2 ) 144end ) 145 146eq ( lua_eval ( 'tostring(root)' ), lua_eval ( 'tostring(node:root())' )) 147end ) 148 149it ( 'can compute the byte length of a node' , function () 150insert ( [[ 151int main() { 152int x = 3; 153}]] ) 154 155exec_lua ( function () 156local tree =vim . treesitter . get_parser ( 0 , 'c' ): parse ()[ 1 ] 157_G . root =tree : root () 158_G . child =_G . root : child ( 0 ): child ( 0 ) 159end ) 160 161eq ( 28 , lua_eval ( 'root:byte_length()' )) 162eq ( 3 , lua_eval ( 'child:byte_length()' )) 163end ) 164 165it ( 'child_with_descendant() works' , function () 166insert ( [[ 167int main() { 168int x = 3; 169}]] ) 170 171exec_lua ( function () 172local tree =vim . treesitter . get_parser ( 0 , 'c' ): parse ()[ 1 ] 173_G . root =assert ( tree : root ()) 174_G . main =assert ( _G . root : child ( 0 )) 175_G . body =assert ( _G . main : child ( 2 )) 176_G . statement =assert ( _G . body : child ( 1 )) 177_G . declarator =assert ( _G . statement : child ( 1 )) 178_G . value =assert ( _G . declarator : child ( 1 )) 179end ) 180 181eq ( lua_eval ( 'main:type()' ), lua_eval ( 'root:child_with_descendant(value):type()' )) 182eq ( lua_eval ( 'body:type()' ), lua_eval ( 'main:child_with_descendant(value):type()' )) 183eq ( lua_eval ( 'statement:type()' ), lua_eval ( 'body:child_with_descendant(value):type()' )) 184eq ( lua_eval ( 'declarator:type()' ), lua_eval ( 'statement:child_with_descendant(value):type()' )) 185eq ( lua_eval ( 'value:type()' ), lua_eval ( 'declarator:child_with_descendant(value):type()' )) 186eq ( vim . NIL , lua_eval ( 'value:child_with_descendant(value)' )) 187end ) 188 189it ( 'gets all children with a given field name' , function () 190insert ( [[ 191function foo(a,b,c) 192end 193]] ) 194 195exec_lua ( function () 196local tree =vim . treesitter . get_parser ( 0 , 'lua' ): parse ()[ 1 ] 197_G . parameters_node =assert ( tree : root (): named_descendant_for_range ( 0 , 18 , 0 , 18 )) 198_G . children_by_field =_G . parameters_node : field ( 'name' ) 199end ) 200 201eq ( 'parameters' , lua_eval ( 'parameters_node:type()' )) 202eq ( 3 , lua_eval ( '#children_by_field' )) 203eq ( 'a' , lua_eval ( 'vim.treesitter.get_node_text(children_by_field[1], 0)' )) 204eq ( 'b' , lua_eval ( 'vim.treesitter.get_node_text(children_by_field[2], 0)' )) 205eq ( 'c' , lua_eval ( 'vim.treesitter.get_node_text(children_by_field[3], 0)' )) 206end ) 207 208it ( 'node access after tree edit' , function () 209insert ( [[ 210function x() 211return true 212end 213]] ) 214exec_lua ( function () 215local tree =vim . treesitter . get_parser ( 0 , 'lua' ): parse ()[ 1 ] 216 217-- ensure treesitter does not try to edit the tree inplace 218tree : copy () 219 220local node =tree : root (): child ( 0 ) 221vim . api . nvim_buf_set_lines ( 0 , 1 , 2 , false , {} ) 222vim . schedule ( function () 223collectgarbage () 224local a , b , c , d =node : range () 225_G . range ={ a , b , c , d } 226end ) 227end ) 228eq ( { 0 , 0 , 2 , 3 } , lua_eval ( 'range' )) 229end ) 230 231it ( 232'get_node_text() includes trailing newline for buffer source when node end_col == 0' , 233function () 234insert ( 'local x = 1' ) 235 236exec_lua ( function () 237_G . node =vim . treesitter . get_parser ( 0 , 'lua' ): parse ()[ 1 ]: root () 238end ) 239 240eq ( 'local x = 1\n' , lua_eval ( 'vim.treesitter.get_node_text(_G.node, 0)' )) 241end 242) 243 244it ( 245'get_node_text() includes trailing newline for string source when node end_col == 0' , 246function () 247exec_lua ( function () 248local source ='local x = 1\n' 249_G . source =source 250_G . node =vim . treesitter . get_string_parser ( source , 'lua' ): parse ()[ 1 ]: root () 251end ) 252 253eq ( 'local x = 1\n' , lua_eval ( 'vim.treesitter.get_node_text(_G.node, _G.source)' )) 254end 255) 256 257it ( 'tree:root() is idempotent' , function () 258insert ( [[ 259function x() 260return true 261end 262]] ) 263exec_lua ( function () 264local tree =vim . treesitter . get_parser ( 0 , 'lua' ): parse ()[ 1 ] 265assert ( tree : root () == tree : root ()) 266assert ( tree : root () == tree : root (): tree (): root ()) 267end ) 268end ) 269end )