robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
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 clear , api =n . clear , n . api 7local eq =t . eq 8local command =n . command 9 10describe ( 'ui/cursor' , function () 11local screen 12 13before_each ( function () 14clear () 15screen =Screen . new ( 25 , 5 ) 16end ) 17 18it ( "'guicursor' is published as a UI event" , function () 19local expected_mode_info ={ 20[ 1 ] ={ 21blinkoff =0 , 22blinkon =0 , 23blinkwait =0 , 24cell_percentage =0 , 25cursor_shape ='block' , 26name ='normal' , 27hl_id =0 , 28id_lm =0 , 29attr ={} , 30attr_lm ={} , 31mouse_shape =0 , 32short_name ='n' , 33} , 34[ 2 ] ={ 35blinkoff =0 , 36blinkon =0 , 37blinkwait =0 , 38cell_percentage =0 , 39cursor_shape ='block' , 40name ='visual' , 41hl_id =0 , 42id_lm =0 , 43attr ={} , 44attr_lm ={} , 45mouse_shape =0 , 46short_name ='v' , 47} , 48[ 3 ] ={ 49blinkoff =0 , 50blinkon =0 , 51blinkwait =0 , 52cell_percentage =25 , 53cursor_shape ='vertical' , 54name ='insert' , 55hl_id =0 , 56id_lm =0 , 57attr ={} , 58attr_lm ={} , 59mouse_shape =0 , 60short_name ='i' , 61} , 62[ 4 ] ={ 63blinkoff =0 , 64blinkon =0 , 65blinkwait =0 , 66cell_percentage =20 , 67cursor_shape ='horizontal' , 68name ='replace' , 69hl_id =0 , 70id_lm =0 , 71attr ={} , 72attr_lm ={} , 73mouse_shape =0 , 74short_name ='r' , 75} , 76[ 5 ] ={ 77blinkoff =0 , 78blinkon =0 , 79blinkwait =0 , 80cell_percentage =0 , 81cursor_shape ='block' , 82name ='cmdline_normal' , 83hl_id =0 , 84id_lm =0 , 85attr ={} , 86attr_lm ={} , 87mouse_shape =0 , 88short_name ='c' , 89} , 90[ 6 ] ={ 91blinkoff =0 , 92blinkon =0 , 93blinkwait =0 , 94cell_percentage =25 , 95cursor_shape ='vertical' , 96name ='cmdline_insert' , 97hl_id =0 , 98id_lm =0 , 99attr ={} , 100attr_lm ={} , 101mouse_shape =0 , 102short_name ='ci' , 103} , 104[ 7 ] ={ 105blinkoff =0 , 106blinkon =0 , 107blinkwait =0 , 108cell_percentage =20 , 109cursor_shape ='horizontal' , 110name ='cmdline_replace' , 111hl_id =0 , 112id_lm =0 , 113attr ={} , 114attr_lm ={} , 115mouse_shape =0 , 116short_name ='cr' , 117} , 118[ 8 ] ={ 119blinkoff =0 , 120blinkon =0 , 121blinkwait =0 , 122cell_percentage =20 , 123cursor_shape ='horizontal' , 124name ='operator' , 125hl_id =0 , 126id_lm =0 , 127attr ={} , 128attr_lm ={} , 129mouse_shape =0 , 130short_name ='o' , 131} , 132[ 9 ] ={ 133blinkoff =0 , 134blinkon =0 , 135blinkwait =0 , 136cell_percentage =25 , 137cursor_shape ='vertical' , 138name ='visual_select' , 139hl_id =0 , 140id_lm =0 , 141attr ={} , 142attr_lm ={} , 143mouse_shape =0 , 144short_name ='ve' , 145} , 146[ 10 ] ={ 147name ='cmdline_hover' , 148mouse_shape =0 , 149short_name ='e' , 150} , 151[ 11 ] ={ 152name ='statusline_hover' , 153mouse_shape =0 , 154short_name ='s' , 155} , 156[ 12 ] ={ 157name ='statusline_drag' , 158mouse_shape =0 , 159short_name ='sd' , 160} , 161[ 13 ] ={ 162name ='vsep_hover' , 163mouse_shape =0 , 164short_name ='vs' , 165} , 166[ 14 ] ={ 167name ='vsep_drag' , 168mouse_shape =0 , 169short_name ='vd' , 170} , 171[ 15 ] ={ 172name ='more' , 173mouse_shape =0 , 174short_name ='m' , 175} , 176[ 16 ] ={ 177name ='more_lastline' , 178mouse_shape =0 , 179short_name ='ml' , 180} , 181[ 17 ] ={ 182blinkoff =0 , 183blinkon =0 , 184blinkwait =0 , 185cell_percentage =0 , 186cursor_shape ='block' , 187name ='showmatch' , 188hl_id =0 , 189id_lm =0 , 190attr ={} , 191attr_lm ={} , 192short_name ='sm' , 193} , 194[ 18 ] ={ 195blinkoff =500 , 196blinkon =500 , 197blinkwait =0 , 198cell_percentage =0 , 199cursor_shape ='block' , 200name ='terminal' , 201hl_id =3 , 202id_lm =3 , 203attr ={ reverse =true } , 204attr_lm ={ reverse =true } , 205short_name ='t' , 206} , 207} 208 209screen : expect ( function () 210-- Default 'guicursor', published on startup. 211eq ( expected_mode_info , screen . _mode_info ) 212eq ( true , screen . _cursor_style_enabled ) 213eq ( 'normal' , screen . mode ) 214end ) 215 216-- Event is published ONLY if the cursor style changed. 217screen . _mode_info =nil 218command ( "echo 'test'" ) 219screen : expect { 220grid =[[ 221^ | 222{1:~ }|*3 223test | 224]] , 225condition =function () 226eq ( nil , screen . _mode_info ) 227end , 228} 229 230-- Change the cursor style. 231n . command ( 'hi Cursor guibg=DarkGray' ) 232n . command ( 233'set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr-o:hor20' 234.. ',a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor' 235.. ',sm:block-blinkwait175-blinkoff150-blinkon175' 236) 237 238-- Update the expected values. 239for _ , m in ipairs ( expected_mode_info ) do 240if m . name == 'showmatch' then 241if m . blinkon then 242m . blinkon =175 243end 244if m . blinkoff then 245m . blinkoff =150 246end 247if m . blinkwait then 248m . blinkwait =175 249end 250else 251if m . blinkon then 252m . blinkon =250 253end 254if m . blinkoff then 255m . blinkoff =400 256end 257if m . blinkwait then 258m . blinkwait =700 259end 260end 261if m . hl_id then 262m . hl_id =67 263m . attr ={ background =Screen . colors . DarkGray } 264end 265if m . id_lm then 266m . id_lm =82 267m . attr_lm ={} 268end 269end 270 271-- Assert the new expectation. 272screen : expect ( function () 273for i , v in ipairs ( expected_mode_info ) do 274eq ( v , screen . _mode_info [ i ]) 275end 276eq ( true , screen . _cursor_style_enabled ) 277eq ( 'normal' , screen . mode ) 278end ) 279 280-- Change hl groups only, should update the styles 281n . command ( 'hi Cursor guibg=Red' ) 282n . command ( 'hi lCursor guibg=Green' ) 283 284-- Update the expected values. 285for _ , m in ipairs ( expected_mode_info ) do 286if m . hl_id then 287m . attr ={ background =Screen . colors . Red } 288end 289if m . id_lm then 290m . attr_lm ={ background =Screen . colors . Green } 291end 292end 293-- Assert the new expectation. 294screen : expect ( function () 295eq ( expected_mode_info , screen . _mode_info ) 296eq ( true , screen . _cursor_style_enabled ) 297eq ( 'normal' , screen . mode ) 298end ) 299 300-- update the highlight again to hide cursor 301n . command ( 'hi Cursor blend=100' ) 302 303for _ , m in ipairs ( expected_mode_info ) do 304if m . hl_id then 305m . attr ={ background =Screen . colors . Red , blend =100 } 306end 307end 308screen : expect { 309grid =[[ 310^ | 311{1:~ }|*3 312test | 313]] , 314condition =function () 315eq ( expected_mode_info , screen . _mode_info ) 316end , 317} 318 319-- Another cursor style. 320api . nvim_set_option_value ( 321'guicursor' , 322'n-v-c:ver35-blinkwait171-blinkoff172-blinkon173' 323.. ',ve:hor35,o:ver50,i-ci:block,r-cr:hor90,sm:ver42' , 324{} 325) 326screen : expect ( function () 327local named ={} 328for _ , m in ipairs ( screen . _mode_info ) do 329named [ m . name ] =m 330end 331eq ( 'vertical' , named . normal . cursor_shape ) 332eq ( 35 , named . normal . cell_percentage ) 333eq ( 'horizontal' , named . visual_select . cursor_shape ) 334eq ( 35 , named . visual_select . cell_percentage ) 335eq ( 'vertical' , named . operator . cursor_shape ) 336eq ( 50 , named . operator . cell_percentage ) 337eq ( 'block' , named . insert . cursor_shape ) 338eq ( 'vertical' , named . showmatch . cursor_shape ) 339eq ( 90 , named . cmdline_replace . cell_percentage ) 340eq ( 171 , named . normal . blinkwait ) 341eq ( 172 , named . normal . blinkoff ) 342eq ( 173 , named . normal . blinkon ) 343eq ( 42 , named . showmatch . cell_percentage ) 344end ) 345 346-- If there is no setting for guicursor, it becomes the default setting. 347api . nvim_set_option_value ( 348'guicursor' , 349'n:ver35-blinkwait171-blinkoff172-blinkon173-Cursor/lCursor' , 350{} 351) 352screen : expect ( function () 353for _ , m in ipairs ( screen . _mode_info ) do 354if m . name ~= 'normal' then 355eq ( 'block' , m . cursor_shape or 'block' ) 356eq ( 0 , m . blinkon or 0 ) 357eq ( 0 , m . blinkoff or 0 ) 358eq ( 0 , m . blinkwait or 0 ) 359eq ( 0 , m . hl_id or 0 ) 360eq ( 0 , m . id_lm or 0 ) 361end 362end 363end ) 364end ) 365 366it ( "empty 'guicursor' sets cursor_shape=block in all modes" , function () 367api . nvim_set_option_value ( 'guicursor' , '' , {} ) 368screen : expect ( function () 369-- Empty 'guicursor' sets enabled=false. 370eq ( false , screen . _cursor_style_enabled ) 371for _ , m in ipairs ( screen . _mode_info ) do 372if m [ 'cursor_shape' ] ~= nil then 373eq ( 'block' , m . cursor_shape ) 374eq ( 0 , m . blinkon ) 375eq ( 0 , m . hl_id ) 376eq ( 0 , m . id_lm ) 377end 378end 379end ) 380end ) 381 382it ( "'set all&' reapplies 'guicursor'" , function () 383command ( 'set guicursor=n:ver25' ) 384screen : expect ( function () 385eq ( 'vertical' , screen . _mode_info [ 1 ]. cursor_shape ) 386eq ( 25 , screen . _mode_info [ 1 ]. cell_percentage ) 387end ) 388 389command ( 'set all&' ) 390screen : expect ( function () 391eq ( 'block' , screen . _mode_info [ 1 ]. cursor_shape ) 392eq ( 0 , screen . _mode_info [ 1 ]. blinkon ) 393eq ( 0 , screen . _mode_info [ 1 ]. blinkoff ) 394eq ( true , screen . _cursor_style_enabled ) 395end ) 396end ) 397 398it ( ':sleep does not hide cursor when sleeping' , function () 399n . feed ( ':sleep 300m | echo 42' ) 400screen : expect ( [[ 401| 402{1:~ }|*3 403:sleep 300m | echo 42^ | 404]] ) 405n . feed ( '\n' ) 406screen : expect ( { 407grid =[[ 408^ | 409{1:~ }|*3 410:sleep 300m | echo 42 | 411]] , 412timeout =100 , 413} ) 414screen : expect ( [[ 415^ | 416{1:~ }|*3 41742 | 418]] ) 419end ) 420 421it ( ':sleep! hides cursor when sleeping' , function () 422n . feed ( ':sleep! 300m | echo 42' ) 423screen : expect ( [[ 424| 425{1:~ }|*3 426:sleep! 300m | echo 42^ | 427]] ) 428n . feed ( '\n' ) 429screen : expect ( { 430grid =[[ 431| 432{1:~ }|*3 433:sleep! 300m | echo 42 | 434]] , 435timeout =100 , 436} ) 437screen : expect ( [[ 438^ | 439{1:~ }|*3 44042 | 441]] ) 442end ) 443end )