robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local ts_t =require ( 'test.functional.treesitter.testutil' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear =n . clear 7local dedent =t . dedent 8local eq =t . eq 9local insert =n . insert 10local exec_lua =n . exec_lua 11local feed =n . feed 12local matches =t . matches 13local pcall_err =t . pcall_err 14local run_query =ts_t . run_query 15local assert_alive =n . assert_alive 16 17describe ( 'treesitter parser API' , function () 18before_each ( function () 19clear () 20exec_lua ( function () 21vim . g . __ts_debug =1 22end ) 23end ) 24 25it ( 'parses buffer' , function () 26insert ( [[ 27int main() { 28int x = 3; 29}]] ) 30 31exec_lua ( function () 32_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 33_G . tree =_G . parser : parse ()[ 1 ] 34_G . root =_G . tree : root () 35_G . lang =vim . treesitter . language . inspect ( 'c' ) 36end ) 37 38eq ( '<tree>' , exec_lua ( 'return tostring(tree)' )) 39eq ( '<node translation_unit>' , exec_lua ( 'return tostring(root)' )) 40eq ( { 0 , 0 , 3 , 0 } , exec_lua ( 'return {root:range()}' )) 41 42eq ( 1 , exec_lua ( 'return root:child_count()' )) 43exec_lua ( 'child = root:child(0)' ) 44eq ( '<node function_definition>' , exec_lua ( 'return tostring(child)' )) 45eq ( { 0 , 0 , 2 , 1 } , exec_lua ( 'return {child:range()}' )) 46 47eq ( 'function_definition' , exec_lua ( 'return child:type()' )) 48eq ( true , exec_lua ( 'return child:named()' )) 49eq ( 'number' , type ( exec_lua ( 'return child:symbol()' ))) 50eq ( true , exec_lua ( 'return lang.symbols[child:type()]' )) 51 52exec_lua ( 'anon = root:descendant_for_range(0,8,0,9)' ) 53eq ( '(' , exec_lua ( 'return anon:type()' )) 54eq ( false , exec_lua ( 'return anon:named()' )) 55eq ( 'number' , type ( exec_lua ( 'return anon:symbol()' ))) 56eq ( false , exec_lua ( [=[return lang.symbols[string.format('"%s"', anon:type())]]=] )) 57 58exec_lua ( 'descendant = root:descendant_for_range(1,2,1,12)' ) 59eq ( '<node declaration>' , exec_lua ( 'return tostring(descendant)' )) 60eq ( { 1 , 2 , 1 , 12 } , exec_lua ( 'return {descendant:range()}' )) 61eq ( 62'(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))' , 63exec_lua ( 'return descendant:sexpr()' ) 64) 65 66feed ( '2G7|ay' ) 67exec_lua ( function () 68_G . tree2 =_G . parser : parse ()[ 1 ] 69_G . root2 =_G . tree2 : root () 70_G . descendant2 =_G . root2 : descendant_for_range ( 1 , 2 , 1 , 13 ) 71end ) 72eq ( false , exec_lua ( 'return tree2 == tree1' )) 73eq ( false , exec_lua ( 'return root2 == root' )) 74eq ( '<node declaration>' , exec_lua ( 'return tostring(descendant2)' )) 75eq ( { 1 , 2 , 1 , 13 } , exec_lua ( 'return {descendant2:range()}' )) 76 77eq ( true , exec_lua ( 'return child == child' )) 78-- separate lua object, but represents same node 79eq ( true , exec_lua ( 'return child == root:child(0)' )) 80eq ( false , exec_lua ( 'return child == descendant2' )) 81eq ( false , exec_lua ( 'return child == nil' )) 82eq ( false , exec_lua ( 'return child == tree' )) 83 84eq ( 'string' , exec_lua ( 'return type(child:id())' )) 85eq ( true , exec_lua ( 'return child:id() == child:id()' )) 86-- separate lua object, but represents same node 87eq ( true , exec_lua ( 'return child:id() == root:child(0):id()' )) 88eq ( false , exec_lua ( 'return child:id() == descendant2:id()' )) 89eq ( false , exec_lua ( 'return child:id() == nil' )) 90eq ( false , exec_lua ( 'return child:id() == tree' )) 91 92-- unchanged buffer: return the same tree 93eq ( true , exec_lua ( 'return parser:parse()[1] == tree2' )) 94end ) 95 96it ( 'preserves full-document included ranges' , function () 97insert ( [[ 98local _ = 1 99]] ) 100 101eq ( 102{ 103{ 0 , 0 , 0 , 4294967295 , 4294967295 , 4294967295 } , 104} , 105exec_lua ( function () 106local parser =vim . treesitter . get_parser ( 0 , 'lua' ) 107local tree =parser : parse ()[ 1 ] 108return tree : included_ranges ( true ) 109end ) 110) 111end ) 112 113it ( 'ignores repeated parser finalization' , function () 114matches ( 115'Parser has been deleted' , 116pcall_err ( exec_lua , function () 117vim . treesitter . language . add ( 'c' ) 118 119local parser =vim . _create_ts_parser ( 'c' ) 120-- The parser metatable exposes __gc through __index, so this simulates a finalizer 121-- running before all Lua references to the userdata are gone. 122parser : __gc () 123parser : __gc () 124 125parser : parse ( nil , 'int x;' , false ) 126end ) 127) 128assert_alive () 129end ) 130 131it ( 'respects eol settings when parsing buffer' , function () 132insert ( [[ 133int main() { 134int x = 3; 135} // :D]] ) 136 137exec_lua ( function () 138vim . bo . eol =false 139vim . bo . fixeol =false 140_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 141_G . tree =_G . parser : parse ()[ 1 ] 142_G . root =_G . tree : root () 143_G . lang =vim . treesitter . language . inspect ( 'c' ) 144end ) 145 146eq ( 147'<node translation_unit>' , 148exec_lua ( function () 149return tostring ( _G . root ) 150end ) 151) 152eq ( 153{ 0 , 0 , 0 , 2 , 7 , 33 } , 154exec_lua ( function () 155return { _G . root : range ( true ) } 156end ) 157) 158 159-- NOTE: Changing these settings marks the buffer as `modified` but does not fire `on_bytes`, 160-- meaning this test case does not pass... is this intended? 161-- exec_lua(function() 162-- vim.bo.eol = true 163-- vim.bo.fixeol = true 164-- vim.cmd.update() 165-- _G.parser = vim.treesitter.get_parser(0, 'c') 166-- _G.tree = _G.parser:parse()[1] 167-- _G.root = _G.tree:root() 168-- _G.lang = vim.treesitter.language.inspect('c') 169-- end) 170-- 171-- eq( 172-- '<node translation_unit>', 173-- exec_lua(function() 174-- return tostring(root) 175-- end) 176-- ) 177-- eq( 178-- { 0, 0, 0, 3, 0, 34 }, 179-- exec_lua(function() 180-- return { root:range(true) } 181-- end) 182-- ) 183end ) 184 185it ( 'parses buffer asynchronously' , function () 186insert ( [[ 187int main() { 188int x = 3; 189}]] ) 190 191exec_lua ( function () 192_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 193_G . lang =vim . treesitter . language . inspect ( 'c' ) 194_G . parser : parse ( nil , function ( _ , trees ) 195_G . tree =trees [ 1 ] 196_G . root =_G . tree : root () 197end ) 198vim . wait ( 100 , function () end ) 199end ) 200 201eq ( '<tree>' , exec_lua ( 'return tostring(tree)' )) 202eq ( '<node translation_unit>' , exec_lua ( 'return tostring(root)' )) 203eq ( { 0 , 0 , 3 , 0 } , exec_lua ( 'return {root:range()}' )) 204 205eq ( 1 , exec_lua ( 'return root:child_count()' )) 206exec_lua ( 'child = root:child(0)' ) 207eq ( '<node function_definition>' , exec_lua ( 'return tostring(child)' )) 208eq ( { 0 , 0 , 2 , 1 } , exec_lua ( 'return {child:range()}' )) 209 210eq ( 'function_definition' , exec_lua ( 'return child:type()' )) 211eq ( true , exec_lua ( 'return child:named()' )) 212eq ( 'number' , type ( exec_lua ( 'return child:symbol()' ))) 213eq ( true , exec_lua ( 'return lang.symbols[child:type()]' )) 214 215exec_lua ( 'anon = root:descendant_for_range(0,8,0,9)' ) 216eq ( '(' , exec_lua ( 'return anon:type()' )) 217eq ( false , exec_lua ( 'return anon:named()' )) 218eq ( 'number' , type ( exec_lua ( 'return anon:symbol()' ))) 219eq ( false , exec_lua ( [=[return lang.symbols[string.format('"%s"', anon:type())]]=] )) 220 221exec_lua ( 'descendant = root:descendant_for_range(1,2,1,12)' ) 222eq ( '<node declaration>' , exec_lua ( 'return tostring(descendant)' )) 223eq ( { 1 , 2 , 1 , 12 } , exec_lua ( 'return {descendant:range()}' )) 224eq ( 225'(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))' , 226exec_lua ( 'return descendant:sexpr()' ) 227) 228 229feed ( '2G7|ay' ) 230exec_lua ( function () 231_G . parser : parse ( nil , function ( _ , trees ) 232_G . tree2 =trees [ 1 ] 233_G . root2 =_G . tree2 : root () 234_G . descendant2 =_G . root2 : descendant_for_range ( 1 , 2 , 1 , 13 ) 235end ) 236vim . wait ( 100 , function () end ) 237end ) 238eq ( false , exec_lua ( 'return tree2 == tree1' )) 239eq ( false , exec_lua ( 'return root2 == root' )) 240eq ( '<node declaration>' , exec_lua ( 'return tostring(descendant2)' )) 241eq ( { 1 , 2 , 1 , 13 } , exec_lua ( 'return {descendant2:range()}' )) 242 243eq ( true , exec_lua ( 'return child == child' )) 244-- separate lua object, but represents same node 245eq ( true , exec_lua ( 'return child == root:child(0)' )) 246eq ( false , exec_lua ( 'return child == descendant2' )) 247eq ( false , exec_lua ( 'return child == nil' )) 248eq ( false , exec_lua ( 'return child == tree' )) 249 250eq ( 'string' , exec_lua ( 'return type(child:id())' )) 251eq ( true , exec_lua ( 'return child:id() == child:id()' )) 252-- separate lua object, but represents same node 253eq ( true , exec_lua ( 'return child:id() == root:child(0):id()' )) 254eq ( false , exec_lua ( 'return child:id() == descendant2:id()' )) 255eq ( false , exec_lua ( 'return child:id() == nil' )) 256eq ( false , exec_lua ( 'return child:id() == tree' )) 257 258-- unchanged buffer: return the same tree 259eq ( true , exec_lua ( 'return parser:parse()[1] == tree2' )) 260end ) 261 262it ( 'does not crash when editing large files' , function () 263insert ( [[printf("%s", "some text");]] ) 264feed ( 'yy49999p' ) 265 266exec_lua ( function () 267_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 268_G . done =false 269vim . treesitter . start ( 0 , 'c' ) 270_G . parser : parse ( nil , function () 271_G . done =true 272end ) 273while not _G . done do 274-- Busy wait until async parsing has completed 275vim . wait ( 100 , function () end ) 276end 277end ) 278 279eq ( true , exec_lua ( [[return done]] )) 280exec_lua ( function () 281vim . api . nvim_input ( 'Lxj' ) 282end ) 283exec_lua ( function () 284vim . api . nvim_input ( 'xj' ) 285end ) 286exec_lua ( function () 287vim . api . nvim_input ( 'xj' ) 288end ) 289assert_alive () 290end ) 291 292it ( 'resets parsing state on tree changes' , function () 293insert ( [[vim.api.nvim_set_hl(0, 'test2', { bg = 'green' })]] ) 294feed ( 'yy1000p' ) 295 296exec_lua ( function () 297vim . cmd ( 'set ft=lua' ) 298 299vim . treesitter . start ( 0 ) 300local parser =assert ( vim . treesitter . get_parser ( 0 )) 301 302parser : parse ( true , function () end ) 303vim . api . nvim_buf_set_lines ( 0 , 1 , - 1 , false , {} ) 304parser : parse ( true ) 305end ) 306end ) 307 308it ( 'resets when buffer was editing during an async parse' , function () 309insert ( [[printf("%s", "some text");]] ) 310feed ( 'yy49999p' ) 311feed ( 'gg4jO// Comment<Esc>' ) 312 313exec_lua ( function () 314_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 315_G . done =false 316vim . treesitter . start ( 0 , 'c' ) 317_G . parser : parse ( nil , function () 318_G . done =true 319end ) 320end ) 321 322exec_lua ( function () 323vim . api . nvim_input ( 'ggdj' ) 324end ) 325 326eq ( false , exec_lua ( [[return done]] )) 327exec_lua ( function () 328while not _G . done do 329-- Busy wait until async parsing finishes 330vim . wait ( 100 , function () end ) 331end 332end ) 333eq ( true , exec_lua ( [[return done]] )) 334eq ( 'comment' , exec_lua ( [[return parser:parse()[1]:root():named_child(2):type()]] )) 335eq ( { 2 , 0 , 2 , 10 } , exec_lua ( [[return {parser:parse()[1]:root():named_child(2):range()}]] )) 336end ) 337 338it ( 'handles multiple async parse calls' , function () 339insert ( [[printf("%s", "some text");]] ) 340feed ( 'yy49999p' ) 341 342exec_lua ( function () 343-- Spy on vim.schedule 344local schedule =vim . schedule 345vim . schedule =function ( fn ) 346_G . schedules =_G . schedules + 1 347schedule ( fn ) 348end 349_G . schedules =0 350_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 351for i =1 , 5 do 352_G [ 'done' .. i ] =false 353_G . parser : parse ( nil , function () 354_G [ 'done' .. i ] =true 355end ) 356end 357schedule ( function () 358_G . schedules_snapshot =_G . schedules 359end ) 360end ) 361 362eq ( 2 , exec_lua ( [[return schedules_snapshot]] )) 363eq ( 364{ false , false , false , false , false } , 365exec_lua ( [[return { done1, done2, done3, done4, done5 }]] ) 366) 367exec_lua ( function () 368while not _G . done1 do 369-- Busy wait until async parsing finishes 370vim . wait ( 100 , function () end ) 371end 372end ) 373eq ( { true , true , true , true , true } , exec_lua ( [[return { done1, done2, done3, done4, done5 }]] )) 374end ) 375 376local test_text =[[ 377void ui_refresh(void) 378{ 379int width = INT_MAX, height = INT_MAX; 380bool ext_widgets[kUIExtCount]; 381for (UIExtension i = 0; (int)i < kUIExtCount; i++) { 382ext_widgets[i] = true; 383} 384 385bool inclusive = ui_override(); 386for (size_t i = 0; i < ui_count; i++) { 387UI *ui = uis[i]; 388width = MIN(ui->width, width); 389height = MIN(ui->height, height); 390foo = BAR(ui->bazaar, bazaar); 391for (UIExtension j = 0; (int)j < kUIExtCount; j++) { 392ext_widgets[j] &= (ui->ui_ext[j] || inclusive); 393} 394} 395}]] 396 397it ( 'can iterate over nodes children' , function () 398insert ( test_text ) 399 400local res =exec_lua ( function () 401local parser =vim . treesitter . get_parser ( 0 , 'c' ) 402 403local func_node =parser : parse ()[ 1 ]: root (): child ( 0 ) 404 405local res ={} 406for node , field in func_node : iter_children () do 407table . insert ( res , { node : type (), field } ) 408end 409return res 410end ) 411 412eq ( { 413{ 'primitive_type' , 'type' } , 414{ 'function_declarator' , 'declarator' } , 415{ 'compound_statement' , 'body' } , 416} , res ) 417end ) 418 419it ( 'does not get parser for empty filetype' , function () 420insert ( test_text ) 421 422local parser , error =exec_lua ( function () 423return vim . treesitter . get_parser ( 0 ) 424end ) 425eq ( vim . NIL , parser ) 426eq ( 'Parser not found for buffer 1: language could not be determined' , error ) 427 428-- Must provide language for buffers with an empty filetype 429exec_lua ( "vim.treesitter.get_parser(0, 'c')" ) 430end ) 431 432it ( 'can get a child by field' , function () 433insert ( test_text ) 434 435local res =exec_lua ( function () 436local parser =vim . treesitter . get_parser ( 0 , 'c' ) 437 438_G . func_node =parser : parse ()[ 1 ]: root (): child ( 0 ) 439 440local res ={} 441for _ , node in ipairs ( _G . func_node : field ( 'type' )) do 442table . insert ( res , { node : type (), node : range () } ) 443end 444return res 445end ) 446 447eq ( { { 'primitive_type' , 0 , 0 , 0 , 4 } } , res ) 448 449local res_fail =exec_lua ( function () 450vim . treesitter . get_parser ( 0 , 'c' ) 451 452return # _G . func_node : field ( 'foo' ) == 0 453end ) 454 455assert ( res_fail ) 456end ) 457 458it ( 'can get text of multiline node' , function () 459insert ( test_text ) 460local res =exec_lua ( function () 461local parser =vim . treesitter . get_parser ( 0 , 'c' ) 462local tree =parser : parse ()[ 1 ] 463return vim . treesitter . get_node_text ( tree : root (), 0 ) 464end ) 465eq ( t . dedent ( test_text ) .. '\n' , res ) 466 467local res2 =exec_lua ( function () 468local parser =vim . treesitter . get_parser ( 0 , 'c' ) 469local root =parser : parse ()[ 1 ]: root () 470return vim . treesitter . get_node_text ( root : child ( 0 ): child ( 0 ), 0 ) 471end ) 472eq ( 'void' , res2 ) 473end ) 474 475it ( 'can get text where start of node is one past EOF' , function () 476local text =[[ 477def run 478a = <<~E 479end]] 480insert ( text ) 481eq ( 482'' , 483exec_lua ( function () 484local fake_node ={} 485function fake_node : start () 486return 3 , 0 , 23 487end 488function fake_node : end_ () 489return 3 , 0 , 23 490end 491function fake_node : range ( bytes ) 492if bytes then 493return 3 , 0 , 23 , 3 , 0 , 23 494end 495return 3 , 0 , 3 , 0 496end 497return vim . treesitter . get_node_text ( fake_node , 0 ) 498end ) 499) 500end ) 501 502it ( 'can get empty text if node range is zero-width' , function () 503local text =[[ 504```lua 505{} 506``` 507]] 508insert ( text ) 509local result =exec_lua ( function () 510local fake_node ={} 511function fake_node : start () 512return 1 , 0 , 7 513end 514function fake_node : end_ () 515return 1 , 0 , 7 516end 517function fake_node : range () 518return 1 , 0 , 1 , 0 519end 520return vim . treesitter . get_node_text ( fake_node , 0 ) == '' 521end ) 522eq ( true , result ) 523end ) 524 525it ( 'can set simple ranges' , function () 526insert ( test_text ) 527 528local res =exec_lua ( function () 529_G . parser =vim . treesitter . get_parser ( 0 , 'c' ) 530return { _G . parser : parse ()[ 1 ]: root (): range () } 531end ) 532 533eq ( { 0 , 0 , 19 , 0 } , res ) 534 535-- The following sets the included ranges for the current parser 536-- As stated here, this only includes the function (thus the whole buffer, without the last line) 537local res2 =exec_lua ( function () 538local root =_G . parser : parse ()[ 1 ]: root () 539_G . parser : set_included_regions ( { { root : child ( 0 ) } } ) 540_G . parser : invalidate () 541return { _G . parser : parse ( true )[ 1 ]: root (): range () } 542end ) 543 544eq ( { 0 , 0 , 18 , 1 } , res2 ) 545 546eq ( { { { 0 , 0 , 0 , 18 , 1 , 512 } } } , exec_lua [[ return parser:included_regions() ]] ) 547 548local range_tbl =exec_lua ( function () 549_G . parser : set_included_regions { { { 0 , 0 , 17 , 1 } } } 550_G . parser : parse () 551return _G . parser : included_regions () 552end ) 553 554eq ( { { { 0 , 0 , 0 , 17 , 1 , 508 } } } , range_tbl ) 555end ) 556 557it ( 'can set complex ranges' , function () 558insert ( test_text ) 559 560local res =exec_lua ( function () 561local parser =vim . treesitter . get_parser ( 0 , 'c' ) 562local query =vim . treesitter . query . parse ( 'c' , '(declaration) @decl' ) 563 564local nodes ={} 565for _ , node in query : iter_captures ( parser : parse ()[ 1 ]: root (), 0 ) do 566table . insert ( nodes , node ) 567end 568 569parser : set_included_regions ( { nodes } ) 570 571local root =parser : parse ( true )[ 1 ]: root () 572 573local res ={} 574for i =0 , ( root : named_child_count () - 1 ) do 575table . insert ( res , { root : named_child ( i ): range () } ) 576end 577return res 578end ) 579 580eq ( { 581{ 2 , 2 , 2 , 40 } , 582{ 3 , 2 , 3 , 32 } , 583{ 4 , 7 , 4 , 25 } , 584{ 8 , 2 , 8 , 33 } , 585{ 9 , 7 , 9 , 20 } , 586{ 10 , 4 , 10 , 20 } , 587{ 14 , 9 , 14 , 27 } , 588} , res ) 589end ) 590 591it ( 'can create string parsers' , function () 592local ret =exec_lua ( function () 593local parser =vim . treesitter . get_string_parser ( 'int foo = 42;' , 'c' ) 594return { parser : parse ()[ 1 ]: root (): range () } 595end ) 596 597eq ( { 0 , 0 , 0 , 13 } , ret ) 598end ) 599 600it ( 'can run async parses with string parsers' , function () 601local ret =exec_lua ( function () 602local parser =vim . treesitter . get_string_parser ( 'int foo = 42;' , 'c' ) 603return { parser : parse ( nil , function () end )[ 1 ]: root (): range () } 604end ) 605 606eq ( { 0 , 0 , 0 , 13 } , ret ) 607end ) 608 609it ( 'can run queries with string parsers' , function () 610local txt =[[ 611int foo = 42; 612int bar = 13; 613]] 614 615local ret =exec_lua ( function ( str ) 616local parser =vim . treesitter . get_string_parser ( str , 'c' ) 617 618local nodes ={} 619local query =vim . treesitter . query . parse ( 'c' , '((identifier) @id (#eq? @id "foo"))' ) 620 621for _ , node in query : iter_captures ( parser : parse ()[ 1 ]: root (), str ) do 622table . insert ( nodes , { node : range () } ) 623end 624 625return nodes 626end , txt ) 627 628eq ( { { 0 , 10 , 0 , 13 } } , ret ) 629end ) 630 631describe ( 'creating a language tree' , function () 632local function get_ranges () 633return exec_lua ( function () 634local result ={} 635_G . parser : for_each_tree ( function ( tree ) 636table . insert ( result , { tree : root (): range () } ) 637end ) 638return result 639end ) 640end 641 642before_each ( function () 643insert ( [[ 644int x = INT_MAX; 645#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 646#define READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 647#define VALUE 123 648#define VALUE1 123 649#define VALUE2 123 650]] ) 651end ) 652 653describe ( 'parsing regions independently' , function () 654it ( 'injects a language' , function () 655exec_lua ( function () 656_G . parser =vim . treesitter . get_parser ( 0 , 'c' , { 657injections ={ 658c =( 659'(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) ' 660.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' 661), 662} , 663} ) 664_G . parser : parse ( true ) 665end ) 666 667eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 668eq ( 5 , exec_lua ( 'return #parser:children().c:trees()' )) 669eq ( { 670{ 0 , 0 , 7 , 0 } , -- root tree 671{ 1 , 26 , 1 , 63 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 672{ 2 , 29 , 2 , 66 } , -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 673{ 3 , 14 , 3 , 17 } , -- VALUE 123 674{ 4 , 15 , 4 , 18 } , -- VALUE1 123 675{ 5 , 15 , 5 , 18 } , -- VALUE2 123 676} , get_ranges ()) 677 678n . feed ( 'ggo<esc>' ) 679eq ( 5 , exec_lua ( 'return #parser:children().c:trees()' )) 680eq ( { 681{ 0 , 0 , 8 , 0 } , -- root tree 682{ 2 , 26 , 2 , 63 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 683{ 3 , 29 , 3 , 66 } , -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 684{ 4 , 14 , 4 , 17 } , -- VALUE 123 685{ 5 , 15 , 5 , 18 } , -- VALUE1 123 686{ 6 , 15 , 6 , 18 } , -- VALUE2 123 687} , get_ranges ()) 688end ) 689 690it ( 'notifies changedtree callbacks when replacing injection regions' , function () 691exec_lua ( function () 692_G . parser =vim . treesitter . _create_parser ( 0 , 'c' , { 693injections ={ 694c ='(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))' , 695} , 696} ) 697_G . parser : parse ( true ) 698 699local child =_G . parser : children (). c 700vim . api . nvim_buf_set_lines ( 0 , 3 , 4 , false , { '#define VALUE changed' } ) 701 702_G . changedtree =0 703_G . precise_changedtree =true 704child : register_cbs ( { 705on_changedtree =function ( changes , tree ) 706_G . changedtree =_G . changedtree + 1 707_G . precise_changedtree =_G . precise_changedtree 708and vim . deep_equal ( changes , tree : included_ranges ( true )) 709end , 710} ) 711 712_G . parser : parse ( { 3 , 4 } ) 713end ) 714 715eq ( true , exec_lua ( 'return _G.changedtree > 0 and _G.precise_changedtree' )) 716end ) 717end ) 718 719describe ( 'when parsing regions combined' , function () 720it ( 'injects a language' , function () 721exec_lua ( function () 722_G . parser =vim . treesitter . get_parser ( 0 , 'c' , { 723injections ={ 724c =( 725'(preproc_def (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined)) ' 726.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined))' 727), 728} , 729} ) 730_G . parser : parse ( true ) 731end ) 732 733eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 734eq ( 2 , exec_lua ( 'return #parser:children().c:trees()' )) 735eq ( { 736{ 0 , 0 , 7 , 0 } , -- root tree 737{ 1 , 26 , 2 , 66 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 738-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 739{ 3 , 14 , 5 , 18 } , -- VALUE 123 740-- VALUE1 123 741-- VALUE2 123 742} , get_ranges ()) 743 744n . feed ( 'ggo<esc>' ) 745eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 746eq ( 2 , exec_lua ( 'return #parser:children().c:trees()' )) 747eq ( { 748{ 0 , 0 , 8 , 0 } , -- root tree 749{ 2 , 26 , 3 , 66 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 750-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 751-- VALUE 123 752{ 4 , 14 , 6 , 18 } , -- VALUE1 123 753-- VALUE2 123 754} , get_ranges ()) 755 756n . feed ( '7ggI//<esc>' ) 757exec_lua ( [[parser:parse(true)]] ) 758eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 759eq ( 2 , exec_lua ( 'return #parser:children().c:trees()' )) 760eq ( { 761{ 0 , 0 , 8 , 0 } , -- root tree 762{ 2 , 26 , 3 , 66 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 763-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 764-- VALUE 123 765{ 4 , 14 , 5 , 18 } , -- VALUE1 123 766} , get_ranges ()) 767end ) 768 769it ( 'scopes injections appropriately' , function () 770-- `injection.combined` are combined within a TSTree. 771-- Lua injections on lines 2-4 should be combined within their 772-- respective C injection trees, and lua injections on lines 0 and 6 773-- are separate from each other and other lua injections on lines 2-4. 774 775exec_lua ( function () 776local lines ={ 777[[func('int a = func("local a = [=[");')]] , 778[[]] , 779[[func('int a = func("local a = 6") + func("+ 3");')]] , 780[[func('int a = func("local a = 6") + func("+ 3");')]] , 781[[func('int a = func("local a = 6") + func("+ 3");')]] , 782[[]] , 783[[func('int a = func("]=]");')]] , 784} 785vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 786_G . parser =vim . treesitter . get_parser ( 0 , 'lua' , { 787injections ={ 788lua =[[ 789((function_call 790arguments: (arguments 791(string (string_content) @injection.content))) 792(#set! injection.language "c")) 793]] , 794c =[[ 795((call_expression 796arguments: (argument_list 797(string_literal (string_content) @injection.content))) 798(#set! injection.combined) 799(#set! injection.language "lua")) 800]] , 801} , 802} ) 803 804function _G . langtree_regions ( parser ) 805local result_regions ={} 806 807local regions =parser : included_regions () 808for region_i , region in pairs ( regions ) do 809local result_region ={} 810 811for _ , range in ipairs ( region ) do 812table . insert ( result_region , { 813range [ 1 ], 814range [ 2 ], 815range [ 4 ], 816range [ 5 ], 817} ) 818end 819 820result_regions [ region_i ] =result_region 821end 822 823return result_regions 824end 825function _G . all_regions ( parser ) 826local this_regions =_G . langtree_regions ( parser ) 827local child_regions ={} 828for lang , child in pairs ( parser : children ()) do 829child_regions [ lang ] =_G . all_regions ( child ) 830end 831return { regions =this_regions , children =child_regions } 832end 833end ) 834 835local expected_regions ={ 836children ={} , -- nothing is parsed 837regions ={ 838{} , -- root tree's regions is the entire buffer 839} , 840} 841eq ( expected_regions , exec_lua ( 'return all_regions(_G.parser)' )) 842 843exec_lua ( '_G.parser:parse({ 3, 0, 3, 45 })' ) 844 845expected_regions ={ 846children ={ 847c ={ 848children ={ 849lua ={ 850children ={} , 851regions ={ 852{ { 3 , 20 , 3 , 31 } , { 3 , 42 , 3 , 45 } } , 853} , 854} , 855} , 856regions ={ 857{ { 3 , 6 , 3 , 48 } } , 858} , 859} , 860} , 861regions ={ 862{} , 863} , 864} 865eq ( expected_regions , exec_lua ( 'return all_regions(_G.parser)' )) 866 867exec_lua ( '_G.parser:parse(true)' ) 868expected_regions ={ 869children ={ 870c ={ 871children ={ 872lua ={ 873children ={} , 874regions ={ 875{ { 0 , 20 , 0 , 33 } } , 876{ { 2 , 20 , 2 , 31 } , { 2 , 42 , 2 , 45 } } , 877{ { 3 , 20 , 3 , 31 } , { 3 , 42 , 3 , 45 } } , 878{ { 4 , 20 , 4 , 31 } , { 4 , 42 , 4 , 45 } } , 879{ { 6 , 20 , 6 , 23 } } , 880} , 881} , 882} , 883regions ={ 884{ { 0 , 6 , 0 , 36 } } , 885{ { 2 , 6 , 2 , 48 } } , 886{ { 3 , 6 , 3 , 48 } } , 887{ { 4 , 6 , 4 , 48 } } , 888{ { 6 , 6 , 6 , 26 } } , 889} , 890} , 891} , 892regions ={ 893{} , 894} , 895} 896eq ( expected_regions , exec_lua ( 'return all_regions(_G.parser)' )) 897end ) 898end ) 899 900describe ( 'when using injection.self' , function () 901it ( 'injects the source language' , function () 902exec_lua ( function () 903_G . parser =vim . treesitter . get_parser ( 0 , 'c' , { 904injections ={ 905c =( 906'(preproc_def (preproc_arg) @injection.content (#set! injection.self)) ' 907.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))' 908), 909} , 910} ) 911_G . parser : parse ( true ) 912end ) 913 914eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 915eq ( 5 , exec_lua ( 'return #parser:children().c:trees()' )) 916eq ( { 917{ 0 , 0 , 7 , 0 } , -- root tree 918{ 1 , 26 , 1 , 63 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 919{ 2 , 29 , 2 , 66 } , -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 920{ 3 , 14 , 3 , 17 } , -- VALUE 123 921{ 4 , 15 , 4 , 18 } , -- VALUE1 123 922{ 5 , 15 , 5 , 18 } , -- VALUE2 123 923} , get_ranges ()) 924 925n . feed ( 'ggo<esc>' ) 926eq ( 5 , exec_lua ( 'return #parser:children().c:trees()' )) 927eq ( { 928{ 0 , 0 , 8 , 0 } , -- root tree 929{ 2 , 26 , 2 , 63 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 930{ 3 , 29 , 3 , 66 } , -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 931{ 4 , 14 , 4 , 17 } , -- VALUE 123 932{ 5 , 15 , 5 , 18 } , -- VALUE1 123 933{ 6 , 15 , 6 , 18 } , -- VALUE2 123 934} , get_ranges ()) 935end ) 936end ) 937 938describe ( 'when using the offset directive' , function () 939it ( 'shifts the range by the directive amount' , function () 940exec_lua ( function () 941_G . parser =vim . treesitter . get_parser ( 0 , 'c' , { 942injections ={ 943c =( 944'(preproc_def ((preproc_arg) @injection.content (#set! injection.language "c") (#offset! @injection.content 0 2 0 -1))) ' 945.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))' 946), 947} , 948} ) 949_G . parser : parse ( true ) 950end ) 951 952eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 953eq ( { 954{ 0 , 0 , 7 , 0 } , -- root tree 955{ 1 , 26 , 1 , 63 } , -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) 956{ 2 , 29 , 2 , 66 } , -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y)) 957{ 3 , 16 , 3 , 16 } , -- VALUE 123 958{ 4 , 17 , 4 , 17 } , -- VALUE1 123 959{ 5 , 17 , 5 , 17 } , -- VALUE2 123 960} , get_ranges ()) 961end ) 962it ( 'applies offsets to quantified captures' , function () 963local function get_ltree_ranges () 964return exec_lua ( function () 965local result ={} 966_G . parser : for_each_tree ( function ( _ , ltree ) 967table . insert ( result , ltree : included_regions ()) 968end ) 969return result 970end ) 971end 972 973exec_lua ( function () 974_G . parser =vim . treesitter . get_parser ( 0 , 'c' , { 975injections ={ 976c ='((preproc_def (preproc_arg) @injection.content)+ (#set! injection.language "c") (#offset! @injection.content 0 1 0 -1))' , 977} , 978} ) 979_G . parser : parse ( true ) 980end ) 981 982eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 983eq ( { 984{ {} } , -- root tree 985{ 986{ 987{ 3 , 15 , 163 , 3 , 16 , 164 } , -- VALUE 123 988{ 4 , 16 , 182 , 4 , 17 , 183 } , -- VALUE1 123 989{ 5 , 16 , 201 , 5 , 17 , 202 } , -- VALUE2 123 990} , 991} , 992} , get_ltree_ranges ()) 993end ) 994it ( 'lists all directives' , function () 995local res_list =exec_lua ( function () 996local query =vim . treesitter . query 997 998local list =query . list_directives () 999 1000table . sort ( list ) 1001 1002return list 1003end ) 1004 1005eq ( { 'gsub!' , 'offset!' , 'set!' , 'trim!' } , res_list ) 1006end ) 1007end ) 1008 1009it ( 'normalizes hyphens to underscores in injection.language' , function () 1010exec_lua ( function () 1011-- register "c" as the parser for "shell_session" (normalized form of "shell-session") 1012vim . treesitter . language . register ( 'c' , 'shell_session' ) 1013_G . parser =vim . treesitter . get_parser ( 0 , 'c' , { 1014injections ={ 1015c ='(preproc_def (preproc_arg) @injection.content (#set! injection.language "shell-session"))' , 1016} , 1017} ) 1018_G . parser : parse ( true ) 1019end ) 1020 1021-- injection.language "shell-session" should normalize to "shell_session", 1022-- which resolves via the registered alias to the "c" parser 1023eq ( 'table' , exec_lua ( 'return type(parser:children().c)' )) 1024end ) 1025end ) 1026 1027it ( 'clips nested injections #34098' , function () 1028insert ( [=[ 1029```lua 1030vim.cmd([[ 1031set noswapfile 1032set noswapfile 1033set noswapfile 1034]]) 1035``` 1036]=] ) 1037 1038local result =exec_lua ( function () 1039local parser =vim . treesitter . get_parser ( 0 , 'markdown' ) 1040parser : parse ( true ) 1041 1042return parser . _children . lua . _children . vim : included_regions () 1043end ) 1044 1045local expected ={ 1046{ 1047{ 1 , 10 , 17 , 2 , 0 , 18 } , 1048{ 2 , 0 , 18 , 3 , 0 , 33 } , 1049{ 3 , 0 , 33 , 4 , 0 , 48 } , 1050{ 4 , 0 , 48 , 5 , 0 , 63 } , 1051} , 1052} 1053eq ( expected , result ) 1054end ) 1055 1056describe ( 'when getting the language for a range' , function () 1057before_each ( function () 1058insert ( [[ 1059int x = INT_MAX; 1060#define VALUE 123456789 1061]] ) 1062end ) 1063 1064it ( 'returns the correct language tree' , function () 1065local result =exec_lua ( function () 1066local parser =vim . treesitter . get_parser ( 0 , 'c' , { 1067injections ={ 1068c ='(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))' , 1069} , 1070} ) 1071parser : parse ( true ) 1072 1073local sub_tree =parser : language_for_range ( { 1 , 18 , 1 , 19 } ) 1074 1075return sub_tree == parser : children (). c 1076end ) 1077 1078eq ( true , result ) 1079end ) 1080end ) 1081 1082describe ( 'when setting the node for an injection' , function () 1083before_each ( function () 1084insert ( [[ 1085print() 1086]] ) 1087end ) 1088 1089it ( 'ignores optional captures #23100' , function () 1090local result =exec_lua ( function () 1091local parser =vim . treesitter . get_parser ( 0 , 'lua' , { 1092injections ={ 1093lua =( 1094'(function_call ' 1095.. '(arguments ' 1096.. '(string)? @injection.content ' 1097.. '(number)? @injection.content ' 1098.. '(#offset! @injection.content 0 1 0 -1) ' 1099.. '(#set! injection.language "c")))' 1100), 1101} , 1102} ) 1103parser : parse ( true ) 1104 1105return parser : is_valid () 1106end ) 1107 1108eq ( true , result ) 1109end ) 1110end ) 1111 1112describe ( 'when getting/setting match data' , function () 1113describe ( 'when setting for the whole match' , function () 1114it ( 'sets/gets the data correctly' , function () 1115insert ( [[ 1116int x = 3; 1117]] ) 1118 1119local result =exec_lua ( function () 1120local query = 1121vim . treesitter . query . parse ( 'c' , '((number_literal) @number (#set! "key" "value"))' ) 1122local parser =vim . treesitter . get_parser ( 0 , 'c' ) 1123 1124local _ , _ , metadata =query : iter_matches ( parser : parse ()[ 1 ]: root (), 0 , 0 , - 1 )() 1125return metadata . key 1126end ) 1127 1128eq ( 'value' , result ) 1129end ) 1130 1131describe ( 'when setting a key on a capture' , function () 1132it ( 'creates the nested table' , function () 1133insert ( [[ 1134int x = 3; 1135]] ) 1136 1137local result =exec_lua ( function () 1138local query =vim . treesitter . query . parse ( 1139'c' , 1140'((number_literal) @number (#set! @number "key" "value"))' 1141) 1142local parser =vim . treesitter . get_parser ( 0 , 'c' ) 1143 1144local _ , _ , metadata =query : iter_matches ( parser : parse ()[ 1 ]: root (), 0 , 0 , - 1 )() 1145local _ , nested_tbl =next ( metadata ) 1146return nested_tbl . key 1147end ) 1148 1149eq ( 'value' , result ) 1150end ) 1151 1152it ( 'does not overwrite the nested table' , function () 1153insert ( [[ 1154int x = 3; 1155]] ) 1156 1157local result =exec_lua ( function () 1158local query =vim . treesitter . query . parse ( 1159'c' , 1160'((number_literal) @number (#set! @number "key" "value") (#set! @number "key2" "value2"))' 1161) 1162local parser =vim . treesitter . get_parser ( 0 , 'c' ) 1163 1164local _ , _ , metadata =query : iter_matches ( parser : parse ()[ 1 ]: root (), 0 , 0 , - 1 )() 1165local _ , nested_tbl =next ( metadata ) 1166return nested_tbl 1167end ) 1168local expected ={ 1169[ 'key' ] ='value' , 1170[ 'key2' ] ='value2' , 1171} 1172 1173eq ( expected , result ) 1174end ) 1175end ) 1176end ) 1177end ) 1178 1179describe ( 'trim! directive' , function () 1180it ( 'can trim all whitespace' , function () 1181exec_lua ( function () 1182local lines ={ 1183' print([[' , 1184'' , 1185' f' , 1186' helllo' , 1187' there' , 1188' asdf' , 1189' asdfassd ' , 1190'' , 1191'' , 1192'' , 1193' ]])' , 1194' print([[' , 1195' ' , 1196' ' , 1197' ' , 1198' ]])' , 1199'' , 1200' print([[]])' , 1201'' , 1202' print([[' , 1203' ]])' , 1204'' , 1205' print([[ hello 😃 ]])' , 1206} 1207vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 1208end ) 1209exec_lua ( function () 1210vim . treesitter . start ( 0 , 'lua' ) 1211end ) 1212 1213local query_text =[[ 1214; query 1215((string_content) @str) 1216]] 1217eq ( { 1218{ 'str' , { 0 , 16 , 10 , 2 } } , 1219{ 'str' , { 11 , 10 , 15 , 2 } } , 1220{ 'str' , { 17 , 10 , 17 , 10 } } , 1221{ 'str' , { 19 , 10 , 20 , 2 } } , 1222{ 'str' , { 22 , 10 , 22 , 29 } } , 1223} , run_query ( 'lua' , query_text )) 1224 1225local trim_query_text =[[ 1226; query 1227((string_content) @str 1228(#trim! @str 1 1 1 1)) 1229]] 1230 1231eq ( { 1232{ 'str' , { 2 , 12 , 6 , 10 } } , 1233{ 'str' , { 11 , 10 , 11 , 10 } } , 1234{ 'str' , { 17 , 10 , 17 , 10 } } , 1235{ 'str' , { 19 , 10 , 19 , 10 } } , 1236{ 'str' , { 22 , 15 , 22 , 25 } } , 1237} , run_query ( 'lua' , trim_query_text )) 1238end ) 1239 1240it ( 'trims only empty lines by default (backwards compatible)' , function () 1241insert ( dedent [[ 1242## Heading 1243 1244With some text 1245 1246## And another 1247 1248With some more here]] ) 1249 1250local query_text =[[ 1251; query 1252((section) @fold 1253(#trim! @fold)) 1254]] 1255 1256exec_lua ( function () 1257vim . treesitter . start ( 0 , 'markdown' ) 1258end ) 1259 1260eq ( { 1261{ 'fold' , { 0 , 0 , 2 , 14 } } , 1262{ 'fold' , { 4 , 0 , 6 , 19 } } , 1263} , run_query ( 'markdown' , query_text )) 1264end ) 1265 1266it ( 'can trim lines' , function () 1267insert ( dedent [[ 1268- Fold list 1269- Fold list 1270- Fold list 1271- Fold list 1272- Fold list 1273- Fold list 1274]] ) 1275 1276local query_text =[[ 1277; query 1278((list_item 1279(list)) @fold 1280(#trim! @fold 1 1 1 1)) 1281]] 1282 1283exec_lua ( function () 1284vim . treesitter . start ( 0 , 'markdown' ) 1285end ) 1286 1287eq ( { 1288{ 'fold' , { 0 , 0 , 4 , 13 } } , 1289{ 'fold' , { 1 , 2 , 3 , 15 } } , 1290} , run_query ( 'markdown' , query_text )) 1291end ) 1292end ) 1293 1294it ( 'tracks the root range properly (#22911)' , function () 1295insert ( [[ 1296int main() { 1297int x = 3; 1298}]] ) 1299 1300local query0 =[[ 1301(declaration) @declaration 1302(function_definition) @function 1303]] 1304 1305exec_lua ( function () 1306vim . treesitter . start ( 0 , 'c' ) 1307end ) 1308 1309eq ( { 1310{ 'function' , { 0 , 0 , 2 , 1 } } , 1311{ 'declaration' , { 1 , 2 , 1 , 12 } } , 1312} , run_query ( 'c' , query0 )) 1313 1314n . command 'normal ggO' 1315insert ( 'int a;' ) 1316 1317eq ( { 1318{ 'declaration' , { 0 , 0 , 0 , 6 } } , 1319{ 'function' , { 1 , 0 , 3 , 1 } } , 1320{ 'declaration' , { 2 , 2 , 2 , 12 } } , 1321} , run_query ( 'c' , query0 )) 1322end ) 1323 1324it ( 'handles ranges when source is a multiline string (#20419)' , function () 1325local source =[==[ 1326vim.cmd[[ 1327set number 1328set cmdheight=2 1329set lastsatus=2 1330]] 1331 1332set query = [[;; query 1333((function_call 1334name: [ 1335(identifier) @_cdef_identifier 1336(_ _ (identifier) @_cdef_identifier) 1337] 1338arguments: (arguments (string content: _ @injection.content))) 1339(#set! injection.language "c") 1340(#eq? @_cdef_identifier "cdef")) 1341]] 1342]==] 1343 1344local r =exec_lua ( function () 1345local parser =vim . treesitter . get_string_parser ( source , 'lua' ) 1346parser : parse ( true ) 1347local ranges ={} 1348parser : for_each_tree ( function ( tstree , tree ) 1349ranges [ tree : lang ()] ={ tstree : root (): range ( true ) } 1350end ) 1351return ranges 1352end ) 1353 1354eq ( { 1355lua ={ 0 , 6 , 6 , 16 , 4 , 438 } , 1356query ={ 6 , 20 , 113 , 15 , 6 , 431 } , 1357vim ={ 1 , 0 , 16 , 4 , 6 , 89 } , 1358} , r ) 1359 1360-- The above ranges are provided directly from treesitter, however query directives may mutate 1361-- the ranges but only provide a Range4. Strip the byte entries from the ranges and make sure 1362-- add_bytes() produces the same result. 1363 1364local rb =exec_lua ( function () 1365local add_bytes =require ( 'vim.treesitter._range' ). add_bytes 1366for lang , range in pairs ( r ) do 1367r [ lang ] ={ range [ 1 ], range [ 2 ], range [ 4 ], range [ 5 ] } 1368r [ lang ] =add_bytes ( source , r [ lang ]) 1369end 1370return r 1371end ) 1372 1373eq ( rb , r ) 1374end ) 1375 1376it ( 'does not produce empty injection ranges (#23409)' , function () 1377insert [[ 1378Examples: >lua 1379local a = {} 1380< 1381]] 1382 1383-- This is not a valid injection since (code) has children and include-children is not set 1384exec_lua ( function () 1385_G . parser1 =require ( 'vim.treesitter.languagetree' ). new ( 0 , 'vimdoc' , { 1386injections ={ 1387vimdoc ='((codeblock (language) @injection.language (code) @injection.content))' , 1388} , 1389} ) 1390_G . parser1 : parse ( true ) 1391end ) 1392 1393eq ( 0 , exec_lua ( 'return #vim.tbl_keys(parser1:children())' )) 1394 1395exec_lua ( function () 1396_G . parser2 =require ( 'vim.treesitter.languagetree' ). new ( 0 , 'vimdoc' , { 1397injections ={ 1398vimdoc ='((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))' , 1399} , 1400} ) 1401_G . parser2 : parse ( true ) 1402end ) 1403 1404eq ( 1 , exec_lua ( 'return #vim.tbl_keys(parser2:children())' )) 1405eq ( { { { 1 , 0 , 21 , 2 , 0 , 42 } } } , exec_lua ( 'return parser2:children().lua:included_regions()' )) 1406end ) 1407 1408it ( 'parses injections incrementally' , function () 1409insert ( dedent [[ 1410>lua 1411local a = {} 1412< 1413 1414>lua 1415local b = {} 1416< 1417 1418>lua 1419local c = {} 1420< 1421 1422>lua 1423local d = {} 1424< 1425 1426>lua 1427local e = {} 1428< 1429 1430>lua 1431local f = {} 1432< 1433 1434>lua 1435local g = {} 1436< 1437]] ) 1438 1439exec_lua ( function () 1440_G . parser =require ( 'vim.treesitter.languagetree' ). new ( 0 , 'vimdoc' , { 1441injections ={ 1442vimdoc ='((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))' , 1443} , 1444} ) 1445end ) 1446 1447--- Do not parse injections by default 1448eq ( 14490 , 1450exec_lua ( function () 1451_G . parser : parse () 1452return # vim . tbl_keys ( _G . parser : children ()) 1453end ) 1454) 1455 1456--- Only parse injections between lines 0, 2 1457eq ( 14581 , 1459exec_lua ( function () 1460_G . parser : parse ( { 0 , 2 } ) 1461return # _G . parser : children (). lua : trees () 1462end ) 1463) 1464 1465eq ( 14661 , 1467exec_lua ( function () 1468_G . parser : parse ( { 2 , 6 } ) 1469return # _G . parser : children (). lua : trees () 1470end ) 1471) 1472 1473eq ( 14747 , 1475exec_lua ( function () 1476_G . parser : parse ( true ) 1477return # _G . parser : children (). lua : trees () 1478end ) 1479) 1480end ) 1481 1482describe ( 'languagetree is_valid()' , function () 1483before_each ( function () 1484insert ( dedent [[ 1485Treesitter integration *treesitter* 1486 1487Nvim integrates the `tree-sitter` library for incremental parsing of buffers: 1488https://tree-sitter.github.io/tree-sitter/ 1489 1490]] ) 1491 1492feed ( ':set ft=help<cr>' ) 1493 1494exec_lua ( function () 1495vim . treesitter 1496. get_parser ( 0 , 'vimdoc' , { 1497injections ={ 1498vimdoc ='((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))' , 1499} , 1500} ) 1501: parse () 1502end ) 1503end ) 1504 1505it ( 'is valid excluding, invalid including children initially' , function () 1506eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1507eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1508end ) 1509 1510it ( 'is fully valid after a full parse' , function () 1511exec_lua ( 'vim.treesitter.get_parser():parse(true)' ) 1512eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1513eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1514end ) 1515 1516it ( 'is valid within a range on parsed tree after parsing it' , function () 1517exec_lua ( 'vim.treesitter.get_parser():parse({5, 7})' ) 1518eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1519eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(nil, {5, 7})' )) 1520end ) 1521 1522describe ( 'when adding content with injections' , function () 1523before_each ( function () 1524feed ( 'G' ) 1525insert ( dedent [[ 1526>lua 1527local a = {} 1528< 1529 1530]] ) 1531end ) 1532 1533it ( 'is fully invalid after changes' , function () 1534eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1535eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1536end ) 1537 1538it ( 'is valid excluding, invalid including children after a rangeless parse' , function () 1539exec_lua ( 'vim.treesitter.get_parser():parse()' ) 1540eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1541eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1542end ) 1543 1544it ( 'is valid within a range on parsed tree after parsing it' , function () 1545exec_lua ( 'vim.treesitter.get_parser():parse({5, 7})' ) 1546eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1547eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(nil, {5, 7})' )) 1548end ) 1549 1550it ( 1551'is valid excluding, invalid including children after a range parse that does not lead to parsing non-parsed injections' , 1552function () 1553exec_lua ( 'vim.treesitter.get_parser():parse({2, 4})' ) 1554eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1555eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1556end 1557) 1558end ) 1559 1560describe ( 'when removing content with injections' , function () 1561before_each ( function () 1562feed ( 'G' ) 1563insert ( dedent [[ 1564>lua 1565local a = {} 1566< 1567 1568>lua 1569local a = {} 1570< 1571 1572]] ) 1573 1574exec_lua ( 'vim.treesitter.get_parser():parse(true)' ) 1575 1576feed ( 'Gd3k' ) 1577end ) 1578 1579it ( 'is fully invalid after changes' , function () 1580eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1581eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1582end ) 1583 1584it ( 'is valid excluding, invalid including children after a rangeless parse' , function () 1585exec_lua ( 'vim.treesitter.get_parser():parse()' ) 1586eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1587eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1588end ) 1589 1590it ( 'is valid within a range parse that leads to parsing modified child tree' , function () 1591exec_lua ( 'vim.treesitter.get_parser():parse({5, 7})' ) 1592eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1593eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(nil, {5, 7})' )) 1594end ) 1595 1596it ( 1597'is valid excluding, invalid including children after a range parse that does not lead to parsing modified child tree' , 1598function () 1599exec_lua ( 'vim.treesitter.get_parser():parse({2, 4})' ) 1600eq ( true , exec_lua ( 'return vim.treesitter.get_parser():is_valid(true)' )) 1601eq ( false , exec_lua ( 'return vim.treesitter.get_parser():is_valid()' )) 1602end 1603) 1604end ) 1605end ) 1606end )