robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1-- Test suite for testing interactions with API bindings 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each , teardown =t . describe , t . it , t . before_each , t . teardown 7local command =n . command 8local api =n . api 9local fn =n . fn 10local clear =n . clear 11local eq =t . eq 12local exec_lua =n . exec_lua 13local feed =n . feed 14local expect_events =t . expect_events 15local write_file =t . write_file 16local dedent =t . dedent 17local matches =t . matches 18local pcall_err =t . pcall_err 19 20local origlines ={ 21'original line 1' , 22'original line 2' , 23'original line 3' , 24'original line 4' , 25'original line 5' , 26'original line 6' , 27' indented line' , 28} 29 30before_each ( function () 31clear () 32exec_lua ( function () 33local events ={} 34 35function _G . test_register ( bufnr , evname , id , changedtick , utf_sizes , preview ) 36local function callback ( ... ) 37table . insert ( events , { id , ... } ) 38if _G . test_unreg == id then 39return true 40end 41end 42local opts ={ 43[ evname ] =callback , 44on_detach =callback , 45on_reload =callback , 46utf_sizes =utf_sizes , 47preview =preview , 48} 49if changedtick then 50opts . on_changedtick =callback 51end 52vim . api . nvim_buf_attach ( bufnr , false , opts ) 53end 54 55function _G . get_events () 56local ret_events =events 57events ={} 58return ret_events 59end 60end ) 61end ) 62 63describe ( 'lua: nvim_buf_attach on_lines' , function () 64local function setup_eventcheck ( verify , utf_sizes , lines ) 65local lastsize 66api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 67if verify then 68lastsize =api . nvim_buf_get_offset ( 0 , api . nvim_buf_line_count ( 0 )) 69end 70exec_lua ( 'return test_register(...)' , 0 , 'on_lines' , 'test1' , false , utf_sizes ) 71local verify_name ='test1' 72 73local function check_events ( expected ) 74local events =exec_lua ( 'return get_events(...)' ) 75if utf_sizes then 76-- this test case uses ASCII only, so sizes should be the same. 77-- Unicode is tested below. 78for _ , event in ipairs ( expected ) do 79event [ 9 ] =event [ 9 ] or event [ 8 ] 80event [ 10 ] =event [ 10 ] or event [ 9 ] 81end 82end 83expect_events ( expected , events , 'line updates' ) 84if verify then 85for _ , event in ipairs ( events ) do 86if event [ 1 ] == verify_name and event [ 2 ] == 'lines' then 87local startline , endline =event [ 5 ], event [ 7 ] 88local newrange =api . nvim_buf_get_offset ( 0 , endline ) 89- api . nvim_buf_get_offset ( 0 , startline ) 90local newsize =api . nvim_buf_get_offset ( 0 , api . nvim_buf_line_count ( 0 )) 91local oldrange =newrange + lastsize - newsize 92eq ( oldrange , event [ 8 ]) 93lastsize =newsize 94end 95end 96end 97end 98return check_events , function ( new ) 99verify_name =new 100end 101end 102 103-- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot 104-- assert the wrong thing), but masks errors with unflushed lines (as 105-- nvim_buf_get_offset forces a flush of the memline). To be safe run the 106-- test both ways. 107local function check ( verify , utf_sizes ) 108local check_events , verify_name =setup_eventcheck ( verify , utf_sizes , origlines ) 109 110local tick =api . nvim_buf_get_changedtick ( 0 ) 111command ( 'set autoindent' ) 112command ( 'normal! GyyggP' ) 113tick =tick + 1 114check_events { { 'test1' , 'lines' , 1 , tick , 0 , 0 , 1 , 0 } } 115 116api . nvim_buf_set_lines ( 0 , 3 , 5 , true , { 'changed line' } ) 117tick =tick + 1 118check_events { { 'test1' , 'lines' , 1 , tick , 3 , 5 , 4 , 32 } } 119 120exec_lua ( 'return test_register(...)' , 0 , 'on_lines' , 'test2' , true , utf_sizes ) 121tick =tick + 1 122command ( 'undo' ) 123 124-- plugins can opt in to receive changedtick events, or choose 125-- to only receive actual changes. 126check_events { 127{ 'test1' , 'lines' , 1 , tick , 3 , 4 , 5 , 13 } , 128{ 'test2' , 'lines' , 1 , tick , 3 , 4 , 5 , 13 } , 129{ 'test2' , 'changedtick' , 1 , tick + 1 } , 130} 131tick =tick + 1 132 133tick =tick + 1 134command ( 'redo' ) 135check_events { 136{ 'test1' , 'lines' , 1 , tick , 3 , 5 , 4 , 32 } , 137{ 'test2' , 'lines' , 1 , tick , 3 , 5 , 4 , 32 } , 138{ 'test2' , 'changedtick' , 1 , tick + 1 } , 139} 140tick =tick + 1 141 142tick =tick + 1 143command ( 'undo!' ) 144check_events { 145{ 'test1' , 'lines' , 1 , tick , 3 , 4 , 5 , 13 } , 146{ 'test2' , 'lines' , 1 , tick , 3 , 4 , 5 , 13 } , 147{ 'test2' , 'changedtick' , 1 , tick + 1 } , 148} 149tick =tick + 1 150 151-- simulate next callback returning true 152exec_lua ( "test_unreg = 'test1'" ) 153 154api . nvim_buf_set_lines ( 0 , 6 , 7 , true , { 'x1' , 'x2' , 'x3' } ) 155tick =tick + 1 156 157-- plugins can opt in to receive changedtick events, or choose 158-- to only receive actual changes. 159check_events { 160{ 'test1' , 'lines' , 1 , tick , 6 , 7 , 9 , 16 } , 161{ 'test2' , 'lines' , 1 , tick , 6 , 7 , 9 , 16 } , 162} 163 164verify_name 'test2' 165 166api . nvim_buf_set_lines ( 0 , 1 , 1 , true , { 'added' } ) 167tick =tick + 1 168check_events { { 'test2' , 'lines' , 1 , tick , 1 , 1 , 2 , 0 } } 169 170-- Undo restored the cursor (|restore-undo-cursor|); pin it for the relative edits below. 171api . nvim_win_set_cursor ( 0 , { 5 , 0 } ) 172feed ( 'wix' ) 173tick =tick + 1 174check_events { { 'test2' , 'lines' , 1 , tick , 4 , 5 , 5 , 16 } } 175 176-- check hot path for multiple insert 177feed ( 'yz' ) 178tick =tick + 1 179check_events { { 'test2' , 'lines' , 1 , tick , 4 , 5 , 5 , 17 } } 180 181feed ( '<bs>' ) 182tick =tick + 1 183check_events { { 'test2' , 'lines' , 1 , tick , 4 , 5 , 5 , 19 } } 184 185feed ( '<esc>Go' ) 186tick =tick + 1 187check_events { { 'test2' , 'lines' , 1 , tick , 11 , 11 , 12 , 0 } } 188 189feed ( 'x' ) 190tick =tick + 1 191check_events { { 'test2' , 'lines' , 1 , tick , 11 , 12 , 12 , 5 } } 192 193command ( 'bwipe!' ) 194check_events { { 'test2' , 'detach' , 1 } } 195end 196 197it ( 'works' , function () 198check ( false ) 199end ) 200 201it ( 'works with verify' , function () 202check ( true ) 203end ) 204 205it ( 'works with utf_sizes and ASCII text' , function () 206check ( false , true ) 207end ) 208 209local function check_unicode ( verify ) 210local unicode_text ={ 211'ascii text' , 212'latin text åäö' , 213'BMP text ɧ αλφά' , 214'BMP text 汉语 ↥↧' , 215'SMP 🤦 🦄🦃' , 216'combining å بِيَّة' , 217} 218local check_events , verify_name =setup_eventcheck ( verify , true , unicode_text ) 219 220local tick =api . nvim_buf_get_changedtick ( 0 ) 221 222feed ( 'ggdd' ) 223tick =tick + 1 224check_events { { 'test1' , 'lines' , 1 , tick , 0 , 1 , 0 , 11 , 11 , 11 } } 225 226feed ( 'A<bs>' ) 227tick =tick + 1 228check_events { { 'test1' , 'lines' , 1 , tick , 0 , 1 , 1 , 18 , 15 , 15 } } 229 230feed ( '<esc>jylp' ) 231tick =tick + 1 232check_events { { 'test1' , 'lines' , 1 , tick , 1 , 2 , 2 , 21 , 16 , 16 } } 233 234feed ( '+eea<cr>' ) 235tick =tick + 1 236check_events { { 'test1' , 'lines' , 1 , tick , 2 , 3 , 4 , 23 , 15 , 15 } } 237 238feed ( '<esc>jdw' ) 239tick =tick + 1 240-- non-BMP chars count as 2 UTF-2 codeunits 241check_events { { 'test1' , 'lines' , 1 , tick , 4 , 5 , 5 , 18 , 9 , 12 } } 242 243feed ( '+rx' ) 244tick =tick + 1 245-- count the individual codepoints of a composed character. 246check_events { { 'test1' , 'lines' , 1 , tick , 5 , 6 , 6 , 27 , 20 , 20 } } 247 248feed ( 'kJ' ) 249tick =tick + 1 250-- verification fails with multiple line updates, sorry about that 251verify_name '' 252-- NB: this is inefficient (but not really wrong). 253check_events { 254{ 'test1' , 'lines' , 1 , tick , 4 , 5 , 5 , 14 , 5 , 8 } , 255{ 'test1' , 'lines' , 1 , tick + 1 , 5 , 6 , 5 , 27 , 20 , 20 } , 256} 257end 258 259it ( 'works with utf_sizes and unicode text' , function () 260check_unicode ( false ) 261end ) 262 263it ( 'works with utf_sizes and unicode text with verify' , function () 264check_unicode ( true ) 265end ) 266 267it ( 'has valid cursor position while shifting' , function () 268api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'line1' } ) 269exec_lua ( function () 270vim . api . nvim_buf_attach ( 0 , false , { 271on_lines =function () 272vim . api . nvim_set_var ( 'listener_cursor_line' , vim . api . nvim_win_get_cursor ( 0 )[ 1 ]) 273end , 274} ) 275end ) 276feed ( '>>' ) 277eq ( 1 , api . nvim_get_var ( 'listener_cursor_line' )) 278end ) 279 280it ( 'has valid cursor position while deleting lines' , function () 281api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'line_1' , 'line_2' , 'line_3' , 'line_4' } ) 282api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 283eq ( 2 , api . nvim_win_get_cursor ( 0 )[ 1 ]) 284api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'line_1' , 'line_2' , 'line_3' } ) 285eq ( 2 , api . nvim_win_get_cursor ( 0 )[ 1 ]) 286end ) 287 288it ( '#12718 lnume' , function () 289api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '1' , '2' , '3' } ) 290exec_lua ( function () 291vim . api . nvim_buf_attach ( 0 , false , { 292on_lines =function ( ... ) 293vim . api . nvim_set_var ( 'linesev' , { ... } ) 294end , 295} ) 296end ) 297feed ( '1G0' ) 298feed ( 'y<C-v>2j' ) 299feed ( 'G0' ) 300feed ( 'p' ) 301-- Is the last arg old_byte_size correct? Doesn't matter for this PR 302eq ( { 'lines' , 1 , 4 , 2 , 3 , 5 , 4 } , api . nvim_get_var ( 'linesev' )) 303 304feed ( '2G0' ) 305feed ( 'p' ) 306eq ( { 'lines' , 1 , 5 , 1 , 4 , 4 , 8 } , api . nvim_get_var ( 'linesev' )) 307 308feed ( '1G0' ) 309feed ( 'P' ) 310eq ( { 'lines' , 1 , 6 , 0 , 3 , 3 , 9 } , api . nvim_get_var ( 'linesev' )) 311end ) 312 313it ( 'nvim_buf_call() from callback does not cause wrong Normal mode CTRL-A #16729' , function () 314exec_lua ( function () 315vim . api . nvim_buf_attach ( 0 , false , { 316on_lines =function () 317vim . api . nvim_buf_call ( 0 , function () end ) 318end , 319} ) 320end ) 321feed ( 'itest123<Esc><C-A>' ) 322eq ( 'test124' , api . nvim_get_current_line ()) 323end ) 324 325it ( 'setting extmark in on_lines callback works' , function () 326local screen =Screen . new ( 40 , 6 ) 327 328api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aaa' , 'bbb' , 'ccc' } ) 329exec_lua ( function () 330local ns =vim . api . nvim_create_namespace ( '' ) 331vim . api . nvim_buf_attach ( 0 , false , { 332on_lines =function ( _ , _ , _ , row , _ , end_row ) 333vim . api . nvim_buf_clear_namespace ( 0 , ns , row , end_row ) 334for i =row , end_row - 1 do 335vim . api . nvim_buf_set_extmark ( 0 , ns , i , 0 , { 336virt_text ={ { 'NEW' .. tostring ( i ), 'WarningMsg' } } , 337} ) 338end 339end , 340} ) 341end ) 342 343feed ( 'o' ) 344screen : expect ( { 345grid =[[ 346aaa | 347^ {19:NEW1} | 348bbb | 349ccc | 350{1:~ }| 351{5:-- INSERT --} | 352]] , 353} ) 354feed ( '<CR>' ) 355screen : expect ( { 356grid =[[ 357aaa | 358{19:NEW1} | 359^ {19:NEW2} | 360bbb | 361ccc | 362{5:-- INSERT --} | 363]] , 364} ) 365end ) 366 367it ( 'line lengths are correct when pressing TAB with folding #29119' , function () 368api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a' , 'b' } ) 369 370exec_lua ( function () 371_G . res ={} 372vim . o . foldmethod ='indent' 373vim . o . softtabstop =- 1 374vim . api . nvim_buf_attach ( 0 , false , { 375on_lines =function ( _ , bufnr , _ , row , _ , end_row ) 376local lines =vim . api . nvim_buf_get_lines ( bufnr , row , end_row , true ) 377table . insert ( _G . res , lines ) 378end , 379} ) 380end ) 381 382feed ( 'i<Tab>' ) 383eq ( { '\ta' } , exec_lua ( 'return _G.res[#_G.res]' )) 384end ) 385 386it ( 'quickfix buffer send change' , function () 387command ( 'copen' ) 388exec_lua ( function () 389vim . api . nvim_buf_attach ( vim . api . nvim_get_current_buf (), false , { 390on_lines =function ( ... ) 391vim . g . qf_on_lines ={ ... } 392end , 393on_bytes =function ( ... ) 394vim . g . qf_on_bytes ={ ... } 395end , 396} ) 397end ) 398command ( 'caddexpr "foo"' ) 399eq ( { 'bytes' , 2 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , 6 } , api . nvim_get_var ( 'qf_on_bytes' )) 400eq ( { 'lines' , 2 , 3 , 0 , 1 , 1 , 1 } , api . nvim_get_var ( 'qf_on_lines' )) 401 402command ( 'caddexpr "bar"' ) 403eq ( { 'bytes' , 2 , 3 , 0 , 6 , 6 , 0 , 0 , 0 , 1 , 6 , 6 } , api . nvim_get_var ( 'qf_on_bytes' )) 404eq ( { 'lines' , 2 , 4 , 1 , 1 , 2 , 0 } , api . nvim_get_var ( 'qf_on_lines' )) 405 406command ( 'caddexpr ["line1", "line2", "line3"]' ) 407eq ( { 'bytes' , 2 , 4 , 1 , 6 , 13 , 0 , 0 , 0 , 3 , 8 , 26 } , api . nvim_get_var ( 'qf_on_bytes' )) 408eq ( { 'lines' , 2 , 5 , 2 , 2 , 5 , 0 } , api . nvim_get_var ( 'qf_on_lines' )) 409 410command ( 'cexpr "replace"' ) 411eq ( { 'bytes' , 2 , 5 , 0 , 0 , 0 , 4 , 0 , 40 , 0 , 10 , 10 } , api . nvim_get_var ( 'qf_on_bytes' )) 412eq ( { 'lines' , 2 , 6 , 0 , 5 , 1 , 42 } , api . nvim_get_var ( 'qf_on_lines' )) 413end ) 414 415it ( 'single-line visual block insert should not trigger extra on_lines #22009' , function () 416exec_lua ( function () 417_G . res ={} 418vim . api . nvim_buf_attach ( 0 , false , { 419on_lines =function ( _ , bufnr , _ , first , last , last_updated , _ , _ , _ ) 420_G . res ={ bufnr , first , last , last_updated } 421end , 422} ) 423end ) 424feed ( '<C-v>I <ESC>' ) 425eq ( { api . nvim_get_current_buf (), 0 , 1 , 1 } , exec_lua ( 'return _G.res' )) 426end ) 427 428it ( 'prompt buffer' , function () 429local check_events =setup_eventcheck ( false , nil , {} ) 430api . nvim_set_option_value ( 'buftype' , 'prompt' , {} ) 431feed ( 'i' ) 432check_events { 433{ 'test1' , 'lines' , 1 , 4 , 0 , 1 , 1 , 1 } , 434} 435fn . prompt_setprompt ( '' , 'foo > ' ) 436check_events { 437{ 'test1' , 'lines' , 1 , 5 , 0 , 1 , 1 , 3 } , 438} 439feed ( 'hello' ) 440check_events { 441{ 'test1' , 'lines' , 1 , 6 , 0 , 1 , 1 , 7 } , 442} 443fn . prompt_setprompt ( '' , 'super-foo > ' ) 444check_events { 445{ 'test1' , 'lines' , 1 , 7 , 0 , 1 , 1 , 12 } , 446} 447eq ( { 'super-foo > hello' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 448-- Do this in the same event. 449exec_lua ( function () 450vim . fn . setpos ( "':" , { 0 , 1 , 999 , 0 } ) 451vim . fn . prompt_setprompt ( '' , 'discard > ' ) 452end ) 453check_events { 454{ 'test1' , 'lines' , 1 , 8 , 0 , 1 , 1 , 18 } , 455} 456eq ( { 'discard > ' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 457feed ( 'hello<S-CR>there' ) 458check_events { 459{ 'test1' , 'lines' , 1 , 9 , 0 , 1 , 1 , 11 } , 460{ 'test1' , 'lines' , 1 , 10 , 0 , 1 , 2 , 16 } , 461{ 'test1' , 'lines' , 1 , 11 , 1 , 2 , 2 , 1 } , 462} 463fn . prompt_setprompt ( '' , 'foo > ' ) 464check_events { 465{ 'test1' , 'lines' , 1 , 12 , 0 , 1 , 1 , 16 } , 466} 467eq ( { 'foo > hello' , 'there' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 468 469-- init_prompt uses appended_lines_mark when appending to fix prompt. 470api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'hi' } ) 471eq ( { 'hi' , 'foo > ' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 472check_events { 473{ 'test1' , 'lines' , 1 , 13 , 0 , 2 , 1 , 18 } , 474{ 'test1' , 'lines' , 1 , 14 , 1 , 1 , 2 , 0 } , 475} 476end ) 477end ) 478 479describe ( 'lua: nvim_buf_attach on_bytes' , function () 480-- verifying the sizes with nvim_buf_get_offset is nice (checks we cannot 481-- assert the wrong thing), but masks errors with unflushed lines (as 482-- nvim_buf_get_offset forces a flush of the memline). To be safe run the 483-- test both ways. 484local function setup_eventcheck ( verify , start_txt ) 485if start_txt then 486api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , start_txt ) 487else 488start_txt =api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 489end 490local shadowbytes =table . concat ( start_txt , '\n' ) .. '\n' 491-- TODO: while we are brewing the real strong coffee, 492-- verify should check buf_get_offset after every check_events 493if verify then 494local len =api . nvim_buf_get_offset ( 0 , api . nvim_buf_line_count ( 0 )) 495eq ( len == - 1 and 1 or len , string . len ( shadowbytes )) 496end 497exec_lua ( 'return test_register(...)' , 0 , 'on_bytes' , 'test1' , false , false , true ) 498api . nvim_buf_get_changedtick ( 0 ) 499 500local verify_name ='test1' 501local function check_events ( expected ) 502local events =exec_lua ( 'return get_events(...)' ) 503expect_events ( expected , events , 'byte updates' ) 504 505if not verify then 506return 507end 508 509for _ , event in ipairs ( events ) do 510for _ , elem in ipairs ( event ) do 511if type ( elem ) == 'number' and elem < 0 then 512error ( 'Received event has negative values' ) 513end 514end 515 516if event [ 1 ] == verify_name and event [ 2 ] == 'bytes' then 517local _ , _ , _ , _ , _ , _ , start_byte , _ , _ , old_byte , _ , _ , new_byte =unpack ( event ) 518local before =string . sub ( shadowbytes , 1 , start_byte ) 519-- no text in the tests will contain 0xff bytes (invalid UTF-8) 520-- so we can use it as marker for unknown bytes 521local unknown =string . rep ( '\255' , new_byte ) 522local after =string . sub ( shadowbytes , start_byte + old_byte + 1 ) 523shadowbytes =before .. unknown .. after 524elseif event [ 1 ] == verify_name and event [ 2 ] == 'reload' then 525shadowbytes =table . concat ( api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), '\n' ) .. '\n' 526end 527end 528 529local text =api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 530local bytes =table . concat ( text , '\n' ) .. '\n' 531 532eq ( 533string . len ( bytes ), 534string . len ( shadowbytes ), 535'\non_bytes: total bytecount of buffer is wrong' 536) 537for i =1 , string . len ( shadowbytes ) do 538local shadowbyte =string . sub ( shadowbytes , i , i ) 539if shadowbyte ~= '\255' then 540eq ( string . sub ( bytes , i , i ), shadowbyte , i ) 541end 542end 543end 544 545return check_events 546end 547 548-- Yes, we can do both 549local function do_both ( verify ) 550it ( 'single and multiple join' , function () 551local check_events =setup_eventcheck ( verify , origlines ) 552feed 'ggJ' 553check_events { 554{ 'test1' , 'bytes' , 1 , 3 , 0 , 15 , 15 , 1 , 0 , 1 , 0 , 1 , 1 } , 555} 556 557feed '3J' 558check_events { 559{ 'test1' , 'bytes' , 1 , 5 , 0 , 31 , 31 , 1 , 0 , 1 , 0 , 1 , 1 } , 560{ 'test1' , 'bytes' , 1 , 5 , 0 , 47 , 47 , 1 , 0 , 1 , 0 , 1 , 1 } , 561} 562end ) 563 564it ( 'opening lines' , function () 565local check_events =setup_eventcheck ( verify , origlines ) 566api . nvim_set_option_value ( 'autoindent' , false , {} ) 567feed 'Go' 568check_events { 569{ 'test1' , 'bytes' , 1 , 3 , 7 , 0 , 114 , 0 , 0 , 0 , 1 , 0 , 1 } , 570} 571feed '<cr>' 572check_events { 573{ 'test1' , 'bytes' , 1 , 4 , 7 , 0 , 114 , 0 , 0 , 0 , 1 , 0 , 1 } , 574} 575end ) 576 577it ( 'opening lines with autoindent' , function () 578local check_events =setup_eventcheck ( verify , origlines ) 579api . nvim_set_option_value ( 'autoindent' , true , {} ) 580feed 'Go' 581check_events { 582{ 'test1' , 'bytes' , 1 , 3 , 7 , 0 , 114 , 0 , 0 , 0 , 1 , 0 , 5 } , 583} 584feed '<cr>' 585check_events { 586{ 'test1' , 'bytes' , 1 , 4 , 7 , 0 , 114 , 0 , 4 , 4 , 0 , 0 , 0 } , 587{ 'test1' , 'bytes' , 1 , 4 , 7 , 0 , 114 , 0 , 0 , 0 , 1 , 4 , 5 } , 588} 589end ) 590 591it ( 'setline(num, line)' , function () 592local check_events =setup_eventcheck ( verify , origlines ) 593fn . setline ( 2 , 'babla' ) 594check_events { 595{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 16 , 0 , 15 , 15 , 0 , 5 , 5 } , 596} 597 598fn . setline ( 2 , { 'foo' , 'bar' } ) 599check_events { 600{ 'test1' , 'bytes' , 1 , 4 , 1 , 0 , 16 , 0 , 5 , 5 , 0 , 3 , 3 } , 601{ 'test1' , 'bytes' , 1 , 5 , 2 , 0 , 20 , 0 , 15 , 15 , 0 , 3 , 3 } , 602} 603 604local buf_len =api . nvim_buf_line_count ( 0 ) 605fn . setline ( buf_len + 1 , 'baz' ) 606check_events { 607{ 'test1' , 'bytes' , 1 , 6 , 7 , 0 , 90 , 0 , 0 , 0 , 1 , 0 , 4 } , 608} 609end ) 610 611it ( 'continuing comments with fo=or' , function () 612local check_events =setup_eventcheck ( verify , { '// Comment' } ) 613api . nvim_set_option_value ( 'formatoptions' , 'ro' , {} ) 614api . nvim_set_option_value ( 'filetype' , 'c' , {} ) 615feed 'A<CR>' 616check_events { 617{ 'test1' , 'bytes' , 1 , 3 , 0 , 10 , 10 , 0 , 0 , 0 , 1 , 3 , 4 } , 618} 619 620feed '<ESC>' 621check_events { 622{ 'test1' , 'bytes' , 1 , 4 , 1 , 2 , 13 , 0 , 1 , 1 , 0 , 0 , 0 } , 623} 624 625feed 'ggo' -- goto first line to continue testing 626check_events { 627{ 'test1' , 'bytes' , 1 , 5 , 1 , 0 , 11 , 0 , 0 , 0 , 1 , 0 , 4 } , 628} 629 630feed '<CR>' 631check_events { 632{ 'test1' , 'bytes' , 1 , 6 , 1 , 2 , 13 , 0 , 1 , 1 , 0 , 0 , 0 } , 633{ 'test1' , 'bytes' , 1 , 6 , 1 , 2 , 13 , 0 , 0 , 0 , 1 , 3 , 4 } , 634} 635end ) 636 637it ( 'editing empty buffers' , function () 638local check_events =setup_eventcheck ( verify , {} ) 639 640feed 'ia' 641check_events { 642{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 } , 643} 644end ) 645 646it ( 'deleting lines' , function () 647local check_events =setup_eventcheck ( verify , origlines ) 648 649feed ( 'dd' ) 650 651check_events { 652{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 1 , 0 , 16 , 0 , 0 , 0 } , 653} 654 655feed ( 'd2j' ) 656 657check_events { 658{ 'test1' , 'bytes' , 1 , 4 , 0 , 0 , 0 , 3 , 0 , 48 , 0 , 0 , 0 } , 659} 660 661feed ( 'ld<c-v>2j' ) 662 663check_events { 664{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 } , 665{ 'test1' , 'bytes' , 1 , 5 , 1 , 1 , 16 , 0 , 1 , 1 , 0 , 0 , 0 } , 666{ 'test1' , 'bytes' , 1 , 5 , 2 , 1 , 31 , 0 , 1 , 1 , 0 , 0 , 0 } , 667} 668 669feed ( 'vjwd' ) 670 671check_events { 672{ 'test1' , 'bytes' , 1 , 10 , 0 , 1 , 1 , 1 , 9 , 23 , 0 , 0 , 0 } , 673} 674end ) 675 676it ( 'changing lines' , function () 677local check_events =setup_eventcheck ( verify , origlines ) 678 679feed 'cc' 680check_events { 681{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 15 , 15 , 0 , 0 , 0 } , 682} 683 684feed '<ESC>' 685check_events {} 686 687feed 'c3j' 688check_events { 689{ 'test1' , 'bytes' , 1 , 4 , 1 , 0 , 1 , 3 , 0 , 48 , 0 , 0 , 0 } , 690} 691end ) 692 693it ( 'visual charwise paste' , function () 694local check_events =setup_eventcheck ( verify , { '1234567890' } ) 695fn . setreg ( 'a' , '___' ) 696 697feed '1G1|vll' 698check_events {} 699 700feed '"ap' 701check_events { 702{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 3 , 3 , 0 , 0 , 0 } , 703{ 'test1' , 'bytes' , 1 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , 3 } , 704} 705end ) 706 707it ( 'blockwise paste' , function () 708local check_events =setup_eventcheck ( verify , { '1' , '2' , '3' } ) 709feed ( '1G0' ) 710feed ( 'y<C-v>2j' ) 711feed ( 'G0' ) 712feed ( 'p' ) 713check_events { 714{ 'test1' , 'bytes' , 1 , 3 , 2 , 1 , 5 , 0 , 0 , 0 , 0 , 1 , 1 } , 715{ 'test1' , 'bytes' , 1 , 3 , 3 , 0 , 7 , 0 , 0 , 0 , 0 , 3 , 3 } , 716{ 'test1' , 'bytes' , 1 , 3 , 4 , 0 , 10 , 0 , 0 , 0 , 0 , 3 , 3 } , 717} 718 719feed ( '2G0' ) 720feed ( 'p' ) 721check_events { 722{ 'test1' , 'bytes' , 1 , 4 , 1 , 1 , 3 , 0 , 0 , 0 , 0 , 1 , 1 } , 723{ 'test1' , 'bytes' , 1 , 4 , 2 , 1 , 6 , 0 , 0 , 0 , 0 , 1 , 1 } , 724{ 'test1' , 'bytes' , 1 , 4 , 3 , 1 , 10 , 0 , 0 , 0 , 0 , 1 , 1 } , 725} 726 727feed ( '1G0' ) 728feed ( 'P' ) 729check_events { 730{ 'test1' , 'bytes' , 1 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 } , 731{ 'test1' , 'bytes' , 1 , 5 , 1 , 0 , 3 , 0 , 0 , 0 , 0 , 1 , 1 } , 732{ 'test1' , 'bytes' , 1 , 5 , 2 , 0 , 7 , 0 , 0 , 0 , 0 , 1 , 1 } , 733} 734end ) 735 736it ( 'linewise paste' , function () 737local check_events =setup_eventcheck ( verify , origlines ) 738 739feed 'yyp' 740check_events { 741{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 16 , 0 , 0 , 0 , 1 , 0 , 16 } , 742} 743 744feed 'Gyyp' 745check_events { 746{ 'test1' , 'bytes' , 1 , 4 , 8 , 0 , 130 , 0 , 0 , 0 , 1 , 0 , 18 } , 747} 748end ) 749 750it ( 'inccomand=nosplit and substitute' , function () 751local check_events =setup_eventcheck ( verify , { 'abcde' , '12345' } ) 752api . nvim_set_option_value ( 'inccommand' , 'nosplit' , {} ) 753 754-- linewise substitute 755feed ( ':%s/bcd/' ) 756check_events { 757{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 3 , 3 , 0 , 0 , 0 } , 758{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 3 , 3 } , 759} 760 761feed ( 'a' ) 762check_events { 763{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 3 , 3 , 0 , 1 , 1 } , 764{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 3 , 3 } , 765} 766 767feed ( '<esc>' ) 768 769-- splitting lines 770feed ( [[:%s/abc/\r]] ) 771check_events { 772{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 3 , 3 , 1 , 0 , 1 } , 773{ 'test1' , 'bytes' , 1 , 6 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 3 , 3 } , 774} 775 776feed ( '<esc>' ) 777-- multi-line regex 778feed ( [[:%s/de\n123/a]] ) 779 780check_events { 781{ 'test1' , 'bytes' , 1 , 3 , 0 , 3 , 3 , 1 , 3 , 6 , 0 , 1 , 1 } , 782{ 'test1' , 'bytes' , 1 , 6 , 0 , 3 , 3 , 0 , 1 , 1 , 1 , 3 , 6 } , 783} 784 785feed ( '<esc>' ) 786-- replacing with unicode 787feed ( ':%s/b/→' ) 788 789check_events { 790{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 3 , 3 } , 791{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 3 , 3 , 0 , 1 , 1 } , 792} 793 794feed ( '<esc>' ) 795-- replacing with expression register 796feed ( [[:%s/b/\=5+5]] ) 797check_events { 798{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 2 , 2 } , 799{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 2 , 2 , 0 , 1 , 1 } , 800} 801 802feed ( '<esc>' ) 803-- replacing with backslash 804feed ( [[:%s/b/\\]] ) 805check_events { 806{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 } , 807{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 } , 808} 809 810feed ( '<esc>' ) 811-- replacing with backslash from expression register 812feed ( [[:%s/b/\='\']] ) 813check_events { 814{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 } , 815{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 } , 816} 817 818feed ( '<esc>' ) 819-- replacing with backslash followed by another character 820feed ( [[:%s/b/\\!]] ) 821check_events { 822{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 2 , 2 } , 823{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 2 , 2 , 0 , 1 , 1 } , 824} 825 826feed ( '<esc>' ) 827-- replacing with backslash followed by another character from expression register 828feed ( [[:%s/b/\='\!']] ) 829check_events { 830{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 2 , 2 } , 831{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 2 , 2 , 0 , 1 , 1 } , 832} 833end ) 834 835it ( 'nvim_buf_set_text insert' , function () 836local check_events =setup_eventcheck ( verify , { 'bastext' } ) 837api . nvim_buf_set_text ( 0 , 0 , 3 , 0 , 3 , { 'fiol' , 'kontra' } ) 838check_events { 839{ 'test1' , 'bytes' , 1 , 3 , 0 , 3 , 3 , 0 , 0 , 0 , 1 , 6 , 11 } , 840} 841 842api . nvim_buf_set_text ( 0 , 1 , 6 , 1 , 6 , { 'punkt' , 'syntgitarr' , 'övnings' } ) 843check_events { 844{ 'test1' , 'bytes' , 1 , 4 , 1 , 6 , 14 , 0 , 0 , 0 , 2 , 8 , 25 } , 845} 846 847eq ( 848{ 'basfiol' , 'kontrapunkt' , 'syntgitarr' , 'övningstext' } , 849api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 850) 851end ) 852 853it ( 'nvim_buf_set_text replace' , function () 854local check_events =setup_eventcheck ( verify , origlines ) 855 856api . nvim_buf_set_text ( 0 , 2 , 3 , 2 , 8 , { 'very text' } ) 857check_events { 858{ 'test1' , 'bytes' , 1 , 3 , 2 , 3 , 35 , 0 , 5 , 5 , 0 , 9 , 9 } , 859} 860 861api . nvim_buf_set_text ( 0 , 3 , 5 , 3 , 7 , { ' splitty' , 'line ' } ) 862check_events { 863{ 'test1' , 'bytes' , 1 , 4 , 3 , 5 , 57 , 0 , 2 , 2 , 1 , 5 , 14 } , 864} 865 866api . nvim_buf_set_text ( 0 , 0 , 8 , 1 , 2 , { 'JOINY' } ) 867check_events { 868{ 'test1' , 'bytes' , 1 , 5 , 0 , 8 , 8 , 1 , 2 , 10 , 0 , 5 , 5 } , 869} 870 871api . nvim_buf_set_text ( 0 , 4 , 0 , 6 , 0 , { 'was 5,6' , '' } ) 872check_events { 873{ 'test1' , 'bytes' , 1 , 6 , 4 , 0 , 75 , 2 , 0 , 32 , 1 , 0 , 8 } , 874} 875 876eq ( { 877'originalJOINYiginal line 2' , 878'orivery text line 3' , 879'origi splitty' , 880'line l line 4' , 881'was 5,6' , 882' indented line' , 883} , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 884end ) 885 886it ( 'nvim_buf_set_text delete' , function () 887local check_events =setup_eventcheck ( verify , origlines ) 888 889-- really {""} but accepts {} as a shorthand 890api . nvim_buf_set_text ( 0 , 0 , 0 , 1 , 0 , {} ) 891check_events { 892{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 1 , 0 , 16 , 0 , 0 , 0 } , 893} 894 895-- TODO(bfredl): this works but is not as convenient as set_lines 896api . nvim_buf_set_text ( 0 , 4 , 15 , 5 , 17 , { '' } ) 897check_events { 898{ 'test1' , 'bytes' , 1 , 4 , 4 , 15 , 79 , 1 , 17 , 18 , 0 , 0 , 0 } , 899} 900eq ( { 901'original line 2' , 902'original line 3' , 903'original line 4' , 904'original line 5' , 905'original line 6' , 906} , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 907end ) 908 909it ( 'checktime autoread' , function () 910write_file ( 911'Xtest-reload' , 912dedent [[ 913old line 1 914old line 2]] 915) 916local atime =os . time () - 10 917vim . uv . fs_utime ( 'Xtest-reload' , atime , atime ) 918command 'e Xtest-reload' 919command 'set autoread' 920 921local check_events =setup_eventcheck ( verify , nil ) 922 923write_file ( 924'Xtest-reload' , 925dedent [[ 926new line 1 927new line 2 928new line 3]] 929) 930 931command 'checktime' 932check_events { 933{ 'test1' , 'reload' , 1 } , 934} 935 936feed 'ggJ' 937check_events { 938{ 'test1' , 'bytes' , 1 , 5 , 0 , 10 , 10 , 1 , 0 , 1 , 0 , 1 , 1 } , 939} 940 941eq ( { 'new line 1 new line 2' , 'new line 3' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 942 943-- check we can undo and redo a reload event. 944feed 'u' 945check_events { 946{ 'test1' , 'bytes' , 1 , 8 , 0 , 10 , 10 , 0 , 1 , 1 , 1 , 0 , 1 } , 947} 948 949feed 'u' 950check_events { 951{ 'test1' , 'reload' , 1 } , 952} 953 954feed '<c-r>' 955check_events { 956{ 'test1' , 'reload' , 1 } , 957} 958 959feed '<c-r>' 960check_events { 961{ 'test1' , 'bytes' , 1 , 14 , 0 , 10 , 10 , 1 , 0 , 1 , 0 , 1 , 1 } , 962} 963end ) 964 965it ( 'tab with noexpandtab and softtabstop' , function () 966command ( 'set noet' ) 967command ( 'set ts=4' ) 968command ( 'set sw=2' ) 969command ( 'set sts=4' ) 970 971local check_events =setup_eventcheck ( verify , { 'asdfasdf' } ) 972 973feed ( 'gg0i<tab>' ) 974 975check_events { 976{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 } , 977{ 'test1' , 'bytes' , 1 , 4 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 } , 978} 979feed ( '<tab>' ) 980 981-- when spaces are merged into a tabstop 982check_events { 983{ 'test1' , 'bytes' , 1 , 5 , 0 , 2 , 2 , 0 , 0 , 0 , 0 , 1 , 1 } , 984{ 'test1' , 'bytes' , 1 , 6 , 0 , 3 , 3 , 0 , 0 , 0 , 0 , 1 , 1 } , 985{ 'test1' , 'bytes' , 1 , 7 , 0 , 0 , 0 , 0 , 4 , 4 , 0 , 1 , 1 } , 986} 987 988feed ( '<esc>u' ) 989check_events { 990{ 'test1' , 'bytes' , 1 , 9 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 4 , 4 } , 991{ 'test1' , 'bytes' , 1 , 9 , 0 , 0 , 0 , 0 , 4 , 4 , 0 , 0 , 0 } , 992} 993 994-- in REPLACE mode 995feed ( 'R<tab><tab>' ) 996check_events { 997{ 'test1' , 'bytes' , 1 , 10 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 } , 998{ 'test1' , 'bytes' , 1 , 11 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 } , 999{ 'test1' , 'bytes' , 1 , 12 , 0 , 2 , 2 , 0 , 1 , 1 , 0 , 1 , 1 } , 1000{ 'test1' , 'bytes' , 1 , 13 , 0 , 3 , 3 , 0 , 0 , 0 , 0 , 1 , 1 } , 1001{ 'test1' , 'bytes' , 1 , 14 , 0 , 0 , 0 , 0 , 4 , 4 , 0 , 1 , 1 } , 1002} 1003feed ( '<esc>u' ) 1004check_events { 1005{ 'test1' , 'bytes' , 1 , 16 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 4 , 4 } , 1006{ 'test1' , 'bytes' , 1 , 16 , 0 , 2 , 2 , 0 , 2 , 2 , 0 , 1 , 1 } , 1007{ 'test1' , 'bytes' , 1 , 16 , 0 , 0 , 0 , 0 , 2 , 2 , 0 , 1 , 1 } , 1008} 1009 1010-- in VISUALREPLACE mode 1011feed ( 'gR<tab><tab>' ) 1012check_events { 1013{ 'test1' , 'bytes' , 1 , 17 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 } , 1014{ 'test1' , 'bytes' , 1 , 18 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 } , 1015{ 'test1' , 'bytes' , 1 , 19 , 0 , 2 , 2 , 0 , 1 , 1 , 0 , 1 , 1 } , 1016{ 'test1' , 'bytes' , 1 , 20 , 0 , 3 , 3 , 0 , 1 , 1 , 0 , 1 , 1 } , 1017{ 'test1' , 'bytes' , 1 , 21 , 0 , 3 , 3 , 0 , 1 , 1 , 0 , 0 , 0 } , 1018{ 'test1' , 'bytes' , 1 , 22 , 0 , 3 , 3 , 0 , 0 , 0 , 0 , 1 , 1 } , 1019{ 'test1' , 'bytes' , 1 , 24 , 0 , 2 , 2 , 0 , 1 , 1 , 0 , 0 , 0 } , 1020{ 'test1' , 'bytes' , 1 , 25 , 0 , 2 , 2 , 0 , 0 , 0 , 0 , 1 , 1 } , 1021{ 'test1' , 'bytes' , 1 , 27 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 } , 1022{ 'test1' , 'bytes' , 1 , 28 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 } , 1023{ 'test1' , 'bytes' , 1 , 30 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 } , 1024{ 'test1' , 'bytes' , 1 , 31 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 } , 1025{ 'test1' , 'bytes' , 1 , 33 , 0 , 0 , 0 , 0 , 4 , 4 , 0 , 1 , 1 } , 1026} 1027 1028-- inserting tab after other tabs 1029command ( 'set sw=4' ) 1030feed ( '<esc>0a<tab>' ) 1031check_events { 1032{ 'test1' , 'bytes' , 1 , 34 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 } , 1033{ 'test1' , 'bytes' , 1 , 35 , 0 , 2 , 2 , 0 , 0 , 0 , 0 , 1 , 1 } , 1034{ 'test1' , 'bytes' , 1 , 36 , 0 , 3 , 3 , 0 , 0 , 0 , 0 , 1 , 1 } , 1035{ 'test1' , 'bytes' , 1 , 37 , 0 , 4 , 4 , 0 , 0 , 0 , 0 , 1 , 1 } , 1036{ 'test1' , 'bytes' , 1 , 38 , 0 , 1 , 1 , 0 , 4 , 4 , 0 , 1 , 1 } , 1037} 1038end ) 1039 1040it ( 'retab' , function () 1041command ( 'set noet' ) 1042command ( 'set ts=4' ) 1043 1044local check_events =setup_eventcheck ( verify , { ' asdf' } ) 1045command ( 'retab 8' ) 1046 1047check_events { 1048{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 7 , 7 , 0 , 9 , 9 } , 1049} 1050end ) 1051 1052it ( 'sends events when undoing with undofile' , function () 1053write_file ( 1054'Xtest-undofile' , 1055dedent ( [[ 105612345 1057hello world 1058]] ) 1059) 1060 1061command ( 'e! Xtest-undofile' ) 1062command ( 'set undodir=. | set undofile' ) 1063 1064local ns =n . request ( 'nvim_create_namespace' , 'ns1' ) 1065api . nvim_buf_set_extmark ( 0 , ns , 0 , 0 , {} ) 1066 1067eq ( { '12345' , 'hello world' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1068 1069-- splice 1070feed ( 'gg0d2l' ) 1071 1072eq ( { '345' , 'hello world' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1073 1074-- move 1075command ( '.m+1' ) 1076 1077eq ( { 'hello world' , '345' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1078 1079-- reload undofile and undo changes 1080command ( 'w' ) 1081command ( 'set noundofile' ) 1082command ( 'bw!' ) 1083command ( 'e! Xtest-undofile' ) 1084 1085command ( 'set undofile' ) 1086 1087local check_events =setup_eventcheck ( verify , nil ) 1088 1089feed ( 'u' ) 1090eq ( { '345' , 'hello world' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1091 1092check_events { 1093{ 'test1' , 'bytes' , 2 , 6 , 1 , 0 , 12 , 1 , 0 , 4 , 0 , 0 , 0 } , 1094{ 'test1' , 'bytes' , 2 , 6 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 4 } , 1095} 1096 1097feed ( 'u' ) 1098eq ( { '12345' , 'hello world' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1099 1100check_events { 1101{ 'test1' , 'bytes' , 2 , 8 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 2 } , 1102} 1103command ( 'bw!' ) 1104end ) 1105 1106it ( 'blockwise paste with uneven line lengths' , function () 1107local check_events =setup_eventcheck ( verify , { 'aaaa' , 'aaa' , 'aaa' } ) 1108 1109-- eq({}, api.nvim_buf_get_lines(0, 0, -1, true)) 1110feed ( 'gg0<c-v>jj$d' ) 1111 1112check_events { 1113{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 4 , 4 , 0 , 0 , 0 } , 1114{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 1 , 0 , 3 , 3 , 0 , 0 , 0 } , 1115{ 'test1' , 'bytes' , 1 , 3 , 2 , 0 , 2 , 0 , 3 , 3 , 0 , 0 , 0 } , 1116} 1117 1118feed ( 'p' ) 1119check_events { 1120{ 'test1' , 'bytes' , 1 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , 4 } , 1121{ 'test1' , 'bytes' , 1 , 4 , 1 , 0 , 5 , 0 , 0 , 0 , 0 , 3 , 3 } , 1122{ 'test1' , 'bytes' , 1 , 4 , 2 , 0 , 9 , 0 , 0 , 0 , 0 , 3 , 3 } , 1123} 1124end ) 1125 1126it ( ':luado' , function () 1127local check_events =setup_eventcheck ( verify , { 'abc' , '12345' } ) 1128 1129command ( ".luado return 'a'" ) 1130 1131check_events { 1132{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 3 , 3 , 0 , 1 , 1 } , 1133} 1134 1135command ( 'luado return 10' ) 1136 1137check_events { 1138{ 'test1' , 'bytes' , 1 , 4 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 2 , 2 } , 1139{ 'test1' , 'bytes' , 1 , 5 , 1 , 0 , 3 , 0 , 5 , 5 , 0 , 2 , 2 } , 1140} 1141end ) 1142 1143it ( 'flushes deleted bytes on move' , function () 1144local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' , 'CCC' , 'DDD' } ) 1145 1146feed ( ':.move+1<cr>' ) 1147 1148check_events { 1149{ 'test1' , 'bytes' , 1 , 5 , 0 , 0 , 0 , 1 , 0 , 4 , 0 , 0 , 0 } , 1150{ 'test1' , 'bytes' , 1 , 5 , 1 , 0 , 4 , 0 , 0 , 0 , 1 , 0 , 4 } , 1151} 1152 1153feed ( 'jd2j' ) 1154 1155check_events { 1156{ 'test1' , 'bytes' , 1 , 6 , 2 , 0 , 8 , 2 , 0 , 8 , 0 , 0 , 0 } , 1157} 1158end ) 1159 1160it ( 'virtual edit' , function () 1161local check_events =setup_eventcheck ( verify , { '' , ' ' } ) 1162 1163api . nvim_set_option_value ( 'virtualedit' , 'all' , {} ) 1164 1165feed [[<Right><Right>iab<ESC>]] 1166 1167check_events { 1168{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 2 } , 1169{ 'test1' , 'bytes' , 1 , 4 , 0 , 2 , 2 , 0 , 0 , 0 , 0 , 2 , 2 } , 1170} 1171 1172feed [[j<Right><Right>iab<ESC>]] 1173 1174check_events { 1175{ 'test1' , 'bytes' , 1 , 5 , 1 , 0 , 5 , 0 , 1 , 1 , 0 , 8 , 8 } , 1176{ 'test1' , 'bytes' , 1 , 6 , 1 , 5 , 10 , 0 , 0 , 0 , 0 , 2 , 2 } , 1177} 1178end ) 1179 1180it ( 'block visual paste' , function () 1181local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' , 'CCC' , 'DDD' , 'EEE' , 'FFF' } ) 1182fn . setreg ( 'a' , '___' ) 1183feed ( [[gg0l<c-v>3jl"ap]] ) 1184 1185check_events { 1186{ 'test1' , 'bytes' , 1 , 3 , 0 , 1 , 1 , 0 , 2 , 2 , 0 , 0 , 0 } , 1187{ 'test1' , 'bytes' , 1 , 3 , 1 , 1 , 3 , 0 , 2 , 2 , 0 , 0 , 0 } , 1188{ 'test1' , 'bytes' , 1 , 3 , 2 , 1 , 5 , 0 , 2 , 2 , 0 , 0 , 0 } , 1189{ 'test1' , 'bytes' , 1 , 3 , 3 , 1 , 7 , 0 , 2 , 2 , 0 , 0 , 0 } , 1190{ 'test1' , 'bytes' , 1 , 5 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 3 , 3 } , 1191{ 'test1' , 'bytes' , 1 , 6 , 1 , 1 , 6 , 0 , 0 , 0 , 0 , 3 , 3 } , 1192{ 'test1' , 'bytes' , 1 , 7 , 2 , 1 , 11 , 0 , 0 , 0 , 0 , 3 , 3 } , 1193{ 'test1' , 'bytes' , 1 , 8 , 3 , 1 , 16 , 0 , 0 , 0 , 0 , 3 , 3 } , 1194} 1195end ) 1196 1197it ( 'visual paste' , function () 1198local check_events =setup_eventcheck ( verify , { 'aaa {' , 'b' , '}' } ) 1199-- Setting up 1200feed [[jdd]] 1201check_events { 1202{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 6 , 1 , 0 , 2 , 0 , 0 , 0 } , 1203} 1204 1205-- Actually testing 1206feed [[v%p]] 1207check_events { 1208{ 'test1' , 'bytes' , 1 , 8 , 0 , 4 , 4 , 1 , 1 , 3 , 0 , 0 , 0 } , 1209{ 'test1' , 'bytes' , 1 , 8 , 0 , 4 , 4 , 0 , 0 , 0 , 2 , 0 , 3 } , 1210} 1211end ) 1212 1213it ( 'visual paste 2: splitting an empty line' , function () 1214local check_events =setup_eventcheck ( verify , { 'abc' , '{' , 'def' , '}' } ) 1215feed ( 'ggyyjjvi{p' ) 1216check_events { 1217{ 'test1' , 'bytes' , 1 , 6 , 2 , 0 , 6 , 1 , 0 , 4 , 0 , 0 , 0 } , 1218{ 'test1' , 'bytes' , 1 , 6 , 2 , 0 , 6 , 0 , 0 , 0 , 2 , 0 , 5 } , 1219} 1220end ) 1221 1222it ( 'nvim_buf_set_lines' , function () 1223local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' } ) 1224 1225-- delete 1226api . nvim_buf_set_lines ( 0 , 0 , 1 , true , {} ) 1227 1228check_events { 1229{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 1 , 0 , 4 , 0 , 0 , 0 } , 1230} 1231 1232-- add 1233api . nvim_buf_set_lines ( 0 , 0 , 0 , true , { 'asdf' } ) 1234check_events { 1235{ 'test1' , 'bytes' , 1 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 5 } , 1236} 1237 1238-- replace 1239api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { 'asdf' , 'fdsa' } ) 1240check_events { 1241{ 'test1' , 'bytes' , 1 , 5 , 0 , 0 , 0 , 1 , 0 , 5 , 2 , 0 , 10 } , 1242} 1243end ) 1244 1245it ( 'flushes delbytes on substitute' , function () 1246local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' , 'CCC' } ) 1247 1248feed ( 'gg0' ) 1249command ( 's/AAA/GGG/' ) 1250 1251check_events { 1252{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 3 , 3 , 0 , 3 , 3 } , 1253} 1254 1255-- check that byte updates for :delete (which uses curbuf->deleted_bytes2) 1256-- are correct 1257command ( 'delete' ) 1258check_events { 1259{ 'test1' , 'bytes' , 1 , 4 , 0 , 0 , 0 , 1 , 0 , 4 , 0 , 0 , 0 } , 1260} 1261end ) 1262 1263it ( 'on_bytes sees modified buffer after substitute' , function () 1264api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'Hello' } ) 1265 1266local buffer_lines =exec_lua ( function () 1267local lines 1268vim . api . nvim_buf_attach ( 0 , false , { 1269on_bytes =function () 1270lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 1271end , 1272} ) 1273vim . cmd ( 's/llo/y/' ) 1274return lines 1275end ) 1276 1277-- Make sure on_bytes is called after the buffer is modified. 1278eq ( { 'Hey' } , buffer_lines ) 1279end ) 1280 1281it ( 'on_bytes called multiple times for multiple substitutions on same line' , function () 1282api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'Hello Hello' } ) 1283 1284local call_count , args =exec_lua ( function () 1285local count =0 1286local args ={} 1287vim . api . nvim_buf_attach ( 0 , false , { 1288on_bytes =function ( 1289_ , 1290_ , 1291_ , 1292start_row , 1293start_col , 1294start_byte , 1295old_row , 1296old_col , 1297old_byte , 1298new_row , 1299new_col , 1300new_byte 1301) 1302count =count + 1 1303table . insert ( args , { 1304start_row =start_row , 1305start_col =start_col , 1306start_byte =start_byte , 1307old_row =old_row , 1308old_col =old_col , 1309old_byte =old_byte , 1310new_row =new_row , 1311new_col =new_col , 1312new_byte =new_byte , 1313buffer_lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), 1314} ) 1315end , 1316} ) 1317vim . cmd ( 's/llo/y/g' ) 1318return count , args 1319end ) 1320 1321-- Should be called twice, once for each match. 1322eq ( 2 , call_count ) 1323 1324-- First match: "llo" at column 2 -> "y". 1325eq ( { 1326start_row =0 , 1327start_col =2 , 1328start_byte =2 , 1329old_row =0 , 1330old_col =3 , 1331old_byte =3 , 1332new_row =0 , 1333new_col =1 , 1334new_byte =1 , 1335buffer_lines ={ 'Hey Hey' } , 1336} , args [ 1 ]) 1337 1338-- Second match: "llo" at column 8 (in original) -> column 6 (after first substitution). 1339eq ( { 1340start_row =0 , 1341start_col =6 , -- Adjusted position after first substitution. 1342start_byte =6 , 1343old_row =0 , 1344old_col =3 , 1345old_byte =3 , 1346new_row =0 , 1347new_col =1 , 1348new_byte =1 , 1349buffer_lines ={ 'Hey Hey' } , 1350} , args [ 2 ]) 1351end ) 1352 1353it ( 'on_bytes called correctly for multi-line substitutions' , function () 1354api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar' , 'baz qux' } ) 1355 1356local call_count , args =exec_lua ( function () 1357local count =0 1358local args ={} 1359vim . api . nvim_buf_attach ( 0 , false , { 1360on_bytes =function ( 1361_ , 1362_ , 1363_ , 1364start_row , 1365start_col , 1366start_byte , 1367old_row , 1368old_col , 1369old_byte , 1370new_row , 1371new_col , 1372new_byte 1373) 1374count =count + 1 1375table . insert ( args , { 1376start_row =start_row , 1377start_col =start_col , 1378start_byte =start_byte , 1379old_row =old_row , 1380old_col =old_col , 1381old_byte =old_byte , 1382new_row =new_row , 1383new_col =new_col , 1384new_byte =new_byte , 1385buffer_lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), 1386} ) 1387end , 1388} ) 1389vim . cmd ( 's/bar/X\\rY/' ) 1390return count , args 1391end ) 1392 1393-- Should be called once for the substitution. 1394eq ( 1 , call_count ) 1395 1396eq ( { 1397start_row =0 , 1398start_col =4 , 1399start_byte =4 , 1400old_row =0 , 1401old_col =3 , 1402old_byte =3 , 1403new_row =1 , 1404new_col =1 , 1405new_byte =3 , 1406buffer_lines ={ 'foo X' , 'Y' , 'baz qux' } , 1407} , args [ 1 ]) 1408end ) 1409 1410it ( 'on_bytes called multiple times for global substitution creating multiple lines' , function () 1411api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'foo bar baz' } ) 1412 1413local call_count , args =exec_lua ( function () 1414local count =0 1415local args ={} 1416vim . api . nvim_buf_attach ( 0 , false , { 1417on_bytes =function ( 1418_ , 1419_ , 1420_ , 1421start_row , 1422start_col , 1423start_byte , 1424old_row , 1425old_col , 1426old_byte , 1427new_row , 1428new_col , 1429new_byte 1430) 1431count =count + 1 1432table . insert ( args , { 1433start_row =start_row , 1434start_col =start_col , 1435start_byte =start_byte , 1436old_row =old_row , 1437old_col =old_col , 1438old_byte =old_byte , 1439new_row =new_row , 1440new_col =new_col , 1441new_byte =new_byte , 1442buffer_lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), 1443} ) 1444end , 1445} ) 1446-- Global substitution with newlines in replacement. 1447vim . cmd ( [[s/ /\r/g]] ) 1448return count , args 1449end ) 1450 1451-- Should be called once per space replacement. 1452eq ( 2 , call_count ) 1453 1454eq ( { 1455start_row =0 , 1456start_col =3 , 1457start_byte =3 , 1458old_row =0 , 1459old_col =1 , 1460old_byte =1 , 1461new_row =1 , 1462new_col =0 , 1463new_byte =1 , 1464buffer_lines ={ 'foo' , 'bar' , 'baz' } , 1465} , args [ 1 ]) 1466 1467eq ( { 1468start_row =1 , 1469start_col =3 , 1470start_byte =7 , 1471old_row =0 , 1472old_col =1 , 1473old_byte =1 , 1474new_row =1 , 1475new_col =0 , 1476new_byte =1 , 1477buffer_lines ={ 'foo' , 'bar' , 'baz' } , 1478} , args [ 2 ]) 1479end ) 1480 1481it ( 1482'no buffer update event is emitted while editing substitute command, only after confirmation' , 1483function () 1484api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'Hello world' , 'Hello Neovim' } ) 1485 1486exec_lua ( function () 1487_G . num_buffer_updates =0 1488vim . api . nvim_buf_attach ( 0 , false , { 1489on_bytes =function () 1490_G . num_buffer_updates =_G . num_buffer_updates + 1 1491end , 1492} ) 1493end ) 1494 1495-- Start typing the substitute command - no events should be emitted yet. 1496feed ( ':%s/Hello/Hi' ) 1497eq ( 0 , exec_lua ( 'return _G.num_buffer_updates' )) 1498 1499-- Continue editing the command - still no events. 1500feed ( '<BS><BS>Hey' ) 1501eq ( 0 , exec_lua ( 'return _G.num_buffer_updates' )) 1502 1503-- After confirming the substitution, two events should be emitted (one per line). 1504feed ( '<CR>' ) 1505eq ( 2 , exec_lua ( 'return _G.num_buffer_updates' )) 1506 1507-- Verify the buffer was actually modified. 1508eq ( { 'Hey world' , 'Hey Neovim' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1509end 1510) 1511 1512it ( 'flushes delbytes on join' , function () 1513local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' , 'CCC' } ) 1514 1515feed ( 'gg0J' ) 1516 1517check_events { 1518{ 'test1' , 'bytes' , 1 , 3 , 0 , 3 , 3 , 1 , 0 , 1 , 0 , 1 , 1 } , 1519} 1520 1521command ( 'delete' ) 1522check_events { 1523{ 'test1' , 'bytes' , 1 , 5 , 0 , 0 , 0 , 1 , 0 , 8 , 0 , 0 , 0 } , 1524} 1525end ) 1526 1527it ( 'sends updates on U' , function () 1528feed ( 'ggiAAA<cr>BBB' ) 1529feed ( '<esc>gg$a CCC' ) 1530 1531local check_events =setup_eventcheck ( verify , nil ) 1532 1533feed ( 'ggU' ) 1534 1535check_events { 1536{ 'test1' , 'bytes' , 1 , 6 , 0 , 7 , 7 , 0 , 0 , 0 , 0 , 3 , 3 } , 1537} 1538end ) 1539 1540it ( 'delete in completely empty buffer' , function () 1541local check_events =setup_eventcheck ( verify , nil ) 1542 1543command 'delete' 1544check_events {} 1545end ) 1546 1547it ( 'delete the only line of a buffer' , function () 1548local check_events =setup_eventcheck ( verify , { 'AAA' } ) 1549 1550command 'delete' 1551check_events { 1552{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 1 , 0 , 4 , 1 , 0 , 1 } , 1553} 1554end ) 1555 1556it ( 'delete the last line of a buffer with two lines' , function () 1557local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' } ) 1558 1559command '2delete' 1560check_events { 1561{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 4 , 1 , 0 , 4 , 0 , 0 , 0 } , 1562} 1563end ) 1564 1565it ( ':sort lines' , function () 1566local check_events =setup_eventcheck ( verify , { 'CCC' , 'BBB' , 'AAA' } ) 1567 1568command '%sort' 1569check_events { 1570{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 3 , 0 , 12 , 3 , 0 , 12 } , 1571} 1572end ) 1573 1574it ( 'handles already sorted lines' , function () 1575local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' , 'CCC' } ) 1576 1577command '%sort' 1578check_events {} 1579end ) 1580 1581it ( 'works with accepting spell suggestions' , function () 1582local check_events =setup_eventcheck ( verify , { 'hallo world' , 'hallo world' } ) 1583 1584feed ( 'gg0z=4<cr><cr>' ) -- accepts 'Hello' 1585check_events { 1586{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 2 , 2 , 0 , 2 , 2 } , 1587} 1588 1589command ( 'spellrepall' ) -- replaces whole words 1590check_events { 1591{ 'test1' , 'bytes' , 1 , 4 , 1 , 0 , 12 , 0 , 5 , 5 , 0 , 5 , 5 } , 1592} 1593end ) 1594 1595it ( 'works with :diffput and :diffget' , function () 1596local check_events =setup_eventcheck ( verify , { 'AAA' } ) 1597command ( 'diffthis' ) 1598command ( 'new' ) 1599command ( 'diffthis' ) 1600api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'AAA' , 'BBB' } ) 1601feed ( 'G' ) 1602command ( 'diffput' ) 1603check_events { 1604{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 4 , 0 , 0 , 0 , 1 , 0 , 4 } , 1605} 1606api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'AAA' , 'CCC' } ) 1607feed ( '<C-w>pG' ) 1608command ( 'diffget' ) 1609check_events { 1610{ 'test1' , 'bytes' , 1 , 4 , 1 , 0 , 4 , 1 , 0 , 4 , 1 , 0 , 4 } , 1611} 1612end ) 1613 1614it ( 'prompt buffer' , function () 1615local check_events =setup_eventcheck ( verify , {} ) 1616api . nvim_set_option_value ( 'buftype' , 'prompt' , {} ) 1617feed ( 'i' ) 1618check_events { 1619{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 2 } , 1620} 1621feed ( '<CR>' ) 1622check_events { 1623{ 'test1' , 'bytes' , 1 , 4 , 1 , 0 , 3 , 0 , 0 , 0 , 1 , 0 , 1 } , 1624{ 'test1' , 'bytes' , 1 , 5 , 1 , 0 , 3 , 0 , 0 , 0 , 0 , 2 , 2 } , 1625} 1626feed ( '<CR>' ) 1627check_events { 1628{ 'test1' , 'bytes' , 1 , 6 , 2 , 0 , 6 , 0 , 0 , 0 , 1 , 0 , 1 } , 1629{ 'test1' , 'bytes' , 1 , 7 , 2 , 0 , 6 , 0 , 0 , 0 , 0 , 2 , 2 } , 1630} 1631fn . prompt_setprompt ( '' , 'foo > ' ) 1632check_events { 1633{ 'test1' , 'bytes' , 1 , 8 , 2 , 0 , 6 , 0 , 2 , 2 , 0 , 6 , 6 } , 1634} 1635feed ( 'hello' ) 1636check_events { 1637{ 'test1' , 'bytes' , 1 , 9 , 2 , 6 , 12 , 0 , 0 , 0 , 0 , 5 , 5 } , 1638} 1639fn . prompt_setprompt ( '' , 'uber-foo > ' ) 1640check_events { 1641{ 'test1' , 'bytes' , 1 , 10 , 2 , 0 , 6 , 0 , 6 , 6 , 0 , 11 , 11 } , 1642} 1643eq ( { '% ' , '% ' , 'uber-foo > hello' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1644-- Do this in the same event. 1645exec_lua ( function () 1646vim . fn . setpos ( "':" , { 0 , vim . fn . line ( '.' ), 999 , 0 } ) 1647vim . fn . prompt_setprompt ( '' , 'discard > ' ) 1648end ) 1649check_events { 1650{ 'test1' , 'bytes' , 1 , 11 , 2 , 0 , 6 , 0 , 16 , 16 , 0 , 10 , 10 } , 1651} 1652eq ( { '% ' , '% ' , 'discard > ' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1653feed ( 'sup<S-CR>dood' ) 1654check_events { 1655{ 'test1' , 'bytes' , 1 , 12 , 2 , 10 , 16 , 0 , 0 , 0 , 0 , 3 , 3 } , 1656{ 'test1' , 'bytes' , 1 , 13 , 2 , 13 , 19 , 0 , 0 , 0 , 1 , 0 , 1 } , 1657{ 'test1' , 'bytes' , 1 , 14 , 3 , 0 , 20 , 0 , 0 , 0 , 0 , 4 , 4 } , 1658} 1659eq ( { '% ' , '% ' , 'discard > sup' , 'dood' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1660fn . prompt_setprompt ( '' , 'cool > ' ) 1661check_events { 1662{ 'test1' , 'bytes' , 1 , 15 , 2 , 0 , 6 , 0 , 10 , 10 , 0 , 7 , 7 } , 1663} 1664eq ( { '% ' , '% ' , 'cool > sup' , 'dood' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1665 1666-- init_prompt uses appended_lines_mark when appending to fix prompt. 1667api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'hi' } ) 1668eq ( { 'hi' , 'cool > ' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1669check_events { 1670{ 'test1' , 'bytes' , 1 , 16 , 0 , 0 , 0 , 4 , 0 , 22 , 1 , 0 , 3 } , 1671{ 'test1' , 'bytes' , 1 , 17 , 1 , 0 , 3 , 0 , 0 , 0 , 1 , 0 , 8 } , 1672} 1673end ) 1674 1675local function test_lockmarks ( mode ) 1676local description =( mode ~= '' ) and mode or '(baseline)' 1677it ( 'test_lockmarks ' .. description .. ' %delete _' , function () 1678local check_events =setup_eventcheck ( verify , { 'AAA' , 'BBB' , 'CCC' } ) 1679 1680command ( mode .. ' %delete _' ) 1681check_events { 1682{ 'test1' , 'bytes' , 1 , 3 , 0 , 0 , 0 , 3 , 0 , 12 , 1 , 0 , 1 } , 1683} 1684end ) 1685 1686it ( 'test_lockmarks ' .. description .. ' append()' , function () 1687local check_events =setup_eventcheck ( verify ) 1688 1689command ( mode .. " call append(0, 'CCC')" ) 1690check_events { 1691{ 'test1' , 'bytes' , 1 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 4 } , 1692} 1693 1694command ( mode .. " call append(1, 'BBBB')" ) 1695check_events { 1696{ 'test1' , 'bytes' , 1 , 3 , 1 , 0 , 4 , 0 , 0 , 0 , 1 , 0 , 5 } , 1697} 1698 1699command ( mode .. " call append(2, '')" ) 1700check_events { 1701{ 'test1' , 'bytes' , 1 , 4 , 2 , 0 , 9 , 0 , 0 , 0 , 1 , 0 , 1 } , 1702} 1703 1704command ( mode .. ' $delete _' ) 1705check_events { 1706{ 'test1' , 'bytes' , 1 , 5 , 3 , 0 , 10 , 1 , 0 , 1 , 0 , 0 , 0 } , 1707} 1708 1709eq ( 'CCC|BBBB|' , table . concat ( api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), '|' )) 1710end ) 1711end 1712 1713-- check that behavior is identical with and without "lockmarks" 1714test_lockmarks '' 1715test_lockmarks 'lockmarks' 1716 1717teardown ( function () 1718os . remove 'Xtest-reload' 1719os . remove 'Xtest-undofile' 1720os . remove '.Xtest-undofile.un~' 1721end ) 1722end 1723 1724describe ( '(with verify) handles' , function () 1725do_both ( true ) 1726end ) 1727 1728describe ( '(without verify) handles' , function () 1729do_both ( false ) 1730end ) 1731end ) 1732 1733describe ( 'nvim_buf_attach on_detach' , function () 1734it ( 'does not SEGFAULT when accessing window buffer info #14998' , function () 1735local code =function () 1736local buf =vim . api . nvim_create_buf ( false , false ) 1737 1738vim . cmd 'split' 1739vim . api . nvim_win_set_buf ( 0 , buf ) 1740 1741vim . api . nvim_buf_attach ( buf , false , { 1742on_detach =function ( _ , buf0 ) 1743vim . fn . tabpagebuflist () 1744vim . fn . win_findbuf ( buf0 ) 1745end , 1746} ) 1747end 1748 1749exec_lua ( code ) 1750command ( 'q!' ) 1751n . assert_alive () 1752 1753exec_lua ( code ) 1754command ( 'bd!' ) 1755n . assert_alive () 1756end ) 1757 1758it ( 'no invalid lnum error for closed memline #31251' , function () 1759eq ( vim . NIL , exec_lua ( 'return _G.did_detach' )) 1760exec_lua ( function () 1761vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '' } ) 1762local bufname ='buf2' 1763local buf =vim . api . nvim_create_buf ( false , true ) 1764vim . api . nvim_buf_set_name ( buf , bufname ) 1765vim . bo [ buf ]. bufhidden ='wipe' 1766vim . cmd ( 'vertical diffsplit ' .. bufname ) 1767vim . api . nvim_buf_attach ( 0 , false , { 1768on_detach =function () 1769vim . cmd ( 'redraw' ) 1770_G . did_detach =true 1771end , 1772} ) 1773vim . cmd . bdelete () 1774end ) 1775eq ( true , exec_lua ( 'return _G.did_detach' )) 1776end ) 1777 1778it ( 'called before buf_freeall autocommands' , function () 1779exec_lua ( function () 1780vim . api . nvim_create_autocmd ( { 'BufUnload' , 'BufDelete' , 'BufWipeout' } , { 1781callback =function ( ev ) 1782table . insert ( 1783_G . events , 1784( '%s: %d %s' ): format ( ev . event , ev . buf , tostring ( vim . api . nvim_buf_is_loaded ( ev . buf ))) 1785) 1786end , 1787} ) 1788function _G . on_detach ( _ , b ) 1789table . insert ( 1790_G . events , 1791( 'on_detach: %d %s' ): format ( b , tostring ( vim . api . nvim_buf_is_loaded ( b ))) 1792) 1793end 1794_G . events ={} 1795vim . cmd 'new' 1796vim . bo . bufhidden ='wipe' 1797vim . api . nvim_buf_attach ( 0 , false , { on_detach =_G . on_detach } ) 1798vim . cmd 'quit!' 1799end ) 1800 1801eq ( 1802{ 'on_detach: 2 true' , 'BufUnload: 2 true' , 'BufDelete: 2 true' , 'BufWipeout: 2 true' } , 1803exec_lua ( 'return _G.events' ) 1804) 1805eq ( false , api . nvim_buf_is_valid ( 2 )) 1806 1807exec_lua ( function () 1808_G . events ={} 1809local buf =vim . api . nvim_create_buf ( false , true ) 1810vim . api . nvim_buf_attach ( buf , false , { on_detach =_G . on_detach } ) 1811vim . api . nvim_buf_delete ( buf , { force =true } ) 1812end ) 1813 1814-- Was unlisted, so no BufDelete. 1815eq ( 1816{ 'on_detach: 3 true' , 'BufUnload: 3 true' , 'BufWipeout: 3 true' } , 1817exec_lua ( 'return _G.events' ) 1818) 1819eq ( false , api . nvim_buf_is_valid ( 3 )) 1820 1821exec_lua ( function () 1822_G . events ={} 1823vim . api . nvim_buf_attach ( 1 , false , { on_detach =_G . on_detach } ) 1824vim . api . nvim_create_autocmd ( 'BufUnload' , { 1825buf =1 , 1826once =true , 1827callback =function () 1828vim . api . nvim_buf_attach ( 1 , false , { 1829on_detach =function ( ... ) 1830vim . fn . bufload ( 1 ) -- Leaks the memfile it were to run inside free_buffer_stuff. 1831return _G . on_detach ( ... ) 1832end , 1833} ) 1834table . insert ( _G . events , 'local BufUnload' ) 1835end , 1836} ) 1837vim . cmd 'edit asdf' -- Reuses buffer 1. 1838end ) 1839 1840-- on_detach shouldn't run after autocommands when reusing a buffer (in free_buffer_stuff), even 1841-- if those autocommands registered it, as curbuf may be in a semi-unloaded state at that point. 1842eq ( { 1843'on_detach: 1 true' , 1844'BufUnload: 1 true' , 1845'local BufUnload' , 1846'BufDelete: 1 true' , 1847'BufWipeout: 1 true' , 1848} , exec_lua ( 'return _G.events' )) 1849 1850exec_lua ( function () 1851_G . events ={} 1852vim . api . nvim_buf_attach ( 0 , false , { on_detach =_G . on_detach } ) 1853vim . cmd 'edit' 1854end ) 1855 1856-- Re-edit buffer; on_detach is called. 1857eq ( { 'on_detach: 1 true' , 'BufUnload: 1 true' } , exec_lua ( 'return _G.events' )) 1858eq ( true , api . nvim_buf_is_valid ( 1 )) 1859 1860exec_lua ( function () 1861vim . cmd '%bwipeout!' 1862vim . bo . modified =true 1863_G . events ={} 1864vim . api . nvim_buf_attach ( 0 , false , { on_detach =_G . on_detach } ) 1865vim . api . nvim_buf_delete ( 0 , { force =true } ) 1866end ) 1867 1868-- on_detach must still be first when wiping the last buffer if it's listed and non-reusable. 1869-- Previously: BufUnload → BufDelete → on_detach → BufWipeout. 1870eq ( 1871{ 'on_detach: 4 true' , 'BufUnload: 4 true' , 'BufDelete: 4 true' , 'BufWipeout: 4 false' } , 1872exec_lua ( 'return _G.events' ) 1873) 1874end ) 1875 1876it ( 'disallows splitting' , function () 1877command ( 'new | setlocal bufhidden=wipe' ) 1878local buf =api . nvim_get_current_buf () 1879exec_lua ( function () 1880vim . api . nvim_buf_attach ( 0 , false , { 1881on_detach =function () 1882-- Used to allow opening more views into a closing buffer, resulting in open windows to an 1883-- unloaded buffer. 1884vim . cmd [=[execute "normal! \<C-W>s"]=] 1885end , 1886} ) 1887end ) 1888matches ( 'E1159: Cannot split a window when closing the buffer$' , pcall_err ( command , 'quit!' )) 1889eq ( {} , fn . win_findbuf ( buf )) 1890eq ( false , api . nvim_buf_is_valid ( buf )) 1891end ) 1892end ) 1893 1894it ( 'nvim_buf_attach from buf_freeall autocommands does not leak' , function () 1895exec_lua ( function () 1896local b =vim . api . nvim_create_buf ( true , true ) 1897vim . api . nvim_create_autocmd ( 'BufWipeout' , { 1898buf =b , 1899once =true , 1900callback =function () 1901vim . api . nvim_buf_attach ( b , false , {} ) 1902_G . autocmd_fired =true 1903end , 1904} ) 1905vim . api . nvim_buf_delete ( b , { force =true } ) 1906end ) 1907eq ( true , exec_lua ( 'return _G.autocmd_fired' )) 1908end )