robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Milad Rashidikhahfix(treesitter): clamp unbounded fold ranges20ff82d9fc

master
26.5 KiB865 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local eq = t.eq
8local insert = n.insert
9local write_file = t.write_file
10local exec_lua = n.exec_lua
11local command = n.command
12local feed = n.feed
13local poke_eventloop = n.poke_eventloop
14
15before_each(clear)
16
17describe('treesitter foldexpr', function()
18  before_each(function()
19    -- open folds to avoid deleting entire folded region
20    exec_lua([[vim.opt.foldlevel = 9]])
21  end)
22
23  local test_text = [[
24void ui_refresh(void)
25{
26  int width = INT_MAX, height = INT_MAX;
27  bool ext_widgets[kUIExtCount];
28  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
29    ext_widgets[i] = true;
30  }
31
32  bool inclusive = ui_override();
33  for (size_t i = 0; i < ui_count; i++) {
34    UI *ui = uis[i];
35    width = MIN(ui->width, width);
36    height = MIN(ui->height, height);
37    foo = BAR(ui->bazaar, bazaar);
38    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
39      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
40    }
41  }
42}]]
43
44  local function parse(lang)
45    exec_lua(
46      ([[vim.treesitter.get_parser(0, %s):parse()]]):format(lang and '"' .. lang .. '"' or 'nil')
47    )
48  end
49
50  local function get_fold_levels()
51    return exec_lua(function()
52      local res = {}
53      for i = 1, vim.api.nvim_buf_line_count(0) do
54        res[i] = vim.treesitter.foldexpr(i)
55      end
56      return res
57    end)
58  end
59
60  it('can compute fold levels', function()
61    insert(test_text)
62
63    parse('c')
64
65    eq({
66      [1] = '>1',
67      [2] = '1',
68      [3] = '1',
69      [4] = '1',
70      [5] = '>2',
71      [6] = '2',
72      [7] = '2',
73      [8] = '1',
74      [9] = '1',
75      [10] = '>2',
76      [11] = '2',
77      [12] = '2',
78      [13] = '2',
79      [14] = '2',
80      [15] = '>3',
81      [16] = '3',
82      [17] = '3',
83      [18] = '2',
84      [19] = '1',
85    }, get_fold_levels())
86  end)
87
88  it('recomputes fold levels after lines are added/removed', function()
89    insert(test_text)
90
91    parse('c')
92
93    command('1,2d')
94    poke_eventloop()
95
96    eq({
97      [1] = '0',
98      [2] = '0',
99      [3] = '>1',
100      [4] = '1',
101      [5] = '1',
102      [6] = '0',
103      [7] = '0',
104      [8] = '>1',
105      [9] = '1',
106      [10] = '1',
107      [11] = '1',
108      [12] = '1',
109      [13] = '>2',
110      [14] = '2',
111      [15] = '2',
112      [16] = '1',
113      [17] = '0',
114    }, get_fold_levels())
115
116    command('1put!')
117    poke_eventloop()
118
119    eq({
120      [1] = '>1',
121      [2] = '1',
122      [3] = '1',
123      [4] = '1',
124      [5] = '>2',
125      [6] = '2',
126      [7] = '2',
127      [8] = '1',
128      [9] = '1',
129      [10] = '>2',
130      [11] = '2',
131      [12] = '2',
132      [13] = '2',
133      [14] = '2',
134      [15] = '>3',
135      [16] = '3',
136      [17] = '3',
137      [18] = '2',
138      [19] = '1',
139    }, get_fold_levels())
140  end)
141
142  it('handles changes close to start/end of folds', function()
143    insert([[
144# h1
145t1
146# h2
147t2]])
148
149    exec_lua([[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]])
150    parse('markdown')
151
152    eq({
153      [1] = '>1',
154      [2] = '1',
155      [3] = '>1',
156      [4] = '1',
157    }, get_fold_levels())
158
159    feed('2ggo<Esc>')
160    poke_eventloop()
161
162    eq({
163      [1] = '>1',
164      [2] = '1',
165      [3] = '1',
166      [4] = '>1',
167      [5] = '1',
168    }, get_fold_levels())
169
170    feed('dd')
171    poke_eventloop()
172
173    eq({
174      [1] = '>1',
175      [2] = '1',
176      [3] = '>1',
177      [4] = '1',
178    }, get_fold_levels())
179
180    feed('2ggdd')
181    poke_eventloop()
182
183    eq({
184      [1] = '0',
185      [2] = '>1',
186      [3] = '1',
187    }, get_fold_levels())
188
189    feed('u')
190    poke_eventloop()
191
192    eq({
193      [1] = '>1',
194      [2] = '1',
195      [3] = '>1',
196      [4] = '1',
197    }, get_fold_levels())
198
199    feed('3ggdd')
200    poke_eventloop()
201
202    eq({
203      [1] = '>1',
204      [2] = '1',
205      [3] = '1',
206    }, get_fold_levels())
207
208    feed('u')
209    poke_eventloop()
210
211    eq({
212      [1] = '>1',
213      [2] = '1',
214      [3] = '>1',
215      [4] = '1',
216    }, get_fold_levels())
217
218    feed('3ggI#<Esc>')
219    parse()
220    poke_eventloop()
221
222    eq({
223      [1] = '>1',
224      [2] = '1',
225      [3] = '>2',
226      [4] = '2',
227    }, get_fold_levels())
228
229    feed('x')
230    parse()
231    poke_eventloop()
232
233    eq({
234      [1] = '>1',
235      [2] = '1',
236      [3] = '>1',
237      [4] = '1',
238    }, get_fold_levels())
239  end)
240
241  it('handles changes that trigger multiple on_bytes', function()
242    insert([[
243function f()
244  asdf()
245  asdf()
246end
247-- comment]])
248
249    exec_lua(function()
250      vim.treesitter.query.set(
251        'lua',
252        'folds',
253        '[(function_declaration) (parameters) (arguments)] @fold'
254      )
255    end)
256    parse('lua')
257
258    eq({
259      [1] = '>1',
260      [2] = '1',
261      [3] = '1',
262      [4] = '1',
263      [5] = '0',
264    }, get_fold_levels())
265
266    command('1,4join')
267    poke_eventloop()
268
269    eq({
270      [1] = '0',
271      [2] = '0',
272    }, get_fold_levels())
273
274    feed('u')
275    poke_eventloop()
276
277    eq({
278      [1] = '>1',
279      [2] = '1',
280      [3] = '1',
281      [4] = '1',
282      [5] = '0',
283    }, get_fold_levels())
284  end)
285
286  it('handles multiple folds that overlap at the end and start', function()
287    insert([[
288function f()
289  g(
290    function()
291      asdf()
292    end, function()
293    end
294  )
295end]])
296
297    exec_lua(function()
298      vim.treesitter.query.set(
299        'lua',
300        'folds',
301        '[(function_declaration) (function_definition) (parameters) (arguments)] @fold'
302      )
303    end)
304    parse('lua')
305
306    -- If fold1.stop = fold2.start, then move fold1's stop up so that fold2.start gets proper level.
307    eq({
308      [1] = '>1',
309      [2] = '>2',
310      [3] = '>3',
311      [4] = '3',
312      [5] = '>3',
313      [6] = '3',
314      [7] = '2',
315      [8] = '1',
316    }, get_fold_levels())
317
318    command('1,8join')
319    feed('u')
320    poke_eventloop()
321
322    eq({
323      [1] = '>1',
324      [2] = '>2',
325      [3] = '>3',
326      [4] = '3',
327      [5] = '>3',
328      [6] = '3',
329      [7] = '2',
330      [8] = '1',
331    }, get_fold_levels())
332  end)
333
334  it('handles multiple folds that start at the same line', function()
335    insert([[
336function f(a)
337  if #(g({
338    k = v,
339  })) > 0 then
340    return
341  end
342end]])
343
344    exec_lua(function()
345      vim.treesitter.query.set(
346        'lua',
347        'folds',
348        '[(if_statement) (function_declaration) (parameters) (arguments) (table_constructor)] @fold'
349      )
350    end)
351    parse('lua')
352
353    eq({
354      [1] = '>1',
355      [2] = '>3',
356      [3] = '3',
357      [4] = '3',
358      [5] = '2',
359      [6] = '2',
360      [7] = '1',
361    }, get_fold_levels())
362
363    command('2,6join')
364    poke_eventloop()
365
366    eq({
367      [1] = '>1',
368      [2] = '1',
369      [3] = '1',
370    }, get_fold_levels())
371
372    feed('u')
373    poke_eventloop()
374
375    eq({
376      [1] = '>1',
377      [2] = '>3',
378      [3] = '3',
379      [4] = '3',
380      [5] = '2',
381      [6] = '2',
382      [7] = '1',
383    }, get_fold_levels())
384  end)
385
386  it('takes account of relevant options', function()
387    insert([[
388# h1
389t1
390## h2
391t2
392### h3
393t3]])
394
395    exec_lua([[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]])
396    parse('markdown')
397
398    command([[set foldminlines=2]])
399
400    eq({
401      [1] = '>1',
402      [2] = '1',
403      [3] = '>2',
404      [4] = '2',
405      [5] = '2',
406      [6] = '2',
407    }, get_fold_levels())
408
409    command([[set foldminlines=1 foldnestmax=1]])
410
411    eq({
412      [1] = '>1',
413      [2] = '1',
414      [3] = '1',
415      [4] = '1',
416      [5] = '1',
417      [6] = '1',
418    }, get_fold_levels())
419  end)
420
421  it('handles quantified patterns', function()
422    insert([[
423-- hello
424-- hello
425-- hello
426-- hello
427-- hello
428-- hello]])
429
430    exec_lua([[vim.treesitter.query.set('lua', 'folds', '(comment)+ @fold')]])
431    parse('lua')
432
433    eq({
434      [1] = '>1',
435      [2] = '1',
436      [3] = '1',
437      [4] = '1',
438      [5] = '1',
439      [6] = '1',
440    }, get_fold_levels())
441  end)
442
443  it('updates folds in all windows', function()
444    local screen = Screen.new(60, 48)
445    screen:set_default_attr_ids({
446      [1] = { background = Screen.colors.Grey, foreground = Screen.colors.DarkBlue },
447      [2] = { bold = true, foreground = Screen.colors.Blue1 },
448      [3] = { bold = true, reverse = true },
449      [4] = { reverse = true },
450    })
451
452    parse('c')
453    command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]])
454    command('split')
455
456    insert(test_text)
457
458    screen:expect {
459      grid = [[
460      {1:-}void ui_refresh(void)                                      |
461      {1:│}{                                                          |
462      {1:│}  int width = INT_MAX, height = INT_MAX;                   |
463      {1:│}  bool ext_widgets[kUIExtCount];                           |
464      {1:-}  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {     |
465      {1:2}    ext_widgets[i] = true;                                 |
466      {1:2}  }                                                        |
467      {1:│}                                                           |
468      {1:│}  bool inclusive = ui_override();                          |
469      {1:-}  for (size_t i = 0; i < ui_count; i++) {                  |
470      {1:2}    UI *ui = uis[i];                                       |
471      {1:2}    width = MIN(ui->width, width);                         |
472      {1:2}    height = MIN(ui->height, height);                      |
473      {1:2}    foo = BAR(ui->bazaar, bazaar);                         |
474      {1:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
475      {1:3}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
476      {1:3}    }                                                      |
477      {1:2}  }                                                        |
478      {1:│}^}                                                          |
479      {2:~                                                           }|*4
480      {3:[No Name] [+]                                               }|
481      {1:-}void ui_refresh(void)                                      |
482      {1:│}{                                                          |
483      {1:│}  int width = INT_MAX, height = INT_MAX;                   |
484      {1:│}  bool ext_widgets[kUIExtCount];                           |
485      {1:-}  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {     |
486      {1:2}    ext_widgets[i] = true;                                 |
487      {1:2}  }                                                        |
488      {1:│}                                                           |
489      {1:│}  bool inclusive = ui_override();                          |
490      {1:-}  for (size_t i = 0; i < ui_count; i++) {                  |
491      {1:2}    UI *ui = uis[i];                                       |
492      {1:2}    width = MIN(ui->width, width);                         |
493      {1:2}    height = MIN(ui->height, height);                      |
494      {1:2}    foo = BAR(ui->bazaar, bazaar);                         |
495      {1:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
496      {1:3}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
497      {1:3}    }                                                      |
498      {1:2}  }                                                        |
499      {1:│}}                                                          |
500      {2:~                                                           }|*3
501      {4:[No Name] [+]                                               }|
502                                                                  |
503    ]],
504    }
505
506    command('1,2d')
507
508    screen:expect {
509      grid = [[
510      {1: }  ^int width = INT_MAX, height = INT_MAX;                   |
511      {1: }  bool ext_widgets[kUIExtCount];                           |
512      {1:-}  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {     |
513      {1:│}    ext_widgets[i] = true;                                 |
514      {1:│}  }                                                        |
515      {1: }                                                           |
516      {1: }  bool inclusive = ui_override();                          |
517      {1:-}  for (size_t i = 0; i < ui_count; i++) {                  |
518      {1:│}    UI *ui = uis[i];                                       |
519      {1:│}    width = MIN(ui->width, width);                         |
520      {1:│}    height = MIN(ui->height, height);                      |
521      {1:│}    foo = BAR(ui->bazaar, bazaar);                         |
522      {1:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
523      {1:2}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
524      {1:2}    }                                                      |
525      {1:│}  }                                                        |
526      {1: }}                                                          |
527      {2:~                                                           }|*6
528      {3:[No Name] [+]                                               }|
529      {1: }  int width = INT_MAX, height = INT_MAX;                   |
530      {1: }  bool ext_widgets[kUIExtCount];                           |
531      {1:-}  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {     |
532      {1:│}    ext_widgets[i] = true;                                 |
533      {1:│}  }                                                        |
534      {1: }                                                           |
535      {1: }  bool inclusive = ui_override();                          |
536      {1:-}  for (size_t i = 0; i < ui_count; i++) {                  |
537      {1:│}    UI *ui = uis[i];                                       |
538      {1:│}    width = MIN(ui->width, width);                         |
539      {1:│}    height = MIN(ui->height, height);                      |
540      {1:│}    foo = BAR(ui->bazaar, bazaar);                         |
541      {1:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
542      {1:2}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
543      {1:2}    }                                                      |
544      {1:│}  }                                                        |
545      {1: }}                                                          |
546      {2:~                                                           }|*5
547      {4:[No Name] [+]                                               }|
548                                                                  |
549    ]],
550    }
551
552    feed([[O<C-u><C-r>"<BS><Esc>]])
553
554    screen:expect {
555      grid = [[
556      {1:-}void ui_refresh(void)                                      |
557      {1:│}^{                                                          |
558      {1:│}  int width = INT_MAX, height = INT_MAX;                   |
559      {1:│}  bool ext_widgets[kUIExtCount];                           |
560      {1:-}  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {     |
561      {1:2}    ext_widgets[i] = true;                                 |
562      {1:2}  }                                                        |
563      {1:│}                                                           |
564      {1:│}  bool inclusive = ui_override();                          |
565      {1:-}  for (size_t i = 0; i < ui_count; i++) {                  |
566      {1:2}    UI *ui = uis[i];                                       |
567      {1:2}    width = MIN(ui->width, width);                         |
568      {1:2}    height = MIN(ui->height, height);                      |
569      {1:2}    foo = BAR(ui->bazaar, bazaar);                         |
570      {1:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
571      {1:3}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
572      {1:3}    }                                                      |
573      {1:2}  }                                                        |
574      {1:│}}                                                          |
575      {2:~                                                           }|*4
576      {3:[No Name] [+]                                               }|
577      {1:-}void ui_refresh(void)                                      |
578      {1:│}{                                                          |
579      {1:│}  int width = INT_MAX, height = INT_MAX;                   |
580      {1:│}  bool ext_widgets[kUIExtCount];                           |
581      {1:-}  for (UIExtension i = 0; (int)i < kUIExtCount; i++) {     |
582      {1:2}    ext_widgets[i] = true;                                 |
583      {1:2}  }                                                        |
584      {1:│}                                                           |
585      {1:│}  bool inclusive = ui_override();                          |
586      {1:-}  for (size_t i = 0; i < ui_count; i++) {                  |
587      {1:2}    UI *ui = uis[i];                                       |
588      {1:2}    width = MIN(ui->width, width);                         |
589      {1:2}    height = MIN(ui->height, height);                      |
590      {1:2}    foo = BAR(ui->bazaar, bazaar);                         |
591      {1:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
592      {1:3}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
593      {1:3}    }                                                      |
594      {1:2}  }                                                        |
595      {1:│}}                                                          |
596      {2:~                                                           }|*3
597      {4:[No Name] [+]                                               }|
598                                                                  |
599    ]],
600    }
601  end)
602
603  it("doesn't open folds in diff mode", function()
604    local screen = Screen.new(60, 36)
605
606    parse('c')
607    command(
608      [[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=9]]
609    )
610    insert(test_text)
611    command('16d')
612
613    command('new')
614    insert(test_text)
615
616    command('windo diffthis')
617    feed('do')
618
619    screen:expect([[
620      {7:+ }{13:+-- 19 lines: void ui_refresh(void)·······················}|
621      {1:~                                                           }|*16
622      {2:[No Name] [+]                                               }|
623      {7:+ }{13:^+-- 19 lines: void ui_refresh(void)·······················}|
624      {1:~                                                           }|*15
625      {3:[No Name] [+]                                               }|
626                                                                  |
627    ]])
628  end)
629
630  it('does not extend closed fold with `o`/`O`', function()
631    local screen = Screen.new(60, 24)
632
633    insert(test_text)
634    parse('c')
635    command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]])
636
637    feed('5ggzco')
638    screen:expect({
639      grid = [[
640        {7:-}void ui_refresh(void)                                      |
641        {7:│}{                                                          |
642        {7:│}  int width = INT_MAX, height = INT_MAX;                   |
643        {7:│}  bool ext_widgets[kUIExtCount];                           |
644        {7:+}{13:+---  3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}|
645        {7:│}^                                                           |
646        {7:│}                                                           |
647        {7:│}  bool inclusive = ui_override();                          |
648        {7:-}  for (size_t i = 0; i < ui_count; i++) {                  |
649        {7:2}    UI *ui = uis[i];                                       |
650        {7:2}    width = MIN(ui->width, width);                         |
651        {7:2}    height = MIN(ui->height, height);                      |
652        {7:2}    foo = BAR(ui->bazaar, bazaar);                         |
653        {7:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
654        {7:3}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
655        {7:3}    }                                                      |
656        {7:2}  }                                                        |
657        {7:│}}                                                          |
658        {1:~                                                           }|*5
659        {5:-- INSERT --}                                                |
660      ]],
661    })
662
663    feed('<Esc>O')
664    screen:expect({
665      grid = [[
666        {7:-}void ui_refresh(void)                                      |
667        {7:│}{                                                          |
668        {7:│}  int width = INT_MAX, height = INT_MAX;                   |
669        {7:│}  bool ext_widgets[kUIExtCount];                           |
670        {7:+}{13:+---  3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}|
671        {7:│}^                                                           |
672        {7:│}                                                           |*2
673        {7:│}  bool inclusive = ui_override();                          |
674        {7:-}  for (size_t i = 0; i < ui_count; i++) {                  |
675        {7:2}    UI *ui = uis[i];                                       |
676        {7:2}    width = MIN(ui->width, width);                         |
677        {7:2}    height = MIN(ui->height, height);                      |
678        {7:2}    foo = BAR(ui->bazaar, bazaar);                         |
679        {7:-}    for (UIExtension j = 0; (int)j < kUIExtCount; j++) {   |
680        {7:3}      ext_widgets[j] &= (ui->ui_ext[j] || inclusive);      |
681        {7:3}    }                                                      |
682        {7:2}  }                                                        |
683        {7:│}}                                                          |
684        {1:~                                                           }|*4
685        {5:-- INSERT --}                                                |
686      ]],
687    })
688  end)
689
690  it("doesn't open folds that are not touched", function()
691    local screen = Screen.new(40, 8)
692    screen:set_default_attr_ids({
693      [1] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.Gray },
694      [2] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.LightGray },
695      [3] = { foreground = Screen.colors.Blue1, bold = true },
696      [4] = { bold = true },
697    })
698
699    insert([[
700# h1
701t1
702# h2
703t2]])
704    exec_lua([[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]])
705    parse('markdown')
706    command(
707      [[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0]]
708    )
709
710    feed('ggzojo')
711    screen:expect([[
712      {1:-}# h1                                   |
713      {1:│}t1                                     |
714      {1:-}^                                       |
715      {1:+}{2:+--  2 lines: # h2·····················}|
716      {3:~                                       }|*3
717      {4:-- INSERT --}                            |
718    ]])
719
720    -- TODO(tomtomjhj): `u` spuriously opens the fold (#26499).
721    feed('<Esc>uzMggzodd')
722    screen:expect([[
723      {1:-}^t1                                     |
724      {1:-}# h2                                   |
725      {1:│}t2                                     |
726      {3:~                                       }|*4
727      1 line less; before #2  {MATCH:.*}|
728    ]])
729  end)
730
731  it("doesn't call get_parser too often when parser is not available", function()
732    -- spy on vim.treesitter.get_parser() to keep track of how many times it is called
733    exec_lua(function()
734      _G.count = 0
735      vim.treesitter.get_parser = (function(wrapped)
736        return function(...)
737          _G.count = _G.count + 1
738          return wrapped(...)
739        end
740      end)(vim.treesitter.get_parser)
741    end)
742
743    insert(test_text)
744    command [[
745      set filetype=some_filetype_without_treesitter_parser
746      set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0
747    ]]
748
749    -- foldexpr will return '0' for all lines
750    local levels = get_fold_levels() ---@type integer[]
751    eq(19, #levels)
752    for lnum, level in ipairs(levels) do
753      eq('0', level, string.format("foldlevel[%d] == %s; expected '0'", lnum, level))
754    end
755
756    eq(
757      1,
758      exec_lua [[ return _G.count ]],
759      'count should not be as high as the # of lines; actually only once for the buffer.'
760    )
761  end)
762
763  it('can detect a new parser and refresh folds accordingly', function()
764    local name = t.tmpname()
765    write_file(name, test_text)
766    command('edit ' .. name)
767    command [[
768      set filetype=some_filetype_without_treesitter_parser
769      set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0
770    ]]
771
772    -- foldexpr will return '0' for all lines
773    local function expect_no_folds()
774      local levels = get_fold_levels() ---@type integer[]
775      eq(19, #levels)
776      for lnum, level in ipairs(levels) do
777        eq('0', level, string.format("foldlevel[%d] == %s; expected '0'", lnum, level))
778      end
779    end
780    expect_no_folds()
781
782    -- reload buffer as c filetype to simulate new parser being found
783    feed('GA// vim: ft=c<Esc>')
784    command([[write | edit]])
785    local foldlevels = {
786      [1] = '>1',
787      [2] = '1',
788      [3] = '1',
789      [4] = '1',
790      [5] = '>2',
791      [6] = '2',
792      [7] = '2',
793      [8] = '1',
794      [9] = '1',
795      [10] = '>2',
796      [11] = '2',
797      [12] = '2',
798      [13] = '2',
799      [14] = '2',
800      [15] = '>3',
801      [16] = '3',
802      [17] = '3',
803      [18] = '2',
804      [19] = '1',
805    }
806    eq(foldlevels, get_fold_levels())
807
808    -- only changing filetype should change the parser again
809    command('set ft=some_filetype_without_treesitter_parser')
810    expect_no_folds()
811
812    command('set ft=c')
813    eq(foldlevels, get_fold_levels())
814  end)
815
816  it('no error when deleting lines at end of buffer with fml=0', function()
817    local screen = Screen.new(40, 2)
818    insert('hello')
819    parse('markdown')
820    command('set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldminlines=0')
821    feed('o<Esc>dd')
822    screen:expect([[
823      ^hello                                   |
824                                              |
825    ]])
826  end)
827
828  it('clamps unbounded changed ranges on 32-bit platforms', function()
829    insert([[
830one
831two]])
832
833    exec_lua(function()
834      local parser = {}
835
836      function parser:parse(_, callback)
837        if callback then
838          callback(nil, {})
839        end
840      end
841
842      function parser:for_each_tree() end
843
844      function parser:register_cbs(callbacks)
845        self.callbacks = callbacks
846      end
847
848      vim.treesitter.get_parser = function()
849        return parser
850      end
851
852      vim.treesitter.foldexpr()
853      vim.wo.foldmethod = 'expr'
854      vim._foldupdate = function(_, first, last)
855        _G.foldupdate_range = { first, last }
856      end
857
858      -- UINT32_MAX is represented as -1 by Lua integers on 32-bit platforms.
859      parser.callbacks.on_changedtree({ { 0, 0, -1, -1 } })
860    end)
861    poke_eventloop()
862
863    eq({ 0, 2 }, exec_lua('return _G.foldupdate_range'))
864  end)
865end)