robin/laufey

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

Justin M. Keyesfix(marks): undo reverts a mark set after the change #4133037c670e682

master
13.6 KiB505 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, finally =
6  t.describe, t.it, t.before_each, t.after_each, t.finally
7local api = n.api
8local clear = n.clear
9local command = n.command
10local fn = n.fn
11local eq = t.eq
12local feed = n.feed
13local write_file = t.write_file
14local pcall_err = t.pcall_err
15local cursor = function()
16  return n.api.nvim_win_get_cursor(0)
17end
18
19describe('named marks', function()
20  local file1 = 'Xtestfile-functional-editor-marks'
21  local file2 = 'Xtestfile-functional-editor-marks-2'
22  before_each(function()
23    clear()
24    write_file(file1, '1test1\n1test2\n1test3\n1test4', false, false)
25    write_file(file2, '2test1\n2test2\n2test3\n2test4', false, false)
26  end)
27  after_each(function()
28    os.remove(file1)
29    os.remove(file2)
30  end)
31
32  it('can be set', function()
33    command('edit ' .. file1)
34    command('mark a')
35    eq({ 1, 0 }, api.nvim_buf_get_mark(0, 'a'))
36    feed('jmb')
37    eq({ 2, 0 }, api.nvim_buf_get_mark(0, 'b'))
38    feed('jmB')
39    eq({ 3, 0 }, api.nvim_buf_get_mark(0, 'B'))
40    command('4kc')
41    eq({ 4, 0 }, api.nvim_buf_get_mark(0, 'c'))
42  end)
43
44  it('moved after a change follow their text through undo #5754', function()
45    command('edit ' .. file1)
46    command('1mark d')
47    command('$')
48    feed('dw')
49    command('2mark d')
50    eq({ 2, 0 }, api.nvim_buf_get_mark(0, 'd'))
51    command('undo')
52    eq({ 2, 0 }, api.nvim_buf_get_mark(0, 'd'))
53    command('redo')
54    eq({ 2, 0 }, api.nvim_buf_get_mark(0, 'd'))
55
56    -- A mark on a line the change deletes is cleared by the change; undo restores it.
57    api.nvim_buf_set_mark(0, 'e', 2, 2, {})
58    command('2delete')
59    eq({ 0, 2 }, api.nvim_buf_get_mark(0, 'e'))
60    command('undo')
61    eq({ 2, 2 }, api.nvim_buf_get_mark(0, 'e'))
62    eq('1test2', fn.getline(2))
63
64    -- A mark moved after a line-count change shifts with the text it was moved to.
65    command('2delete')
66    eq('1test3', fn.getline(2))
67    command('2mark f')
68    command('undo')
69    eq({ 3, 0 }, api.nvim_buf_get_mark(0, 'f'))
70    eq('1test3', fn.getline(3))
71  end)
72
73  it('errors when set out of range with :mark', function()
74    command('edit ' .. file1)
75    local err = pcall_err(n.exec_capture, '1000mark x')
76    eq('nvim_exec2(), line 1: Vim(mark):E16: Invalid range: 1000mark x', err)
77  end)
78
79  it('errors when set out of range with :k', function()
80    command('edit ' .. file1)
81    local err = pcall_err(n.exec_capture, '1000kx')
82    eq('nvim_exec2(), line 1: Vim(k):E16: Invalid range: 1000kx', err)
83  end)
84
85  it('errors on unknown mark name with :mark', function()
86    command('edit ' .. file1)
87    local err = pcall_err(n.exec_capture, 'mark #')
88    eq(
89      'nvim_exec2(), line 1: Vim(mark):E191: Argument must be a letter or forward/backward quote',
90      err
91    )
92  end)
93
94  it("errors on unknown mark name with '", function()
95    command('edit ' .. file1)
96    local err = pcall_err(n.exec_capture, "normal! '#")
97    eq('nvim_exec2(), line 1: Vim(normal):E78: Unknown mark', err)
98  end)
99
100  it('errors on unknown mark name with `', function()
101    command('edit ' .. file1)
102    local err = pcall_err(n.exec_capture, 'normal! `#')
103    eq('nvim_exec2(), line 1: Vim(normal):E78: Unknown mark', err)
104  end)
105
106  it("errors when moving to a mark that is not set with '", function()
107    command('edit ' .. file1)
108    local err = pcall_err(n.exec_capture, "normal! 'z")
109    eq('nvim_exec2(), line 1: Vim(normal):E20: Mark not set', err)
110    err = pcall_err(n.exec_capture, "normal! '.")
111    eq('nvim_exec2(), line 1: Vim(normal):E20: Mark not set', err)
112  end)
113
114  it('errors when moving to a mark that is not set with `', function()
115    command('edit ' .. file1)
116    local err = pcall_err(n.exec_capture, 'normal! `z')
117    eq('nvim_exec2(), line 1: Vim(normal):E20: Mark not set', err)
118    err = pcall_err(n.exec_capture, 'normal! `>')
119    eq('nvim_exec2(), line 1: Vim(normal):E20: Mark not set', err)
120  end)
121
122  it("errors when moving to a global mark that is not set with '", function()
123    command('edit ' .. file1)
124    local err = pcall_err(n.exec_capture, "normal! 'Z")
125    eq('nvim_exec2(), line 1: Vim(normal):E20: Mark not set', err)
126  end)
127
128  it('errors when moving to a global mark that is not set with `', function()
129    command('edit ' .. file1)
130    local err = pcall_err(n.exec_capture, 'normal! `Z')
131    eq('nvim_exec2(), line 1: Vim(normal):E20: Mark not set', err)
132  end)
133
134  it("can move to them using '", function()
135    command('args ' .. file1 .. ' ' .. file2)
136    feed('j')
137    feed('ma')
138    feed("G'a")
139    eq({ 2, 0 }, cursor())
140    feed('mA')
141    command('next')
142    feed("'A")
143    eq(1, api.nvim_get_current_buf())
144    eq({ 2, 0 }, cursor())
145  end)
146
147  it('can move to them using `', function()
148    command('args ' .. file1 .. ' ' .. file2)
149    feed('jll')
150    feed('ma')
151    feed('G`a')
152    eq({ 2, 2 }, cursor())
153    feed('mA')
154    command('next')
155    feed('`A')
156    eq(1, api.nvim_get_current_buf())
157    eq({ 2, 2 }, cursor())
158  end)
159
160  it("can move to them using g'", function()
161    command('args ' .. file1 .. ' ' .. file2)
162    feed('jll')
163    feed('ma')
164    feed("Gg'a")
165    eq({ 2, 0 }, cursor())
166    feed('mA')
167    command('next')
168    feed("g'A")
169    eq(1, api.nvim_get_current_buf())
170    eq({ 2, 0 }, cursor())
171  end)
172
173  it('can move to them using g`', function()
174    command('args ' .. file1 .. ' ' .. file2)
175    feed('jll')
176    feed('ma')
177    feed('Gg`a')
178    eq({ 2, 2 }, cursor())
179    feed('mA')
180    command('next')
181    feed('g`A')
182    eq(1, api.nvim_get_current_buf())
183    eq({ 2, 2 }, cursor())
184  end)
185
186  it("can move to them using :'", function()
187    command('args ' .. file1 .. ' ' .. file2)
188    feed('j')
189    feed('ma')
190    feed('G')
191    command("'a")
192    eq({ 2, 0 }, cursor())
193    feed('mA')
194    command('next')
195    command("'A")
196    eq(1, api.nvim_get_current_buf())
197    eq({ 2, 0 }, cursor())
198  end)
199
200  it("errors when it can't find the buffer", function()
201    command('args ' .. file1 .. ' ' .. file2)
202    feed('mA')
203    command('next')
204    command('bw! ' .. file1)
205    local err = pcall_err(n.exec_capture, "normal! 'A")
206    eq('nvim_exec2(), line 1: Vim(normal):E92: Buffer 1 not found', err)
207    os.remove(file1)
208  end)
209
210  it('errors when using a mark in another buffer in command range', function()
211    feed('ifoo<Esc>mA')
212    command('enew')
213    feed('ibar<Esc>')
214    eq("Vim(print):E20: Mark not set: 'Aprint", pcall_err(command, [['Aprint]]))
215  end)
216
217  it("leave a context mark when moving with '", function()
218    command('edit ' .. file1)
219    feed('llmamA')
220    feed('10j0') -- first col, last line
221    local pos = cursor()
222    feed("'a")
223    feed('<C-o>')
224    eq(pos, cursor())
225    feed("'A")
226    feed('<C-o>')
227    eq(pos, cursor())
228  end)
229
230  it('leave a context mark when moving with `', function()
231    command('edit ' .. file1)
232    feed('llmamA')
233    feed('10j0') -- first col, last line
234    local pos = cursor()
235    feed('`a')
236    feed('<C-o>')
237    eq(pos, cursor())
238    feed('`A')
239    feed('<C-o>')
240    eq(pos, cursor())
241  end)
242
243  it("leave a context mark when the mark changes buffer with g'", function()
244    command('args ' .. file1 .. ' ' .. file2)
245    local pos
246    feed('GmA')
247    command('next')
248    pos = cursor()
249    command('clearjumps')
250    feed("g'A") -- since the mark is in another buffer, it leaves a context mark
251    feed('<C-o>')
252    eq(pos, cursor())
253  end)
254
255  it('leave a context mark when the mark changes buffer with g`', function()
256    command('args ' .. file1 .. ' ' .. file2)
257    local pos
258    feed('GmA')
259    command('next')
260    pos = cursor()
261    command('clearjumps')
262    feed('g`A') -- since the mark is in another buffer, it leaves a context mark
263    feed('<C-o>')
264    eq(pos, cursor())
265  end)
266
267  it("do not leave a context mark when moving with g'", function()
268    command('edit ' .. file1)
269    local pos
270    feed('ma')
271    pos = cursor() -- Mark pos
272    feed('10j0') -- first col, last line
273    feed("g'a")
274    feed('<C-o>') -- should do nothing
275    eq(pos, cursor())
276    feed('mA')
277    pos = cursor() -- Mark pos
278    feed('10j0') -- first col, last line
279    feed("g'a")
280    feed('<C-o>') -- should do nothing
281    eq(pos, cursor())
282  end)
283
284  it('do not leave a context mark when moving with g`', function()
285    command('edit ' .. file1)
286    local pos
287    feed('ma')
288    pos = cursor() -- Mark pos
289    feed('10j0') -- first col, last line
290    feed('g`a')
291    feed('<C-o>') -- should do nothing
292    eq(pos, cursor())
293    feed('mA')
294    pos = cursor() -- Mark pos
295    feed('10j0') -- first col, last line
296    feed("g'a")
297    feed('<C-o>') -- should do nothing
298    eq(pos, cursor())
299  end)
300
301  it('open folds when moving to them', function()
302    command('edit ' .. file1)
303    feed('jzfG') -- Fold from the second line to the end
304    command('3mark a')
305    feed('G') -- On top of the fold
306    assert(fn.foldclosed('.') ~= -1) -- folded
307    feed("'a")
308    eq(-1, fn.foldclosed('.'))
309
310    feed('zc')
311    assert(fn.foldclosed('.') ~= -1) -- folded
312    -- TODO: remove this workaround after fixing #15873
313    feed('k`a')
314    eq(-1, fn.foldclosed('.'))
315
316    feed('zc')
317    assert(fn.foldclosed('.') ~= -1) -- folded
318    feed("kg'a")
319    eq(-1, fn.foldclosed('.'))
320
321    feed('zc')
322    assert(fn.foldclosed('.') ~= -1) -- folded
323    feed('kg`a')
324    eq(-1, fn.foldclosed('.'))
325  end)
326
327  it("do not open folds when moving to them doesn't move the cursor", function()
328    command('edit ' .. file1)
329    feed('jzfG') -- Fold from the second line to the end
330    assert(fn.foldclosed('.') == 2) -- folded
331    feed('ma')
332    feed("'a")
333    feed('`a')
334    feed("g'a")
335    feed('g`a')
336    -- should still be folded
337    eq(2, fn.foldclosed('.'))
338  end)
339
340  it("getting '{ '} '( ') does not move cursor", function()
341    api.nvim_buf_set_lines(0, 0, 0, true, { 'aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee' })
342    api.nvim_win_set_cursor(0, { 2, 0 })
343    fn.getpos("'{")
344    eq({ 2, 0 }, api.nvim_win_get_cursor(0))
345    fn.getpos("'}")
346    eq({ 2, 0 }, api.nvim_win_get_cursor(0))
347    fn.getpos("'(")
348    eq({ 2, 0 }, api.nvim_win_get_cursor(0))
349    fn.getpos("')")
350    eq({ 2, 0 }, api.nvim_win_get_cursor(0))
351  end)
352
353  it('in command range does not move cursor #19248', function()
354    api.nvim_create_user_command('Test', ':', { range = true })
355    api.nvim_buf_set_lines(0, 0, 0, true, { 'aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee' })
356    api.nvim_win_set_cursor(0, { 2, 0 })
357    command([['{,'}Test]])
358    eq({ 2, 0 }, api.nvim_win_get_cursor(0))
359  end)
360end)
361
362describe('named marks view', function()
363  local file1 = 'Xtestfile-functional-editor-marks'
364  local file2 = 'Xtestfile-functional-editor-marks-2'
365  local function content()
366    local c = {}
367    for i = 1, 30 do
368      c[i] = i .. ' line'
369    end
370    return table.concat(c, '\n')
371  end
372  before_each(function()
373    clear()
374    write_file(file1, content(), false, false)
375    write_file(file2, content(), false, false)
376    command('set jumpoptions+=view')
377  end)
378  after_each(function()
379    os.remove(file1)
380    os.remove(file2)
381  end)
382
383  it('is restored in normal mode but not op-pending mode', function()
384    local screen = Screen.new(12, 8)
385    command('edit ' .. file1)
386    feed('<C-e>jWma')
387    feed("G'a")
388    local expected = [[
389      2 line      |
390      ^3 line      |
391      4 line      |
392      5 line      |
393      6 line      |
394      7 line      |
395      8 line      |
396                  |
397    ]]
398    screen:expect({ grid = expected })
399    feed('G`a')
400    screen:expect([[
401      2 line      |
402      3 ^line      |
403      4 line      |
404      5 line      |
405      6 line      |
406      7 line      |
407      8 line      |
408                  |
409    ]])
410    -- not in op-pending mode #20886
411    feed('ggj=`a')
412    screen:expect([[
413      1 line      |
414      ^2 line      |
415      3 line      |
416      4 line      |
417      5 line      |
418      6 line      |
419      7 line      |
420                  |
421    ]])
422  end)
423
424  it('is restored across files', function()
425    local screen = Screen.new(12, 5)
426    command('args ' .. file1 .. ' ' .. file2)
427    feed('<C-e>mA')
428    local mark_view = [[
429      ^2 line      |
430      3 line      |
431      4 line      |
432      5 line      |
433                  |
434    ]]
435    screen:expect(mark_view)
436    command('next')
437    screen:expect([[
438      ^1 line      |
439      2 line      |
440      3 line      |
441      4 line      |
442                  |
443    ]])
444    feed("'A")
445    screen:expect(mark_view)
446  end)
447
448  it("fallback to standard behavior when view can't be recovered", function()
449    local screen = Screen.new(12, 10)
450    command('edit ' .. file1)
451    feed('7GzbmaG') -- Seven lines from the top
452    command('new') -- Screen size for window is now half the height can't be restored
453    feed("<C-w>p'a")
454    screen:expect([[
455                  |
456      {1:~           }|*3
457      {2:[No Name]   }|
458      6 line      |
459      ^7 line      |
460      8 line      |
461      {3:<itor-marks }|
462                  |
463    ]])
464  end)
465
466  it('fallback to standard behavior when mark is loaded from shada', function()
467    local screen = Screen.new(12, 6)
468    command('edit ' .. file1)
469    feed('G')
470    feed('mA')
471    screen:expect([[
472      26 line     |
473      27 line     |
474      28 line     |
475      29 line     |
476      ^30 line     |
477                  |
478    ]])
479    command('set shadafile=Xtestfile-functional-editor-marks-shada')
480    finally(function()
481      command('set shadafile=NONE')
482      os.remove('Xtestfile-functional-editor-marks-shada')
483    end)
484    command('wshada!')
485    command('bwipe!')
486    screen:expect([[
487      ^            |
488      {1:~           }|*4
489                  |
490    ]])
491    command('rshada!')
492    command('edit ' .. file1)
493    feed('`"')
494    screen:expect([[
495      26 line     |
496      27 line     |
497      28 line     |
498      29 line     |
499      ^30 line     |
500                  |
501    ]])
502    feed('`A')
503    screen:expect_unchanged()
504  end)
505end)