robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
13.3 KiB461 linesraw
1-- ShaDa marks saving/reading support
2local t = require('test.testutil')
3local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
4local pcall_err = t.pcall_err
5local n = require('test.functional.testnvim')()
6local t_shada = require('test.functional.shada.testutil')
7
8local api, nvim_command, fn, eq = n.api, n.command, n.fn, t.eq
9local feed = n.feed
10local exec_capture = n.exec_capture
11local expect_exit = n.expect_exit
12local command = n.command
13
14local reset, clear = t_shada.reset, t_shada.clear
15
16local nvim_current_line = function()
17  return api.nvim_win_get_cursor(0)[1]
18end
19
20describe('ShaDa support code', function()
21  local testfilename = 'Xtestfile-functional-shada-marks'
22  local testfilename_2 = 'Xtestfile-functional-shada-marks-2'
23  local non_existent_testfilename = testfilename .. '.nonexistent'
24  before_each(function()
25    reset()
26    os.remove(non_existent_testfilename)
27    local fd = io.open(testfilename, 'w')
28    fd:write('test\n')
29    fd:write('test2\n')
30    fd:close()
31    fd = io.open(testfilename_2, 'w')
32    fd:write('test3\n')
33    fd:write('test4\n')
34    fd:close()
35  end)
36  after_each(function()
37    clear()
38    os.remove(testfilename)
39    os.remove(testfilename_2)
40  end)
41
42  it('is able to dump and read back global mark', function()
43    nvim_command('edit ' .. testfilename)
44    nvim_command('mark A')
45    nvim_command('2')
46    nvim_command('kB')
47    nvim_command('wshada')
48    reset()
49    nvim_command('rshada')
50    nvim_command('normal! `A')
51    eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
52    eq(1, nvim_current_line())
53    nvim_command('normal! `B')
54    eq(2, nvim_current_line())
55  end)
56
57  it('can dump and read back numbered marks', function()
58    local function move(cmd)
59      feed(cmd)
60      nvim_command('wshada')
61    end
62    nvim_command('edit ' .. testfilename)
63    move('l')
64    move('l')
65    move('j')
66    move('l')
67    move('l')
68    nvim_command('edit ' .. testfilename_2)
69    move('l')
70    move('l')
71    move('j')
72    move('l')
73    move('l')
74    -- we have now populated marks 0 through 9
75    nvim_command('edit ' .. testfilename)
76    feed('gg0')
77    -- during shada save on exit, mark 0 will become the current position,
78    -- 9 will be removed, and all other marks shifted
79    expect_exit(nvim_command, 'qall')
80    reset()
81    nvim_command('edit ' .. testfilename)
82    nvim_command('edit ' .. testfilename_2)
83    local marklist = fn.getmarklist()
84    for _, mark in ipairs(marklist) do
85      mark.file = fn.fnamemodify(mark.file, ':t')
86    end
87    eq({
88      {
89        file = testfilename,
90        mark = "'0",
91        pos = { 1, 1, 1, 0 },
92      },
93      {
94        file = testfilename_2,
95        mark = "'1",
96        pos = { 2, 2, 5, 0 },
97      },
98      {
99        file = testfilename_2,
100        mark = "'2",
101        pos = { 2, 2, 4, 0 },
102      },
103      {
104        file = testfilename_2,
105        mark = "'3",
106        pos = { 2, 2, 3, 0 },
107      },
108      {
109        file = testfilename_2,
110        mark = "'4",
111        pos = { 2, 1, 3, 0 },
112      },
113      {
114        file = testfilename_2,
115        mark = "'5",
116        pos = { 2, 1, 2, 0 },
117      },
118      {
119        file = testfilename,
120        mark = "'6",
121        pos = { 1, 2, 5, 0 },
122      },
123      {
124        file = testfilename,
125        mark = "'7",
126        pos = { 1, 2, 4, 0 },
127      },
128      {
129        file = testfilename,
130        mark = "'8",
131        pos = { 1, 2, 3, 0 },
132      },
133      {
134        file = testfilename,
135        mark = "'9",
136        pos = { 1, 1, 3, 0 },
137      },
138    }, marklist)
139  end)
140
141  it('does not dump global or numbered marks with `f0` in shada', function()
142    nvim_command('set shada+=f0')
143    nvim_command('edit ' .. testfilename)
144    nvim_command('mark A')
145    nvim_command('2')
146    nvim_command('wshada')
147    reset()
148    nvim_command('language C')
149    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `A'))
150    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `0'))
151  end)
152
153  it("restores global and numbered marks even with `'0` and `f0` in shada", function()
154    nvim_command('edit ' .. testfilename)
155    nvim_command('mark A')
156    nvim_command('2')
157    nvim_command('wshada')
158    reset("set shada='0,f0")
159    nvim_command('language C')
160    nvim_command('normal! `A')
161    eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
162    eq(1, nvim_current_line())
163    nvim_command('normal! `0')
164    eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
165    eq(2, nvim_current_line())
166  end)
167
168  it('is able to dump and read back local mark', function()
169    nvim_command('edit ' .. testfilename)
170    nvim_command('mark a')
171    nvim_command('2')
172    nvim_command('kb')
173    expect_exit(nvim_command, 'qall')
174    reset()
175    nvim_command('edit ' .. testfilename)
176    nvim_command('normal! `a')
177    eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
178    eq(1, nvim_current_line())
179    nvim_command('normal! `b')
180    eq(2, nvim_current_line())
181  end)
182
183  it('is able to dump and read back mark "', function()
184    nvim_command('edit ' .. testfilename)
185    nvim_command('2')
186    expect_exit(nvim_command, 'qall')
187    reset()
188    nvim_command('edit ' .. testfilename)
189    nvim_command('normal! `"')
190    eq(2, nvim_current_line())
191  end)
192
193  it("is able to dump, read back local mark and use `]'`", function()
194    nvim_command('edit ' .. testfilename)
195    nvim_command('2')
196    nvim_command('mark a')
197    expect_exit(nvim_command, 'qall')
198    reset({ args = { testfilename } })
199    eq(1, nvim_current_line())
200    nvim_command("normal! ]'")
201    eq(2, nvim_current_line())
202  end)
203
204  it('is able to dump and read back mark " from a closed tab', function()
205    nvim_command('edit ' .. testfilename)
206    nvim_command('tabedit ' .. testfilename_2)
207    nvim_command('2')
208    nvim_command('q!')
209    expect_exit(nvim_command, 'qall')
210    reset()
211    nvim_command('edit ' .. testfilename_2)
212    nvim_command('normal! `"')
213    eq(2, nvim_current_line())
214  end)
215
216  it('is able to populate v:oldfiles', function()
217    nvim_command('edit ' .. testfilename)
218    local tf_full = api.nvim_buf_get_name(0)
219    nvim_command('edit ' .. testfilename_2)
220    local tf_full_2 = api.nvim_buf_get_name(0)
221    expect_exit(nvim_command, 'qall')
222    reset()
223    local oldfiles = api.nvim_get_vvar('oldfiles')
224    table.sort(oldfiles)
225    eq(2, #oldfiles)
226    eq(testfilename, oldfiles[1]:sub(-#testfilename))
227    eq(testfilename_2, oldfiles[2]:sub(-#testfilename_2))
228    eq(tf_full, oldfiles[1])
229    eq(tf_full_2, oldfiles[2])
230    nvim_command('rshada!')
231    oldfiles = api.nvim_get_vvar('oldfiles')
232    table.sort(oldfiles)
233    eq(2, #oldfiles)
234    eq(testfilename, oldfiles[1]:sub(-#testfilename))
235    eq(testfilename_2, oldfiles[2]:sub(-#testfilename_2))
236    eq(tf_full, oldfiles[1])
237    eq(tf_full_2, oldfiles[2])
238  end)
239
240  it('is able to dump and restore jump list', function()
241    nvim_command('edit ' .. testfilename_2)
242    nvim_command('normal! G')
243    nvim_command('normal! gg')
244    nvim_command('edit ' .. testfilename)
245    nvim_command('normal! G')
246    nvim_command('normal! gg')
247    nvim_command('enew')
248    nvim_command('normal! gg')
249    local saved = exec_capture('jumps')
250    expect_exit(nvim_command, 'qall')
251    reset()
252    eq(saved, exec_capture('jumps'))
253  end)
254
255  it("does not dump jumplist if `'0` in shada", function()
256    local empty_jumps = exec_capture('jumps')
257    nvim_command("set shada='0")
258    nvim_command('edit ' .. testfilename_2)
259    nvim_command('normal! G')
260    nvim_command('normal! gg')
261    nvim_command('edit ' .. testfilename)
262    nvim_command('normal! G')
263    nvim_command('normal! gg')
264    nvim_command('enew')
265    nvim_command('normal! gg')
266    expect_exit(nvim_command, 'qall')
267    reset()
268    eq(empty_jumps, exec_capture('jumps'))
269  end)
270
271  it("does read back jumplist even with `'0` in shada", function()
272    nvim_command('edit ' .. testfilename_2)
273    nvim_command('normal! G')
274    nvim_command('normal! gg')
275    nvim_command('edit ' .. testfilename)
276    nvim_command('normal! G')
277    nvim_command('normal! gg')
278    nvim_command('enew')
279    nvim_command('normal! gg')
280    local saved = exec_capture('jumps')
281    expect_exit(nvim_command, 'qall')
282    reset("set shada='0")
283    eq(saved, exec_capture('jumps'))
284  end)
285
286  it('when dumping jump list also dumps current position', function()
287    nvim_command('edit ' .. testfilename)
288    nvim_command('normal! G')
289    nvim_command('split ' .. testfilename_2)
290    nvim_command('normal! G')
291    nvim_command('wshada')
292    nvim_command('quit')
293    nvim_command('rshada')
294    nvim_command('normal! \15') -- <C-o>
295    eq(testfilename_2, fn.bufname('%'))
296    eq({ 2, 0 }, api.nvim_win_get_cursor(0))
297  end)
298
299  it('is able to dump and restore jump list with different times', function()
300    nvim_command('edit ' .. testfilename_2)
301    nvim_command('sleep 10m')
302    nvim_command('normal! G')
303    nvim_command('sleep 10m')
304    nvim_command('normal! gg')
305    nvim_command('sleep 10m')
306    nvim_command('edit ' .. testfilename)
307    nvim_command('sleep 10m')
308    nvim_command('normal! G')
309    nvim_command('sleep 10m')
310    nvim_command('normal! gg')
311    expect_exit(nvim_command, 'qall')
312    reset()
313    nvim_command('redraw')
314    nvim_command('edit ' .. testfilename)
315    eq(testfilename, fn.bufname('%'))
316    eq(1, nvim_current_line())
317    nvim_command('execute "normal! \\<C-o>"')
318    eq(testfilename, fn.bufname('%'))
319    eq(2, nvim_current_line())
320    nvim_command('execute "normal! \\<C-o>"')
321    eq(testfilename_2, fn.bufname('%'))
322    eq(1, nvim_current_line())
323    nvim_command('execute "normal! \\<C-o>"')
324    eq(testfilename_2, fn.bufname('%'))
325    eq(2, nvim_current_line())
326    nvim_command('execute "normal! \\<C-o>"')
327    eq(testfilename_2, fn.bufname('%'))
328    eq(2, nvim_current_line())
329  end)
330
331  it('is able to dump and restore change list', function()
332    nvim_command('edit ' .. testfilename)
333    nvim_command('normal! Gra')
334    nvim_command('normal! ggrb')
335    expect_exit(nvim_command, 'qall!')
336    reset()
337    nvim_command('edit ' .. testfilename)
338    nvim_command('normal! Gg;')
339    -- Note: without “sync” “commands” test has good changes to fail for unknown
340    -- reason (in first eq expected 1 is compared with 2). Any command inserted
341    -- causes this to work properly.
342    nvim_command('" sync')
343    eq(1, nvim_current_line())
344    nvim_command('normal! g;')
345    nvim_command('" sync 2')
346    eq(2, nvim_current_line())
347  end)
348
349  -- -c temporary sets lnum to zero to make `+/pat` work, so calling setpcmark()
350  -- during -c used to add item with zero lnum to jump list.
351  it('does not create incorrect file for non-existent buffers when writing from -c', function()
352    local p = n.spawn_wait {
353      args_rm = {
354        '-i',
355        '--embed', -- no --embed
356      },
357      args = {
358        '-i',
359        api.nvim_get_var('tmpname'), -- Use same shada file as parent.
360        '--cmd',
361        'silent edit ' .. non_existent_testfilename,
362        '-c',
363        'qall',
364      },
365    }
366    eq('', p:output())
367    eq(0, p.status)
368    command('rshada')
369  end)
370
371  it('does not create incorrect file for non-existent buffers opened from -c', function()
372    local p = n.spawn_wait {
373      args_rm = {
374        '-i',
375        '--embed', -- no --embed
376      },
377      args = {
378        '-i',
379        api.nvim_get_var('tmpname'), -- Use same shada file as parent.
380        '-c',
381        'silent edit ' .. non_existent_testfilename,
382        '-c',
383        'autocmd VimEnter * qall',
384      },
385    }
386    eq('', p:output())
387    eq(0, p.status)
388    command('rshada')
389  end)
390
391  it('updates deleted marks with :delmarks', function()
392    nvim_command('edit ' .. testfilename)
393
394    nvim_command('mark A')
395    nvim_command('mark a')
396    -- create a change to set the '.' mark,
397    -- since it can't be set via :mark
398    feed('ggifoobar<esc>')
399    nvim_command('wshada')
400
401    reset()
402    nvim_command('edit ' .. testfilename)
403    nvim_command('normal! `A`a`.')
404    nvim_command('delmarks A a .')
405    nvim_command('wshada')
406
407    reset()
408    nvim_command('edit ' .. testfilename)
409    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `A'))
410    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `a'))
411    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `.'))
412  end)
413
414  it('updates deleted marks with :delmarks!', function()
415    nvim_command('edit ' .. testfilename)
416
417    nvim_command('mark A')
418    nvim_command('mark a')
419    feed('ggifoobar<esc>')
420    nvim_command('wshada')
421
422    reset()
423    nvim_command('edit ' .. testfilename)
424    nvim_command('normal! `A`a`.')
425    nvim_command('delmarks!')
426    nvim_command('wshada')
427
428    reset()
429    nvim_command('edit ' .. testfilename)
430    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `a'))
431    eq('Vim(normal):E20: Mark not set', pcall_err(command, 'normal! `.'))
432    -- Make sure that uppercase marks aren't deleted.
433    nvim_command('normal! `A')
434  end)
435
436  it('preserves marks after :bdelete #35770', function()
437    nvim_command('edit foo')
438    nvim_command('mark X')
439    nvim_command('bdelete')
440    nvim_command('wshada')
441
442    reset()
443    local mX = api.nvim_get_mark('X', {})
444    t.matches(vim.pesc('foo'), mX[4])
445
446    nvim_command('delmarks X')
447    nvim_command('edit foo')
448    nvim_command('mark X')
449    nvim_command('edit bar')
450    nvim_command('mark Y')
451    nvim_command('bufdo bdelete')
452    nvim_command('edit foo')
453    nvim_command('wshada')
454
455    reset()
456    mX = api.nvim_get_mark('X', {})
457    local mY = api.nvim_get_mark('Y', {})
458    t.matches(vim.pesc('foo'), mX[4])
459    t.matches(vim.pesc('bar'), mY[4])
460  end)
461end)