blob: 287d59d9018d2dc46d9289e641a5f12e9fe9ef57 [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 Moolenaarae0f30b2018-06-12 15:22:43 +02009func ListMonths()
Bram Moolenaar47247282016-08-02 22:36:02 +020010 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 Moolenaarae0f30b2018-06-12 15:22:43 +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!
Bram Moolenaarae0f30b2018-06-12 15:22:43 +020037endfunc
Bram Moolenaardac19472016-09-03 22:35:40 +020038
Bram Moolenaarae0f30b2018-06-12 15:22:43 +020039func Test_popup_complete()
Bram Moolenaar47247282016-08-02 22:36:02 +020040 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
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200218endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200219
220
Bram Moolenaarae0f30b2018-06-12 15:22:43 +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 Moolenaar67081e52016-07-09 21:49:03 +0200250func Test_noinsert_complete()
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200251 func! s:complTest1() abort
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200252 call complete(1, ['source', 'soundfold'])
253 return ''
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200254 endfunc
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200255
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200256 func! s:complTest2() abort
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200257 call complete(1, ['source', 'soundfold'])
258 return ''
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200259 endfunc
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200260
Bram Moolenaar67081e52016-07-09 21:49:03 +0200261 new
262 set completeopt+=noinsert
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200263 inoremap <F5> <C-R>=s:complTest1()<CR>
Bram Moolenaar67081e52016-07-09 21:49:03 +0200264 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
265 call assert_equal('soundfold', getline(1))
266 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200267 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200268
269 new
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200270 inoremap <F5> <C-R>=s:complTest2()<CR>
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200271 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
272 call assert_equal('source', getline(1))
273 bwipe!
274
Bram Moolenaar67081e52016-07-09 21:49:03 +0200275 set completeopt-=noinsert
276 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200277endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200278
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200279func Test_compl_vim_cmds_after_register_expr()
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200280 func! s:test_func()
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200281 return 'autocmd '
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200282 endfunc
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200283 augroup AAAAA_Group
284 au!
285 augroup END
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200286
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200287 new
288 call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx')
289 call assert_equal('autocmd AAAAA_Group', getline(1))
290 autocmd! AAAAA_Group
291 augroup! AAAAA_Group
292 bwipe!
293endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200294
Bram Moolenaar472e8592016-10-15 17:06:47 +0200295func DummyCompleteOne(findstart, base)
296 if a:findstart
297 return 0
298 else
299 wincmd n
300 return ['onedef', 'oneDEF']
301 endif
302endfunc
303
304" Test that nothing happens if the 'completefunc' opens
305" a new window (no completion, no crash)
306func Test_completefunc_opens_new_window_one()
307 new
308 let winid = win_getid()
309 setlocal completefunc=DummyCompleteOne
310 call setline(1, 'one')
311 /^one
312 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:')
313 call assert_notequal(winid, win_getid())
314 q!
315 call assert_equal(winid, win_getid())
316 call assert_equal('', getline(1))
317 q!
318endfunc
319
320" Test that nothing happens if the 'completefunc' opens
321" a new window (no completion, no crash)
322func DummyCompleteTwo(findstart, base)
323 if a:findstart
324 wincmd n
325 return 0
326 else
327 return ['twodef', 'twoDEF']
328 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200329endfunc
Bram Moolenaar472e8592016-10-15 17:06:47 +0200330
331" Test that nothing happens if the 'completefunc' opens
332" a new window (no completion, no crash)
333func Test_completefunc_opens_new_window_two()
334 new
335 let winid = win_getid()
336 setlocal completefunc=DummyCompleteTwo
337 call setline(1, 'two')
338 /^two
339 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:')
340 call assert_notequal(winid, win_getid())
341 q!
342 call assert_equal(winid, win_getid())
343 call assert_equal('two', getline(1))
344 q!
345endfunc
346
347func DummyCompleteThree(findstart, base)
348 if a:findstart
349 return 0
350 else
351 return ['threedef', 'threeDEF']
352 endif
353endfunc
354
355:"Test that 'completefunc' works when it's OK.
356func Test_completefunc_works()
357 new
358 let winid = win_getid()
359 setlocal completefunc=DummyCompleteThree
360 call setline(1, 'three')
361 /^three
362 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
363 call assert_equal(winid, win_getid())
364 call assert_equal('threeDEF', getline(1))
365 q!
366endfunc
367
368func DummyCompleteFour(findstart, base)
369 if a:findstart
370 return 0
371 else
372 call complete_add('four1')
373 call complete_add('four2')
374 call complete_check()
375 call complete_add('four3')
376 call complete_add('four4')
377 call complete_check()
378 call complete_add('four5')
379 call complete_add('four6')
380 return []
381 endif
382endfunc
383
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200384" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200385func Test_omnifunc_with_check()
386 new
387 setlocal omnifunc=DummyCompleteFour
388 call setline(1, 'four')
389 /^four
390 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
391 call assert_equal('four2', getline(1))
392
393 call setline(1, 'four')
394 /^four
395 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
396 call assert_equal('four3', getline(1))
397
398 call setline(1, 'four')
399 /^four
400 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
401 call assert_equal('four5', getline(1))
402
403 q!
404endfunc
405
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200406func UndoComplete()
Bram Moolenaar869e3522016-10-16 15:35:47 +0200407 call complete(1, ['January', 'February', 'March',
408 \ 'April', 'May', 'June', 'July', 'August', 'September',
409 \ 'October', 'November', 'December'])
410 return ''
411endfunc
412
413" Test that no undo item is created when no completion is inserted
414func Test_complete_no_undo()
415 set completeopt=menu,preview,noinsert,noselect
416 inoremap <Right> <C-R>=UndoComplete()<CR>
417 new
418 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
419 call feedkeys("iaaa\<Esc>0", 'xt')
420 call assert_equal('aaa', getline(2))
421 call feedkeys("i\<Right>\<Esc>", 'xt')
422 call assert_equal('aaa', getline(2))
423 call feedkeys("u", 'xt')
424 call assert_equal('', getline(2))
425
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200426 call feedkeys("ibbb\<Esc>0", 'xt')
427 call assert_equal('bbb', getline(2))
428 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
429 call assert_equal('January', getline(2))
430 call feedkeys("u", 'xt')
431 call assert_equal('bbb', getline(2))
432
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200433 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
434 call assert_equal('January', getline(2))
435 call feedkeys("u", 'xt')
436 call assert_equal('bbb', getline(2))
437
Bram Moolenaar869e3522016-10-16 15:35:47 +0200438 iunmap <Right>
439 set completeopt&
440 q!
441endfunc
442
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200443func DummyCompleteFive(findstart, base)
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200444 if a:findstart
445 return 0
446 else
447 return [
448 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
449 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
450 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
451 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
452 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
453 \ ]
454 endif
455endfunc
456
457" Test that 'completefunc' on Scratch buffer with preview window works when
458" it's OK.
459func Test_completefunc_with_scratch_buffer()
460 new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile
461 set completeopt+=preview
462 setlocal completefunc=DummyCompleteFive
463 call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
464 call assert_equal(['April'], getline(1, '$'))
465 pclose
466 q!
467 set completeopt&
468endfunc
Bram Moolenaar869e3522016-10-16 15:35:47 +0200469
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100470" <C-E> - select original typed text before the completion started without
471" auto-wrap text.
472func Test_completion_ctrl_e_without_autowrap()
473 new
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100474 let tw_save = &tw
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100475 set tw=78
476 let li = [
477 \ '" zzz',
478 \ '" zzzyyyyyyyyyyyyyyyyyyy']
479 call setline(1, li)
480 0
481 call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx")
482 call assert_equal(li, getline(1, '$'))
483
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100484 let &tw = tw_save
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100485 q!
486endfunc
487
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200488func DummyCompleteSix()
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100489 call complete(1, ['Hello', 'World'])
490 return ''
491endfunction
492
493" complete() correctly clears the list of autocomplete candidates
494" See #1411
495func Test_completion_clear_candidate_list()
496 new
497 %d
498 " select first entry from the completion popup
499 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx")
500 call assert_equal('Hello', getline(1))
501 %d
502 " select second entry from the completion popup
503 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx")
504 call assert_equal('World', getline(1))
505 %d
506 " select original text
507 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx")
508 call assert_equal(' xxx', getline(1))
509 %d
510 " back at first entry from completion list
511 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx")
512 call assert_equal('Hello', getline(1))
513
514 bw!
515endfunc
516
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100517func Test_completion_respect_bs_option()
518 new
519 let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"]
520
521 set bs=indent,eol
522 call setline(1, li)
523 1
524 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
525 call assert_equal('aaa', getline(1))
526
527 %d
528 set bs=indent,eol,start
529 call setline(1, li)
530 1
531 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
532 call assert_equal('', getline(1))
533
534 bw!
535endfunc
536
Bram Moolenaard56a79d2017-02-19 15:26:18 +0100537func CompleteUndo() abort
538 call complete(1, g:months)
539 return ''
540endfunc
541
542func Test_completion_can_undo()
543 inoremap <Right> <c-r>=CompleteUndo()<cr>
544 set completeopt+=noinsert,noselect
545
546 new
547 call feedkeys("a\<Right>a\<Esc>", 'xt')
548 call assert_equal('a', getline(1))
549 undo
550 call assert_equal('', getline(1))
551
552 bwipe!
553 set completeopt&
554 iunmap <Right>
555endfunc
556
Bram Moolenaard099e032017-02-21 23:00:36 +0100557func Test_completion_comment_formatting()
558 new
559 setl formatoptions=tcqro
560 call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx')
561 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
562 %d
563 call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx')
564 call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4))
565 %d
566 try
567 call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx')
Bram Moolenaar37175402017-03-18 20:18:45 +0100568 call assert_report('completefunc not set, should have failed')
Bram Moolenaard099e032017-02-21 23:00:36 +0100569 catch
570 call assert_exception('E764:')
571 endtry
572 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
573 bwipe!
574endfunc
575
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200576func MessCompleteMonths()
Bram Moolenaar4475b622017-05-01 20:46:52 +0200577 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep")
578 call complete_add(m)
579 if complete_check()
580 break
581 endif
582 endfor
583 return []
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200584endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200585
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200586func MessCompleteMore()
Bram Moolenaar4475b622017-05-01 20:46:52 +0200587 call complete(1, split("Oct Nov Dec"))
588 return []
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200589endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200590
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200591func MessComplete(findstart, base)
Bram Moolenaar4475b622017-05-01 20:46:52 +0200592 if a:findstart
593 let line = getline('.')
594 let start = col('.') - 1
595 while start > 0 && line[start - 1] =~ '\a'
596 let start -= 1
597 endwhile
598 return start
599 else
600 call MessCompleteMonths()
601 call MessCompleteMore()
602 return []
603 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200604endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200605
606func Test_complete_func_mess()
607 " Calling complete() after complete_add() in 'completefunc' is wrong, but it
608 " should not crash.
609 set completefunc=MessComplete
610 new
611 call setline(1, 'Ju')
612 call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx')
613 call assert_equal('Oct/Oct', getline(1))
614 bwipe!
615 set completefunc=
616endfunc
617
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200618func Test_complete_CTRLN_startofbuffer()
619 new
620 call setline(1, [ 'organize(cupboard, 3, 2);',
621 \ 'prioritize(bureau, 8, 7);',
622 \ 'realize(bannister, 4, 4);',
623 \ 'moralize(railing, 3,9);'])
624 let expected=['cupboard.organize(3, 2);',
625 \ 'bureau.prioritize(8, 7);',
626 \ 'bannister.realize(4, 4);',
627 \ 'railing.moralize(3,9);']
628 call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx')
629 call assert_equal(expected, getline(1,'$'))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200630 bwipe!
631endfunc
632
633func Test_popup_and_window_resize()
634 if !has('terminal') || has('gui_running')
635 return
636 endif
637 let h = winheight(0)
638 if h < 15
639 return
640 endif
Bram Moolenaarb3158762017-11-02 17:50:14 +0100641 let rows = h / 3
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100642 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows})
643 call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>")
Bram Moolenaarb3158762017-11-02 17:50:14 +0100644 " Wait for the nested Vim to exit insert mode, where it will show the ruler.
645 " Need to trigger a redraw.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100646 call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'})
Bram Moolenaarb3158762017-11-02 17:50:14 +0100647
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100648 call term_sendkeys(buf, "Gi\<c-x>")
649 call term_sendkeys(buf, "\<c-v>")
650 call term_wait(buf, 100)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200651 " popup first entry "!" must be at the top
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200652 call WaitForAssert({-> 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 Moolenaar0e9d1ae2018-04-30 14:28:24 +0200657 call WaitForAssert({-> assert_equal('', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200658 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200659 " popup is below cursor line and shows first match "!"
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200660 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))})
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200661 " cursor line also shows !
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100662 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200663 bwipe!
664endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200665
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200666func Test_popup_and_preview_autocommand()
667 " This used to crash Vim
668 if !has('python')
669 return
670 endif
671 let h = winheight(0)
672 if h < 15
673 return
674 endif
675 new
676 augroup MyBufAdd
677 au!
678 au BufAdd * nested tab sball
679 augroup END
680 set omnifunc=pythoncomplete#Complete
681 call setline(1, 'import os')
682 " make the line long
683 call setline(2, ' os.')
684 $
685 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<enter>\<esc>", 'tx')
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200686 call assert_equal("import os", getline(1))
687 call assert_match(' os.\(EX_IOERR\|O_CREAT\)$', getline(2))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200688 call assert_equal(1, winnr('$'))
689 " previewwindow option is not set
690 call assert_equal(0, &previewwindow)
691 norm! gt
692 call assert_equal(0, &previewwindow)
693 norm! gT
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100694 call assert_equal(10, tabpagenr('$'))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200695 tabonly
696 pclose
697 augroup MyBufAdd
698 au!
699 augroup END
700 augroup! MyBufAdd
701 bw!
702endfunc
703
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100704func Test_popup_and_previewwindow_dump()
705 if !CanRunVimInTerminal()
706 return
707 endif
708 call writefile([
709 \ 'set previewheight=9',
710 \ 'silent! pedit',
711 \ 'call setline(1, map(repeat(["ab"], 10), "v:val. v:key"))',
712 \ 'exec "norm! G\<C-E>\<C-E>"',
713 \ ], 'Xscript')
714 let buf = RunVimInTerminal('-S Xscript', {})
715
716 " Test that popup and previewwindow do not overlap.
717 call term_sendkeys(buf, "o\<C-X>\<C-N>")
718 sleep 100m
719 call VerifyScreenDump(buf, 'Test_popup_and_previewwindow_01', {})
720
721 call term_sendkeys(buf, "\<Esc>u")
722 call StopVimInTerminal(buf)
723 call delete('Xscript')
724endfunc
725
Bram Moolenaar246fe032017-11-19 19:56:27 +0100726func Test_balloon_split()
Bram Moolenaar7221fce2017-11-19 20:32:49 +0100727 if !exists('*balloon_split')
728 return
729 endif
Bram Moolenaar246fe032017-11-19 19:56:27 +0100730 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100731 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
732 \ ], balloon_split(
733 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
734 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100735 \ 'one two three four one two three four one two thre',
736 \ 'e four',
737 \ ], balloon_split(
738 \ 'one two three four one two three four one two three four'))
739
740 call assert_equal([
741 \ 'struct = {',
742 \ ' one = 1,',
743 \ ' two = 2,',
744 \ ' three = 3}',
745 \ ], balloon_split(
746 \ 'struct = {one = 1, two = 2, three = 3}'))
747
748 call assert_equal([
749 \ 'struct = {',
750 \ ' one = 1,',
751 \ ' nested = {',
752 \ ' n1 = "yes",',
753 \ ' n2 = "no"}',
754 \ ' two = 2}',
755 \ ], balloon_split(
756 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
757 call assert_equal([
758 \ 'struct = 0x234 {',
759 \ ' long = 2343 "\\"some long string that will be wr',
760 \ 'apped in two\\"",',
761 \ ' next = 123}',
762 \ ], balloon_split(
763 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
764endfunc
765
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100766func Test_popup_position()
767 if !CanRunVimInTerminal()
768 return
769 endif
770 call writefile([
771 \ '123456789_123456789_123456789_a',
772 \ '123456789_123456789_123456789_b',
773 \ ' 123',
774 \ ], 'Xtest')
775 let buf = RunVimInTerminal('Xtest', {})
776 call term_sendkeys(buf, ":vsplit\<CR>")
777
778 " default pumwidth in left window: overlap in right window
779 call term_sendkeys(buf, "GA\<C-N>")
780 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
781 call term_sendkeys(buf, "\<Esc>u")
782
783 " default pumwidth: fill until right of window
784 call term_sendkeys(buf, "\<C-W>l")
785 call term_sendkeys(buf, "GA\<C-N>")
786 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
787
788 " larger pumwidth: used as minimum width
789 call term_sendkeys(buf, "\<Esc>u")
790 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
791 call term_sendkeys(buf, "GA\<C-N>")
792 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
793
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100794 " completed text wider than the window and 'pumwidth' smaller than available
795 " space
796 call term_sendkeys(buf, "\<Esc>u")
797 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
798 call term_sendkeys(buf, "ggI123456789_\<Esc>")
799 call term_sendkeys(buf, "jI123456789_\<Esc>")
800 call term_sendkeys(buf, "GA\<C-N>")
801 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
802
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100803 call term_sendkeys(buf, "\<Esc>u")
804 call StopVimInTerminal(buf)
805 call delete('Xtest')
806endfunc
807
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100808func Test_popup_command()
809 if !CanRunVimInTerminal() || !has('menu')
810 return
811 endif
812
813 call writefile([
814 \ 'one two three four five',
815 \ 'and one two Xthree four five',
816 \ 'one more two three four five',
817 \ ], 'Xtest')
818 let buf = RunVimInTerminal('Xtest', {})
819 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
820 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
821 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
822
823 " Select a word
824 call term_sendkeys(buf, "jj")
825 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
826
827 " Select a word
828 call term_sendkeys(buf, "j\<CR>")
829 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
830
831 call term_sendkeys(buf, "\<Esc>")
832 call StopVimInTerminal(buf)
833 call delete('Xtest')
834endfunc
835
Bram Moolenaare87edf32018-04-17 22:14:32 +0200836func Test_popup_complete_backwards()
837 new
838 call setline(1, ['Post', 'Port', 'Po'])
839 let expected=['Post', 'Port', 'Port']
840 call cursor(3,2)
841 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<cr>", 'tx')
842 call assert_equal(expected, getline(1,'$'))
843 bwipe!
844endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100845
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200846func Test_popup_complete_backwards_ctrl_p()
847 new
848 call setline(1, ['Post', 'Port', 'Po'])
849 let expected=['Post', 'Port', 'Port']
850 call cursor(3,2)
851 call feedkeys("A\<C-P>\<C-N>rt\<cr>", 'tx')
852 call assert_equal(expected, getline(1,'$'))
853 bwipe!
854endfunc
855
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200856func Test_complete_o_tab()
Bram Moolenaar39de9522018-05-08 22:48:00 +0200857 let s:o_char_pressed = 0
858
859 fun! s:act_on_text_changed()
860 if s:o_char_pressed
861 let s:o_char_pressed = 0
862 call feedkeys("\<c-x>\<c-n>", 'i')
863 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200864 endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200865
866 set completeopt=menu,noselect
867 new
868 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
869 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
870 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
871 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
872 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
873 call cursor(4,1)
874 call test_override("char_avail", 1)
875 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
876 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
877 call assert_equal(l:expected, getline(1,'$'))
878
879 call test_override("char_avail", 0)
880 bwipe!
881 set completeopt&
882 delfunc s:act_on_text_changed
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200883endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200884
885
Bram Moolenaar47247282016-08-02 22:36:02 +0200886" vim: shiftwidth=2 sts=2 expandtab