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' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear , feed , insert =n . clear , n . feed , n . insert 7local command =n . command 8local feed_command =n . feed_command 9local eq =t . eq 10local eval =n . eval 11local fn =n . fn 12local testprg =n . testprg 13 14describe ( 'search highlighting' , function () 15local screen 16 17before_each ( function () 18clear () 19screen =Screen . new ( 40 , 7 ) 20screen : add_extra_attr_ids ( { 21[ 100 ] ={ foreground =Screen . colors . Gray100 , background =Screen . colors . Grey0 } , 22[ 101 ] ={ foreground =Screen . colors . White , background =Screen . colors . DarkGreen } , 23[ 102 ] ={ background =Screen . colors . WebGreen , bold =true } , 24[ 103 ] ={ background =Screen . colors . Magenta1 , italic =true } , 25[ 104 ] ={ background =Screen . colors . Yellow1 , bold =true } , 26} ) 27end ) 28 29it ( 'is disabled by ":set nohlsearch"' , function () 30feed_command ( 'set nohlsearch' ) 31insert ( 'some text\nmore text' ) 32feed ( 'gg/text<cr>' ) 33screen : expect ( [[ 34some ^text | 35more text | 36{1:~ }|*4 37/text | 38]] ) 39end ) 40 41it ( 'is disabled in folded text' , function () 42insert ( 'some text\nmore text' ) 43feed_command ( '1,2fold' ) 44feed ( 'gg/text' ) 45screen : expect { 46grid =[[ 47{13:+-- 2 lines: some text·················}| 48{1:~ }|*5 49/text^ | 50]] , 51win_viewport ={ 52[ 2 ] ={ 53win =1000 , 54topline =0 , 55botline =3 , 56curline =0 , 57curcol =9 , 58linecount =2 , 59sum_scroll_delta =0 , 60} , 61} , 62} 63end ) 64 65local function test_search_hl () 66insert ( [[ 67some text 68more textstuff 69stupidtexttextstuff 70a text word 71]] ) 72 73-- 'hlsearch' is enabled by default. #2859 74feed ( 'gg/text<cr>' ) 75screen : expect ( [[ 76some {100:^text} | 77more {100:text}stuff | 78stupid{100:texttext}stuff | 79a {100:text} word | 80| 81{101:~ }| 82/text | 83]] ) 84 85-- overlapping matches not allowed 86feed ( '3nx' ) 87screen : expect ( [[ 88some {100:text} | 89more {100:text}stuff | 90stupid{100:text}^extstuff | 91a {100:text} word | 92| 93{101:~ }| 94/text | 95]] ) 96 97feed ( 'ggn*' ) -- search for entire word 98screen : expect ( [[ 99some {100:text} | 100more textstuff | 101stupidtextextstuff | 102a {100:^text} word | 103| 104{101:~ }| 105/\<text\> | 106]] ) 107 108feed_command ( 'nohlsearch' ) 109screen : expect ( [[ 110some text | 111more textstuff | 112stupidtextextstuff | 113a ^text word | 114| 115{101:~ }| 116:nohlsearch | 117]] ) 118end 119 120it ( "works when 'winhighlight' is not set" , function () 121screen : add_extra_attr_ids ( { 122[ 100 ] ={ background =Screen . colors . Yellow1 } , 123[ 101 ] ={ foreground =Screen . colors . Blue1 , bold =true } , 124} ) 125test_search_hl () 126end ) 127 128it ( "works when 'winhighlight' doesn't change Search highlight" , function () 129command ( 'setlocal winhl=NonText:Underlined' ) 130screen : add_extra_attr_ids ( { 131[ 100 ] ={ background =Screen . colors . Yellow } , 132[ 101 ] ={ foreground =Screen . colors . SlateBlue , underline =true } , 133} ) 134test_search_hl () 135end ) 136 137it ( "works when 'winhighlight' changes Search highlight" , function () 138command ( 'setlocal winhl=Search:Underlined' ) 139screen : add_extra_attr_ids ( { 140[ 100 ] ={ foreground =Screen . colors . SlateBlue , underline =true } , 141[ 101 ] ={ foreground =Screen . colors . Blue1 , bold =true } , 142} ) 143test_search_hl () 144end ) 145 146describe ( 'CurSearch highlight' , function () 147before_each ( function () 148command ( 'highlight CurSearch guibg=Black guifg=White' ) 149end ) 150 151it ( 'works for match under cursor' , function () 152insert ( [[ 153There is no way that a bee should be 154able to fly. Its wings are too small 155to get its fat little body off the 156ground. The bee, of course, flies 157anyway because bees don't care what 158humans think is impossible.]] ) 159 160feed ( '/bee<CR>' ) 161screen : expect ( [[ 162There is no way that a {100:^bee} should be | 163able to fly. Its wings are too small | 164to get its fat little body off the | 165ground. The {10:bee}, of course, flies | 166anyway because {10:bee}s don't care what | 167humans think is impossible. | 168{19:search hit BOTTOM, continuing at TOP} | 169]] ) 170 171feed ( 'nn' ) 172screen : expect ( [[ 173There is no way that a {10:bee} should be | 174able to fly. Its wings are too small | 175to get its fat little body off the | 176ground. The {10:bee}, of course, flies | 177anyway because {100:^bee}s don't care what | 178humans think is impossible. | 179/bee | 180]] ) 181 182feed ( 'N' ) 183screen : expect ( [[ 184There is no way that a {10:bee} should be | 185able to fly. Its wings are too small | 186to get its fat little body off the | 187ground. The {100:^bee}, of course, flies | 188anyway because {10:bee}s don't care what | 189humans think is impossible. | 190?bee | 191]] ) 192end ) 193 194-- oldtest: Test_hlsearch_cursearch() 195it ( 'works for multiline match, no duplicate highlight' , function () 196command ( [[call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])]] ) 197feed ( 'gg/foo<CR>' ) 198screen : expect ( [[ 199one | 200{100:^foo} | 201bar | 202baz | 203{10:foo} the {10:foo} and {10:foo} | 204bar | 205/foo | 206]] ) 207feed ( 'n' ) 208screen : expect ( [[ 209one | 210{10:foo} | 211bar | 212baz | 213{100:^foo} the {10:foo} and {10:foo} | 214bar | 215/foo | 216]] ) 217feed ( 'n' ) 218screen : expect ( [[ 219one | 220{10:foo} | 221bar | 222baz | 223{10:foo} the {100:^foo} and {10:foo} | 224bar | 225/foo | 226]] ) 227feed ( 'n' ) 228screen : expect ( [[ 229one | 230{10:foo} | 231bar | 232baz | 233{10:foo} the {10:foo} and {100:^foo} | 234bar | 235/foo | 236]] ) 237command ( [[call setline(5, 'foo')]] ) 238feed ( '0?<CR>' ) 239screen : expect ( [[ 240one | 241{100:^foo} | 242bar | 243baz | 244{10:foo} | 245bar | 246?foo | 247]] ) 248feed ( 'gg/foo\\nbar<CR>' ) 249screen : expect ( [[ 250one | 251{100:^foo } | 252{100:bar} | 253baz | 254{10:foo } | 255{10:bar} | 256/foo\nbar | 257]] ) 258command ( [[call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])]] ) 259feed ( 'gg/efg\\nhij<CR>' ) 260screen : expect ( [[ 261--- | 262abcd{100:^efg } | 263{100:hij}kl | 264--- | 265abcd{10:efg } | 266{10:hij}kl | 267/efg\nhij | 268]] ) 269feed ( 'n' ) 270screen : expect ( [[ 271--- | 272abcd{10:efg } | 273{10:hij}kl | 274--- | 275abcd{100:^efg } | 276{100:hij}kl | 277/efg\nhij | 278]] ) 279 280-- check clearing CurSearch when using it for another match 281feed ( 'G?^abcd<CR>Y' ) 282screen : expect ( [[ 283--- | 284{10:abcd}efg | 285hijkl | 286--- | 287{100:^abcd}efg | 288hijkl | 289?^abcd | 290]] ) 291feed ( 'kkP' ) 292screen : expect ( [[ 293--- | 294{10:abcd}efg | 295{100:^abcd}efg | 296hijkl | 297--- | 298{10:abcd}efg | 299?^abcd | 300]] ) 301end ) 302end ) 303 304it ( 'highlights after EOL' , function () 305insert ( '\n\n\n\n\n\n' ) 306 307feed ( 'gg/^<cr>' ) 308screen : expect ( [[ 309{10: } | 310{10:^ } | 311{10: } |*4 312/^ | 313]] ) 314 315-- Test that highlights are preserved after moving the cursor. 316feed ( 'j' ) 317screen : expect ( [[ 318{10: } |*2 319{10:^ } | 320{10: } |*3 321/^ | 322]] ) 323 324-- Repeat the test in rightleft mode. 325command ( 'nohlsearch' ) 326command ( 'set rightleft' ) 327feed ( 'gg/^<cr>' ) 328 329screen : expect ( [[ 330{10: }| 331{10:^ }| 332{10: }|*4 333^/ | 334]] ) 335 336feed ( 'j' ) 337screen : expect ( [[ 338{10: }|*2 339{10:^ }| 340{10: }|*3 341^/ | 342]] ) 343end ) 344 345it ( 'is preserved during :terminal activity' , function () 346feed (( ':terminal "%s" REP 5000 foo<cr>' ): format ( testprg ( 'shell-test' ))) 347feed ( ':file term<CR>' ) 348screen : expect ( [[ 349^0: foo | 3501: foo | 3512: foo | 3523: foo | 3534: foo | 3545: foo | 355:file term | 356]] ) 357 358feed ( 'G' ) -- Follow :terminal output. 359feed ( ':vnew<CR>' ) 360insert ( [[ 361foo bar baz 362bar baz foo 363bar foo baz]] ) 364feed ( '/foo' ) 365screen : expect ( [[ 366{2:foo} bar baz │{MATCH:%d+}: {10:foo}{MATCH:%s+}| 367bar baz {10:foo} │{MATCH:%d+}: {10:foo}{MATCH:%s+}| 368bar {10:foo} baz │{MATCH:%d+}: {10:foo}{MATCH:%s+}| 369{1:~ }│{MATCH:.*}|*2 370{3:[No Name] [+] }{101:term [-] }| 371/foo^ | 372]] ) 373end ) 374 375it ( 'works with incsearch' , function () 376command ( 'set hlsearch' ) 377command ( 'set incsearch' ) 378command ( 'set laststatus=0' ) 379insert ( [[ 380the first line 381in a little file]] ) 382command ( 'vsplit' ) 383feed ( 'gg/li' ) 384screen : expect ( [[ 385the first {2:li}ne │the first {10:li}ne | 386in a {10:li}ttle file │in a {10:li}ttle file | 387{1:~ }│{1:~ }|*4 388/li^ | 389]] ) 390 391-- check that consecutive matches are caught by C-g/C-t 392feed ( '<C-g>' ) 393screen : expect ( [[ 394the first {10:li}ne │the first {10:li}ne | 395in a {2:li}ttle file │in a {10:li}ttle file | 396{1:~ }│{1:~ }|*4 397/li^ | 398]] ) 399 400feed ( '<C-t>' ) 401screen : expect ( [[ 402the first {2:li}ne │the first {10:li}ne | 403in a {10:li}ttle file │in a {10:li}ttle file | 404{1:~ }│{1:~ }|*4 405/li^ | 406]] ) 407 408feed ( 't' ) 409screen : expect ( [[ 410the first line │the first line | 411in a {2:lit}tle file │in a {10:lit}tle file | 412{1:~ }│{1:~ }|*4 413/lit^ | 414]] ) 415 416feed ( '<cr>' ) 417screen : expect ( [[ 418the first line │the first line | 419in a {10:^lit}tle file │in a {10:lit}tle file | 420{1:~ }│{1:~ }|*4 421/lit | 422]] ) 423 424feed ( '/fir' ) 425screen : expect ( [[ 426the {2:fir}st line │the {10:fir}st line | 427in a little file │in a little file | 428{1:~ }│{1:~ }|*4 429/fir^ | 430]] ) 431 432-- incsearch have priority over hlsearch 433feed ( '<esc>/ttle' ) 434screen : expect ( [[ 435the first line │the first line | 436in a li{2:ttle} file │in a li{10:ttle} file | 437{1:~ }│{1:~ }|*4 438/ttle^ | 439]] ) 440 441-- cancelling search resets to the old search term 442feed ( '<esc>' ) 443screen : expect ( [[ 444the first line │the first line | 445in a {10:^lit}tle file │in a {10:lit}tle file | 446{1:~ }│{1:~ }|*4 447| 448]] ) 449eq ( 'lit' , eval ( '@/' )) 450 451-- cancelling inc search restores the hl state 452feed ( ':noh<cr>' ) 453screen : expect ( [[ 454the first line │the first line | 455in a ^little file │in a little file | 456{1:~ }│{1:~ }|*4 457:noh | 458]] ) 459 460feed ( '/first' ) 461screen : expect ( [[ 462the {2:first} line │the {10:first} line | 463in a little file │in a little file | 464{1:~ }│{1:~ }|*4 465/first^ | 466]] ) 467feed ( '<esc>' ) 468screen : expect ( [[ 469the first line │the first line | 470in a ^little file │in a little file | 471{1:~ }│{1:~ }|*4 472| 473]] ) 474 475-- test that pressing C-g in an empty command line does not move the cursor 476feed ( 'gg0' ) 477command ( [[let @/ = 'i']] ) 478-- moves to next match of previous search pattern, just like /<cr> 479feed ( '/<c-g><cr>' ) 480eq ( { 0 , 1 , 6 , 0 } , fn . getpos ( '.' )) 481-- moves to next match of previous search pattern, just like /<cr> 482feed ( '/<cr>' ) 483eq ( { 0 , 1 , 12 , 0 } , fn . getpos ( '.' )) 484-- moves to next match of previous search pattern, just like /<cr> 485feed ( '/<c-t><cr>' ) 486eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( '.' )) 487 488-- 8.0.1304, test that C-g and C-t works with incsearch and empty pattern 489feed ( '<esc>/fi<CR>' ) 490screen : expect ( [[ 491the {10:fi}rst line │the {10:fi}rst line | 492in a little {10:^fi}le │in a little {10:fi}le | 493{1:~ }│{1:~ }|*4 494/fi | 495]] ) 496feed ( '//' ) 497screen : expect ( [[ 498the {2:fi}rst line │the {10:fi}rst line | 499in a little {10:fi}le │in a little {10:fi}le | 500{1:~ }│{1:~ }|*4 501//^ | 502]] ) 503feed ( '<C-g>' ) 504screen : expect ( [[ 505the {10:fi}rst line │the {10:fi}rst line | 506in a little {2:fi}le │in a little {10:fi}le | 507{1:~ }│{1:~ }|*4 508//^ | 509]] ) 510feed ( '<Esc>' ) 511 512-- incsearch works after c_CTRL-R_CTRL-R 513command ( 'let @" = "file"' ) 514feed ( '/<C-R><C-R>"' ) 515screen : expect ( [[ 516the first line │the first line | 517in a little {2:file} │in a little {10:file} | 518{1:~ }│{1:~ }|*4 519/file^ | 520]] ) 521feed ( '<Esc>' ) 522 523command ( 'set rtp^=test/functional/fixtures' ) 524-- incsearch works after c_CTRL-R inserts clipboard register 525 526command ( 'let @* = "first"' ) 527feed ( '/<C-R>*' ) 528screen : expect ( [[ 529the {2:first} line │the {10:first} line | 530in a little file │in a little file | 531{1:~ }│{1:~ }|*4 532/first^ | 533]] ) 534feed ( '<Esc>' ) 535 536command ( 'let @+ = "little"' ) 537feed ( '/<C-R>+' ) 538screen : expect ( [[ 539the first line │the first line | 540in a {2:little} file │in a {10:little} file | 541{1:~ }│{1:~ }|*4 542/little^ | 543]] ) 544feed ( '<Esc>' ) 545end ) 546 547it ( 'works with incsearch and offset' , function () 548feed_command ( 'set hlsearch' ) 549feed_command ( 'set incsearch' ) 550insert ( [[ 551not the match you're looking for 552the match is here]] ) 553 554feed ( 'gg/mat/e' ) 555screen : expect ( [[ 556not the {2:mat}ch you're looking for | 557the {10:mat}ch is here | 558{1:~ }|*4 559/mat/e^ | 560]] ) 561 562-- Search with count and /e offset fixed in Vim patch 7.4.532. 563feed ( '<esc>2/mat/e' ) 564screen : expect ( [[ 565not the {10:mat}ch you're looking for | 566the {2:mat}ch is here | 567{1:~ }|*4 568/mat/e^ | 569]] ) 570 571feed ( '<cr>' ) 572screen : expect ( [[ 573not the {10:mat}ch you're looking for | 574the {10:ma^t}ch is here | 575{1:~ }|*4 576/mat/e | 577]] ) 578 579command ( [[call setline(1, ['blah blah blah']) | 2,$delete_]] ) 580feed ( 'gg0/blah/e<C-g><cr>' ) 581eq ( { 0 , 1 , 9 , 0 } , fn . getpos ( '.' )) 582 583feed ( 'gg0/blah/e<C-g><C-g><cr>' ) 584eq ( { 0 , 1 , 14 , 0 } , fn . getpos ( '.' )) 585 586feed ( 'gg0/blah/e<C-g><C-g><C-t><cr>' ) 587eq ( { 0 , 1 , 9 , 0 } , fn . getpos ( '.' )) 588end ) 589 590it ( 'works with multiline regexps' , function () 591feed_command ( 'set hlsearch' ) 592feed ( '4oa repeated line<esc>' ) 593feed ( '/line\\na<cr>' ) 594screen : expect ( [[ 595| 596a repeated {10:^line } | 597{10:a} repeated {10:line } |*2 598{10:a} repeated line | 599{1:~ }| 600{19:search hit BOTTOM, continuing at TOP} | 601]] ) 602 603-- it redraws rows above the changed one 604feed ( '4Grb' ) 605screen : expect ( [[ 606| 607a repeated {10:line } | 608{10:a} repeated line | 609^b repeated {10:line } | 610{10:a} repeated line | 611{1:~ }| 612{19:search hit BOTTOM, continuing at TOP} | 613]] ) 614end ) 615 616it ( 'works with matchadd and syntax' , function () 617feed_command ( 'set hlsearch' ) 618insert [[ 619very special text 620]] 621feed_command ( 'syntax on' ) 622feed_command ( 'highlight MyGroup guibg=Green gui=bold' ) 623feed_command ( 'highlight MyGroup2 guibg=Magenta gui=italic' ) 624feed_command ( "call matchadd('MyGroup', 'special')" ) 625feed_command ( "call matchadd('MyGroup2', 'text', 0)" ) 626 627-- searchhl and matchadd matches are exclusive, only the highest priority 628-- is used (and matches with lower priorities are not combined) 629feed_command ( '/ial te' ) 630screen : expect ( [[ 631very {102:spec^ial}{10: te}{103:xt} | 632| 633{1:~ }|*4 634{19:search hit BOTTOM, continuing at TOP} | 635]] ) 636 637-- check highlights work also in folds 638feed ( 'zf4j' ) 639screen : expect ( [[ 640{13:^+-- 2 lines: very special text·········}| 641{1:~ }|*5 642{19:search hit BOTTOM, continuing at TOP} | 643]] ) 644command ( '%foldopen' ) 645screen : expect ( [[ 646very {102:spec^ial}{10: te}{103:xt} | 647| 648{1:~ }|*4 649{19:search hit BOTTOM, continuing at TOP} | 650]] ) 651 652feed_command ( 'call clearmatches()' ) 653screen : expect ( [[ 654very spec{10:^ial te}xt | 655| 656{1:~ }|*4 657:call clearmatches() | 658]] ) 659 660-- searchhl has priority over syntax, but in this case 661-- nonconflicting attributes are combined 662feed_command ( 'syntax keyword MyGroup special' ) 663screen : expect ( [[ 664very {102:spec}{104:^ial}{10: te}xt | 665| 666{1:~ }|*4 667:syntax keyword MyGroup special | 668]] ) 669end ) 670 671it ( 'highlights entire pattern on :%g@a/b' , function () 672command ( 'set inccommand=nosplit' ) 673feed ( 'ia/b/c<Esc>' ) 674feed ( ':%g@a/b' ) 675screen : expect ( [[ 676{2:a/b}/c | 677{1:~ }|*5 678:%g@a/b^ | 679]] ) 680end ) 681 682it ( 'incsearch is still visible after :redraw from K_EVENT' , function () 683fn . setline ( 1 , { 'foo' , 'bar' } ) 684feed ( '/foo<CR>/bar' ) 685screen : expect ( [[ 686foo | 687{2:bar} | 688{1:~ }|*4 689/bar^ | 690]] ) 691command ( 'redraw!' ) 692-- There is an intermediate state where :redraw! removes 'incsearch' highlight. 693screen : expect_unchanged ( true ) 694end ) 695 696it ( 'no ml_get error with incsearch and <Cmd> mapping that opens window' , function () 697command ( 'cnoremap <F3> <Cmd>vnew<Bar>redraw!<CR>' ) 698fn . setline ( 1 , { 'foo' , 'bar' , 'baz' } ) 699feed ( 'G/z' ) 700screen : expect ( [[ 701foo | 702bar | 703ba{2:z} | 704{1:~ }|*3 705/z^ | 706]] ) 707feed ( '<F3>' ) 708screen : expect ( [[ 709│foo | 710{1:~ }│bar | 711{1:~ }│baz | 712{1:~ }│{1:~ }|*2 713{3:[No Name] }{2:[No Name] [+] }| 714/z^ | 715]] ) 716eq ( '' , n . api . nvim_get_vvar ( 'errmsg' )) 717feed ( '<C-G>' ) 718screen : expect_unchanged ( true ) 719eq ( '' , n . api . nvim_get_vvar ( 'errmsg' )) 720end ) 721 722it ( 'highlight is not after redraw during substitute confirm prompt' , function () 723fn . setline ( 1 , { 'foo' , 'bar' } ) 724command ( 'set nohlsearch' ) 725feed ( ':%s/bar/baz/c<CR>' ) 726screen : try_resize ( screen . _width , screen . _height - 1 ) 727screen : expect ( [[ 728foo | 729{2:bar} | 730{1:~ }| 731{3: }| 732{6:replace with baz? (y)es/(n)o/(a)ll/(q)ui}| 733{6:t/(l)ast/scroll up(^E)/down(^Y)}^ | 734]] ) 735end ) 736end )