blob: d121affc7d71124ec88a2f937635fa1ce2877d4c [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>
41
42 " <C-E> - select original typed text before the completion started
43 call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx')
44 call assert_equal(["Ju"], getline(1,2))
45 %d
46
47 " <C-Y> - accept current match
48 call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx')
49 call assert_equal(["August"], getline(1,2))
50 %d
51
52 " <BS> - Delete one character from the inserted text (state: 1)
53 " TODO: This should not end the completion, but it does.
54 " This should according to the documentation:
55 " January
56 " but instead, this does
57 " Januar
58 " (idea is, C-L inserts the match from the popup menu
59 " but if the menu is closed, it will insert the character <c-l>
60 call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx')
61 call assert_equal(["Januar "], getline(1,2))
62 %d
63
64 " any-non special character: Stop completion without changing the match
65 " and insert the typed character
66 call feedkeys("a\<f5>20", 'tx')
67 call assert_equal(["January20"], getline(1,2))
68 %d
69
70 " any-non printable, non-white character: Add this character and
71 " reduce number of matches
72 call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx')
73 call assert_equal(["Jul"], getline(1,2))
74 %d
75
76 " any-non printable, non-white character: Add this character and
77 " reduce number of matches
78 call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx')
79 call assert_equal(["July"], getline(1,2))
80 %d
81
82 " any-non printable, non-white character: Add this character and
83 " reduce number of matches
84 call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx')
85 call assert_equal(["Jul"], getline(1,2))
86 %d
87
88 " <BS> - Delete one character from the inserted text (state: 2)
89 call feedkeys("a\<f5>\<c-n>\<bs>", 'tx')
90 call assert_equal(["Februar"], getline(1,2))
91 %d
92
93 " <c-l> - Insert one character from the current match
94 call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx')
95 call assert_equal(["J "], getline(1,2))
96 %d
97
98 " <c-l> - Insert one character from the current match
99 call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx')
100 call assert_equal(["January "], getline(1,2))
101 %d
102
103 " <c-y> - Accept current selected match
104 call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx')
105 call assert_equal(["January"], getline(1,2))
106 %d
107
108 " <c-e> - End completion, go back to what was there before selecting a match
109 call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx')
110 call assert_equal(["Ju"], getline(1,2))
111 %d
112
113 " <PageUp> - Select a match several entries back
114 call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx')
115 call assert_equal([""], getline(1,2))
116 %d
117
118 " <PageUp><PageUp> - Select a match several entries back
119 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
120 call assert_equal(["December"], getline(1,2))
121 %d
122
123 " <PageUp><PageUp><PageUp> - Select a match several entries back
124 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
125 call assert_equal(["February"], getline(1,2))
126 %d
127
128 " <PageDown> - Select a match several entries further
129 call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx')
130 call assert_equal(["November"], getline(1,2))
131 %d
132
133 " <PageDown><PageDown> - Select a match several entries further
134 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
135 call assert_equal(["December"], getline(1,2))
136 %d
137
138 " <PageDown><PageDown><PageDown> - Select a match several entries further
139 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
140 call assert_equal([""], getline(1,2))
141 %d
142
143 " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further
144 call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx')
145 call assert_equal(["October"], getline(1,2))
146 %d
147
148 " <Up> - Select a match don't insert yet
149 call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx')
150 call assert_equal([""], getline(1,2))
151 %d
152
153 " <Up><Up> - Select a match don't insert yet
154 call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx')
155 call assert_equal(["December"], getline(1,2))
156 %d
157
158 " <Up><Up><Up> - Select a match don't insert yet
159 call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx')
160 call assert_equal(["November"], getline(1,2))
161 %d
162
163 " <Tab> - Stop completion and insert the match
164 call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx')
165 call assert_equal(["January "], getline(1,2))
166 %d
167
168 " <Space> - Stop completion and insert the match
169 call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx')
170 call assert_equal(["September "], getline(1,2))
171 %d
172
173 " <Enter> - Use the text and insert line break (state: 1)
174 call feedkeys("a\<f5>\<enter>\<esc>", 'tx')
175 call assert_equal(["January", ''], getline(1,2))
176 %d
177
178 " <Enter> - Insert the current selected text (state: 2)
179 call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx')
180 call assert_equal(["September"], getline(1,2))
181 %d
182
183 " Insert match immediately, if there is only one match
184 " <c-y> selects a character from the line above
185 call append(0, ["December2015"])
186 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
187 call assert_equal(["December2015", "December2015", ""], getline(1,3))
188 %d
189
Bram Moolenaar47247282016-08-02 22:36:02 +0200190 " use menuone for 'completeopt'
191 " Since for the first <c-y> the menu is still shown, will only select
192 " three letters from the line above
193 set completeopt&vim
194 set completeopt+=menuone
195 call append(0, ["December2015"])
196 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
197 call assert_equal(["December2015", "December201", ""], getline(1,3))
198 %d
199
200 " use longest for 'completeopt'
201 set completeopt&vim
202 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
203 set completeopt+=longest
204 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
205 call assert_equal(["M", "Ma", ""], getline(1,3))
206 %d
207
208 " use noselect/noinsert for 'completeopt'
209 set completeopt&vim
210 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
211 set completeopt+=noselect
212 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
213 set completeopt-=noselect completeopt+=noinsert
214 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
215 call assert_equal(["March", "M", "March"], getline(1,4))
216 %d
217endfu
218
219
Bram Moolenaar00672e12016-06-26 18:38:13 +0200220func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200221 new
222 inoremap <F5> <C-R>=ListMonths()<CR>
223
224 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
225 call assert_equal('February', getline(1))
226 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200227 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200228 let g:setting = 'noinsertmode'
229 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
230 call assert_equal('February', getline(1))
231 call assert_false(pumvisible())
232 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200233 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200234 let g:setting = ''
235 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
236 call assert_equal('', getline(1))
237 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200238 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200239 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
240 call assert_equal('', getline(1))
241 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200242 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200243 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
244 call assert_equal('December', getline(1))
245
Bram Moolenaar67081e52016-07-09 21:49:03 +0200246 iunmap <F5>
247endfunc
248
Bram Moolenaar67081e52016-07-09 21:49:03 +0200249func Test_noinsert_complete()
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200250 function! s:complTest1() abort
251 call complete(1, ['source', 'soundfold'])
252 return ''
253 endfunction
254
255 function! s:complTest2() abort
256 call complete(1, ['source', 'soundfold'])
257 return ''
258 endfunction
259
Bram Moolenaar67081e52016-07-09 21:49:03 +0200260 new
261 set completeopt+=noinsert
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200262 inoremap <F5> <C-R>=s:complTest1()<CR>
Bram Moolenaar67081e52016-07-09 21:49:03 +0200263 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
264 call assert_equal('soundfold', getline(1))
265 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200266 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200267
268 new
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200269 inoremap <F5> <C-R>=s:complTest2()<CR>
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200270 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
271 call assert_equal('source', getline(1))
272 bwipe!
273
Bram Moolenaar67081e52016-07-09 21:49:03 +0200274 set completeopt-=noinsert
275 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200276endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200277
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200278func Test_compl_vim_cmds_after_register_expr()
279 function! s:test_func()
280 return 'autocmd '
281 endfunction
282 augroup AAAAA_Group
283 au!
284 augroup END
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200285
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200286 new
287 call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx')
288 call assert_equal('autocmd AAAAA_Group', getline(1))
289 autocmd! AAAAA_Group
290 augroup! AAAAA_Group
291 bwipe!
292endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200293
Bram Moolenaar472e8592016-10-15 17:06:47 +0200294func DummyCompleteOne(findstart, base)
295 if a:findstart
296 return 0
297 else
298 wincmd n
299 return ['onedef', 'oneDEF']
300 endif
301endfunc
302
303" Test that nothing happens if the 'completefunc' opens
304" a new window (no completion, no crash)
305func Test_completefunc_opens_new_window_one()
306 new
307 let winid = win_getid()
308 setlocal completefunc=DummyCompleteOne
309 call setline(1, 'one')
310 /^one
311 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:')
312 call assert_notequal(winid, win_getid())
313 q!
314 call assert_equal(winid, win_getid())
315 call assert_equal('', getline(1))
316 q!
317endfunc
318
319" Test that nothing happens if the 'completefunc' opens
320" a new window (no completion, no crash)
321func DummyCompleteTwo(findstart, base)
322 if a:findstart
323 wincmd n
324 return 0
325 else
326 return ['twodef', 'twoDEF']
327 endif
328endfunction
329
330" Test that nothing happens if the 'completefunc' opens
331" a new window (no completion, no crash)
332func Test_completefunc_opens_new_window_two()
333 new
334 let winid = win_getid()
335 setlocal completefunc=DummyCompleteTwo
336 call setline(1, 'two')
337 /^two
338 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:')
339 call assert_notequal(winid, win_getid())
340 q!
341 call assert_equal(winid, win_getid())
342 call assert_equal('two', getline(1))
343 q!
344endfunc
345
346func DummyCompleteThree(findstart, base)
347 if a:findstart
348 return 0
349 else
350 return ['threedef', 'threeDEF']
351 endif
352endfunc
353
354:"Test that 'completefunc' works when it's OK.
355func Test_completefunc_works()
356 new
357 let winid = win_getid()
358 setlocal completefunc=DummyCompleteThree
359 call setline(1, 'three')
360 /^three
361 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
362 call assert_equal(winid, win_getid())
363 call assert_equal('threeDEF', getline(1))
364 q!
365endfunc
366
367func DummyCompleteFour(findstart, base)
368 if a:findstart
369 return 0
370 else
371 call complete_add('four1')
372 call complete_add('four2')
373 call complete_check()
374 call complete_add('four3')
375 call complete_add('four4')
376 call complete_check()
377 call complete_add('four5')
378 call complete_add('four6')
379 return []
380 endif
381endfunc
382
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200383" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200384func Test_omnifunc_with_check()
385 new
386 setlocal omnifunc=DummyCompleteFour
387 call setline(1, 'four')
388 /^four
389 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
390 call assert_equal('four2', getline(1))
391
392 call setline(1, 'four')
393 /^four
394 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
395 call assert_equal('four3', getline(1))
396
397 call setline(1, 'four')
398 /^four
399 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
400 call assert_equal('four5', getline(1))
401
402 q!
403endfunc
404
Bram Moolenaar869e3522016-10-16 15:35:47 +0200405function UndoComplete()
406 call complete(1, ['January', 'February', 'March',
407 \ 'April', 'May', 'June', 'July', 'August', 'September',
408 \ 'October', 'November', 'December'])
409 return ''
410endfunc
411
412" Test that no undo item is created when no completion is inserted
413func Test_complete_no_undo()
414 set completeopt=menu,preview,noinsert,noselect
415 inoremap <Right> <C-R>=UndoComplete()<CR>
416 new
417 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
418 call feedkeys("iaaa\<Esc>0", 'xt')
419 call assert_equal('aaa', getline(2))
420 call feedkeys("i\<Right>\<Esc>", 'xt')
421 call assert_equal('aaa', getline(2))
422 call feedkeys("u", 'xt')
423 call assert_equal('', getline(2))
424
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200425 call feedkeys("ibbb\<Esc>0", 'xt')
426 call assert_equal('bbb', getline(2))
427 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
428 call assert_equal('January', getline(2))
429 call feedkeys("u", 'xt')
430 call assert_equal('bbb', getline(2))
431
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200432 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
433 call assert_equal('January', getline(2))
434 call feedkeys("u", 'xt')
435 call assert_equal('bbb', getline(2))
436
Bram Moolenaar869e3522016-10-16 15:35:47 +0200437 iunmap <Right>
438 set completeopt&
439 q!
440endfunc
441
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200442function! DummyCompleteFive(findstart, base)
443 if a:findstart
444 return 0
445 else
446 return [
447 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
448 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
449 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
450 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
451 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
452 \ ]
453 endif
454endfunc
455
456" Test that 'completefunc' on Scratch buffer with preview window works when
457" it's OK.
458func Test_completefunc_with_scratch_buffer()
459 new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile
460 set completeopt+=preview
461 setlocal completefunc=DummyCompleteFive
462 call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
463 call assert_equal(['April'], getline(1, '$'))
464 pclose
465 q!
466 set completeopt&
467endfunc
Bram Moolenaar869e3522016-10-16 15:35:47 +0200468
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100469" <C-E> - select original typed text before the completion started without
470" auto-wrap text.
471func Test_completion_ctrl_e_without_autowrap()
472 new
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100473 let tw_save = &tw
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100474 set tw=78
475 let li = [
476 \ '" zzz',
477 \ '" zzzyyyyyyyyyyyyyyyyyyy']
478 call setline(1, li)
479 0
480 call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx")
481 call assert_equal(li, getline(1, '$'))
482
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100483 let &tw = tw_save
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100484 q!
485endfunc
486
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100487function! DummyCompleteSix()
488 call complete(1, ['Hello', 'World'])
489 return ''
490endfunction
491
492" complete() correctly clears the list of autocomplete candidates
493" See #1411
494func Test_completion_clear_candidate_list()
495 new
496 %d
497 " select first entry from the completion popup
498 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx")
499 call assert_equal('Hello', getline(1))
500 %d
501 " select second entry from the completion popup
502 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx")
503 call assert_equal('World', getline(1))
504 %d
505 " select original text
506 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx")
507 call assert_equal(' xxx', getline(1))
508 %d
509 " back at first entry from completion list
510 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx")
511 call assert_equal('Hello', getline(1))
512
513 bw!
514endfunc
515
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100516func Test_completion_respect_bs_option()
517 new
518 let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"]
519
520 set bs=indent,eol
521 call setline(1, li)
522 1
523 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
524 call assert_equal('aaa', getline(1))
525
526 %d
527 set bs=indent,eol,start
528 call setline(1, li)
529 1
530 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
531 call assert_equal('', getline(1))
532
533 bw!
534endfunc
535
Bram Moolenaard56a79d2017-02-19 15:26:18 +0100536func CompleteUndo() abort
537 call complete(1, g:months)
538 return ''
539endfunc
540
541func Test_completion_can_undo()
542 inoremap <Right> <c-r>=CompleteUndo()<cr>
543 set completeopt+=noinsert,noselect
544
545 new
546 call feedkeys("a\<Right>a\<Esc>", 'xt')
547 call assert_equal('a', getline(1))
548 undo
549 call assert_equal('', getline(1))
550
551 bwipe!
552 set completeopt&
553 iunmap <Right>
554endfunc
555
Bram Moolenaard099e032017-02-21 23:00:36 +0100556func Test_completion_comment_formatting()
557 new
558 setl formatoptions=tcqro
559 call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx')
560 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
561 %d
562 call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx')
563 call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4))
564 %d
565 try
566 call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx')
Bram Moolenaar37175402017-03-18 20:18:45 +0100567 call assert_report('completefunc not set, should have failed')
Bram Moolenaard099e032017-02-21 23:00:36 +0100568 catch
569 call assert_exception('E764:')
570 endtry
571 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
572 bwipe!
573endfunc
574
Bram Moolenaar4475b622017-05-01 20:46:52 +0200575fun MessCompleteMonths()
576 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep")
577 call complete_add(m)
578 if complete_check()
579 break
580 endif
581 endfor
582 return []
583endfun
584
585fun MessCompleteMore()
586 call complete(1, split("Oct Nov Dec"))
587 return []
588endfun
589
590fun MessComplete(findstart, base)
591 if a:findstart
592 let line = getline('.')
593 let start = col('.') - 1
594 while start > 0 && line[start - 1] =~ '\a'
595 let start -= 1
596 endwhile
597 return start
598 else
599 call MessCompleteMonths()
600 call MessCompleteMore()
601 return []
602 endif
603endf
604
605func Test_complete_func_mess()
606 " Calling complete() after complete_add() in 'completefunc' is wrong, but it
607 " should not crash.
608 set completefunc=MessComplete
609 new
610 call setline(1, 'Ju')
611 call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx')
612 call assert_equal('Oct/Oct', getline(1))
613 bwipe!
614 set completefunc=
615endfunc
616
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200617func Test_complete_CTRLN_startofbuffer()
618 new
619 call setline(1, [ 'organize(cupboard, 3, 2);',
620 \ 'prioritize(bureau, 8, 7);',
621 \ 'realize(bannister, 4, 4);',
622 \ 'moralize(railing, 3,9);'])
623 let expected=['cupboard.organize(3, 2);',
624 \ 'bureau.prioritize(8, 7);',
625 \ 'bannister.realize(4, 4);',
626 \ 'railing.moralize(3,9);']
627 call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx')
628 call assert_equal(expected, getline(1,'$'))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200629 bwipe!
630endfunc
631
632func Test_popup_and_window_resize()
633 if !has('terminal') || has('gui_running')
634 return
635 endif
636 let h = winheight(0)
637 if h < 15
638 return
639 endif
Bram Moolenaarb3158762017-11-02 17:50:14 +0100640 let rows = h / 3
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100641 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows})
642 call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>")
Bram Moolenaarb3158762017-11-02 17:50:14 +0100643 " Wait for the nested Vim to exit insert mode, where it will show the ruler.
644 " Need to trigger a redraw.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100645 call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'})
Bram Moolenaarb3158762017-11-02 17:50:14 +0100646
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100647 call term_sendkeys(buf, "Gi\<c-x>")
648 call term_sendkeys(buf, "\<c-v>")
649 call term_wait(buf, 100)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200650 " popup first entry "!" must be at the top
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100651 call WaitFor({-> term_getline(buf, 1) =~ "^!"})
652 call assert_match('^!\s*$', term_getline(buf, 1))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200653 exe 'resize +' . (h - 1)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100654 call term_wait(buf, 100)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200655 redraw!
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200656 " popup shifted down, first line is now empty
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100657 call WaitFor({-> term_getline(buf, 1) == ""})
658 call assert_equal('', term_getline(buf, 1))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200659 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200660 " popup is below cursor line and shows first match "!"
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100661 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0] + 1) =~ "^!"})
662 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200663 " cursor line also shows !
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200665 bwipe!
666endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200667
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200668func Test_popup_and_preview_autocommand()
669 " This used to crash Vim
670 if !has('python')
671 return
672 endif
673 let h = winheight(0)
674 if h < 15
675 return
676 endif
677 new
678 augroup MyBufAdd
679 au!
680 au BufAdd * nested tab sball
681 augroup END
682 set omnifunc=pythoncomplete#Complete
683 call setline(1, 'import os')
684 " make the line long
685 call setline(2, ' os.')
686 $
687 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<enter>\<esc>", 'tx')
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200688 call assert_equal("import os", getline(1))
689 call assert_match(' os.\(EX_IOERR\|O_CREAT\)$', getline(2))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200690 call assert_equal(1, winnr('$'))
691 " previewwindow option is not set
692 call assert_equal(0, &previewwindow)
693 norm! gt
694 call assert_equal(0, &previewwindow)
695 norm! gT
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100696 call assert_equal(10, tabpagenr('$'))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200697 tabonly
698 pclose
699 augroup MyBufAdd
700 au!
701 augroup END
702 augroup! MyBufAdd
703 bw!
704endfunc
705
Bram Moolenaar246fe032017-11-19 19:56:27 +0100706func Test_balloon_split()
Bram Moolenaar7221fce2017-11-19 20:32:49 +0100707 if !exists('*balloon_split')
708 return
709 endif
Bram Moolenaar246fe032017-11-19 19:56:27 +0100710 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100711 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
712 \ ], balloon_split(
713 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
714 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100715 \ 'one two three four one two three four one two thre',
716 \ 'e four',
717 \ ], balloon_split(
718 \ 'one two three four one two three four one two three four'))
719
720 call assert_equal([
721 \ 'struct = {',
722 \ ' one = 1,',
723 \ ' two = 2,',
724 \ ' three = 3}',
725 \ ], balloon_split(
726 \ 'struct = {one = 1, two = 2, three = 3}'))
727
728 call assert_equal([
729 \ 'struct = {',
730 \ ' one = 1,',
731 \ ' nested = {',
732 \ ' n1 = "yes",',
733 \ ' n2 = "no"}',
734 \ ' two = 2}',
735 \ ], balloon_split(
736 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
737 call assert_equal([
738 \ 'struct = 0x234 {',
739 \ ' long = 2343 "\\"some long string that will be wr',
740 \ 'apped in two\\"",',
741 \ ' next = 123}',
742 \ ], balloon_split(
743 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
744endfunc
745
Bram Moolenaar47247282016-08-02 22:36:02 +0200746" vim: shiftwidth=2 sts=2 expandtab