Merge pull request #13878 from bfredl/incmark

inccommand: preserve extmarks when undoing preview substitution
This commit is contained in:
Björn Linse
2021-02-05 08:50:53 +01:00
committed by GitHub
2 changed files with 78 additions and 63 deletions

View File

@@ -3316,11 +3316,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout,
int save_b_changed = curbuf->b_changed; int save_b_changed = curbuf->b_changed;
bool preview = (State & CMDPREVIEW); bool preview = (State & CMDPREVIEW);
// inccommand tests fail without this check bool did_save = false;
if (!preview) {
// Required for Undo to work for extmarks.
u_save_cursor();
}
if (!global_busy) { if (!global_busy) {
sub_nsubs = 0; sub_nsubs = 0;
@@ -3997,6 +3993,11 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout,
int matchcols = end.col - ((end.lnum == start.lnum) int matchcols = end.col - ((end.lnum == start.lnum)
? start.col : 0); ? start.col : 0);
int subcols = new_endcol - ((lnum == lnum_start) ? start_col : 0); int subcols = new_endcol - ((lnum == lnum_start) ? start_col : 0);
if (!did_save) {
// Required for Undo to work for extmarks.
u_save_cursor();
did_save = true;
}
extmark_splice(curbuf, lnum_start-1, start_col, extmark_splice(curbuf, lnum_start-1, start_col,
end.lnum-start.lnum, matchcols, replaced_bytes, end.lnum-start.lnum, matchcols, replaced_bytes,
lnum-lnum_start, subcols, sublen-1, kExtmarkUndo); lnum-lnum_start, subcols, sublen-1, kExtmarkUndo);

View File

@@ -20,12 +20,13 @@ local origlines = {"original line 1",
"original line 6", "original line 6",
" indented line"} " indented line"}
local function attach_buffer(evname) before_each(function ()
exec_lua([[ clear()
exec_lua [[
local evname = ... local evname = ...
local events = {} local events = {}
function test_register(bufnr, id, changedtick, utf_sizes, preview) function test_register(bufnr, evname, id, changedtick, utf_sizes, preview)
local function callback(...) local function callback(...)
table.insert(events, {id, ...}) table.insert(events, {id, ...})
if test_unreg == id then if test_unreg == id then
@@ -44,41 +45,30 @@ local function attach_buffer(evname)
events = {} events = {}
return ret_events return ret_events
end end
]], evname) ]]
end
describe('lua buffer event callbacks: on_lines', function()
before_each(function()
clear()
attach_buffer('on_lines')
end) end)
describe('lua buffer event callbacks: on_lines', function()
-- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot local function setup_eventcheck(verify, utf_sizes, lines)
-- assert the wrong thing), but masks errors with unflushed lines (as
-- nvim_buf_get_offset forces a flush of the memline). To be safe run the
-- test both ways.
local function check(verify,utf_sizes)
local lastsize local lastsize
meths.buf_set_lines(0, 0, -1, true, origlines) meths.buf_set_lines(0, 0, -1, true, lines)
if verify then if verify then
lastsize = meths.buf_get_offset(0, meths.buf_line_count(0)) lastsize = meths.buf_get_offset(0, meths.buf_line_count(0))
end end
exec_lua("return test_register(...)", 0, "test1",false,utf_sizes) exec_lua("return test_register(...)", 0, "on_lines", "test1",false,utf_sizes)
local tick = meths.buf_get_changedtick(0)
local verify_name = "test1" local verify_name = "test1"
local function check_events(expected) local function check_events(expected)
local events = exec_lua("return get_events(...)" ) local events = exec_lua("return get_events(...)" )
if utf_sizes then if utf_sizes then
-- this test case uses ASCII only, so sizes should be the same. -- this test case uses ASCII only, so sizes should be the same.
-- Unicode is tested below. -- Unicode is tested below.
for _, event in ipairs(expected) do for _, event in ipairs(expected) do
event[9] = event[8] event[9] = event[9] or event[8]
event[10] = event[8] event[10] = event[10] or event[9]
end end
end end
eq(expected, events) expect_events(expected, events, "line updates")
if verify then if verify then
for _, event in ipairs(events) do for _, event in ipairs(events) do
if event[1] == verify_name and event[2] == "lines" then if event[1] == verify_name and event[2] == "lines" then
@@ -92,25 +82,38 @@ describe('lua buffer event callbacks: on_lines', function()
end end
end end
end end
return check_events, function(new) verify_name = new end
end
-- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot
-- assert the wrong thing), but masks errors with unflushed lines (as
-- nvim_buf_get_offset forces a flush of the memline). To be safe run the
-- test both ways.
local function check(verify,utf_sizes)
local check_events, verify_name = setup_eventcheck(verify, utf_sizes, origlines)
local tick = meths.buf_get_changedtick(0)
command('set autoindent') command('set autoindent')
command('normal! GyyggP') command('normal! GyyggP')
tick = tick + 1 tick = tick + 1
check_events({{ "test1", "lines", 1, tick, 0, 0, 1, 0}}) check_events {{ "test1", "lines", 1, tick, 0, 0, 1, 0}}
meths.buf_set_lines(0, 3, 5, true, {"changed line"}) meths.buf_set_lines(0, 3, 5, true, {"changed line"})
tick = tick + 1 tick = tick + 1
check_events({{ "test1", "lines", 1, tick, 3, 5, 4, 32 }}) check_events {{ "test1", "lines", 1, tick, 3, 5, 4, 32 }}
exec_lua("return test_register(...)", 0, "test2", true, utf_sizes) exec_lua("return test_register(...)", 0, "on_lines", "test2", true, utf_sizes)
tick = tick + 1 tick = tick + 1
command('undo') command('undo')
-- plugins can opt in to receive changedtick events, or choose -- plugins can opt in to receive changedtick events, or choose
-- to only receive actual changes. -- to only receive actual changes.
check_events({{ "test1", "lines", 1, tick, 3, 4, 5, 13 }, check_events {
{ "test2", "lines", 1, tick, 3, 4, 5, 13 }, { "test1", "lines", 1, tick, 3, 4, 5, 13 };
{ "test2", "changedtick", 1, tick+1 } }) { "test2", "lines", 1, tick, 3, 4, 5, 13 };
{ "test2", "changedtick", 1, tick+1 };
}
tick = tick + 1 tick = tick + 1
-- simulate next callback returning true -- simulate next callback returning true
@@ -121,38 +124,40 @@ describe('lua buffer event callbacks: on_lines', function()
-- plugins can opt in to receive changedtick events, or choose -- plugins can opt in to receive changedtick events, or choose
-- to only receive actual changes. -- to only receive actual changes.
check_events({{ "test1", "lines", 1, tick, 6, 7, 9, 16 }, check_events {
{ "test2", "lines", 1, tick, 6, 7, 9, 16 }}) { "test1", "lines", 1, tick, 6, 7, 9, 16 };
{ "test2", "lines", 1, tick, 6, 7, 9, 16 };
}
verify_name = "test2" verify_name "test2"
meths.buf_set_lines(0, 1, 1, true, {"added"}) meths.buf_set_lines(0, 1, 1, true, {"added"})
tick = tick + 1 tick = tick + 1
check_events({{ "test2", "lines", 1, tick, 1, 1, 2, 0 }}) check_events {{ "test2", "lines", 1, tick, 1, 1, 2, 0 }}
feed('wix') feed('wix')
tick = tick + 1 tick = tick + 1
check_events({{ "test2", "lines", 1, tick, 4, 5, 5, 16 }}) check_events {{ "test2", "lines", 1, tick, 4, 5, 5, 16 }}
-- check hot path for multiple insert -- check hot path for multiple insert
feed('yz') feed('yz')
tick = tick + 1 tick = tick + 1
check_events({{ "test2", "lines", 1, tick, 4, 5, 5, 17 }}) check_events {{ "test2", "lines", 1, tick, 4, 5, 5, 17 }}
feed('<bs>') feed('<bs>')
tick = tick + 1 tick = tick + 1
check_events({{ "test2", "lines", 1, tick, 4, 5, 5, 19 }}) check_events {{ "test2", "lines", 1, tick, 4, 5, 5, 19 }}
feed('<esc>Go') feed('<esc>Go')
tick = tick + 1 tick = tick + 1
check_events({{ "test2", "lines", 1, tick, 11, 11, 12, 0 }}) check_events {{ "test2", "lines", 1, tick, 11, 11, 12, 0 }}
feed('x') feed('x')
tick = tick + 1 tick = tick + 1
check_events({{ "test2", "lines", 1, tick, 11, 12, 12, 5 }}) check_events {{ "test2", "lines", 1, tick, 11, 12, 12, 5 }}
command('bwipe!') command('bwipe!')
check_events({{ "test2", "detach", 1 }}) check_events {{ "test2", "detach", 1 }}
end end
it('works', function() it('works', function()
@@ -167,51 +172,63 @@ describe('lua buffer event callbacks: on_lines', function()
check(false,true) check(false,true)
end) end)
it('works with utf_sizes and unicode text', function() local function check_unicode(verify)
local unicode_text = {"ascii text", local unicode_text = {"ascii text",
"latin text åäö", "latin text åäö",
"BMP text ɧ αλφά", "BMP text ɧ αλφά",
"BMP text 汉语 ↥↧", "BMP text 汉语 ↥↧",
"SMP 🤦 🦄🦃", "SMP 🤦 🦄🦃",
"combining å بِيَّة"} "combining å بِيَّة"}
meths.buf_set_lines(0, 0, -1, true, unicode_text) local check_events, verify_name = setup_eventcheck(verify, true, unicode_text)
feed('gg')
exec_lua("return test_register(...)", 0, "test1", false, true)
local tick = meths.buf_get_changedtick(0) local tick = meths.buf_get_changedtick(0)
feed('dd') feed('ggdd')
tick = tick + 1 tick = tick + 1
eq({{ "test1", "lines", 1, tick, 0, 1, 0, 11, 11, 11 }}, exec_lua("return get_events(...)" )) check_events {{ "test1", "lines", 1, tick, 0, 1, 0, 11, 11, 11 }}
feed('A<bs>') feed('A<bs>')
tick = tick + 1 tick = tick + 1
eq({{ "test1", "lines", 1, tick, 0, 1, 1, 18, 15, 15 }}, exec_lua("return get_events(...)" )) check_events {{ "test1", "lines", 1, tick, 0, 1, 1, 18, 15, 15 }}
feed('<esc>jylp') feed('<esc>jylp')
tick = tick + 1 tick = tick + 1
eq({{ "test1", "lines", 1, tick, 1, 2, 2, 21, 16, 16 }}, exec_lua("return get_events(...)" )) check_events {{ "test1", "lines", 1, tick, 1, 2, 2, 21, 16, 16 }}
feed('+eea<cr>') feed('+eea<cr>')
tick = tick + 1 tick = tick + 1
eq({{ "test1", "lines", 1, tick, 2, 3, 4, 23, 15, 15 }}, exec_lua("return get_events(...)" )) check_events {{ "test1", "lines", 1, tick, 2, 3, 4, 23, 15, 15 }}
feed('<esc>jdw') feed('<esc>jdw')
tick = tick + 1 tick = tick + 1
-- non-BMP chars count as 2 UTF-2 codeunits -- non-BMP chars count as 2 UTF-2 codeunits
eq({{ "test1", "lines", 1, tick, 4, 5, 5, 18, 9, 12 }}, exec_lua("return get_events(...)" )) check_events {{ "test1", "lines", 1, tick, 4, 5, 5, 18, 9, 12 }}
feed('+rx') feed('+rx')
tick = tick + 1 tick = tick + 1
-- count the individual codepoints of a composed character. -- count the individual codepoints of a composed character.
eq({{ "test1", "lines", 1, tick, 5, 6, 6, 27, 20, 20 }}, exec_lua("return get_events(...)" )) check_events {{ "test1", "lines", 1, tick, 5, 6, 6, 27, 20, 20 }}
feed('kJ') feed('kJ')
tick = tick + 1 tick = tick + 1
-- verification fails with multiple line updates, sorry about that
verify_name ""
-- NB: this is inefficient (but not really wrong). -- NB: this is inefficient (but not really wrong).
eq({{ "test1", "lines", 1, tick, 4, 5, 5, 14, 5, 8 }, check_events {
{ "test1", "lines", 1, tick+1, 5, 6, 5, 27, 20, 20 }}, exec_lua("return get_events(...)" )) { "test1", "lines", 1, tick, 4, 5, 5, 14, 5, 8 };
{ "test1", "lines", 1, tick+1, 5, 6, 5, 27, 20, 20 };
}
end
it('works with utf_sizes and unicode text', function()
check_unicode(false)
end) end)
it('works with utf_sizes and unicode text with verify', function()
check_unicode(true)
end)
it('has valid cursor position while shifting', function() it('has valid cursor position while shifting', function()
meths.buf_set_lines(0, 0, -1, true, {'line1'}) meths.buf_set_lines(0, 0, -1, true, {'line1'})
exec_lua([[ exec_lua([[
@@ -272,11 +289,6 @@ describe('lua buffer event callbacks: on_lines', function()
end) end)
describe('lua: nvim_buf_attach on_bytes', function() describe('lua: nvim_buf_attach on_bytes', function()
before_each(function()
clear()
attach_buffer('on_bytes')
end)
-- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot -- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot
-- assert the wrong thing), but masks errors with unflushed lines (as -- assert the wrong thing), but masks errors with unflushed lines (as
-- nvim_buf_get_offset forces a flush of the memline). To be safe run the -- nvim_buf_get_offset forces a flush of the memline). To be safe run the
@@ -291,7 +303,7 @@ describe('lua: nvim_buf_attach on_bytes', function()
local len = meths.buf_get_offset(0, meths.buf_line_count(0)) local len = meths.buf_get_offset(0, meths.buf_line_count(0))
eq(len == -1 and 1 or len, string.len(shadowbytes)) eq(len == -1 and 1 or len, string.len(shadowbytes))
end end
exec_lua("return test_register(...)", 0, "test1", false, false, true) exec_lua("return test_register(...)", 0, "on_bytes", "test1", false, false, true)
meths.buf_get_changedtick(0) meths.buf_get_changedtick(0)
local verify_name = "test1" local verify_name = "test1"
@@ -504,11 +516,13 @@ describe('lua: nvim_buf_attach on_bytes', function()
feed ':%s/bcd/' feed ':%s/bcd/'
check_events { check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 3, 3, 0, 0, 0 }; { "test1", "bytes", 1, 3, 0, 1, 1, 0, 3, 3, 0, 0, 0 };
{ "test1", "bytes", 1, 5, 0, 1, 1, 0, 0, 0, 0, 3, 3 };
} }
feed 'a' feed 'a'
check_events { check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 3, 3, 0, 1, 1 }; { "test1", "bytes", 1, 3, 0, 1, 1, 0, 3, 3, 0, 1, 1 };
{ "test1", "bytes", 1, 5, 0, 1, 1, 0, 1, 1, 0, 3, 3 };
} }
end) end)