robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local tt =require ( 'test.functional.testterm' ) 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear , eq , eval =n . clear , t . eq , n . eval 7local feed , api , command =n . feed , n . api , n . command 8local feed_data =tt . feed_data 9local is_os =t . is_os 10local skip =t . skip 11 12describe ( ':terminal mouse' , function () 13local screen 14 15before_each ( function () 16clear () 17api . nvim_set_option_value ( 'statusline' , '==========' , {} ) 18screen =tt . setup_screen () 19command ( 'highlight StatusLine NONE' ) 20command ( 'highlight StatusLineNC NONE' ) 21command ( 'highlight StatusLineTerm NONE' ) 22command ( 'highlight StatusLineTermNC NONE' ) 23command ( 'highlight VertSplit NONE' ) 24local lines ={} 25for i =1 , 30 do 26table . insert ( lines , 'line' .. tostring ( i )) 27end 28table . insert ( lines , '' ) 29feed_data ( lines ) 30screen : expect ( [[ 31line26 | 32line27 | 33line28 | 34line29 | 35line30 | 36^ | 37{5:-- TERMINAL --} | 38]] ) 39end ) 40 41describe ( 'when the terminal has focus' , function () 42it ( 'will exit focus on mouse-scroll' , function () 43eq ( 't' , eval ( 'mode(1)' )) 44feed ( '<ScrollWheelUp><0,0>' ) 45eq ( 'nt' , eval ( 'mode(1)' )) 46end ) 47 48it ( 'will exit focus and trigger Normal mode mapping on mouse click' , function () 49feed ( [[<C-\><C-N>qri]] ) 50command ( 'let g:got_leftmouse = 0' ) 51command ( 'nnoremap <LeftMouse> <Cmd>let g:got_leftmouse = 1<CR>' ) 52eq ( 't' , eval ( 'mode(1)' )) 53eq ( 0 , eval ( 'g:got_leftmouse' )) 54feed ( '<LeftMouse>' ) 55eq ( 'nt' , eval ( 'mode(1)' )) 56eq ( 1 , eval ( 'g:got_leftmouse' )) 57feed ( 'q' ) 58eq ( 'i<LeftMouse>' , eval ( 'keytrans(@r)' )) 59end ) 60 61it ( 'will exit focus and trigger Normal mode mapping on mouse click with modifier' , function () 62feed ( [[<C-\><C-N>qri]] ) 63command ( 'let g:got_ctrl_leftmouse = 0' ) 64command ( 'nnoremap <C-LeftMouse> <Cmd>let g:got_ctrl_leftmouse = 1<CR>' ) 65eq ( 't' , eval ( 'mode(1)' )) 66eq ( 0 , eval ( 'g:got_ctrl_leftmouse' )) 67feed ( '<C-LeftMouse>' ) 68eq ( 'nt' , eval ( 'mode(1)' )) 69eq ( 1 , eval ( 'g:got_ctrl_leftmouse' )) 70feed ( 'q' ) 71eq ( 'i<C-LeftMouse>' , eval ( 'keytrans(@r)' )) 72end ) 73 74it ( 'will exit focus on <C-\\> + mouse-scroll' , function () 75eq ( 't' , eval ( 'mode(1)' )) 76feed ( '<C-\\>' ) 77feed ( '<ScrollWheelUp><0,0>' ) 78eq ( 'nt' , eval ( 'mode(1)' )) 79end ) 80 81it ( 'will not exit focus on left-release' , function () 82eq ( 't' , eval ( 'mode(1)' )) 83feed ( '<LeftRelease><0,0>' ) 84eq ( 't' , eval ( 'mode(1)' )) 85command ( 'setlocal number' ) 86eq ( 't' , eval ( 'mode(1)' )) 87feed ( '<LeftRelease><0,0>' ) 88eq ( 't' , eval ( 'mode(1)' )) 89end ) 90 91it ( 'will not exit focus on mouse movement' , function () 92eq ( 't' , eval ( 'mode(1)' )) 93feed ( '<MouseMove><0,0>' ) 94eq ( 't' , eval ( 'mode(1)' )) 95command ( 'setlocal number' ) 96eq ( 't' , eval ( 'mode(1)' )) 97feed ( '<MouseMove><0,0>' ) 98eq ( 't' , eval ( 'mode(1)' )) 99end ) 100 101describe ( 'with mouse events enabled by the program' , function () 102before_each ( function () 103tt . enable_mouse () -- FIXME: this doesn't work on Windows? 104tt . feed_data ( 'mouse enabled\n' ) 105screen : expect ( [[ 106line27 | 107line28 | 108line29 | 109line30 | 110mouse enabled | 111^ | 112{5:-- TERMINAL --} | 113]] ) 114end ) 115 116it ( 'will forward mouse press, drag and release to the program' , function () 117skip ( is_os ( 'win' )) 118feed ( '<LeftMouse><1,2>' ) 119screen : expect ( [[ 120line27 | 121line28 | 122line29 | 123line30 | 124mouse enabled | 125"#^ | 126{5:-- TERMINAL --} | 127]] ) 128feed ( '<LeftDrag><2,2>' ) 129screen : expect ( [[ 130line27 | 131line28 | 132line29 | 133line30 | 134mouse enabled | 135@##^ | 136{5:-- TERMINAL --} | 137]] ) 138feed ( '<LeftDrag><3,2>' ) 139screen : expect ( [[ 140line27 | 141line28 | 142line29 | 143line30 | 144mouse enabled | 145@$#^ | 146{5:-- TERMINAL --} | 147]] ) 148feed ( '<LeftRelease><3,2>' ) 149screen : expect ( [[ 150line27 | 151line28 | 152line29 | 153line30 | 154mouse enabled | 155#$#^ | 156{5:-- TERMINAL --} | 157]] ) 158end ) 159 160it ( 'will forward mouse scroll to the program' , function () 161skip ( is_os ( 'win' )) 162feed ( '<ScrollWheelUp><0,0>' ) 163screen : expect ( [[ 164line27 | 165line28 | 166line29 | 167line30 | 168mouse enabled | 169`!!^ | 170{5:-- TERMINAL --} | 171]] ) 172end ) 173 174it ( 'dragging and scrolling do not interfere with each other' , function () 175skip ( is_os ( 'win' )) 176feed ( '<LeftMouse><1,2>' ) 177screen : expect ( [[ 178line27 | 179line28 | 180line29 | 181line30 | 182mouse enabled | 183"#^ | 184{5:-- TERMINAL --} | 185]] ) 186feed ( '<ScrollWheelUp><1,2>' ) 187screen : expect ( [[ 188line27 | 189line28 | 190line29 | 191line30 | 192mouse enabled | 193`"#^ | 194{5:-- TERMINAL --} | 195]] ) 196feed ( '<LeftDrag><2,2>' ) 197screen : expect ( [[ 198line27 | 199line28 | 200line29 | 201line30 | 202mouse enabled | 203@##^ | 204{5:-- TERMINAL --} | 205]] ) 206feed ( '<ScrollWheelUp><2,2>' ) 207screen : expect ( [[ 208line27 | 209line28 | 210line29 | 211line30 | 212mouse enabled | 213`##^ | 214{5:-- TERMINAL --} | 215]] ) 216feed ( '<LeftRelease><2,2>' ) 217screen : expect ( [[ 218line27 | 219line28 | 220line29 | 221line30 | 222mouse enabled | 223###^ | 224{5:-- TERMINAL --} | 225]] ) 226end ) 227 228it ( 'will forward mouse clicks to the program with the correct even if set nu' , function () 229skip ( is_os ( 'win' )) 230command ( 'set number' ) 231screen : expect ( [[ 232{121: 11 }line28 | 233{121: 12 }line29 | 234{121: 13 }line30 | 235{121: 14 }mouse enabled | 236{121: 15 }rows: 6, cols: 46 | 237{121: 16 }^ | 238{5:-- TERMINAL --} | 239]] ) 240-- When the display area such as a number is clicked, it returns to the 241-- normal mode. 242feed ( '<LeftMouse><3,0>' ) 243eq ( 'nt' , eval ( 'mode(1)' )) 244screen : expect ( [[ 245{121: 11 }^line28 | 246{121: 12 }line29 | 247{121: 13 }line30 | 248{121: 14 }mouse enabled | 249{121: 15 }rows: 6, cols: 46 | 250{121: 16 } | 251| 252]] ) 253-- If click on the coordinate (0,1) of the region of the terminal 254-- (i.e. the coordinate (4,1) of vim), 'CSI !"' is sent to the terminal. 255feed ( 'i<LeftMouse><4,1>' ) 256screen : expect ( [[ 257{121: 11 }line28 | 258{121: 12 }line29 | 259{121: 13 }line30 | 260{121: 14 }mouse enabled | 261{121: 15 }rows: 6, cols: 46 | 262{121: 16 } !"^ | 263{5:-- TERMINAL --} | 264]] ) 265end ) 266 267it ( 'will lose focus if statusline is clicked' , function () 268command ( 'set laststatus=2' ) 269screen : expect ( [[ 270line29 | 271line30 | 272mouse enabled | 273rows: 5, cols: 50 | 274^ | 275========== | 276{5:-- TERMINAL --} | 277]] ) 278feed ( '<LeftMouse><0,5>' ) 279screen : expect ( [[ 280line29 | 281line30 | 282mouse enabled | 283rows: 5, cols: 50 | 284^ | 285========== | 286| 287]] ) 288feed ( '<LeftDrag><0,4>' ) 289screen : expect ( [[ 290mouse enabled | 291rows: 5, cols: 50 | 292rows: 4, cols: 50 | 293^ | 294========== | 295|*2 296]] ) 297end ) 298 299it ( 'will lose focus if right separator is clicked' , function () 300command ( 'rightbelow vnew | wincmd p | startinsert' ) 301screen : expect ( [[ 302line29 │ | 303line30 │{100:~ }| 304mouse enabled │{100:~ }| 305rows: 5, cols: 24 │{100:~ }| 306^ │{100:~ }| 307========== ========== | 308{5:-- TERMINAL --} | 309]] ) 310feed ( '<LeftMouse><24,0>' ) 311screen : expect ( [[ 312line29 │ | 313line30 │{100:~ }| 314mouse enabled │{100:~ }| 315rows: 5, cols: 24 │{100:~ }| 316^ │{100:~ }| 317========== ========== | 318| 319]] ) 320feed ( '<LeftDrag><23,0>' ) 321screen : expect ( [[ 322line30 │ | 323mouse enabled │{100:~ }| 324rows: 5, cols: 24 │{100:~ }| 325rows: 5, cols: 23 │{100:~ }| 326^ │{100:~ }| 327========== ========== | 328| 329]] ) 330end ) 331 332it ( 'will lose focus if winbar/tabline is clicked' , function () 333command ( 'setlocal winbar=WINBAR' ) 334screen : expect ( [[ 335{5:WINBAR }| 336line29 | 337line30 | 338mouse enabled | 339rows: 5, cols: 50 | 340^ | 341{5:-- TERMINAL --} | 342]] ) 343feed ( '<LeftMouse><0,0>' ) 344screen : expect ( [[ 345{5:WINBAR }| 346line29 | 347line30 | 348mouse enabled | 349rows: 5, cols: 50 | 350^ | 351| 352]] ) 353command ( 'set showtabline=2 tabline=TABLINE | startinsert' ) 354screen : expect ( [[ 355{2:TABLINE }| 356{5:WINBAR }| 357mouse enabled | 358rows: 5, cols: 50 | 359rows: 4, cols: 50 | 360^ | 361{5:-- TERMINAL --} | 362]] ) 363feed ( '<LeftMouse><0,0>' ) 364screen : expect ( [[ 365{2:TABLINE }| 366{5:WINBAR }| 367mouse enabled | 368rows: 5, cols: 50 | 369rows: 4, cols: 50 | 370^ | 371| 372]] ) 373command ( 'setlocal winbar= | startinsert' ) 374screen : expect ( [[ 375{2:TABLINE }| 376mouse enabled | 377rows: 5, cols: 50 | 378rows: 4, cols: 50 | 379rows: 5, cols: 50 | 380^ | 381{5:-- TERMINAL --} | 382]] ) 383feed ( '<LeftMouse><0,0>' ) 384screen : expect ( [[ 385{2:TABLINE }| 386mouse enabled | 387rows: 5, cols: 50 | 388rows: 4, cols: 50 | 389rows: 5, cols: 50 | 390^ | 391| 392]] ) 393end ) 394 395it ( 'mouse forwarding works with resized grid' , function () 396screen : detach () 397local Screen =require ( 'test.functional.ui.screen' ) 398screen =Screen . new ( 50 , 7 , { ext_multigrid =true } ) 399screen : expect ( [[ 400## grid 1 401[2:--------------------------------------------------]|*6 402[3:--------------------------------------------------]| 403## grid 2 404line27 | 405line28 | 406line29 | 407line30 | 408mouse enabled | 409^ | 410## grid 3 411{5:-- TERMINAL --} | 412]] ) 413 414screen : try_resize_grid ( 2 , 58 , 11 ) 415screen : expect ( { any =vim . pesc ( 'rows: 11, cols: 58' ) } ) 416 417api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 0 , 0 ) 418screen : expect ( { any =vim . pesc ( '"!!^' ) } ) 419api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 0 , 0 ) 420screen : expect ( { any =vim . pesc ( '#!!^' ) } ) 421 422api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 10 , 0 ) 423screen : expect ( { any =vim . pesc ( '"!+^' ) } ) 424api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 10 , 0 ) 425screen : expect ( { any =vim . pesc ( '#!+^' ) } ) 426 427api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 0 , 57 ) 428screen : expect ( { any =vim . pesc ( '"Z!^' ) } ) 429api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 0 , 57 ) 430screen : expect ( { any =vim . pesc ( '#Z!^' ) } ) 431 432api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 10 , 57 ) 433screen : expect ( { any =vim . pesc ( '"Z+^' ) } ) 434api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 10 , 57 ) 435screen : expect ( { any =vim . pesc ( '#Z+^' ) } ) 436 437command ( 'setlocal winbar=WINBAR' ) 438screen : expect ( { any =vim . pesc (( '{5:WINBAR%s}' ): format (( ' ' ): rep ( 52 ))) } ) 439eq ( 't' , api . nvim_get_mode (). mode ) 440 441api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 0 , 0 ) 442eq ( 'nt' , api . nvim_get_mode (). mode ) 443api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 0 , 0 ) 444feed ( 'i' ) 445eq ( 't' , api . nvim_get_mode (). mode ) 446 447api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 0 , 57 ) 448eq ( 'nt' , api . nvim_get_mode (). mode ) 449api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 0 , 57 ) 450feed ( 'i' ) 451eq ( 't' , api . nvim_get_mode (). mode ) 452 453api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 1 , 0 ) 454screen : expect ( { any =vim . pesc ( '"!!^' ) } ) 455api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 1 , 0 ) 456screen : expect ( { any =vim . pesc ( '#!!^' ) } ) 457 458api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 10 , 0 ) 459screen : expect ( { any =vim . pesc ( '"!*^' ) } ) 460api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 10 , 0 ) 461screen : expect ( { any =vim . pesc ( '#!*^' ) } ) 462 463api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 1 , 57 ) 464screen : expect ( { any =vim . pesc ( '"Z!^' ) } ) 465api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 1 , 57 ) 466screen : expect ( { any =vim . pesc ( '#Z!^' ) } ) 467 468api . nvim_input_mouse ( 'right' , 'press' , '' , 2 , 10 , 57 ) 469screen : expect ( { any =vim . pesc ( '"Z*^' ) } ) 470api . nvim_input_mouse ( 'right' , 'release' , '' , 2 , 10 , 57 ) 471screen : expect ( { any =vim . pesc ( '#Z*^' ) } ) 472end ) 473end ) 474 475describe ( 'with a split window and other buffer' , function () 476before_each ( function () 477feed ( '<c-\\><c-n>:vsp<cr>' ) 478screen : expect ( [[ 479line28 │line28 | 480line29 │line29 | 481line30 │line30 | 482rows: 5, cols: 25 │rows: 5, cols: 25 | 483^ │ | 484========== ========== | 485:vsp | 486]] ) 487feed ( ':enew | set number<cr>' ) 488screen : expect ( [[ 489{121: 1 }^ │line29 | 490{100:~ }│line30 | 491{100:~ }│rows: 5, cols: 25 | 492{100:~ }│rows: 5, cols: 24 | 493{100:~ }│ | 494========== ========== | 495:enew | set number | 496]] ) 497feed ( '30iline\n<esc>' ) 498screen : expect ( [[ 499{121: 27 }line │line29 | 500{121: 28 }line │line30 | 501{121: 29 }line │rows: 5, cols: 25 | 502{121: 30 }line │rows: 5, cols: 24 | 503{121: 31 }^ │ | 504========== ========== | 505| 506]] ) 507feed ( '<c-w>li' ) 508screen : expect ( [[ 509{121: 27 }line │line29 | 510{121: 28 }line │line30 | 511{121: 29 }line │rows: 5, cols: 25 | 512{121: 30 }line │rows: 5, cols: 24 | 513{121: 31 } │^ | 514========== ========== | 515{5:-- TERMINAL --} | 516]] ) 517 518-- enabling mouse won't affect interaction with other windows 519tt . enable_mouse () 520tt . feed_data ( 'mouse enabled\n' ) 521screen : expect ( [[ 522{121: 27 }line │line30 | 523{121: 28 }line │rows: 5, cols: 25 | 524{121: 29 }line │rows: 5, cols: 24 | 525{121: 30 }line │mouse enabled | 526{121: 31 } │^ | 527========== ========== | 528{5:-- TERMINAL --} | 529]] ) 530end ) 531 532it ( "scrolling another window keeps focus and respects 'mousescroll'" , function () 533feed ( '<ScrollWheelUp><4,0><ScrollWheelUp><4,0>' ) 534screen : expect ( [[ 535{121: 21 }line │line30 | 536{121: 22 }line │rows: 5, cols: 25 | 537{121: 23 }line │rows: 5, cols: 24 | 538{121: 24 }line │mouse enabled | 539{121: 25 }line │^ | 540========== ========== | 541{5:-- TERMINAL --} | 542]] ) 543feed ( '<S-ScrollWheelDown><4,0>' ) 544screen : expect ( [[ 545{121: 26 }line │line30 | 546{121: 27 }line │rows: 5, cols: 25 | 547{121: 28 }line │rows: 5, cols: 24 | 548{121: 29 }line │mouse enabled | 549{121: 30 }line │^ | 550========== ========== | 551{5:-- TERMINAL --} | 552]] ) 553command ( 'set mousescroll=ver:10' ) 554feed ( '<ScrollWheelUp><0,0>' ) 555screen : expect ( [[ 556{121: 16 }line │line30 | 557{121: 17 }line │rows: 5, cols: 25 | 558{121: 18 }line │rows: 5, cols: 24 | 559{121: 19 }line │mouse enabled | 560{121: 20 }line │^ | 561========== ========== | 562{5:-- TERMINAL --} | 563]] ) 564command ( 'set mousescroll=ver:0' ) 565feed ( '<ScrollWheelUp><0,0>' ) 566screen : expect_unchanged () 567feed ( [[<C-\><C-N><C-W>w]] ) 568command ( 'setlocal nowrap' ) 569feed ( '0<C-V>gg3ly$4p<C-W>wi' ) 570screen : expect ( [[ 571{121: 1 }linelinelinelineline │line30 | 572{121: 2 }linelinelinelineline │rows: 5, cols: 25 | 573{121: 3 }linelinelinelineline │rows: 5, cols: 24 | 574{121: 4 }linelinelinelineline │mouse enabled | 575{121: 5 }linelinelinelineline │^ | 576========== ========== | 577{5:-- TERMINAL --} | 578]] ) 579feed ( '<ScrollWheelRight><4,0>' ) 580screen : expect ( [[ 581{121: 1 }nelinelineline │line30 | 582{121: 2 }nelinelineline │rows: 5, cols: 25 | 583{121: 3 }nelinelineline │rows: 5, cols: 24 | 584{121: 4 }nelinelineline │mouse enabled | 585{121: 5 }nelinelineline │^ | 586========== ========== | 587{5:-- TERMINAL --} | 588]] ) 589command ( 'set mousescroll=hor:4' ) 590feed ( '<ScrollWheelLeft><4,0>' ) 591screen : expect ( [[ 592{121: 1 }nelinelinelineline │line30 | 593{121: 2 }nelinelinelineline │rows: 5, cols: 25 | 594{121: 3 }nelinelinelineline │rows: 5, cols: 24 | 595{121: 4 }nelinelinelineline │mouse enabled | 596{121: 5 }nelinelinelineline │^ | 597========== ========== | 598{5:-- TERMINAL --} | 599]] ) 600end ) 601 602it ( 'will lose focus if another window is clicked' , function () 603feed ( '<LeftMouse><5,1>' ) 604screen : expect ( [[ 605{121: 27 }line │line30 | 606{121: 28 }l^ine │rows: 5, cols: 25 | 607{121: 29 }line │rows: 5, cols: 24 | 608{121: 30 }line │mouse enabled | 609{121: 31 } │ | 610========== ========== | 611| 612]] ) 613end ) 614 615it ( 'handles terminal size when switching buffers' , function () 616api . nvim_set_option_value ( 'hidden' , true , {} ) 617feed ( '<c-\\><c-n><c-w><c-w>' ) 618screen : expect ( [[ 619{121: 27 }line │line30 | 620{121: 28 }line │rows: 5, cols: 25 | 621{121: 29 }line │rows: 5, cols: 24 | 622{121: 30 }line │mouse enabled | 623{121: 31 }^ │ | 624========== ========== | 625| 626]] ) 627feed ( ':bn<cr>' ) 628screen : expect ( [[ 629rows: 5, cols: 25 │rows: 5, cols: 25 | 630rows: 5, cols: 24 │rows: 5, cols: 24 | 631mouse enabled │mouse enabled | 632rows: 5, cols: 25 │rows: 5, cols: 25 | 633^ │ | 634========== ========== | 635:bn | 636]] ) 637feed ( ':bn<cr>' ) 638screen : expect ( [[ 639{121: 27 }line │rows: 5, cols: 24 | 640{121: 28 }line │mouse enabled | 641{121: 29 }line │rows: 5, cols: 25 | 642{121: 30 }line │rows: 5, cols: 24 | 643{121: 31 }^ │ | 644========== ========== | 645:bn | 646]] ) 647end ) 648end ) 649end ) 650end )