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