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