blob: f9499acf09b5698109199114df209c975879329f [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01003set belloff=all
4
Bram Moolenaarae3150e2016-06-11 23:22:36 +02005func Test_complete_tab()
6 call writefile(['testfile'], 'Xtestfile')
7 call feedkeys(":e Xtest\t\r", "tx")
8 call assert_equal('testfile', getline(1))
9 call delete('Xtestfile')
10endfunc
11
12func Test_complete_list()
13 " We can't see the output, but at least we check the code runs properly.
14 call feedkeys(":e test\<C-D>\r", "tx")
15 call assert_equal('test', expand('%:t'))
16endfunc
17
18func Test_complete_wildmenu()
19 call writefile(['testfile1'], 'Xtestfile1')
20 call writefile(['testfile2'], 'Xtestfile2')
21 set wildmenu
22 call feedkeys(":e Xtest\t\t\r", "tx")
23 call assert_equal('testfile2', getline(1))
24
25 call delete('Xtestfile1')
26 call delete('Xtestfile2')
27 set nowildmenu
28endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020029
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010030func Test_map_completion()
31 if !has('cmdline_compl')
32 return
33 endif
34 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
35 call assert_equal('"map <unique> <silent>', getreg(':'))
36 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
37 call assert_equal('"map <script> <unique>', getreg(':'))
38 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
39 call assert_equal('"map <expr> <script>', getreg(':'))
40 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
41 call assert_equal('"map <buffer> <expr>', getreg(':'))
42 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
43 call assert_equal('"map <nowait> <buffer>', getreg(':'))
44 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
45 call assert_equal('"map <special> <nowait>', getreg(':'))
46 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
47 call assert_equal('"map <silent> <special>', getreg(':'))
48endfunc
49
Bram Moolenaar15eedf12017-01-22 19:25:33 +010050func Test_match_completion()
51 if !has('cmdline_compl')
52 return
53 endif
54 hi Aardig ctermfg=green
55 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
56 call assert_equal('"match Aardig', getreg(':'))
57 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
58 call assert_equal('"match none', getreg(':'))
59endfunc
60
61func Test_highlight_completion()
62 if !has('cmdline_compl')
63 return
64 endif
65 hi Aardig ctermfg=green
66 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
67 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +020068 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
69 call assert_equal('"hi default Aardig', getreg(':'))
70 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
71 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +010072 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
73 call assert_equal('"hi link', getreg(':'))
74 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
75 call assert_equal('"hi default', getreg(':'))
76 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
77 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +020078
79 " A cleared group does not show up in completions.
80 hi Anders ctermfg=green
81 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
82 hi clear Aardig
83 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
84 hi clear Anders
85 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +010086endfunc
87
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010088func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +010089 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +010090 return
91 endif
92 for cmd in [
93 \ 'let a = ',
94 \ 'if',
95 \ 'elseif',
96 \ 'while',
97 \ 'for',
98 \ 'echo',
99 \ 'echon',
100 \ 'execute',
101 \ 'echomsg',
102 \ 'echoerr',
103 \ 'call',
104 \ 'return',
105 \ 'cexpr',
106 \ 'caddexpr',
107 \ 'cgetexpr',
108 \ 'lexpr',
109 \ 'laddexpr',
110 \ 'lgetexpr']
111 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
112 call assert_equal('"' . cmd . ' getline(', getreg(':'))
113 endfor
114endfunc
115
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200116func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200117 if !has('cmdline_compl')
118 return
119 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200120 let groupcount = len(getcompletion('', 'event'))
121 call assert_true(groupcount > 0)
122 let matchcount = len(getcompletion('File', 'event'))
123 call assert_true(matchcount > 0)
124 call assert_true(groupcount > matchcount)
125
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200126 if has('menu')
127 source $VIMRUNTIME/menu.vim
128 let matchcount = len(getcompletion('', 'menu'))
129 call assert_true(matchcount > 0)
130 call assert_equal(['File.'], getcompletion('File', 'menu'))
131 call assert_true(matchcount > 0)
132 let matchcount = len(getcompletion('File.', 'menu'))
133 call assert_true(matchcount > 0)
134 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200135
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200136 let l = getcompletion('v:n', 'var')
137 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200138 let l = getcompletion('v:notexists', 'var')
139 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200140
141 let l = getcompletion('', 'augroup')
142 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200143 let l = getcompletion('blahblah', 'augroup')
144 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200145
146 let l = getcompletion('', 'behave')
147 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200148 let l = getcompletion('not', 'behave')
149 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200150
151 let l = getcompletion('', 'color')
152 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200153 let l = getcompletion('dirty', 'color')
154 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200155
156 let l = getcompletion('', 'command')
157 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200158 let l = getcompletion('awake', 'command')
159 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200160
161 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200162 call assert_true(index(l, 'samples/') >= 0)
163 let l = getcompletion('NoMatch', 'dir')
164 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200165
166 let l = getcompletion('exe', 'expression')
167 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200168 let l = getcompletion('kill', 'expression')
169 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200170
171 let l = getcompletion('tag', 'function')
172 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200173 let l = getcompletion('paint', 'function')
174 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200175
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200176 let Flambda = {-> 'hello'}
177 let l = getcompletion('', 'function')
178 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200179 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200180
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200181 let l = getcompletion('run', 'file')
182 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200183 let l = getcompletion('walk', 'file')
184 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200185 set wildignore=*.vim
186 let l = getcompletion('run', 'file', 1)
187 call assert_true(index(l, 'runtest.vim') < 0)
188 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200189
190 let l = getcompletion('ha', 'filetype')
191 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200192 let l = getcompletion('horse', 'filetype')
193 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200194
195 let l = getcompletion('z', 'syntax')
196 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200197 let l = getcompletion('emacs', 'syntax')
198 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200199
200 let l = getcompletion('jikes', 'compiler')
201 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200202 let l = getcompletion('break', 'compiler')
203 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200204
205 let l = getcompletion('last', 'help')
206 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200207 let l = getcompletion('giveup', 'help')
208 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200209
210 let l = getcompletion('time', 'option')
211 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200212 let l = getcompletion('space', 'option')
213 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200214
215 let l = getcompletion('er', 'highlight')
216 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200217 let l = getcompletion('dark', 'highlight')
218 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200219
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200220 let l = getcompletion('', 'messages')
221 call assert_true(index(l, 'clear') >= 0)
222 let l = getcompletion('not', 'messages')
223 call assert_equal([], l)
224
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200225 let l = getcompletion('', 'mapclear')
226 call assert_true(index(l, '<buffer>') >= 0)
227 let l = getcompletion('not', 'mapclear')
228 call assert_equal([], l)
229
Bram Moolenaarb650b982016-08-05 20:35:13 +0200230 if has('cscope')
231 let l = getcompletion('', 'cscope')
232 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
233 call assert_equal(cmds, l)
234 " using cmdline completion must not change the result
235 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
236 let l = getcompletion('', 'cscope')
237 call assert_equal(cmds, l)
238 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
239 let l = getcompletion('find ', 'cscope')
240 call assert_equal(keys, l)
241 endif
242
Bram Moolenaar7522f692016-08-06 14:12:50 +0200243 if has('signs')
244 sign define Testing linehl=Comment
245 let l = getcompletion('', 'sign')
246 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
247 call assert_equal(cmds, l)
248 " using cmdline completion must not change the result
249 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
250 let l = getcompletion('', 'sign')
251 call assert_equal(cmds, l)
252 let l = getcompletion('list ', 'sign')
253 call assert_equal(['Testing'], l)
254 endif
255
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200256 " For others test if the name is recognized.
257 let names = ['buffer', 'environment', 'file_in_path',
258 \ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200259 if has('cmdline_hist')
260 call add(names, 'history')
261 endif
262 if has('gettext')
263 call add(names, 'locale')
264 endif
265 if has('profile')
266 call add(names, 'syntime')
267 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200268
269 set tags=Xtags
270 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
271
272 for name in names
273 let matchcount = len(getcompletion('', name))
274 call assert_true(matchcount >= 0, 'No matches for ' . name)
275 endfor
276
277 call delete('Xtags')
278
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200279 call assert_fails('call getcompletion("", "burp")', 'E475:')
280endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200281
282func Test_expand_star_star()
283 call mkdir('a/b', 'p')
284 call writefile(['asdfasdf'], 'a/b/fileXname')
285 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
286 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200287 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200288 call delete('a', 'rf')
289endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100290
291func Test_paste_in_cmdline()
292 let @a = "def"
293 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
294 call assert_equal('"abc def ghi', @:)
295
296 new
297 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
298
299 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
300 call assert_equal('"aaa asdf bbb', @:)
301
302 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
303 call assert_equal('"aaa /tmp/some bbb', @:)
304
305 set incsearch
306 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
307 call assert_equal('"aaa verylongword bbb', @:)
308
309 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
310 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100311
312 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
313 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100314 bwipe!
315endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100316
317func Test_remove_char_in_cmdline()
318 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
319 call assert_equal('"abc ef', @:)
320
321 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
322 call assert_equal('"abcdef', @:)
323
324 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
325 call assert_equal('"abc ghi', @:)
326
327 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
328 call assert_equal('"def', @:)
329endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100330
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100331func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100332 new
333 2;'(
334 2;')
335 quit
336endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100337
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100338func Test_illegal_address2()
339 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
340 new
341 source Xtest.vim
342 " Trigger calling validate_cursor()
343 diffsp Xtest.vim
344 quit!
345 bwipe!
346 call delete('Xtest.vim')
347endfunc
348
Bram Moolenaarba47b512017-01-24 21:18:19 +0100349func Test_cmdline_complete_wildoptions()
350 help
351 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
352 let a = join(sort(split(@:)),' ')
353 set wildoptions=tagfile
354 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
355 let b = join(sort(split(@:)),' ')
356 call assert_equal(a, b)
357 bw!
358endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100359
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200360func Test_cmdline_complete_user_cmd()
361 command! -complete=color -nargs=1 Foo :
362 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
363 call assert_equal('"Foo blue', @:)
364 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
365 call assert_equal('"Foo blue', @:)
366 delcommand Foo
367endfunc
368
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100369" using a leading backslash here
370set cpo+=C
371
372func Test_cmdline_search_range()
373 new
374 call setline(1, ['a', 'b', 'c', 'd'])
375 /d
376 1,\/s/b/B/
377 call assert_equal('B', getline(2))
378
379 /a
380 $
381 \?,4s/c/C/
382 call assert_equal('C', getline(3))
383
384 call setline(1, ['a', 'b', 'c', 'd'])
385 %s/c/c/
386 1,\&s/b/B/
387 call assert_equal('B', getline(2))
388
389 bwipe!
390endfunc
391
Bram Moolenaar65189a12017-02-06 22:22:17 +0100392" Tests for getcmdline(), getcmdpos() and getcmdtype()
393func Check_cmdline(cmdtype)
394 call assert_equal('MyCmd a', getcmdline())
395 call assert_equal(8, getcmdpos())
396 call assert_equal(a:cmdtype, getcmdtype())
397 return ''
398endfunc
399
400func Test_getcmdtype()
401 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
402
403 let cmdtype = ''
404 debuggreedy
405 call feedkeys(":debug echo 'test'\<CR>", "t")
406 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
407 call feedkeys("cont\<CR>", "xt")
408 0debuggreedy
409 call assert_equal('>', cmdtype)
410
411 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
412 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
413
414 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100415 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100416
417 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
418
419 cnoremap <expr> <F6> Check_cmdline('=')
420 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
421 cunmap <F6>
422endfunc
423
Bram Moolenaar52604f22017-04-07 16:17:39 +0200424func Test_verbosefile()
425 set verbosefile=Xlog
426 echomsg 'foo'
427 echomsg 'bar'
428 set verbosefile=
429 let log = readfile('Xlog')
430 call assert_match("foo\nbar", join(log, "\n"))
431 call delete('Xlog')
432endfunc
433
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100434set cpo&