robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
359459dec6
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local tt =require ( 'test.functional.testterm' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local feed , clear =n . feed , n . clear 7local testprg , command =n . testprg , n . command 8local eq , eval =t . eq , n . eval 9local api =n . api 10local exec_lua =n . exec_lua 11local matches =t . matches 12local call =n . call 13local hide_cursor =tt . hide_cursor 14local show_cursor =tt . show_cursor 15local retry =t . retry 16local is_os =t . is_os 17local skip =t . skip 18 19describe ( ':terminal cursor' , function () 20local screen 21 22local terminal_mode_idx ---@type number 23 24before_each ( function () 25clear () 26screen =tt . setup_screen () 27 28if terminal_mode_idx == nil then 29for i , v in ipairs ( screen . _mode_info ) do 30if v . name == 'terminal' then 31terminal_mode_idx =i 32end 33end 34assert ( terminal_mode_idx ) 35end 36end ) 37 38it ( 'moves the screen cursor when focused' , function () 39tt . feed_data ( 'testing cursor' ) 40screen : expect ( [[ 41tty ready | 42testing cursor^ | 43|*4 44{5:-- TERMINAL --} | 45]] ) 46end ) 47 48it ( 'is highlighted when not focused' , function () 49feed ( '<c-\\><c-n>' ) 50screen : expect ( [[ 51tty ready | 52^ | 53|*5 54]] ) 55end ) 56 57describe ( 'with number column' , function () 58before_each ( function () 59feed ( '<c-\\><c-n>:set number<cr>' ) 60end ) 61 62it ( 'is positioned correctly when unfocused' , function () 63screen : expect ( [[ 64{121: 1 }tty ready | 65{121: 2 }^rows: 6, cols: 46 | 66{121: 3 } | 67{121: 4 } | 68{121: 5 } | 69{121: 6 } | 70:set number | 71]] ) 72end ) 73 74it ( 'is positioned correctly when focused' , function () 75screen : expect ( [[ 76{121: 1 }tty ready | 77{121: 2 }^rows: 6, cols: 46 | 78{121: 3 } | 79{121: 4 } | 80{121: 5 } | 81{121: 6 } | 82:set number | 83]] ) 84feed ( 'i' ) 85n . poke_eventloop () 86screen : expect ( [[ 87{121: 1 }tty ready | 88{121: 2 }rows: 6, cols: 46 | 89{121: 3 }^ | 90{121: 4 } | 91{121: 5 } | 92{121: 6 } | 93{5:-- TERMINAL --} | 94]] ) 95end ) 96end ) 97 98describe ( 'when invisible' , function () 99it ( 'is not highlighted' , function () 100hide_cursor () 101screen : expect ( [[ 102tty ready | 103|*5 104{5:-- TERMINAL --} | 105]] ) 106show_cursor () 107screen : expect ( [[ 108tty ready | 109^ | 110|*4 111{5:-- TERMINAL --} | 112]] ) 113-- same for when the terminal is unfocused 114feed ( '<c-\\><c-n>' ) 115hide_cursor () 116screen : expect ( [[ 117tty ready | 118^ | 119|*5 120]] ) 121show_cursor () 122screen : expect_unchanged () 123end ) 124 125it ( 'becomes visible when exiting Terminal mode' , function () 126hide_cursor () 127screen : expect ( [[ 128tty ready | 129|*5 130{5:-- TERMINAL --} | 131]] ) 132feed ( '<c-\\><c-n>' ) 133screen : expect ( [[ 134tty ready | 135^ | 136|*5 137]] ) 138feed ( 'i' ) 139screen : expect ( [[ 140tty ready | 141|*5 142{5:-- TERMINAL --} | 143]] ) 144 145-- Cursor is hidden; now request to show it while in a TermLeave autocmd. 146-- Process events (via :sleep) to handle the escape sequence now. 147n . exec ( [[ 148autocmd TermLeave * ++once call chansend(&channel, "\e[?25h") | sleep 1m 149\ | let g:did_termleave = 1]] ) 150feed ( [[<C-\><C-N>]] ) -- Exit terminal mode; cursor should not remain hidden 151screen : expect ( [[ 152tty ready | 153^ | 154|*5 155]] ) 156-- Wait for the TermLeave autocommand to finish. Sometimes :sleep can be slow. 157retry ( nil , 1000 , function () 158eq ( 1 , api . nvim_get_var ( 'did_termleave' )) 159end ) 160 161command ( 'bwipeout! | let chan = nvim_open_term(0, {})' ) 162feed ( 'i' ) 163-- Hide the cursor, switch to a non-terminal buffer, then show the cursor; it shouldn't remain 164-- hidden after we're kicked out of terminal mode in the new buffer. 165-- Must ensure these actions happen within the same terminal_execute call. The stream is 166-- internal, so polling the event loop isn't necessary (terminal_receive is directly called). 167command ( [[call chansend(chan, "\e[?25l") | new floob | call chansend(chan, "\e[?25h")]] ) 168screen : expect ( [[ 169^ | 170{100:~ }| 171{3:floob }| 172|*2 173{119:[Scratch] [-] }| 174| 175]] ) 176 177feed ( '<C-W>pi' ) 178screen : expect ( [[ 179| 180{100:~ }| 181{2:floob }| 182^ | 183| 184{120:[Scratch] [-] }| 185{5:-- TERMINAL --} | 186]] ) 187end ) 188 189it ( 'becomes visible on TermLeave if hidden immediately by events #32456' , function () 190-- Reproducing the issue is quite fragile; it's easiest done in a lone test case like this 191-- with no prior commands. 192feed ( [[<C-\><C-N>]] ) 193screen : expect ( [[ 194tty ready | 195^ | 196|*5 197]] ) 198 199-- Hide the cursor such that the escape sequence is processed as a side effect of showmode in 200-- terminal_enter handling events (skip_showmode -> char_avail -> vpeekc -> os_breakcheck). 201-- This requires a particular set of actions; :startinsert repros better than feed('i') here. 202hide_cursor () 203command ( 'mode | startinsert' ) 204screen : expect ( [[ 205tty ready | 206|*5 207{5:-- TERMINAL --} | 208]] ) 209 210feed ( [[<C-\><C-N>]] ) 211screen : expect ( [[ 212tty ready | 213^ | 214|*5 215]] ) 216end ) 217end ) 218 219it ( 'can be modified by application #3681 #31685' , function () 220local states ={ 221[ 1 ] ={ blink =true , shape ='block' } , 222[ 2 ] ={ blink =false , shape ='block' } , 223[ 3 ] ={ blink =true , shape ='horizontal' } , 224[ 4 ] ={ blink =false , shape ='horizontal' } , 225[ 5 ] ={ blink =true , shape ='vertical' } , 226[ 6 ] ={ blink =false , shape ='vertical' } , 227} 228 229for k , v in pairs ( states ) do 230tt . feed_csi (( '%d q' ): format ( k )) 231screen : expect ( { 232grid =[[ 233tty ready | 234^ | 235|*4 236{5:-- TERMINAL --} | 237]] , 238condition =function () 239if v . blink then 240eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 241eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 242else 243eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 244eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 245end 246 247eq ( v . shape , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 248 249-- Cell percentages are hard coded for each shape in terminal.c 250if v . shape == 'horizontal' then 251eq ( 20 , screen . _mode_info [ terminal_mode_idx ]. cell_percentage ) 252elseif v . shape == 'vertical' then 253eq ( 25 , screen . _mode_info [ terminal_mode_idx ]. cell_percentage ) 254end 255end , 256} ) 257end 258 259feed ( [[<C-\><C-N>]] ) 260 261screen : expect ( [[ 262tty ready | 263^ | 264|*5 265]] ) 266 267-- Cursor returns to default on TermLeave 268eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 269eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 270eq ( 'block' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 271end ) 272 273it ( 'can be modified per terminal' , function () 274-- Set cursor to vertical bar with blink 275tt . feed_csi ( '5 q' ) 276screen : expect ( { 277grid =[[ 278tty ready | 279^ | 280|*4 281{5:-- TERMINAL --} | 282]] , 283condition =function () 284eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 285eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 286eq ( 'vertical' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 287end , 288} ) 289 290tt . hide_cursor () 291screen : expect ( { 292grid =[[ 293tty ready | 294|*5 295{5:-- TERMINAL --} | 296]] , 297condition =function () 298eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 299eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 300eq ( 'vertical' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 301end , 302} ) 303 304-- Exit terminal mode to reset terminal cursor settings to default and 305-- create a new terminal window 306feed ( [[<C-\><C-N>]] ) 307command ( 'set statusline=~~~' ) 308command ( 'new' ) 309call ( 'jobstart' , { testprg ( 'tty-test' ) } , { term =true } ) 310feed ( 'i' ) 311screen : expect ( { 312grid =[[ 313tty ready | 314^ | 315{120:~~~ }| 316rows: 2, cols: 50 | 317| 318{119:~~~ }| 319{5:-- TERMINAL --} | 320]] , 321condition =function () 322-- New terminal, cursor resets to defaults 323eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 324eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 325eq ( 'block' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 326end , 327} ) 328 329-- Set cursor to underline, no blink 330tt . feed_csi ( '4 q' ) 331screen : expect ( { 332grid =[[ 333tty ready | 334^ | 335{120:~~~ }| 336rows: 2, cols: 50 | 337| 338{119:~~~ }| 339{5:-- TERMINAL --} | 340]] , 341condition =function () 342eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 343eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 344eq ( 'horizontal' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 345end , 346} ) 347 348-- Switch back to first terminal, cursor should still be hidden 349command ( 'wincmd p' ) 350screen : expect ( { 351grid =[[ 352tty ready | 353| 354{119:~~~ }| 355rows: 2, cols: 50 | 356| 357{120:~~~ }| 358{5:-- TERMINAL --} | 359]] , 360condition =function () 361eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 362eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 363eq ( 'vertical' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 364end , 365} ) 366end ) 367 368it ( 'can be positioned arbitrarily' , function () 369clear () 370screen =tt . setup_child_nvim ( { 371'-u' , 372'NONE' , 373'-i' , 374'NONE' , 375'--cmd' , 376n . nvim_set .. ' noshowmode' , 377} ) 378screen : expect ( [[ 379^ | 380~ |*4 381| 382{5:-- TERMINAL --} | 383]] ) 384 385feed ( 'i<Tab>' ) 386screen : expect ( [[ 387^ | 388~ |*4 389| 390{5:-- TERMINAL --} | 391]] ) 392end ) 393 394it ( 'preserves guicursor value on TermLeave #31612' , function () 395eq ( 3 , screen . _mode_info [ terminal_mode_idx ]. hl_id ) 396 397-- Change 'guicursor' while terminal mode is active 398command ( 'set guicursor+=t:Error' ) 399 400local error_hl_id =call ( 'hlID' , 'Error' ) 401 402screen : expect ( { 403condition =function () 404eq ( error_hl_id , screen . _mode_info [ terminal_mode_idx ]. hl_id ) 405end , 406} ) 407 408-- Exit terminal mode 409feed ( [[<C-\><C-N>]] ) 410 411screen : expect ( [[ 412tty ready | 413^ | 414|*5 415]] ) 416 417eq ( error_hl_id , screen . _mode_info [ terminal_mode_idx ]. hl_id ) 418end ) 419 420it ( 'uses the correct attributes' , function () 421feed ( [[<C-\><C-N>]] ) 422command ( [[ 423bwipeout! 424let chan1 = nvim_open_term(0, {}) 425vnew 426let chan2 = nvim_open_term(0, {}) 427]] ) 428feed ( 'i' ) 429local s0 =[[ 430^ │ | 431│ |*4 432{120:[Scratch] [-] }{119:[Scratch] [-] }| 433{5:-- TERMINAL --} | 434]] 435screen : expect ( s0 ) 436eq ( 'block' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 437eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 438eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 439 440-- Modify cursor in the non-current terminal; should not affect this cursor. 441command ( [[call chansend(chan1, "\e[4 q")]] ) 442screen : expect ( { 443grid =s0 , 444unchanged =true , 445condition =function () 446eq ( 'block' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 447eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 448eq ( 500 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 449end , 450} ) 451 452-- Modify cursor in the current terminal. 453command ( [[call chansend(chan2, "\e[6 q")]] ) 454screen : expect ( { 455grid =s0 , 456condition =function () 457eq ( 'vertical' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 458eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 459eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 460end , 461} ) 462 463-- Check the cursor in the other terminal reflects our changes from before. 464command ( 'wincmd p' ) 465screen : expect ( [[ 466│^ | 467│ |*4 468{119:[Scratch] [-] }{120:[Scratch] [-] }| 469{5:-- TERMINAL --} | 470]] ) 471eq ( 'horizontal' , screen . _mode_info [ terminal_mode_idx ]. cursor_shape ) 472eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkon ) 473eq ( 0 , screen . _mode_info [ terminal_mode_idx ]. blinkoff ) 474end ) 475 476it ( 'position correct within events' , function () 477local term , term_unfocused =exec_lua ( function () 478vim . cmd 'bwipeout!' 479local term_unfocused =vim . api . nvim_open_term ( 0 , {} ) 480vim . cmd . vnew () 481vim . cmd . wincmd '|' 482local term =vim . api . nvim_open_term ( 0 , {} ) 483-- We'll use this keymap to pause the main loop while we send events, as we want the test to 484-- run within the same terminal_execute call (while using test suite facilities like retry). 485vim . keymap . set ( 't' , '<F1>' , '<Cmd>let g:sleepy = 1 | sleep 5000 | let g:sleepy = 0<CR>' ) 486return term , term_unfocused 487end ) 488feed ( 'i<F1>' ) 489 490local function check_pos ( expected_pos , expected_virtcol , chan , data ) 491api . nvim_chan_send ( chan , data ) -- Using nvim_chan_send so terminal_receive is immediate. 492 493-- Results won't be visible until refresh_terminal is called, which happens on a timer. 494retry ( nil , nil , function () 495eq ( expected_pos , eval ( "getpos('.')[1:]" )) 496end ) 497eq ( expected_virtcol , eval ( "virtcol('.', 1)" )) 498eq ( 1 , eval ( 'g:sleepy' )) -- :sleep shouldn't have timed out. 499end 500 501check_pos ( { 1 , 4 , 0 } , { 4 , 4 } , term , 'foo' ) 502-- double-width char at end (3 bytes) 503check_pos ( { 2 , 13 , 0 } , { 12 , 12 } , term , '\r\nbarbaaaar哦' ) 504-- Move to 1,12 (beyond eol; sets coladd) 505check_pos ( { 1 , 4 , 8 } , { 12 , 12 } , term , '\27[1;12H' ) 506-- Move to 4,1 507check_pos ( { 4 , 1 , 0 } , { 1 , 1 } , term , '\27[4;1H' ) 508-- Move to 4,5 (beyond eol; sets coladd) 509check_pos ( { 4 , 1 , 4 } , { 5 , 5 } , term , '\27[4;5H' ) 510-- Move to 2,10 (head of wide char) 511check_pos ( { 2 , 10 , 0 } , { 10 , 11 } , term , '\27[2;10H' ) 512-- Move to 2,11 (non-head of wide char) 513check_pos ( { 2 , 10 , 0 } , { 10 , 11 } , term , '\27[2;11H' ) 514-- Move to 2,12 (after wide char) 515check_pos ( { 2 , 13 , 0 } , { 12 , 12 } , term , '\27[2;12H' ) 516-- Move to 2,13 (beyond eol; sets coladd) 517check_pos ( { 2 , 13 , 1 } , { 13 , 13 } , term , '\27[2;13H' ) 518-- Cursor movement in unfocused terminal shouldn't affect us 519check_pos ( { 2 , 13 , 1 } , { 13 , 13 } , term_unfocused , 'amogus' ) 520end ) 521end ) 522 523describe ( 'buffer cursor position is correct in terminal without number column' , function () 524local screen 525 526local function setup_register_and_prompt ( str , prompt ) 527prompt =':' .. ( prompt or '' ) 528screen =tt . setup_child_nvim ( { 529'-u' , 530'NONE' , 531'-i' , 532'NONE' , 533'--cmd' , 534string . format ( 'let @r = "%s"' , str ), 535-- <Left> and <Right> don't always work 536'--cmd' , 537'cnoremap <C-X> <Left>' , 538'--cmd' , 539'cnoremap <C-O> <Right>' , 540'--cmd' , 541'set notermguicolors' , 542-- A blocked cmdline prompt at the last row, as a stable text-entry to test cursor tracking. 543( '+call input("%s")' ): format ( prompt ), 544} , { 545cols =70 , 546} ) 547screen : expect (( [[ 548|*5 549%s^%s| 550{5:-- TERMINAL --} | 551]] ): format ( prompt , ( ' ' ): rep ( 70 - # prompt ))) 552end 553 554before_each ( function () 555clear () 556command ( 'autocmd! nvim.terminal TermOpen' ) 557end ) 558 559describe ( 'in a line with no multibyte chars or trailing spaces,' , function () 560before_each ( function () 561setup_register_and_prompt ( 'aaaaaaaa' ) 562end ) 563 564it ( 'at the end' , function () 565feed ( '<C-R>r' ) 566screen : expect ( [[ 567|*5 568:aaaaaaaa^ | 569{5:-- TERMINAL --} | 570]] ) 571eq ( { 6 , 9 } , eval ( 'nvim_win_get_cursor(0)' )) 572feed ( [[<C-\><C-N>]] ) 573screen : expect ( [[ 574|*5 575:aaaaaaa^a | 576| 577]] ) 578eq ( { 6 , 8 } , eval ( 'nvim_win_get_cursor(0)' )) 579end ) 580 581it ( 'near the end' , function () 582feed ( '<C-R>r<C-X><C-X>' ) 583screen : expect ( [[ 584|*5 585:aaaaaa^aa | 586{5:-- TERMINAL --} | 587]] ) 588eq ( { 6 , 7 } , eval ( 'nvim_win_get_cursor(0)' )) 589feed ( [[<C-\><C-N>]] ) 590screen : expect ( [[ 591|*5 592:aaaaa^aaa | 593| 594]] ) 595eq ( { 6 , 6 } , eval ( 'nvim_win_get_cursor(0)' )) 596end ) 597 598it ( 'near the start' , function () 599feed ( '<C-R>r<C-B><C-O>' ) 600screen : expect ( [[ 601|*5 602:a^aaaaaaa | 603{5:-- TERMINAL --} | 604]] ) 605eq ( { 6 , 2 } , eval ( 'nvim_win_get_cursor(0)' )) 606feed ( [[<C-\><C-N>]] ) 607screen : expect ( [[ 608|*5 609:^aaaaaaaa | 610| 611]] ) 612eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 613end ) 614end ) 615 616describe ( 'in a line with single-cell multibyte chars and no trailing spaces,' , function () 617before_each ( function () 618setup_register_and_prompt ( 'µµµµµµµµ' ) 619end ) 620 621it ( 'at the end' , function () 622feed ( '<C-R>r' ) 623screen : expect ( [[ 624|*5 625:µµµµµµµµ^ | 626{5:-- TERMINAL --} | 627]] ) 628eq ( { 6 , 17 } , eval ( 'nvim_win_get_cursor(0)' )) 629feed ( [[<C-\><C-N>]] ) 630screen : expect ( [[ 631|*5 632:µµµµµµµ^µ | 633| 634]] ) 635eq ( { 6 , 15 } , eval ( 'nvim_win_get_cursor(0)' )) 636end ) 637 638it ( 'near the end' , function () 639feed ( '<C-R>r<C-X><C-X>' ) 640screen : expect ( [[ 641|*5 642:µµµµµµ^µµ | 643{5:-- TERMINAL --} | 644]] ) 645eq ( { 6 , 13 } , eval ( 'nvim_win_get_cursor(0)' )) 646feed ( [[<C-\><C-N>]] ) 647screen : expect ( [[ 648|*5 649:µµµµµ^µµµ | 650| 651]] ) 652eq ( { 6 , 11 } , eval ( 'nvim_win_get_cursor(0)' )) 653end ) 654 655it ( 'near the start' , function () 656feed ( '<C-R>r<C-B><C-O>' ) 657screen : expect ( [[ 658|*5 659:µ^µµµµµµµ | 660{5:-- TERMINAL --} | 661]] ) 662eq ( { 6 , 3 } , eval ( 'nvim_win_get_cursor(0)' )) 663feed ( [[<C-\><C-N>]] ) 664screen : expect ( [[ 665|*5 666:^µµµµµµµµ | 667| 668]] ) 669eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 670end ) 671end ) 672 673describe ( 'in a line with single-cell composed multibyte chars and no trailing spaces,' , function () 674before_each ( function () 675setup_register_and_prompt ( 'µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳' ) 676end ) 677 678it ( 'at the end' , function () 679feed ( '<C-R>r' ) 680screen : expect ( [[ 681|*5 682:µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳^ | 683{5:-- TERMINAL --} | 684]] ) 685eq ( { 6 , 33 } , eval ( 'nvim_win_get_cursor(0)' )) 686feed ( [[<C-\><C-N>]] ) 687screen : expect ( [[ 688|*5 689:µ̳µ̳µ̳µ̳µ̳µ̳µ̳^µ̳ | 690| 691]] ) 692eq ( { 6 , 29 } , eval ( 'nvim_win_get_cursor(0)' )) 693end ) 694 695it ( 'near the end' , function () 696skip ( is_os ( 'win' )) 697feed ( '<C-R>r<C-X><C-X>' ) 698screen : expect ( [[ 699|*5 700:µ̳µ̳µ̳µ̳µ̳µ̳^µ̳µ̳ | 701{5:-- TERMINAL --} | 702]] ) 703eq ( { 6 , 25 } , eval ( 'nvim_win_get_cursor(0)' )) 704feed ( [[<C-\><C-N>]] ) 705screen : expect ( [[ 706|*5 707:µ̳µ̳µ̳µ̳µ̳^µ̳µ̳µ̳ | 708| 709]] ) 710eq ( { 6 , 21 } , eval ( 'nvim_win_get_cursor(0)' )) 711end ) 712 713it ( 'near the start' , function () 714skip ( is_os ( 'win' )) 715feed ( '<C-R>r<C-B><C-O>' ) 716screen : expect ( [[ 717|*5 718:µ̳^µ̳µ̳µ̳µ̳µ̳µ̳µ̳ | 719{5:-- TERMINAL --} | 720]] ) 721eq ( { 6 , 5 } , eval ( 'nvim_win_get_cursor(0)' )) 722feed ( [[<C-\><C-N>]] ) 723screen : expect ( [[ 724|*5 725:^µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳ | 726| 727]] ) 728eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 729end ) 730end ) 731 732describe ( 'in a line with double-cell multibyte chars and no trailing spaces,' , function () 733before_each ( function () 734setup_register_and_prompt ( '哦哦哦哦哦哦哦哦' ) 735end ) 736 737it ( 'at the end' , function () 738feed ( '<C-R>r' ) 739screen : expect ( [[ 740|*5 741:哦哦哦哦哦哦哦哦^ | 742{5:-- TERMINAL --} | 743]] ) 744eq ( { 6 , 25 } , eval ( 'nvim_win_get_cursor(0)' )) 745feed ( [[<C-\><C-N>]] ) 746screen : expect ( [[ 747|*5 748:哦哦哦哦哦哦哦^哦 | 749| 750]] ) 751eq ( { 6 , 22 } , eval ( 'nvim_win_get_cursor(0)' )) 752end ) 753 754it ( 'near the end' , function () 755feed ( '<C-R>r<C-X><C-X>' ) 756screen : expect ( [[ 757|*5 758:哦哦哦哦哦哦^哦哦 | 759{5:-- TERMINAL --} | 760]] ) 761eq ( { 6 , 19 } , eval ( 'nvim_win_get_cursor(0)' )) 762feed ( [[<C-\><C-N>]] ) 763screen : expect ( [[ 764|*5 765:哦哦哦哦哦^哦哦哦 | 766| 767]] ) 768eq ( { 6 , 16 } , eval ( 'nvim_win_get_cursor(0)' )) 769end ) 770 771it ( 'near the start' , function () 772feed ( '<C-R>r<C-B><C-O>' ) 773screen : expect ( [[ 774|*5 775:哦^哦哦哦哦哦哦哦 | 776{5:-- TERMINAL --} | 777]] ) 778eq ( { 6 , 4 } , eval ( 'nvim_win_get_cursor(0)' )) 779feed ( [[<C-\><C-N>]] ) 780screen : expect ( [[ 781|*5 782:^哦哦哦哦哦哦哦哦 | 783| 784]] ) 785eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 786end ) 787end ) 788 789it ( 'at the end of a line with trailing spaces #16234' , function () 790-- Use the raw tty-test child: it echoes input verbatim, so the trailing spaces are real 791-- terminal cells (a cmdline/TUI child never emits trailing blank cells). 792screen =tt . setup_screen ( nil , nil , 70 ) 793tt . feed_data ( 'aaaaaaaa ' ) 794screen : expect ( [[ 795tty ready | 796aaaaaaaa ^ | 797|*4 798{5:-- TERMINAL --} | 799]] ) 800matches ( '^aaaaaaaa [ ]*$' , eval ( 'nvim_get_current_line()' )) 801eq ( { 2 , 12 } , eval ( 'nvim_win_get_cursor(0)' )) 802feed ( [[<C-\><C-N>]] ) 803eq ( { 2 , 11 } , eval ( 'nvim_win_get_cursor(0)' )) 804end ) 805end ) 806 807describe ( 'buffer cursor position is correct in terminal with number column' , function () 808local screen 809 810local function setup_register_and_prompt ( str , prompt ) 811prompt =':' .. ( prompt or '' ) 812screen =tt . setup_child_nvim ( { 813'-u' , 814'NONE' , 815'-i' , 816'NONE' , 817'--cmd' , 818string . format ( 'let @r = "%s"' , str ), 819-- <Left> and <Right> don't always work 820'--cmd' , 821'cnoremap <C-X> <Left>' , 822'--cmd' , 823'cnoremap <C-O> <Right>' , 824'--cmd' , 825'set notermguicolors' , 826-- A blocked cmdline prompt at the last row, as a stable text-entry to test cursor tracking. 827( '+call input("%s")' ): format ( prompt ), 828} , { 829cols =70 , 830} ) 831screen : expect (( [[ 832{121: 1 } | 833{121: 2 } | 834{121: 3 } | 835{121: 4 } | 836{121: 5 } | 837{121: 6 }%s^%s| 838{5:-- TERMINAL --} | 839]] ): format ( prompt , ( ' ' ): rep ( 66 - # prompt ))) 840end 841 842before_each ( function () 843clear () 844command ( 'autocmd! nvim.terminal TermOpen' ) 845-- 'number' should be set before the terminal process starts, otherwise the resize 846-- from setting 'number' may cause a redraw that changes the child screen. 847command ( 'set number' ) 848end ) 849 850describe ( 'in a line with no multibyte chars or trailing spaces,' , function () 851before_each ( function () 852setup_register_and_prompt ( 'aaaaaaaa' ) 853end ) 854 855it ( 'at the end' , function () 856feed ( '<C-R>r' ) 857screen : expect ( [[ 858{121: 1 } | 859{121: 2 } | 860{121: 3 } | 861{121: 4 } | 862{121: 5 } | 863{121: 6 }:aaaaaaaa^ | 864{5:-- TERMINAL --} | 865]] ) 866eq ( { 6 , 9 } , eval ( 'nvim_win_get_cursor(0)' )) 867feed ( [[<C-\><C-N>]] ) 868screen : expect ( [[ 869{121: 1 } | 870{121: 2 } | 871{121: 3 } | 872{121: 4 } | 873{121: 5 } | 874{121: 6 }:aaaaaaa^a | 875| 876]] ) 877eq ( { 6 , 8 } , eval ( 'nvim_win_get_cursor(0)' )) 878end ) 879 880it ( 'near the end' , function () 881feed ( '<C-R>r<C-X><C-X>' ) 882screen : expect ( [[ 883{121: 1 } | 884{121: 2 } | 885{121: 3 } | 886{121: 4 } | 887{121: 5 } | 888{121: 6 }:aaaaaa^aa | 889{5:-- TERMINAL --} | 890]] ) 891eq ( { 6 , 7 } , eval ( 'nvim_win_get_cursor(0)' )) 892feed ( [[<C-\><C-N>]] ) 893screen : expect ( [[ 894{121: 1 } | 895{121: 2 } | 896{121: 3 } | 897{121: 4 } | 898{121: 5 } | 899{121: 6 }:aaaaa^aaa | 900| 901]] ) 902eq ( { 6 , 6 } , eval ( 'nvim_win_get_cursor(0)' )) 903end ) 904 905it ( 'near the start' , function () 906feed ( '<C-R>r<C-B><C-O>' ) 907screen : expect ( [[ 908{121: 1 } | 909{121: 2 } | 910{121: 3 } | 911{121: 4 } | 912{121: 5 } | 913{121: 6 }:a^aaaaaaa | 914{5:-- TERMINAL --} | 915]] ) 916eq ( { 6 , 2 } , eval ( 'nvim_win_get_cursor(0)' )) 917feed ( [[<C-\><C-N>]] ) 918screen : expect ( [[ 919{121: 1 } | 920{121: 2 } | 921{121: 3 } | 922{121: 4 } | 923{121: 5 } | 924{121: 6 }:^aaaaaaaa | 925| 926]] ) 927eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 928end ) 929end ) 930 931describe ( 'in a line with single-cell multibyte chars and no trailing spaces,' , function () 932before_each ( function () 933setup_register_and_prompt ( 'µµµµµµµµ' ) 934end ) 935 936it ( 'at the end' , function () 937feed ( '<C-R>r' ) 938screen : expect ( [[ 939{121: 1 } | 940{121: 2 } | 941{121: 3 } | 942{121: 4 } | 943{121: 5 } | 944{121: 6 }:µµµµµµµµ^ | 945{5:-- TERMINAL --} | 946]] ) 947eq ( { 6 , 17 } , eval ( 'nvim_win_get_cursor(0)' )) 948feed ( [[<C-\><C-N>]] ) 949screen : expect ( [[ 950{121: 1 } | 951{121: 2 } | 952{121: 3 } | 953{121: 4 } | 954{121: 5 } | 955{121: 6 }:µµµµµµµ^µ | 956| 957]] ) 958eq ( { 6 , 15 } , eval ( 'nvim_win_get_cursor(0)' )) 959end ) 960 961it ( 'near the end' , function () 962feed ( '<C-R>r<C-X><C-X>' ) 963screen : expect ( [[ 964{121: 1 } | 965{121: 2 } | 966{121: 3 } | 967{121: 4 } | 968{121: 5 } | 969{121: 6 }:µµµµµµ^µµ | 970{5:-- TERMINAL --} | 971]] ) 972eq ( { 6 , 13 } , eval ( 'nvim_win_get_cursor(0)' )) 973feed ( [[<C-\><C-N>]] ) 974screen : expect ( [[ 975{121: 1 } | 976{121: 2 } | 977{121: 3 } | 978{121: 4 } | 979{121: 5 } | 980{121: 6 }:µµµµµ^µµµ | 981| 982]] ) 983eq ( { 6 , 11 } , eval ( 'nvim_win_get_cursor(0)' )) 984end ) 985 986it ( 'near the start' , function () 987feed ( '<C-R>r<C-B><C-O>' ) 988screen : expect ( [[ 989{121: 1 } | 990{121: 2 } | 991{121: 3 } | 992{121: 4 } | 993{121: 5 } | 994{121: 6 }:µ^µµµµµµµ | 995{5:-- TERMINAL --} | 996]] ) 997eq ( { 6 , 3 } , eval ( 'nvim_win_get_cursor(0)' )) 998feed ( [[<C-\><C-N>]] ) 999screen : expect ( [[ 1000{121: 1 } | 1001{121: 2 } | 1002{121: 3 } | 1003{121: 4 } | 1004{121: 5 } | 1005{121: 6 }:^µµµµµµµµ | 1006| 1007]] ) 1008eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 1009end ) 1010end ) 1011 1012describe ( 'in a line with single-cell composed multibyte chars and no trailing spaces,' , function () 1013before_each ( function () 1014setup_register_and_prompt ( 'µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳' ) 1015end ) 1016 1017it ( 'at the end' , function () 1018feed ( '<C-R>r' ) 1019screen : expect ( [[ 1020{121: 1 } | 1021{121: 2 } | 1022{121: 3 } | 1023{121: 4 } | 1024{121: 5 } | 1025{121: 6 }:µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳^ | 1026{5:-- TERMINAL --} | 1027]] ) 1028eq ( { 6 , 33 } , eval ( 'nvim_win_get_cursor(0)' )) 1029feed ( [[<C-\><C-N>]] ) 1030screen : expect ( [[ 1031{121: 1 } | 1032{121: 2 } | 1033{121: 3 } | 1034{121: 4 } | 1035{121: 5 } | 1036{121: 6 }:µ̳µ̳µ̳µ̳µ̳µ̳µ̳^µ̳ | 1037| 1038]] ) 1039eq ( { 6 , 29 } , eval ( 'nvim_win_get_cursor(0)' )) 1040end ) 1041 1042it ( 'near the end' , function () 1043skip ( is_os ( 'win' )) 1044feed ( '<C-R>r<C-X><C-X>' ) 1045screen : expect ( [[ 1046{121: 1 } | 1047{121: 2 } | 1048{121: 3 } | 1049{121: 4 } | 1050{121: 5 } | 1051{121: 6 }:µ̳µ̳µ̳µ̳µ̳µ̳^µ̳µ̳ | 1052{5:-- TERMINAL --} | 1053]] ) 1054eq ( { 6 , 25 } , eval ( 'nvim_win_get_cursor(0)' )) 1055feed ( [[<C-\><C-N>]] ) 1056screen : expect ( [[ 1057{121: 1 } | 1058{121: 2 } | 1059{121: 3 } | 1060{121: 4 } | 1061{121: 5 } | 1062{121: 6 }:µ̳µ̳µ̳µ̳µ̳^µ̳µ̳µ̳ | 1063| 1064]] ) 1065eq ( { 6 , 21 } , eval ( 'nvim_win_get_cursor(0)' )) 1066end ) 1067 1068it ( 'near the start' , function () 1069skip ( is_os ( 'win' )) 1070feed ( '<C-R>r<C-B><C-O>' ) 1071screen : expect ( [[ 1072{121: 1 } | 1073{121: 2 } | 1074{121: 3 } | 1075{121: 4 } | 1076{121: 5 } | 1077{121: 6 }:µ̳^µ̳µ̳µ̳µ̳µ̳µ̳µ̳ | 1078{5:-- TERMINAL --} | 1079]] ) 1080eq ( { 6 , 5 } , eval ( 'nvim_win_get_cursor(0)' )) 1081feed ( [[<C-\><C-N>]] ) 1082screen : expect ( [[ 1083{121: 1 } | 1084{121: 2 } | 1085{121: 3 } | 1086{121: 4 } | 1087{121: 5 } | 1088{121: 6 }:^µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳ | 1089| 1090]] ) 1091eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 1092end ) 1093end ) 1094 1095describe ( 'in a line with double-cell multibyte chars and no trailing spaces,' , function () 1096before_each ( function () 1097setup_register_and_prompt ( '哦哦哦哦哦哦哦哦' ) 1098end ) 1099 1100it ( 'at the end' , function () 1101feed ( '<C-R>r' ) 1102screen : expect ( [[ 1103{121: 1 } | 1104{121: 2 } | 1105{121: 3 } | 1106{121: 4 } | 1107{121: 5 } | 1108{121: 6 }:哦哦哦哦哦哦哦哦^ | 1109{5:-- TERMINAL --} | 1110]] ) 1111eq ( { 6 , 25 } , eval ( 'nvim_win_get_cursor(0)' )) 1112feed ( [[<C-\><C-N>]] ) 1113screen : expect ( [[ 1114{121: 1 } | 1115{121: 2 } | 1116{121: 3 } | 1117{121: 4 } | 1118{121: 5 } | 1119{121: 6 }:哦哦哦哦哦哦哦^哦 | 1120| 1121]] ) 1122eq ( { 6 , 22 } , eval ( 'nvim_win_get_cursor(0)' )) 1123end ) 1124 1125it ( 'near the end' , function () 1126feed ( '<C-R>r<C-X><C-X>' ) 1127screen : expect ( [[ 1128{121: 1 } | 1129{121: 2 } | 1130{121: 3 } | 1131{121: 4 } | 1132{121: 5 } | 1133{121: 6 }:哦哦哦哦哦哦^哦哦 | 1134{5:-- TERMINAL --} | 1135]] ) 1136eq ( { 6 , 19 } , eval ( 'nvim_win_get_cursor(0)' )) 1137feed ( [[<C-\><C-N>]] ) 1138screen : expect ( [[ 1139{121: 1 } | 1140{121: 2 } | 1141{121: 3 } | 1142{121: 4 } | 1143{121: 5 } | 1144{121: 6 }:哦哦哦哦哦^哦哦哦 | 1145| 1146]] ) 1147eq ( { 6 , 16 } , eval ( 'nvim_win_get_cursor(0)' )) 1148end ) 1149 1150it ( 'near the start' , function () 1151feed ( '<C-R>r<C-B><C-O>' ) 1152screen : expect ( [[ 1153{121: 1 } | 1154{121: 2 } | 1155{121: 3 } | 1156{121: 4 } | 1157{121: 5 } | 1158{121: 6 }:哦^哦哦哦哦哦哦哦 | 1159{5:-- TERMINAL --} | 1160]] ) 1161eq ( { 6 , 4 } , eval ( 'nvim_win_get_cursor(0)' )) 1162feed ( [[<C-\><C-N>]] ) 1163screen : expect ( [[ 1164{121: 1 } | 1165{121: 2 } | 1166{121: 3 } | 1167{121: 4 } | 1168{121: 5 } | 1169{121: 6 }:^哦哦哦哦哦哦哦哦 | 1170| 1171]] ) 1172eq ( { 6 , 1 } , eval ( 'nvim_win_get_cursor(0)' )) 1173end ) 1174end ) 1175 1176it ( 'at the end of a line with trailing spaces #16234' , function () 1177-- Raw tty-test child: trailing spaces are real terminal cells. See the no-number variant. 1178-- 'number' is set after the terminal opens: setup_screen() asserts the default layout. 1179command ( 'set nonumber' ) 1180screen =tt . setup_screen ( nil , nil , 70 ) 1181command ( 'set number' ) 1182screen : expect ( { any ='rows: 6, cols: 66' } ) 1183tt . feed_data ( 'aaaaaaaa ' ) 1184screen : expect ( [[ 1185{121: 1 }tty ready | 1186{121: 2 }rows: 6, cols: 66 | 1187{121: 3 }aaaaaaaa ^ | 1188{121: 4 } | 1189{121: 5 } | 1190{121: 6 } | 1191{5:-- TERMINAL --} | 1192]] ) 1193matches ( '^aaaaaaaa [ ]*$' , eval ( 'nvim_get_current_line()' )) 1194eq ( { 3 , 12 } , eval ( 'nvim_win_get_cursor(0)' )) 1195feed ( [[<C-\><C-N>]] ) 1196eq ( { 3 , 11 } , eval ( 'nvim_win_get_cursor(0)' )) 1197end ) 1198end )