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' ) 4local tt =require ( 'test.functional.testterm' ) 5 6local describe , it , before_each , setup , teardown = 7t . describe , t . it , t . before_each , t . setup , t . teardown 8local clear , eq , neq =n . clear , t . eq , t . neq 9local feed , testprg =n . feed , n . testprg 10local fn =n . fn 11local eval =n . eval 12local command =n . command 13local poke_eventloop =n . poke_eventloop 14local retry =t . retry 15local api =n . api 16local feed_data =tt . feed_data 17local pcall_err =t . pcall_err 18local exec_lua =n . exec_lua 19local assert_alive =n . assert_alive 20local skip =t . skip 21local is_os =t . is_os 22 23local function test_terminal_scrollback ( hide_curbuf ) 24local screen --- @type test.functional.ui.screen 25local buf --- @type integer 26local chan --- @type integer 27local otherbuf --- @type integer 28local restore_terminal_mode --- @type boolean? 29local save_feed_data =feed_data 30 31local function may_hide_curbuf () 32if hide_curbuf then 33eq ( nil , restore_terminal_mode ) 34restore_terminal_mode =vim . startswith ( api . nvim_get_mode (). mode , 't' ) 35api . nvim_set_current_buf ( otherbuf ) 36end 37end 38 39local function may_restore_curbuf () 40if hide_curbuf then 41neq ( nil , restore_terminal_mode ) 42eq ( buf , fn . bufnr ( '#' )) 43feed ( '<C-^>' ) -- "view" in 'jumpoptions' applies to this 44if restore_terminal_mode then 45feed ( 'i' ) 46else 47-- Cursor position was restored from wi_mark, not b_last_cursor. 48-- Check that b_last_cursor and wi_mark are the same. 49--- @type integer[], integer[] 50local last_cursor , restored_cursor =unpack ( exec_lua ( function () 51-- Get these two positions on the same RPC call. 52return { vim . fn . getpos ( [['"]] ), vim . fn . getpos ( '.' ) } 53end )) 54if last_cursor [ 2 ] > 0 then 55eq ( restored_cursor , last_cursor ) 56else 57eq ( { 0 , 0 , 0 , 0 } , last_cursor ) 58eq ( { 0 , 1 , 1 , 0 } , restored_cursor ) 59end 60end 61restore_terminal_mode =nil 62end 63end 64 65setup ( function () 66feed_data =function ( data ) 67may_hide_curbuf () 68api . nvim_chan_send ( chan , data ) 69may_restore_curbuf () 70end 71end ) 72 73teardown ( function () 74feed_data =save_feed_data 75end ) 76 77--- @param prefix string 78--- @param start integer 79--- @param stop integer 80local function feed_lines ( prefix , start , stop ) 81may_hide_curbuf () 82local data ='' 83for i =start , stop do 84data =data .. prefix .. tostring ( i ) .. '\n' 85end 86api . nvim_chan_send ( chan , data ) 87retry ( nil , 1000 , function () 88eq ( { prefix .. tostring ( stop ), '' } , api . nvim_buf_get_lines ( buf , - 3 , - 1 , true )) 89end ) 90may_restore_curbuf () 91end 92 93local function try_resize ( width , height ) 94may_hide_curbuf () 95screen : try_resize ( width , height ) 96may_restore_curbuf () 97end 98 99before_each ( function () 100clear () 101command ( 'set nostartofline jumpoptions+=view' ) 102screen =tt . setup_screen ( nil , nil , 30 ) 103buf =api . nvim_get_current_buf () 104chan =api . nvim_get_option_value ( 'channel' , { buf =buf } ) 105otherbuf =hide_curbuf and api . nvim_create_buf ( true , false ) or nil 106restore_terminal_mode =nil 107end ) 108 109describe ( 'when the limit is exceeded' , function () 110before_each ( function () 111feed_lines ( 'line' , 1 , 30 ) 112screen : expect ( [[ 113line26 | 114line27 | 115line28 | 116line29 | 117line30 | 118^ | 119{5:-- TERMINAL --} | 120]] ) 121eq ( 16 , api . nvim_buf_line_count ( 0 )) 122end ) 123 124it ( 'will delete extra lines at the top' , function () 125feed ( '<c-\\><c-n>gg' ) 126screen : expect ( [[ 127^line16 | 128line17 | 129line18 | 130line19 | 131line20 | 132line21 | 133| 134]] ) 135end ) 136 137describe ( 'and cursor on non-last row in screen' , function () 138before_each ( function () 139feed ( [[<C-\><C-N>M$]] ) 140fn . setpos ( "'m" , { 0 , 13 , 4 , 0 } ) 141local ns =api . nvim_create_namespace ( 'test' ) 142api . nvim_buf_set_extmark ( 0 , ns , 12 , 0 , { end_col =6 , hl_group ='ErrorMsg' } ) 143screen : expect ( [[ 144line26 | 145line27 | 146{101:line2^8} | 147line29 | 148line30 | 149|*2 150]] ) 151end ) 152 153it ( "outputting fewer than 'scrollback' lines" , function () 154feed_lines ( 'new_line' , 1 , 6 ) 155screen : expect ( [[ 156line26 | 157line27 | 158{101:line2^8} | 159line29 | 160line30 | 161new_line1 | 162| 163]] ) 164eq ( { 0 , 7 , 4 , 0 } , fn . getpos ( "'m" )) 165eq ( { 0 , 7 , 6 , 0 } , fn . getpos ( '.' )) 166end ) 167 168it ( "outputting more than 'scrollback' lines" , function () 169feed_lines ( 'new_line' , 1 , 11 ) 170screen : expect ( [[ 171line27 | 172{101:line2^8} | 173line29 | 174line30 | 175new_line1 | 176new_line2 | 177| 178]] ) 179eq ( { 0 , 2 , 4 , 0 } , fn . getpos ( "'m" )) 180eq ( { 0 , 2 , 6 , 0 } , fn . getpos ( '.' )) 181end ) 182 183it ( 'outputting more lines than whole buffer' , function () 184feed_lines ( 'new_line' , 1 , 20 ) 185screen : expect ( [[ 186^new_line6 | 187new_line7 | 188new_line8 | 189new_line9 | 190new_line10 | 191new_line11 | 192| 193]] ) 194eq ( { 0 , 0 , 0 , 0 } , fn . getpos ( "'m" )) 195eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( '.' )) 196end ) 197 198it ( 'clearing scrollback with ED 3' , function () 199feed_data ( '\027[3J' ) 200screen : expect_unchanged ( hide_curbuf ) 201eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 202eq ( { 0 , 3 , 6 , 0 } , fn . getpos ( '.' )) 203feed ( 'gg' ) 204screen : expect ( [[ 205line2^6 | 206line27 | 207{101:line28} | 208line29 | 209line30 | 210|*2 211]] ) 212end ) 213 214it ( 'clearing scrollback with ED 3 and outputting lines' , function () 215feed_data ( '\027[3J' .. 'new_line1\nnew_line2\nnew_line3' ) 216screen : expect ( [[ 217line26 | 218line27 | 219{101:line2^8} | 220line29 | 221line30 | 222new_line1 | 223| 224]] ) 225eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 226eq ( { 0 , 3 , 6 , 0 } , fn . getpos ( '.' )) 227end ) 228 229it ( 'clearing scrollback with ED 3 between outputting lines' , function () 230skip ( is_os ( 'win' ), 'FIXME: wrong behavior on Windows, ConPTY bug?' ) 231feed_data ( 'line31\nline32\n' .. '\027[3J' .. 'new_line1\nnew_line2' ) 232screen : expect ( [[ 233{101:line2^8} | 234line29 | 235line30 | 236line31 | 237line32 | 238new_line1 | 239| 240]] ) 241eq ( { 0 , 1 , 4 , 0 } , fn . getpos ( "'m" )) 242eq ( { 0 , 1 , 6 , 0 } , fn . getpos ( '.' )) 243end ) 244end ) 245 246describe ( 'and cursor on scrollback row #12651' , function () 247before_each ( function () 248feed ( [[<C-\><C-N>Hk$]] ) 249fn . setpos ( "'m" , { 0 , 10 , 4 , 0 } ) 250local ns =api . nvim_create_namespace ( 'test' ) 251api . nvim_buf_set_extmark ( 0 , ns , 9 , 0 , { end_col =6 , hl_group ='ErrorMsg' } ) 252screen : expect ( [[ 253{101:line2^5} | 254line26 | 255line27 | 256line28 | 257line29 | 258line30 | 259| 260]] ) 261end ) 262 263it ( "outputting fewer than 'scrollback' lines" , function () 264feed_lines ( 'new_line' , 1 , 6 ) 265screen : expect_unchanged ( hide_curbuf ) 266eq ( { 0 , 4 , 4 , 0 } , fn . getpos ( "'m" )) 267eq ( { 0 , 4 , 6 , 0 } , fn . getpos ( '.' )) 268end ) 269 270it ( "outputting more than 'scrollback' lines" , function () 271feed_lines ( 'new_line' , 1 , 11 ) 272screen : expect ( [[ 273^line27 | 274line28 | 275line29 | 276line30 | 277new_line1 | 278new_line2 | 279| 280]] ) 281eq ( { 0 , 0 , 0 , 0 } , fn . getpos ( "'m" )) 282eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( '.' )) 283end ) 284end ) 285 286it ( 'changing window height does not duplicate lines' , function () 287-- XXX: Can't test this reliably on Windows unless the cursor is _moved_ 288-- by the resize. http://docs.libuv.org/en/v1.x/signal.html 289-- See also: https://github.com/rprichard/winpty/issues/110 290skip ( is_os ( 'win' )) 291try_resize ( screen . _width , screen . _height + 4 ) 292screen : expect ( [[ 293line23 | 294line24 | 295line25 | 296line26 | 297line27 | 298line28 | 299line29 | 300line30 | 301rows: 10, cols: 30 | 302^ | 303{5:-- TERMINAL --} | 304]] ) 305eq ( 17 , api . nvim_buf_line_count ( 0 )) 306try_resize ( screen . _width , screen . _height - 2 ) 307screen : expect ( [[ 308line26 | 309line27 | 310line28 | 311line29 | 312line30 | 313rows: 10, cols: 30 | 314rows: 8, cols: 30 | 315^ | 316{5:-- TERMINAL --} | 317]] ) 318eq ( 18 , api . nvim_buf_line_count ( 0 )) 319try_resize ( screen . _width , screen . _height - 3 ) 320screen : expect ( [[ 321line30 | 322rows: 10, cols: 30 | 323rows: 8, cols: 30 | 324rows: 5, cols: 30 | 325^ | 326{5:-- TERMINAL --} | 327]] ) 328eq ( 15 , api . nvim_buf_line_count ( 0 )) 329try_resize ( screen . _width , screen . _height + 3 ) 330screen : expect ( [[ 331line28 | 332line29 | 333line30 | 334rows: 10, cols: 30 | 335rows: 8, cols: 30 | 336rows: 5, cols: 30 | 337rows: 8, cols: 30 | 338^ | 339{5:-- TERMINAL --} | 340]] ) 341eq ( 16 , api . nvim_buf_line_count ( 0 )) 342feed ( [[<C-\><C-N>8<C-Y>]] ) 343screen : expect ( [[ 344line20 | 345line21 | 346line22 | 347line23 | 348line24 | 349line25 | 350line26 | 351^line27 | 352| 353]] ) 354end ) 355end ) 356 357describe ( 'with cursor at last row' , function () 358before_each ( function () 359feed_lines ( 'line' , 1 , 4 ) 360screen : expect ( [[ 361tty ready | 362line1 | 363line2 | 364line3 | 365line4 | 366^ | 367{5:-- TERMINAL --} | 368]] ) 369fn . setpos ( "'m" , { 0 , 3 , 4 , 0 } ) 370local ns =api . nvim_create_namespace ( 'test' ) 371api . nvim_buf_set_extmark ( 0 , ns , 2 , 0 , { end_col =5 , hl_group ='ErrorMsg' } ) 372screen : expect ( [[ 373tty ready | 374line1 | 375{101:line2} | 376line3 | 377line4 | 378^ | 379{5:-- TERMINAL --} | 380]] ) 381end ) 382 383it ( "outputting more than 'scrollback' lines in Normal mode" , function () 384feed ( [[<C-\><C-N>]] ) 385feed_lines ( 'new_line' , 1 , 11 ) 386screen : expect ( [[ 387new_line7 | 388new_line8 | 389new_line9 | 390new_line10 | 391new_line11 | 392^ | 393| 394]] ) 395feed ( 'gg' ) 396screen : expect ( [[ 397^line1 | 398{101:line2} | 399line3 | 400line4 | 401new_line1 | 402new_line2 | 403| 404]] ) 405eq ( { 0 , 2 , 4 , 0 } , fn . getpos ( "'m" )) 406feed ( 'G' ) 407feed_lines ( 'new_line' , 12 , 31 ) 408screen : expect ( [[ 409new_line27 | 410new_line28 | 411new_line29 | 412new_line30 | 413new_line31 | 414^ | 415| 416]] ) 417feed ( 'gg' ) 418screen : expect ( [[ 419^new_line17 | 420new_line18 | 421new_line19 | 422new_line20 | 423new_line21 | 424new_line22 | 425| 426]] ) 427eq ( { 0 , 0 , 0 , 0 } , fn . getpos ( "'m" )) 428end ) 429 430it ( 'clearing scrollback with ED 3' , function () 431-- Clearing empty scrollback and then outputting a line 432feed_data ( '\027[3J' .. 'line5\n' ) 433screen : expect ( [[ 434line1 | 435{101:line2} | 436line3 | 437line4 | 438line5 | 439^ | 440{5:-- TERMINAL --} | 441]] ) 442eq ( 7 , api . nvim_buf_line_count ( 0 )) 443eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 444-- Clearing 1 line of scrollback 445feed_data ( '\027[3J' ) 446screen : expect_unchanged ( hide_curbuf ) 447eq ( 6 , api . nvim_buf_line_count ( 0 )) 448eq ( { 0 , 2 , 4 , 0 } , fn . getpos ( "'m" )) 449-- Outputting a line 450feed_data ( 'line6\n' ) 451screen : expect ( [[ 452{101:line2} | 453line3 | 454line4 | 455line5 | 456line6 | 457^ | 458{5:-- TERMINAL --} | 459]] ) 460eq ( 7 , api . nvim_buf_line_count ( 0 )) 461eq ( { 0 , 2 , 4 , 0 } , fn . getpos ( "'m" )) 462-- Clearing 1 line of scrollback and then outputting a line 463feed_data ( '\027[3J' .. 'line7\n' ) 464screen : expect ( [[ 465line3 | 466line4 | 467line5 | 468line6 | 469line7 | 470^ | 471{5:-- TERMINAL --} | 472]] ) 473eq ( 7 , api . nvim_buf_line_count ( 0 )) 474eq ( { 0 , 1 , 4 , 0 } , fn . getpos ( "'m" )) 475-- Check first line of buffer in Normal mode 476feed ( [[<C-\><C-N>gg]] ) 477screen : expect ( [[ 478{101:^line2} | 479line3 | 480line4 | 481line5 | 482line6 | 483line7 | 484| 485]] ) 486feed ( 'G' ) 487-- Outputting lines and then clearing scrollback 488skip ( is_os ( 'win' ), 'FIXME: wrong behavior on Windows, ConPTY bug?' ) 489feed_data ( 'line8\nline9\n' .. '\027[3J' ) 490screen : expect ( [[ 491line5 | 492line6 | 493line7 | 494line8 | 495line9 | 496^ | 497| 498]] ) 499eq ( 6 , api . nvim_buf_line_count ( 0 )) 500eq ( { 0 , 0 , 0 , 0 } , fn . getpos ( "'m" )) 501end ) 502 503describe ( 'and 1 line is printed' , function () 504before_each ( function () 505feed_lines ( 'line' , 5 , 5 ) 506end ) 507 508it ( 'will hide the top line' , function () 509screen : expect ( [[ 510line1 | 511{101:line2} | 512line3 | 513line4 | 514line5 | 515^ | 516{5:-- TERMINAL --} | 517]] ) 518eq ( 7 , api . nvim_buf_line_count ( 0 )) 519eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 520end ) 521 522describe ( 'and then 3 more lines are printed' , function () 523before_each ( function () 524feed_lines ( 'line' , 6 , 8 ) 525end ) 526 527it ( 'will hide the top 4 lines' , function () 528screen : expect ( [[ 529line4 | 530line5 | 531line6 | 532line7 | 533line8 | 534^ | 535{5:-- TERMINAL --} | 536]] ) 537eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 538 539feed ( '<c-\\><c-n>6k' ) 540screen : expect ( [[ 541^line3 | 542line4 | 543line5 | 544line6 | 545line7 | 546line8 | 547| 548]] ) 549 550feed ( 'gg' ) 551screen : expect ( [[ 552^tty ready | 553line1 | 554{101:line2} | 555line3 | 556line4 | 557line5 | 558| 559]] ) 560 561feed ( 'G' ) 562screen : expect ( [[ 563line4 | 564line5 | 565line6 | 566line7 | 567line8 | 568^ | 569| 570]] ) 571end ) 572end ) 573end ) 574 575describe ( 'and height decreased by 1' , function () 576local function will_hide_top_line () 577feed ( [[<C-\><C-N>]] ) 578try_resize ( screen . _width - 2 , screen . _height - 1 ) 579screen : expect ( [[ 580{101:line2} | 581line3 | 582line4 | 583rows: 5, cols: 28 | 584^ | 585| 586]] ) 587eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 588end 589 590it ( 'will hide top line' , will_hide_top_line ) 591 592describe ( 'and then decreased by 2' , function () 593before_each ( function () 594will_hide_top_line () 595try_resize ( screen . _width - 2 , screen . _height - 2 ) 596end ) 597 598it ( 'will hide the top 3 lines' , function () 599screen : expect ( [[ 600rows: 5, cols: 28 | 601rows: 3, cols: 26 | 602^ | 603| 604]] ) 605eq ( 8 , api . nvim_buf_line_count ( 0 )) 606eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 607feed ( '3k' ) 608screen : expect ( [[ 609^line4 | 610rows: 5, cols: 28 | 611rows: 3, cols: 26 | 612| 613]] ) 614feed ( 'gg' ) 615screen : expect ( [[ 616^tty ready | 617line1 | 618{101:line2} | 619| 620]] ) 621end ) 622end ) 623end ) 624end ) 625 626describe ( 'with empty lines after the cursor' , function () 627-- XXX: Can't test this reliably on Windows unless the cursor is _moved_ 628-- by the resize. http://docs.libuv.org/en/v1.x/signal.html 629-- See also: https://github.com/rprichard/winpty/issues/110 630if skip ( is_os ( 'win' )) then 631return 632end 633 634describe ( 'and the height is decreased by 2' , function () 635before_each ( function () 636try_resize ( screen . _width , screen . _height - 2 ) 637end ) 638 639local function will_delete_last_two_lines () 640screen : expect ( [[ 641tty ready | 642rows: 4, cols: 30 | 643^ | 644| 645{5:-- TERMINAL --} | 646]] ) 647eq ( 4 , api . nvim_buf_line_count ( 0 )) 648end 649 650it ( 'will delete the last two empty lines' , will_delete_last_two_lines ) 651 652describe ( 'and then decreased by 1' , function () 653before_each ( function () 654will_delete_last_two_lines () 655try_resize ( screen . _width , screen . _height - 1 ) 656end ) 657 658it ( 'will delete the last line and hide the first' , function () 659screen : expect ( [[ 660rows: 4, cols: 30 | 661rows: 3, cols: 30 | 662^ | 663{5:-- TERMINAL --} | 664]] ) 665eq ( 4 , api . nvim_buf_line_count ( 0 )) 666feed ( '<c-\\><c-n>gg' ) 667screen : expect ( [[ 668^tty ready | 669rows: 4, cols: 30 | 670rows: 3, cols: 30 | 671| 672]] ) 673feed ( 'a' ) 674screen : expect ( [[ 675rows: 4, cols: 30 | 676rows: 3, cols: 30 | 677^ | 678{5:-- TERMINAL --} | 679]] ) 680end ) 681end ) 682end ) 683end ) 684 685describe ( 'with 4 lines hidden in the scrollback' , function () 686before_each ( function () 687feed_lines ( 'line' , 1 , 4 ) 688screen : expect ( [[ 689tty ready | 690line1 | 691line2 | 692line3 | 693line4 | 694^ | 695{5:-- TERMINAL --} | 696]] ) 697fn . setpos ( "'m" , { 0 , 3 , 4 , 0 } ) 698local ns =api . nvim_create_namespace ( 'test' ) 699api . nvim_buf_set_extmark ( 0 , ns , 2 , 0 , { end_col =5 , hl_group ='ErrorMsg' } ) 700screen : expect ( [[ 701tty ready | 702line1 | 703{101:line2} | 704line3 | 705line4 | 706^ | 707{5:-- TERMINAL --} | 708]] ) 709try_resize ( screen . _width , screen . _height - 3 ) 710screen : expect ( [[ 711line4 | 712rows: 3, cols: 30 | 713^ | 714{5:-- TERMINAL --} | 715]] ) 716eq ( 7 , api . nvim_buf_line_count ( 0 )) 717end ) 718 719describe ( 'and the height is increased by 1' , function () 720-- XXX: Can't test this reliably on Windows unless the cursor is _moved_ 721-- by the resize. http://docs.libuv.org/en/v1.x/signal.html 722-- See also: https://github.com/rprichard/winpty/issues/110 723if skip ( is_os ( 'win' )) then 724return 725end 726local function pop_then_push () 727try_resize ( screen . _width , screen . _height + 1 ) 728screen : expect ( [[ 729line4 | 730rows: 3, cols: 30 | 731rows: 4, cols: 30 | 732^ | 733{5:-- TERMINAL --} | 734]] ) 735eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 736end 737 738it ( 'will pop 1 line and then push it back' , pop_then_push ) 739 740describe ( 'and then by 3' , function () 741before_each ( function () 742pop_then_push () 743eq ( 8 , api . nvim_buf_line_count ( 0 )) 744try_resize ( screen . _width , screen . _height + 3 ) 745end ) 746 747local function pop3_then_push1 () 748screen : expect ( [[ 749{101:line2} | 750line3 | 751line4 | 752rows: 3, cols: 30 | 753rows: 4, cols: 30 | 754rows: 7, cols: 30 | 755^ | 756{5:-- TERMINAL --} | 757]] ) 758eq ( 9 , api . nvim_buf_line_count ( 0 )) 759eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 760feed ( '<c-\\><c-n>gg' ) 761screen : expect ( [[ 762^tty ready | 763line1 | 764{101:line2} | 765line3 | 766line4 | 767rows: 3, cols: 30 | 768rows: 4, cols: 30 | 769| 770]] ) 771end 772 773it ( 'will pop 3 lines and then push one back' , pop3_then_push1 ) 774 775describe ( 'and then by 4' , function () 776before_each ( function () 777pop3_then_push1 () 778feed ( 'Gi' ) 779try_resize ( screen . _width , screen . _height + 4 ) 780end ) 781 782it ( 'will show all lines and leave a blank one at the end' , function () 783screen : expect ( [[ 784tty ready | 785line1 | 786{101:line2} | 787line3 | 788line4 | 789rows: 3, cols: 30 | 790rows: 4, cols: 30 | 791rows: 7, cols: 30 | 792rows: 11, cols: 30 | 793^ | 794| 795{5:-- TERMINAL --} | 796]] ) 797-- since there's an empty line after the cursor, the buffer line 798-- count equals the terminal screen height 799eq ( 11 , api . nvim_buf_line_count ( 0 )) 800eq ( { 0 , 3 , 4 , 0 } , fn . getpos ( "'m" )) 801end ) 802end ) 803end ) 804end ) 805end ) 806 807it ( 'reducing &scrollback deletes extra lines immediately' , function () 808feed_lines ( 'line' , 1 , 30 ) 809screen : expect ( [[ 810line26 | 811line27 | 812line28 | 813line29 | 814line30 | 815^ | 816{5:-- TERMINAL --} | 817]] ) 818local term_height =6 -- Actual terminal screen height, not the scrollback 819-- Initial 820local scrollback =api . nvim_get_option_value ( 'scrollback' , { buf =buf } ) 821eq ( scrollback + term_height , fn . line ( '$' )) 822eq ( scrollback + term_height , fn . line ( '.' )) 823n . fn . setpos ( "'m" , { 0 , scrollback + 1 , 4 , 0 } ) 824local ns =api . nvim_create_namespace ( 'test' ) 825api . nvim_buf_set_extmark ( 0 , ns , scrollback , 0 , { end_col =6 , hl_group ='ErrorMsg' } ) 826screen : expect ( [[ 827{101:line26} | 828line27 | 829line28 | 830line29 | 831line30 | 832^ | 833{5:-- TERMINAL --} | 834]] ) 835-- Reduction 836scrollback =scrollback - 2 837may_hide_curbuf () 838api . nvim_set_option_value ( 'scrollback' , scrollback , { buf =buf } ) 839may_restore_curbuf () 840eq ( scrollback + term_height , fn . line ( '$' )) 841eq ( scrollback + term_height , fn . line ( '.' )) 842screen : expect_unchanged ( hide_curbuf ) 843eq ( { 0 , scrollback + 1 , 4 , 0 } , n . fn . getpos ( "'m" )) 844end ) 845end 846 847describe ( ':terminal scrollback' , function () 848describe ( 'in current buffer' , function () 849test_terminal_scrollback ( false ) 850end ) 851 852describe ( 'in hidden buffer' , function () 853test_terminal_scrollback ( true ) 854end ) 855end ) 856 857describe ( ':terminal prints more lines than the screen height and exits' , function () 858it ( 'will push extra lines to scrollback' , function () 859clear () 860local screen =Screen . new ( 30 , 7 , { rgb =false } ) 861screen : add_extra_attr_ids ( { [ 100 ] ={ foreground =12 } } ) 862command ( 863( "call jobstart(['%s', '10'], {'term':v:true}) | startinsert" ): format ( testprg ( 'tty-test' )) 864) 865screen : expect ( [[ 866line5 | 867line6 | 868line7 | 869line8 | 870line9 | 871^[Process exited 0] | 872{5:-- TERMINAL --} | 873]] ) 874feed ( '<cr>' ) 875-- closes the buffer correctly after pressing a key 876screen : expect ( [[ 877^ | 878{100:~ }|*5 879| 880]] ) 881end ) 882end ) 883 884describe ( "'scrollback' option" , function () 885before_each ( function () 886clear () 887end ) 888 889local function set_fake_shell () 890api . nvim_set_option_value ( 'shell' , string . format ( '"%s" INTERACT' , testprg ( 'shell-test' )), {} ) 891end 892 893local function expect_lines ( expected , epsilon ) 894local ep =epsilon and epsilon or 0 895local actual =eval ( "line('$')" ) 896if expected > actual + ep and expected < actual - ep then 897error ( 'expected (+/- ' .. ep .. '): ' .. expected .. ', actual: ' .. tostring ( actual )) 898end 899end 900 901it ( 'set to 0 behaves as 1' , function () 902local screen 903if is_os ( 'win' ) then 904screen =tt . setup_screen ( nil , { 'cmd.exe' } , 30 ) 905else 906screen =tt . setup_screen ( nil , { 'sh' } , 30 ) 907end 908 909api . nvim_set_option_value ( 'scrollback' , 0 , {} ) 910feed_data (( '%s REP 31 line%s' ): format ( testprg ( 'shell-test' ), is_os ( 'win' ) and '\r' or '\n' )) 911screen : expect { any ='30: line ' } 912retry ( nil , nil , function () 913expect_lines ( 7 ) 914end ) 915end ) 916 917it ( 'deletes lines (only) if necessary' , function () 918local screen 919if is_os ( 'win' ) then 920command ( [[let $PROMPT='$$']] ) 921screen =tt . setup_screen ( nil , { 'cmd.exe' } , 30 ) 922else 923command ( 'let $PS1 = "$"' ) 924screen =tt . setup_screen ( nil , { 'sh' } , 30 ) 925end 926 927api . nvim_set_option_value ( 'scrollback' , 200 , {} ) 928 929-- Wait for prompt. 930screen : expect { any ='%$' } 931 932feed_data (( '%s REP 31 line%s' ): format ( testprg ( 'shell-test' ), is_os ( 'win' ) and '\r' or '\n' )) 933screen : expect { any ='30: line ' } 934 935retry ( nil , nil , function () 936expect_lines ( 33 , 2 ) 937end ) 938api . nvim_set_option_value ( 'scrollback' , 10 , {} ) 939poke_eventloop () 940retry ( nil , nil , function () 941expect_lines ( 16 ) 942end ) 943api . nvim_set_option_value ( 'scrollback' , 10000 , {} ) 944retry ( nil , nil , function () 945expect_lines ( 16 ) 946end ) 947-- Terminal job data is received asynchronously, may happen before the 948-- 'scrollback' option is synchronized with the internal sb_buffer. 949command ( 'sleep 100m' ) 950 951feed_data (( '%s REP 41 line%s' ): format ( testprg ( 'shell-test' ), is_os ( 'win' ) and '\r' or '\n' )) 952if is_os ( 'win' ) then 953screen : expect ( [[ 95437: line | 95538: line | 95639: line | 95740: line | 958| 959$^ | 960{5:-- TERMINAL --} | 961]] ) 962else 963screen : expect ( [[ 96436: line | 96537: line | 96638: line | 96739: line | 96840: line | 969{MATCH:.*}| 970{5:-- TERMINAL --} | 971]] ) 972end 973expect_lines ( 58 ) 974 975-- Verify off-screen state 976eq (( is_os ( 'win' ) and '36: line' or '35: line' ), eval ( "getline(line('w0') - 1)->trim(' ', 2)" )) 977eq (( is_os ( 'win' ) and '27: line' or '26: line' ), eval ( "getline(line('w0') - 10)->trim(' ', 2)" )) 978end ) 979 980it ( 'defaults to 10000 in :terminal buffers' , function () 981set_fake_shell () 982command ( 'terminal' ) 983eq ( 10000 , api . nvim_get_option_value ( 'scrollback' , {} )) 984end ) 985 986it ( 'error if set to invalid value' , function () 987eq ( 'Vim(set):E474: Invalid argument: scrollback=-2' , pcall_err ( command , 'set scrollback=-2' )) 988eq ( 989'Vim(set):E474: Invalid argument: scrollback=1000001' , 990pcall_err ( command , 'set scrollback=1000001' ) 991) 992end ) 993 994it ( 'defaults to -1 on normal buffers' , function () 995command ( 'new' ) 996eq ( - 1 , api . nvim_get_option_value ( 'scrollback' , {} )) 997end ) 998 999it ( ':setlocal in a :terminal buffer' , function () 1000set_fake_shell () 1001 1002-- _Global_ scrollback=-1 defaults :terminal to 10_000. 1003command ( 'setglobal scrollback=-1' ) 1004command ( 'terminal' ) 1005eq ( 10000 , api . nvim_get_option_value ( 'scrollback' , {} )) 1006 1007-- _Local_ scrollback=-1 in :terminal forces the _maximum_. 1008command ( 'setlocal scrollback=-1' ) 1009retry ( nil , nil , function () -- Fixup happens on refresh, not immediately. 1010eq ( 1000000 , api . nvim_get_option_value ( 'scrollback' , {} )) 1011end ) 1012 1013-- _Local_ scrollback=-1 during TermOpen forces the maximum. #9605 1014command ( 'setglobal scrollback=-1' ) 1015command ( 'autocmd TermOpen * setlocal scrollback=-1' ) 1016command ( 'terminal' ) 1017eq ( 1000000 , api . nvim_get_option_value ( 'scrollback' , {} )) 1018end ) 1019 1020it ( ':setlocal in a normal buffer' , function () 1021command ( 'new' ) 1022-- :setlocal to -1. 1023command ( 'setlocal scrollback=-1' ) 1024eq ( - 1 , api . nvim_get_option_value ( 'scrollback' , {} )) 1025-- :setlocal to anything except -1. Currently, this just has no effect. 1026command ( 'setlocal scrollback=42' ) 1027eq ( 42 , api . nvim_get_option_value ( 'scrollback' , {} )) 1028end ) 1029 1030it ( ':set updates local value and global default' , function () 1031set_fake_shell () 1032command ( 'set scrollback=42' ) -- set global value 1033eq ( 42 , api . nvim_get_option_value ( 'scrollback' , {} )) 1034command ( 'terminal' ) 1035eq ( 42 , api . nvim_get_option_value ( 'scrollback' , {} )) -- inherits global default 1036command ( 'setlocal scrollback=99' ) 1037eq ( 99 , api . nvim_get_option_value ( 'scrollback' , {} )) 1038command ( 'set scrollback<' ) -- reset to global default 1039eq ( 42 , api . nvim_get_option_value ( 'scrollback' , {} )) 1040command ( 'setglobal scrollback=734' ) -- new global default 1041eq ( 42 , api . nvim_get_option_value ( 'scrollback' , {} )) -- local value did not change 1042command ( 'terminal' ) 1043eq ( 734 , api . nvim_get_option_value ( 'scrollback' , {} )) 1044end ) 1045end ) 1046 1047describe ( 'pending scrollback line handling' , function () 1048local screen 1049 1050before_each ( function () 1051clear () 1052screen =Screen . new ( 30 , 7 ) 1053end ) 1054 1055it ( "does not crash after setting 'number' #14891" , function () 1056exec_lua [[ 1057local api = vim.api 1058local buf = api.nvim_create_buf(true, true) 1059local chan = api.nvim_open_term(buf, {}) 1060vim.wo.number = true 1061api.nvim_chan_send(chan, ("a\n"):rep(11) .. "a") 1062api.nvim_win_set_buf(0, buf) 1063]] 1064screen : expect [[ 1065{8: 1 }^a | 1066{8: 2 }a | 1067{8: 3 }a | 1068{8: 4 }a | 1069{8: 5 }a | 1070{8: 6 }a | 1071| 1072]] 1073feed ( 'G' ) 1074screen : expect [[ 1075{8: 7 }a | 1076{8: 8 }a | 1077{8: 9 }a | 1078{8: 10 }a | 1079{8: 11 }a | 1080{8: 12 }^a | 1081| 1082]] 1083assert_alive () 1084end ) 1085 1086it ( 'does not crash after nvim_buf_call #14891' , function () 1087exec_lua ( 1088[[ 1089local bufnr = vim.api.nvim_create_buf(false, true) 1090local args = ... 1091vim.api.nvim_buf_call(bufnr, function() 1092vim.fn.jobstart(args, { term = true }) 1093end) 1094vim.api.nvim_win_set_buf(0, bufnr) 1095vim.cmd('startinsert') 1096]] , 1097is_os ( 'win' ) and { 'cmd.exe' , '/c' , 'for /L %I in (1,1,12) do @echo hi' } 1098or { 'printf' , ( 'hi\n' ): rep ( 12 ) } 1099) 1100screen : expect [[ 1101hi |*5 1102^[Process exited 0] | 1103{5:-- TERMINAL --} | 1104]] 1105assert_alive () 1106end ) 1107 1108it ( 'does not crash after deleting buffer lines' , function () 1109local buf =api . nvim_get_current_buf () 1110local chan =api . nvim_open_term ( buf , {} ) 1111api . nvim_chan_send ( chan , ( 'a\n' ): rep ( 11 ) .. 'a' ) 1112screen : expect ( [[ 1113^a | 1114a |*5 1115| 1116]] ) 1117api . nvim_set_option_value ( 'modifiable' , true , { buf =buf } ) 1118api . nvim_buf_set_lines ( buf , 0 , - 1 , true , {} ) 1119screen : expect ( [[ 1120^ | 1121{1:~ }|*5 1122| 1123]] ) 1124api . nvim_chan_send ( chan , ( '\nb' ): rep ( 11 ) .. '\n' ) 1125screen : expect ( [[ 1126b |*5 1127^ | 1128| 1129]] ) 1130assert_alive () 1131end ) 1132end ) 1133 1134describe ( 'scrollback is correct' , function () 1135local screen --- @type test.functional.ui.screen 1136local buf --- @type integer 1137local win --- @type integer 1138 1139before_each ( function () 1140clear () 1141screen =Screen . new ( 30 , 7 ) 1142screen : add_extra_attr_ids ( { 1143[ 100 ] ={ foreground =tonumber ( '0xe00000' ), fg_indexed =true } , 1144[ 101 ] ={ foreground =Screen . colors . White , background =Screen . colors . DarkGreen } , 1145[ 102 ] ={ 1146bold =true , 1147foreground =Screen . colors . White , 1148background =Screen . colors . DarkGreen , 1149} , 1150} ) 1151api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '\027[31mTEST\027[0m 0' } ) 1152feed ( 'yy99pG$<C-V>98kg<C-A>' ) 1153screen : expect ( [[ 1154{18:^[}[31mTEST{18:^[}[0m 0 | 1155{18:^[}[31mTEST{18:^[}[0m ^1 | 1156{18:^[}[31mTEST{18:^[}[0m 2 | 1157{18:^[}[31mTEST{18:^[}[0m 3 | 1158{18:^[}[31mTEST{18:^[}[0m 4 | 1159{18:^[}[31mTEST{18:^[}[0m 5 | 116099 lines changed | 1161]] ) 1162buf =api . nvim_get_current_buf () 1163win =api . nvim_get_current_win () 1164command ( 'set winwidth=10 | 10vnew' ) 1165end ) 1166 1167local function check_buffer_lines ( start , stop ) 1168local lines =api . nvim_buf_get_lines ( buf , 0 , - 1 , true ) 1169for i =start , stop do 1170eq (( 'TEST %d' ): format ( i ), lines [ i - start + 1 ]) 1171end 1172eq ( '' , lines [ # lines ]) 1173eq ( stop - start + 2 , # lines ) 1174end 1175 1176local function check_common () 1177feed ( '<C-W>lG' ) 1178screen : expect ( [[ 1179│{100:TEST} 96 | 1180{1:~ }│{100:TEST} 97 | 1181{1:~ }│{100:TEST} 98 | 1182{1:~ }│{100:TEST} 99 | 1183{1:~ }│^ | 1184{2:[No Name] }{102:[Scratch] [-] }| 118599 lines changed | 1186]] ) 1187end 1188 1189it ( 'with nvim_open_term() on buffer with fewer lines than scrollback' , function () 1190exec_lua ( function () 1191vim . api . nvim_open_term ( buf , {} ) 1192vim . api . nvim_win_set_cursor ( win , { 3 , 0 } ) 1193end ) 1194screen : expect ( [[ 1195^ │{100:TEST} 0 | 1196{1:~ }│{100:TEST} 1 | 1197{1:~ }│{100:TEST} 2 | 1198{1:~ }│{100:TEST} 3 | 1199{1:~ }│{100:TEST} 4 | 1200{3:[No Name] }{101:[Scratch] [-] }| 120199 lines changed | 1202]] ) 1203eq ( { 3 , 0 } , api . nvim_win_get_cursor ( win )) 1204check_buffer_lines ( 0 , 99 ) 1205check_common () 1206end ) 1207 1208it ( 'with nvim_open_term() on buffer with more lines than scrollback' , function () 1209api . nvim_set_option_value ( 'scrollback' , 10 , { buf =buf } ) 1210exec_lua ( function () 1211vim . api . nvim_open_term ( buf , {} ) 1212vim . api . nvim_win_set_cursor ( win , { 3 , 3 } ) 1213end ) 1214screen : expect ( [[ 1215^ │{100:TEST} 86 | 1216{1:~ }│{100:TEST} 87 | 1217{1:~ }│{100:TEST} 88 | 1218{1:~ }│{100:TEST} 89 | 1219{1:~ }│{100:TEST} 90 | 1220{3:[No Name] }{101:[Scratch] [-] }| 122199 lines changed | 1222]] ) 1223eq ( { 1 , 0 } , api . nvim_win_get_cursor ( win )) 1224check_buffer_lines ( 86 , 99 ) 1225check_common () 1226end ) 1227 1228describe ( 'when window height' , function () 1229before_each ( function () 1230feed ( '<C-W>lGV4kdgg' ) 1231screen : try_resize ( 30 , 20 ) 1232command ( 'botright 9new | wincmd p' ) 1233exec_lua ( function () 1234vim . g . chan =vim . api . nvim_open_term ( buf , {} ) 1235vim . cmd ( '$' ) 1236end ) 1237screen : expect ( [[ 1238│{100:TEST} 88 | 1239{1:~ }│{100:TEST} 89 | 1240{1:~ }│{100:TEST} 90 | 1241{1:~ }│{100:TEST} 91 | 1242{1:~ }│{100:TEST} 92 | 1243{1:~ }│{100:TEST} 93 | 1244{1:~ }│{100:TEST} 94 | 1245{1:~ }│^ | 1246{2:[No Name] }{102:[Scratch] [-] }| 1247| 1248{1:~ }|*8 1249{2:[No Name] }| 1250| 1251]] ) 1252check_buffer_lines ( 0 , 94 ) 1253end ) 1254 1255local send_cmd ='call chansend(g:chan, @")' 1256 1257describe ( 'increases in the same refresh cycle as outputting lines' , function () 1258--- @type string[][] 1259local perms =t . concat_tables ( 1260t . permutations ( { 'resize +2' , send_cmd } ), 1261t . permutations ( { 'resize +4' , 'resize -2' , send_cmd } ), 1262t . permutations ( { 'resize +6' , 'resize -4' , send_cmd } ) 1263) 1264assert ( # perms == 2 + 6 + 6 ) 1265local screen_final =[[ 1266│{100:TEST} 91 | 1267{1:~ }│{100:TEST} 92 | 1268{1:~ }│{100:TEST} 93 | 1269{1:~ }│{100:TEST} 94 | 1270{1:~ }│{100:TEST} 95 | 1271{1:~ }│{100:TEST} 96 | 1272{1:~ }│{100:TEST} 97 | 1273{1:~ }│{100:TEST} 98 | 1274{1:~ }│{100:TEST} 99 | 1275{1:~ }│^ | 1276{2:[No Name] }{102:[Scratch] [-] }| 1277| 1278{1:~ }|*6 1279{2:[No Name] }| 1280| 1281]] 1282 1283for i , perm in ipairs ( perms ) do 1284it (( 'permutation %d' ): format ( i ), function () 1285exec_lua ( function () 1286for _ , cmd in ipairs ( perm ) do 1287vim . cmd ( cmd ) 1288end 1289end ) 1290screen : expect ( screen_final ) 1291check_buffer_lines ( 0 , 99 ) 1292end ) 1293end 1294 1295describe ( 'with full scrollback,' , function () 1296before_each ( function () 1297api . nvim_set_option_value ( 'scrollback' , 6 , { buf =buf } ) 1298check_buffer_lines ( 82 , 94 ) 1299end ) 1300 1301it ( 'output first' , function () 1302command ( send_cmd .. ' | resize +2' ) 1303screen : expect ( screen_final ) 1304check_buffer_lines ( 87 , 99 ) 1305end ) 1306 1307it ( 'resize first' , function () 1308command ( 'resize +2 | ' .. send_cmd ) 1309screen : expect ( screen_final ) 1310check_buffer_lines ( 85 , 99 ) 1311end ) 1312end ) 1313end ) 1314 1315describe ( 'decreases in the same refresh cycle as outputting lines' , function () 1316--- @type string[][] 1317local perms =t . concat_tables ( 1318t . permutations ( { 'resize -2' , send_cmd } ), 1319t . permutations ( { 'resize -4' , 'resize +2' , send_cmd } ), 1320t . permutations ( { 'resize -6' , 'resize +4' , send_cmd } ) 1321) 1322assert ( # perms == 2 + 6 + 6 ) 1323local screen_final =[[ 1324│{100:TEST} 95 | 1325{1:~ }│{100:TEST} 96 | 1326{1:~ }│{100:TEST} 97 | 1327{1:~ }│{100:TEST} 98 | 1328{1:~ }│{100:TEST} 99 | 1329{1:~ }│^ | 1330{2:[No Name] }{102:[Scratch] [-] }| 1331| 1332{1:~ }|*10 1333{2:[No Name] }| 1334| 1335]] 1336 1337for i , perm in ipairs ( perms ) do 1338it (( 'permutation %d' ): format ( i ), function () 1339exec_lua ( function () 1340for _ , cmd in ipairs ( perm ) do 1341vim . cmd ( cmd ) 1342end 1343end ) 1344screen : expect ( screen_final ) 1345check_buffer_lines ( 0 , 99 ) 1346end ) 1347end 1348end ) 1349 1350describe ( "decreases by more than 'scrollback'" , function () 1351before_each ( function () 1352api . nvim_set_option_value ( 'scrollback' , 4 , { buf =buf } ) 1353check_buffer_lines ( 84 , 94 ) 1354end ) 1355 1356local perms ={ 1357{ send_cmd , 'resize -6' } , 1358{ 'resize -6' , send_cmd } , 1359{ send_cmd , 'resize +6' , 'resize -12' } , 1360{ 'resize +6' , send_cmd , 'resize -12' } , 1361{ 'resize +6' , 'resize -12' , send_cmd } , 1362} 1363local screen_final =[[ 1364│{100:TEST} 99 | 1365{1:~ }│^ | 1366{2:[No Name] }{102:[Scratch] [-] }| 1367| 1368{1:~ }|*14 1369{2:[No Name] }| 1370| 1371]] 1372 1373for i , perm in ipairs ( perms ) do 1374it (( 'permutation %d' ): format ( i ), function () 1375exec_lua ( function () 1376for _ , cmd in ipairs ( perm ) do 1377vim . cmd ( cmd ) 1378end 1379end ) 1380screen : expect ( screen_final ) 1381check_buffer_lines ( 95 , 99 ) 1382end ) 1383end 1384end ) 1385end ) 1386end )