blob: cb61be21c8323b3ad06705beb73ea43a87e0456e [file] [log] [blame]
Bram Moolenaar00672e12016-06-26 18:38:13 +02001" Test for completion menu
2
Bram Moolenaara5e66212017-09-29 22:42:33 +02003source shared.vim
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004source screendump.vim
Bram Moolenaara5e66212017-09-29 22:42:33 +02005
Bram Moolenaar00672e12016-06-26 18:38:13 +02006let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
7let g:setting = ''
8
Bram Moolenaar47247282016-08-02 22:36:02 +02009func! ListMonths()
10 if g:setting != ''
11 exe ":set" g:setting
12 endif
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +010013 let mth = copy(g:months)
Bram Moolenaar47247282016-08-02 22:36:02 +020014 let entered = strcharpart(getline('.'),0,col('.'))
15 if !empty(entered)
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +010016 let mth = filter(mth, 'v:val=~"^".entered')
Bram Moolenaar47247282016-08-02 22:36:02 +020017 endif
18 call complete(1, mth)
19 return ''
Bram Moolenaar00672e12016-06-26 18:38:13 +020020endfunc
21
Bram Moolenaardac19472016-09-03 22:35:40 +020022func! Test_popup_complete2()
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020023 " Although the popupmenu is not visible, this does not mean completion mode
24 " has ended. After pressing <f5> to complete the currently typed char, Vim
25 " still stays in the first state of the completion (:h ins-completion-menu),
26 " although the popupmenu wasn't shown <c-e> will remove the inserted
27 " completed text (:h complete_CTRL-E), while the following <c-e> will behave
28 " like expected (:h i_CTRL-E)
Bram Moolenaardac19472016-09-03 22:35:40 +020029 new
30 inoremap <f5> <c-r>=ListMonths()<cr>
31 call append(1, ["December2015"])
32 :1
33 call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx')
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020034 call assert_equal(["Dece", "", "December2015"], getline(1,3))
Bram Moolenaardac19472016-09-03 22:35:40 +020035 %d
36 bw!
37endfu
38
Bram Moolenaar47247282016-08-02 22:36:02 +020039func! Test_popup_complete()
40 new
41 inoremap <f5> <c-r>=ListMonths()<cr>
42
43 " <C-E> - select original typed text before the completion started
44 call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx')
45 call assert_equal(["Ju"], getline(1,2))
46 %d
47
48 " <C-Y> - accept current match
49 call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx')
50 call assert_equal(["August"], getline(1,2))
51 %d
52
53 " <BS> - Delete one character from the inserted text (state: 1)
54 " TODO: This should not end the completion, but it does.
55 " This should according to the documentation:
56 " January
57 " but instead, this does
58 " Januar
59 " (idea is, C-L inserts the match from the popup menu
60 " but if the menu is closed, it will insert the character <c-l>
61 call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx')
62 call assert_equal(["Januar "], getline(1,2))
63 %d
64
65 " any-non special character: Stop completion without changing the match
66 " and insert the typed character
67 call feedkeys("a\<f5>20", 'tx')
68 call assert_equal(["January20"], getline(1,2))
69 %d
70
71 " any-non printable, non-white character: Add this character and
72 " reduce number of matches
73 call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx')
74 call assert_equal(["Jul"], getline(1,2))
75 %d
76
77 " any-non printable, non-white character: Add this character and
78 " reduce number of matches
79 call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx')
80 call assert_equal(["July"], getline(1,2))
81 %d
82
83 " any-non printable, non-white character: Add this character and
84 " reduce number of matches
85 call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx')
86 call assert_equal(["Jul"], getline(1,2))
87 %d
88
89 " <BS> - Delete one character from the inserted text (state: 2)
90 call feedkeys("a\<f5>\<c-n>\<bs>", 'tx')
91 call assert_equal(["Februar"], getline(1,2))
92 %d
93
94 " <c-l> - Insert one character from the current match
95 call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx')
96 call assert_equal(["J "], getline(1,2))
97 %d
98
99 " <c-l> - Insert one character from the current match
100 call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx')
101 call assert_equal(["January "], getline(1,2))
102 %d
103
104 " <c-y> - Accept current selected match
105 call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx')
106 call assert_equal(["January"], getline(1,2))
107 %d
108
109 " <c-e> - End completion, go back to what was there before selecting a match
110 call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx')
111 call assert_equal(["Ju"], getline(1,2))
112 %d
113
114 " <PageUp> - Select a match several entries back
115 call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx')
116 call assert_equal([""], getline(1,2))
117 %d
118
119 " <PageUp><PageUp> - Select a match several entries back
120 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
121 call assert_equal(["December"], getline(1,2))
122 %d
123
124 " <PageUp><PageUp><PageUp> - Select a match several entries back
125 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
126 call assert_equal(["February"], getline(1,2))
127 %d
128
129 " <PageDown> - Select a match several entries further
130 call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx')
131 call assert_equal(["November"], getline(1,2))
132 %d
133
134 " <PageDown><PageDown> - Select a match several entries further
135 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
136 call assert_equal(["December"], getline(1,2))
137 %d
138
139 " <PageDown><PageDown><PageDown> - Select a match several entries further
140 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
141 call assert_equal([""], getline(1,2))
142 %d
143
144 " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further
145 call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx')
146 call assert_equal(["October"], getline(1,2))
147 %d
148
149 " <Up> - Select a match don't insert yet
150 call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx')
151 call assert_equal([""], getline(1,2))
152 %d
153
154 " <Up><Up> - Select a match don't insert yet
155 call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx')
156 call assert_equal(["December"], getline(1,2))
157 %d
158
159 " <Up><Up><Up> - Select a match don't insert yet
160 call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx')
161 call assert_equal(["November"], getline(1,2))
162 %d
163
164 " <Tab> - Stop completion and insert the match
165 call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx')
166 call assert_equal(["January "], getline(1,2))
167 %d
168
169 " <Space> - Stop completion and insert the match
170 call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx')
171 call assert_equal(["September "], getline(1,2))
172 %d
173
174 " <Enter> - Use the text and insert line break (state: 1)
175 call feedkeys("a\<f5>\<enter>\<esc>", 'tx')
176 call assert_equal(["January", ''], getline(1,2))
177 %d
178
179 " <Enter> - Insert the current selected text (state: 2)
180 call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx')
181 call assert_equal(["September"], getline(1,2))
182 %d
183
184 " Insert match immediately, if there is only one match
185 " <c-y> selects a character from the line above
186 call append(0, ["December2015"])
187 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
188 call assert_equal(["December2015", "December2015", ""], getline(1,3))
189 %d
190
Bram Moolenaar47247282016-08-02 22:36:02 +0200191 " use menuone for 'completeopt'
192 " Since for the first <c-y> the menu is still shown, will only select
193 " three letters from the line above
194 set completeopt&vim
195 set completeopt+=menuone
196 call append(0, ["December2015"])
197 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
198 call assert_equal(["December2015", "December201", ""], getline(1,3))
199 %d
200
201 " use longest for 'completeopt'
202 set completeopt&vim
203 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
204 set completeopt+=longest
205 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
206 call assert_equal(["M", "Ma", ""], getline(1,3))
207 %d
208
209 " use noselect/noinsert for 'completeopt'
210 set completeopt&vim
211 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
212 set completeopt+=noselect
213 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
214 set completeopt-=noselect completeopt+=noinsert
215 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
216 call assert_equal(["March", "M", "March"], getline(1,4))
217 %d
218endfu
219
220
Bram Moolenaar00672e12016-06-26 18:38:13 +0200221func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200222 new
223 inoremap <F5> <C-R>=ListMonths()<CR>
224
225 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
226 call assert_equal('February', getline(1))
227 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200228 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200229 let g:setting = 'noinsertmode'
230 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
231 call assert_equal('February', getline(1))
232 call assert_false(pumvisible())
233 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200234 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200235 let g:setting = ''
236 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
237 call assert_equal('', getline(1))
238 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200239 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200240 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
241 call assert_equal('', getline(1))
242 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200243 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200244 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
245 call assert_equal('December', getline(1))
246
Bram Moolenaar67081e52016-07-09 21:49:03 +0200247 iunmap <F5>
248endfunc
249
Bram Moolenaar680c99b2018-06-07 15:18:41 +0200250" TODO: Fix what breaks after this line.
251" - Do not use "q!", it may exit Vim if there is an error
252finish
253
Bram Moolenaar67081e52016-07-09 21:49:03 +0200254func Test_noinsert_complete()
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200255 function! s:complTest1() abort
256 call complete(1, ['source', 'soundfold'])
257 return ''
258 endfunction
259
260 function! s:complTest2() abort
261 call complete(1, ['source', 'soundfold'])
262 return ''
263 endfunction
264
Bram Moolenaar67081e52016-07-09 21:49:03 +0200265 new
266 set completeopt+=noinsert
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200267 inoremap <F5> <C-R>=s:complTest1()<CR>
Bram Moolenaar67081e52016-07-09 21:49:03 +0200268 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
269 call assert_equal('soundfold', getline(1))
270 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200271 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200272
273 new
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200274 inoremap <F5> <C-R>=s:complTest2()<CR>
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200275 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
276 call assert_equal('source', getline(1))
277 bwipe!
278
Bram Moolenaar67081e52016-07-09 21:49:03 +0200279 set completeopt-=noinsert
280 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200281endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200282
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200283func Test_compl_vim_cmds_after_register_expr()
284 function! s:test_func()
285 return 'autocmd '
286 endfunction
287 augroup AAAAA_Group
288 au!
289 augroup END
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200290
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200291 new
292 call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx')
293 call assert_equal('autocmd AAAAA_Group', getline(1))
294 autocmd! AAAAA_Group
295 augroup! AAAAA_Group
296 bwipe!
297endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200298
Bram Moolenaar472e8592016-10-15 17:06:47 +0200299func DummyCompleteOne(findstart, base)
300 if a:findstart
301 return 0
302 else
303 wincmd n
304 return ['onedef', 'oneDEF']
305 endif
306endfunc
307
308" Test that nothing happens if the 'completefunc' opens
309" a new window (no completion, no crash)
310func Test_completefunc_opens_new_window_one()
311 new
312 let winid = win_getid()
313 setlocal completefunc=DummyCompleteOne
314 call setline(1, 'one')
315 /^one
316 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:')
317 call assert_notequal(winid, win_getid())
318 q!
319 call assert_equal(winid, win_getid())
320 call assert_equal('', getline(1))
321 q!
322endfunc
323
324" Test that nothing happens if the 'completefunc' opens
325" a new window (no completion, no crash)
326func DummyCompleteTwo(findstart, base)
327 if a:findstart
328 wincmd n
329 return 0
330 else
331 return ['twodef', 'twoDEF']
332 endif
333endfunction
334
335" Test that nothing happens if the 'completefunc' opens
336" a new window (no completion, no crash)
337func Test_completefunc_opens_new_window_two()
338 new
339 let winid = win_getid()
340 setlocal completefunc=DummyCompleteTwo
341 call setline(1, 'two')
342 /^two
343 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:')
344 call assert_notequal(winid, win_getid())
345 q!
346 call assert_equal(winid, win_getid())
347 call assert_equal('two', getline(1))
348 q!
349endfunc
350
351func DummyCompleteThree(findstart, base)
352 if a:findstart
353 return 0
354 else
355 return ['threedef', 'threeDEF']
356 endif
357endfunc
358
359:"Test that 'completefunc' works when it's OK.
360func Test_completefunc_works()
361 new
362 let winid = win_getid()
363 setlocal completefunc=DummyCompleteThree
364 call setline(1, 'three')
365 /^three
366 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
367 call assert_equal(winid, win_getid())
368 call assert_equal('threeDEF', getline(1))
369 q!
370endfunc
371
372func DummyCompleteFour(findstart, base)
373 if a:findstart
374 return 0
375 else
376 call complete_add('four1')
377 call complete_add('four2')
378 call complete_check()
379 call complete_add('four3')
380 call complete_add('four4')
381 call complete_check()
382 call complete_add('four5')
383 call complete_add('four6')
384 return []
385 endif
386endfunc
387
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200388" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200389func Test_omnifunc_with_check()
390 new
391 setlocal omnifunc=DummyCompleteFour
392 call setline(1, 'four')
393 /^four
394 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
395 call assert_equal('four2', getline(1))
396
397 call setline(1, 'four')
398 /^four
399 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
400 call assert_equal('four3', getline(1))
401
402 call setline(1, 'four')
403 /^four
404 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
405 call assert_equal('four5', getline(1))
406
407 q!
408endfunc
409
Bram Moolenaar869e3522016-10-16 15:35:47 +0200410function UndoComplete()
411 call complete(1, ['January', 'February', 'March',
412 \ 'April', 'May', 'June', 'July', 'August', 'September',
413 \ 'October', 'November', 'December'])
414 return ''
415endfunc
416
417" Test that no undo item is created when no completion is inserted
418func Test_complete_no_undo()
419 set completeopt=menu,preview,noinsert,noselect
420 inoremap <Right> <C-R>=UndoComplete()<CR>
421 new
422 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
423 call feedkeys("iaaa\<Esc>0", 'xt')
424 call assert_equal('aaa', getline(2))
425 call feedkeys("i\<Right>\<Esc>", 'xt')
426 call assert_equal('aaa', getline(2))
427 call feedkeys("u", 'xt')
428 call assert_equal('', getline(2))
429
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200430 call feedkeys("ibbb\<Esc>0", 'xt')
431 call assert_equal('bbb', getline(2))
432 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
433 call assert_equal('January', getline(2))
434 call feedkeys("u", 'xt')
435 call assert_equal('bbb', getline(2))
436
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200437 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
438 call assert_equal('January', getline(2))
439 call feedkeys("u", 'xt')
440 call assert_equal('bbb', getline(2))
441
Bram Moolenaar869e3522016-10-16 15:35:47 +0200442 iunmap <Right>
443 set completeopt&
444 q!
445endfunc
446
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200447function! DummyCompleteFive(findstart, base)
448 if a:findstart
449 return 0
450 else
451 return [
452 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
453 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
454 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
455 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
456 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
457 \ ]
458 endif
459endfunc
460
461" Test that 'completefunc' on Scratch buffer with preview window works when
462" it's OK.
463func Test_completefunc_with_scratch_buffer()
464 new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile
465 set completeopt+=preview
466 setlocal completefunc=DummyCompleteFive
467 call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
468 call assert_equal(['April'], getline(1, '$'))
469 pclose
470 q!
471 set completeopt&
472endfunc
Bram Moolenaar869e3522016-10-16 15:35:47 +0200473
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100474" <C-E> - select original typed text before the completion started without
475" auto-wrap text.
476func Test_completion_ctrl_e_without_autowrap()
477 new
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100478 let tw_save = &tw
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100479 set tw=78
480 let li = [
481 \ '" zzz',
482 \ '" zzzyyyyyyyyyyyyyyyyyyy']
483 call setline(1, li)
484 0
485 call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx")
486 call assert_equal(li, getline(1, '$'))
487
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100488 let &tw = tw_save
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100489 q!
490endfunc
491
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100492function! DummyCompleteSix()
493 call complete(1, ['Hello', 'World'])
494 return ''
495endfunction
496
497" complete() correctly clears the list of autocomplete candidates
498" See #1411
499func Test_completion_clear_candidate_list()
500 new
501 %d
502 " select first entry from the completion popup
503 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx")
504 call assert_equal('Hello', getline(1))
505 %d
506 " select second entry from the completion popup
507 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx")
508 call assert_equal('World', getline(1))
509 %d
510 " select original text
511 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx")
512 call assert_equal(' xxx', getline(1))
513 %d
514 " back at first entry from completion list
515 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx")
516 call assert_equal('Hello', getline(1))
517
518 bw!
519endfunc
520
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100521func Test_completion_respect_bs_option()
522 new
523 let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"]
524
525 set bs=indent,eol
526 call setline(1, li)
527 1
528 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
529 call assert_equal('aaa', getline(1))
530
531 %d
532 set bs=indent,eol,start
533 call setline(1, li)
534 1
535 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
536 call assert_equal('', getline(1))
537
538 bw!
539endfunc
540
Bram Moolenaard56a79d2017-02-19 15:26:18 +0100541func CompleteUndo() abort
542 call complete(1, g:months)
543 return ''
544endfunc
545
546func Test_completion_can_undo()
547 inoremap <Right> <c-r>=CompleteUndo()<cr>
548 set completeopt+=noinsert,noselect
549
550 new
551 call feedkeys("a\<Right>a\<Esc>", 'xt')
552 call assert_equal('a', getline(1))
553 undo
554 call assert_equal('', getline(1))
555
556 bwipe!
557 set completeopt&
558 iunmap <Right>
559endfunc
560
Bram Moolenaard099e032017-02-21 23:00:36 +0100561func Test_completion_comment_formatting()
562 new
563 setl formatoptions=tcqro
564 call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx')
565 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
566 %d
567 call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx')
568 call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4))
569 %d
570 try
571 call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx')
Bram Moolenaar37175402017-03-18 20:18:45 +0100572 call assert_report('completefunc not set, should have failed')
Bram Moolenaard099e032017-02-21 23:00:36 +0100573 catch
574 call assert_exception('E764:')
575 endtry
576 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
577 bwipe!
578endfunc
579
Bram Moolenaar4475b622017-05-01 20:46:52 +0200580fun MessCompleteMonths()
581 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep")
582 call complete_add(m)
583 if complete_check()
584 break
585 endif
586 endfor
587 return []
588endfun
589
590fun MessCompleteMore()
591 call complete(1, split("Oct Nov Dec"))
592 return []
593endfun
594
595fun MessComplete(findstart, base)
596 if a:findstart
597 let line = getline('.')
598 let start = col('.') - 1
599 while start > 0 && line[start - 1] =~ '\a'
600 let start -= 1
601 endwhile
602 return start
603 else
604 call MessCompleteMonths()
605 call MessCompleteMore()
606 return []
607 endif
608endf
609
610func Test_complete_func_mess()
611 " Calling complete() after complete_add() in 'completefunc' is wrong, but it
612 " should not crash.
613 set completefunc=MessComplete
614 new
615 call setline(1, 'Ju')
616 call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx')
617 call assert_equal('Oct/Oct', getline(1))
618 bwipe!
619 set completefunc=
620endfunc
621
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200622func Test_complete_CTRLN_startofbuffer()
623 new
624 call setline(1, [ 'organize(cupboard, 3, 2);',
625 \ 'prioritize(bureau, 8, 7);',
626 \ 'realize(bannister, 4, 4);',
627 \ 'moralize(railing, 3,9);'])
628 let expected=['cupboard.organize(3, 2);',
629 \ 'bureau.prioritize(8, 7);',
630 \ 'bannister.realize(4, 4);',
631 \ 'railing.moralize(3,9);']
632 call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx')
633 call assert_equal(expected, getline(1,'$'))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200634 bwipe!
635endfunc
636
637func Test_popup_and_window_resize()
638 if !has('terminal') || has('gui_running')
639 return
640 endif
641 let h = winheight(0)
642 if h < 15
643 return
644 endif
Bram Moolenaarb3158762017-11-02 17:50:14 +0100645 let rows = h / 3
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100646 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows})
647 call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>")
Bram Moolenaarb3158762017-11-02 17:50:14 +0100648 " Wait for the nested Vim to exit insert mode, where it will show the ruler.
649 " Need to trigger a redraw.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100650 call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'})
Bram Moolenaarb3158762017-11-02 17:50:14 +0100651
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100652 call term_sendkeys(buf, "Gi\<c-x>")
653 call term_sendkeys(buf, "\<c-v>")
654 call term_wait(buf, 100)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200655 " popup first entry "!" must be at the top
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200656 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200657 exe 'resize +' . (h - 1)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100658 call term_wait(buf, 100)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200659 redraw!
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200660 " popup shifted down, first line is now empty
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200661 call WaitForAssert({-> assert_equal('', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200662 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200663 " popup is below cursor line and shows first match "!"
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200664 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))})
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200665 " cursor line also shows !
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100666 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200667 bwipe!
668endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200669
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200670func Test_popup_and_preview_autocommand()
671 " This used to crash Vim
672 if !has('python')
673 return
674 endif
675 let h = winheight(0)
676 if h < 15
677 return
678 endif
679 new
680 augroup MyBufAdd
681 au!
682 au BufAdd * nested tab sball
683 augroup END
684 set omnifunc=pythoncomplete#Complete
685 call setline(1, 'import os')
686 " make the line long
687 call setline(2, ' os.')
688 $
689 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<enter>\<esc>", 'tx')
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200690 call assert_equal("import os", getline(1))
691 call assert_match(' os.\(EX_IOERR\|O_CREAT\)$', getline(2))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200692 call assert_equal(1, winnr('$'))
693 " previewwindow option is not set
694 call assert_equal(0, &previewwindow)
695 norm! gt
696 call assert_equal(0, &previewwindow)
697 norm! gT
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100698 call assert_equal(10, tabpagenr('$'))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200699 tabonly
700 pclose
701 augroup MyBufAdd
702 au!
703 augroup END
704 augroup! MyBufAdd
705 bw!
706endfunc
707
Bram Moolenaar246fe032017-11-19 19:56:27 +0100708func Test_balloon_split()
Bram Moolenaar7221fce2017-11-19 20:32:49 +0100709 if !exists('*balloon_split')
710 return
711 endif
Bram Moolenaar246fe032017-11-19 19:56:27 +0100712 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100713 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
714 \ ], balloon_split(
715 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
716 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100717 \ 'one two three four one two three four one two thre',
718 \ 'e four',
719 \ ], balloon_split(
720 \ 'one two three four one two three four one two three four'))
721
722 call assert_equal([
723 \ 'struct = {',
724 \ ' one = 1,',
725 \ ' two = 2,',
726 \ ' three = 3}',
727 \ ], balloon_split(
728 \ 'struct = {one = 1, two = 2, three = 3}'))
729
730 call assert_equal([
731 \ 'struct = {',
732 \ ' one = 1,',
733 \ ' nested = {',
734 \ ' n1 = "yes",',
735 \ ' n2 = "no"}',
736 \ ' two = 2}',
737 \ ], balloon_split(
738 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
739 call assert_equal([
740 \ 'struct = 0x234 {',
741 \ ' long = 2343 "\\"some long string that will be wr',
742 \ 'apped in two\\"",',
743 \ ' next = 123}',
744 \ ], balloon_split(
745 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
746endfunc
747
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100748func Test_popup_position()
749 if !CanRunVimInTerminal()
750 return
751 endif
752 call writefile([
753 \ '123456789_123456789_123456789_a',
754 \ '123456789_123456789_123456789_b',
755 \ ' 123',
756 \ ], 'Xtest')
757 let buf = RunVimInTerminal('Xtest', {})
758 call term_sendkeys(buf, ":vsplit\<CR>")
759
760 " default pumwidth in left window: overlap in right window
761 call term_sendkeys(buf, "GA\<C-N>")
762 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
763 call term_sendkeys(buf, "\<Esc>u")
764
765 " default pumwidth: fill until right of window
766 call term_sendkeys(buf, "\<C-W>l")
767 call term_sendkeys(buf, "GA\<C-N>")
768 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
769
770 " larger pumwidth: used as minimum width
771 call term_sendkeys(buf, "\<Esc>u")
772 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
773 call term_sendkeys(buf, "GA\<C-N>")
774 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
775
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100776 " completed text wider than the window and 'pumwidth' smaller than available
777 " space
778 call term_sendkeys(buf, "\<Esc>u")
779 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
780 call term_sendkeys(buf, "ggI123456789_\<Esc>")
781 call term_sendkeys(buf, "jI123456789_\<Esc>")
782 call term_sendkeys(buf, "GA\<C-N>")
783 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
784
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100785 call term_sendkeys(buf, "\<Esc>u")
786 call StopVimInTerminal(buf)
787 call delete('Xtest')
788endfunc
789
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100790func Test_popup_command()
791 if !CanRunVimInTerminal() || !has('menu')
792 return
793 endif
794
795 call writefile([
796 \ 'one two three four five',
797 \ 'and one two Xthree four five',
798 \ 'one more two three four five',
799 \ ], 'Xtest')
800 let buf = RunVimInTerminal('Xtest', {})
801 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
802 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
803 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
804
805 " Select a word
806 call term_sendkeys(buf, "jj")
807 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
808
809 " Select a word
810 call term_sendkeys(buf, "j\<CR>")
811 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
812
813 call term_sendkeys(buf, "\<Esc>")
814 call StopVimInTerminal(buf)
815 call delete('Xtest')
816endfunc
817
Bram Moolenaare87edf32018-04-17 22:14:32 +0200818func Test_popup_complete_backwards()
819 new
820 call setline(1, ['Post', 'Port', 'Po'])
821 let expected=['Post', 'Port', 'Port']
822 call cursor(3,2)
823 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<cr>", 'tx')
824 call assert_equal(expected, getline(1,'$'))
825 bwipe!
826endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100827
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200828func Test_popup_complete_backwards_ctrl_p()
829 new
830 call setline(1, ['Post', 'Port', 'Po'])
831 let expected=['Post', 'Port', 'Port']
832 call cursor(3,2)
833 call feedkeys("A\<C-P>\<C-N>rt\<cr>", 'tx')
834 call assert_equal(expected, getline(1,'$'))
835 bwipe!
836endfunc
837
Bram Moolenaar39de9522018-05-08 22:48:00 +0200838fun! Test_complete_o_tab()
839 let s:o_char_pressed = 0
840
841 fun! s:act_on_text_changed()
842 if s:o_char_pressed
843 let s:o_char_pressed = 0
844 call feedkeys("\<c-x>\<c-n>", 'i')
845 endif
846 endf
847
848 set completeopt=menu,noselect
849 new
850 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
851 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
852 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
853 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
854 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
855 call cursor(4,1)
856 call test_override("char_avail", 1)
857 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
858 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
859 call assert_equal(l:expected, getline(1,'$'))
860
861 call test_override("char_avail", 0)
862 bwipe!
863 set completeopt&
864 delfunc s:act_on_text_changed
865endf
866
867
Bram Moolenaar47247282016-08-02 22:36:02 +0200868" vim: shiftwidth=2 sts=2 expandtab