robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
50c50ab119
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 =t . describe , t . it , t . before_each 6local exec_lua =n . exec_lua 7local eq =t . eq 8local eval =n . eval 9local command =n . command 10local clear =n . clear 11local api =n . api 12 13describe ( 'vim.hl.range' , function () 14local screen 15 16before_each ( function () 17clear () 18screen =Screen . new ( 60 , 6 ) 19screen : add_extra_attr_ids ( { 20[ 100 ] ={ foreground =Screen . colors . Blue , background =Screen . colors . Yellow , bold =true } , 21} ) 22api . nvim_set_option_value ( 'list' , true , {} ) 23api . nvim_set_option_value ( 'listchars' , 'eol:$' , {} ) 24api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 25'asdfghjkl' , 26'«口=口»' , 27'qwertyuiop' , 28'口口=口口' , 29'zxcvbnm' , 30} ) 31screen : expect ( [[ 32^asdfghjkl{1:$} | 33«口=口»{1:$} | 34qwertyuiop{1:$} | 35口口=口口{1:$} | 36zxcvbnm{1:$} | 37| 38]] ) 39end ) 40 41it ( 'works with charwise selection' , function () 42exec_lua ( function () 43local ns =vim . api . nvim_create_namespace ( '' ) 44vim . hl . range ( 0 , ns , 'Search' , { 1 , 5 } , { 3 , 10 } ) 45end ) 46screen : expect ( [[ 47^asdfghjkl{1:$} | 48«口{10:=口»}{100:$} | 49{10:qwertyuiop}{100:$} | 50{10:口口=口}口{1:$} | 51zxcvbnm{1:$} | 52| 53]] ) 54end ) 55 56it ( 'works with linewise selection' , function () 57exec_lua ( function () 58local ns =vim . api . nvim_create_namespace ( '' ) 59vim . hl . range ( 0 , ns , 'Search' , { 0 , 0 } , { 4 , 0 } , { regtype ='V' } ) 60end ) 61screen : expect ( [[ 62{10:^asdfghjkl}{100:$} | 63{10:«口=口»}{100:$} | 64{10:qwertyuiop}{100:$} | 65{10:口口=口口}{100:$} | 66{10:zxcvbnm}{100:$} | 67| 68]] ) 69end ) 70 71it ( 'works with blockwise selection' , function () 72exec_lua ( function () 73local ns =vim . api . nvim_create_namespace ( '' ) 74vim . hl . range ( 0 , ns , 'Search' , { 0 , 0 } , { 4 , 4 } , { regtype ='\022' } ) 75end ) 76screen : expect ( [[ 77{10:^asdf}ghjkl{1:$} | 78{10:«口=}口»{1:$} | 79{10:qwer}tyuiop{1:$} | 80{10:口口}=口口{1:$} | 81{10:zxcv}bnm{1:$} | 82| 83]] ) 84end ) 85 86it ( 'works with blockwise selection with width' , function () 87exec_lua ( function () 88local ns =vim . api . nvim_create_namespace ( '' ) 89vim . hl . range ( 0 , ns , 'Search' , { 0 , 4 } , { 4 , 7 } , { regtype ='\0226' } ) 90end ) 91screen : expect ( [[ 92^asdf{10:ghjkl}{1:$} | 93«口={10:口»}{1:$} | 94qwer{10:tyuiop}{1:$} | 95口口{10:=口口}{1:$} | 96zxcv{10:bnm}{1:$} | 97| 98]] ) 99end ) 100 101it ( 'can use -1 or v:maxcol to indicate end of line' , function () 102exec_lua ( function () 103local ns =vim . api . nvim_create_namespace ( '' ) 104vim . hl . range ( 0 , ns , 'Search' , { 0 , 4 } , { 1 , - 1 } , {} ) 105vim . hl . range ( 0 , ns , 'Search' , { 2 , 6 } , { 3 , vim . v . maxcol } , {} ) 106end ) 107screen : expect ( [[ 108^asdf{10:ghjkl}{100:$} | 109{10:«口=口»}{100:$} | 110qwerty{10:uiop}{100:$} | 111{10:口口=口口}{100:$} | 112zxcvbnm{1:$} | 113| 114]] ) 115end ) 116 117it ( 'removes highlight after `timeout`' , function () 118local timeout =300 119exec_lua ( function () 120local ns =vim . api . nvim_create_namespace ( '' ) 121vim . hl . range ( 0 , ns , 'Search' , { 0 , 0 } , { 4 , 0 } , { timeout =timeout } ) 122end ) 123screen : expect ( { 124grid =[[ 125{10:^asdfghjkl}{100:$} | 126{10:«口=口»}{100:$} | 127{10:qwertyuiop}{100:$} | 128{10:口口=口口}{1:$} | 129zxcvbnm{1:$} | 130| 131]] , 132timeout =timeout / 3 , 133} ) 134-- The deferred `range_hl_clear` works even if a different buffer was switched-to. 135exec_lua ( function () 136local orig =vim . api . nvim_get_current_buf () 137vim . api . nvim_set_current_buf ( vim . api . nvim_create_buf ( false , true )) 138vim . wait ( timeout * 2 ) 139vim . api . nvim_set_current_buf ( orig ) 140end ) 141screen : expect ( [[ 142^asdfghjkl{1:$} | 143«口=口»{1:$} | 144qwertyuiop{1:$} | 145口口=口口{1:$} | 146zxcvbnm{1:$} | 147| 148]] ) 149end ) 150 151it ( 'shows multiple highlights with different timeouts simultaneously' , function () 152local timeout1 =300 153local timeout2 =600 154exec_lua ( function () 155local ns =vim . api . nvim_create_namespace ( '' ) 156vim . hl . range ( 0 , ns , 'Search' , { 0 , 0 } , { 4 , 0 } , { timeout =timeout1 } ) 157vim . hl . range ( 0 , ns , 'Search' , { 2 , 6 } , { 3 , 5 } , { timeout =timeout2 } ) 158end ) 159screen : expect ( { 160grid =[[ 161{10:^asdfghjkl}{100:$} | 162{10:«口=口»}{100:$} | 163{10:qwertyuiop}{100:$} | 164{10:口口=口口}{1:$} | 165zxcvbnm{1:$} | 166| 167]] , 168timeout =timeout1 / 3 , 169} ) 170screen : expect ( { 171grid =[[ 172^asdfghjkl{1:$} | 173«口=口»{1:$} | 174qwerty{10:uiop}{100:$} | 175{10:口口}=口口{1:$} | 176zxcvbnm{1:$} | 177| 178]] , 179timeout =timeout1 + (( timeout2 - timeout1 ) / 3 ), 180} ) 181screen : expect ( [[ 182^asdfghjkl{1:$} | 183«口=口»{1:$} | 184qwertyuiop{1:$} | 185口口=口口{1:$} | 186zxcvbnm{1:$} | 187| 188]] ) 189end ) 190 191it ( 'allows cancelling a highlight before timeout' , function () 192exec_lua ( function () 193local timeout =3000 194local range_timer 195local range_hl_clear 196local ns =vim . api . nvim_create_namespace ( '' ) 197range_timer , range_hl_clear =vim . hl . range ( 1980 , 199ns , 200'Search' , 201{ 0 , 0 } , 202{ 4 , 0 } , 203{ timeout =timeout } 204) 205if range_timer and not range_timer : is_closing () then 206range_timer : close () 207assert ( range_hl_clear ) 208range_hl_clear () 209range_hl_clear () -- Exercise redundant call 210end 211end ) 212screen : expect ( { 213grid =[[ 214^asdfghjkl{1:$} | 215«口=口»{1:$} | 216qwertyuiop{1:$} | 217口口=口口{1:$} | 218zxcvbnm{1:$} | 219| 220]] , 221unchanged =true , 222} ) 223end ) 224end ) 225 226describe ( 'vim.hl.hl_op' , function () 227before_each ( function () 228clear () 229end ) 230 231it ( 'no errors even if buffer is wiped before timeout' , function () 232command ( 'new' ) 233n . feed ( 'ifoo<esc>' ) -- set '[, '] 234exec_lua ( function () 235vim . hl . hl_op ( { 236timeout =10 , 237on_macro =true , 238event ={ operator ='y' , regtype ='v' } , 239} ) 240vim . cmd ( 'bwipeout!' ) 241end ) 242vim . uv . sleep ( 10 ) 243n . feed ( '<cr>' ) -- avoid hang if error message exists 244eq ( '' , eval ( 'v:errmsg' )) 245end ) 246 247it ( 'does not close timer twice' , function () 248exec_lua ( function () 249vim . hl . hl_op ( { timeout =10 , on_macro =true , event ={ operator ='y' } } ) 250vim . uv . sleep ( 10 ) 251vim . schedule ( function () 252vim . hl . hl_op ( { timeout =0 , on_macro =true , event ={ operator ='y' } } ) 253end ) 254end ) 255eq ( '' , eval ( 'v:errmsg' )) 256end ) 257 258it ( 'does not show in another window' , function () 259command ( 'vsplit' ) 260exec_lua ( function () 261vim . api . nvim_buf_set_mark ( 0 , '[' , 1 , 1 , {} ) 262vim . api . nvim_buf_set_mark ( 0 , ']' , 1 , 1 , {} ) 263vim . hl . hl_op ( { timeout =math . huge , on_macro =true , event ={ operator ='y' } } ) 264end ) 265local ns =api . nvim_create_namespace ( 'nvim.hl.events' ) 266local win =api . nvim_get_current_win () 267eq ( { win } , api . nvim__ns_get ( ns ). wins ) 268command ( 'wincmd w' ) 269eq ( { win } , api . nvim__ns_get ( ns ). wins ) 270-- Use a new vim.hl.hl_op() call to cancel the previous timer 271exec_lua ( function () 272vim . hl . hl_op ( { timeout =0 , on_macro =true , event ={ operator ='y' } } ) 273end ) 274end ) 275 276it ( 'removes old highlight if new one is created before old timeout' , function () 277command ( 'vnew' ) 278exec_lua ( function () 279vim . api . nvim_buf_set_mark ( 0 , '[' , 1 , 1 , {} ) 280vim . api . nvim_buf_set_mark ( 0 , ']' , 1 , 1 , {} ) 281vim . hl . hl_op ( { timeout =math . huge , on_macro =true , event ={ operator ='y' } } ) 282end ) 283local ns =api . nvim_create_namespace ( 'nvim.hl.events' ) 284eq ( api . nvim_get_current_win (), api . nvim__ns_get ( ns ). wins [ 1 ]) 285command ( 'wincmd w' ) 286exec_lua ( function () 287vim . api . nvim_buf_set_mark ( 0 , '[' , 1 , 1 , {} ) 288vim . api . nvim_buf_set_mark ( 0 , ']' , 1 , 1 , {} ) 289vim . hl . hl_op ( { timeout =math . huge , on_macro =true , event ={ operator ='y' } } ) 290end ) 291local win =api . nvim_get_current_win () 292eq ( { win } , api . nvim__ns_get ( ns ). wins ) 293command ( 'wincmd w' ) 294eq ( { win } , api . nvim__ns_get ( ns ). wins ) 295-- Use a new vim.hl.hl_op() call to cancel the previous timer 296exec_lua ( function () 297vim . hl . hl_op ( { timeout =0 , on_macro =true , event ={ operator ='y' } } ) 298end ) 299end ) 300 301it ( 'highlights last character with exclusive motion' , function () 302local screen =Screen . new ( 60 , 4 ) 303screen : add_extra_attr_ids ( { 304[ 100 ] ={ foreground =Screen . colors . Blue , background =Screen . colors . Yellow , bold =true } , 305} ) 306command ( 'autocmd TextYankPost * lua vim.hl.hl_op{timeout=100000}' ) 307api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 308[[foo(bar) 'baz']] , 309[[foo(bar) 'baz']] , 310} ) 311n . feed ( 'yw' ) 312screen : expect ( [[ 313{2:^foo}(bar) 'baz' | 314foo(bar) 'baz' | 315{1:~ }| 316| 317]] ) 318n . feed ( "yi'" ) 319screen : expect ( [[ 320foo(bar) '{2:^baz}' | 321foo(bar) 'baz' | 322{1:~ }| 323| 324]] ) 325n . feed ( 'yvj' ) 326screen : expect ( [[ 327foo(bar) '{2:^baz'} | 328{2:foo(bar) '}baz' | 329{1:~ }| 330| 331]] ) 332-- Use a new vim.hl.hl_op() call to cancel the previous timer 333exec_lua ( function () 334vim . hl . hl_op ( { timeout =0 , on_macro =true , event ={ operator ='y' } } ) 335end ) 336end ) 337 338it ( 'highlights TextPutPost' , function () 339local screen =Screen . new ( 60 , 4 ) 340screen : add_extra_attr_ids ( { 341[ 100 ] ={ foreground =Screen . colors . Blue , background =Screen . colors . Yellow , bold =true } , 342} ) 343command ( 'autocmd TextPutPost * lua vim.hl.hl_op{timeout=100000}' ) 344api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 345[[foo(bar) '1']] , 346[[foo(bar) '2']] , 347} ) 348n . feed ( 'ggyyp' ) 349screen : expect ( [[ 350foo(bar) '1' | 351{2:^foo(bar) '1'} | 352foo(bar) '2' | 353| 354]] ) 355-- Use a new vim.hl.hl_op() call to cancel the previous timer 356exec_lua ( function () 357vim . hl . hl_op ( { timeout =0 , on_macro =true , event ={ operator ='p' } } ) 358end ) 359end ) 360end )