robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
30.3 KiB942 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local eq, ok = t.eq, t.ok
7local fn = n.fn
8local api = n.api
9local command, eval, next_msg = n.command, n.eval, n.next_msg
10local nvim_prog = n.nvim_prog
11local pcall_err = t.pcall_err
12local sleep = vim.uv.sleep
13local write_file = t.write_file
14
15local origlines = {
16  'original line 1',
17  'original line 2',
18  'original line 3',
19  'original line 4',
20  'original line 5',
21  'original line 6',
22}
23
24local function expectn(name, args)
25  -- expect the next message to be the specified notification event
26  eq({ 'notification', name, args }, next_msg())
27end
28
29local function sendkeys(keys)
30  api.nvim_input(keys)
31  -- Wait for Nvim to fully process pending input before possibly sending
32  -- more key presses - otherwise they all pile up in the queue and get
33  -- processed at once
34  n.poke_eventloop()
35end
36
37local function open(activate, lines)
38  local filename = t.tmpname()
39  write_file(filename, table.concat(lines, '\n') .. '\n', true)
40  command('edit ' .. filename)
41  local b = api.nvim_get_current_buf()
42  -- what is the value of b:changedtick?
43  local tick = eval('b:changedtick')
44
45  -- Enable buffer events, ensure that the nvim_buf_lines_event messages
46  -- arrive as expected
47  if activate then
48    local firstline = 0
49    ok(api.nvim_buf_attach(b, true, {}))
50    expectn('nvim_buf_lines_event', { b, tick, firstline, -1, lines, false })
51  end
52
53  return b, tick, filename
54end
55
56local function editoriginal(activate, lines)
57  if not lines then
58    lines = origlines
59  end
60  -- load up the file with the correct contents
61  return open(activate, lines)
62end
63
64local function reopen(buf, expectedlines)
65  ok(api.nvim_buf_detach(buf))
66  expectn('nvim_buf_detach_event', { buf })
67  -- for some reason the :edit! increments tick by 2
68  command('edit!')
69  local tick = eval('b:changedtick')
70  ok(api.nvim_buf_attach(buf, true, {}))
71  local firstline = 0
72  expectn('nvim_buf_lines_event', { buf, tick, firstline, -1, expectedlines, false })
73  command('normal! gg')
74  return tick
75end
76
77local function reopenwithfolds(b)
78  -- discard any changes to the buffer
79  local tick = reopen(b, origlines)
80
81  -- use markers for folds, make all folds open by default
82  command('setlocal foldmethod=marker foldlevel=20 commentstring=/*%s*/')
83
84  -- add a fold
85  command('2,4fold')
86  tick = tick + 1
87  expectn('nvim_buf_lines_event', {
88    b,
89    tick,
90    1,
91    4,
92    {
93      'original line 2/*{{{*/',
94      'original line 3',
95      'original line 4/*}}}*/',
96    },
97    false,
98  })
99  -- make a new fold that wraps lines 1-6
100  command('1,6fold')
101  tick = tick + 1
102  expectn('nvim_buf_lines_event', {
103    b,
104    tick,
105    0,
106    6,
107    {
108      'original line 1/*{{{*/',
109      'original line 2/*{{{*/',
110      'original line 3',
111      'original line 4/*}}}*/',
112      'original line 5',
113      'original line 6/*}}}*/',
114    },
115    false,
116  })
117  return tick
118end
119
120describe('API: buffer events:', function()
121  before_each(clear)
122
123  it('validation', function()
124    local b = editoriginal(false)
125    eq("Invalid key: 'builtin'", pcall_err(api.nvim_buf_attach, b, false, { builtin = 'asfd' }))
126  end)
127
128  it('when lines are added', function()
129    local b, tick = editoriginal(true)
130
131    -- add a new line at the start of the buffer
132    command('normal! GyyggP')
133    tick = tick + 1
134    expectn('nvim_buf_lines_event', { b, tick, 0, 0, { 'original line 6' }, false })
135
136    -- add multiple lines at the start of the file
137    command('normal! GkkyGggP')
138    tick = tick + 1
139    expectn(
140      'nvim_buf_lines_event',
141      { b, tick, 0, 0, { 'original line 4', 'original line 5', 'original line 6' }, false }
142    )
143
144    -- add one line to the middle of the file, several times
145    command('normal! ggYjjp')
146    tick = tick + 1
147    expectn('nvim_buf_lines_event', { b, tick, 3, 3, { 'original line 4' }, false })
148    command('normal! p')
149    tick = tick + 1
150    expectn('nvim_buf_lines_event', { b, tick, 4, 4, { 'original line 4' }, false })
151    command('normal! p')
152    tick = tick + 1
153    expectn('nvim_buf_lines_event', { b, tick, 5, 5, { 'original line 4' }, false })
154
155    -- add multiple lines to the middle of the file
156    command('normal! gg4Yjjp')
157    tick = tick + 1
158    expectn('nvim_buf_lines_event', {
159      b,
160      tick,
161      3,
162      3,
163      {
164        'original line 4',
165        'original line 5',
166        'original line 6',
167        'original line 4',
168      },
169      false,
170    })
171
172    -- add one line to the end of the file
173    command('normal! ggYGp')
174    tick = tick + 1
175    expectn('nvim_buf_lines_event', { b, tick, 17, 17, { 'original line 4' }, false })
176
177    -- add one line to the end of the file, several times
178    command('normal! ggYGppp')
179    tick = tick + 1
180    expectn('nvim_buf_lines_event', { b, tick, 18, 18, { 'original line 4' }, false })
181    tick = tick + 1
182    expectn('nvim_buf_lines_event', { b, tick, 19, 19, { 'original line 4' }, false })
183    tick = tick + 1
184    expectn('nvim_buf_lines_event', { b, tick, 20, 20, { 'original line 4' }, false })
185
186    -- add several lines to the end of the file, several times
187    command('normal! gg4YGp')
188    command('normal! Gp')
189    command('normal! Gp')
190    local firstfour = { 'original line 4', 'original line 5', 'original line 6', 'original line 4' }
191    tick = tick + 1
192    expectn('nvim_buf_lines_event', { b, tick, 21, 21, firstfour, false })
193    tick = tick + 1
194    expectn('nvim_buf_lines_event', { b, tick, 25, 25, firstfour, false })
195    tick = tick + 1
196    expectn('nvim_buf_lines_event', { b, tick, 29, 29, firstfour, false })
197
198    -- delete the current buffer to turn off buffer events
199    command('bdelete!')
200    expectn('nvim_buf_detach_event', { b })
201
202    -- add a line at the start of an empty file
203    command('enew')
204    tick = eval('b:changedtick')
205    local b2 = api.nvim_get_current_buf()
206    ok(api.nvim_buf_attach(b2, true, {}))
207    expectn('nvim_buf_lines_event', { b2, tick, 0, -1, { '' }, false })
208    eval('append(0, ["new line 1"])')
209    tick = tick + 1
210    expectn('nvim_buf_lines_event', { b2, tick, 0, 0, { 'new line 1' }, false })
211
212    -- turn off buffer events manually
213    api.nvim_buf_detach(b2)
214    expectn('nvim_buf_detach_event', { b2 })
215
216    -- add multiple lines to a blank file
217    command('enew!')
218    local b3 = api.nvim_get_current_buf()
219    ok(api.nvim_buf_attach(b3, true, {}))
220    tick = eval('b:changedtick')
221    expectn('nvim_buf_lines_event', { b3, tick, 0, -1, { '' }, false })
222    eval('append(0, ["new line 1", "new line 2", "new line 3"])')
223    tick = tick + 1
224    expectn(
225      'nvim_buf_lines_event',
226      { b3, tick, 0, 0, { 'new line 1', 'new line 2', 'new line 3' }, false }
227    )
228
229    -- use the API itself to add a line to the start of the buffer
230    api.nvim_buf_set_lines(b3, 0, 0, true, { 'New First Line' })
231    tick = tick + 1
232    expectn('nvim_buf_lines_event', { b3, tick, 0, 0, { 'New First Line' }, false })
233  end)
234
235  it('when lines are removed', function()
236    local b, tick = editoriginal(true)
237
238    -- remove one line from start of file
239    command('normal! dd')
240    tick = tick + 1
241    expectn('nvim_buf_lines_event', { b, tick, 0, 1, {}, false })
242
243    -- remove multiple lines from the start of the file
244    command('normal! 4dd')
245    tick = tick + 1
246    expectn('nvim_buf_lines_event', { b, tick, 0, 4, {}, false })
247
248    -- remove multiple lines from middle of file
249    tick = reopen(b, origlines)
250    command('normal! jj3dd')
251    tick = tick + 1
252    expectn('nvim_buf_lines_event', { b, tick, 2, 5, {}, false })
253
254    -- remove one line from the end of the file
255    tick = reopen(b, origlines)
256    command('normal! Gdd')
257    tick = tick + 1
258    expectn('nvim_buf_lines_event', { b, tick, 5, 6, {}, false })
259
260    -- remove multiple lines from the end of the file
261    tick = reopen(b, origlines)
262    command('normal! 4G3dd')
263    tick = tick + 1
264    expectn('nvim_buf_lines_event', { b, tick, 3, 6, {}, false })
265
266    -- pretend to remove heaps lines from the end of the file but really
267    -- just remove two
268    tick = reopen(b, origlines)
269    command('normal! Gk5dd')
270    tick = tick + 1
271    expectn('nvim_buf_lines_event', { b, tick, 4, 6, {}, false })
272  end)
273
274  it('when text is changed', function()
275    local b, tick = editoriginal(true)
276
277    -- some normal text editing
278    command('normal! A555')
279    tick = tick + 1
280    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'original line 1555' }, false })
281    command('normal! jj8X')
282    tick = tick + 1
283    expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'origin3' }, false })
284
285    -- modify multiple lines at once using visual block mode
286    tick = reopen(b, origlines)
287    command('normal! jjw')
288    sendkeys('<C-v>jjllx')
289    tick = tick + 1
290    expectn(
291      'nvim_buf_lines_event',
292      { b, tick, 2, 5, { 'original e 3', 'original e 4', 'original e 5' }, false }
293    )
294
295    -- replace part of a line line using :s
296    tick = reopen(b, origlines)
297    command('3s/line 3/foo/')
298    tick = tick + 1
299    expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'original foo' }, false })
300
301    -- replace parts of several lines line using :s
302    tick = reopen(b, origlines)
303    command('%s/line [35]/foo/')
304    tick = tick + 1
305    expectn(
306      'nvim_buf_lines_event',
307      { b, tick, 2, 5, { 'original foo', 'original line 4', 'original foo' }, false }
308    )
309
310    -- type text into the first line of a blank file, one character at a time
311    command('bdelete!')
312    tick = 2
313    expectn('nvim_buf_detach_event', { b })
314    local bnew = api.nvim_get_current_buf()
315    ok(api.nvim_buf_attach(bnew, true, {}))
316    expectn('nvim_buf_lines_event', { bnew, tick, 0, -1, { '' }, false })
317    sendkeys('i')
318    sendkeys('h')
319    sendkeys('e')
320    sendkeys('l')
321    sendkeys('l')
322    sendkeys('o\nworld')
323    expectn('nvim_buf_lines_event', { bnew, tick + 1, 0, 1, { 'h' }, false })
324    expectn('nvim_buf_lines_event', { bnew, tick + 2, 0, 1, { 'he' }, false })
325    expectn('nvim_buf_lines_event', { bnew, tick + 3, 0, 1, { 'hel' }, false })
326    expectn('nvim_buf_lines_event', { bnew, tick + 4, 0, 1, { 'hell' }, false })
327    expectn('nvim_buf_lines_event', { bnew, tick + 5, 0, 1, { 'hello' }, false })
328    expectn('nvim_buf_lines_event', { bnew, tick + 6, 0, 1, { 'hello', '' }, false })
329    expectn('nvim_buf_lines_event', { bnew, tick + 7, 1, 2, { 'world' }, false })
330  end)
331
332  it('when lines are replaced', function()
333    local b, tick = editoriginal(true)
334
335    -- blast away parts of some lines with visual mode
336    command('normal! jjwvjjllx')
337    tick = tick + 1
338    expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'original ' }, false })
339    tick = tick + 1
340    expectn('nvim_buf_lines_event', { b, tick, 3, 4, {}, false })
341    tick = tick + 1
342    expectn('nvim_buf_lines_event', { b, tick, 3, 4, { 'e 5' }, false })
343    tick = tick + 1
344    expectn('nvim_buf_lines_event', { b, tick, 2, 3, { 'original e 5' }, false })
345    tick = tick + 1
346    expectn('nvim_buf_lines_event', { b, tick, 3, 4, {}, false })
347
348    -- blast away a few lines using :g
349    tick = reopen(b, origlines)
350    command('global/line [35]/delete')
351    tick = tick + 1
352    expectn('nvim_buf_lines_event', { b, tick, 2, 3, {}, false })
353    tick = tick + 1
354    expectn('nvim_buf_lines_event', { b, tick, 3, 4, {}, false })
355  end)
356
357  it('visual paste split empty line', function()
358    local b, tick = editoriginal(true, { 'abc', '{', 'def', '}' })
359    command('normal! ggyyjjvi{p')
360    expectn('nvim_buf_lines_event', { b, tick + 1, 2, 3, { '' }, false })
361    expectn('nvim_buf_lines_event', { b, tick + 2, 2, 3, { '}' }, false })
362    expectn('nvim_buf_lines_event', { b, tick + 3, 3, 4, {}, false })
363    expectn('nvim_buf_lines_event', { b, tick + 3, 2, 3, { '' }, false })
364    expectn('nvim_buf_lines_event', { b, tick + 4, 3, 3, { 'abc', '}' }, false })
365  end)
366
367  it('when lines are filtered', function()
368    -- Test filtering lines with !cat
369    local b, tick = editoriginal(true, { 'A', 'C', 'E', 'B', 'D', 'F' })
370
371    command('silent 2,5!cat')
372    -- the change comes through as two changes:
373    -- 1) addition of the new lines after the filtered lines
374    -- 2) removal of the original lines
375    tick = tick + 1
376    expectn('nvim_buf_lines_event', { b, tick, 5, 5, { 'C', 'E', 'B', 'D' }, false })
377    tick = tick + 1
378    expectn('nvim_buf_lines_event', { b, tick, 1, 5, {}, false })
379  end)
380
381  it('when you use "o"', function()
382    local b, tick = editoriginal(true, { 'AAA', 'BBB' })
383    command('set noautoindent nosmartindent')
384
385    -- use 'o' to start a new line from a line with no indent
386    command('normal! o')
387    tick = tick + 1
388    expectn('nvim_buf_lines_event', { b, tick, 1, 1, { '' }, false })
389
390    -- undo the change, indent line 1 a bit, and try again
391    command('undo')
392    tick = tick + 1
393    expectn('nvim_buf_lines_event', { b, tick, 1, 2, {}, false })
394    tick = tick + 1
395    expectn('nvim_buf_changedtick_event', { b, tick })
396    command('set autoindent')
397    command('normal! >>')
398    tick = tick + 1
399    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { '\tAAA' }, false })
400    command('normal! ommm')
401    tick = tick + 1
402    expectn('nvim_buf_lines_event', { b, tick, 1, 1, { '\t' }, false })
403    tick = tick + 1
404    expectn('nvim_buf_lines_event', { b, tick, 1, 2, { '\tmmm' }, false })
405
406    -- undo the change, and try again with 'O'
407    command('undo')
408    tick = tick + 1
409    expectn('nvim_buf_lines_event', { b, tick, 1, 2, { '\t' }, false })
410    tick = tick + 1
411    expectn('nvim_buf_lines_event', { b, tick, 1, 2, {}, false })
412    tick = tick + 1
413    expectn('nvim_buf_changedtick_event', { b, tick })
414    command('normal! ggOmmm')
415    tick = tick + 1
416    expectn('nvim_buf_lines_event', { b, tick, 0, 0, { '\t' }, false })
417    tick = tick + 1
418    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { '\tmmm' }, false })
419  end)
420
421  it('deactivates if the buffer is changed externally', function()
422    -- Test changing file from outside vim and reloading using :edit
423    local lines = { 'Line 1', 'Line 2' }
424    local b, tick, filename = editoriginal(true, lines)
425
426    command('normal! x')
427    tick = tick + 1
428    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'ine 1' }, false })
429    command('undo')
430    tick = tick + 1
431    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'Line 1' }, false })
432    tick = tick + 1
433    expectn('nvim_buf_changedtick_event', { b, tick })
434
435    -- change the file directly
436    write_file(filename, 'another line\n', true, true)
437
438    -- reopen the file and watch buffer events shut down
439    command('edit')
440    expectn('nvim_buf_detach_event', { b })
441  end)
442
443  it('channel can watch many buffers at once', function()
444    -- edit 3 buffers, make sure they all have windows visible so that when we
445    -- move between buffers, none of them are unloaded
446    local b1, tick1 = editoriginal(true, { 'A1', 'A2' })
447    local b1nr = eval('bufnr("")')
448    command('split')
449    local b2, tick2 = open(true, { 'B1', 'B2' })
450    local b2nr = eval('bufnr("")')
451    command('split')
452    local b3, tick3 = open(true, { 'C1', 'C2' })
453    local b3nr = eval('bufnr("")')
454
455    -- make a new window for moving between buffers
456    command('split')
457
458    command('b' .. b1nr)
459    command('normal! x')
460    tick1 = tick1 + 1
461    expectn('nvim_buf_lines_event', { b1, tick1, 0, 1, { '1' }, false })
462    command('undo')
463    tick1 = tick1 + 1
464    expectn('nvim_buf_lines_event', { b1, tick1, 0, 1, { 'A1' }, false })
465    tick1 = tick1 + 1
466    expectn('nvim_buf_changedtick_event', { b1, tick1 })
467
468    command('b' .. b2nr)
469    command('normal! x')
470    tick2 = tick2 + 1
471    expectn('nvim_buf_lines_event', { b2, tick2, 0, 1, { '1' }, false })
472    command('undo')
473    tick2 = tick2 + 1
474    expectn('nvim_buf_lines_event', { b2, tick2, 0, 1, { 'B1' }, false })
475    tick2 = tick2 + 1
476    expectn('nvim_buf_changedtick_event', { b2, tick2 })
477
478    command('b' .. b3nr)
479    command('normal! x')
480    tick3 = tick3 + 1
481    expectn('nvim_buf_lines_event', { b3, tick3, 0, 1, { '1' }, false })
482    command('undo')
483    tick3 = tick3 + 1
484    expectn('nvim_buf_lines_event', { b3, tick3, 0, 1, { 'C1' }, false })
485    tick3 = tick3 + 1
486    expectn('nvim_buf_changedtick_event', { b3, tick3 })
487  end)
488
489  it('does not get confused if enabled/disabled many times', function()
490    local channel = api.nvim_get_chan_info(0).id
491    local b, tick = editoriginal(false)
492
493    -- Enable buffer events many times.
494    ok(api.nvim_buf_attach(b, true, {}))
495    ok(api.nvim_buf_attach(b, true, {}))
496    ok(api.nvim_buf_attach(b, true, {}))
497    ok(api.nvim_buf_attach(b, true, {}))
498    ok(api.nvim_buf_attach(b, true, {}))
499    expectn('nvim_buf_lines_event', { b, tick, 0, -1, origlines, false })
500    eval('rpcnotify(' .. channel .. ', "Hello There")')
501    expectn('Hello There', {})
502
503    -- Disable buffer events many times.
504    ok(api.nvim_buf_detach(b))
505    ok(api.nvim_buf_detach(b))
506    ok(api.nvim_buf_detach(b))
507    ok(api.nvim_buf_detach(b))
508    ok(api.nvim_buf_detach(b))
509    expectn('nvim_buf_detach_event', { b })
510    eval('rpcnotify(' .. channel .. ', "Hello Again")')
511    expectn('Hello Again', {})
512  end)
513
514  it('can notify several channels at once', function()
515    -- create several new sessions, in addition to our main API
516    local sessions = {}
517    local pipe = n.new_pipename()
518    eval("serverstart('" .. pipe .. "')")
519    sessions[1] = n.connect(pipe)
520    sessions[2] = n.connect(pipe)
521    sessions[3] = n.connect(pipe)
522
523    local function request(sessionnr, method, ...)
524      local status, rv = sessions[sessionnr]:request(method, ...)
525      if not status then
526        error(rv[2])
527      end
528      return rv
529    end
530
531    local function wantn(sessionid, name, args)
532      local session = sessions[sessionid]
533      eq({ 'notification', name, args }, session:next_message(10000))
534    end
535
536    -- Edit a new file, but don't enable buffer events.
537    local lines = { 'AAA', 'BBB' }
538    local b, tick = open(false, lines)
539
540    -- Enable buffer events for sessions 1, 2 and 3.
541    ok(request(1, 'nvim_buf_attach', b, true, {}))
542    ok(request(2, 'nvim_buf_attach', b, true, {}))
543    ok(request(3, 'nvim_buf_attach', b, true, {}))
544    wantn(1, 'nvim_buf_lines_event', { b, tick, 0, -1, lines, false })
545    wantn(2, 'nvim_buf_lines_event', { b, tick, 0, -1, lines, false })
546    wantn(3, 'nvim_buf_lines_event', { b, tick, 0, -1, lines, false })
547
548    -- Change the buffer.
549    command('normal! x')
550    tick = tick + 1
551    wantn(1, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
552    wantn(2, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
553    wantn(3, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
554
555    -- Stop watching on channel 1.
556    ok(request(1, 'nvim_buf_detach', b))
557    wantn(1, 'nvim_buf_detach_event', { b })
558
559    -- Undo the change to buffer 1.
560    command('undo')
561    tick = tick + 1
562    wantn(2, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
563    wantn(3, 'nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
564    tick = tick + 1
565    wantn(2, 'nvim_buf_changedtick_event', { b, tick })
566    wantn(3, 'nvim_buf_changedtick_event', { b, tick })
567
568    -- make sure there are no other pending nvim_buf_lines_event messages going to
569    -- channel 1
570    local channel1 = request(1, 'nvim_get_chan_info', 0).id
571    eval('rpcnotify(' .. channel1 .. ', "Hello")')
572    wantn(1, 'Hello', {})
573
574    -- close the buffer and channels 2 and 3 should get a nvim_buf_detach_event
575    -- notification
576    command('edit')
577    wantn(2, 'nvim_buf_detach_event', { b })
578    wantn(3, 'nvim_buf_detach_event', { b })
579
580    -- make sure there are no other pending nvim_buf_lines_event messages going to
581    -- channel 1
582    channel1 = request(1, 'nvim_get_chan_info', 0).id
583    eval('rpcnotify(' .. channel1 .. ', "Hello Again")')
584    wantn(1, 'Hello Again', {})
585  end)
586
587  it('works with :diffput and :diffget', function()
588    local b1, tick1 = editoriginal(true, { 'AAA', 'BBB' })
589    local channel = api.nvim_get_chan_info(0).id
590    command('diffthis')
591    command('rightbelow vsplit')
592    local b2, tick2 = open(true, { 'BBB', 'CCC' })
593    command('diffthis')
594    -- go back to first buffer, and push the 'AAA' line to the second buffer
595    command('1wincmd w')
596    command('normal! gg')
597    command('diffput')
598    tick2 = tick2 + 1
599    expectn('nvim_buf_lines_event', { b2, tick2, 0, 0, { 'AAA' }, false })
600
601    -- use :diffget to grab the other change from buffer 2
602    command('normal! G')
603    command('diffget')
604    tick1 = tick1 + 1
605    expectn('nvim_buf_lines_event', { b1, tick1, 2, 2, { 'CCC' }, false })
606
607    eval('rpcnotify(' .. channel .. ', "Goodbye")')
608    expectn('Goodbye', {})
609  end)
610
611  it('works with :sort', function()
612    -- test for :sort
613    local b, tick = editoriginal(true, { 'B', 'D', 'C', 'A', 'E' })
614    command('%sort')
615    tick = tick + 1
616    expectn('nvim_buf_lines_event', { b, tick, 0, 5, { 'A', 'B', 'C', 'D', 'E' }, false })
617  end)
618
619  it('works with :left', function()
620    local b, tick = editoriginal(true, { ' A', '  B', 'B', '\tB', '\t\tC' })
621    command('2,4left')
622    tick = tick + 1
623    expectn('nvim_buf_lines_event', { b, tick, 1, 4, { 'B', 'B', 'B' }, false })
624  end)
625
626  it('works with :right', function()
627    local b, tick = editoriginal(true, { ' A', '\t  B', '\t  \tBB', ' \tB', '\t\tC' })
628    command('set ts=2 et')
629    command('2,4retab')
630    tick = tick + 1
631    expectn('nvim_buf_lines_event', { b, tick, 1, 4, { '    B', '      BB', '  B' }, false })
632  end)
633
634  it('works with :move', function()
635    local b, tick = editoriginal(true, origlines)
636    -- move text down towards the end of the file
637    command('2,3move 4')
638    tick = tick + 2
639    expectn(
640      'nvim_buf_lines_event',
641      { b, tick, 4, 4, { 'original line 2', 'original line 3' }, false }
642    )
643    tick = tick + 1
644    expectn('nvim_buf_lines_event', { b, tick, 1, 3, {}, false })
645
646    -- move text up towards the start of the file
647    tick = reopen(b, origlines)
648    command('4,5move 2')
649    tick = tick + 2
650    expectn(
651      'nvim_buf_lines_event',
652      { b, tick, 2, 2, { 'original line 4', 'original line 5' }, false }
653    )
654    tick = tick + 1
655    expectn('nvim_buf_lines_event', { b, tick, 5, 7, {}, false })
656  end)
657
658  it('when you manually add/remove folds', function()
659    local b = editoriginal(true)
660    local tick = reopenwithfolds(b)
661
662    -- delete the inner fold
663    command('normal! zR3Gzd')
664    tick = tick + 1
665    expectn(
666      'nvim_buf_lines_event',
667      { b, tick, 1, 4, { 'original line 2', 'original line 3', 'original line 4' }, false }
668    )
669    -- delete the outer fold
670    command('normal! zd')
671    tick = tick + 1
672    expectn('nvim_buf_lines_event', { b, tick, 0, 6, origlines, false })
673
674    -- discard changes and put the folds back
675    tick = reopenwithfolds(b)
676
677    -- remove both folds at once
678    command('normal! ggzczD')
679    tick = tick + 1
680    expectn('nvim_buf_lines_event', { b, tick, 0, 6, origlines, false })
681
682    -- discard changes and put the folds back
683    tick = reopenwithfolds(b)
684
685    -- now delete all folds at once
686    command('normal! zE')
687    tick = tick + 1
688    expectn('nvim_buf_lines_event', { b, tick, 0, 6, origlines, false })
689
690    -- create a fold from line 4 to the end of the file
691    command('normal! 4GA/*{{{*/')
692    tick = tick + 1
693    expectn('nvim_buf_lines_event', { b, tick, 3, 4, { 'original line 4/*{{{*/' }, false })
694
695    -- delete the fold which only has one marker
696    command('normal! Gzd')
697    tick = tick + 1
698    expectn(
699      'nvim_buf_lines_event',
700      { b, tick, 3, 6, { 'original line 4', 'original line 5', 'original line 6' }, false }
701    )
702  end)
703
704  it('detaches if the buffer is closed', function()
705    local b, tick = editoriginal(true, { 'AAA' })
706    local channel = api.nvim_get_chan_info(0).id
707
708    -- Test that buffer events are working.
709    command('normal! x')
710    tick = tick + 1
711    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
712    command('undo')
713    tick = tick + 1
714    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
715    tick = tick + 1
716    expectn('nvim_buf_changedtick_event', { b, tick })
717
718    -- close our buffer and create a new one
719    command('bdelete')
720    command('enew')
721    expectn('nvim_buf_detach_event', { b })
722
723    -- Reopen the original buffer, make sure there are no buffer events sent.
724    command('b1')
725    command('normal! x')
726
727    eval('rpcnotify(' .. channel .. ', "Hello There")')
728    expectn('Hello There', {})
729  end)
730
731  it(':edit! (reload) causes detach #9642', function()
732    local b, tick = editoriginal(true, { 'AAA', 'BBB' })
733    command('set undoreload=1')
734
735    command('normal! x')
736    tick = tick + 1
737    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
738
739    command('edit!')
740    expectn('nvim_buf_detach_event', { b })
741  end)
742
743  it(':enew! does not detach hidden buffer', function()
744    local b, tick = editoriginal(true, { 'AAA', 'BBB' })
745    local channel = api.nvim_get_chan_info(0).id
746
747    command('set undoreload=1 hidden')
748    command('normal! x')
749    tick = tick + 1
750    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
751
752    command('enew!')
753    eval('rpcnotify(' .. channel .. ', "Hello There")')
754    expectn('Hello There', {})
755  end)
756
757  it('stays attached if the buffer is hidden', function()
758    local b, tick = editoriginal(true, { 'AAA' })
759    local channel = api.nvim_get_chan_info(0).id
760
761    -- Test that buffer events are working.
762    command('normal! x')
763    tick = tick + 1
764    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
765    command('undo')
766    tick = tick + 1
767    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AAA' }, false })
768    tick = tick + 1
769    expectn('nvim_buf_changedtick_event', { b, tick })
770
771    -- Close our buffer by creating a new one.
772    command('set hidden')
773    command('enew')
774
775    -- Assert that no nvim_buf_detach_event is sent.
776    eval('rpcnotify(' .. channel .. ', "Hello There")')
777    expectn('Hello There', {})
778
779    -- Reopen the original buffer, assert that buffer events are still active.
780    command('b1')
781    command('normal! x')
782    tick = tick + 1
783    expectn('nvim_buf_lines_event', { b, tick, 0, 1, { 'AA' }, false })
784  end)
785
786  it('detaches if the buffer is unloaded/deleted/wiped', function()
787    -- need to make a new window with a buffer because :bunload doesn't let you
788    -- unload the last buffer
789    for _, cmd in ipairs({ 'bunload', 'bdelete', 'bwipeout' }) do
790      command('new')
791      -- open a brand spanking new file
792      local b = open(true, { 'AAA' })
793
794      -- call :bunload or whatever the command is, and then check that we
795      -- receive a nvim_buf_detach_event
796      command(cmd)
797      expectn('nvim_buf_detach_event', { b })
798    end
799  end)
800
801  it('does not send the buffer content if not requested', function()
802    local b, tick = editoriginal(false)
803    ok(api.nvim_buf_attach(b, false, {}))
804    expectn('nvim_buf_changedtick_event', { b, tick })
805  end)
806
807  it('nvim_buf_attach returns response after delay #8634', function()
808    sleep(250)
809    -- response
810    eq(true, n.request('nvim_buf_attach', 0, false, {}))
811    -- notification
812    eq({
813      [1] = 'notification',
814      [2] = 'nvim_buf_changedtick_event',
815      [3] = {
816        [1] = 1,
817        [2] = 2,
818      },
819    }, next_msg())
820  end)
821
822  it('when updating quickfix list #34610', function()
823    command('copen')
824
825    local b = api.nvim_get_current_buf()
826    ok(api.nvim_buf_attach(b, true, {}))
827    expectn('nvim_buf_lines_event', { b, 2, 0, -1, { '' }, false })
828
829    command("cexpr ['Xa', 'Xb']")
830    expectn('nvim_buf_lines_event', { b, 3, 0, 1, { '|| Xa', '|| Xb' }, false })
831
832    command("caddexpr ['Xc']")
833    expectn('nvim_buf_lines_event', { b, 4, 2, 2, { '|| Xc' }, false })
834  end)
835end)
836
837describe('API: buffer events:', function()
838  before_each(function()
839    clear()
840  end)
841
842  local function lines_subset(first, second)
843    for i = 1, #first do
844      -- need to ignore trailing spaces
845      if first[i]:gsub(' +$', '') ~= second[i]:gsub(' +$', '') then
846        return false
847      end
848    end
849    return true
850  end
851
852  local function lines_equal(f, s)
853    return lines_subset(f, s) and lines_subset(s, f)
854  end
855
856  local function assert_match_somewhere(expected_lines, buffer_lines)
857    local msg = next_msg()
858
859    while msg ~= nil do
860      local event = msg[2]
861      if event == 'nvim_buf_lines_event' then
862        local args = msg[3]
863        local starts = args[3]
864        local newlines = args[5]
865
866        -- Size of the contained nvim instance is 23 lines, this might change
867        -- with the test setup. Note updates are contiguous.
868        assert(#newlines <= 23)
869
870        for i = 1, #newlines do
871          buffer_lines[starts + i] = newlines[i]
872        end
873        -- we don't compare the msg area of the embedded nvim, it's too flakey
874        buffer_lines[23] = nil
875
876        if lines_equal(buffer_lines, expected_lines) then
877          -- OK
878          return
879        end
880      end
881      msg = next_msg()
882    end
883    assert(false, 'did not match/receive expected nvim_buf_lines_event lines')
884  end
885
886  it('when :terminal lines change', function()
887    local buffer_lines = {}
888    local expected_lines = {}
889    fn.jobstart({ nvim_prog, '-u', 'NONE', '-i', 'NONE', '-n', '-c', 'set shortmess+=A' }, {
890      term = true,
891      env = { VIMRUNTIME = os.getenv('VIMRUNTIME') },
892    })
893
894    local b = api.nvim_get_current_buf()
895    ok(api.nvim_buf_attach(b, true, {}))
896
897    for _ = 1, 22 do
898      table.insert(expected_lines, '~')
899    end
900    expected_lines[1] = ''
901    expected_lines[22] = ('tmp_terminal_nvim' .. (' '):rep(45) .. '0,0-1          All')
902
903    sendkeys('i:e tmp_terminal_nvim<Enter>')
904    assert_match_somewhere(expected_lines, buffer_lines)
905
906    expected_lines[1] = 'Blarg'
907    expected_lines[22] = ('tmp_terminal_nvim [+]' .. (' '):rep(41) .. '1,6            All')
908
909    sendkeys('iBlarg')
910    assert_match_somewhere(expected_lines, buffer_lines)
911
912    for i = 1, 21 do
913      expected_lines[i] = 'xyz'
914    end
915    expected_lines[22] = ('tmp_terminal_nvim [+]' .. (' '):rep(41) .. '31,4           Bot')
916
917    local s = string.rep('\nxyz', 30)
918    sendkeys(s)
919    assert_match_somewhere(expected_lines, buffer_lines)
920
921    -- Trigger synchronized output in child Nvim, and then exit parent Nvim after
922    -- blocking its event loop for a while. This should not cause an error log.
923    n.expect_exit(n.exec_lua, function()
924      vim.api.nvim_chan_send(vim.bo.channel, 'aaa')
925      vim.uv.sleep(5)
926      vim.cmd('qall!')
927    end)
928  end)
929
930  it('no spurious event with nvim_open_term() on empty buffer', function()
931    local b = api.nvim_get_current_buf()
932    local tick = api.nvim_buf_get_var(b, 'changedtick')
933    ok(api.nvim_buf_attach(b, true, {}))
934    expectn('nvim_buf_lines_event', { b, tick, 0, -1, { '' }, false })
935    api.nvim_open_term(0, {})
936    local expected_lines = {}
937    for _ = 1, 23 do
938      table.insert(expected_lines, '')
939    end
940    expectn('nvim_buf_lines_event', { b, tick + 1, 0, 1, expected_lines, false })
941  end)
942end)