2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
local clear = n.clear
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq, ok = t.eq, t.ok
|
2024-04-20 08:44:13 -07:00
|
|
|
local fn = n.fn
|
|
|
|
local api = n.api
|
|
|
|
local command, eval, next_msg = n.command, n.eval, n.next_msg
|
|
|
|
local nvim_prog = n.nvim_prog
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
2024-01-12 04:41:09 -07:00
|
|
|
local sleep = vim.uv.sleep
|
2024-04-08 02:03:20 -07:00
|
|
|
local write_file = t.write_file
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
local origlines = {
|
|
|
|
'original line 1',
|
|
|
|
'original line 2',
|
|
|
|
'original line 3',
|
|
|
|
'original line 4',
|
|
|
|
'original line 5',
|
|
|
|
'original line 6',
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:25:53 -07:00
|
|
|
local function expectn(name, args)
|
|
|
|
-- expect the next message to be the specified notification event
|
2018-04-24 12:41:59 -07:00
|
|
|
eq({ 'notification', name, args }, next_msg())
|
2018-01-26 12:25:53 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function sendkeys(keys)
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_input(keys)
|
2024-09-26 03:34:35 -07:00
|
|
|
-- Wait for Nvim to fully process pending input before possibly sending
|
2018-01-26 12:38:05 -07:00
|
|
|
-- more key presses - otherwise they all pile up in the queue and get
|
|
|
|
-- processed at once
|
2024-09-26 03:34:35 -07:00
|
|
|
n.poke_eventloop()
|
2018-01-26 12:38:05 -07:00
|
|
|
end
|
|
|
|
|
2018-01-26 12:25:53 -07:00
|
|
|
local function open(activate, lines)
|
2024-04-08 02:03:20 -07:00
|
|
|
local filename = t.tmpname()
|
2018-06-08 01:13:04 -07:00
|
|
|
write_file(filename, table.concat(lines, '\n') .. '\n', true)
|
2018-01-26 12:38:05 -07:00
|
|
|
command('edit ' .. filename)
|
2024-01-12 10:59:57 -07:00
|
|
|
local b = api.nvim_get_current_buf()
|
2018-01-26 12:38:05 -07:00
|
|
|
-- what is the value of b:changedtick?
|
|
|
|
local tick = eval('b:changedtick')
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Enable buffer events, ensure that the nvim_buf_lines_event messages
|
|
|
|
-- arrive as expected
|
2018-01-26 12:38:05 -07:00
|
|
|
if activate then
|
2018-05-10 06:47:00 -07:00
|
|
|
local firstline = 0
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, firstline, -1, lines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return b, tick, filename
|
|
|
|
end
|
|
|
|
|
2018-01-26 12:25:53 -07:00
|
|
|
local function editoriginal(activate, lines)
|
|
|
|
if not lines then
|
|
|
|
lines = origlines
|
|
|
|
end
|
|
|
|
-- load up the file with the correct contents
|
2018-07-01 03:42:19 -07:00
|
|
|
clear()
|
2018-01-26 12:25:53 -07:00
|
|
|
return open(activate, lines)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function reopen(buf, expectedlines)
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(api.nvim_buf_detach(buf))
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { buf })
|
2018-01-26 12:38:05 -07:00
|
|
|
-- for some reason the :edit! increments tick by 2
|
|
|
|
command('edit!')
|
|
|
|
local tick = eval('b:changedtick')
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(api.nvim_buf_attach(buf, true, {}))
|
2018-05-10 06:47:00 -07:00
|
|
|
local firstline = 0
|
|
|
|
expectn('nvim_buf_lines_event', { buf, tick, firstline, -1, expectedlines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! gg')
|
|
|
|
return tick
|
|
|
|
end
|
|
|
|
|
2018-01-26 12:25:53 -07:00
|
|
|
local function reopenwithfolds(b)
|
2018-01-26 12:38:05 -07:00
|
|
|
-- discard any changes to the buffer
|
|
|
|
local tick = reopen(b, origlines)
|
|
|
|
|
|
|
|
-- use markers for folds, make all folds open by default
|
2023-04-02 08:01:48 -07:00
|
|
|
command('setlocal foldmethod=marker foldlevel=20 commentstring=/*%s*/')
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add a fold
|
|
|
|
command('2,4fold')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', {
|
|
|
|
b,
|
|
|
|
tick,
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
{
|
|
|
|
'original line 2/*{{{*/',
|
2018-01-26 12:38:05 -07:00
|
|
|
'original line 3',
|
2018-05-10 06:47:00 -07:00
|
|
|
'original line 4/*}}}*/',
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
})
|
2018-01-26 12:38:05 -07:00
|
|
|
-- make a new fold that wraps lines 1-6
|
|
|
|
command('1,6fold')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', {
|
|
|
|
b,
|
|
|
|
tick,
|
|
|
|
0,
|
|
|
|
6,
|
|
|
|
{
|
|
|
|
'original line 1/*{{{*/',
|
2018-01-26 12:38:05 -07:00
|
|
|
'original line 2/*{{{*/',
|
|
|
|
'original line 3',
|
|
|
|
'original line 4/*}}}*/',
|
|
|
|
'original line 5',
|
2018-05-10 06:47:00 -07:00
|
|
|
'original line 6/*}}}*/',
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
})
|
2018-01-26 12:38:05 -07:00
|
|
|
return tick
|
|
|
|
end
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
describe('API: buffer events:', function()
|
2022-06-13 04:05:17 -07:00
|
|
|
before_each(clear)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when lines are added', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true)
|
|
|
|
|
|
|
|
-- add a new line at the start of the buffer
|
|
|
|
command('normal! GyyggP')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 0, { 'original line 6' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add multiple lines at the start of the file
|
|
|
|
command('normal! GkkyGggP')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 0, 0, { 'original line 4', 'original line 5', 'original line 6' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add one line to the middle of the file, several times
|
|
|
|
command('normal! ggYjjp')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 3, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! p')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 4, 4, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! p')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 5, 5, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add multiple lines to the middle of the file
|
|
|
|
command('normal! gg4Yjjp')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', {
|
|
|
|
b,
|
|
|
|
tick,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
{
|
|
|
|
'original line 4',
|
2018-01-26 12:38:05 -07:00
|
|
|
'original line 5',
|
|
|
|
'original line 6',
|
2018-05-10 06:47:00 -07:00
|
|
|
'original line 4',
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
})
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add one line to the end of the file
|
|
|
|
command('normal! ggYGp')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 17, 17, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add one line to the end of the file, several times
|
|
|
|
command('normal! ggYGppp')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 18, 18, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 19, 19, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 20, 20, { 'original line 4' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add several lines to the end of the file, several times
|
|
|
|
command('normal! gg4YGp')
|
|
|
|
command('normal! Gp')
|
|
|
|
command('normal! Gp')
|
2018-01-26 12:25:53 -07:00
|
|
|
local firstfour = { 'original line 4', 'original line 5', 'original line 6', 'original line 4' }
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 21, 21, firstfour, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 25, 25, firstfour, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 29, 29, firstfour, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2021-08-17 20:30:58 -07:00
|
|
|
-- delete the current buffer to turn off buffer events
|
|
|
|
command('bdelete!')
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add a line at the start of an empty file
|
|
|
|
command('enew')
|
2018-01-26 12:25:53 -07:00
|
|
|
tick = eval('b:changedtick')
|
2024-01-12 10:59:57 -07:00
|
|
|
local b2 = api.nvim_get_current_buf()
|
|
|
|
ok(api.nvim_buf_attach(b2, true, {}))
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b2, tick, 0, -1, { '' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('append(0, ["new line 1"])')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b2, tick, 0, 0, { 'new line 1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- turn off buffer events manually
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_buf_detach(b2)
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b2 })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- add multiple lines to a blank file
|
|
|
|
command('enew!')
|
2024-01-12 10:59:57 -07:00
|
|
|
local b3 = api.nvim_get_current_buf()
|
|
|
|
ok(api.nvim_buf_attach(b3, true, {}))
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = eval('b:changedtick')
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b3, tick, 0, -1, { '' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('append(0, ["new line 1", "new line 2", "new line 3"])')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b3, tick, 0, 0, { 'new line 1', 'new line 2', 'new line 3' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- use the API itself to add a line to the start of the buffer
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_buf_set_lines(b3, 0, 0, true, { 'New First Line' })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b3, tick, 0, 0, { 'New First Line' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when lines are removed', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true)
|
|
|
|
|
|
|
|
-- remove one line from start of file
|
|
|
|
command('normal! dd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- remove multiple lines from the start of the file
|
|
|
|
command('normal! 4dd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 4, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- remove multiple lines from middle of file
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('normal! jj3dd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 2, 5, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- remove one line from the end of the file
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('normal! Gdd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 5, 6, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- remove multiple lines from the end of the file
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('normal! 4G3dd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 6, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- pretend to remove heaps lines from the end of the file but really
|
|
|
|
-- just remove two
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('normal! Gk5dd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 4, 6, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when text is changed', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true)
|
|
|
|
|
|
|
|
-- some normal text editing
|
|
|
|
command('normal! A555')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'original line 1555' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! jj8X')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'origin3' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- modify multiple lines at once using visual block mode
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('normal! jjw')
|
2018-02-12 14:49:35 -07:00
|
|
|
sendkeys('<C-v>jjllx')
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 2, 5, { 'original e 3', 'original e 4', 'original e 5' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- replace part of a line line using :s
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('3s/line 3/foo/')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'original foo' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- replace parts of several lines line using :s
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('%s/line [35]/foo/')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 2, 5, { 'original foo', 'original line 4', 'original foo' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- type text into the first line of a blank file, one character at a time
|
2021-08-17 20:30:58 -07:00
|
|
|
command('bdelete!')
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = 2
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b })
|
2024-01-12 10:59:57 -07:00
|
|
|
local bnew = api.nvim_get_current_buf()
|
|
|
|
ok(api.nvim_buf_attach(bnew, true, {}))
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick, 0, -1, { '' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
sendkeys('i')
|
|
|
|
sendkeys('h')
|
|
|
|
sendkeys('e')
|
|
|
|
sendkeys('l')
|
|
|
|
sendkeys('l')
|
|
|
|
sendkeys('o\nworld')
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 1, 0, 1, { 'h' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 2, 0, 1, { 'he' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 3, 0, 1, { 'hel' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 4, 0, 1, { 'hell' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 5, 0, 1, { 'hello' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 6, 0, 1, { 'hello', '' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { bnew, tick + 7, 1, 2, { 'world' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when lines are replaced', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true)
|
|
|
|
|
|
|
|
-- blast away parts of some lines with visual mode
|
|
|
|
command('normal! jjwvjjllx')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'original ' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 4, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 4, { 'e 5' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'original e 5' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 4, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- blast away a few lines using :g
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('global/line [35]/delete')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 2, 3, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 4, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2023-12-25 05:06:00 -07:00
|
|
|
it('visual paste split empty line', function()
|
|
|
|
local b, tick = editoriginal(true, { 'abc', '{', 'def', '}' })
|
|
|
|
command('normal! ggyyjjvi{p')
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick + 1, 2, 3, { '' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick + 2, 2, 3, { '}' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick + 3, 3, 4, {}, false })
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick + 3, 2, 3, { '' }, false })
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick + 4, 3, 3, { 'abc', '}' }, false })
|
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when lines are filtered', function()
|
2018-01-28 06:53:38 -07:00
|
|
|
-- Test filtering lines with !cat
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true, { 'A', 'C', 'E', 'B', 'D', 'F' })
|
|
|
|
|
2018-01-28 06:53:38 -07:00
|
|
|
command('silent 2,5!cat')
|
2018-01-26 12:38:05 -07:00
|
|
|
-- the change comes through as two changes:
|
|
|
|
-- 1) addition of the new lines after the filtered lines
|
|
|
|
-- 2) removal of the original lines
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 5, 5, { 'C', 'E', 'B', 'D' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 5, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when you use "o"', function()
|
2018-01-26 12:25:53 -07:00
|
|
|
local b, tick = editoriginal(true, { 'AAA', 'BBB' })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('set noautoindent nosmartindent')
|
|
|
|
|
|
|
|
-- use 'o' to start a new line from a line with no indent
|
|
|
|
command('normal! o')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 1, { '' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- undo the change, indent line 1 a bit, and try again
|
|
|
|
command('undo')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 2, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('set autoindent')
|
|
|
|
command('normal! >>')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { '\tAAA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! ommm')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 1, { '\t' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 2, { '\tmmm' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- undo the change, and try again with 'O'
|
|
|
|
command('undo')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 2, { '\t' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 2, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! ggOmmm')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 0, { '\t' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { '\tmmm' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('deactivates if the buffer is changed externally', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
-- Test changing file from outside vim and reloading using :edit
|
|
|
|
local lines = { 'Line 1', 'Line 2' }
|
|
|
|
local b, tick, filename = editoriginal(true, lines)
|
|
|
|
|
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'ine 1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'Line 1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- change the file directly
|
2018-06-08 01:13:04 -07:00
|
|
|
write_file(filename, 'another line\n', true, true)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- reopen the file and watch buffer events shut down
|
2018-01-26 12:38:05 -07:00
|
|
|
command('edit')
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('channel can watch many buffers at once', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
-- edit 3 buffers, make sure they all have windows visible so that when we
|
|
|
|
-- move between buffers, none of them are unloaded
|
2018-01-26 12:25:53 -07:00
|
|
|
local b1, tick1 = editoriginal(true, { 'A1', 'A2' })
|
|
|
|
local b1nr = eval('bufnr("")')
|
2018-01-26 12:38:05 -07:00
|
|
|
command('split')
|
2018-01-26 12:25:53 -07:00
|
|
|
local b2, tick2 = open(true, { 'B1', 'B2' })
|
|
|
|
local b2nr = eval('bufnr("")')
|
2018-01-26 12:38:05 -07:00
|
|
|
command('split')
|
2018-01-26 12:25:53 -07:00
|
|
|
local b3, tick3 = open(true, { 'C1', 'C2' })
|
|
|
|
local b3nr = eval('bufnr("")')
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- make a new window for moving between buffers
|
|
|
|
command('split')
|
|
|
|
|
|
|
|
command('b' .. b1nr)
|
|
|
|
command('normal! x')
|
|
|
|
tick1 = tick1 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b1, tick1, 0, 1, { '1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick1 = tick1 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b1, tick1, 0, 1, { 'A1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick1 = tick1 + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b1, tick1 })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
command('b' .. b2nr)
|
|
|
|
command('normal! x')
|
|
|
|
tick2 = tick2 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b2, tick2, 0, 1, { '1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick2 = tick2 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b2, tick2, 0, 1, { 'B1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick2 = tick2 + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b2, tick2 })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
command('b' .. b3nr)
|
|
|
|
command('normal! x')
|
|
|
|
tick3 = tick3 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b3, tick3, 0, 1, { '1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick3 = tick3 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b3, tick3, 0, 1, { 'C1' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick3 = tick3 + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b3, tick3 })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-07-01 03:42:19 -07:00
|
|
|
it('does not get confused if enabled/disabled many times', function()
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel = api.nvim_get_chan_info(0).id
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(false)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Enable buffer events many times.
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, -1, origlines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('rpcnotify(' .. channel .. ', "Hello There")')
|
|
|
|
expectn('Hello There', {})
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Disable buffer events many times.
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(api.nvim_buf_detach(b))
|
|
|
|
ok(api.nvim_buf_detach(b))
|
|
|
|
ok(api.nvim_buf_detach(b))
|
|
|
|
ok(api.nvim_buf_detach(b))
|
|
|
|
ok(api.nvim_buf_detach(b))
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('rpcnotify(' .. channel .. ', "Hello Again")')
|
|
|
|
expectn('Hello Again', {})
|
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('can notify several channels at once', function()
|
2018-07-01 03:42:19 -07:00
|
|
|
clear()
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- create several new sessions, in addition to our main API
|
2018-01-26 12:25:53 -07:00
|
|
|
local sessions = {}
|
2024-04-20 08:44:13 -07:00
|
|
|
local pipe = n.new_pipename()
|
2018-01-28 06:53:38 -07:00
|
|
|
eval("serverstart('" .. pipe .. "')")
|
2024-04-20 08:44:13 -07:00
|
|
|
sessions[1] = n.connect(pipe)
|
|
|
|
sessions[2] = n.connect(pipe)
|
|
|
|
sessions[3] = n.connect(pipe)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-01-26 12:25:53 -07:00
|
|
|
local function request(sessionnr, method, ...)
|
2018-01-26 12:38:05 -07:00
|
|
|
local status, rv = sessions[sessionnr]:request(method, ...)
|
|
|
|
if not status then
|
|
|
|
error(rv[2])
|
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2018-01-26 12:25:53 -07:00
|
|
|
local function wantn(sessionid, name, args)
|
2018-01-26 12:38:05 -07:00
|
|
|
local session = sessions[sessionid]
|
2018-07-01 07:35:35 -07:00
|
|
|
eq({ 'notification', name, args }, session:next_message(10000))
|
2018-01-26 12:38:05 -07:00
|
|
|
end
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Edit a new file, but don't enable buffer events.
|
2018-01-26 12:38:05 -07:00
|
|
|
local lines = { 'AAA', 'BBB' }
|
|
|
|
local b, tick = open(false, lines)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Enable buffer events for sessions 1, 2 and 3.
|
2018-05-21 11:42:59 -07:00
|
|
|
ok(request(1, 'nvim_buf_attach', b, true, {}))
|
|
|
|
ok(request(2, 'nvim_buf_attach', b, true, {}))
|
|
|
|
ok(request(3, 'nvim_buf_attach', b, true, {}))
|
2018-05-10 06:47:00 -07:00
|
|
|
wantn(1, 'nvim_buf_lines_event', { b, tick, 0, -1, lines, false })
|
|
|
|
wantn(2, 'nvim_buf_lines_event', { b, tick, 0, -1, lines, false })
|
|
|
|
wantn(3, 'nvim_buf_lines_event', { b, tick, 0, -1, lines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Change the buffer.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
wantn(1, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
|
|
|
wantn(2, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
|
|
|
wantn(3, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Stop watching on channel 1.
|
2018-02-11 14:51:29 -07:00
|
|
|
ok(request(1, 'nvim_buf_detach', b))
|
2018-05-18 01:09:11 -07:00
|
|
|
wantn(1, 'nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Undo the change to buffer 1.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
wantn(2, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
|
|
|
|
wantn(3, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
wantn(2, 'nvim_buf_changedtick_event', { b, tick })
|
|
|
|
wantn(3, 'nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-05-10 06:47:00 -07:00
|
|
|
-- make sure there are no other pending nvim_buf_lines_event messages going to
|
2018-01-26 12:38:05 -07:00
|
|
|
-- channel 1
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel1 = request(1, 'nvim_get_chan_info', 0).id
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('rpcnotify(' .. channel1 .. ', "Hello")')
|
|
|
|
wantn(1, 'Hello', {})
|
|
|
|
|
2018-05-18 01:09:11 -07:00
|
|
|
-- close the buffer and channels 2 and 3 should get a nvim_buf_detach_event
|
2018-01-26 12:38:05 -07:00
|
|
|
-- notification
|
|
|
|
command('edit')
|
2018-05-18 01:09:11 -07:00
|
|
|
wantn(2, 'nvim_buf_detach_event', { b })
|
|
|
|
wantn(3, 'nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-05-10 06:47:00 -07:00
|
|
|
-- make sure there are no other pending nvim_buf_lines_event messages going to
|
2018-01-26 12:38:05 -07:00
|
|
|
-- channel 1
|
2024-02-12 06:50:39 -07:00
|
|
|
channel1 = request(1, 'nvim_get_chan_info', 0).id
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('rpcnotify(' .. channel1 .. ', "Hello Again")')
|
|
|
|
wantn(1, 'Hello Again', {})
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with :diffput and :diffget', function()
|
|
|
|
local b1, tick1 = editoriginal(true, { 'AAA', 'BBB' })
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel = api.nvim_get_chan_info(0).id
|
2018-01-26 12:38:05 -07:00
|
|
|
command('diffthis')
|
|
|
|
command('rightbelow vsplit')
|
|
|
|
local b2, tick2 = open(true, { 'BBB', 'CCC' })
|
|
|
|
command('diffthis')
|
|
|
|
-- go back to first buffer, and push the 'AAA' line to the second buffer
|
|
|
|
command('1wincmd w')
|
|
|
|
command('normal! gg')
|
|
|
|
command('diffput')
|
|
|
|
tick2 = tick2 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b2, tick2, 0, 0, { 'AAA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- use :diffget to grab the other change from buffer 2
|
|
|
|
command('normal! G')
|
|
|
|
command('diffget')
|
|
|
|
tick1 = tick1 + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b1, tick1, 2, 2, { 'CCC' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
eval('rpcnotify(' .. channel .. ', "Goodbye")')
|
|
|
|
expectn('Goodbye', {})
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with :sort', function()
|
|
|
|
-- test for :sort
|
|
|
|
local b, tick = editoriginal(true, { 'B', 'D', 'C', 'A', 'E' })
|
|
|
|
command('%sort')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 5, { 'A', 'B', 'C', 'D', 'E' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with :left', function()
|
|
|
|
local b, tick = editoriginal(true, { ' A', ' B', 'B', '\tB', '\t\tC' })
|
|
|
|
command('2,4left')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 4, { 'B', 'B', 'B' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with :right', function()
|
|
|
|
local b, tick = editoriginal(true, { ' A', '\t B', '\t \tBB', ' \tB', '\t\tC' })
|
|
|
|
command('set ts=2 et')
|
|
|
|
command('2,4retab')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 4, { ' B', ' BB', ' B' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with :move', function()
|
|
|
|
local b, tick = editoriginal(true, origlines)
|
|
|
|
-- move text down towards the end of the file
|
|
|
|
command('2,3move 4')
|
|
|
|
tick = tick + 2
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 4, 4, { 'original line 2', 'original line 3' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 1, 3, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- move text up towards the start of the file
|
|
|
|
tick = reopen(b, origlines)
|
|
|
|
command('4,5move 2')
|
|
|
|
tick = tick + 2
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 2, 2, { 'original line 4', 'original line 5' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 5, 7, {}, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('when you manually add/remove folds', function()
|
2018-01-26 12:25:53 -07:00
|
|
|
local b = editoriginal(true)
|
|
|
|
local tick = reopenwithfolds(b)
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- delete the inner fold
|
|
|
|
command('normal! zR3Gzd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 1, 4, { 'original line 2', 'original line 3', 'original line 4' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
-- delete the outer fold
|
|
|
|
command('normal! zd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 6, origlines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- discard changes and put the folds back
|
|
|
|
tick = reopenwithfolds(b)
|
|
|
|
|
|
|
|
-- remove both folds at once
|
|
|
|
command('normal! ggzczD')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 6, origlines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- discard changes and put the folds back
|
|
|
|
tick = reopenwithfolds(b)
|
|
|
|
|
|
|
|
-- now delete all folds at once
|
|
|
|
command('normal! zE')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 6, origlines, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- create a fold from line 4 to the end of the file
|
|
|
|
command('normal! 4GA/*{{{*/')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 3, 4, { 'original line 4/*{{{*/' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- delete the fold which only has one marker
|
|
|
|
command('normal! Gzd')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn(
|
|
|
|
'nvim_buf_lines_event',
|
|
|
|
{ b, tick, 3, 6, { 'original line 4', 'original line 5', 'original line 6' }, false }
|
|
|
|
)
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('detaches if the buffer is closed', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true, { 'AAA' })
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel = api.nvim_get_chan_info(0).id
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Test that buffer events are working.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2021-08-17 20:30:58 -07:00
|
|
|
-- close our buffer and create a new one
|
|
|
|
command('bdelete')
|
2018-01-26 12:38:05 -07:00
|
|
|
command('enew')
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Reopen the original buffer, make sure there are no buffer events sent.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('b1')
|
|
|
|
command('normal! x')
|
|
|
|
|
|
|
|
eval('rpcnotify(' .. channel .. ', "Hello There")')
|
|
|
|
expectn('Hello There', {})
|
|
|
|
end)
|
2018-01-26 12:25:53 -07:00
|
|
|
|
2019-02-24 06:17:30 -07:00
|
|
|
it(':edit! (reload) causes detach #9642', function()
|
|
|
|
local b, tick = editoriginal(true, { 'AAA', 'BBB' })
|
|
|
|
command('set undoreload=1')
|
|
|
|
|
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
|
|
|
|
|
|
|
command('edit!')
|
|
|
|
expectn('nvim_buf_detach_event', { b })
|
|
|
|
end)
|
|
|
|
|
|
|
|
it(':enew! does not detach hidden buffer', function()
|
|
|
|
local b, tick = editoriginal(true, { 'AAA', 'BBB' })
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel = api.nvim_get_chan_info(0).id
|
2019-02-24 06:17:30 -07:00
|
|
|
|
|
|
|
command('set undoreload=1 hidden')
|
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
|
|
|
|
|
|
|
command('enew!')
|
|
|
|
eval('rpcnotify(' .. channel .. ', "Hello There")')
|
|
|
|
expectn('Hello There', {})
|
|
|
|
end)
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('stays attached if the buffer is hidden', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
local b, tick = editoriginal(true, { 'AAA' })
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel = api.nvim_get_chan_info(0).id
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Test that buffer events are working.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
command('undo')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
tick = tick + 1
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Close our buffer by creating a new one.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('set hidden')
|
|
|
|
command('enew')
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Assert that no nvim_buf_detach_event is sent.
|
2018-01-26 12:38:05 -07:00
|
|
|
eval('rpcnotify(' .. channel .. ', "Hello There")')
|
|
|
|
expectn('Hello There', {})
|
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
-- Reopen the original buffer, assert that buffer events are still active.
|
2018-01-26 12:38:05 -07:00
|
|
|
command('b1')
|
|
|
|
command('normal! x')
|
|
|
|
tick = tick + 1
|
2018-05-10 06:47:00 -07:00
|
|
|
expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
|
|
|
|
2018-07-01 03:42:19 -07:00
|
|
|
it('detaches if the buffer is unloaded/deleted/wiped', function()
|
2018-01-26 12:38:05 -07:00
|
|
|
-- start with a blank nvim
|
2018-07-01 03:42:19 -07:00
|
|
|
clear()
|
2018-01-26 12:38:05 -07:00
|
|
|
-- need to make a new window with a buffer because :bunload doesn't let you
|
|
|
|
-- unload the last buffer
|
2018-01-26 12:25:53 -07:00
|
|
|
for _, cmd in ipairs({ 'bunload', 'bdelete', 'bwipeout' }) do
|
2018-01-26 12:38:05 -07:00
|
|
|
command('new')
|
|
|
|
-- open a brand spanking new file
|
2018-01-26 12:25:53 -07:00
|
|
|
local b = open(true, { 'AAA' })
|
2018-01-26 12:38:05 -07:00
|
|
|
|
|
|
|
-- call :bunload or whatever the command is, and then check that we
|
2018-05-18 01:09:11 -07:00
|
|
|
-- receive a nvim_buf_detach_event
|
2018-01-26 12:38:05 -07:00
|
|
|
command(cmd)
|
2018-05-18 01:09:11 -07:00
|
|
|
expectn('nvim_buf_detach_event', { b })
|
2018-01-26 12:38:05 -07:00
|
|
|
end
|
|
|
|
end)
|
2018-01-26 07:49:35 -07:00
|
|
|
|
2018-06-08 01:13:04 -07:00
|
|
|
it('does not send the buffer content if not requested', function()
|
2018-07-01 03:42:19 -07:00
|
|
|
clear()
|
2018-01-26 07:49:35 -07:00
|
|
|
local b, tick = editoriginal(false)
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(api.nvim_buf_attach(b, false, {}))
|
2018-05-10 14:13:58 -07:00
|
|
|
expectn('nvim_buf_changedtick_event', { b, tick })
|
2018-01-26 07:49:35 -07:00
|
|
|
end)
|
|
|
|
|
2018-05-21 11:42:59 -07:00
|
|
|
it('returns a proper error on nonempty options dict', function()
|
2018-07-01 03:42:19 -07:00
|
|
|
clear()
|
2018-05-21 11:42:59 -07:00
|
|
|
local b = editoriginal(false)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq("Invalid key: 'builtin'", pcall_err(api.nvim_buf_attach, b, false, { builtin = 'asfd' }))
|
2018-05-21 11:42:59 -07:00
|
|
|
end)
|
|
|
|
|
2018-07-01 03:42:19 -07:00
|
|
|
it('nvim_buf_attach returns response after delay #8634', function()
|
|
|
|
clear()
|
2018-07-02 11:47:28 -07:00
|
|
|
sleep(250)
|
2018-07-01 03:42:19 -07:00
|
|
|
-- response
|
2024-04-20 08:44:13 -07:00
|
|
|
eq(true, n.request('nvim_buf_attach', 0, false, {}))
|
2018-07-01 03:42:19 -07:00
|
|
|
-- notification
|
|
|
|
eq({
|
|
|
|
[1] = 'notification',
|
|
|
|
[2] = 'nvim_buf_changedtick_event',
|
|
|
|
[3] = {
|
2024-01-16 06:26:21 -07:00
|
|
|
[1] = 1,
|
2018-07-01 03:42:19 -07:00
|
|
|
[2] = 2,
|
|
|
|
},
|
|
|
|
}, next_msg())
|
|
|
|
end)
|
2018-01-26 12:38:05 -07:00
|
|
|
end)
|
2018-07-01 05:40:51 -07:00
|
|
|
|
|
|
|
describe('API: buffer events:', function()
|
|
|
|
before_each(function()
|
2018-07-01 03:42:19 -07:00
|
|
|
clear()
|
2018-07-01 05:40:51 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
local function lines_subset(first, second)
|
|
|
|
for i = 1, #first do
|
2022-05-17 06:09:28 -07:00
|
|
|
-- need to ignore trailing spaces
|
|
|
|
if first[i]:gsub(' +$', '') ~= second[i]:gsub(' +$', '') then
|
2018-07-01 05:40:51 -07:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function lines_equal(f, s)
|
|
|
|
return lines_subset(f, s) and lines_subset(s, f)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function assert_match_somewhere(expected_lines, buffer_lines)
|
|
|
|
local msg = next_msg()
|
|
|
|
|
|
|
|
while msg ~= nil do
|
|
|
|
local event = msg[2]
|
|
|
|
if event == 'nvim_buf_lines_event' then
|
|
|
|
local args = msg[3]
|
|
|
|
local starts = args[3]
|
|
|
|
local newlines = args[5]
|
|
|
|
|
|
|
|
-- Size of the contained nvim instance is 23 lines, this might change
|
2022-11-28 18:45:48 -07:00
|
|
|
-- with the test setup. Note updates are contiguous.
|
2018-07-01 05:40:51 -07:00
|
|
|
assert(#newlines <= 23)
|
|
|
|
|
|
|
|
for i = 1, #newlines do
|
|
|
|
buffer_lines[starts + i] = newlines[i]
|
|
|
|
end
|
|
|
|
-- we don't compare the msg area of the embedded nvim, it's too flakey
|
|
|
|
buffer_lines[23] = nil
|
|
|
|
|
|
|
|
if lines_equal(buffer_lines, expected_lines) then
|
|
|
|
-- OK
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
msg = next_msg()
|
|
|
|
end
|
2022-03-03 01:07:39 -07:00
|
|
|
assert(false, 'did not match/receive expected nvim_buf_lines_event lines')
|
2018-07-01 05:40:51 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it('when :terminal lines change', function()
|
|
|
|
local buffer_lines = {}
|
|
|
|
local expected_lines = {}
|
2024-01-12 10:59:57 -07:00
|
|
|
fn.termopen({ nvim_prog, '-u', 'NONE', '-i', 'NONE', '-n', '-c', 'set shortmess+=A' }, {
|
2023-12-07 17:00:27 -07:00
|
|
|
env = { VIMRUNTIME = os.getenv('VIMRUNTIME') },
|
|
|
|
})
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local b = api.nvim_get_current_buf()
|
|
|
|
ok(api.nvim_buf_attach(b, true, {}))
|
2018-07-01 05:40:51 -07:00
|
|
|
|
|
|
|
for _ = 1, 22 do
|
|
|
|
table.insert(expected_lines, '~')
|
|
|
|
end
|
|
|
|
expected_lines[1] = ''
|
|
|
|
expected_lines[22] = ('tmp_terminal_nvim' .. (' '):rep(45) .. '0,0-1 All')
|
|
|
|
|
|
|
|
sendkeys('i:e tmp_terminal_nvim<Enter>')
|
|
|
|
assert_match_somewhere(expected_lines, buffer_lines)
|
|
|
|
|
|
|
|
expected_lines[1] = 'Blarg'
|
|
|
|
expected_lines[22] = ('tmp_terminal_nvim [+]' .. (' '):rep(41) .. '1,6 All')
|
|
|
|
|
|
|
|
sendkeys('iBlarg')
|
|
|
|
assert_match_somewhere(expected_lines, buffer_lines)
|
|
|
|
|
|
|
|
for i = 1, 21 do
|
|
|
|
expected_lines[i] = 'xyz'
|
|
|
|
end
|
|
|
|
expected_lines[22] = ('tmp_terminal_nvim [+]' .. (' '):rep(41) .. '31,4 Bot')
|
|
|
|
|
|
|
|
local s = string.rep('\nxyz', 30)
|
|
|
|
sendkeys(s)
|
|
|
|
assert_match_somewhere(expected_lines, buffer_lines)
|
|
|
|
end)
|
|
|
|
end)
|