parser_spec.lua 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local ts_t = require('test.functional.treesitter.testutil')
  4. local clear = n.clear
  5. local dedent = t.dedent
  6. local eq = t.eq
  7. local insert = n.insert
  8. local exec_lua = n.exec_lua
  9. local pcall_err = t.pcall_err
  10. local feed = n.feed
  11. local run_query = ts_t.run_query
  12. local assert_alive = n.assert_alive
  13. describe('treesitter parser API', function()
  14. before_each(function()
  15. clear()
  16. exec_lua(function()
  17. vim.g.__ts_debug = 1
  18. end)
  19. end)
  20. it('parses buffer', function()
  21. insert([[
  22. int main() {
  23. int x = 3;
  24. }]])
  25. exec_lua(function()
  26. _G.parser = vim.treesitter.get_parser(0, 'c')
  27. _G.tree = _G.parser:parse()[1]
  28. _G.root = _G.tree:root()
  29. _G.lang = vim.treesitter.language.inspect('c')
  30. end)
  31. eq('<tree>', exec_lua('return tostring(tree)'))
  32. eq('<node translation_unit>', exec_lua('return tostring(root)'))
  33. eq({ 0, 0, 3, 0 }, exec_lua('return {root:range()}'))
  34. eq(1, exec_lua('return root:child_count()'))
  35. exec_lua('child = root:child(0)')
  36. eq('<node function_definition>', exec_lua('return tostring(child)'))
  37. eq({ 0, 0, 2, 1 }, exec_lua('return {child:range()}'))
  38. eq('function_definition', exec_lua('return child:type()'))
  39. eq(true, exec_lua('return child:named()'))
  40. eq('number', type(exec_lua('return child:symbol()')))
  41. eq(true, exec_lua('return lang.symbols[child:type()]'))
  42. exec_lua('anon = root:descendant_for_range(0,8,0,9)')
  43. eq('(', exec_lua('return anon:type()'))
  44. eq(false, exec_lua('return anon:named()'))
  45. eq('number', type(exec_lua('return anon:symbol()')))
  46. eq(false, exec_lua([=[return lang.symbols[string.format('"%s"', anon:type())]]=]))
  47. exec_lua('descendant = root:descendant_for_range(1,2,1,12)')
  48. eq('<node declaration>', exec_lua('return tostring(descendant)'))
  49. eq({ 1, 2, 1, 12 }, exec_lua('return {descendant:range()}'))
  50. eq(
  51. '(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))',
  52. exec_lua('return descendant:sexpr()')
  53. )
  54. feed('2G7|ay')
  55. exec_lua(function()
  56. _G.tree2 = _G.parser:parse()[1]
  57. _G.root2 = _G.tree2:root()
  58. _G.descendant2 = _G.root2:descendant_for_range(1, 2, 1, 13)
  59. end)
  60. eq(false, exec_lua('return tree2 == tree1'))
  61. eq(false, exec_lua('return root2 == root'))
  62. eq('<node declaration>', exec_lua('return tostring(descendant2)'))
  63. eq({ 1, 2, 1, 13 }, exec_lua('return {descendant2:range()}'))
  64. eq(true, exec_lua('return child == child'))
  65. -- separate lua object, but represents same node
  66. eq(true, exec_lua('return child == root:child(0)'))
  67. eq(false, exec_lua('return child == descendant2'))
  68. eq(false, exec_lua('return child == nil'))
  69. eq(false, exec_lua('return child == tree'))
  70. eq('string', exec_lua('return type(child:id())'))
  71. eq(true, exec_lua('return child:id() == child:id()'))
  72. -- separate lua object, but represents same node
  73. eq(true, exec_lua('return child:id() == root:child(0):id()'))
  74. eq(false, exec_lua('return child:id() == descendant2:id()'))
  75. eq(false, exec_lua('return child:id() == nil'))
  76. eq(false, exec_lua('return child:id() == tree'))
  77. -- unchanged buffer: return the same tree
  78. eq(true, exec_lua('return parser:parse()[1] == tree2'))
  79. end)
  80. it('parses buffer asynchronously', function()
  81. insert([[
  82. int main() {
  83. int x = 3;
  84. }]])
  85. exec_lua(function()
  86. _G.parser = vim.treesitter.get_parser(0, 'c')
  87. _G.lang = vim.treesitter.language.inspect('c')
  88. _G.parser:parse(nil, function(_, trees)
  89. _G.tree = trees[1]
  90. _G.root = _G.tree:root()
  91. end)
  92. vim.wait(100, function() end)
  93. end)
  94. eq('<tree>', exec_lua('return tostring(tree)'))
  95. eq('<node translation_unit>', exec_lua('return tostring(root)'))
  96. eq({ 0, 0, 3, 0 }, exec_lua('return {root:range()}'))
  97. eq(1, exec_lua('return root:child_count()'))
  98. exec_lua('child = root:child(0)')
  99. eq('<node function_definition>', exec_lua('return tostring(child)'))
  100. eq({ 0, 0, 2, 1 }, exec_lua('return {child:range()}'))
  101. eq('function_definition', exec_lua('return child:type()'))
  102. eq(true, exec_lua('return child:named()'))
  103. eq('number', type(exec_lua('return child:symbol()')))
  104. eq(true, exec_lua('return lang.symbols[child:type()]'))
  105. exec_lua('anon = root:descendant_for_range(0,8,0,9)')
  106. eq('(', exec_lua('return anon:type()'))
  107. eq(false, exec_lua('return anon:named()'))
  108. eq('number', type(exec_lua('return anon:symbol()')))
  109. eq(false, exec_lua([=[return lang.symbols[string.format('"%s"', anon:type())]]=]))
  110. exec_lua('descendant = root:descendant_for_range(1,2,1,12)')
  111. eq('<node declaration>', exec_lua('return tostring(descendant)'))
  112. eq({ 1, 2, 1, 12 }, exec_lua('return {descendant:range()}'))
  113. eq(
  114. '(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))',
  115. exec_lua('return descendant:sexpr()')
  116. )
  117. feed('2G7|ay')
  118. exec_lua(function()
  119. _G.parser:parse(nil, function(_, trees)
  120. _G.tree2 = trees[1]
  121. _G.root2 = _G.tree2:root()
  122. _G.descendant2 = _G.root2:descendant_for_range(1, 2, 1, 13)
  123. end)
  124. vim.wait(100, function() end)
  125. end)
  126. eq(false, exec_lua('return tree2 == tree1'))
  127. eq(false, exec_lua('return root2 == root'))
  128. eq('<node declaration>', exec_lua('return tostring(descendant2)'))
  129. eq({ 1, 2, 1, 13 }, exec_lua('return {descendant2:range()}'))
  130. eq(true, exec_lua('return child == child'))
  131. -- separate lua object, but represents same node
  132. eq(true, exec_lua('return child == root:child(0)'))
  133. eq(false, exec_lua('return child == descendant2'))
  134. eq(false, exec_lua('return child == nil'))
  135. eq(false, exec_lua('return child == tree'))
  136. eq('string', exec_lua('return type(child:id())'))
  137. eq(true, exec_lua('return child:id() == child:id()'))
  138. -- separate lua object, but represents same node
  139. eq(true, exec_lua('return child:id() == root:child(0):id()'))
  140. eq(false, exec_lua('return child:id() == descendant2:id()'))
  141. eq(false, exec_lua('return child:id() == nil'))
  142. eq(false, exec_lua('return child:id() == tree'))
  143. -- unchanged buffer: return the same tree
  144. eq(true, exec_lua('return parser:parse()[1] == tree2'))
  145. end)
  146. it('does not crash when editing large files', function()
  147. insert([[printf("%s", "some text");]])
  148. feed('yy49999p')
  149. exec_lua(function()
  150. _G.parser = vim.treesitter.get_parser(0, 'c')
  151. _G.done = false
  152. vim.treesitter.start(0, 'c')
  153. _G.parser:parse(nil, function()
  154. _G.done = true
  155. end)
  156. while not _G.done do
  157. -- Busy wait until async parsing has completed
  158. vim.wait(100, function() end)
  159. end
  160. end)
  161. eq(true, exec_lua([[return done]]))
  162. exec_lua(function()
  163. vim.api.nvim_input('Lxj')
  164. end)
  165. exec_lua(function()
  166. vim.api.nvim_input('xj')
  167. end)
  168. exec_lua(function()
  169. vim.api.nvim_input('xj')
  170. end)
  171. assert_alive()
  172. end)
  173. it('resets parsing state on tree changes', function()
  174. insert([[vim.api.nvim_set_hl(0, 'test2', { bg = 'green' })]])
  175. feed('yy1000p')
  176. exec_lua(function()
  177. vim.cmd('set ft=lua')
  178. vim.treesitter.start(0)
  179. local parser = assert(vim.treesitter.get_parser(0))
  180. parser:parse(true, function() end)
  181. vim.api.nvim_buf_set_lines(0, 1, -1, false, {})
  182. parser:parse(true)
  183. end)
  184. end)
  185. it('resets when buffer was editing during an async parse', function()
  186. insert([[printf("%s", "some text");]])
  187. feed('yy49999p')
  188. feed('gg4jO// Comment<Esc>')
  189. exec_lua(function()
  190. _G.parser = vim.treesitter.get_parser(0, 'c')
  191. _G.done = false
  192. vim.treesitter.start(0, 'c')
  193. _G.parser:parse(nil, function()
  194. _G.done = true
  195. end)
  196. end)
  197. exec_lua(function()
  198. vim.api.nvim_input('ggdj')
  199. end)
  200. eq(false, exec_lua([[return done]]))
  201. exec_lua(function()
  202. while not _G.done do
  203. -- Busy wait until async parsing finishes
  204. vim.wait(100, function() end)
  205. end
  206. end)
  207. eq(true, exec_lua([[return done]]))
  208. eq('comment', exec_lua([[return parser:parse()[1]:root():named_child(2):type()]]))
  209. eq({ 2, 0, 2, 10 }, exec_lua([[return {parser:parse()[1]:root():named_child(2):range()}]]))
  210. end)
  211. it('handles multiple async parse calls', function()
  212. insert([[printf("%s", "some text");]])
  213. feed('yy49999p')
  214. exec_lua(function()
  215. -- Spy on vim.schedule
  216. local schedule = vim.schedule
  217. vim.schedule = function(fn)
  218. _G.schedules = _G.schedules + 1
  219. schedule(fn)
  220. end
  221. _G.schedules = 0
  222. _G.parser = vim.treesitter.get_parser(0, 'c')
  223. for i = 1, 5 do
  224. _G['done' .. i] = false
  225. _G.parser:parse(nil, function()
  226. _G['done' .. i] = true
  227. end)
  228. end
  229. schedule(function()
  230. _G.schedules_snapshot = _G.schedules
  231. end)
  232. end)
  233. eq(2, exec_lua([[return schedules_snapshot]]))
  234. eq(
  235. { false, false, false, false, false },
  236. exec_lua([[return { done1, done2, done3, done4, done5 }]])
  237. )
  238. exec_lua(function()
  239. while not _G.done1 do
  240. -- Busy wait until async parsing finishes
  241. vim.wait(100, function() end)
  242. end
  243. end)
  244. eq({ true, true, true, true, true }, exec_lua([[return { done1, done2, done3, done4, done5 }]]))
  245. end)
  246. local test_text = [[
  247. void ui_refresh(void)
  248. {
  249. int width = INT_MAX, height = INT_MAX;
  250. bool ext_widgets[kUIExtCount];
  251. for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
  252. ext_widgets[i] = true;
  253. }
  254. bool inclusive = ui_override();
  255. for (size_t i = 0; i < ui_count; i++) {
  256. UI *ui = uis[i];
  257. width = MIN(ui->width, width);
  258. height = MIN(ui->height, height);
  259. foo = BAR(ui->bazaar, bazaar);
  260. for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
  261. ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
  262. }
  263. }
  264. }]]
  265. it('allows to iterate over nodes children', function()
  266. insert(test_text)
  267. local res = exec_lua(function()
  268. local parser = vim.treesitter.get_parser(0, 'c')
  269. local func_node = parser:parse()[1]:root():child(0)
  270. local res = {}
  271. for node, field in func_node:iter_children() do
  272. table.insert(res, { node:type(), field })
  273. end
  274. return res
  275. end)
  276. eq({
  277. { 'primitive_type', 'type' },
  278. { 'function_declarator', 'declarator' },
  279. { 'compound_statement', 'body' },
  280. }, res)
  281. end)
  282. it('does not get parser for empty filetype', function()
  283. insert(test_text)
  284. eq(
  285. '.../treesitter.lua:0: Parser not found for buffer 1: language could not be determined',
  286. pcall_err(exec_lua, 'vim.treesitter.get_parser(0)')
  287. )
  288. -- Must provide language for buffers with an empty filetype
  289. exec_lua("vim.treesitter.get_parser(0, 'c')")
  290. end)
  291. it('allows to get a child by field', function()
  292. insert(test_text)
  293. local res = exec_lua(function()
  294. local parser = vim.treesitter.get_parser(0, 'c')
  295. _G.func_node = parser:parse()[1]:root():child(0)
  296. local res = {}
  297. for _, node in ipairs(_G.func_node:field('type')) do
  298. table.insert(res, { node:type(), node:range() })
  299. end
  300. return res
  301. end)
  302. eq({ { 'primitive_type', 0, 0, 0, 4 } }, res)
  303. local res_fail = exec_lua(function()
  304. vim.treesitter.get_parser(0, 'c')
  305. return #_G.func_node:field('foo') == 0
  306. end)
  307. assert(res_fail)
  308. end)
  309. it('supports getting text of multiline node', function()
  310. insert(test_text)
  311. local res = exec_lua(function()
  312. local parser = vim.treesitter.get_parser(0, 'c')
  313. local tree = parser:parse()[1]
  314. return vim.treesitter.get_node_text(tree:root(), 0)
  315. end)
  316. eq(test_text, res)
  317. local res2 = exec_lua(function()
  318. local parser = vim.treesitter.get_parser(0, 'c')
  319. local root = parser:parse()[1]:root()
  320. return vim.treesitter.get_node_text(root:child(0):child(0), 0)
  321. end)
  322. eq('void', res2)
  323. end)
  324. it('supports getting text where start of node is one past EOF', function()
  325. local text = [[
  326. def run
  327. a = <<~E
  328. end]]
  329. insert(text)
  330. eq(
  331. '',
  332. exec_lua(function()
  333. local fake_node = {}
  334. function fake_node:start()
  335. return 3, 0, 23
  336. end
  337. function fake_node:end_()
  338. return 3, 0, 23
  339. end
  340. function fake_node:range(bytes)
  341. if bytes then
  342. return 3, 0, 23, 3, 0, 23
  343. end
  344. return 3, 0, 3, 0
  345. end
  346. return vim.treesitter.get_node_text(fake_node, 0)
  347. end)
  348. )
  349. end)
  350. it('supports getting empty text if node range is zero width', function()
  351. local text = [[
  352. ```lua
  353. {}
  354. ```]]
  355. insert(text)
  356. local result = exec_lua(function()
  357. local fake_node = {}
  358. function fake_node:start()
  359. return 1, 0, 7
  360. end
  361. function fake_node:end_()
  362. return 1, 0, 7
  363. end
  364. function fake_node:range()
  365. return 1, 0, 1, 0
  366. end
  367. return vim.treesitter.get_node_text(fake_node, 0) == ''
  368. end)
  369. eq(true, result)
  370. end)
  371. it('allows to set simple ranges', function()
  372. insert(test_text)
  373. local res = exec_lua(function()
  374. _G.parser = vim.treesitter.get_parser(0, 'c')
  375. return { _G.parser:parse()[1]:root():range() }
  376. end)
  377. eq({ 0, 0, 19, 0 }, res)
  378. -- The following sets the included ranges for the current parser
  379. -- As stated here, this only includes the function (thus the whole buffer, without the last line)
  380. local res2 = exec_lua(function()
  381. local root = _G.parser:parse()[1]:root()
  382. _G.parser:set_included_regions({ { root:child(0) } })
  383. _G.parser:invalidate()
  384. return { _G.parser:parse(true)[1]:root():range() }
  385. end)
  386. eq({ 0, 0, 18, 1 }, res2)
  387. eq({ { { 0, 0, 0, 18, 1, 512 } } }, exec_lua [[ return parser:included_regions() ]])
  388. local range_tbl = exec_lua(function()
  389. _G.parser:set_included_regions { { { 0, 0, 17, 1 } } }
  390. _G.parser:parse()
  391. return _G.parser:included_regions()
  392. end)
  393. eq({ { { 0, 0, 0, 17, 1, 508 } } }, range_tbl)
  394. end)
  395. it('allows to set complex ranges', function()
  396. insert(test_text)
  397. local res = exec_lua(function()
  398. local parser = vim.treesitter.get_parser(0, 'c')
  399. local query = vim.treesitter.query.parse('c', '(declaration) @decl')
  400. local nodes = {}
  401. for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do
  402. table.insert(nodes, node)
  403. end
  404. parser:set_included_regions({ nodes })
  405. local root = parser:parse(true)[1]:root()
  406. local res = {}
  407. for i = 0, (root:named_child_count() - 1) do
  408. table.insert(res, { root:named_child(i):range() })
  409. end
  410. return res
  411. end)
  412. eq({
  413. { 2, 2, 2, 40 },
  414. { 3, 2, 3, 32 },
  415. { 4, 7, 4, 25 },
  416. { 8, 2, 8, 33 },
  417. { 9, 7, 9, 20 },
  418. { 10, 4, 10, 20 },
  419. { 14, 9, 14, 27 },
  420. }, res)
  421. end)
  422. it('allows to create string parsers', function()
  423. local ret = exec_lua(function()
  424. local parser = vim.treesitter.get_string_parser('int foo = 42;', 'c')
  425. return { parser:parse()[1]:root():range() }
  426. end)
  427. eq({ 0, 0, 0, 13 }, ret)
  428. end)
  429. it('can run async parses with string parsers', function()
  430. local ret = exec_lua(function()
  431. local parser = vim.treesitter.get_string_parser('int foo = 42;', 'c')
  432. return { parser:parse(nil, function() end)[1]:root():range() }
  433. end)
  434. eq({ 0, 0, 0, 13 }, ret)
  435. end)
  436. it('allows to run queries with string parsers', function()
  437. local txt = [[
  438. int foo = 42;
  439. int bar = 13;
  440. ]]
  441. local ret = exec_lua(function(str)
  442. local parser = vim.treesitter.get_string_parser(str, 'c')
  443. local nodes = {}
  444. local query = vim.treesitter.query.parse('c', '((identifier) @id (#eq? @id "foo"))')
  445. for _, node in query:iter_captures(parser:parse()[1]:root(), str) do
  446. table.insert(nodes, { node:range() })
  447. end
  448. return nodes
  449. end, txt)
  450. eq({ { 0, 10, 0, 13 } }, ret)
  451. end)
  452. describe('when creating a language tree', function()
  453. local function get_ranges()
  454. return exec_lua(function()
  455. local result = {}
  456. _G.parser:for_each_tree(function(tree)
  457. table.insert(result, { tree:root():range() })
  458. end)
  459. return result
  460. end)
  461. end
  462. before_each(function()
  463. insert([[
  464. int x = INT_MAX;
  465. #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  466. #define READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  467. #define VALUE 123
  468. #define VALUE1 123
  469. #define VALUE2 123
  470. ]])
  471. end)
  472. describe('when parsing regions independently', function()
  473. it('should inject a language', function()
  474. exec_lua(function()
  475. _G.parser = vim.treesitter.get_parser(0, 'c', {
  476. injections = {
  477. c = (
  478. '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) '
  479. .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
  480. ),
  481. },
  482. })
  483. _G.parser:parse(true)
  484. end)
  485. eq('table', exec_lua('return type(parser:children().c)'))
  486. eq(5, exec_lua('return #parser:children().c:trees()'))
  487. eq({
  488. { 0, 0, 7, 0 }, -- root tree
  489. { 3, 14, 3, 17 }, -- VALUE 123
  490. { 4, 15, 4, 18 }, -- VALUE1 123
  491. { 5, 15, 5, 18 }, -- VALUE2 123
  492. { 1, 26, 1, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  493. { 2, 29, 2, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  494. }, get_ranges())
  495. n.feed('ggo<esc>')
  496. eq(5, exec_lua('return #parser:children().c:trees()'))
  497. eq({
  498. { 0, 0, 8, 0 }, -- root tree
  499. { 4, 14, 4, 17 }, -- VALUE 123
  500. { 5, 15, 5, 18 }, -- VALUE1 123
  501. { 6, 15, 6, 18 }, -- VALUE2 123
  502. { 2, 26, 2, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  503. { 3, 29, 3, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  504. }, get_ranges())
  505. end)
  506. end)
  507. describe('when parsing regions combined', function()
  508. it('should inject a language', function()
  509. exec_lua(function()
  510. _G.parser = vim.treesitter.get_parser(0, 'c', {
  511. injections = {
  512. c = (
  513. '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined)) '
  514. .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined))'
  515. ),
  516. },
  517. })
  518. _G.parser:parse(true)
  519. end)
  520. eq('table', exec_lua('return type(parser:children().c)'))
  521. eq(2, exec_lua('return #parser:children().c:trees()'))
  522. eq({
  523. { 0, 0, 7, 0 }, -- root tree
  524. { 3, 14, 5, 18 }, -- VALUE 123
  525. -- VALUE1 123
  526. -- VALUE2 123
  527. { 1, 26, 2, 66 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  528. -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  529. }, get_ranges())
  530. n.feed('ggo<esc>')
  531. eq('table', exec_lua('return type(parser:children().c)'))
  532. eq(2, exec_lua('return #parser:children().c:trees()'))
  533. eq({
  534. { 0, 0, 8, 0 }, -- root tree
  535. { 4, 14, 6, 18 }, -- VALUE 123
  536. -- VALUE1 123
  537. -- VALUE2 123
  538. { 2, 26, 3, 66 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  539. -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  540. }, get_ranges())
  541. n.feed('7ggI//<esc>')
  542. exec_lua([[parser:parse({5, 6})]])
  543. eq('table', exec_lua('return type(parser:children().c)'))
  544. eq(2, exec_lua('return #parser:children().c:trees()'))
  545. eq({
  546. { 0, 0, 8, 0 }, -- root tree
  547. { 4, 14, 5, 18 }, -- VALUE 123
  548. -- VALUE1 123
  549. { 2, 26, 3, 66 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  550. -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  551. }, get_ranges())
  552. end)
  553. end)
  554. describe('when using injection.self', function()
  555. it('should inject the source language', function()
  556. exec_lua(function()
  557. _G.parser = vim.treesitter.get_parser(0, 'c', {
  558. injections = {
  559. c = (
  560. '(preproc_def (preproc_arg) @injection.content (#set! injection.self)) '
  561. .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))'
  562. ),
  563. },
  564. })
  565. _G.parser:parse(true)
  566. end)
  567. eq('table', exec_lua('return type(parser:children().c)'))
  568. eq(5, exec_lua('return #parser:children().c:trees()'))
  569. eq({
  570. { 0, 0, 7, 0 }, -- root tree
  571. { 3, 14, 3, 17 }, -- VALUE 123
  572. { 4, 15, 4, 18 }, -- VALUE1 123
  573. { 5, 15, 5, 18 }, -- VALUE2 123
  574. { 1, 26, 1, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  575. { 2, 29, 2, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  576. }, get_ranges())
  577. n.feed('ggo<esc>')
  578. eq(5, exec_lua('return #parser:children().c:trees()'))
  579. eq({
  580. { 0, 0, 8, 0 }, -- root tree
  581. { 4, 14, 4, 17 }, -- VALUE 123
  582. { 5, 15, 5, 18 }, -- VALUE1 123
  583. { 6, 15, 6, 18 }, -- VALUE2 123
  584. { 2, 26, 2, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  585. { 3, 29, 3, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  586. }, get_ranges())
  587. end)
  588. end)
  589. describe('when using the offset directive', function()
  590. it('should shift the range by the directive amount', function()
  591. exec_lua(function()
  592. _G.parser = vim.treesitter.get_parser(0, 'c', {
  593. injections = {
  594. c = (
  595. '(preproc_def ((preproc_arg) @injection.content (#set! injection.language "c") (#offset! @injection.content 0 2 0 -1))) '
  596. .. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
  597. ),
  598. },
  599. })
  600. _G.parser:parse(true)
  601. end)
  602. eq('table', exec_lua('return type(parser:children().c)'))
  603. eq({
  604. { 0, 0, 7, 0 }, -- root tree
  605. { 3, 16, 3, 16 }, -- VALUE 123
  606. { 4, 17, 4, 17 }, -- VALUE1 123
  607. { 5, 17, 5, 17 }, -- VALUE2 123
  608. { 1, 26, 1, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  609. { 2, 29, 2, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
  610. }, get_ranges())
  611. end)
  612. it('should list all directives', function()
  613. local res_list = exec_lua(function()
  614. local query = vim.treesitter.query
  615. local list = query.list_directives()
  616. table.sort(list)
  617. return list
  618. end)
  619. eq({ 'gsub!', 'offset!', 'set!', 'trim!' }, res_list)
  620. end)
  621. end)
  622. end)
  623. describe('when getting the language for a range', function()
  624. before_each(function()
  625. insert([[
  626. int x = INT_MAX;
  627. #define VALUE 123456789
  628. ]])
  629. end)
  630. it('should return the correct language tree', function()
  631. local result = exec_lua(function()
  632. local parser = vim.treesitter.get_parser(0, 'c', {
  633. injections = {
  634. c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))',
  635. },
  636. })
  637. parser:parse(true)
  638. local sub_tree = parser:language_for_range({ 1, 18, 1, 19 })
  639. return sub_tree == parser:children().c
  640. end)
  641. eq(true, result)
  642. end)
  643. end)
  644. describe('when setting the node for an injection', function()
  645. before_each(function()
  646. insert([[
  647. print()
  648. ]])
  649. end)
  650. it('ignores optional captures #23100', function()
  651. local result = exec_lua(function()
  652. local parser = vim.treesitter.get_parser(0, 'lua', {
  653. injections = {
  654. lua = (
  655. '(function_call '
  656. .. '(arguments '
  657. .. '(string)? @injection.content '
  658. .. '(number)? @injection.content '
  659. .. '(#offset! @injection.content 0 1 0 -1) '
  660. .. '(#set! injection.language "c")))'
  661. ),
  662. },
  663. })
  664. parser:parse(true)
  665. return parser:is_valid()
  666. end)
  667. eq(true, result)
  668. end)
  669. end)
  670. describe('when getting/setting match data', function()
  671. describe('when setting for the whole match', function()
  672. it('should set/get the data correctly', function()
  673. insert([[
  674. int x = 3;
  675. ]])
  676. local result = exec_lua(function()
  677. local query =
  678. vim.treesitter.query.parse('c', '((number_literal) @number (#set! "key" "value"))')
  679. local parser = vim.treesitter.get_parser(0, 'c')
  680. local _, _, metadata = query:iter_matches(parser:parse()[1]:root(), 0, 0, -1)()
  681. return metadata.key
  682. end)
  683. eq('value', result)
  684. end)
  685. describe('when setting a key on a capture', function()
  686. it('it should create the nested table', function()
  687. insert([[
  688. int x = 3;
  689. ]])
  690. local result = exec_lua(function()
  691. local query = vim.treesitter.query.parse(
  692. 'c',
  693. '((number_literal) @number (#set! @number "key" "value"))'
  694. )
  695. local parser = vim.treesitter.get_parser(0, 'c')
  696. local _, _, metadata = query:iter_matches(parser:parse()[1]:root(), 0, 0, -1)()
  697. local _, nested_tbl = next(metadata)
  698. return nested_tbl.key
  699. end)
  700. eq('value', result)
  701. end)
  702. it('it should not overwrite the nested table', function()
  703. insert([[
  704. int x = 3;
  705. ]])
  706. local result = exec_lua(function()
  707. local query = vim.treesitter.query.parse(
  708. 'c',
  709. '((number_literal) @number (#set! @number "key" "value") (#set! @number "key2" "value2"))'
  710. )
  711. local parser = vim.treesitter.get_parser(0, 'c')
  712. local _, _, metadata = query:iter_matches(parser:parse()[1]:root(), 0, 0, -1)()
  713. local _, nested_tbl = next(metadata)
  714. return nested_tbl
  715. end)
  716. local expected = {
  717. ['key'] = 'value',
  718. ['key2'] = 'value2',
  719. }
  720. eq(expected, result)
  721. end)
  722. end)
  723. end)
  724. end)
  725. describe('trim! directive', function()
  726. it('can trim all whitespace', function()
  727. -- luacheck: push ignore 611 613
  728. insert([=[
  729. print([[
  730. f
  731. helllo
  732. there
  733. asdf
  734. asdfassd
  735. ]])
  736. print([[
  737. ]])
  738. print([[]])
  739. print([[
  740. ]])
  741. print([[ hello 😃 ]])
  742. ]=])
  743. -- luacheck: pop
  744. local query_text = [[
  745. ; query
  746. ((string_content) @str
  747. (#trim! @str 1 1 1 1))
  748. ]]
  749. exec_lua(function()
  750. vim.treesitter.start(0, 'lua')
  751. end)
  752. eq({
  753. { 'str', { 2, 12, 6, 10 } },
  754. { 'str', { 11, 10, 11, 10 } },
  755. { 'str', { 17, 10, 17, 10 } },
  756. { 'str', { 19, 10, 19, 10 } },
  757. { 'str', { 22, 15, 22, 25 } },
  758. }, run_query('lua', query_text))
  759. end)
  760. it('trims only empty lines by default (backwards compatible)', function()
  761. insert(dedent [[
  762. ## Heading
  763. With some text
  764. ## And another
  765. With some more here]])
  766. local query_text = [[
  767. ; query
  768. ((section) @fold
  769. (#trim! @fold))
  770. ]]
  771. exec_lua(function()
  772. vim.treesitter.start(0, 'markdown')
  773. end)
  774. eq({
  775. { 'fold', { 0, 0, 2, 14 } },
  776. { 'fold', { 4, 0, 6, 19 } },
  777. }, run_query('markdown', query_text))
  778. end)
  779. it('can trim lines', function()
  780. insert(dedent [[
  781. - Fold list
  782. - Fold list
  783. - Fold list
  784. - Fold list
  785. - Fold list
  786. - Fold list
  787. ]])
  788. local query_text = [[
  789. ; query
  790. ((list_item
  791. (list)) @fold
  792. (#trim! @fold 1 1 1 1))
  793. ]]
  794. exec_lua(function()
  795. vim.treesitter.start(0, 'markdown')
  796. end)
  797. eq({
  798. { 'fold', { 0, 0, 4, 13 } },
  799. { 'fold', { 1, 2, 3, 15 } },
  800. }, run_query('markdown', query_text))
  801. end)
  802. end)
  803. it('tracks the root range properly (#22911)', function()
  804. insert([[
  805. int main() {
  806. int x = 3;
  807. }]])
  808. local query0 = [[
  809. (declaration) @declaration
  810. (function_definition) @function
  811. ]]
  812. exec_lua(function()
  813. vim.treesitter.start(0, 'c')
  814. end)
  815. eq({
  816. { 'function', { 0, 0, 2, 1 } },
  817. { 'declaration', { 1, 2, 1, 12 } },
  818. }, run_query('c', query0))
  819. n.command 'normal ggO'
  820. insert('int a;')
  821. eq({
  822. { 'declaration', { 0, 0, 0, 6 } },
  823. { 'function', { 1, 0, 3, 1 } },
  824. { 'declaration', { 2, 2, 2, 12 } },
  825. }, run_query('c', query0))
  826. end)
  827. it('handles ranges when source is a multiline string (#20419)', function()
  828. local source = [==[
  829. vim.cmd[[
  830. set number
  831. set cmdheight=2
  832. set lastsatus=2
  833. ]]
  834. set query = [[;; query
  835. ((function_call
  836. name: [
  837. (identifier) @_cdef_identifier
  838. (_ _ (identifier) @_cdef_identifier)
  839. ]
  840. arguments: (arguments (string content: _ @injection.content)))
  841. (#set! injection.language "c")
  842. (#eq? @_cdef_identifier "cdef"))
  843. ]]
  844. ]==]
  845. local r = exec_lua(function()
  846. local parser = vim.treesitter.get_string_parser(source, 'lua')
  847. parser:parse(true)
  848. local ranges = {}
  849. parser:for_each_tree(function(tstree, tree)
  850. ranges[tree:lang()] = { tstree:root():range(true) }
  851. end)
  852. return ranges
  853. end)
  854. eq({
  855. lua = { 0, 6, 6, 16, 4, 438 },
  856. query = { 6, 20, 113, 15, 6, 431 },
  857. vim = { 1, 0, 16, 4, 6, 89 },
  858. }, r)
  859. -- The above ranges are provided directly from treesitter, however query directives may mutate
  860. -- the ranges but only provide a Range4. Strip the byte entries from the ranges and make sure
  861. -- add_bytes() produces the same result.
  862. local rb = exec_lua(function()
  863. local add_bytes = require('vim.treesitter._range').add_bytes
  864. for lang, range in pairs(r) do
  865. r[lang] = { range[1], range[2], range[4], range[5] }
  866. r[lang] = add_bytes(source, r[lang])
  867. end
  868. return r
  869. end)
  870. eq(rb, r)
  871. end)
  872. it('does not produce empty injection ranges (#23409)', function()
  873. insert [[
  874. Examples: >lua
  875. local a = {}
  876. <
  877. ]]
  878. -- This is not a valid injection since (code) has children and include-children is not set
  879. exec_lua(function()
  880. _G.parser1 = require('vim.treesitter.languagetree').new(0, 'vimdoc', {
  881. injections = {
  882. vimdoc = '((codeblock (language) @injection.language (code) @injection.content))',
  883. },
  884. })
  885. _G.parser1:parse(true)
  886. end)
  887. eq(0, exec_lua('return #vim.tbl_keys(parser1:children())'))
  888. exec_lua(function()
  889. _G.parser2 = require('vim.treesitter.languagetree').new(0, 'vimdoc', {
  890. injections = {
  891. vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))',
  892. },
  893. })
  894. _G.parser2:parse(true)
  895. end)
  896. eq(1, exec_lua('return #vim.tbl_keys(parser2:children())'))
  897. eq({ { { 1, 0, 21, 2, 0, 42 } } }, exec_lua('return parser2:children().lua:included_regions()'))
  898. end)
  899. it('parsers injections incrementally', function()
  900. insert(dedent [[
  901. >lua
  902. local a = {}
  903. <
  904. >lua
  905. local b = {}
  906. <
  907. >lua
  908. local c = {}
  909. <
  910. >lua
  911. local d = {}
  912. <
  913. >lua
  914. local e = {}
  915. <
  916. >lua
  917. local f = {}
  918. <
  919. >lua
  920. local g = {}
  921. <
  922. ]])
  923. exec_lua(function()
  924. _G.parser = require('vim.treesitter.languagetree').new(0, 'vimdoc', {
  925. injections = {
  926. vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))',
  927. },
  928. })
  929. end)
  930. --- Do not parse injections by default
  931. eq(
  932. 0,
  933. exec_lua(function()
  934. _G.parser:parse()
  935. return #vim.tbl_keys(_G.parser:children())
  936. end)
  937. )
  938. --- Only parse injections between lines 0, 2
  939. eq(
  940. 1,
  941. exec_lua(function()
  942. _G.parser:parse({ 0, 2 })
  943. return #_G.parser:children().lua:trees()
  944. end)
  945. )
  946. eq(
  947. 2,
  948. exec_lua(function()
  949. _G.parser:parse({ 2, 6 })
  950. return #_G.parser:children().lua:trees()
  951. end)
  952. )
  953. eq(
  954. 7,
  955. exec_lua(function()
  956. _G.parser:parse(true)
  957. return #_G.parser:children().lua:trees()
  958. end)
  959. )
  960. end)
  961. describe('languagetree is_valid()', function()
  962. before_each(function()
  963. insert(dedent [[
  964. Treesitter integration *treesitter*
  965. Nvim integrates the `tree-sitter` library for incremental parsing of buffers:
  966. https://tree-sitter.github.io/tree-sitter/
  967. ]])
  968. feed(':set ft=help<cr>')
  969. exec_lua(function()
  970. vim.treesitter
  971. .get_parser(0, 'vimdoc', {
  972. injections = {
  973. vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))',
  974. },
  975. })
  976. :parse()
  977. end)
  978. end)
  979. it('is valid excluding, invalid including children initially', function()
  980. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  981. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  982. end)
  983. it('is fully valid after a full parse', function()
  984. exec_lua('vim.treesitter.get_parser():parse(true)')
  985. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  986. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  987. end)
  988. it('is fully valid after a parsing a range on parsed tree', function()
  989. exec_lua('vim.treesitter.get_parser():parse({5, 7})')
  990. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  991. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  992. end)
  993. describe('when adding content with injections', function()
  994. before_each(function()
  995. feed('G')
  996. insert(dedent [[
  997. >lua
  998. local a = {}
  999. <
  1000. ]])
  1001. end)
  1002. it('is fully invalid after changes', function()
  1003. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1004. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1005. end)
  1006. it('is valid excluding, invalid including children after a rangeless parse', function()
  1007. exec_lua('vim.treesitter.get_parser():parse()')
  1008. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1009. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1010. end)
  1011. it(
  1012. 'is fully valid after a range parse that leads to parsing not parsed injections',
  1013. function()
  1014. exec_lua('vim.treesitter.get_parser():parse({5, 7})')
  1015. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1016. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1017. end
  1018. )
  1019. it(
  1020. 'is valid excluding, invalid including children after a range parse that does not lead to parsing not parsed injections',
  1021. function()
  1022. exec_lua('vim.treesitter.get_parser():parse({2, 4})')
  1023. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1024. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1025. end
  1026. )
  1027. end)
  1028. describe('when removing content with injections', function()
  1029. before_each(function()
  1030. feed('G')
  1031. insert(dedent [[
  1032. >lua
  1033. local a = {}
  1034. <
  1035. >lua
  1036. local a = {}
  1037. <
  1038. ]])
  1039. exec_lua('vim.treesitter.get_parser():parse(true)')
  1040. feed('Gd3k')
  1041. end)
  1042. it('is fully invalid after changes', function()
  1043. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1044. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1045. end)
  1046. it('is valid excluding, invalid including children after a rangeless parse', function()
  1047. exec_lua('vim.treesitter.get_parser():parse()')
  1048. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1049. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1050. end)
  1051. it('is fully valid after a range parse that leads to parsing modified child tree', function()
  1052. exec_lua('vim.treesitter.get_parser():parse({5, 7})')
  1053. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1054. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1055. end)
  1056. it(
  1057. 'is valid excluding, invalid including children after a range parse that does not lead to parsing modified child tree',
  1058. function()
  1059. exec_lua('vim.treesitter.get_parser():parse({2, 4})')
  1060. eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
  1061. eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
  1062. end
  1063. )
  1064. end)
  1065. end)
  1066. end)