robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

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