blob: fdd51de51cb60f53952fceb9b57001f152cdbedb [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
4
Bram Moolenaar00672e12016-06-26 18:38:13 +02005let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
6let g:setting = ''
7
Bram Moolenaar47247282016-08-02 22:36:02 +02008func! ListMonths()
9 if g:setting != ''
10 exe ":set" g:setting
11 endif
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +010012 let mth = copy(g:months)
Bram Moolenaar47247282016-08-02 22:36:02 +020013 let entered = strcharpart(getline('.'),0,col('.'))
14 if !empty(entered)
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +010015 let mth = filter(mth, 'v:val=~"^".entered')
Bram Moolenaar47247282016-08-02 22:36:02 +020016 endif
17 call complete(1, mth)
18 return ''
Bram Moolenaar00672e12016-06-26 18:38:13 +020019endfunc
20
Bram Moolenaardac19472016-09-03 22:35:40 +020021func! Test_popup_complete2()
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020022 " Although the popupmenu is not visible, this does not mean completion mode
23 " has ended. After pressing <f5> to complete the currently typed char, Vim
24 " still stays in the first state of the completion (:h ins-completion-menu),
25 " although the popupmenu wasn't shown <c-e> will remove the inserted
26 " completed text (:h complete_CTRL-E), while the following <c-e> will behave
27 " like expected (:h i_CTRL-E)
Bram Moolenaardac19472016-09-03 22:35:40 +020028 new
29 inoremap <f5> <c-r>=ListMonths()<cr>
30 call append(1, ["December2015"])
31 :1
32 call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx')
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020033 call assert_equal(["Dece", "", "December2015"], getline(1,3))
Bram Moolenaardac19472016-09-03 22:35:40 +020034 %d
35 bw!
36endfu
37
Bram Moolenaar47247282016-08-02 22:36:02 +020038func! Test_popup_complete()
39 new
40 inoremap <f5> <c-r>=ListMonths()<cr>
Bram Moolenaara5e66212017-09-29 22:42:33 +020041 set belloff=all
Bram Moolenaar47247282016-08-02 22:36:02 +020042
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
Bram Moolenaara5e66212017-09-29 22:42:33 +0200218 set belloff&
Bram Moolenaar47247282016-08-02 22:36:02 +0200219endfu
220
221
Bram Moolenaar00672e12016-06-26 18:38:13 +0200222func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200223 new
224 inoremap <F5> <C-R>=ListMonths()<CR>
225
226 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
227 call assert_equal('February', getline(1))
228 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200229 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200230 let g:setting = 'noinsertmode'
231 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
232 call assert_equal('February', getline(1))
233 call assert_false(pumvisible())
234 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200235 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200236 let g:setting = ''
237 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
238 call assert_equal('', getline(1))
239 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200240 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200241 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
242 call assert_equal('', getline(1))
243 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200244 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200245 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
246 call assert_equal('December', getline(1))
247
Bram Moolenaar67081e52016-07-09 21:49:03 +0200248 iunmap <F5>
249endfunc
250
Bram Moolenaar67081e52016-07-09 21:49:03 +0200251func Test_noinsert_complete()
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200252 function! s:complTest1() abort
253 call complete(1, ['source', 'soundfold'])
254 return ''
255 endfunction
256
257 function! s:complTest2() abort
258 call complete(1, ['source', 'soundfold'])
259 return ''
260 endfunction
261
Bram Moolenaar67081e52016-07-09 21:49:03 +0200262 new
263 set completeopt+=noinsert
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200264 inoremap <F5> <C-R>=s:complTest1()<CR>
Bram Moolenaar67081e52016-07-09 21:49:03 +0200265 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
266 call assert_equal('soundfold', getline(1))
267 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200268 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200269
270 new
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200271 inoremap <F5> <C-R>=s:complTest2()<CR>
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200272 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
273 call assert_equal('source', getline(1))
274 bwipe!
275
Bram Moolenaar67081e52016-07-09 21:49:03 +0200276 set completeopt-=noinsert
277 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200278endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200279
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200280func Test_compl_vim_cmds_after_register_expr()
281 function! s:test_func()
282 return 'autocmd '
283 endfunction
284 augroup AAAAA_Group
285 au!
286 augroup END
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200287
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200288 new
289 call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx')
290 call assert_equal('autocmd AAAAA_Group', getline(1))
291 autocmd! AAAAA_Group
292 augroup! AAAAA_Group
293 bwipe!
294endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200295
Bram Moolenaar472e8592016-10-15 17:06:47 +0200296func DummyCompleteOne(findstart, base)
297 if a:findstart
298 return 0
299 else
300 wincmd n
301 return ['onedef', 'oneDEF']
302 endif
303endfunc
304
305" Test that nothing happens if the 'completefunc' opens
306" a new window (no completion, no crash)
307func Test_completefunc_opens_new_window_one()
308 new
309 let winid = win_getid()
310 setlocal completefunc=DummyCompleteOne
311 call setline(1, 'one')
312 /^one
313 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:')
314 call assert_notequal(winid, win_getid())
315 q!
316 call assert_equal(winid, win_getid())
317 call assert_equal('', getline(1))
318 q!
319endfunc
320
321" Test that nothing happens if the 'completefunc' opens
322" a new window (no completion, no crash)
323func DummyCompleteTwo(findstart, base)
324 if a:findstart
325 wincmd n
326 return 0
327 else
328 return ['twodef', 'twoDEF']
329 endif
330endfunction
331
332" Test that nothing happens if the 'completefunc' opens
333" a new window (no completion, no crash)
334func Test_completefunc_opens_new_window_two()
335 new
336 let winid = win_getid()
337 setlocal completefunc=DummyCompleteTwo
338 call setline(1, 'two')
339 /^two
340 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:')
341 call assert_notequal(winid, win_getid())
342 q!
343 call assert_equal(winid, win_getid())
344 call assert_equal('two', getline(1))
345 q!
346endfunc
347
348func DummyCompleteThree(findstart, base)
349 if a:findstart
350 return 0
351 else
352 return ['threedef', 'threeDEF']
353 endif
354endfunc
355
356:"Test that 'completefunc' works when it's OK.
357func Test_completefunc_works()
358 new
359 let winid = win_getid()
360 setlocal completefunc=DummyCompleteThree
361 call setline(1, 'three')
362 /^three
363 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
364 call assert_equal(winid, win_getid())
365 call assert_equal('threeDEF', getline(1))
366 q!
367endfunc
368
369func DummyCompleteFour(findstart, base)
370 if a:findstart
371 return 0
372 else
373 call complete_add('four1')
374 call complete_add('four2')
375 call complete_check()
376 call complete_add('four3')
377 call complete_add('four4')
378 call complete_check()
379 call complete_add('four5')
380 call complete_add('four6')
381 return []
382 endif
383endfunc
384
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200385" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200386func Test_omnifunc_with_check()
387 new
388 setlocal omnifunc=DummyCompleteFour
389 call setline(1, 'four')
390 /^four
391 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
392 call assert_equal('four2', getline(1))
393
394 call setline(1, 'four')
395 /^four
396 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
397 call assert_equal('four3', getline(1))
398
399 call setline(1, 'four')
400 /^four
401 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
402 call assert_equal('four5', getline(1))
403
404 q!
405endfunc
406
Bram Moolenaar869e3522016-10-16 15:35:47 +0200407function UndoComplete()
408 call complete(1, ['January', 'February', 'March',
409 \ 'April', 'May', 'June', 'July', 'August', 'September',
410 \ 'October', 'November', 'December'])
411 return ''
412endfunc
413
414" Test that no undo item is created when no completion is inserted
415func Test_complete_no_undo()
416 set completeopt=menu,preview,noinsert,noselect
417 inoremap <Right> <C-R>=UndoComplete()<CR>
418 new
419 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
420 call feedkeys("iaaa\<Esc>0", 'xt')
421 call assert_equal('aaa', getline(2))
422 call feedkeys("i\<Right>\<Esc>", 'xt')
423 call assert_equal('aaa', getline(2))
424 call feedkeys("u", 'xt')
425 call assert_equal('', getline(2))
426
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200427 call feedkeys("ibbb\<Esc>0", 'xt')
428 call assert_equal('bbb', getline(2))
429 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
430 call assert_equal('January', getline(2))
431 call feedkeys("u", 'xt')
432 call assert_equal('bbb', getline(2))
433
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200434 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
435 call assert_equal('January', getline(2))
436 call feedkeys("u", 'xt')
437 call assert_equal('bbb', getline(2))
438
Bram Moolenaar869e3522016-10-16 15:35:47 +0200439 iunmap <Right>
440 set completeopt&
441 q!
442endfunc
443
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200444function! DummyCompleteFive(findstart, base)
445 if a:findstart
446 return 0
447 else
448 return [
449 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
450 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
451 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
452 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
453 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
454 \ ]
455 endif
456endfunc
457
458" Test that 'completefunc' on Scratch buffer with preview window works when
459" it's OK.
460func Test_completefunc_with_scratch_buffer()
461 new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile
462 set completeopt+=preview
463 setlocal completefunc=DummyCompleteFive
464 call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
465 call assert_equal(['April'], getline(1, '$'))
466 pclose
467 q!
468 set completeopt&
469endfunc
Bram Moolenaar869e3522016-10-16 15:35:47 +0200470
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100471" <C-E> - select original typed text before the completion started without
472" auto-wrap text.
473func Test_completion_ctrl_e_without_autowrap()
474 new
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100475 let tw_save = &tw
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100476 set tw=78
477 let li = [
478 \ '" zzz',
479 \ '" zzzyyyyyyyyyyyyyyyyyyy']
480 call setline(1, li)
481 0
482 call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx")
483 call assert_equal(li, getline(1, '$'))
484
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100485 let &tw = tw_save
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100486 q!
487endfunc
488
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100489function! DummyCompleteSix()
490 call complete(1, ['Hello', 'World'])
491 return ''
492endfunction
493
494" complete() correctly clears the list of autocomplete candidates
495" See #1411
496func Test_completion_clear_candidate_list()
497 new
498 %d
499 " select first entry from the completion popup
500 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx")
501 call assert_equal('Hello', getline(1))
502 %d
503 " select second entry from the completion popup
504 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx")
505 call assert_equal('World', getline(1))
506 %d
507 " select original text
508 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx")
509 call assert_equal(' xxx', getline(1))
510 %d
511 " back at first entry from completion list
512 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx")
513 call assert_equal('Hello', getline(1))
514
515 bw!
516endfunc
517
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100518func Test_completion_respect_bs_option()
519 new
Bram Moolenaara5e66212017-09-29 22:42:33 +0200520 set belloff=all
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100521 let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"]
522
523 set bs=indent,eol
524 call setline(1, li)
525 1
526 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
527 call assert_equal('aaa', getline(1))
528
529 %d
530 set bs=indent,eol,start
531 call setline(1, li)
532 1
533 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
534 call assert_equal('', getline(1))
535
Bram Moolenaara5e66212017-09-29 22:42:33 +0200536 set belloff&
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100537 bw!
538endfunc
539
Bram Moolenaard56a79d2017-02-19 15:26:18 +0100540func CompleteUndo() abort
541 call complete(1, g:months)
542 return ''
543endfunc
544
545func Test_completion_can_undo()
546 inoremap <Right> <c-r>=CompleteUndo()<cr>
547 set completeopt+=noinsert,noselect
548
549 new
550 call feedkeys("a\<Right>a\<Esc>", 'xt')
551 call assert_equal('a', getline(1))
552 undo
553 call assert_equal('', getline(1))
554
555 bwipe!
556 set completeopt&
557 iunmap <Right>
558endfunc
559
Bram Moolenaard099e032017-02-21 23:00:36 +0100560func Test_completion_comment_formatting()
561 new
562 setl formatoptions=tcqro
563 call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx')
564 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
565 %d
566 call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx')
567 call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4))
568 %d
569 try
570 call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx')
Bram Moolenaar37175402017-03-18 20:18:45 +0100571 call assert_report('completefunc not set, should have failed')
Bram Moolenaard099e032017-02-21 23:00:36 +0100572 catch
573 call assert_exception('E764:')
574 endtry
575 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
576 bwipe!
577endfunc
578
Bram Moolenaar4475b622017-05-01 20:46:52 +0200579fun MessCompleteMonths()
580 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep")
581 call complete_add(m)
582 if complete_check()
583 break
584 endif
585 endfor
586 return []
587endfun
588
589fun MessCompleteMore()
590 call complete(1, split("Oct Nov Dec"))
591 return []
592endfun
593
594fun MessComplete(findstart, base)
595 if a:findstart
596 let line = getline('.')
597 let start = col('.') - 1
598 while start > 0 && line[start - 1] =~ '\a'
599 let start -= 1
600 endwhile
601 return start
602 else
603 call MessCompleteMonths()
604 call MessCompleteMore()
605 return []
606 endif
607endf
608
609func Test_complete_func_mess()
610 " Calling complete() after complete_add() in 'completefunc' is wrong, but it
611 " should not crash.
612 set completefunc=MessComplete
613 new
614 call setline(1, 'Ju')
615 call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx')
616 call assert_equal('Oct/Oct', getline(1))
617 bwipe!
618 set completefunc=
619endfunc
620
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200621func Test_complete_CTRLN_startofbuffer()
622 new
Bram Moolenaara5e66212017-09-29 22:42:33 +0200623 set belloff=all
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200624 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 set belloff&
635 bwipe!
636endfunc
637
638func Test_popup_and_window_resize()
639 if !has('terminal') || has('gui_running')
640 return
641 endif
642 let h = winheight(0)
643 if h < 15
644 return
645 endif
646 let g:buf = term_start([$VIMPROG, '--clean', '-c', 'set noswapfile'], {'term_rows': h / 3})
647 call term_sendkeys(g:buf, (h / 3 - 1)."o\<esc>G")
648 call term_sendkeys(g:buf, "i\<c-x>")
Bram Moolenaar712549e2017-10-01 16:59:18 +0200649 call term_wait(g:buf, 200)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200650 call term_sendkeys(g:buf, "\<c-v>")
651 call term_wait(g:buf, 100)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200652 " popup first entry "!" must be at the top
Bram Moolenaarc79977a2017-09-30 14:39:27 +0200653 call WaitFor('term_getline(g:buf, 1) =~ "^!"')
Bram Moolenaara5e66212017-09-29 22:42:33 +0200654 call assert_match('^!\s*$', term_getline(g:buf, 1))
655 exe 'resize +' . (h - 1)
656 call term_wait(g:buf, 100)
657 redraw!
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200658 " popup shifted down, first line is now empty
Bram Moolenaarc79977a2017-09-30 14:39:27 +0200659 call WaitFor('term_getline(g:buf, 1) == ""')
Bram Moolenaara5e66212017-09-29 22:42:33 +0200660 call assert_equal('', term_getline(g:buf, 1))
661 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200662 " popup is below cursor line and shows first match "!"
Bram Moolenaarc79977a2017-09-30 14:39:27 +0200663 call WaitFor('term_getline(g:buf, term_getcursor(g:buf)[0] + 1) =~ "^!"')
Bram Moolenaara5e66212017-09-29 22:42:33 +0200664 call assert_match('^!\s*$', term_getline(g:buf, term_getcursor(g:buf)[0] + 1))
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200665 " cursor line also shows !
666 call assert_match('^!\s*$', term_getline(g:buf, term_getcursor(g:buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200667 bwipe!
668endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200669
Bram Moolenaar47247282016-08-02 22:36:02 +0200670" vim: shiftwidth=2 sts=2 expandtab