robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
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 , after_each =t . describe , t . it , t . before_each , t . after_each 6local eq =t . eq 7local exec_lua =n . exec_lua 8local clear =n . clear 9local feed =n . feed 10local fn =n . fn 11local assert_log =t . assert_log 12local check_close =n . check_close 13 14local testlog ='Xtest_lua_ui_event_log' 15 16describe ( 'vim.ui_attach' , function () 17local screen 18before_each ( function () 19clear () 20exec_lua [[ 21ns = vim.api.nvim_create_namespace 'testspace' 22events = {} 23function on_event(event, ...) 24events[#events+1] = {event, ...} 25return true 26end 27 28function get_events() 29local ret_events = events 30events = {} 31return ret_events 32end 33]] 34 35screen =Screen . new ( 40 , 5 ) 36end ) 37 38local function expect_events ( expected ) 39local evs =exec_lua 'return get_events(...)' 40eq ( expected , evs , vim . inspect ( evs )) 41end 42 43it ( 'can receive popupmenu events' , function () 44exec_lua [[ vim.ui_attach(ns, {ext_popupmenu=true}, on_event) ]] 45feed ( 'ifo' ) 46screen : expect { 47grid =[[ 48fo^ | 49{1:~ }|*3 50{5:-- INSERT --} | 51]] , 52} 53 54fn . complete ( 1 , { 'food' , 'foobar' , 'foo' } ) 55screen : expect { 56grid =[[ 57food^ | 58{1:~ }|*3 59{5:-- INSERT --} | 60]] , 61} 62expect_events { 63{ 64'popupmenu_show' , 65{ { 'food' , '' , '' , '' } , { 'foobar' , '' , '' , '' } , { 'foo' , '' , '' , '' } } , 660 , 670 , 680 , 691 , 70} , 71} 72 73feed '<c-n>' 74screen : expect { 75grid =[[ 76foobar^ | 77{1:~ }|*3 78{5:-- INSERT --} | 79]] , 80} 81expect_events { 82{ 'popupmenu_select' , 1 } , 83} 84 85feed '<c-y>' 86screen : expect_unchanged () 87expect_events { 88{ 'popupmenu_hide' } , 89} 90 91-- vim.ui_detach() stops events, and reenables builtin pum immediately 92exec_lua [[ 93vim.ui_detach(ns) 94vim.fn.complete(1, {'food', 'foobar', 'foo'}) 95]] 96 97screen : expect { 98grid =[[ 99food^ | 100{12:food }{1: }| 101{4:foobar }{1: }| 102{4:foo }{1: }| 103{5:-- INSERT --} | 104]] , 105} 106expect_events {} 107end ) 108 109it ( 'does not crash on exit' , function () 110local p =n . spawn_wait ( 111'--cmd' , 112[[ lua ns = vim.api.nvim_create_namespace 'testspace' ]] , 113'--cmd' , 114[[ lua vim.ui_attach(ns, {ext_popupmenu=true}, function() end) ]] , 115'--cmd' , 116'quitall!' 117) 118eq ( 0 , p . status ) 119end ) 120 121it ( 'can receive accurate message kinds even if they are history' , function () 122exec_lua ( [[ 123vim.cmd.echomsg("'message1'") 124print('message2') 125vim.ui_attach(ns, { ext_messages = true }, on_event) 126vim.cmd.echomsg("'message3'") 127]] ) 128feed ( ':messages<cr>' ) 129feed ( '<cr>' ) 130 131local actual =exec_lua ( [[ 132return vim.tbl_filter(function (event) 133return event[1] == "msg_history_show" 134end, events) 135]] ) 136eq ( { 137{ 138'msg_history_show' , 139{ 140{ 'echomsg' , { { 0 , 'message1' , 0 } } , false } , 141{ 'lua_print' , { { 0 , 'message2' , 0 } } , false } , 142{ 'echomsg' , { { 0 , 'message3' , 0 } } , false } , 143} , 144false , 145} , 146} , actual , vim . inspect ( actual )) 147end ) 148 149it ( 'ui_refresh() activates correct capabilities without remote UI' , function () 150screen : detach () 151exec_lua ( 'vim.ui_attach(ns, { ext_cmdline = true }, on_event)' ) 152eq ( 1 , n . api . nvim_get_option_value ( 'cmdheight' , {} )) 153exec_lua ( 'vim.ui_detach(ns)' ) 154exec_lua ( 'vim.ui_attach(ns, { ext_messages = true }, on_event)' ) 155n . api . nvim_set_option_value ( 'cmdheight' , 1 , {} ) 156screen : attach () 157eq ( 1 , n . api . nvim_get_option_value ( 'cmdheight' , {} )) 158end ) 159 160it ( "ui_refresh() sets 'cmdheight' for all open tabpages with ext_messages" , function () 161exec_lua ( 'vim.cmd.tabnew()' ) 162exec_lua ( 'vim.ui_attach(ns, { ext_messages = true }, on_event)' ) 163exec_lua ( 'vim.cmd.tabnext()' ) 164eq ( 0 , n . api . nvim_get_option_value ( 'cmdheight' , {} )) 165end ) 166 167it ( "can attach ext_messages without changing 'cmdheight'" , function () 168exec_lua ( 'vim.ui_attach(ns, { ext_messages = true, set_cmdheight = false }, on_event)' ) 169eq ( 1 , n . api . nvim_get_option_value ( 'cmdheight' , {} )) 170end ) 171 172it ( 'avoids recursive flushing and invalid memory access with :redraw' , function () 173exec_lua ( [[ 174_G.cmdline = 0 175vim.ui_attach(ns, { ext_messages = true }, function(ev) 176if ev == 'msg_show' then 177vim.schedule(function() vim.cmd.redraw() end) 178elseif ev:find('cmdline') then 179_G.cmdline = _G.cmdline + (ev == 'cmdline_show' and 1 or 0) 180vim.api.nvim_buf_set_lines(0, 0, -1, false, { tostring(_G.cmdline) }) 181vim.cmd('redraw') 182end 183end 184)]] ) 185screen : expect ( [[ 186^ | 187{1:~ }|*4 188]] ) 189feed ( ':' ) 190screen : expect ( { 191grid =[[ 192^1 | 193{1:~ }|*4 194]] , 195cmdline ={ { content ={ { '' } } , firstc =':' , pos =0 } } , 196} ) 197feed ( 'version<CR>' ) 198screen : expect ( { 199grid =[[ 200^2 | 201{1:~ }|*4 202]] , 203condition =function () 204eq ( 'list_cmd' , screen . messages [ 1 ]. kind ) 205screen . messages ={} -- Ignore the build dependent :version content 206end , 207} ) 208feed ( [[v<Esc>:call confirm("Save changes?", "&Yes\n&No\n&Cancel")<CR>]] ) 209screen : expect ( { 210grid =[[ 211^4 | 212{1:~ }|*4 213]] , 214cmdline ={ 215{ content ={ { '' } } , hl ='MoreMsg' , pos =0 , prompt ='[Y]es, (N)o, (C)ancel: ' } , 216} , 217messages ={ { content ={ { 'Save changes?' , 6 , 'MoreMsg' } } , kind ='confirm' } } , 218} ) 219feed ( 'n' ) 220screen : expect_unchanged () 221end ) 222 223it ( "preserved 'incsearch/command' screen state after :redraw from ext_cmdline" , function () 224exec_lua ( [[ 225vim.cmd.norm('ifoobar') 226vim.cmd('1split cmdline') 227local buf = vim.api.nvim_get_current_buf() 228vim.cmd.wincmd('p') 229vim.ui_attach(ns, { ext_cmdline = true }, function(event, ...) 230if event == 'cmdline_show' then 231local content = select(1, ...) 232vim.api.nvim_buf_set_lines(buf, -2, -1, false, {content[1][2]}) 233vim.cmd('redraw') 234end 235return true 236end) 237]] ) 238-- Updates a cmdline window 239feed ( ':cmdline' ) 240screen : expect ( [[ 241cmdline | 242{2:cmdline [+] }| 243fooba^r | 244{3:[No Name] [+] }| 245| 246]] ) 247-- Does not clear 'incsearch' highlighting 248feed ( '<Esc>/foo' ) 249screen : expect ( [[ 250foo | 251{2:cmdline [+] }| 252{2:foo}ba^r | 253{3:[No Name] [+] }| 254| 255]] ) 256-- Shows new cmdline state during 'inccommand' 257feed ( '<Esc>:%s/bar/baz' ) 258screen : expect ( [[ 259%s/bar/baz | 260{2:cmdline [+] }| 261foo{10:ba^z} | 262{3:[No Name] [+] }| 263| 264]] ) 265end ) 266 267it ( 'msg_show in fast context' , function () 268exec_lua ( [[ 269vim.ui_attach(ns, { ext_messages = true }, function(event, _, content) 270if event == "msg_show" then 271vim.api.nvim_get_runtime_file("foo", false) 272-- non-"fast-api" is not allowed in msg_show callback and should be scheduled 273local _, err = pcall(vim.api.nvim_buf_set_lines, 0, -2, -1, false, { content[1][2] }) 274pcall(vim.api.nvim__redraw, { flush = true }) 275vim.schedule(function() 276vim.api.nvim_buf_set_lines(0, -2, -1, false, { content[1][2], err }) 277end) 278end 279end) 280]] ) 281-- "fast-api" does not prevent aborting :function 282feed ( ':func Foo()<cr>bar<cr>endf<cr>:func Foo()<cr>' ) 283screen : expect ( { 284grid =[[ 285^E122: Function Foo already exists, add !| 286to replace it | 287E5560: nvim_buf_set_lines must not be ca| 288lled in a fast event context | 289{1:~ }| 290]] , 291messages ={ 292{ 293content ={ { 'E122: Function Foo already exists, add ! to replace it' , 9 , 'ErrorMsg' } } , 294history =true , 295kind ='emsg' , 296} , 297} , 298} ) 299end ) 300 301it ( 'ext_cmdline completion popupmenu' , function () 302screen : try_resize ( screen . _width , 10 ) 303screen : add_extra_attr_ids { [ 100 ] ={ background =Screen . colors . Black } } 304exec_lua ( [[ 305vim.o.wildoptions = 'pum' 306local buf = vim.api.nvim_create_buf(false, true) 307vim.cmd('call setline(1, range(1, 10))') 308_G.win = vim.api.nvim_open_win(buf, false, { 309relative = 'editor', 310col = 3, 311row = 3, 312width = 20, 313height = 1, 314style = 'minimal', 315focusable = false, 316zindex = 300, 317_cmdline_offset = 0, 318}) 319vim.ui_attach(ns, { ext_cmdline = true }, function(event, content, _, firstc) 320if event == 'cmdline_show' then 321local prompt = vim.api.nvim_win_get_config(_G.win)._cmdline_offset == 0 322prompt = (prompt and firstc or 'Excommand:') .. content[1][2] 323vim.api.nvim_buf_set_lines(buf, -2, -1, false, { prompt }) 324vim.api.nvim_win_set_cursor(_G.win, { 1, #prompt }) 325vim.api.nvim__redraw({ win = _G.win, cursor = true, flush = true }) 326end 327return true 328end) 329vim.api.nvim_set_hl(0, 'Pmenu', {}) 330]] ) 331feed ( ':call buf<tab>' ) 332screen : expect ( [[ 3331 | 3342 | 3353 | 3364 :call bufadd^( | 3375 {12: bufadd( }{100: } | 3386 bufexists( {100: } | 3397 buffer_exists( {12: } | 3408 buffer_name( {12: } | 3419 buffer_number( {12: } | 342| 343]] ) 344exec_lua ( [[ 345vim.api.nvim_win_set_config(_G.win, { 346relative = 'editor', 347col = 0, 348row = 1000, 349width = 1000, 350height = 1, 351}) 352vim.api.nvim__redraw({flush = true}) 353]] ) 354screen : expect ( [[ 3551 | 3562 | 3573 | 3584 | 3595 {12: bufadd( }{100: } | 3606 bufexists( {100: } | 3617 buffer_exists( {12: } | 3628 buffer_name( {12: } | 3639 buffer_number( {12: } | 364:call bufadd^( | 365]] ) 366feed ( '<tab>' ) 367screen : expect ( [[ 3681 bufadd( {100: } | 3692 {12: bufexists( }{100: } | 3703 buffer_exists( {100: } | 3714 buffer_name( {100: } | 3725 buffer_number( {100: } | 3736 buflisted( {100: } | 3747 bufload( {12: } | 3758 bufloaded( {12: } | 3769 bufname( {12: } | 377:call bufexists^( | 378]] ) 379-- Test different offset (e.g. for custom prompt) 380exec_lua ( 'vim.api.nvim_win_set_config(_G.win, { _cmdline_offset = 9 })' ) 381feed ( '<Esc>:call buf<Tab>' ) 382screen : expect ( [[ 3831 {12: bufadd( }{100: } | 3842 bufexists( {100: } | 3853 buffer_exists( {100: } | 3864 buffer_name( {100: } | 3875 buffer_number( {100: } | 3886 buflisted( {100: } | 3897 bufload( {12: } | 3908 bufloaded( {12: } | 3919 bufname( {12: } | 392Excommand:call bufadd^( | 393]] ) 394-- _cmdline_offset remains set after being turned into a split. 395exec_lua ( function () 396vim . fn . win_execute ( _G . win , 'wincmd J' ) 397end ) 398feed ( '<Tab>' ) -- Was a signed int overflow; offset was INT_MAX despite cmdline_win being set. 399eq ( 9 , exec_lua ( 'return vim.api.nvim_win_get_config(_G.win)._cmdline_offset' )) 400-- No crash after _cmdline_offset window is closed #35584. 401exec_lua ( function () 402vim . ui_detach ( _G . ns ) 403vim . api . nvim_win_close ( _G . win , true ) 404end ) 405feed ( '<Esc>:<Tab>' ) 406n . assert_alive () 407end ) 408 409it ( "does not crash with :norm 'showcmd' from shell message callback #38233" , function () 410exec_lua ( function () 411vim . ui_attach ( vim . api . nvim_create_namespace ( '' ), { ext_messages =true } , function ( event ) 412if event == 'msg_show' then 413vim . api . nvim_command ( 'norm! G' ) 414end 415end ) 416end ) 417n . command ( 'set showcmd | !echo "foo"' ) 418n . assert_alive () 419end ) 420end ) 421 422describe ( 'vim.ui_attach' , function () 423before_each ( function () 424clear ( { env ={ NVIM_LOG_FILE =testlog } } ) 425end ) 426 427after_each ( function () 428check_close () 429os . remove ( testlog ) 430end ) 431 432it ( 'callback error is logged' , function () 433exec_lua ( [[ 434local ns = vim.api.nvim_create_namespace('test') 435vim.ui_attach(ns, { ext_popupmenu = true }, function() error(42) end) 436]] ) 437feed ( 'ifoo<CR>foobar<CR>fo<C-X><C-N>' ) 438assert_log ( 439'Error in "popupmenu_show" UI event handler %(ns=test%):[\r\n\t ]+Lua: .*: 42' , 440testlog , 441100 442) 443end ) 444 445it ( 'detaches after excessive errors' , function () 446local screen =Screen . new ( 66 , 10 ) 447screen : add_extra_attr_ids ( { [ 100 ] ={ bold =true , foreground =Screen . colors . SeaGreen } } ) 448exec_lua ( [[ 449vim.ui_attach(vim.api.nvim_create_namespace(''), { ext_messages = true }, function(ev) 450if ev == 'msg_show' then 451error('foo') 452end 453end) 454]] ) 455feed ( '@@' ) 456screen : expect ( { 457grid =[[ 458| 459{1:~ }|*2 460{3: }| 461{9:Error in "msg_show" UI event handler (ns=(UNKNOWN PLUGIN)):} | 462{9:Lua: [string "<nvim>"]:3: foo} | 463{9:stack traceback:} | 464{9: [C]: in function 'error'} | 465{9: [string "<nvim>"]:3: in function <[string "<nvim>"]:1>} | 466{100:Press ENTER or type command to continue}^ | 467]] , 468condition =function () 469screen . messages ={} 470end , 471} ) 472feed ( '<Esc>' ) 473 474-- Also when scheduled 475exec_lua ( [[ 476vim.ui_attach(vim.api.nvim_create_namespace(''), { ext_messages = true }, function(ev) 477if ev == 'msg_show' then 478vim.schedule(function() error('foo') end) 479end 480end) 481]] ) 482feed ( '@@' ) 483screen : expect ( { 484grid =[[ 485| 486{1:~ }|*6 487{3: }| 488{9:Excessive errors in vim.ui_attach() callback (ns=(UNKNOWN PLUGIN))}| 489{100:Press ENTER or type command to continue}^ | 490]] , 491condition =function () 492screen . messages ={} 493end , 494} ) 495end ) 496 497it ( 'sourcing invalid file does not crash #32166' , function () 498exec_lua ( [[ 499local ns = vim.api.nvim_create_namespace("") 500vim.ui_attach(ns, { ext_messages = true }, function() end) 501]] ) 502feed (( ':luafile %s<CR>' ): format ( testlog )) 503n . assert_alive () 504end ) 505end )