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 dedent =t . dedent 8local eq =t . eq 9local exec_lua =n . exec_lua 10local feed =n . feed 11 12describe ( 'vim.treesitter.inspect_tree' , function () 13before_each ( clear ) 14 15local expect_tree =function ( x ) 16local expected =vim . split ( vim . trim ( dedent ( x )), '\n' ) 17local actual =n . buf_lines ( 0 ) ---@type string[] 18eq ( expected , actual ) 19end 20 21it ( 'working' , function () 22insert ( [[ 23print() 24]] ) 25 26exec_lua ( function () 27vim . treesitter . start ( 0 , 'lua' ) 28vim . treesitter . inspect_tree () 29end ) 30 31expect_tree [[ 32(chunk ; [0, 0] - [2, 0] 33(function_call ; [0, 0] - [0, 7] 34name: (identifier) ; [0, 0] - [0, 5] 35arguments: (arguments))) ; [0, 5] - [0, 7] 36]] 37end ) 38 39it ( 'sets correct buffer name' , function () 40n . api . nvim_set_current_dir ( t . paths . test_source_path .. '/test/functional/fixtures' ) 41n . command ( 'edit lua/syntax_error.lua' ) 42eq ( 'lua/syntax_error.lua' , n . fn . bufname ( '%' )) 43local full_path =n . api . nvim_buf_get_name ( 0 ) 44 45n . exec_lua ( 'vim.treesitter.start(0, "lua")' ) 46n . exec_lua ( 'vim.treesitter.inspect_tree()' ) 47local expected =[[ 48(chunk ; [0, 0] - [1, 0] 49(ERROR ; [0, 0] - [0, 21] 50(identifier) ; [0, 5] - [0, 8] 51(identifier) ; [0, 9] - [0, 15] 52(identifier))) ; [0, 16] - [0, 21] 53]] 54expect_tree ( expected ) 55eq ( 'Syntax tree for lua/syntax_error.lua' , n . fn . bufname ( '%' )) 56 57n . command ( 'bwipe!' ) 58eq ( 'lua/syntax_error.lua' , n . fn . bufname ( '%' )) 59 60n . api . nvim_set_current_dir ( 'pack' ) 61eq ( full_path , n . fn . bufname ( '%' )) 62 63n . exec_lua ( 'vim.treesitter.inspect_tree()' ) 64expect_tree ( expected ) 65eq ( 'Syntax tree for ' .. full_path , n . fn . bufname ( '%' )) 66end ) 67 68it ( 'can toggle to show anonymous nodes' , function () 69insert ( [[ 70print('hello') 71]] ) 72 73exec_lua ( function () 74vim . treesitter . start ( 0 , 'lua' ) 75vim . treesitter . inspect_tree () 76end ) 77feed ( 'a' ) 78 79expect_tree [[ 80(chunk ; [0, 0] - [2, 0] 81(function_call ; [0, 0] - [0, 14] 82name: (identifier) ; [0, 0] - [0, 5] 83arguments: (arguments ; [0, 5] - [0, 14] 84"(" ; [0, 5] - [0, 6] 85(string ; [0, 6] - [0, 13] 86start: "'" ; [0, 6] - [0, 7] 87content: (string_content) ; [0, 7] - [0, 12] 88end: "'") ; [0, 12] - [0, 13] 89")"))) ; [0, 13] - [0, 14] 90]] 91end ) 92 93it ( 'works for injected trees' , function () 94insert ( [[ 95```lua 96return 97``` 98]] ) 99 100exec_lua ( function () 101vim . treesitter . start ( 0 , 'markdown' ) 102vim . treesitter . get_parser (): parse () 103vim . treesitter . inspect_tree () 104end ) 105 106expect_tree [[ 107(document ; [0, 0] - [4, 0] 108(section ; [0, 0] - [4, 0] 109(fenced_code_block ; [0, 0] - [3, 0] 110(fenced_code_block_delimiter) ; [0, 0] - [0, 3] 111(info_string ; [0, 3] - [0, 6] 112(language)) ; [0, 3] - [0, 6] 113(block_continuation) ; [1, 0] - [1, 0] 114(code_fence_content ; [1, 0] - [2, 0] 115(chunk ; [1, 0] - [2, 0] 116(return_statement)) ; [1, 0] - [1, 6] 117(block_continuation)) ; [2, 0] - [2, 0] 118(fenced_code_block_delimiter)))) ; [2, 0] - [2, 3] 119]] 120end ) 121 122it ( 'works with multiple injection on the same node' , function () 123insert ( [[--* #include<stdio.h>]] ) 124exec_lua ( function () 125vim . treesitter . query . set ( 126'lua' , 127'injections' , 128[[ 129(comment 130content: (_) @injection.content 131(#set! injection.language "markdown")) 132(comment 133content: (_) @injection.content 134(#set! injection.language "c") 135(#offset! @injection.content 0 1 0 0)) 136]] 137) 138vim . treesitter . start ( 0 , 'lua' ) 139vim . treesitter . get_parser (): parse ( true ) 140vim . treesitter . inspect_tree () 141end ) 142feed ( 'I' ) 143expect_tree [[ 144(chunk ; [0, 0] - [1, 0] lua 145(comment ; [0, 0] - [0, 21] lua 146content: (comment_content ; [0, 2] - [0, 21] lua 147(document ; [0, 2] - [0, 21] markdown 148(section ; [0, 2] - [0, 21] markdown 149(list ; [0, 2] - [0, 21] markdown 150(list_item ; [0, 2] - [0, 21] markdown 151(list_marker_star) ; [0, 2] - [0, 4] markdown 152(paragraph ; [0, 4] - [0, 21] markdown 153(inline ; [0, 4] - [0, 21] markdown 154(inline))))))) ; [0, 4] - [0, 21] markdown_inline 155(translation_unit ; [0, 4] - [0, 21] c 156(preproc_include ; [0, 4] - [0, 21] c 157path: (system_lib_string)))))) ; [0, 12] - [0, 21] c 158]] 159end ) 160 161it ( 'can toggle to show languages' , function () 162insert ( [[ 163```lua 164return 165``` 166]] ) 167 168exec_lua ( function () 169vim . treesitter . start ( 0 , 'markdown' ) 170vim . treesitter . get_parser (): parse () 171vim . treesitter . inspect_tree () 172end ) 173feed ( 'I' ) 174 175expect_tree [[ 176(document ; [0, 0] - [4, 0] markdown 177(section ; [0, 0] - [4, 0] markdown 178(fenced_code_block ; [0, 0] - [3, 0] markdown 179(fenced_code_block_delimiter) ; [0, 0] - [0, 3] markdown 180(info_string ; [0, 3] - [0, 6] markdown 181(language)) ; [0, 3] - [0, 6] markdown 182(block_continuation) ; [1, 0] - [1, 0] markdown 183(code_fence_content ; [1, 0] - [2, 0] markdown 184(chunk ; [1, 0] - [2, 0] lua 185(return_statement)) ; [1, 0] - [1, 6] lua 186(block_continuation)) ; [2, 0] - [2, 0] markdown 187(fenced_code_block_delimiter)))) ; [2, 0] - [2, 3] markdown 188]] 189end ) 190 191it ( 'updates source and tree buffer windows and closes them correctly' , function () 192local name =t . tmpname () 193n . command ( 'edit ' .. name ) 194insert ( [[ 195print() 196]] ) 197n . command ( 'set filetype=lua | write' ) 198 199-- setup two windows for the source buffer 200exec_lua ( function () 201_G . source_win =vim . api . nvim_get_current_win () 202_G . source_win2 =vim . api . nvim_open_win ( 0 , false , { 203win =0 , 204split ='left' , 205} ) 206end ) 207 208-- setup three windows for the tree buffer 209exec_lua ( function () 210vim . treesitter . inspect_tree () 211_G . tree_win =vim . api . nvim_get_current_win () 212_G . tree_win2 =vim . api . nvim_open_win ( 0 , false , { 213win =0 , 214split ='left' , 215} ) 216_G . tree_win3 =vim . api . nvim_open_win ( 0 , false , { 217win =0 , 218split ='left' , 219} ) 220end ) 221 222-- close original source window without closing tree views 223exec_lua ( 'vim.api.nvim_set_current_win(source_win)' ) 224feed ( ':quit<CR>' ) 225eq ( '' , n . api . nvim_get_vvar ( 'errmsg' )) 226eq ( true , exec_lua ( 'return vim.api.nvim_win_is_valid(tree_win)' )) 227eq ( true , exec_lua ( 'return vim.api.nvim_win_is_valid(tree_win2)' )) 228eq ( true , exec_lua ( 'return vim.api.nvim_win_is_valid(tree_win3)' )) 229 230-- navigates correctly to the remaining source buffer window 231exec_lua ( 'vim.api.nvim_set_current_win(tree_win)' ) 232feed ( '<CR>' ) 233eq ( '' , n . api . nvim_get_vvar ( 'errmsg' )) 234eq ( true , exec_lua ( 'return vim.api.nvim_get_current_win() == source_win2' )) 235 236-- close original tree window 237exec_lua ( function () 238vim . api . nvim_set_current_win ( _G . tree_win2 ) 239vim . api . nvim_win_close ( _G . tree_win , false ) 240end ) 241 242-- navigates correctly to the remaining source buffer window 243feed ( '<CR>' ) 244eq ( '' , n . api . nvim_get_vvar ( 'errmsg' )) 245eq ( true , exec_lua ( 'return vim.api.nvim_get_current_win() == source_win2' )) 246 247-- close source buffer window and all remaining tree windows 248n . expect_exit ( n . command , 'quit' ) 249end ) 250 251it ( 'shows which nodes are missing' , function () 252insert ( [[ 253int main() { 254if (a.) { 255// ^ MISSING field_identifier here 256if (1) d() 257// ^ MISSING ";" here 258} 259} 260]] ) 261 262exec_lua ( function () 263vim . treesitter . start ( 0 , 'c' ) 264vim . treesitter . inspect_tree () 265end ) 266feed ( 'a' ) 267 268expect_tree [[ 269(translation_unit ; [0, 0] - [8, 0] 270(function_definition ; [0, 0] - [6, 1] 271type: (primitive_type) ; [0, 0] - [0, 3] 272declarator: (function_declarator ; [0, 4] - [0, 10] 273declarator: (identifier) ; [0, 4] - [0, 8] 274parameters: (parameter_list ; [0, 8] - [0, 10] 275"(" ; [0, 8] - [0, 9] 276")")) ; [0, 9] - [0, 10] 277body: (compound_statement ; [0, 11] - [6, 1] 278"{" ; [0, 11] - [0, 12] 279(if_statement ; [1, 4] - [5, 5] 280"if" ; [1, 4] - [1, 6] 281condition: (parenthesized_expression ; [1, 7] - [1, 11] 282"(" ; [1, 7] - [1, 8] 283(field_expression ; [1, 8] - [1, 10] 284argument: (identifier) ; [1, 8] - [1, 9] 285operator: "." ; [1, 9] - [1, 10] 286field: (MISSING field_identifier)) ; [1, 10] - [1, 10] 287")") ; [1, 10] - [1, 11] 288consequence: (compound_statement ; [1, 12] - [5, 5] 289"{" ; [1, 12] - [1, 13] 290(comment) ; [2, 4] - [2, 41] 291(if_statement ; [3, 8] - [4, 36] 292"if" ; [3, 8] - [3, 10] 293condition: (parenthesized_expression ; [3, 11] - [3, 14] 294"(" ; [3, 11] - [3, 12] 295(number_literal) ; [3, 12] - [3, 13] 296")") ; [3, 13] - [3, 14] 297consequence: (expression_statement ; [3, 15] - [4, 36] 298(call_expression ; [3, 15] - [3, 18] 299function: (identifier) ; [3, 15] - [3, 16] 300arguments: (argument_list ; [3, 16] - [3, 18] 301"(" ; [3, 16] - [3, 17] 302")")) ; [3, 17] - [3, 18] 303(comment) ; [4, 8] - [4, 36] 304(MISSING ";"))) ; [4, 36] - [4, 36] 305"}")) ; [5, 4] - [5, 5] 306"}"))) ; [6, 0] - [6, 1] 307]] 308end ) 309end )