robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
16.0 KiB667 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, after_each = t.describe, t.it, t.before_each, t.after_each
6local clear = n.clear
7local command = n.command
8local dedent = t.dedent
9local eq = t.eq
10local fn = n.fn
11local feed = n.feed
12local exec_capture = n.exec_capture
13local write_file = t.write_file
14local api = n.api
15
16describe('jumplist', function()
17  local fname1 = 'Xtest-functional-normal-jump'
18  local fname2 = fname1 .. '2'
19  before_each(clear)
20  after_each(function()
21    os.remove(fname1)
22    os.remove(fname2)
23  end)
24
25  it('does not add a new entry on startup', function()
26    eq('\n jump line  col file/text\n>', fn.execute('jumps'))
27  end)
28
29  it('does not require two <C-O> strokes to jump back', function()
30    write_file(fname1, 'first file contents')
31    write_file(fname2, 'second file contents')
32
33    command('args ' .. fname1 .. ' ' .. fname2)
34    local buf1 = fn.bufnr(fname1)
35    local buf2 = fn.bufnr(fname2)
36
37    command('next')
38    feed('<C-O>')
39    eq(buf1, fn.bufnr('%'))
40
41    command('first')
42    command('snext')
43    feed('<C-O>')
44    eq(buf1, fn.bufnr('%'))
45    feed('<C-I>')
46    eq(buf2, fn.bufnr('%'))
47    feed('<C-O>')
48    eq(buf1, fn.bufnr('%'))
49
50    command('drop ' .. fname2)
51    feed('<C-O>')
52    eq(buf1, fn.bufnr('%'))
53  end)
54
55  it('<C-O> scrolls cursor halfway when switching buffer #25763', function()
56    write_file(fname1, ('foobar\n'):rep(100))
57    write_file(fname2, 'baz')
58
59    local screen = Screen.new(12, 25)
60    command('set number')
61    command('edit ' .. fname1)
62    feed('35gg')
63    command('edit ' .. fname2)
64    feed('<C-O>')
65    screen:expect([[
66      {8: 24 }foobar  |
67      {8: 25 }foobar  |
68      {8: 26 }foobar  |
69      {8: 27 }foobar  |
70      {8: 28 }foobar  |
71      {8: 29 }foobar  |
72      {8: 30 }foobar  |
73      {8: 31 }foobar  |
74      {8: 32 }foobar  |
75      {8: 33 }foobar  |
76      {8: 34 }foobar  |
77      {8: 35 }^foobar  |
78      {8: 36 }foobar  |
79      {8: 37 }foobar  |
80      {8: 38 }foobar  |
81      {8: 39 }foobar  |
82      {8: 40 }foobar  |
83      {8: 41 }foobar  |
84      {8: 42 }foobar  |
85      {8: 43 }foobar  |
86      {8: 44 }foobar  |
87      {8: 45 }foobar  |
88      {8: 46 }foobar  |
89      {8: 47 }foobar  |
90                  |
91    ]])
92  end)
93end)
94
95describe("jumpoptions=stack behaves like 'tagstack'", function()
96  before_each(function()
97    clear()
98    feed(':clearjumps<cr>')
99
100    -- Add lines so that we have locations to jump to.
101    for i = 1, 101, 1 do
102      feed('iLine ' .. i .. '<cr><esc>')
103    end
104
105    -- Jump around to add some locations to the jump list.
106    feed('0gg')
107    feed('10gg')
108    feed('20gg')
109    feed('30gg')
110    feed('40gg')
111    feed('50gg')
112
113    feed(':set jumpoptions=stack<cr>')
114  end)
115
116  after_each(function()
117    feed('set jumpoptions=')
118  end)
119
120  it('discards the tail when navigating from the middle', function()
121    feed('<C-O>')
122    feed('<C-O>')
123
124    eq(
125      ''
126        .. ' jump line  col file/text\n'
127        .. '   4   102    0 \n'
128        .. '   3     1    0 Line 1\n'
129        .. '   2    10    0 Line 10\n'
130        .. '   1    20    0 Line 20\n'
131        .. '>  0    30    0 Line 30\n'
132        .. '   1    40    0 Line 40\n'
133        .. '   2    50    0 Line 50',
134      exec_capture('jumps')
135    )
136
137    feed('90gg')
138
139    eq(
140      ''
141        .. ' jump line  col file/text\n'
142        .. '   5   102    0 \n'
143        .. '   4     1    0 Line 1\n'
144        .. '   3    10    0 Line 10\n'
145        .. '   2    20    0 Line 20\n'
146        .. '   1    30    0 Line 30\n'
147        .. '>',
148      exec_capture('jumps')
149    )
150  end)
151
152  it('does not add the same location twice adjacently', function()
153    feed('60gg')
154    feed('60gg')
155
156    eq(
157      ''
158        .. ' jump line  col file/text\n'
159        .. '   7   102    0 \n'
160        .. '   6     1    0 Line 1\n'
161        .. '   5    10    0 Line 10\n'
162        .. '   4    20    0 Line 20\n'
163        .. '   3    30    0 Line 30\n'
164        .. '   2    40    0 Line 40\n'
165        .. '   1    50    0 Line 50\n'
166        .. '>',
167      exec_capture('jumps')
168    )
169  end)
170
171  it('does add the same location twice nonadjacently', function()
172    feed('10gg')
173    feed('20gg')
174
175    eq(
176      ''
177        .. ' jump line  col file/text\n'
178        .. '   8   102    0 \n'
179        .. '   7     1    0 Line 1\n'
180        .. '   6    10    0 Line 10\n'
181        .. '   5    20    0 Line 20\n'
182        .. '   4    30    0 Line 30\n'
183        .. '   3    40    0 Line 40\n'
184        .. '   2    50    0 Line 50\n'
185        .. '   1    10    0 Line 10\n'
186        .. '>',
187      exec_capture('jumps')
188    )
189  end)
190end)
191
192describe('buffer deletion with jumpoptions+=clean', function()
193  local base_file = 'Xtest-functional-buffer-deletion'
194  local file1 = base_file .. '1'
195  local file2 = base_file .. '2'
196  local file3 = base_file .. '3'
197  local base_content = 'text'
198  local content1 = base_content .. '1'
199  local content2 = base_content .. '2'
200  local content3 = base_content .. '3'
201
202  local function format_jumplist(input)
203    return dedent(input)
204      :gsub('%{file1%}', file1)
205      :gsub('%{file2%}', file2)
206      :gsub('%{file3%}', file3)
207      :gsub('%{content1%}', content1)
208      :gsub('%{content2%}', content2)
209      :gsub('%{content3%}', content3)
210  end
211
212  before_each(function()
213    clear()
214    command('clearjumps')
215
216    write_file(file1, content1, false, false)
217    write_file(file2, content2, false, false)
218    write_file(file3, content3, false, false)
219
220    command('edit ' .. file1)
221    command('edit ' .. file2)
222    command('edit ' .. file3)
223  end)
224
225  after_each(function()
226    os.remove(file1)
227    os.remove(file2)
228    os.remove(file3)
229  end)
230
231  it('deletes jump list entries when the current buffer is deleted', function()
232    command('edit ' .. file1)
233
234    eq(
235      format_jumplist([[
236       jump line  col file/text
237         3     1    0 {content1}
238         2     1    0 {file2}
239         1     1    0 {file3}
240      >]]),
241      exec_capture('jumps')
242    )
243
244    command('bwipeout')
245
246    eq(
247      format_jumplist([[
248       jump line  col file/text
249         1     1    0 {file2}
250      >  0     1    0 {content3}]]),
251      exec_capture('jumps')
252    )
253  end)
254
255  it('deletes jump list entries when another buffer is deleted', function()
256    eq(
257      format_jumplist([[
258       jump line  col file/text
259         2     1    0 {file1}
260         1     1    0 {file2}
261      >]]),
262      exec_capture('jumps')
263    )
264
265    command('bwipeout ' .. file2)
266
267    eq(
268      format_jumplist([[
269       jump line  col file/text
270         1     1    0 {file1}
271      >]]),
272      exec_capture('jumps')
273    )
274  end)
275
276  it('sets the correct jump index when the current buffer is deleted', function()
277    feed('<C-O>')
278
279    eq(
280      format_jumplist([[
281       jump line  col file/text
282         1     1    0 {file1}
283      >  0     1    0 {content2}
284         1     1    0 {file3}]]),
285      exec_capture('jumps')
286    )
287
288    command('bw')
289
290    eq(
291      format_jumplist([[
292       jump line  col file/text
293         1     1    0 {file1}
294      >  0     1    0 {content3}]]),
295      exec_capture('jumps')
296    )
297  end)
298
299  it('sets the correct jump index when the another buffer is deleted', function()
300    feed('<C-O>')
301
302    eq(
303      format_jumplist([[
304       jump line  col file/text
305         1     1    0 {file1}
306      >  0     1    0 {content2}
307         1     1    0 {file3}]]),
308      exec_capture('jumps')
309    )
310
311    command('bwipeout ' .. file1)
312
313    eq(
314      format_jumplist([[
315       jump line  col file/text
316      >  0     1    0 {content2}
317         1     1    0 {file3}]]),
318      exec_capture('jumps')
319    )
320  end)
321end)
322
323describe('buffer deletion with jumpoptions-=clean', function()
324  local base_file = 'Xtest-functional-buffer-deletion'
325  local file1 = base_file .. '1'
326  local file2 = base_file .. '2'
327  local base_content = 'text'
328  local content1 = base_content .. '1'
329  local content2 = base_content .. '2'
330
331  before_each(function()
332    clear()
333    command('clearjumps')
334    command('set jumpoptions-=clean')
335
336    write_file(file1, content1, false, false)
337    write_file(file2, content2, false, false)
338
339    command('edit ' .. file1)
340    command('edit ' .. file2)
341  end)
342
343  after_each(function()
344    os.remove(file1)
345    os.remove(file2)
346  end)
347
348  it('Ctrl-O reopens previous buffer with :bunload or :bdelete #28968', function()
349    eq(file2, fn.bufname(''))
350    command('bunload')
351    eq(file1, fn.bufname(''))
352    feed('<C-O>')
353    eq(file2, fn.bufname(''))
354    command('bdelete')
355    eq(file1, fn.bufname(''))
356    feed('<C-O>')
357    eq(file2, fn.bufname(''))
358  end)
359end)
360
361describe('jumpoptions=view', function()
362  local file1 = 'Xtestfile-functional-editor-jumps'
363  local file2 = 'Xtestfile-functional-editor-jumps-2'
364  local function content()
365    local c = {}
366    for i = 1, 30 do
367      c[i] = i .. ' line'
368    end
369    return table.concat(c, '\n')
370  end
371  before_each(function()
372    clear()
373    write_file(file1, content(), false, false)
374    write_file(file2, content(), false, false)
375    command('set jumpoptions=view')
376  end)
377  after_each(function()
378    os.remove(file1)
379    os.remove(file2)
380  end)
381
382  it('restores the view', function()
383    local screen = Screen.new(12, 8)
384    command('edit ' .. file1)
385    feed('12Gztj')
386    feed('gg<C-o>')
387    screen:expect([[
388      12 line     |
389      ^13 line     |
390      14 line     |
391      15 line     |
392      16 line     |
393      17 line     |
394      18 line     |
395                  |
396    ]])
397  end)
398
399  it("restores the view with 'smoothscroll'", function()
400    local screen = Screen.new(12, 8)
401    command('edit ' .. file1)
402    command('setlocal smoothscroll')
403    command([[call setline(24, repeat('a', 55))]])
404    feed('12Gzz')
405    local s1 = [[
406      9 line      |
407      10 line     |
408      11 line     |
409      ^12 line     |
410      13 line     |
411      14 line     |
412      15 line     |
413                  |
414    ]]
415    screen:expect(s1)
416    feed('G')
417    local s2 = [[
418      {1:<<<}aaaa     |
419      25 line     |
420      26 line     |
421      27 line     |
422      28 line     |
423      29 line     |
424      ^30 line     |
425                  |
426    ]]
427    screen:expect(s2)
428    feed('<C-o>')
429    screen:expect(s1)
430    feed('<C-i>')
431    screen:expect(s2)
432    feed('<C-o>')
433    screen:expect(s1)
434    command([[call setline(10, repeat('a', 55))]])
435    feed('zz')
436    local s3 = [[
437      {1:<<<}aaaaaaaaa|
438      aaaaaaa     |
439      11 line     |
440      ^12 line     |
441      13 line     |
442      14 line     |
443      15 line     |
444                  |
445    ]]
446    screen:expect(s3)
447    feed('G')
448    screen:expect(s2)
449    feed('<C-o>')
450    screen:expect(s3)
451    feed('<C-i>')
452    screen:expect(s2)
453    command([[call setline(10, '10 line')]])
454    feed('<C-o>')
455    -- Topline should still be line 10, but skipcol should be reset.
456    screen:expect([[
457      10 line     |
458      11 line     |
459      ^12 line     |
460      13 line     |
461      14 line     |
462      15 line     |
463      16 line     |
464                  |
465    ]])
466  end)
467
468  it('restores the view across files', function()
469    local screen = Screen.new(12, 5)
470    command('args ' .. file1 .. ' ' .. file2)
471    feed('12Gzt')
472    command('next')
473    feed('G')
474    screen:expect([[
475      27 line     |
476      28 line     |
477      29 line     |
478      ^30 line     |
479                  |
480    ]])
481    feed('<C-o><C-o>')
482    screen:expect([[
483      ^12 line     |
484      13 line     |
485      14 line     |
486      15 line     |
487                  |
488    ]])
489  end)
490
491  it('restores the view across files with <C-^>/:bprevious/:bnext', function()
492    local screen = Screen.new(12, 5)
493    command('args ' .. file1 .. ' ' .. file2)
494    feed('12Gzt')
495    local s1 = [[
496      ^12 line     |
497      13 line     |
498      14 line     |
499      15 line     |
500                  |
501    ]]
502    screen:expect(s1)
503    command('next')
504    feed('G')
505    local s2 = [[
506      27 line     |
507      28 line     |
508      29 line     |
509      ^30 line     |
510                  |
511    ]]
512    screen:expect(s2)
513    feed('<C-^>')
514    screen:expect(s1)
515    feed('<C-^>')
516    screen:expect(s2)
517    command('bprevious')
518    screen:expect(s1)
519    command('bnext')
520    screen:expect(s2)
521  end)
522
523  it("falls back to standard behavior when view can't be recovered", function()
524    local screen = Screen.new(12, 8)
525    command('edit ' .. file1)
526    feed('7GzbG')
527    api.nvim_buf_set_lines(0, 0, 2, true, {})
528    -- Move to line 7, and set it as the last line visible on the view with zb, meaning to recover
529    -- the view it needs to put the cursor 7 lines from the top line. Then go to the end of the
530    -- file, delete 2 lines before line 7, meaning the jump/mark is moved 2 lines up to line 5.
531    -- Therefore when trying to jump back to it it's not possible to set a 7 line offset from the
532    -- mark position to the top line, since there's only 5 lines from the mark position to line 0.
533    -- Therefore falls back to standard behavior which is centering the view/line.
534    feed('<C-o>')
535    screen:expect([[
536      4 line      |
537      5 line      |
538      6 line      |
539      ^7 line      |
540      8 line      |
541      9 line      |
542      10 line     |
543                  |
544    ]])
545  end)
546
547  it('falls back to standard behavior for a mark without a view', function()
548    local screen = Screen.new(12, 8)
549    command('edit ' .. file1)
550    feed('10ggzzvwy')
551    screen:expect([[
552      7 line      |
553      8 line      |
554      9 line      |
555      ^10 line     |
556      11 line     |
557      12 line     |
558      13 line     |
559                  |
560    ]])
561    feed('`]')
562    screen:expect([[
563      7 line      |
564      8 line      |
565      9 line      |
566      10 ^line     |
567      11 line     |
568      12 line     |
569      13 line     |
570                  |
571    ]])
572  end)
573
574  describe('tagstack popping', function()
575    local tags_file = 'Xtestfile-functional-editor-jumps-tags'
576    before_each(function()
577      write_file(
578        tags_file,
579        '!_TAG_FILE_ENCODING\tutf-8\t//\n'
580          .. ('10\t%s\t2\n'):format(file1)
581          .. ('30\t%s\t20\n'):format(file2),
582        false,
583        false
584      )
585      command('set tags=' .. tags_file)
586    end)
587    after_each(function()
588      os.remove(tags_file)
589    end)
590
591    it('restores the view', function()
592      local screen = Screen.new(12, 6)
593      command('set laststatus=2 | set statusline=%f | edit ' .. file1)
594      feed('10Gzb<C-]>30Gzt<C-]>')
595      screen:expect([[
596        19 line     |
597        ^20 line     |
598        21 line     |
599        22 line     |
600        {3:<tor-jumps-2}|
601                    |
602      ]])
603      feed('<C-T>')
604      screen:expect([[
605        ^30 line     |
606        {1:~           }|*3
607        {3:<ditor-jumps}|
608                    |
609      ]])
610      feed('<C-T>')
611      screen:expect([[
612        7 line      |
613        8 line      |
614        9 line      |
615        ^10 line     |
616        {3:<ditor-jumps}|
617                    |
618      ]])
619
620      local tagstack = '  # TO tag         FROM line  in file/text\n'
621        .. '> 1  1 10                 10  \n'
622        .. '  2  1 30                 30  '
623      eq(tagstack, exec_capture('tags'))
624      -- Un-pop via `:tag`; like `:tag 10` it should go to L2. (restoring cursor/view doesn't apply)
625      -- However, after a `:pop`, it should restore the view from after the single `<C-E>` below.
626      feed('<C-E>')
627      command('tag')
628      screen:expect([[
629        1 line      |
630        ^2 line      |
631        3 line      |
632        4 line      |
633        {3:<ditor-jumps}|
634                    |
635      ]])
636      command('pop')
637      screen:expect([[
638        8 line      |
639        9 line      |
640        ^10 line     |
641        11 line     |
642        {3:<ditor-jumps}|
643                    |
644      ]])
645      eq(tagstack, exec_capture('tags'))
646
647      -- No view information associated with tags set via settagstack().
648      -- (specifically, replacing tag "10" shouldn't continue to use it's now unrelated view)
649      fn.settagstack(fn.win_getid(), {
650        items = { { from = { fn.bufnr(), 11, 1, 0, 1 }, tagname = 'settagstack!!!' } },
651      }, 'r')
652      tagstack = '  # TO tag         FROM line  in file/text\n'
653        .. '  1  1 settagstack!!!     11  \n'
654        .. '>'
655      eq(tagstack, exec_capture('tags'))
656      feed('G<C-T>')
657      screen:expect([[
658        10 line     |
659        ^11 line     |
660        12 line     |
661        13 line     |
662        {3:<ditor-jumps}|
663                    |
664      ]])
665    end)
666  end)
667end)