fold_spec.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local clear = n.clear
  5. local eq = t.eq
  6. local insert = n.insert
  7. local exec_lua = n.exec_lua
  8. local command = n.command
  9. local feed = n.feed
  10. local poke_eventloop = n.poke_eventloop
  11. before_each(clear)
  12. describe('treesitter foldexpr', function()
  13. clear()
  14. before_each(function()
  15. -- open folds to avoid deleting entire folded region
  16. exec_lua([[vim.opt.foldlevel = 9]])
  17. end)
  18. local test_text = [[
  19. void ui_refresh(void)
  20. {
  21. int width = INT_MAX, height = INT_MAX;
  22. bool ext_widgets[kUIExtCount];
  23. for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
  24. ext_widgets[i] = true;
  25. }
  26. bool inclusive = ui_override();
  27. for (size_t i = 0; i < ui_count; i++) {
  28. UI *ui = uis[i];
  29. width = MIN(ui->width, width);
  30. height = MIN(ui->height, height);
  31. foo = BAR(ui->bazaar, bazaar);
  32. for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
  33. ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
  34. }
  35. }
  36. }]]
  37. local function parse(lang)
  38. exec_lua(
  39. ([[vim.treesitter.get_parser(0, %s):parse()]]):format(lang and '"' .. lang .. '"' or 'nil')
  40. )
  41. end
  42. local function get_fold_levels()
  43. return exec_lua(function()
  44. local res = {}
  45. for i = 1, vim.api.nvim_buf_line_count(0) do
  46. res[i] = vim.treesitter.foldexpr(i)
  47. end
  48. return res
  49. end)
  50. end
  51. it('can compute fold levels', function()
  52. insert(test_text)
  53. parse('c')
  54. eq({
  55. [1] = '>1',
  56. [2] = '1',
  57. [3] = '1',
  58. [4] = '1',
  59. [5] = '>2',
  60. [6] = '2',
  61. [7] = '2',
  62. [8] = '1',
  63. [9] = '1',
  64. [10] = '>2',
  65. [11] = '2',
  66. [12] = '2',
  67. [13] = '2',
  68. [14] = '2',
  69. [15] = '>3',
  70. [16] = '3',
  71. [17] = '3',
  72. [18] = '2',
  73. [19] = '1',
  74. }, get_fold_levels())
  75. end)
  76. it('recomputes fold levels after lines are added/removed', function()
  77. insert(test_text)
  78. parse('c')
  79. command('1,2d')
  80. poke_eventloop()
  81. eq({
  82. [1] = '0',
  83. [2] = '0',
  84. [3] = '>1',
  85. [4] = '1',
  86. [5] = '1',
  87. [6] = '0',
  88. [7] = '0',
  89. [8] = '>1',
  90. [9] = '1',
  91. [10] = '1',
  92. [11] = '1',
  93. [12] = '1',
  94. [13] = '>2',
  95. [14] = '2',
  96. [15] = '2',
  97. [16] = '1',
  98. [17] = '0',
  99. }, get_fold_levels())
  100. command('1put!')
  101. poke_eventloop()
  102. eq({
  103. [1] = '>1',
  104. [2] = '1',
  105. [3] = '1',
  106. [4] = '1',
  107. [5] = '>2',
  108. [6] = '2',
  109. [7] = '2',
  110. [8] = '1',
  111. [9] = '1',
  112. [10] = '>2',
  113. [11] = '2',
  114. [12] = '2',
  115. [13] = '2',
  116. [14] = '2',
  117. [15] = '>3',
  118. [16] = '3',
  119. [17] = '3',
  120. [18] = '2',
  121. [19] = '1',
  122. }, get_fold_levels())
  123. end)
  124. it('handles changes close to start/end of folds', function()
  125. insert([[
  126. # h1
  127. t1
  128. # h2
  129. t2]])
  130. exec_lua([[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]])
  131. parse('markdown')
  132. eq({
  133. [1] = '>1',
  134. [2] = '1',
  135. [3] = '>1',
  136. [4] = '1',
  137. }, get_fold_levels())
  138. feed('2ggo<Esc>')
  139. poke_eventloop()
  140. eq({
  141. [1] = '>1',
  142. [2] = '1',
  143. [3] = '1',
  144. [4] = '>1',
  145. [5] = '1',
  146. }, get_fold_levels())
  147. feed('dd')
  148. poke_eventloop()
  149. eq({
  150. [1] = '>1',
  151. [2] = '1',
  152. [3] = '>1',
  153. [4] = '1',
  154. }, get_fold_levels())
  155. feed('2ggdd')
  156. poke_eventloop()
  157. eq({
  158. [1] = '0',
  159. [2] = '>1',
  160. [3] = '1',
  161. }, get_fold_levels())
  162. feed('u')
  163. poke_eventloop()
  164. eq({
  165. [1] = '>1',
  166. [2] = '1',
  167. [3] = '>1',
  168. [4] = '1',
  169. }, get_fold_levels())
  170. feed('3ggdd')
  171. poke_eventloop()
  172. eq({
  173. [1] = '>1',
  174. [2] = '1',
  175. [3] = '1',
  176. }, get_fold_levels())
  177. feed('u')
  178. poke_eventloop()
  179. eq({
  180. [1] = '>1',
  181. [2] = '1',
  182. [3] = '>1',
  183. [4] = '1',
  184. }, get_fold_levels())
  185. feed('3ggI#<Esc>')
  186. parse()
  187. poke_eventloop()
  188. eq({
  189. [1] = '>1',
  190. [2] = '1',
  191. [3] = '>2',
  192. [4] = '2',
  193. }, get_fold_levels())
  194. feed('x')
  195. parse()
  196. poke_eventloop()
  197. eq({
  198. [1] = '>1',
  199. [2] = '1',
  200. [3] = '>1',
  201. [4] = '1',
  202. }, get_fold_levels())
  203. end)
  204. it('handles changes that trigger multiple on_bytes', function()
  205. insert([[
  206. function f()
  207. asdf()
  208. asdf()
  209. end
  210. -- comment]])
  211. exec_lua(function()
  212. vim.treesitter.query.set(
  213. 'lua',
  214. 'folds',
  215. '[(function_declaration) (parameters) (arguments)] @fold'
  216. )
  217. end)
  218. parse('lua')
  219. eq({
  220. [1] = '>1',
  221. [2] = '1',
  222. [3] = '1',
  223. [4] = '1',
  224. [5] = '0',
  225. }, get_fold_levels())
  226. command('1,4join')
  227. poke_eventloop()
  228. eq({
  229. [1] = '0',
  230. [2] = '0',
  231. }, get_fold_levels())
  232. feed('u')
  233. poke_eventloop()
  234. eq({
  235. [1] = '>1',
  236. [2] = '1',
  237. [3] = '1',
  238. [4] = '1',
  239. [5] = '0',
  240. }, get_fold_levels())
  241. end)
  242. it('handles multiple folds that overlap at the end and start', function()
  243. insert([[
  244. function f()
  245. g(
  246. function()
  247. asdf()
  248. end, function()
  249. end
  250. )
  251. end]])
  252. exec_lua(function()
  253. vim.treesitter.query.set(
  254. 'lua',
  255. 'folds',
  256. '[(function_declaration) (function_definition) (parameters) (arguments)] @fold'
  257. )
  258. end)
  259. parse('lua')
  260. -- If fold1.stop = fold2.start, then move fold1's stop up so that fold2.start gets proper level.
  261. eq({
  262. [1] = '>1',
  263. [2] = '>2',
  264. [3] = '>3',
  265. [4] = '3',
  266. [5] = '>3',
  267. [6] = '3',
  268. [7] = '2',
  269. [8] = '1',
  270. }, get_fold_levels())
  271. command('1,8join')
  272. feed('u')
  273. poke_eventloop()
  274. eq({
  275. [1] = '>1',
  276. [2] = '>2',
  277. [3] = '>3',
  278. [4] = '3',
  279. [5] = '>3',
  280. [6] = '3',
  281. [7] = '2',
  282. [8] = '1',
  283. }, get_fold_levels())
  284. end)
  285. it('handles multiple folds that start at the same line', function()
  286. insert([[
  287. function f(a)
  288. if #(g({
  289. k = v,
  290. })) > 0 then
  291. return
  292. end
  293. end]])
  294. exec_lua(function()
  295. vim.treesitter.query.set(
  296. 'lua',
  297. 'folds',
  298. '[(if_statement) (function_declaration) (parameters) (arguments) (table_constructor)] @fold'
  299. )
  300. end)
  301. parse('lua')
  302. eq({
  303. [1] = '>1',
  304. [2] = '>3',
  305. [3] = '3',
  306. [4] = '3',
  307. [5] = '2',
  308. [6] = '2',
  309. [7] = '1',
  310. }, get_fold_levels())
  311. command('2,6join')
  312. poke_eventloop()
  313. eq({
  314. [1] = '>1',
  315. [2] = '1',
  316. [3] = '1',
  317. }, get_fold_levels())
  318. feed('u')
  319. poke_eventloop()
  320. eq({
  321. [1] = '>1',
  322. [2] = '>3',
  323. [3] = '3',
  324. [4] = '3',
  325. [5] = '2',
  326. [6] = '2',
  327. [7] = '1',
  328. }, get_fold_levels())
  329. end)
  330. it('takes account of relevant options', function()
  331. insert([[
  332. # h1
  333. t1
  334. ## h2
  335. t2
  336. ### h3
  337. t3]])
  338. exec_lua([[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]])
  339. parse('markdown')
  340. command([[set foldminlines=2]])
  341. eq({
  342. [1] = '>1',
  343. [2] = '1',
  344. [3] = '>2',
  345. [4] = '2',
  346. [5] = '2',
  347. [6] = '2',
  348. }, get_fold_levels())
  349. command([[set foldminlines=1 foldnestmax=1]])
  350. eq({
  351. [1] = '>1',
  352. [2] = '1',
  353. [3] = '1',
  354. [4] = '1',
  355. [5] = '1',
  356. [6] = '1',
  357. }, get_fold_levels())
  358. end)
  359. it('handles quantified patterns', function()
  360. insert([[
  361. -- hello
  362. -- hello
  363. -- hello
  364. -- hello
  365. -- hello
  366. -- hello]])
  367. exec_lua([[vim.treesitter.query.set('lua', 'folds', '(comment)+ @fold')]])
  368. parse('lua')
  369. eq({
  370. [1] = '>1',
  371. [2] = '1',
  372. [3] = '1',
  373. [4] = '1',
  374. [5] = '1',
  375. [6] = '1',
  376. }, get_fold_levels())
  377. end)
  378. it('updates folds in all windows', function()
  379. local screen = Screen.new(60, 48)
  380. screen:set_default_attr_ids({
  381. [1] = { background = Screen.colors.Grey, foreground = Screen.colors.DarkBlue },
  382. [2] = { bold = true, foreground = Screen.colors.Blue1 },
  383. [3] = { bold = true, reverse = true },
  384. [4] = { reverse = true },
  385. })
  386. parse('c')
  387. command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]])
  388. command('split')
  389. insert(test_text)
  390. screen:expect {
  391. grid = [[
  392. {1:-}void ui_refresh(void) |
  393. {1:│}{ |
  394. {1:│} int width = INT_MAX, height = INT_MAX; |
  395. {1:│} bool ext_widgets[kUIExtCount]; |
  396. {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  397. {1:2} ext_widgets[i] = true; |
  398. {1:2} } |
  399. {1:│} |
  400. {1:│} bool inclusive = ui_override(); |
  401. {1:-} for (size_t i = 0; i < ui_count; i++) { |
  402. {1:2} UI *ui = uis[i]; |
  403. {1:2} width = MIN(ui->width, width); |
  404. {1:2} height = MIN(ui->height, height); |
  405. {1:2} foo = BAR(ui->bazaar, bazaar); |
  406. {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  407. {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  408. {1:3} } |
  409. {1:2} } |
  410. {1:│}^} |
  411. {2:~ }|*4
  412. {3:[No Name] [+] }|
  413. {1:-}void ui_refresh(void) |
  414. {1:│}{ |
  415. {1:│} int width = INT_MAX, height = INT_MAX; |
  416. {1:│} bool ext_widgets[kUIExtCount]; |
  417. {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  418. {1:2} ext_widgets[i] = true; |
  419. {1:2} } |
  420. {1:│} |
  421. {1:│} bool inclusive = ui_override(); |
  422. {1:-} for (size_t i = 0; i < ui_count; i++) { |
  423. {1:2} UI *ui = uis[i]; |
  424. {1:2} width = MIN(ui->width, width); |
  425. {1:2} height = MIN(ui->height, height); |
  426. {1:2} foo = BAR(ui->bazaar, bazaar); |
  427. {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  428. {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  429. {1:3} } |
  430. {1:2} } |
  431. {1:│}} |
  432. {2:~ }|*3
  433. {4:[No Name] [+] }|
  434. |
  435. ]],
  436. }
  437. command('1,2d')
  438. screen:expect {
  439. grid = [[
  440. {1: } ^int width = INT_MAX, height = INT_MAX; |
  441. {1: } bool ext_widgets[kUIExtCount]; |
  442. {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  443. {1:│} ext_widgets[i] = true; |
  444. {1:│} } |
  445. {1: } |
  446. {1: } bool inclusive = ui_override(); |
  447. {1:-} for (size_t i = 0; i < ui_count; i++) { |
  448. {1:│} UI *ui = uis[i]; |
  449. {1:│} width = MIN(ui->width, width); |
  450. {1:│} height = MIN(ui->height, height); |
  451. {1:│} foo = BAR(ui->bazaar, bazaar); |
  452. {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  453. {1:2} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  454. {1:2} } |
  455. {1:│} } |
  456. {1: }} |
  457. {2:~ }|*6
  458. {3:[No Name] [+] }|
  459. {1: } int width = INT_MAX, height = INT_MAX; |
  460. {1: } bool ext_widgets[kUIExtCount]; |
  461. {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  462. {1:│} ext_widgets[i] = true; |
  463. {1:│} } |
  464. {1: } |
  465. {1: } bool inclusive = ui_override(); |
  466. {1:-} for (size_t i = 0; i < ui_count; i++) { |
  467. {1:│} UI *ui = uis[i]; |
  468. {1:│} width = MIN(ui->width, width); |
  469. {1:│} height = MIN(ui->height, height); |
  470. {1:│} foo = BAR(ui->bazaar, bazaar); |
  471. {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  472. {1:2} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  473. {1:2} } |
  474. {1:│} } |
  475. {1: }} |
  476. {2:~ }|*5
  477. {4:[No Name] [+] }|
  478. |
  479. ]],
  480. }
  481. feed([[O<C-u><C-r>"<BS><Esc>]])
  482. screen:expect {
  483. grid = [[
  484. {1:-}void ui_refresh(void) |
  485. {1:│}^{ |
  486. {1:│} int width = INT_MAX, height = INT_MAX; |
  487. {1:│} bool ext_widgets[kUIExtCount]; |
  488. {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  489. {1:2} ext_widgets[i] = true; |
  490. {1:2} } |
  491. {1:│} |
  492. {1:│} bool inclusive = ui_override(); |
  493. {1:-} for (size_t i = 0; i < ui_count; i++) { |
  494. {1:2} UI *ui = uis[i]; |
  495. {1:2} width = MIN(ui->width, width); |
  496. {1:2} height = MIN(ui->height, height); |
  497. {1:2} foo = BAR(ui->bazaar, bazaar); |
  498. {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  499. {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  500. {1:3} } |
  501. {1:2} } |
  502. {1:│}} |
  503. {2:~ }|*4
  504. {3:[No Name] [+] }|
  505. {1:-}void ui_refresh(void) |
  506. {1:│}{ |
  507. {1:│} int width = INT_MAX, height = INT_MAX; |
  508. {1:│} bool ext_widgets[kUIExtCount]; |
  509. {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  510. {1:2} ext_widgets[i] = true; |
  511. {1:2} } |
  512. {1:│} |
  513. {1:│} bool inclusive = ui_override(); |
  514. {1:-} for (size_t i = 0; i < ui_count; i++) { |
  515. {1:2} UI *ui = uis[i]; |
  516. {1:2} width = MIN(ui->width, width); |
  517. {1:2} height = MIN(ui->height, height); |
  518. {1:2} foo = BAR(ui->bazaar, bazaar); |
  519. {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  520. {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  521. {1:3} } |
  522. {1:2} } |
  523. {1:│}} |
  524. {2:~ }|*3
  525. {4:[No Name] [+] }|
  526. |
  527. ]],
  528. }
  529. end)
  530. it("doesn't open folds in diff mode", function()
  531. local screen = Screen.new(60, 36)
  532. parse('c')
  533. command(
  534. [[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=9]]
  535. )
  536. insert(test_text)
  537. command('16d')
  538. command('new')
  539. insert(test_text)
  540. command('windo diffthis')
  541. feed('do')
  542. screen:expect {
  543. grid = [[
  544. {1:+ }{2:+-- 9 lines: void ui_refresh(void)·······················}|
  545. {1: } for (size_t i = 0; i < ui_count; i++) { |
  546. {1: } UI *ui = uis[i]; |
  547. {1: } width = MIN(ui->width, width); |
  548. {1: } height = MIN(ui->height, height); |
  549. {1: } foo = BAR(ui->bazaar, bazaar); |
  550. {1: } for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  551. {1: } ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  552. {1: } } |
  553. {1: } } |
  554. {1: }} |
  555. {3:~ }|*6
  556. {4:[No Name] [+] }|
  557. {1:+ }{2:+-- 9 lines: void ui_refresh(void)·······················}|
  558. {1: } for (size_t i = 0; i < ui_count; i++) { |
  559. {1: } UI *ui = uis[i]; |
  560. {1: } width = MIN(ui->width, width); |
  561. {1: } height = MIN(ui->height, height); |
  562. {1: } foo = BAR(ui->bazaar, bazaar); |
  563. {1: } for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  564. {1: } ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  565. {1: } ^} |
  566. {1: } } |
  567. {1: }} |
  568. {3:~ }|*5
  569. {5:[No Name] [+] }|
  570. |
  571. ]],
  572. attr_ids = {
  573. [1] = { background = Screen.colors.Grey, foreground = Screen.colors.Blue4 },
  574. [2] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Blue4 },
  575. [3] = { foreground = Screen.colors.Blue, bold = true },
  576. [4] = { reverse = true },
  577. [5] = { reverse = true, bold = true },
  578. },
  579. }
  580. end)
  581. it('does not extend closed fold with `o`/`O`', function()
  582. local screen = Screen.new(60, 24)
  583. insert(test_text)
  584. parse('c')
  585. command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]])
  586. feed('5ggzco')
  587. screen:expect({
  588. grid = [[
  589. {7:-}void ui_refresh(void) |
  590. {7:│}{ |
  591. {7:│} int width = INT_MAX, height = INT_MAX; |
  592. {7:│} bool ext_widgets[kUIExtCount]; |
  593. {7:+}{13:+--- 3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}|
  594. {7:│}^ |
  595. {7:│} |
  596. {7:│} bool inclusive = ui_override(); |
  597. {7:-} for (size_t i = 0; i < ui_count; i++) { |
  598. {7:2} UI *ui = uis[i]; |
  599. {7:2} width = MIN(ui->width, width); |
  600. {7:2} height = MIN(ui->height, height); |
  601. {7:2} foo = BAR(ui->bazaar, bazaar); |
  602. {7:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  603. {7:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  604. {7:3} } |
  605. {7:2} } |
  606. {7:│}} |
  607. {1:~ }|*5
  608. {5:-- INSERT --} |
  609. ]],
  610. })
  611. feed('<Esc>O')
  612. screen:expect({
  613. grid = [[
  614. {7:-}void ui_refresh(void) |
  615. {7:│}{ |
  616. {7:│} int width = INT_MAX, height = INT_MAX; |
  617. {7:│} bool ext_widgets[kUIExtCount]; |
  618. {7:+}{13:+--- 3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}|
  619. {7:│}^ |
  620. {7:│} |*2
  621. {7:│} bool inclusive = ui_override(); |
  622. {7:-} for (size_t i = 0; i < ui_count; i++) { |
  623. {7:2} UI *ui = uis[i]; |
  624. {7:2} width = MIN(ui->width, width); |
  625. {7:2} height = MIN(ui->height, height); |
  626. {7:2} foo = BAR(ui->bazaar, bazaar); |
  627. {7:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  628. {7:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  629. {7:3} } |
  630. {7:2} } |
  631. {7:│}} |
  632. {1:~ }|*4
  633. {5:-- INSERT --} |
  634. ]],
  635. })
  636. end)
  637. it("doesn't open folds that are not touched", function()
  638. local screen = Screen.new(40, 8)
  639. screen:set_default_attr_ids({
  640. [1] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.Gray },
  641. [2] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.LightGray },
  642. [3] = { foreground = Screen.colors.Blue1, bold = true },
  643. [4] = { bold = true },
  644. })
  645. insert([[
  646. # h1
  647. t1
  648. # h2
  649. t2]])
  650. exec_lua([[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]])
  651. parse('markdown')
  652. command(
  653. [[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0]]
  654. )
  655. feed('ggzojo')
  656. poke_eventloop()
  657. screen:expect {
  658. grid = [[
  659. {1:-}# h1 |
  660. {1:│}t1 |
  661. {1:-}^ |
  662. {1:+}{2:+-- 2 lines: # h2·····················}|
  663. {3:~ }|*3
  664. {4:-- INSERT --} |
  665. ]],
  666. }
  667. feed('<Esc>u')
  668. -- TODO(tomtomjhj): `u` spuriously opens the fold (#26499).
  669. feed('zMggzo')
  670. feed('dd')
  671. poke_eventloop()
  672. screen:expect {
  673. grid = [[
  674. {1:-}^t1 |
  675. {1:-}# h2 |
  676. {1:│}t2 |
  677. {3:~ }|*4
  678. 1 line less; before #2 {MATCH:.*}|
  679. ]],
  680. }
  681. end)
  682. end)