robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
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 dedent =t . dedent 7local eq =t . eq 8local insert =n . insert 9local exec_lua =n . exec_lua 10local pcall_err =t . pcall_err 11local api =n . api 12 13local function get_query_result ( query_text ) 14local cquery =vim . treesitter . query . parse ( 'c' , query_text ) 15local parser =vim . treesitter . get_parser ( 0 , 'c' ) 16local tree =parser : parse ()[ 1 ] 17local res ={} 18for cid , node in cquery : iter_captures ( tree : root (), 0 ) do 19-- can't transmit node over RPC. just check the name, range, and text 20local text =vim . treesitter . get_node_text ( node , 0 ) 21local range ={ node : range () } 22table . insert ( res , { cquery . captures [ cid ], node : type (), range , text } ) 23end 24return res 25end 26 27describe ( 'treesitter query API' , function () 28before_each ( function () 29clear () 30exec_lua ( function () 31vim . g . __ts_debug =1 32end ) 33end ) 34 35local test_text =[[ 36void ui_refresh(void) 37{ 38int width = INT_MAX, height = INT_MAX; 39bool ext_widgets[kUIExtCount]; 40for (UIExtension i = 0; (int)i < kUIExtCount; i++) { 41ext_widgets[i] = true; 42} 43 44bool inclusive = ui_override(); 45for (size_t i = 0; i < ui_count; i++) { 46UI *ui = uis[i]; 47width = MIN(ui->width, width); 48height = MIN(ui->height, height); 49foo = BAR(ui->bazaar, bazaar); 50for (UIExtension j = 0; (int)j < kUIExtCount; j++) { 51ext_widgets[j] &= (ui->ui_ext[j] || inclusive); 52} 53} 54}]] 55 56local test_query =[[ 57((call_expression 58function: (identifier) @minfunc 59(argument_list (identifier) @min_id)) 60(#eq? @minfunc "MIN") 61) 62 63"for" @keyword 64 65(primitive_type) @type 66 67(field_expression argument: (identifier) @fieldarg) 68]] 69 70it ( 'supports runtime queries' , function () 71---@type string[] 72local ret =exec_lua ( function () 73return vim . treesitter . query . get ( 'c' , 'highlights' ). captures 74end ) 75 76-- see $VIMRUNTIME/queries/c/highlights.scm 77eq ( 'variable' , ret [ 1 ]) 78eq ( 'keyword' , ret [ 2 ]) 79end ) 80 81it ( 'supports caching queries' , function () 82local long_query =test_query : rep ( 100 ) 83---@return number 84local function q ( _n ) 85return exec_lua ( function () 86local before =vim . api . nvim__stats (). ts_query_parse_count 87collectgarbage ( 'stop' ) 88for _ =1 , _n , 1 do 89vim . treesitter . query . parse ( 'c' , long_query ) 90end 91collectgarbage ( 'restart' ) 92collectgarbage ( 'collect' ) 93local after =vim . api . nvim__stats (). ts_query_parse_count 94return after - before 95end ) 96end 97 98eq ( 1 , q ( 1 )) 99-- cache is retained even after garbage collection 100eq ( 0 , q ( 100 )) 101end ) 102 103it ( 'cache is cleared upon runtimepath changes, or setting query manually' , function () 104---@return number 105exec_lua ( function () 106_G . query_parse_count =_G . query_parse_count or 0 107local parse =vim . treesitter . query . parse 108vim . treesitter . query . parse =function ( ... ) 109_G . query_parse_count =_G . query_parse_count + 1 110return parse ( ... ) 111end 112end ) 113 114local function q ( _n ) 115return exec_lua ( function () 116for _ =1 , _n , 1 do 117vim . treesitter . query . get ( 'c' , 'highlights' ) 118end 119return _G . query_parse_count 120end ) 121end 122 123eq ( 1 , q ( 10 )) 124exec_lua ( function () 125vim . opt . rtp : prepend ( '/another/dir' ) 126end ) 127eq ( 2 , q ( 100 )) 128exec_lua ( function () 129vim . treesitter . query . set ( 'c' , 'highlights' , [[; test]] ) 130end ) 131eq ( 3 , q ( 100 )) 132end ) 133 134it ( 'supports query and iter by capture (iter_captures)' , function () 135insert ( test_text ) 136 137local res =exec_lua ( function () 138local cquery =vim . treesitter . query . parse ( 'c' , test_query ) 139local parser =vim . treesitter . get_parser ( 0 , 'c' ) 140local tree =parser : parse ()[ 1 ] 141local res ={} 142for cid , node in cquery : iter_captures ( tree : root (), 0 , 7 , 14 ) do 143-- can't transmit node over RPC. just check the name and range 144table . insert ( res , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 145end 146return res 147end ) 148 149eq ( { 150{ '@type' , 'primitive_type' , 8 , 2 , 8 , 6 } , -- bool 151{ '@keyword' , 'for' , 9 , 2 , 9 , 5 } , -- for 152{ '@type' , 'primitive_type' , 9 , 7 , 9 , 13 } , -- size_t 153{ '@minfunc' , 'identifier' , 11 , 12 , 11 , 15 } , -- "MIN"(ui->width, width); 154{ '@fieldarg' , 'identifier' , 11 , 16 , 11 , 18 } , -- ui 155{ '@min_id' , 'identifier' , 11 , 27 , 11 , 32 } , -- width 156{ '@minfunc' , 'identifier' , 12 , 13 , 12 , 16 } , -- "MIN"(ui->height, height); 157{ '@fieldarg' , 'identifier' , 12 , 17 , 12 , 19 } , -- ui 158{ '@min_id' , 'identifier' , 12 , 29 , 12 , 35 } , -- height 159{ '@fieldarg' , 'identifier' , 13 , 14 , 13 , 16 } , -- ui ; in BAR(..) 160} , res ) 161end ) 162 163it ( 'supports query and iter by match (iter_matches)' , function () 164insert ( test_text ) 165 166---@type table 167local res =exec_lua ( function () 168local cquery =vim . treesitter . query . parse ( 'c' , test_query ) 169local parser =vim . treesitter . get_parser ( 0 , 'c' ) 170local tree =parser : parse ()[ 1 ] 171local res ={} 172for pattern , match in cquery : iter_matches ( tree : root (), 0 , 7 , 14 ) do 173-- can't transmit node over RPC. just check the name and range 174local mrepr ={} 175for cid , nodes in pairs ( match ) do 176for _ , node in ipairs ( nodes ) do 177table . insert ( mrepr , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 178end 179end 180table . insert ( res , { pattern , mrepr } ) 181end 182return res 183end ) 184 185eq ( { 186{ 3 , { { '@type' , 'primitive_type' , 8 , 2 , 8 , 6 } } } , 187{ 2 , { { '@keyword' , 'for' , 9 , 2 , 9 , 5 } } } , 188{ 3 , { { '@type' , 'primitive_type' , 9 , 7 , 9 , 13 } } } , 189{ 4 , { { '@fieldarg' , 'identifier' , 11 , 16 , 11 , 18 } } } , 190{ 1911 , 192{ 193{ '@minfunc' , 'identifier' , 11 , 12 , 11 , 15 } , 194{ '@min_id' , 'identifier' , 11 , 27 , 11 , 32 } , 195} , 196} , 197{ 4 , { { '@fieldarg' , 'identifier' , 12 , 17 , 12 , 19 } } } , 198{ 1991 , 200{ 201{ '@minfunc' , 'identifier' , 12 , 13 , 12 , 16 } , 202{ '@min_id' , 'identifier' , 12 , 29 , 12 , 35 } , 203} , 204} , 205{ 4 , { { '@fieldarg' , 'identifier' , 13 , 14 , 13 , 16 } } } , 206} , res ) 207end ) 208 209it ( 'supports query and iter by capture for quantifiers' , function () 210insert ( test_text ) 211 212local res =exec_lua ( function () 213local cquery =vim . treesitter . query . parse ( 214'c' , 215'(expression_statement (assignment_expression (call_expression)))+ @funccall' 216) 217local parser =vim . treesitter . get_parser ( 0 , 'c' ) 218local tree =parser : parse ()[ 1 ] 219local res ={} 220for cid , node in cquery : iter_captures ( tree : root (), 0 , 7 , 14 ) do 221-- can't transmit node over RPC. just check the name and range 222table . insert ( res , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 223end 224return res 225end ) 226 227eq ( { 228{ '@funccall' , 'expression_statement' , 11 , 4 , 11 , 34 } , 229{ '@funccall' , 'expression_statement' , 12 , 4 , 12 , 37 } , 230{ '@funccall' , 'expression_statement' , 13 , 4 , 13 , 34 } , 231} , res ) 232end ) 233 234it ( 'supports query and iter by match for quantifiers' , function () 235insert ( test_text ) 236 237local res =exec_lua ( function () 238local cquery =vim . treesitter . query . parse ( 239'c' , 240'(expression_statement (assignment_expression (call_expression)))+ @funccall' 241) 242local parser =vim . treesitter . get_parser ( 0 , 'c' ) 243local tree =parser : parse ()[ 1 ] 244local res ={} 245for pattern , match in cquery : iter_matches ( tree : root (), 0 , 7 , 14 ) do 246-- can't transmit node over RPC. just check the name and range 247local mrepr ={} 248for cid , nodes in pairs ( match ) do 249for _ , node in ipairs ( nodes ) do 250table . insert ( mrepr , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 251end 252end 253table . insert ( res , { pattern , mrepr } ) 254end 255return res 256end , '(expression_statement (assignment_expression (call_expression)))+ @funccall' ) 257 258eq ( { 259{ 2601 , 261{ 262{ '@funccall' , 'expression_statement' , 11 , 4 , 11 , 34 } , 263{ '@funccall' , 'expression_statement' , 12 , 4 , 12 , 37 } , 264{ '@funccall' , 'expression_statement' , 13 , 4 , 13 , 34 } , 265} , 266} , 267} , res ) 268end ) 269 270it ( 'returns quantified matches in order of range #29344' , function () 271insert ( [[ 272int main() { 273int a, b, c, d, e, f, g, h, i; 274a = MIN(0, 1); 275b = MIN(0, 1); 276c = MIN(0, 1); 277d = MIN(0, 1); 278e = MIN(0, 1); 279f = MIN(0, 1); 280g = MIN(0, 1); 281h = MIN(0, 1); 282i = MIN(0, 1); 283} 284]] ) 285 286local res =exec_lua ( function () 287local cquery =vim . treesitter . query . parse ( 288'c' , 289'(expression_statement (assignment_expression (call_expression)))+ @funccall' 290) 291local parser =vim . treesitter . get_parser ( 0 , 'c' ) 292local tree =parser : parse ()[ 1 ] 293local res ={} 294for pattern , match in cquery : iter_matches ( tree : root (), 0 , 7 , 14 ) do 295-- can't transmit node over RPC. just check the name and range 296local mrepr ={} 297for cid , nodes in pairs ( match ) do 298for _ , node in ipairs ( nodes ) do 299table . insert ( mrepr , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 300end 301end 302table . insert ( res , { pattern , mrepr } ) 303end 304return res 305end ) 306 307eq ( { 308{ 3091 , 310{ 311{ '@funccall' , 'expression_statement' , 2 , 2 , 2 , 16 } , 312{ '@funccall' , 'expression_statement' , 3 , 2 , 3 , 16 } , 313{ '@funccall' , 'expression_statement' , 4 , 2 , 4 , 16 } , 314{ '@funccall' , 'expression_statement' , 5 , 2 , 5 , 16 } , 315{ '@funccall' , 'expression_statement' , 6 , 2 , 6 , 16 } , 316{ '@funccall' , 'expression_statement' , 7 , 2 , 7 , 16 } , 317{ '@funccall' , 'expression_statement' , 8 , 2 , 8 , 16 } , 318{ '@funccall' , 'expression_statement' , 9 , 2 , 9 , 16 } , 319{ '@funccall' , 'expression_statement' , 10 , 2 , 10 , 16 } , 320} , 321} , 322} , res ) 323end ) 324 325it ( 'can match special regex characters like \\ * + ( with `vim-match?`' , function () 326insert ( 'char* astring = "\\n"; (1 + 1) * 2 != 2;' ) 327 328---@type table 329local res =exec_lua ( function () 330local query =( 331'([_] @plus (#vim-match? @plus "^\\\\+$"))' 332.. '([_] @times (#vim-match? @times "^\\\\*$"))' 333.. '([_] @paren (#vim-match? @paren "^\\\\($"))' 334.. '([_] @escape (#vim-match? @escape "^\\\\\\\\n$"))' 335.. '([_] @string (#vim-match? @string "^\\"\\\\\\\\n\\"$"))' 336) 337local cquery =vim . treesitter . query . parse ( 'c' , query ) 338local parser =vim . treesitter . get_parser ( 0 , 'c' ) 339local tree =parser : parse ()[ 1 ] 340local res ={} 341for pattern , match in cquery : iter_matches ( tree : root (), 0 , 0 , - 1 ) do 342-- can't transmit node over RPC. just check the name and range 343local mrepr ={} 344for cid , nodes in pairs ( match ) do 345for _ , node in ipairs ( nodes ) do 346table . insert ( mrepr , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 347end 348end 349table . insert ( res , { pattern , mrepr } ) 350end 351return res 352end ) 353 354eq ( { 355{ 2 , { { '@times' , '*' , 0 , 4 , 0 , 5 } } } , 356{ 5 , { { '@string' , 'string_literal' , 0 , 16 , 0 , 20 } } } , 357{ 4 , { { '@escape' , 'escape_sequence' , 0 , 17 , 0 , 19 } } } , 358{ 3 , { { '@paren' , '(' , 0 , 22 , 0 , 23 } } } , 359{ 1 , { { '@plus' , '+' , 0 , 25 , 0 , 26 } } } , 360{ 2 , { { '@times' , '*' , 0 , 30 , 0 , 31 } } } , 361} , res ) 362end ) 363 364it ( 'supports builtin query predicate any-of?' , function () 365insert ( [[ 366#include <stdio.h> 367 368int main(void) { 369int i; 370for(i=1; i<=100; i++) { 371if(((i%3)||(i%5))== 0) 372printf("number= %d FizzBuzz\n", i); 373else if((i%3)==0) 374printf("number= %d Fizz\n", i); 375else if((i%5)==0) 376printf("number= %d Buzz\n", i); 377else 378printf("number= %d\n",i); 379} 380return 0; 381} 382]] ) 383 384local res0 =exec_lua ( 385get_query_result , 386[[((primitive_type) @c-keyword (#any-of? @c-keyword "int" "float"))]] 387) 388eq ( { 389{ 'c-keyword' , 'primitive_type' , { 2 , 0 , 2 , 3 } , 'int' } , 390{ 'c-keyword' , 'primitive_type' , { 3 , 2 , 3 , 5 } , 'int' } , 391} , res0 ) 392 393local res1 =exec_lua ( 394get_query_result , 395[[ 396((string_literal) @fizzbuzz-strings (#any-of? @fizzbuzz-strings 397"\"number= %d FizzBuzz\\n\"" 398"\"number= %d Fizz\\n\"" 399"\"number= %d Buzz\\n\"" 400)) 401]] 402) 403eq ( { 404{ 'fizzbuzz-strings' , 'string_literal' , { 6 , 13 , 6 , 36 } , '"number= %d FizzBuzz\\n"' } , 405{ 'fizzbuzz-strings' , 'string_literal' , { 8 , 13 , 8 , 32 } , '"number= %d Fizz\\n"' } , 406{ 'fizzbuzz-strings' , 'string_literal' , { 10 , 13 , 10 , 32 } , '"number= %d Buzz\\n"' } , 407} , res1 ) 408end ) 409 410it ( 'supports builtin predicate has-ancestor?' , function () 411insert ( [[ 412int x = 123; 413enum C { y = 124 }; 414int main() { int z = 125; }]] ) 415 416local result =exec_lua ( 417get_query_result , 418[[((number_literal) @literal (#has-ancestor? @literal "function_definition"))]] 419) 420eq ( { { 'literal' , 'number_literal' , { 2 , 21 , 2 , 24 } , '125' } } , result ) 421 422result =exec_lua ( 423get_query_result , 424[[((number_literal) @literal (#has-ancestor? @literal "function_definition" "enum_specifier"))]] 425) 426eq ( { 427{ 'literal' , 'number_literal' , { 1 , 13 , 1 , 16 } , '124' } , 428{ 'literal' , 'number_literal' , { 2 , 21 , 2 , 24 } , '125' } , 429} , result ) 430 431result =exec_lua ( 432get_query_result , 433[[((number_literal) @literal (#not-has-ancestor? @literal "enum_specifier"))]] 434) 435eq ( { 436{ 'literal' , 'number_literal' , { 0 , 8 , 0 , 11 } , '123' } , 437{ 'literal' , 'number_literal' , { 2 , 21 , 2 , 24 } , '125' } , 438} , result ) 439 440result =exec_lua ( 441get_query_result , 442[[((number_literal) @literal (#has-ancestor? @literal "enumerator"))]] 443) 444eq ( { 445{ 'literal' , 'number_literal' , { 1 , 13 , 1 , 16 } , '124' } , 446} , result ) 447 448result =exec_lua ( 449get_query_result , 450[[((number_literal) @literal (#has-ancestor? @literal "number_literal"))]] 451) 452eq ( {} , result ) 453end ) 454 455it ( 'supports builtin predicate has-parent?' , function () 456insert ( [[ 457int x = 123; 458enum C { y = 124 };]] ) 459 460local result =exec_lua ( 461get_query_result , 462[[((number_literal) @literal (#has-parent? @literal "init_declarator"))]] 463) 464eq ( { { 'literal' , 'number_literal' , { 0 , 8 , 0 , 11 } , '123' } } , result ) 465 466-- The root node has no parent: the predicate does not match, rather than 467-- erroring on the nil parent. 468result =exec_lua ( 469get_query_result , 470[[((translation_unit) @root (#has-parent? @root "translation_unit"))]] 471) 472eq ( {} , result ) 473end ) 474 475it ( 'allows loading query with escaped quotes and capture them `#{lua,vim}-match`?' , function () 476insert ( 'char* astring = "Hello World!";' ) 477 478local res =exec_lua ( function () 479local cquery =vim . treesitter . query . parse ( 480'c' , 481'([_] @quote (#vim-match? @quote "^\\"$")) ([_] @quote (#lua-match? @quote "^\\"$"))' 482) 483local parser =vim . treesitter . get_parser ( 0 , 'c' ) 484local tree =parser : parse ()[ 1 ] 485local res ={} 486for pattern , match in cquery : iter_matches ( tree : root (), 0 , 0 , - 1 ) do 487-- can't transmit node over RPC. just check the name and range 488local mrepr ={} 489for cid , nodes in pairs ( match ) do 490for _ , node in ipairs ( nodes ) do 491table . insert ( mrepr , { '@' .. cquery . captures [ cid ], node : type (), node : range () } ) 492end 493end 494table . insert ( res , { pattern , mrepr } ) 495end 496return res 497end ) 498 499eq ( { 500{ 1 , { { '@quote' , '"' , 0 , 16 , 0 , 17 } } } , 501{ 2 , { { '@quote' , '"' , 0 , 16 , 0 , 17 } } } , 502{ 1 , { { '@quote' , '"' , 0 , 29 , 0 , 30 } } } , 503{ 2 , { { '@quote' , '"' , 0 , 29 , 0 , 30 } } } , 504} , res ) 505end ) 506 507it ( 'allows to add predicates' , function () 508insert ( [[ 509int main(void) { 510return 0; 511} 512]] ) 513 514local custom_query ='((identifier) @main (#is-main? @main))' 515 516do 517local res =exec_lua ( function () 518local query =vim . treesitter . query 519 520local function is_main ( match , _pattern , bufnr , predicate ) 521local nodes =match [ predicate [ 2 ]] 522for _ , node in ipairs ( nodes ) do 523if vim . treesitter . get_node_text ( node , bufnr ) == 'main' then 524return true 525end 526end 527return false 528end 529 530local parser =vim . treesitter . get_parser ( 0 , 'c' ) 531 532query . add_predicate ( 'is-main?' , is_main ) 533 534local query0 =query . parse ( 'c' , custom_query ) 535 536local nodes ={} 537for _ , node in query0 : iter_captures ( parser : parse ()[ 1 ]: root (), 0 ) do 538table . insert ( nodes , { node : range () } ) 539end 540 541return nodes 542end ) 543 544eq ( { { 0 , 4 , 0 , 8 } } , res ) 545end 546 547do 548local res =exec_lua ( function () 549local query =vim . treesitter . query 550 551local r ={} 552for _ , v in ipairs ( query . list_predicates ()) do 553r [ v ] =true 554end 555 556return r 557end ) 558 559eq ( true , res [ 'is-main?' ]) 560end 561end ) 562 563it ( 'supports "all" and "any" semantics for predicates on quantified captures #24738' , function () 564local query_all =[[ 565(((comment (comment_content))+) @bar 566(#lua-match? @bar "Yes")) 567]] 568 569local query_any =[[ 570(((comment (comment_content))+) @bar 571(#any-lua-match? @bar "Yes")) 572]] 573 574local function test ( input , query ) 575api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , vim . split ( dedent ( input ), '\n' )) 576return exec_lua ( function () 577local parser =vim . treesitter . get_parser ( 0 , 'lua' ) 578local query0 =vim . treesitter . query . parse ( 'lua' , query ) 579local nodes ={} 580for _ , node in query0 : iter_captures ( parser : parse ()[ 1 ]: root (), 0 ) do 581nodes [ # nodes + 1 ] ={ node : range () } 582end 583return nodes 584end ) 585end 586 587eq ( 588{} , 589test ( 590[[ 591-- Yes 592-- No 593-- Yes 594]] , 595query_all 596) 597) 598 599eq ( 600{ 601{ 0 , 0 , 0 , 6 } , 602{ 1 , 0 , 1 , 6 } , 603{ 2 , 0 , 2 , 6 } , 604} , 605test ( 606[[ 607-- Yes 608-- Yes 609-- Yes 610]] , 611query_all 612) 613) 614 615eq ( 616{} , 617test ( 618[[ 619-- No 620-- No 621-- No 622]] , 623query_any 624) 625) 626 627eq ( 628{ 629{ 0 , 0 , 0 , 5 } , 630{ 1 , 0 , 1 , 6 } , 631{ 2 , 0 , 2 , 5 } , 632} , 633test ( 634[[ 635-- No 636-- Yes 637-- No 638]] , 639query_any 640) 641) 642end ) 643 644it ( 'supports any- prefix to match any capture when using quantifiers #24738' , function () 645insert ( [[ 646-- Comment 647-- Comment 648-- Comment 649]] ) 650 651local result =exec_lua ( function () 652local parser =vim . treesitter . get_parser ( 0 , 'lua' ) 653local query =vim . treesitter . query . parse ( 654'lua' , 655[[ 656(((comment (comment_content))+) @bar 657(#lua-match? @bar "Comment")) 658]] 659) 660local nodes ={} 661for _ , node in query : iter_captures ( parser : parse ()[ 1 ]: root (), 0 ) do 662nodes [ # nodes + 1 ] ={ node : range () } 663end 664return nodes 665end ) 666 667eq ( { 668{ 0 , 0 , 0 , 10 } , 669{ 1 , 0 , 1 , 10 } , 670{ 2 , 0 , 2 , 10 } , 671} , result ) 672end ) 673 674it ( 'should use node range when omitted' , function () 675local txt =[[ 676int foo = 42; 677int bar = 13; 678]] 679 680local ret =exec_lua ( function () 681local parser =vim . treesitter . get_string_parser ( txt , 'c' ) 682 683local nodes ={} 684local query =vim . treesitter . query . parse ( 'c' , '((identifier) @foo)' ) 685local first_child =assert ( parser : parse ()[ 1 ]: root (): child ( 1 )) 686 687for _ , node in query : iter_captures ( first_child , txt ) do 688table . insert ( nodes , { node : range () } ) 689end 690 691return nodes 692end ) 693 694eq ( { { 1 , 10 , 1 , 13 } } , ret ) 695end ) 696 697it ( 'iter_captures supports columns' , function () 698local txt =table . concat ( { 699'int aaa = 1, bbb = 2;' , 700'int foo = 1, bar = 2;' , 701'int baz = 3, qux = 4;' , 702'int ccc = 1, ddd = 2;' , 703} , '\n' ) 704 705local function test ( opts ) 706local parser =vim . treesitter . get_string_parser ( txt , 'c' ) 707 708local nodes ={} 709local query =vim . treesitter . query . parse ( 'c' , '((identifier) @foo)' ) 710local root =assert ( parser : parse ()[ 1 ]: root ()) 711local iter =query : iter_captures ( root , txt , 1 , 2 , opts ) 712 713while true do 714local capture , node =iter () 715if not capture then 716break 717end 718table . insert ( nodes , { node : range () } ) 719end 720 721return nodes 722end 723 724local ret 725ret =exec_lua ( test , { start_col =7 , end_col =13 } ) 726eq ( { { 1 , 13 , 1 , 16 } , { 2 , 4 , 2 , 7 } } , ret ) 727 728ret =exec_lua ( test , { start_col =7 } ) 729eq ( { { 1 , 13 , 1 , 16 } } , ret ) 730 731ret =exec_lua ( test , { end_col =13 } ) 732eq ( { { 1 , 4 , 1 , 7 } , { 1 , 13 , 1 , 16 } , { 2 , 4 , 2 , 7 } } , ret ) 733 734ret =exec_lua ( test , {} ) 735eq ( { { 1 , 4 , 1 , 7 } , { 1 , 13 , 1 , 16 } } , ret ) 736end ) 737 738it ( 'fails to load queries' , function () 739local function test ( exp , cquery ) 740eq ( exp , pcall_err ( exec_lua , "vim.treesitter.query.parse('c', ...)" , cquery )) 741end 742 743-- Invalid node types 744test ( 745'.../query.lua:0: Query error at 1:2. Invalid node type ">\\">>":\n' 746.. '">\\">>" @operator\n' 747.. ' ^' , 748'">\\">>" @operator' 749) 750test ( 751'.../query.lua:0: Query error at 1:2. Invalid node type "\\\\":\n' 752.. '"\\\\" @operator\n' 753.. ' ^' , 754'"\\\\" @operator' 755) 756test ( 757'.../query.lua:0: Query error at 1:2. Invalid node type ">>>":\n' 758.. '">>>" @operator\n' 759.. ' ^' , 760'">>>" @operator' 761) 762test ( 763'.../query.lua:0: Query error at 1:2. Invalid node type "dentifier":\n' 764.. '(dentifier) @variable\n' 765.. ' ^' , 766'(dentifier) @variable' 767) 768 769-- Impossible pattern 770test ( 771'.../query.lua:0: Query error at 1:13. Impossible pattern:\n' 772.. '(identifier (identifier) @variable)\n' 773.. ' ^' , 774'(identifier (identifier) @variable)' 775) 776 777-- Invalid syntax 778test ( 779'.../query.lua:0: Query error at 1:13. Invalid syntax:\n' 780.. '(identifier @variable\n' 781.. ' ^' , 782'(identifier @variable' 783) 784 785-- Invalid field name 786test ( 787'.../query.lua:0: Query error at 1:15. Invalid field name "invalid_field":\n' 788.. '((identifier) invalid_field: (identifier))\n' 789.. ' ^' , 790'((identifier) invalid_field: (identifier))' 791) 792 793-- Invalid capture name 794test ( 795'.../query.lua:0: Query error at 3:2. Invalid capture name "ok.capture":\n' 796.. '@ok.capture\n' 797.. ' ^' , 798'((identifier) @id \n(#eq? @id\n@ok.capture\n))' 799) 800end ) 801 802it ( 'supports "; extends" modeline in custom queries' , function () 803insert ( 'int zeero = 0;' ) 804local result =exec_lua ( function () 805vim . treesitter . query . set ( 806'c' , 807'highlights' , 808[[; extends 809(identifier) @spell]] 810) 811local query =vim . treesitter . query . get ( 'c' , 'highlights' ) 812local parser =vim . treesitter . get_parser ( 0 , 'c' ) 813local root =parser : parse ()[ 1 ]: root () 814local res ={} 815for id , node in query : iter_captures ( root , 0 ) do 816table . insert ( res , { query . captures [ id ], vim . treesitter . get_node_text ( node , 0 ) } ) 817end 818return res 819end ) 820eq ( { 821{ 'type.builtin' , 'int' } , 822{ 'variable' , 'zeero' } , 823{ 'spell' , 'zeero' } , 824{ 'operator' , '=' } , 825{ 'number' , '0' } , 826{ 'punctuation.delimiter' , ';' } , 827} , result ) 828end ) 829 830describe ( 'Query:iter_captures' , function () 831it ( 'includes metadata for all captured nodes #23664' , function () 832insert ( [[ 833const char *sql = "SELECT * FROM Students WHERE name = 'Robert'); DROP TABLE Students;--"; 834]] ) 835 836local result =exec_lua ( function () 837local query =vim . treesitter . query . parse ( 838'c' , 839[[ 840(declaration 841type: (_) 842declarator: (init_declarator 843declarator: (pointer_declarator 844declarator: (identifier)) @_id 845value: (string_literal 846(string_content) @injection.content)) 847(#set! injection.language "sql") 848(#contains? @_id "sql")) 849]] 850) 851local parser =vim . treesitter . get_parser ( 0 , 'c' ) 852local root =parser : parse ()[ 1 ]: root () 853local res ={} 854for id , _ , metadata in query : iter_captures ( root , 0 ) do 855res [ query . captures [ id ]] =metadata 856end 857return res 858end ) 859 860eq ( { 861[ '_id' ] ={ [ 'injection.language' ] ='sql' } , 862[ 'injection.content' ] ={ [ 'injection.language' ] ='sql' } , 863} , result ) 864end ) 865 866it ( 'only evaluates predicates once per match' , function () 867insert ( [[ 868void foo(int x, int y); 869]] ) 870local query =[[ 871(declaration 872type: (_) 873declarator: (function_declarator 874declarator: (identifier) @function.name 875parameters: (parameter_list 876(parameter_declaration 877type: (_) 878declarator: (identifier) @argument))) 879(#eq? @function.name "foo")) 880]] 881 882local result =exec_lua ( function () 883local query0 =vim . treesitter . query . parse ( 'c' , query ) 884local match_preds =query0 . _match_predicates 885local called =0 886function query0 . _match_predicates ( ... ) 887called =called + 1 888return match_preds ( ... ) 889end 890local parser =vim . treesitter . get_parser ( 0 , 'c' ) 891local root =parser : parse ()[ 1 ]: root () 892local captures ={} 893for id in query0 : iter_captures ( root , 0 ) do 894captures [ # captures + 1 ] =id 895end 896return { called , captures } 897end ) 898 899eq ( { 2 , { 1 , 1 , 2 , 2 } } , result ) 900end ) 901end ) 902 903describe ( 'TSQuery' , function () 904local source =[[ 905void foo(int x, int y); 906]] 907 908local query_text =[[ 909((identifier) @func 910(#eq? @func "foo")) 911((identifier) @param 912(#eq? @param "x")) 913((identifier) @param 914(#eq? @param "y")) 915]] 916 917---@param query string 918---@param disabled { capture: string?, pattern: integer? } 919local function get_patterns ( query , disabled ) 920local q =vim . treesitter . query . parse ( 'c' , query ) 921if disabled . capture then 922q . query : disable_capture ( disabled . capture ) 923end 924if disabled . pattern then 925q . query : disable_pattern ( disabled . pattern ) 926end 927 928local parser =vim . treesitter . get_parser ( 0 , 'c' ) 929local root =parser : parse ()[ 1 ]: root () 930local captures ={} ---@type {id: number, pattern: number}[] 931for id , _ , _ , match in q : iter_captures ( root , 0 ) do 932local _ , pattern =match : info () 933captures [ # captures + 1 ] ={ id =id , pattern =pattern } 934end 935return captures 936end 937 938it ( 'supports disabling patterns' , function () 939insert ( source ) 940local result =exec_lua ( get_patterns , query_text , { pattern =2 } ) 941eq ( { { id =1 , pattern =1 } , { id =2 , pattern =3 } } , result ) 942end ) 943 944it ( 'supports disabling captures' , function () 945insert ( source ) 946local result =exec_lua ( get_patterns , query_text , { capture ='param' } ) 947eq ( { { id =1 , pattern =1 } } , result ) 948end ) 949end ) 950end )