buffer_updates_spec.lua 29 KB

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