robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1-- Tests for non-blocking cmdwin. #40312 2 3local t =require ( 'test.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5 6local describe , it , before_each =t . describe , t . it , t . before_each 7local feed =n . feed 8local api =n . api 9local fn =n . fn 10local exec_lua =n . exec_lua 11local eq =t . eq 12 13describe ( 'cmdwin' , function () 14before_each ( n . clear ) 15 16it ( 'q: opens cmdwin' , function () 17feed ( 'q:' ) 18eq ( ':' , fn . getcmdwintype ()) 19eq ( '[Command Line]' , vim . fs . basename ( api . nvim_buf_get_name ( 0 ))) 20-- cmdwin-char is shown in window-local 'statuscolumn'. 21eq ( '%#NonText#:' , api . nvim_get_option_value ( 'statuscolumn' , { win =0 } )) 22 23eq ( 'nofile' , api . nvim_get_option_value ( 'buftype' , { buf =0 } )) 24eq ( 'wipe' , api . nvim_get_option_value ( 'bufhidden' , { buf =0 } )) 25eq ( false , api . nvim_get_option_value ( 'swapfile' , { buf =0 } )) 26eq ( true , api . nvim_get_option_value ( 'buflisted' , { buf =0 } )) -- #40431 27 28-- <CR> executes the cmdline 29feed ( 'ilet g:cmdwin_result = 42<Esc>' ) 30feed ( '<CR>' ) 31eq ( 42 , api . nvim_get_var ( 'cmdwin_result' )) 32eq ( '' , fn . getcmdwintype ()) 33end ) 34 35it ( '<CR> closes all cmdwin (split) windows #40484' , function () 36feed ( 'q:' ) 37feed ( 'ilet g:cmdwin_result = 7<Esc>' ) 38feed ( '<C-w>s' ) -- split: two windows now show the cmdwin buffer. 39local cmdwin_buf =api . nvim_get_current_buf () 40eq ( 2 , # fn . win_findbuf ( cmdwin_buf )) -- sanity: the split happened 41 42feed ( '<CR>' ) 43eq ( 7 , api . nvim_get_var ( 'cmdwin_result' )) -- the cmdline executed 44eq ( '' , fn . getcmdwintype ()) 45eq ( 0 , # fn . win_findbuf ( cmdwin_buf )) -- All cmdwin windows are closed. 46eq ( 1 , # api . nvim_list_wins ()) -- Back to the single original window. 47end ) 48 49it ( 'treats buffer-change as cmdwin-exit' , function () 50feed ( 'q:' ) 51local cmdwin_buf =api . nvim_get_current_buf () 52feed ( ':enew<CR>' ) -- this is typed into the cmdline, not the cmdwin (so it applies to the cmdwin) 53 54eq ( '' , fn . getcmdwintype ()) -- Cleanup has run 55eq ( 0 , # fn . win_findbuf ( cmdwin_buf )) -- All cmdwin windows are closed. 56eq ( 2 , # api . nvim_list_wins ()) -- The two plain windows 57 58feed ( 'q:' ) 59eq ( ':' , fn . getcmdwintype ()) -- cmdwin can reopen (internal state is correct) 60cmdwin_buf =api . nvim_get_current_buf () 61 62feed ( ':tabedit<CR>' ) 63feed ( ':tabonly<CR>' ) -- closes the cmdwin indirectly 64 65eq ( '' , fn . getcmdwintype ()) -- Cleanup has run 66eq ( 0 , # fn . win_findbuf ( cmdwin_buf )) -- All cmdwin windows are closed. 67eq ( 1 , # api . nvim_list_wins ()) -- The one plain tab window 68end ) 69 70it ( '<CR> executes when cmdwin was moved to another tabpage #40484' , function () 71feed ( 'q:' ) 72feed ( 'ilet g:cmdwin_result = 9<Esc>' ) 73feed ( '<C-w>T' ) -- Move the cmdwin to its own new tabpage. 74eq ( 2 , fn . tabpagenr ( '$' )) -- sanity: the new tabpage exists 75local cmdwin_buf =api . nvim_get_current_buf () 76 77feed ( '<CR>' ) 78eq ( 9 , api . nvim_get_var ( 'cmdwin_result' )) -- the cmdline executed. 79eq ( '' , fn . getcmdwintype ()) 80eq ( 0 , # fn . win_findbuf ( cmdwin_buf )) -- cmdwin window closed. 81eq ( 1 , fn . tabpagenr ( '$' )) -- cmdwin's tabpage is gone. 82eq ( 1 , # api . nvim_list_wins ()) 83end ) 84 85it ( 'q/ opens cmdwin with search history' , function () 86fn . histadd ( '/' , 'foo' ) 87fn . histadd ( '/' , 'bar' ) 88feed ( 'q/' ) 89eq ( '/' , fn . getcmdwintype ()) 90-- The "/" history is presented, plus an empty editable last line. 91eq ( { 'foo' , 'bar' , '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 92end ) 93 94it ( 'c_CTRL-F opens cmdwin pre-filled with current cmdline' , function () 95fn . histadd ( ':' , 'let g:x = 1' ) 96fn . histadd ( ':' , 'let g:y = 2' ) 97feed ( ':echo "hi"<C-F>' ) 98n . poke_eventloop () 99eq ( ':' , fn . getcmdwintype ()) 100-- The ":" history is presented. The in-flight cmdline must not be added to history (else it shows up twice). 101eq ( { 'let g:x = 1' , 'let g:y = 2' , 'echo "hi"' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 102eq ( 2 , fn . histnr ( 'cmd' )) -- History unchanged (the in-flight cmdline was not added). 103end ) 104 105it ( '<C-C> in normal mode cancels without executing' , function () 106feed ( 'q:' ) 107feed ( 'ilet g:executed = 1<Esc>' ) 108n . poke_eventloop () -- Ensure previous input is processed before <C-C>. 109feed ( '<C-C>' ) 110eq ( '' , fn . getcmdwintype ()) 111-- The cancelled line is neither executed nor added to history. It is pre-filled into the 112-- cmdline; reopening via c_CTRL-F must then not duplicate it. 113eq ( 0 , fn . exists ( 'g:executed' )) 114eq ( - 1 , fn . histnr ( 'cmd' )) 115eq ( 'let g:executed = 1' , fn . getcmdline ()) 116eq ( ':' , fn . getcmdtype ()) 117end ) 118 119it ( 'history entry or current cmdline with control chars or 0x80 byte' , function () 120local firstbuf =api . nvim_get_current_buf () 121local cmdline ='normal! \023\022ifoo\nbar…\027' -- Ctrl-W Ctrl-V ifoo\nbar… Esc 122local bufline =cmdline : gsub ( '\n' , '\0' ) 123fn . histadd ( ':' , cmdline ) 124feed ( 'q:' ) 125eq ( ':' , fn . getcmdwintype ()) 126eq ( { bufline , '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 127api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 128feed ( '<C-C>' ) 129eq ( cmdline , fn . getcmdline ()) 130eq ( ':' , fn . getcmdtype ()) 131eq ( { firstbuf } , fn . tabpagebuflist ()) 132feed ( '<C-F>' ) 133n . poke_eventloop () 134eq ( ':' , fn . getcmdwintype ()) 135eq ( { bufline , bufline } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 136feed ( '<CR>' ) 137eq ( { firstbuf , firstbuf } , fn . tabpagebuflist ()) 138eq ( { 'foo' , 'bar…' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 139end ) 140 141it ( 'async API calls work while cmdwin is open #40312' , function () 142feed ( 'q:' ) 143eq ( ':' , fn . getcmdwintype ()) 144local ok , err =pcall ( function () 145local b =api . nvim_create_buf ( true , false ) 146api . nvim_buf_delete ( b , { force =true } ) 147end ) 148eq ( true , ok , 'buf_delete unexpectedly failed: ' .. tostring ( err )) 149end ) 150 151it ( 'textlock does not include cmdwin #40312' , function () 152feed ( 'q:' ) 153-- Setting an option from API used to fail with E11 in cmdwin. 154api . nvim_set_option_value ( 'wildignore' , '*.tmp' , {} ) 155end ) 156 157it ( 'E1292 cannot nest cmdwin' , function () 158feed ( 'q:' ) 159local cmdwin_buf =api . nvim_get_current_buf () 160eq ( ':' , fn . getcmdwintype ()) 161feed ( 'q:' ) 162-- Still just one cmdwin. 163eq ( ':' , fn . getcmdwintype ()) 164local nwin =0 165for _ , w in ipairs ( api . nvim_list_wins ()) do 166if api . nvim_win_get_buf ( w ) == cmdwin_buf then 167nwin =nwin + 1 168end 169end 170eq ( 1 , nwin ) 171end ) 172 173it ( ':quit closes cmdwin' , function () 174feed ( 'q:' ) 175eq ( ':' , fn . getcmdwintype ()) 176exec_lua ( [[ 177_G.events = {} 178vim.api.nvim_create_autocmd('WinEnter', { 179callback = function() table.insert(_G.events, vim.api.nvim_get_current_win()) end, 180}) 181]] ) 182feed ( ':q<CR>' ) 183eq ( '' , fn . getcmdwintype ()) 184eq ( { api . nvim_get_current_win () } , exec_lua ( 'return _G.events' )) 185end ) 186 187it ( ':messages from inside cmdwin works #40312' , function () 188-- Before #40312, this raised E11 because text_locked() returned true for cmdwin. 189exec_lua ( [[vim.api.nvim_echo({{'hello from history'}}, true, {})]] ) 190feed ( 'q:' ) 191eq ( ':' , fn . getcmdwintype ()) 192n . command ( 'messages' ) -- Previously E11; now actually runs. 193end ) 194 195it ( 'CmdwinEnter/CmdwinLeave events' , function () 196exec_lua ( [[ 197_G.events = {} 198vim.api.nvim_create_autocmd('CmdwinEnter', { 199callback = function(a) table.insert(_G.events, 'enter:'..a.match) end, 200}) 201vim.api.nvim_create_autocmd('CmdwinLeave', { 202callback = function(a) table.insert(_G.events, 'leave:'..a.match) end, 203}) 204]] ) 205feed ( 'q:' ) 206n . poke_eventloop () -- Ensure q: is processed before <C-C>. 207feed ( '<C-C>' ) 208eq ( { 'enter::' , 'leave::' } , exec_lua ( 'return _G.events' )) 209end ) 210 211it ( [[q: in visual mode automatically inserts "'<,'>"]] , function () 212feed ( 'vq:' ) 213eq ( ':' , fn . getcmdwintype ()) 214eq ( { [['<,'>]] } , api . nvim_buf_get_lines ( 0 , - 2 , - 1 , false )) 215eq ( 4 , api . nvim_win_get_cursor ( 0 )[ 2 ]) 216end ) 217 218for _ , ty in ipairs ( { '/' , '?' } ) do 219it (( 'q%s in visual mode inserts nothing in current line' ): format ( ty ), function () 220feed ( 'vq' .. ty ) 221eq ( ty , fn . getcmdwintype ()) 222eq ( { '' } , api . nvim_buf_get_lines ( 0 , - 2 , - 1 , false )) 223end ) 224end 225end )