robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 6local clear =n . clear 7local eq =t . eq 8local eval =n . eval 9local exec =n . exec 10local command =n . command 11local feed =n . feed 12local api =n . api 13local assert_alive =n . assert_alive 14 15before_each ( clear ) 16 17describe ( 'WinResized' , function () 18-- oldtest: Test_WinResized() 19it ( 'works' , function () 20exec ( [[ 21set scrolloff=0 22call setline(1, ['111', '222']) 23vnew 24call setline(1, ['aaa', 'bbb']) 25new 26call setline(1, ['foo', 'bar']) 27 28let g:resized = 0 29au WinResized * let g:resized += 1 30au WinResized * let g:v_event = deepcopy(v:event) 31]] ) 32eq ( 0 , eval ( 'g:resized' )) 33 34-- increase window height, two windows will be reported 35feed ( '<C-W>+' ) 36eq ( 1 , eval ( 'g:resized' )) 37eq ( { windows ={ 1002 , 1001 } } , eval ( 'g:v_event' )) 38 39-- increase window width, three windows will be reported 40feed ( '<C-W>>' ) 41eq ( 2 , eval ( 'g:resized' )) 42eq ( { windows ={ 1002 , 1001 , 1000 } } , eval ( 'g:v_event' )) 43end ) 44 45it ( 'is triggered in terminal mode #21197 #27207' , function () 46exec ( [[ 47autocmd TermOpen * startinsert 48let g:resized = 0 49autocmd WinResized * let g:resized += 1 50]] ) 51eq ( 0 , eval ( 'g:resized' )) 52 53command ( 'vsplit term://' ) 54feed ( '<Ignore>' ) -- Add input to separate two RPC requests 55eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 56eq ( 1 , eval ( 'g:resized' )) 57 58command ( 'split' ) 59feed ( '<Ignore>' ) -- Add input to separate two RPC requests 60eq ( { mode ='t' , blocking =false } , api . nvim_get_mode ()) 61eq ( 2 , eval ( 'g:resized' )) 62end ) 63end ) 64 65describe ( 'WinScrolled' , function () 66local win_id 67 68before_each ( function () 69win_id =api . nvim_get_current_win () 70command ( string . format ( 'autocmd WinScrolled %d let g:matched = v:true' , win_id )) 71exec ( [[ 72let g:scrolled = 0 73au WinScrolled * let g:scrolled += 1 74au WinScrolled * let g:amatch = str2nr(expand('<amatch>')) 75au WinScrolled * let g:afile = str2nr(expand('<afile>')) 76au WinScrolled * let g:v_event = deepcopy(v:event) 77]] ) 78end ) 79 80after_each ( function () 81eq ( true , eval ( 'g:matched' )) 82eq ( win_id , eval ( 'g:amatch' )) 83eq ( win_id , eval ( 'g:afile' )) 84end ) 85 86it ( 'is triggered by scrolling vertically' , function () 87local lines ={ '123' , '123' } 88api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 89eq ( 0 , eval ( 'g:scrolled' )) 90 91feed ( '<C-E>' ) 92eq ( 1 , eval ( 'g:scrolled' )) 93eq ( { 94all ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 95[ '1000' ] ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 96} , eval ( 'g:v_event' )) 97 98feed ( '<C-Y>' ) 99eq ( 2 , eval ( 'g:scrolled' )) 100eq ( { 101all ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 102[ '1000' ] ={ leftcol =0 , topline =- 1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 103} , eval ( 'g:v_event' )) 104end ) 105 106it ( 'is triggered by scrolling horizontally' , function () 107command ( 'set nowrap' ) 108local width =api . nvim_win_get_width ( 0 ) 109local line ='123' .. ( '*' ): rep ( width * 2 ) 110local lines ={ line , line } 111api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 112eq ( 0 , eval ( 'g:scrolled' )) 113 114feed ( 'zl' ) 115eq ( 1 , eval ( 'g:scrolled' )) 116eq ( { 117all ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 118[ '1000' ] ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 119} , eval ( 'g:v_event' )) 120 121feed ( 'zh' ) 122eq ( 2 , eval ( 'g:scrolled' )) 123eq ( { 124all ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 125[ '1000' ] ={ leftcol =- 1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 126} , eval ( 'g:v_event' )) 127end ) 128 129it ( 'is triggered by horizontal scrolling from cursor move' , function () 130command ( 'set nowrap' ) 131local lines ={ '' , '' , 'Foo' } 132api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 133api . nvim_win_set_cursor ( 0 , { 3 , 0 } ) 134eq ( 0 , eval ( 'g:scrolled' )) 135 136feed ( 'zl' ) 137eq ( 1 , eval ( 'g:scrolled' )) 138eq ( { 139all ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 140[ '1000' ] ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 141} , eval ( 'g:v_event' )) 142 143feed ( 'zl' ) 144eq ( 2 , eval ( 'g:scrolled' )) 145eq ( { 146all ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 147[ '1000' ] ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 148} , eval ( 'g:v_event' )) 149 150feed ( 'h' ) 151eq ( 3 , eval ( 'g:scrolled' )) 152eq ( { 153all ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 154[ '1000' ] ={ leftcol =- 1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 155} , eval ( 'g:v_event' )) 156 157feed ( 'zh' ) 158eq ( 4 , eval ( 'g:scrolled' )) 159eq ( { 160all ={ leftcol =1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 161[ '1000' ] ={ leftcol =- 1 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =0 } , 162} , eval ( 'g:v_event' )) 163end ) 164 165-- oldtest: Test_WinScrolled_long_wrapped() 166it ( 'is triggered by scrolling on a long wrapped line #19968' , function () 167local height =api . nvim_win_get_height ( 0 ) 168local width =api . nvim_win_get_width ( 0 ) 169api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { ( 'foo' ): rep ( height * width ) } ) 170api . nvim_win_set_cursor ( 0 , { 1 , height * width - 1 } ) 171eq ( 0 , eval ( 'g:scrolled' )) 172 173feed ( 'gj' ) 174eq ( 1 , eval ( 'g:scrolled' )) 175eq ( { 176all ={ leftcol =0 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =width } , 177[ '1000' ] ={ leftcol =0 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =width } , 178} , eval ( 'g:v_event' )) 179 180feed ( '0' ) 181eq ( 2 , eval ( 'g:scrolled' )) 182eq ( { 183all ={ leftcol =0 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =width } , 184[ '1000' ] ={ leftcol =0 , topline =0 , topfill =0 , width =0 , height =0 , skipcol =- width } , 185} , eval ( 'g:v_event' )) 186 187feed ( '$' ) 188eq ( 3 , eval ( 'g:scrolled' )) 189end ) 190 191it ( 'is triggered when the window scrolls in Insert mode' , function () 192local height =api . nvim_win_get_height ( 0 ) 193local lines ={} 194for i =1 , height * 2 do 195lines [ i ] =tostring ( i ) 196end 197api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 198 199feed ( 'M' ) 200eq ( 0 , eval ( 'g:scrolled' )) 201 202feed ( 'i<C-X><C-E><Esc>' ) 203eq ( 1 , eval ( 'g:scrolled' )) 204eq ( { 205all ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 206[ '1000' ] ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 207} , eval ( 'g:v_event' )) 208 209feed ( 'i<C-X><C-Y><Esc>' ) 210eq ( 2 , eval ( 'g:scrolled' )) 211eq ( { 212all ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 213[ '1000' ] ={ leftcol =0 , topline =- 1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 214} , eval ( 'g:v_event' )) 215 216feed ( 'L' ) 217eq ( 2 , eval ( 'g:scrolled' )) 218 219feed ( 'A<CR><Esc>' ) 220eq ( 3 , eval ( 'g:scrolled' )) 221eq ( { 222all ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 223[ '1000' ] ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 224} , eval ( 'g:v_event' )) 225end ) 226end ) 227 228describe ( 'WinScrolled' , function () 229-- oldtest: Test_WinScrolled_mouse() 230it ( 'is triggered by mouse scrolling in another window' , function () 231local _ =Screen . new ( 75 , 10 ) 232exec ( [[ 233set nowrap scrolloff=0 234set mouse=a 235call setline(1, ['foo']->repeat(32)) 236split 237let g:scrolled = 0 238au WinScrolled * let g:scrolled += 1 239]] ) 240eq ( 0 , eval ( 'g:scrolled' )) 241 242-- With the upper split focused, send a scroll-down event to the unfocused one. 243api . nvim_input_mouse ( 'wheel' , 'down' , '' , 0 , 6 , 0 ) 244eq ( 1 , eval ( 'g:scrolled' )) 245 246-- Again, but this time while we're in insert mode. 247feed ( 'i' ) 248api . nvim_input_mouse ( 'wheel' , 'down' , '' , 0 , 6 , 0 ) 249feed ( '<Esc>' ) 250eq ( 2 , eval ( 'g:scrolled' )) 251end ) 252 253it ( 'is triggered by incsearch scrolling #21207' , function () 254local lines ={} 255for i =1 , 100 do 256lines [ i ] ='line ' .. i 257end 258lines [ 60 ] ='special target line' 259api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 260exec ( [[ 261set incsearch scrolloff=0 262let g:scrolled = 0 263au WinScrolled * let g:scrolled += 1 264]] ) 265eq ( 0 , eval ( 'g:scrolled' )) 266 267-- Typing the pattern scrolls to the match. 268feed ( '/target' ) 269eq ( 1 , eval ( 'g:scrolled' )) 270 271-- <C-c> restores the original view, which is another scroll. 272feed ( '<C-c>' ) 273eq ( 2 , eval ( 'g:scrolled' )) 274end ) 275 276-- oldtest: Test_WinScrolled_close_curwin() 277it ( 'closing window does not cause use-after-free #13265' , function () 278exec ( [[ 279set nowrap scrolloff=0 280call setline(1, ['aaa', 'bbb']) 281vsplit 282au WinScrolled * close 283]] ) 284 285-- This was using freed memory 286feed ( '<C-E>' ) 287assert_alive () 288end ) 289 290-- oldtest: Test_WinScrolled_diff() 291it ( 'is triggered for both windows when scrolling in diff mode' , function () 292exec ( [[ 293set diffopt+=foldcolumn:0 294call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) 295vnew 296call setline(1, ['d', 'e', 'f', 'g', 'h', 'i']) 297windo diffthis 298au WinScrolled * let g:v_event = deepcopy(v:event) 299]] ) 300 301feed ( '<C-E>' ) 302eq ( { 303all ={ leftcol =0 , topline =1 , topfill =1 , width =0 , height =0 , skipcol =0 } , 304[ '1000' ] ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 305[ '1001' ] ={ leftcol =0 , topline =0 , topfill =- 1 , width =0 , height =0 , skipcol =0 } , 306} , eval ( 'g:v_event' )) 307 308feed ( '2<C-E>' ) 309eq ( { 310all ={ leftcol =0 , topline =2 , topfill =2 , width =0 , height =0 , skipcol =0 } , 311[ '1000' ] ={ leftcol =0 , topline =2 , topfill =0 , width =0 , height =0 , skipcol =0 } , 312[ '1001' ] ={ leftcol =0 , topline =0 , topfill =- 2 , width =0 , height =0 , skipcol =0 } , 313} , eval ( 'g:v_event' )) 314 315feed ( '<C-E>' ) 316eq ( { 317all ={ leftcol =0 , topline =2 , topfill =0 , width =0 , height =0 , skipcol =0 } , 318[ '1000' ] ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 319[ '1001' ] ={ leftcol =0 , topline =1 , topfill =0 , width =0 , height =0 , skipcol =0 } , 320} , eval ( 'g:v_event' )) 321 322feed ( '2<C-Y>' ) 323eq ( { 324all ={ leftcol =0 , topline =3 , topfill =1 , width =0 , height =0 , skipcol =0 } , 325[ '1000' ] ={ leftcol =0 , topline =- 2 , topfill =0 , width =0 , height =0 , skipcol =0 } , 326[ '1001' ] ={ leftcol =0 , topline =- 1 , topfill =1 , width =0 , height =0 , skipcol =0 } , 327} , eval ( 'g:v_event' )) 328end ) 329 330it ( 'is triggered by mouse scrolling in unfocused floating window #18222' , function () 331local screen =Screen . new ( 80 , 24 ) 332 333exec ( [[ 334let g:scrolled = 0 335autocmd WinScrolled * let g:scrolled += 1 336autocmd WinScrolled * let g:amatch = expand('<amatch>') 337autocmd WinScrolled * let g:v_event = deepcopy(v:event) 338]] ) 339eq ( 0 , eval ( 'g:scrolled' )) 340 341local buf =api . nvim_create_buf ( true , true ) 342api . nvim_buf_set_lines ( 343buf , 3440 , 345- 1 , 346false , 347{ '@' , 'b' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' } 348) 349local win =api . nvim_open_win ( buf , false , { 350height =5 , 351width =10 , 352col =0 , 353row =1 , 354relative ='editor' , 355style ='minimal' , 356} ) 357screen : expect ( { any ='@' } ) 358local winid_str =tostring ( win ) 359-- WinScrolled should not be triggered when creating a new floating window 360eq ( 0 , eval ( 'g:scrolled' )) 361 362api . nvim_input_mouse ( 'wheel' , 'down' , '' , 0 , 3 , 3 ) 363eq ( 1 , eval ( 'g:scrolled' )) 364eq ( winid_str , eval ( 'g:amatch' )) 365eq ( { 366all ={ leftcol =0 , topline =3 , topfill =0 , width =0 , height =0 , skipcol =0 } , 367[ winid_str ] ={ leftcol =0 , topline =3 , topfill =0 , width =0 , height =0 , skipcol =0 } , 368} , eval ( 'g:v_event' )) 369 370api . nvim_input_mouse ( 'wheel' , 'up' , '' , 0 , 3 , 3 ) 371eq ( 2 , eval ( 'g:scrolled' )) 372eq ( tostring ( win ), eval ( 'g:amatch' )) 373eq ( { 374all ={ leftcol =0 , topline =3 , topfill =0 , width =0 , height =0 , skipcol =0 } , 375[ winid_str ] ={ leftcol =0 , topline =- 3 , topfill =0 , width =0 , height =0 , skipcol =0 } , 376} , eval ( 'g:v_event' )) 377end ) 378 379it ( 'does not crash when WinResized closes popup before WinScrolled #35803' , function () 380exec ( [[ 381set scrolloff=0 382call setline(1, range(1, 100)) 383 384" Create first popup window (will be resized and closed) 385let buf1 = nvim_create_buf(v:false, v:true) 386call nvim_buf_set_lines(buf1, 0, -1, v:false, map(range(1, 50), 'string(v:val)')) 387let popup1 = nvim_open_win(buf1, v:false, { 388\ 'relative': 'editor', 389\ 'width': 20, 390\ 'height': 5, 391\ 'col': 10, 392\ 'row': 5 393\ }) 394 395" Create second popup window (will be scrolled) 396let buf2 = nvim_create_buf(v:false, v:true) 397call nvim_buf_set_lines(buf2, 0, -1, v:false, map(range(1, 50), 'string(v:val)')) 398let popup2 = nvim_open_win(buf2, v:false, { 399\ 'relative': 'editor', 400\ 'width': 20, 401\ 'height': 5, 402\ 'col': 35, 403\ 'row': 5 404\ }) 405 406let g:resized = 0 407let g:scrolled = 0 408 409" WinResized autocmd resizes and closes the first popup 410autocmd WinResized * let g:resized += 1 | call nvim_win_set_height(popup1, 10) | call nvim_win_close(popup1, v:true) 411" WinScrolled autocmd scrolls the second popup 412autocmd WinScrolled * let g:scrolled += 1 | call nvim_win_call(popup2, {-> execute('normal! \<C-E>')}) 413]] ) 414eq ( 0 , eval ( 'g:resized' )) 415eq ( 0 , eval ( 'g:scrolled' )) 416 417-- Trigger a resize on popup1, which will close it 418-- This should trigger WinResized (which closes popup1) and WinScrolled (which scrolls popup2) 419-- Before the fix, WinScrolled would use the freed pointer causing a crash 420api . nvim_win_set_height ( eval ( 'popup1' ), 8 ) 421 422-- The key is that it should not crash when WinResized closes a window 423-- that WinScrolled might have referenced via a stale buf_T pointer 424assert_alive () 425-- Verify autocmds were actually triggered 426eq ( 1 , eval ( 'g:resized > 0' )) 427end ) 428end )