aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/buffer_updates_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/lua/buffer_updates_spec.lua')
-rw-r--r--test/functional/lua/buffer_updates_spec.lua278
1 files changed, 213 insertions, 65 deletions
diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua
index 67dc5f5a16..7a6b5be8bc 100644
--- a/test/functional/lua/buffer_updates_spec.lua
+++ b/test/functional/lua/buffer_updates_spec.lua
@@ -1,5 +1,6 @@
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)
+local lfs = require('lfs')
local command = helpers.command
local meths = helpers.meths
@@ -9,8 +10,9 @@ local eq = helpers.eq
local fail = helpers.fail
local exec_lua = helpers.exec_lua
local feed = helpers.feed
-local deepcopy = helpers.deepcopy
local expect_events = helpers.expect_events
+local write_file = helpers.write_file
+local dedent = helpers.dedent
local origlines = {"original line 1",
"original line 2",
@@ -20,19 +22,20 @@ local origlines = {"original line 1",
"original line 6",
" indented line"}
-local function attach_buffer(evname)
- exec_lua([[
+before_each(function ()
+ clear()
+ exec_lua [[
local evname = ...
local events = {}
- function test_register(bufnr, id, changedtick, utf_sizes, preview)
+ function test_register(bufnr, evname, id, changedtick, utf_sizes, preview)
local function callback(...)
table.insert(events, {id, ...})
if test_unreg == id then
return true
end
end
- local opts = {[evname]=callback, on_detach=callback, utf_sizes=utf_sizes, preview=preview}
+ local opts = {[evname]=callback, on_detach=callback, on_reload=callback, utf_sizes=utf_sizes, preview=preview}
if changedtick then
opts.on_changedtick = callback
end
@@ -44,41 +47,30 @@ local function attach_buffer(evname)
events = {}
return ret_events
end
- ]], evname)
-end
+ ]]
+end)
describe('lua buffer event callbacks: on_lines', function()
- before_each(function()
- clear()
- attach_buffer('on_lines')
- 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 function setup_eventcheck(verify, utf_sizes, lines)
local lastsize
- meths.buf_set_lines(0, 0, -1, true, origlines)
+ meths.buf_set_lines(0, 0, -1, true, lines)
if verify then
lastsize = meths.buf_get_offset(0, meths.buf_line_count(0))
end
- exec_lua("return test_register(...)", 0, "test1",false,utf_sizes)
- local tick = meths.buf_get_changedtick(0)
-
+ exec_lua("return test_register(...)", 0, "on_lines", "test1",false,utf_sizes)
local verify_name = "test1"
+
local function check_events(expected)
local events = exec_lua("return get_events(...)" )
if utf_sizes then
-- this test case uses ASCII only, so sizes should be the same.
-- Unicode is tested below.
for _, event in ipairs(expected) do
- event[9] = event[8]
- event[10] = event[8]
+ event[9] = event[9] or event[8]
+ event[10] = event[10] or event[9]
end
end
- eq(expected, events)
+ expect_events(expected, events, "line updates")
if verify then
for _, event in ipairs(events) do
if event[1] == verify_name and event[2] == "lines" then
@@ -92,25 +84,38 @@ describe('lua buffer event callbacks: on_lines', function()
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('normal! GyyggP')
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"})
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
command('undo')
-- plugins can opt in to receive changedtick events, or choose
-- to only receive actual changes.
- check_events({{ "test1", "lines", 1, tick, 3, 4, 5, 13 },
- { "test2", "lines", 1, tick, 3, 4, 5, 13 },
- { "test2", "changedtick", 1, tick+1 } })
+ check_events {
+ { "test1", "lines", 1, tick, 3, 4, 5, 13 };
+ { "test2", "lines", 1, tick, 3, 4, 5, 13 };
+ { "test2", "changedtick", 1, tick+1 };
+ }
tick = tick + 1
-- simulate next callback returning true
@@ -121,38 +126,40 @@ describe('lua buffer event callbacks: on_lines', function()
-- plugins can opt in to receive changedtick events, or choose
-- to only receive actual changes.
- check_events({{ "test1", "lines", 1, tick, 6, 7, 9, 16 },
- { "test2", "lines", 1, tick, 6, 7, 9, 16 }})
+ check_events {
+ { "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"})
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')
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
feed('yz')
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>')
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')
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')
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!')
- check_events({{ "test2", "detach", 1 }})
+ check_events {{ "test2", "detach", 1 }}
end
it('works', function()
@@ -167,51 +174,63 @@ describe('lua buffer event callbacks: on_lines', function()
check(false,true)
end)
- it('works with utf_sizes and unicode text', function()
+ local function check_unicode(verify)
local unicode_text = {"ascii text",
"latin text åäö",
"BMP text ɧ αλφά",
"BMP text 汉语 ↥↧",
"SMP 🤦 🦄🦃",
"combining å بِيَّة"}
- meths.buf_set_lines(0, 0, -1, true, unicode_text)
- feed('gg')
- exec_lua("return test_register(...)", 0, "test1", false, true)
+ local check_events, verify_name = setup_eventcheck(verify, true, unicode_text)
+
local tick = meths.buf_get_changedtick(0)
- feed('dd')
+ feed('ggdd')
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>')
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')
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>')
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')
tick = tick + 1
-- 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')
tick = tick + 1
-- 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')
tick = tick + 1
+ -- verification fails with multiple line updates, sorry about that
+ verify_name ""
-- NB: this is inefficient (but not really wrong).
- eq({{ "test1", "lines", 1, tick, 4, 5, 5, 14, 5, 8 },
- { "test1", "lines", 1, tick+1, 5, 6, 5, 27, 20, 20 }}, exec_lua("return get_events(...)" ))
+ check_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)
+ it('works with utf_sizes and unicode text with verify', function()
+ check_unicode(true)
+ end)
+
+
it('has valid cursor position while shifting', function()
meths.buf_set_lines(0, 0, -1, true, {'line1'})
exec_lua([[
@@ -225,6 +244,14 @@ describe('lua buffer event callbacks: on_lines', function()
eq(1, meths.get_var('listener_cursor_line'))
end)
+ it('has valid cursor position while deleting lines', function()
+ meths.buf_set_lines(0, 0, -1, true, { "line_1", "line_2", "line_3", "line_4"})
+ meths.win_set_cursor(0, {2, 0})
+ eq(2, meths.win_get_cursor(0)[1])
+ meths.buf_set_lines(0, 0, -1, true, { "line_1", "line_2", "line_3"})
+ eq(2, meths.win_get_cursor(0)[1])
+ end)
+
it('does not SEGFAULT when calling win_findbuf in on_detach', function()
exec_lua[[
@@ -272,26 +299,24 @@ describe('lua buffer event callbacks: on_lines', function()
end)
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
-- 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 setup_eventcheck(verify, start_txt)
- meths.buf_set_lines(0, 0, -1, true, start_txt)
- local shadow = deepcopy(start_txt)
- local shadowbytes = table.concat(shadow, '\n') .. '\n'
+ if start_txt then
+ meths.buf_set_lines(0, 0, -1, true, start_txt)
+ else
+ start_txt = meths.buf_get_lines(0, 0, -1, true)
+ end
+ local shadowbytes = table.concat(start_txt, '\n') .. '\n'
-- TODO: while we are brewing the real strong coffe,
-- verify should check buf_get_offset after every check_events
if verify then
local len = meths.buf_get_offset(0, meths.buf_line_count(0))
eq(len == -1 and 1 or len, string.len(shadowbytes))
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)
local verify_name = "test1"
@@ -318,6 +343,8 @@ describe('lua: nvim_buf_attach on_bytes', function()
local unknown = string.rep('\255', new_byte)
local after = string.sub(shadowbytes, start_byte + old_byte + 1)
shadowbytes = before .. unknown .. after
+ elseif event[1] == verify_name and event[2] == "reload" then
+ shadowbytes = table.concat(meths.buf_get_lines(0, 0, -1, true), '\n') .. '\n'
end
end
@@ -495,20 +522,34 @@ describe('lua: nvim_buf_attach on_bytes', function()
end)
- it('inccomand=nosplit and substitute', function()
- if verify then pending("Verification can't be done when previewing") end
+ it("linewise paste", function()
+ local check_events = setup_eventcheck(verify, origlines)
+
+ feed'yyp'
+ check_events {
+ { "test1", "bytes", 1, 3, 1, 0, 16, 0, 0, 0, 1, 0, 16 };
+ }
+ feed'Gyyp'
+ check_events {
+ { "test1", "bytes", 1, 4, 8, 0, 130, 0, 0, 0, 1, 0, 18 };
+ }
+ end)
+
+ it('inccomand=nosplit and substitute', function()
local check_events = setup_eventcheck(verify, {"abcde"})
meths.set_option('inccommand', 'nosplit')
feed ':%s/bcd/'
check_events {
{ "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'
check_events {
{ "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)
@@ -575,6 +616,113 @@ describe('lua: nvim_buf_attach on_bytes', function()
"original line 5", "original line 6" },
meths.buf_get_lines(0, 0, -1, true))
end)
+
+ it('checktime autoread', function()
+ write_file("Xtest-reload", dedent [[
+ old line 1
+ old line 2]])
+ lfs.touch("Xtest-reload", os.time() - 10)
+ command "e Xtest-reload"
+ command "set autoread"
+
+ local check_events = setup_eventcheck(verify, nil)
+
+ write_file("Xtest-reload", dedent [[
+ new line 1
+ new line 2
+ new line 3]])
+
+ command "checktime"
+ check_events {
+ { "test1", "reload", 1 };
+ }
+
+ feed 'ggJ'
+ check_events {
+ { "test1", "bytes", 1, 5, 0, 10, 10, 1, 0, 1, 0, 1, 1 };
+ }
+
+ eq({'new line 1 new line 2', 'new line 3'}, meths.buf_get_lines(0, 0, -1, true))
+
+ -- check we can undo and redo a reload event.
+ feed 'u'
+ check_events {
+ { "test1", "bytes", 1, 8, 0, 10, 10, 0, 1, 1, 1, 0, 1 };
+ }
+
+ feed 'u'
+ check_events {
+ { "test1", "reload", 1 };
+ }
+
+ feed '<c-r>'
+ check_events {
+ { "test1", "reload", 1 };
+ }
+
+ feed '<c-r>'
+ check_events {
+ { "test1", "bytes", 1, 14, 0, 10, 10, 1, 0, 1, 0, 1, 1 };
+ }
+ end)
+
+ it("sends events when undoing with undofile", function()
+ write_file("Xtest-undofile", dedent([[
+ 12345
+ hello world
+ ]]))
+
+ command("e! Xtest-undofile")
+ command("set undodir=. | set undofile")
+
+ local ns = helpers.request('nvim_create_namespace', "ns1")
+ meths.buf_set_extmark(0, ns, 0, 0, {})
+
+ eq({"12345", "hello world"}, meths.buf_get_lines(0, 0, -1, true))
+
+ -- splice
+ feed("gg0d2l")
+
+ eq({"345", "hello world"}, meths.buf_get_lines(0, 0, -1, true))
+
+ -- move
+ command(".m+1")
+
+ eq({"hello world", "345"}, meths.buf_get_lines(0, 0, -1, true))
+
+ -- reload undofile and undo changes
+ command("w")
+ command("set noundofile")
+ command("bw!")
+ command("e! Xtest-undofile")
+
+ command("set undofile")
+
+ local check_events = setup_eventcheck(verify, nil)
+
+ feed("u")
+ eq({"345", "hello world"}, meths.buf_get_lines(0, 0, -1, true))
+
+ check_events {
+ { "test1", "bytes", 2, 6, 1, 0, 12, 1, 0, 4, 0, 0, 0 },
+ { "test1", "bytes", 2, 6, 0, 0, 0, 0, 0, 0, 1, 0, 4 }
+ }
+
+ feed("u")
+ eq({"12345", "hello world"}, meths.buf_get_lines(0, 0, -1, true))
+
+ check_events {
+ { "test1", "bytes", 2, 8, 0, 0, 0, 0, 0, 0, 0, 2, 2 }
+ }
+ command("bw!")
+ end)
+
+
+ teardown(function()
+ os.remove "Xtest-reload"
+ os.remove "Xtest-undofile"
+ os.remove ".Xtest-undofile.un~"
+ end)
end
describe('(with verify) handles', function()