robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.3 KiB527 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each, setup =
5  t.describe, t.it, t.before_each, t.after_each, t.setup
6local clear = n.clear
7local insert = n.insert
8local exec = n.exec
9local feed = n.feed
10local expect = n.expect
11local command = n.command
12local fn = n.fn
13local eq = t.eq
14local neq = t.neq
15
16describe('Folding', function()
17  local tempfname = 'Xtest-fold.txt'
18
19  setup(clear)
20  before_each(function()
21    command('bwipe! | new')
22  end)
23  after_each(function()
24    os.remove(tempfname)
25  end)
26
27  it('manual folding adjusts with filter', function()
28    insert([[
29    1
30    2
31    3
32    4
33    5
34    6
35    7
36    8
37    9
38    10
39    11
40    12
41    13
42    14
43    15
44    16
45    17
46    18
47    19
48    20]])
49    command('4,$fold')
50    command('%foldopen')
51    command('10,$fold')
52    command('%foldopen')
53    command('1,8! cat')
54    feed('5ggzdzMGdd')
55    expect([[
56    1
57    2
58    3
59    4
60    5
61    6
62    7
63    8
64    9]])
65  end)
66
67  describe('adjusting folds after :move', function()
68    local function manually_fold_indent()
69      -- setting foldmethod twice is a trick to get vim to set the folds for me
70      command('setlocal foldmethod=indent')
71      command('setlocal foldmethod=manual')
72      -- Ensure that all folds will get closed (makes it easier to test the
73      -- length of folds).
74      command('setlocal foldminlines=0')
75      -- Start with all folds open (so :move ranges aren't affected by closed
76      -- folds).
77      command('%foldopen!')
78    end
79
80    local function get_folds()
81      local rettab = {}
82      for i = 1, fn.line('$') do
83        table.insert(rettab, fn.foldlevel(i))
84      end
85      return rettab
86    end
87
88    local function test_move_indent(insert_string, move_command)
89      -- This test is easy because we just need to ensure that the resulting
90      -- fold is the same as calculated when creating folds from scratch.
91      insert(insert_string)
92      command(move_command)
93      local after_move_folds = get_folds()
94      -- Doesn't change anything, but does call foldUpdateAll()
95      command('setlocal foldminlines=0')
96      eq(after_move_folds, get_folds())
97      -- Set up the buffer with insert_string for the manual fold testing.
98      command('enew!')
99      insert(insert_string)
100      manually_fold_indent()
101      command(move_command)
102    end
103
104    it('neither closes nor corrupts folds', function()
105      test_move_indent(
106        [[
107a
108	a
109	a
110	a
111	a
112	a
113a
114	a
115	a
116		a
117	a
118	a
119a
120	a
121	a
122	a
123	a
124	a]],
125        '7,12m0'
126      )
127      expect([[
128a
129	a
130	a
131		a
132	a
133	a
134a
135	a
136	a
137	a
138	a
139	a
140a
141	a
142	a
143	a
144	a
145	a]])
146      -- lines are not closed, folds are correct
147      for i = 1, fn.line('$') do
148        eq(-1, fn.foldclosed(i))
149        if i == 1 or i == 7 or i == 13 then
150          eq(0, fn.foldlevel(i))
151        elseif i == 4 then
152          eq(2, fn.foldlevel(i))
153        else
154          eq(1, fn.foldlevel(i))
155        end
156      end
157      -- folds are not corrupted
158      feed('zM')
159      eq(6, fn.foldclosedend(2))
160      eq(12, fn.foldclosedend(8))
161      eq(18, fn.foldclosedend(14))
162    end)
163
164    it("doesn't split a fold when the move is within it", function()
165      test_move_indent(
166        [[
167a
168	a
169	a
170		a
171		a
172		a
173		a
174	a
175	a
176a]],
177        '5m6'
178      )
179      eq({ 0, 1, 1, 2, 2, 2, 2, 1, 1, 0 }, get_folds())
180    end)
181
182    it('truncates folds that end in the moved range', function()
183      test_move_indent(
184        [[
185a
186	a
187		a
188		a
189		a
190a
191a]],
192        '4,5m6'
193      )
194      eq({ 0, 1, 2, 0, 0, 0, 0 }, get_folds())
195    end)
196
197    it('moves folds that start between moved range and destination', function()
198      test_move_indent(
199        [[
200a
201	a
202	a
203	a
204	a
205a
206a
207	a
208		a
209	a
210a
211a
212	a]],
213        '3,4m$'
214      )
215      eq({ 0, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 0, 0 }, get_folds())
216    end)
217
218    it('does not affect folds outside changed lines', function()
219      test_move_indent(
220        [[
221	a
222	a
223	a
224a
225a
226a
227	a
228	a
229	a]],
230        '4m5'
231      )
232      eq({ 1, 1, 1, 0, 0, 0, 1, 1, 1 }, get_folds())
233    end)
234
235    it('moves and truncates folds that start in moved range', function()
236      test_move_indent(
237        [[
238a
239	a
240		a
241		a
242		a
243a
244a
245a
246a
247a]],
248        '1,3m7'
249      )
250      eq({ 0, 0, 0, 0, 0, 1, 2, 0, 0, 0 }, get_folds())
251    end)
252
253    it('breaks a fold when moving text into it', function()
254      test_move_indent(
255        [[
256a
257	a
258		a
259		a
260		a
261a
262a]],
263        '$m4'
264      )
265      eq({ 0, 1, 2, 2, 0, 0, 0 }, get_folds())
266    end)
267
268    it('adjusts correctly when moving a range backwards', function()
269      test_move_indent(
270        [[
271a
272	a
273		a
274		a
275a]],
276        '2,3m0'
277      )
278      eq({ 1, 2, 0, 0, 0 }, get_folds())
279    end)
280
281    it('handles shifting all remaining folds', function()
282      test_move_indent(
283        [[
284	a
285		a
286		a
287		a
288	a
289		a
290		a
291		a
292	a
293		a
294		a
295		a
296		a
297	a
298a]],
299        '13m7'
300      )
301      eq({ 1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0 }, get_folds())
302    end)
303  end)
304
305  it('updates correctly on :read', function()
306    -- luacheck: ignore 621
307    t.write_file(
308      tempfname,
309      [[
310    a
311
312
313    	a]]
314    )
315    insert([[
316    	a
317    	a
318    	a
319    	a
320    ]])
321    command('setlocal foldmethod=indent')
322    command('2')
323    command('%foldopen')
324    command('read ' .. tempfname)
325    -- Just to check we have the correct file text.
326    expect([[
327    	a
328    	a
329    a
330
331
332    	a
333    	a
334    	a
335    ]])
336    for i = 1, 2 do
337      eq(1, fn.foldlevel(i))
338    end
339    for i = 3, 5 do
340      eq(0, fn.foldlevel(i))
341    end
342    for i = 6, 8 do
343      eq(1, fn.foldlevel(i))
344    end
345  end)
346
347  it('combines folds when removing separating space', function()
348    -- luacheck: ignore 621
349    insert([[
350    	a
351    	a
352    a
353    a
354    a
355    	a
356    	a
357    	a
358    ]])
359    command('setlocal foldmethod=indent')
360    command('3,5d')
361    eq(5, fn.foldclosedend(1))
362  end)
363
364  it("doesn't combine folds that have a specified end", function()
365    insert([[
366    {{{
367    }}}
368
369
370
371    {{{
372
373    }}}
374    ]])
375    command('setlocal foldmethod=marker')
376    command('3,5d')
377    command('%foldclose')
378    eq(2, fn.foldclosedend(1))
379  end)
380
381  it('splits folds according to >N and <N with foldexpr', function()
382    n.source([[
383    function TestFoldExpr(lnum)
384      let thisline = getline(a:lnum)
385      if thisline == 'a'
386        return 1
387      elseif thisline == 'b'
388        return 0
389      elseif thisline == 'c'
390        return '<1'
391      elseif thisline == 'd'
392        return '>1'
393      endif
394      return 0
395    endfunction
396    ]])
397    t.write_file(
398      tempfname,
399      [[
400    b
401    b
402    a
403    a
404    d
405    a
406    a
407    c]]
408    )
409    insert([[
410    a
411    a
412    a
413    a
414    a
415    a
416    ]])
417    command('setlocal foldmethod=expr foldexpr=TestFoldExpr(v:lnum)')
418    command('2')
419    command('foldopen')
420    command('read ' .. tempfname)
421    command('%foldclose')
422    eq(2, fn.foldclosedend(1))
423    eq(0, fn.foldlevel(3))
424    eq(0, fn.foldlevel(4))
425    eq(6, fn.foldclosedend(5))
426    eq(10, fn.foldclosedend(7))
427    eq(14, fn.foldclosedend(11))
428  end)
429
430  it('fdm=expr works correctly with :move #18668', function()
431    exec([[
432      set foldmethod=expr foldexpr=indent(v:lnum)
433      let content = ['', '', 'Line1', '  Line2', '  Line3',
434            \ 'Line4', '  Line5', '  Line6',
435            \ 'Line7', '  Line8', '  Line9']
436      call append(0, content)
437      normal! zM
438      call cursor(4, 1)
439      move 2
440      move 1
441    ]])
442    expect([[
443
444      Line2
445      Line3
446
447    Line1
448    Line4
449      Line5
450      Line6
451    Line7
452      Line8
453      Line9
454    ]])
455    eq(0, fn.foldlevel(1))
456    eq(3, fn.foldclosedend(2))
457    eq(0, fn.foldlevel(4))
458    eq(0, fn.foldlevel(5))
459    eq(0, fn.foldlevel(6))
460    eq(8, fn.foldclosedend(7))
461    eq(0, fn.foldlevel(9))
462    eq(11, fn.foldclosedend(10))
463    eq(0, fn.foldlevel(12))
464  end)
465
466  it('no folds remain if :delete makes buffer empty #19671', function()
467    command('setlocal foldmethod=manual')
468    fn.setline(1, { 'foo', 'bar', 'baz' })
469    command('2,3fold')
470    command('%delete')
471    eq(0, fn.foldlevel(1))
472  end)
473
474  it('multibyte fold markers work #20438', function()
475    command('setlocal foldmethod=marker foldmarker=«,» commentstring=/*%s*/')
476    insert([[
477      bbbbb
478      bbbbb
479      bbbbb]])
480    feed('zfgg')
481    expect([[
482      bbbbb/*«*/
483      bbbbb
484      bbbbb/*»*/]])
485    eq(1, fn.foldlevel(1))
486  end)
487
488  it('fdm=indent updates correctly with visual blockwise insertion #22898', function()
489    insert([[
490    a
491    b
492    ]])
493    command('setlocal foldmethod=indent shiftwidth=2')
494    feed('gg0<C-v>jI  <Esc>') -- indent both lines using visual blockwise mode
495    eq(1, fn.foldlevel(1))
496    eq(1, fn.foldlevel(2))
497  end)
498
499  it("fdm=indent doesn't open folds when inserting lower foldlevel line", function()
500    insert([[
501        insert an unindented line under this line
502        keep the lines under this line folded
503          keep this line folded 1
504          keep this line folded 2
505
506      .
507    ]])
508    command('set foldmethod=indent shiftwidth=2 noautoindent')
509    eq(1, fn.foldlevel(1))
510    eq(1, fn.foldlevel(2))
511    eq(2, fn.foldlevel(3))
512    eq(2, fn.foldlevel(4))
513
514    feed('zo') -- open the outer fold
515    neq(-1, fn.foldclosed(3)) -- make sure the inner fold is not open
516
517    feed('gg0oa<Esc>') -- insert unindented line
518
519    eq(1, fn.foldlevel(1)) --|  insert an unindented line under this line
520    eq(0, fn.foldlevel(2)) --|a
521    eq(1, fn.foldlevel(3)) --|  keep the lines under this line folded
522    eq(2, fn.foldlevel(4)) --|    keep this line folded 1
523    eq(2, fn.foldlevel(5)) --|    keep this line folded 2
524
525    neq(-1, fn.foldclosed(4)) -- make sure the inner fold is still not open
526  end)
527end)