robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
bf3d4210c3
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local os =require ( 'os' ) 5 6local describe , it , before_each =t . describe , t . it , t . before_each 7local after_each , finally =t . after_each , t . finally 8local clear , command , api , fn =n . clear , n . command , n . api , n . fn 9local eq , pcall_err , write_file =t . eq , t . pcall_err , t . write_file 10local exec , feed =n . exec , n . feed 11 12--- Joins lines "1".."count", with `overrides` (1-based row → text) substituted. 13--- @param count integer 14--- @param overrides table<integer, string> 15local function numbered_lines ( count , overrides ) 16local lines ={} --- @type string[] 17for i =1 , count do 18lines [ i ] =overrides [ i ] or tostring ( i ) 19end 20return table . concat ( lines , '\n' ) 21end 22 23describe ( "'previewpopup'" , function () 24before_each ( function () 25clear () 26end ) 27 28local function with_ext_multigrid ( multigrid ) 29local screen ---@type test.functional.ui.screen 30before_each ( function () 31screen =Screen . new ( 40 , 7 , { ext_multigrid =multigrid } ) 32screen : add_extra_attr_ids ( { 33[ 100 ] ={ 34foreground =Screen . colors . Magenta1 , 35background =Screen . colors . Plum1 , 36bold =true , 37} , 38} ) 39end ) 40 41it ( 'validation' , function () 42local err =pcall_err ( n . exec_capture , 'set previewpopup=height:yes' ) 43eq ( 44"nvim_exec2(), line 1: Vim(set):E474: 'height' requires a number: previewpopup=height:yes" , 45err 46) 47 48err =pcall_err ( n . exec_capture , 'set previewpopup=width:yes' ) 49eq ( 50"nvim_exec2(), line 1: Vim(set):E474: 'width' requires a number: previewpopup=width:yes" , 51err 52) 53 54err =pcall_err ( n . exec_capture , 'set previewpopup=width:20,height;10' ) 55eq ( 56"nvim_exec2(), line 1: Vim(set):E474: Unknown item 'height;10': previewpopup=width:20,height;10" , 57err 58) 59 60err =pcall_err ( n . exec_capture , 'set previewpopup=border:fancy' ) 61eq ( 62'nvim_exec2(), line 1: Vim(set):E474: ' 63.. "'border' must be one of: double, single, shadow, rounded, solid, bold, none: " 64.. 'previewpopup=border:fancy' , 65err 66) 67 68-- height/width must be >= 1 (semantic check, not the schema). 69err =pcall_err ( n . exec_capture , 'set previewpopup=height:0' ) 70eq ( 'nvim_exec2(), line 1: Vim(set):E474: Invalid argument: previewpopup=height:0' , err ) 71end ) 72 73-- oldtest: Test_previewpopup 74it ( 'with tags and search' , function () 75finally ( function () 76os . remove ( 'Xtags' ) 77os . remove ( 'Xtagfile' ) 78os . remove ( 'Xheader.h' ) 79end ) 80screen : try_resize ( 40 , 20 ) 81write_file ( 82'Xtags' , 83t . dedent ( [[ 84!_TAG_FILE_ENCODING utf-8 // 85another Xtagfile /^this is another 86theword Xtagfile /^theword 87]] ) 88) 89write_file ( 90'Xtagfile' , 91numbered_lines ( 40 , { [ 21 ] ='theword is here' , [ 28 ] ='this is another place' } ) 92) 93write_file ( 'Xheader.h' , numbered_lines ( 20 , { [ 11 ] ='searched word is here' } )) 94command ( 'set tags=Xtags' ) 95api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 96'one' , 97'#include "Xheader.h"' , 98'three' , 99'four' , 100'five' , 101'six' , 102'seven' , 103'find theword somewhere' , 104'nine' , 105'this is another word' , 106'very long line where the word is also another' , 107} ) 108command ( 'set previewpopup=height:4,width:40' ) 109command ( 'set winborder=single' ) 110command ( 'set path=.' ) 111 112feed ( '/theword<CR><C-W>}' ) 113if multigrid then 114screen : expect ( { 115grid =[[ 116## grid 1 117[2:----------------------------------------]|*19 118[3:----------------------------------------]| 119## grid 2 120one | 121#include "Xheader.h" | 122three | 123four | 124five | 125six | 126seven | 127find {10:^theword} somewhere | 128nine | 129this is another word | 130very long line where the word is also an| 131other | 132{1:~ }|*7 133## grid 3 134/theword | 135## grid 4 136{4:┌────────────}{100:Xtagfile}{4:────────────┐}| 137{4:│20 │}| 138{4:│}{10:theword}{4: is here │}| 139{4:│22 │}| 140{4:│23 │}| 141{4:└────────────────────────────────┘}| 142]] , 143win_pos ={ 144[ 2 ] ={ height =19 , startcol =0 , startrow =0 , width =40 , win =1000 } , 145} , 146float_pos ={ 147[ 4 ] ={ 1001 , 'NW' , 1 , 8 , 6 , true , 50 , 1 , 8 , 6 } , 148} , 149} ) 150else 151screen : expect ( [[ 152one | 153#include "Xheader.h" | 154three | 155four | 156five | 157six | 158seven | 159find {10:^theword} somewhere | 160nine {4:┌────────────}{100:Xtagfile}{4:────────────┐}| 161this i{4:│20 │}| 162very l{4:│}{10:theword}{4: is here │}| 163other {4:│22 │}| 164{1:~ }{4:│23 │}| 165{1:~ }{4:└────────────────────────────────┘}| 166{1:~ }|*5 167/theword | 168]] ) 169end 170 171command ( 'pclose' ) 172if multigrid then 173screen : expect ( { 174grid =[[ 175## grid 1 176[2:----------------------------------------]|*19 177[3:----------------------------------------]| 178## grid 2 179one | 180#include "Xheader.h" | 181three | 182four | 183five | 184six | 185seven | 186find {10:^theword} somewhere | 187nine | 188this is another word | 189very long line where the word is also an| 190other | 191{1:~ }|*7 192## grid 3 193/theword | 194]] , 195win_pos ={ 196[ 2 ] ={ height =19 , startcol =0 , startrow =0 , width =40 , win =1000 } , 197} , 198} ) 199else 200screen : expect ( [[ 201one | 202#include "Xheader.h" | 203three | 204four | 205five | 206six | 207seven | 208find {10:^theword} somewhere | 209nine | 210this is another word | 211very long line where the word is also an| 212other | 213{1:~ }|*7 214/theword | 215]] ) 216end 217 218command ( [[set include=^\s*#\s*include]] ) 219command ( 'pedit +/theword Xtagfile' ) 220if multigrid then 221screen : expect ( { 222grid =[[ 223## grid 1 224[2:----------------------------------------]|*19 225[3:----------------------------------------]| 226## grid 2 227one | 228#include "Xheader.h" | 229three | 230four | 231five | 232six | 233seven | 234find {10:^theword} somewhere | 235nine | 236this is another word | 237very long line where the word is also an| 238other | 239{1:~ }|*7 240## grid 3 241/theword | 242## grid 5 243{4:┌────────────}{100:Xtagfile}{4:────────────┐}| 244{4:│20 │}| 245{4:│}{10:theword}{4: is here │}| 246{4:│22 │}| 247{4:│23 │}| 248{4:└────────────────────────────────┘}| 249]] , 250win_pos ={ 251[ 2 ] ={ height =19 , startcol =0 , startrow =0 , width =40 , win =1000 } , 252} , 253float_pos ={ 254[ 5 ] ={ 1002 , 'NW' , 1 , 8 , 6 , true , 50 , 1 , 8 , 6 } , 255} , 256} ) 257else 258screen : expect ( [[ 259one | 260#include "Xheader.h" | 261three | 262four | 263five | 264six | 265seven | 266find {10:^theword} somewhere | 267nine {4:┌────────────}{100:Xtagfile}{4:────────────┐}| 268this i{4:│20 │}| 269very l{4:│}{10:theword}{4: is here │}| 270other {4:│22 │}| 271{1:~ }{4:│23 │}| 272{1:~ }{4:└────────────────────────────────┘}| 273{1:~ }|*5 274/theword | 275]] ) 276end 277 278command ( 'pclose | psearch searched' ) 279if multigrid then 280screen : expect ( { 281grid =[[ 282## grid 1 283[2:----------------------------------------]|*19 284[3:----------------------------------------]| 285## grid 2 286one | 287#include "Xheader.h" | 288three | 289four | 290five | 291six | 292seven | 293find {10:^theword} somewhere | 294nine | 295this is another word | 296very long line where the word is also an| 297other | 298{1:~ }|*7 299## grid 3 300/theword | 301## grid 6 302{4:┌───────────}{100:Xheader.h}{4:────────────┐}| 303{4:│10 │}| 304{4:│searched word is here │}| 305{4:│12 │}| 306{4:│13 │}| 307{4:└────────────────────────────────┘}| 308]] , 309win_pos ={ 310[ 2 ] ={ height =19 , startcol =0 , startrow =0 , width =40 , win =1000 } , 311} , 312float_pos ={ 313[ 6 ] ={ 1003 , 'NW' , 1 , 8 , 6 , true , 50 , 1 , 8 , 6 } , 314} , 315} ) 316else 317screen : expect ( [[ 318one | 319#include "Xheader.h" | 320three | 321four | 322five | 323six | 324seven | 325find {10:^theword} somewhere | 326nine {4:┌───────────}{100:Xheader.h}{4:────────────┐}| 327this i{4:│10 │}| 328very l{4:│searched word is here │}| 329other {4:│12 │}| 330{1:~ }{4:│13 │}| 331{1:~ }{4:└────────────────────────────────┘}| 332{1:~ }|*5 333/theword | 334]] ) 335end 336end ) 337 338describe ( 'with pum' , function () 339before_each ( function () 340write_file ( 341'XpreviewText.vim' , 342t . dedent ( [[ 343let a = 3 344let b = 1 345echo a 346echo b 347call system('echo hello') 348" the end 349]] ) 350) 351end ) 352after_each ( function () 353os . remove ( 'XpreviewText.vim' ) 354end ) 355local expect_screen =[[ 356one other^ | 357t{4:le}{12: other }{4: }| 358t{4:le once }| 359o{4:ec only }| 360o{4:ec off }| 361o{4:ca one hello') }| 362o{4:" the end }| 363{1:~ }|*2 364{5:-- }{6:match 1 of 5} | 365]] 366 367-- oldtest: Test_previewpopup_pum_pedit 368it ( 'pum pedit' , function () 369screen : try_resize ( 30 , 10 ) 370exec ( [[ 371call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off']) 372set previewpopup=height:6,width:40 373]] ) 374command ( 'pedit XpreviewText.vim' ) 375feed ( 'A o<C-N>' ) 376if not multigrid then 377screen : expect ( expect_screen ) 378end 379end ) 380 381-- oldtest: Test_previewpopup_pum_pbuffer 382it ( 'pum pbuffer' , function () 383screen : try_resize ( 30 , 10 ) 384exec ( [[ 385call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off']) 386set previewpopup=height:6,width:40 387badd XpreviewText.vim 388]] ) 389command ( fn . bufnr ( '$' ) .. 'pbuffer' ) 390feed ( 'A o<C-N>' ) 391if not multigrid then 392screen : expect ( expect_screen ) 393end 394end ) 395end ) 396 397it ( ':pedit and border overrides' , function () 398command ( 'call writefile(["bar"], "foo", "a")' ) 399finally ( function () 400os . remove ( 'foo' ) 401end ) 402command ( 'set winborder=single | set previewpopup=height:2,width:5 | pedit foo' ) 403if multigrid then 404screen : expect ( { 405grid =[[ 406## grid 1 407[2:----------------------------------------]|*6 408[3:----------------------------------------]| 409## grid 2 410^ | 411{1:~ }|*5 412## grid 3 413| 414## grid 4 415{4:┌─}{100:foo}{4:─┐}| 416{4:│bar │}| 417{4:│ │}| 418{4:└─────┘}| 419]] , 420win_pos ={ 421[ 2 ] ={ 422height =6 , 423startcol =0 , 424startrow =0 , 425width =40 , 426win =1000 , 427} , 428} , 429float_pos ={ 430[ 4 ] ={ 1001 , 'NW' , 1 , 1 , 1 , true , 50 , 1 , 1 , 1 } , 431} , 432win_viewport ={ 433[ 2 ] ={ 434win =1000 , 435topline =0 , 436botline =2 , 437curline =0 , 438curcol =0 , 439linecount =1 , 440sum_scroll_delta =0 , 441} , 442[ 4 ] ={ 443win =1001 , 444topline =0 , 445botline =2 , 446curline =0 , 447curcol =0 , 448linecount =1 , 449sum_scroll_delta =0 , 450} , 451} , 452} ) 453else 454screen : expect ( [[ 455^ | 456{1:~}{4:┌─}{100:foo}{4:─┐}{1: }| 457{1:~}{4:│bar │}{1: }| 458{1:~}{4:│ │}{1: }| 459{1:~}{4:└─────┘}{1: }| 460{1:~ }| 461| 462]] ) 463end 464 465-- move floating preview window to current split window 466command ( 'vs | wincmd p | pedit none_exist' ) 467if multigrid then 468screen : expect ( { 469grid =[[ 470## grid 1 471[5:-------------------]│[2:--------------------]|*5 472{2:[No Name] }{3:[No Name] }| 473[3:----------------------------------------]| 474## grid 2 475^ | 476{1:~ }|*4 477## grid 3 478| 479## grid 4 480{4:┌}{100:<xist}{4:┐}| 481{4:│ │}|*2 482{4:└─────┘}| 483## grid 5 484| 485{1:~ }|*4 486]] , 487win_pos ={ 488[ 2 ] ={ 489height =5 , 490startcol =20 , 491startrow =0 , 492width =20 , 493win =1000 , 494} , 495[ 5 ] ={ 496height =5 , 497startcol =0 , 498startrow =0 , 499width =19 , 500win =1002 , 501} , 502} , 503float_pos ={ 504[ 4 ] ={ 1001 , 'NW' , 1 , 1 , 21 , true , 50 , 1 , 1 , 21 } , 505} , 506win_viewport ={ 507[ 2 ] ={ 508win =1000 , 509topline =0 , 510botline =2 , 511curline =0 , 512curcol =0 , 513linecount =1 , 514sum_scroll_delta =0 , 515} , 516[ 4 ] ={ 517win =1001 , 518topline =0 , 519botline =2 , 520curline =0 , 521curcol =0 , 522linecount =1 , 523sum_scroll_delta =0 , 524} , 525[ 5 ] ={ 526win =1002 , 527topline =0 , 528botline =2 , 529curline =0 , 530curcol =0 , 531linecount =1 , 532sum_scroll_delta =0 , 533} , 534} , 535win_viewport_margins ={ 536[ 2 ] ={ 537bottom =0 , 538left =0 , 539right =0 , 540top =0 , 541win =1000 , 542} , 543[ 4 ] ={ 544bottom =1 , 545left =1 , 546right =1 , 547top =1 , 548win =1001 , 549} , 550[ 5 ] ={ 551bottom =0 , 552left =0 , 553right =0 , 554top =0 , 555win =1002 , 556} , 557} , 558} ) 559else 560screen : expect ( [[ 561│^ | 562{1:~ }│{1:~}{4:┌}{100:<xist}{4:┐}{1: }| 563{1:~ }│{1:~}{4:│ │}{1: }|*2 564{1:~ }│{1:~}{4:└─────┘}{1: }| 565{2:[No Name] }{3:[No Name] }| 566| 567]] ) 568end 569command ( 'pclose' ) -- can close by pclose command 570eq ( 2 , # api . nvim_list_wins ()) 571 572-- border overrides and falls back to 'winborder' 573command ( 'only | set previewpopup=height:2,width:5,border:rounded | pedit foo' ) 574if not multigrid then 575screen : expect ( [[ 576^ | 577{1:~}{4:╭─}{100:foo}{4:─╮}{1: }| 578{1:~}{4:│bar │}{1: }| 579{1:~}{4:│ │}{1: }| 580{1:~}{4:╰─────╯}{1: }| 581{1:~ }| 582| 583]] ) 584end 585command ( 'pclose | set previewpopup+=border:none | pedit foo' ) 586if multigrid then 587screen : expect ( { 588grid =[[ 589## grid 1 590[2:----------------------------------------]|*6 591[3:----------------------------------------]| 592## grid 2 593^ | 594{1:~ }|*5 595## grid 3 596| 597## grid 7 598{4:bar }| 599{4: }| 600]] , 601win_pos ={ 602[ 2 ] ={ height =6 , startcol =0 , startrow =0 , width =40 , win =1000 } , 603} , 604float_pos ={ 605[ 7 ] ={ 1004 , 'NW' , 1 , 1 , 1 , true , 50 , 1 , 1 , 1 } , 606} , 607} ) 608else 609screen : expect ( [[ 610^ | 611{1:~}{4:bar }{1: }| 612{1:~}{4: }{1: }| 613{1:~ }|*3 614| 615]] ) 616end 617end ) 618 619it ( 'autosizes to content when height/width are omitted' , function () 620screen : try_resize ( 40 , 20 ) 621command ( 'call writefile(["short", "a much longer line here"], "foo")' ) 622finally ( function () 623os . remove ( 'foo' ) 624end ) 625command ( 'set previewpopup=border:none | pedit foo' ) 626 627local pwin ---@type integer 628for _ , w in ipairs ( api . nvim_list_wins ()) do 629if api . nvim_win_get_config ( w ). relative ~= '' then 630pwin =w 631break 632end 633end 634local conf =api . nvim_win_get_config ( pwin ) 635eq ( 23 , conf . width ) -- longest line 636eq ( 2 , conf . height ) -- line count 637end ) 638end 639 640describe ( 'with ext_multigrid' , function () 641with_ext_multigrid ( true ) 642end ) 643 644describe ( 'without ext_multigrid' , function () 645with_ext_multigrid ( false ) 646end ) 647end )