Skip to content

Commit 6b45dbc

Browse files
committed
mark: Make sure that jumplist item will not have zero lnum
Fixes neovim#7169
1 parent dee78a4 commit 6b45dbc

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

Diff for: src/nvim/mark.c

+4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ void setpcmark(void)
171171
curwin->w_prev_pcmark = curwin->w_pcmark;
172172
curwin->w_pcmark = curwin->w_cursor;
173173

174+
if (curwin->w_pcmark.lnum == 0) {
175+
curwin->w_pcmark.lnum = 1;
176+
}
177+
174178
/* If jumplist is full: remove oldest entry */
175179
if (++curwin->w_jumplistlen > JUMPLISTSIZE) {
176180
curwin->w_jumplistlen = JUMPLISTSIZE;

Diff for: src/nvim/shada.c

+6
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,12 @@ static ShaDaWriteResult shada_write(ShaDaWriteDef *const sd_writer,
25572557
xfmark_T fm;
25582558
jump_iter = mark_jumplist_iter(jump_iter, curwin, &fm);
25592559

2560+
if (fm.fmark.mark.lnum == 0) {
2561+
iemsgf("ShaDa: mark lnum zero (ji:%p, js:%p, len:%i)",
2562+
(void *)jump_iter, (void *)&curwin->w_jumplist[0],
2563+
curwin->w_jumplistlen);
2564+
continue;
2565+
}
25602566
const buf_T *const buf = (fm.fmark.fnum == 0
25612567
? NULL
25622568
: buflist_findnr(fm.fmark.fnum));

Diff for: test/functional/shada/helpers.lua

+18-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ local write_file, merge_args = helpers.write_file, helpers.merge_args
66
local mpack = require('mpack')
77

88
local tmpname = helpers.tmpname()
9-
local additional_cmd = ''
9+
local append_argv = nil
1010

11-
local function nvim_argv(shada_file)
11+
local function nvim_argv(shada_file, embed)
1212
local argv = {nvim_prog, '-u', 'NONE', '-i', shada_file or tmpname, '-N',
1313
'--cmd', 'set shortmess+=I background=light noswapfile',
14-
'--cmd', additional_cmd,
15-
'--embed'}
16-
if helpers.prepend_argv then
17-
return merge_args(helpers.prepend_argv, argv)
14+
embed or '--embed'}
15+
if helpers.prepend_argv or append_argv then
16+
return merge_args(helpers.prepend_argv, argv, append_argv)
1817
else
1918
return argv
2019
end
@@ -26,12 +25,21 @@ local reset = function(shada_file)
2625
end
2726

2827
local set_additional_cmd = function(s)
29-
additional_cmd = s
28+
append_argv = {'--cmd', s}
29+
end
30+
31+
local function add_argv(...)
32+
if select('#', ...) == 0 then
33+
append_argv = nil
34+
else
35+
append_argv = {...}
36+
end
3037
end
3138

3239
local clear = function()
40+
os.execute('cp ' .. tmpname .. ' /tmp/test.shada')
3341
os.remove(tmpname)
34-
set_additional_cmd('')
42+
append_argv = nil
3543
end
3644

3745
local get_shada_rw = function(fname)
@@ -80,7 +88,9 @@ end
8088
return {
8189
reset=reset,
8290
set_additional_cmd=set_additional_cmd,
91+
add_argv=add_argv,
8392
clear=clear,
8493
get_shada_rw=get_shada_rw,
8594
read_shada_file=read_shada_file,
95+
nvim_argv=nvim_argv,
8696
}

Diff for: test/functional/shada/marks_spec.lua

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ local shada_helpers = require('test.functional.shada.helpers')
99
local reset, set_additional_cmd, clear =
1010
shada_helpers.reset, shada_helpers.set_additional_cmd,
1111
shada_helpers.clear
12+
local add_argv = shada_helpers.add_argv
13+
local nvim_argv = shada_helpers.nvim_argv
1214

1315
local nvim_current_line = function()
1416
return curwinmeths.get_cursor()[1]
@@ -17,8 +19,10 @@ end
1719
describe('ShaDa support code', function()
1820
local testfilename = 'Xtestfile-functional-shada-marks'
1921
local testfilename_2 = 'Xtestfile-functional-shada-marks-2'
22+
local non_existent_testfilename = testfilename .. '.nonexistent'
2023
before_each(function()
2124
reset()
25+
os.remove(non_existent_testfilename)
2226
local fd = io.open(testfilename, 'w')
2327
fd:write('test\n')
2428
fd:write('test2\n')
@@ -214,4 +218,23 @@ describe('ShaDa support code', function()
214218
nvim_command('" sync 2')
215219
eq(2, nvim_current_line())
216220
end)
221+
222+
-- -c temporary sets lnum to zero to make `+/pat` work, so calling setpcmark()
223+
-- during -c used to add item with zero lnum to jump list.
224+
it('does not create incorrect file for non-existent buffers when writing from -c',
225+
function()
226+
add_argv('--cmd', 'silent edit ' .. non_existent_testfilename, '-c', 'qall')
227+
local argv = nvim_argv(nil, '--headless')
228+
eq('', funcs.system(argv))
229+
eq(0, exc_exec('rshada'))
230+
end)
231+
232+
it('does not create incorrect file for non-existent buffers opened from -c',
233+
function()
234+
add_argv('-c', 'silent edit ' .. non_existent_testfilename,
235+
'-c', 'autocmd VimEnter * qall')
236+
local argv = nvim_argv(nil, '--headless')
237+
eq('', funcs.system(argv))
238+
eq(0, exc_exec('rshada'))
239+
end)
217240
end)

0 commit comments

Comments
 (0)