neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

buffer_updates_spec.lua (30707B)


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