blob: 6c86d6936ae9de0e5e3db25682d601e501a905dd [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 Moolenaar8c5a2782019-08-07 23:07:07 +02005source check.vim
Bram Moolenaara5e66212017-09-29 22:42:33 +02006
Bram Moolenaar00672e12016-06-26 18:38:13 +02007let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
8let g:setting = ''
9
Bram Moolenaarae0f30b2018-06-12 15:22:43 +020010func ListMonths()
Bram Moolenaar47247282016-08-02 22:36:02 +020011 if g:setting != ''
12 exe ":set" g:setting
13 endif
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +010014 let mth = copy(g:months)
Bram Moolenaar47247282016-08-02 22:36:02 +020015 let entered = strcharpart(getline('.'),0,col('.'))
16 if !empty(entered)
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +010017 let mth = filter(mth, 'v:val=~"^".entered')
Bram Moolenaar47247282016-08-02 22:36:02 +020018 endif
19 call complete(1, mth)
20 return ''
Bram Moolenaar00672e12016-06-26 18:38:13 +020021endfunc
22
Bram Moolenaarae0f30b2018-06-12 15:22:43 +020023func Test_popup_complete2()
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020024 " Although the popupmenu is not visible, this does not mean completion mode
25 " has ended. After pressing <f5> to complete the currently typed char, Vim
26 " still stays in the first state of the completion (:h ins-completion-menu),
27 " although the popupmenu wasn't shown <c-e> will remove the inserted
28 " completed text (:h complete_CTRL-E), while the following <c-e> will behave
29 " like expected (:h i_CTRL-E)
Bram Moolenaardac19472016-09-03 22:35:40 +020030 new
31 inoremap <f5> <c-r>=ListMonths()<cr>
32 call append(1, ["December2015"])
33 :1
34 call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx')
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020035 call assert_equal(["Dece", "", "December2015"], getline(1,3))
Bram Moolenaardac19472016-09-03 22:35:40 +020036 %d
37 bw!
Bram Moolenaarae0f30b2018-06-12 15:22:43 +020038endfunc
Bram Moolenaardac19472016-09-03 22:35:40 +020039
Bram Moolenaarae0f30b2018-06-12 15:22:43 +020040func Test_popup_complete()
Bram Moolenaar47247282016-08-02 22:36:02 +020041 new
42 inoremap <f5> <c-r>=ListMonths()<cr>
43
44 " <C-E> - select original typed text before the completion started
45 call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx')
46 call assert_equal(["Ju"], getline(1,2))
47 %d
48
49 " <C-Y> - accept current match
50 call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx')
51 call assert_equal(["August"], getline(1,2))
52 %d
53
54 " <BS> - Delete one character from the inserted text (state: 1)
55 " TODO: This should not end the completion, but it does.
56 " This should according to the documentation:
57 " January
58 " but instead, this does
59 " Januar
60 " (idea is, C-L inserts the match from the popup menu
61 " but if the menu is closed, it will insert the character <c-l>
62 call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx')
63 call assert_equal(["Januar "], getline(1,2))
64 %d
65
66 " any-non special character: Stop completion without changing the match
67 " and insert the typed character
68 call feedkeys("a\<f5>20", 'tx')
69 call assert_equal(["January20"], getline(1,2))
70 %d
71
72 " any-non printable, non-white character: Add this character and
73 " reduce number of matches
74 call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx')
75 call assert_equal(["Jul"], getline(1,2))
76 %d
77
78 " any-non printable, non-white character: Add this character and
79 " reduce number of matches
80 call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx')
81 call assert_equal(["July"], getline(1,2))
82 %d
83
84 " any-non printable, non-white character: Add this character and
85 " reduce number of matches
86 call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx')
87 call assert_equal(["Jul"], getline(1,2))
88 %d
89
90 " <BS> - Delete one character from the inserted text (state: 2)
91 call feedkeys("a\<f5>\<c-n>\<bs>", 'tx')
92 call assert_equal(["Februar"], getline(1,2))
93 %d
94
95 " <c-l> - Insert one character from the current match
96 call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx')
97 call assert_equal(["J "], getline(1,2))
98 %d
99
100 " <c-l> - Insert one character from the current match
101 call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx')
102 call assert_equal(["January "], getline(1,2))
103 %d
104
105 " <c-y> - Accept current selected match
106 call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx')
107 call assert_equal(["January"], getline(1,2))
108 %d
109
110 " <c-e> - End completion, go back to what was there before selecting a match
111 call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx')
112 call assert_equal(["Ju"], getline(1,2))
113 %d
114
115 " <PageUp> - Select a match several entries back
116 call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx')
117 call assert_equal([""], getline(1,2))
118 %d
119
120 " <PageUp><PageUp> - Select a match several entries back
121 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
122 call assert_equal(["December"], getline(1,2))
123 %d
124
125 " <PageUp><PageUp><PageUp> - Select a match several entries back
126 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
127 call assert_equal(["February"], getline(1,2))
128 %d
129
130 " <PageDown> - Select a match several entries further
131 call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx')
132 call assert_equal(["November"], getline(1,2))
133 %d
134
135 " <PageDown><PageDown> - Select a match several entries further
136 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
137 call assert_equal(["December"], getline(1,2))
138 %d
139
140 " <PageDown><PageDown><PageDown> - Select a match several entries further
141 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
142 call assert_equal([""], getline(1,2))
143 %d
144
145 " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further
146 call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx')
147 call assert_equal(["October"], getline(1,2))
148 %d
149
150 " <Up> - Select a match don't insert yet
151 call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx')
152 call assert_equal([""], getline(1,2))
153 %d
154
155 " <Up><Up> - Select a match don't insert yet
156 call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx')
157 call assert_equal(["December"], getline(1,2))
158 %d
159
160 " <Up><Up><Up> - Select a match don't insert yet
161 call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx')
162 call assert_equal(["November"], getline(1,2))
163 %d
164
165 " <Tab> - Stop completion and insert the match
166 call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx')
167 call assert_equal(["January "], getline(1,2))
168 %d
169
170 " <Space> - Stop completion and insert the match
171 call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx')
172 call assert_equal(["September "], getline(1,2))
173 %d
174
175 " <Enter> - Use the text and insert line break (state: 1)
176 call feedkeys("a\<f5>\<enter>\<esc>", 'tx')
177 call assert_equal(["January", ''], getline(1,2))
178 %d
179
180 " <Enter> - Insert the current selected text (state: 2)
181 call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx')
182 call assert_equal(["September"], getline(1,2))
183 %d
184
185 " Insert match immediately, if there is only one match
186 " <c-y> selects a character from the line above
187 call append(0, ["December2015"])
188 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
189 call assert_equal(["December2015", "December2015", ""], getline(1,3))
190 %d
191
Bram Moolenaar47247282016-08-02 22:36:02 +0200192 " use menuone for 'completeopt'
193 " Since for the first <c-y> the menu is still shown, will only select
194 " three letters from the line above
195 set completeopt&vim
196 set completeopt+=menuone
197 call append(0, ["December2015"])
198 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
199 call assert_equal(["December2015", "December201", ""], getline(1,3))
200 %d
201
202 " use longest for 'completeopt'
203 set completeopt&vim
204 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
205 set completeopt+=longest
206 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
207 call assert_equal(["M", "Ma", ""], getline(1,3))
208 %d
209
210 " use noselect/noinsert for 'completeopt'
211 set completeopt&vim
212 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
213 set completeopt+=noselect
214 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
215 set completeopt-=noselect completeopt+=noinsert
216 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
217 call assert_equal(["March", "M", "March"], getline(1,4))
218 %d
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200219endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200220
221
Bram Moolenaarae0f30b2018-06-12 15:22:43 +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 Moolenaarae0f30b2018-06-12 15:22:43 +0200252 func! s:complTest1() abort
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200253 call complete(1, ['source', 'soundfold'])
254 return ''
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200255 endfunc
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200256
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200257 func! s:complTest2() abort
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200258 call complete(1, ['source', 'soundfold'])
259 return ''
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200260 endfunc
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200261
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 Moolenaar73655cf2019-04-06 13:45:55 +0200280func Test_complete_no_filter()
281 func! s:complTest1() abort
282 call complete(1, [{'word': 'foobar'}])
283 return ''
284 endfunc
285 func! s:complTest2() abort
286 call complete(1, [{'word': 'foobar', 'equal': 1}])
287 return ''
288 endfunc
289
290 let completeopt = &completeopt
291
292 " without equal=1
293 new
294 set completeopt=menuone,noinsert,menu
295 inoremap <F5> <C-R>=s:complTest1()<CR>
296 call feedkeys("i\<F5>z\<CR>\<CR>\<ESC>.", 'tx')
297 call assert_equal('z', getline(1))
298 bwipe!
299
300 " with equal=1
301 new
302 set completeopt=menuone,noinsert,menu
303 inoremap <F5> <C-R>=s:complTest2()<CR>
304 call feedkeys("i\<F5>z\<CR>\<CR>\<ESC>.", 'tx')
305 call assert_equal('foobar', getline(1))
306 bwipe!
307
308 let &completeopt = completeopt
309 iunmap <F5>
310endfunc
311
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200312func Test_compl_vim_cmds_after_register_expr()
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200313 func! s:test_func()
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200314 return 'autocmd '
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200315 endfunc
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200316 augroup AAAAA_Group
317 au!
318 augroup END
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200319
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200320 new
321 call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx')
322 call assert_equal('autocmd AAAAA_Group', getline(1))
323 autocmd! AAAAA_Group
324 augroup! AAAAA_Group
325 bwipe!
326endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200327
Bram Moolenaar472e8592016-10-15 17:06:47 +0200328func DummyCompleteOne(findstart, base)
329 if a:findstart
330 return 0
331 else
332 wincmd n
333 return ['onedef', 'oneDEF']
334 endif
335endfunc
336
337" Test that nothing happens if the 'completefunc' opens
338" a new window (no completion, no crash)
339func Test_completefunc_opens_new_window_one()
340 new
341 let winid = win_getid()
342 setlocal completefunc=DummyCompleteOne
343 call setline(1, 'one')
344 /^one
345 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:')
346 call assert_notequal(winid, win_getid())
347 q!
348 call assert_equal(winid, win_getid())
349 call assert_equal('', getline(1))
350 q!
351endfunc
352
353" Test that nothing happens if the 'completefunc' opens
354" a new window (no completion, no crash)
355func DummyCompleteTwo(findstart, base)
356 if a:findstart
357 wincmd n
358 return 0
359 else
360 return ['twodef', 'twoDEF']
361 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200362endfunc
Bram Moolenaar472e8592016-10-15 17:06:47 +0200363
364" Test that nothing happens if the 'completefunc' opens
365" a new window (no completion, no crash)
366func Test_completefunc_opens_new_window_two()
367 new
368 let winid = win_getid()
369 setlocal completefunc=DummyCompleteTwo
370 call setline(1, 'two')
371 /^two
372 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:')
373 call assert_notequal(winid, win_getid())
374 q!
375 call assert_equal(winid, win_getid())
376 call assert_equal('two', getline(1))
377 q!
378endfunc
379
380func DummyCompleteThree(findstart, base)
381 if a:findstart
382 return 0
383 else
384 return ['threedef', 'threeDEF']
385 endif
386endfunc
387
388:"Test that 'completefunc' works when it's OK.
389func Test_completefunc_works()
390 new
391 let winid = win_getid()
392 setlocal completefunc=DummyCompleteThree
393 call setline(1, 'three')
394 /^three
395 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
396 call assert_equal(winid, win_getid())
397 call assert_equal('threeDEF', getline(1))
398 q!
399endfunc
400
401func DummyCompleteFour(findstart, base)
402 if a:findstart
403 return 0
404 else
405 call complete_add('four1')
406 call complete_add('four2')
407 call complete_check()
408 call complete_add('four3')
409 call complete_add('four4')
410 call complete_check()
411 call complete_add('four5')
412 call complete_add('four6')
413 return []
414 endif
415endfunc
416
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200417" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200418func Test_omnifunc_with_check()
419 new
420 setlocal omnifunc=DummyCompleteFour
421 call setline(1, 'four')
422 /^four
423 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
424 call assert_equal('four2', getline(1))
425
426 call setline(1, 'four')
427 /^four
428 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
429 call assert_equal('four3', getline(1))
430
431 call setline(1, 'four')
432 /^four
433 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
434 call assert_equal('four5', getline(1))
435
436 q!
437endfunc
438
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200439func UndoComplete()
Bram Moolenaar869e3522016-10-16 15:35:47 +0200440 call complete(1, ['January', 'February', 'March',
441 \ 'April', 'May', 'June', 'July', 'August', 'September',
442 \ 'October', 'November', 'December'])
443 return ''
444endfunc
445
446" Test that no undo item is created when no completion is inserted
447func Test_complete_no_undo()
448 set completeopt=menu,preview,noinsert,noselect
449 inoremap <Right> <C-R>=UndoComplete()<CR>
450 new
451 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
452 call feedkeys("iaaa\<Esc>0", 'xt')
453 call assert_equal('aaa', getline(2))
454 call feedkeys("i\<Right>\<Esc>", 'xt')
455 call assert_equal('aaa', getline(2))
456 call feedkeys("u", 'xt')
457 call assert_equal('', getline(2))
458
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200459 call feedkeys("ibbb\<Esc>0", 'xt')
460 call assert_equal('bbb', getline(2))
461 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
462 call assert_equal('January', getline(2))
463 call feedkeys("u", 'xt')
464 call assert_equal('bbb', getline(2))
465
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200466 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
467 call assert_equal('January', getline(2))
468 call feedkeys("u", 'xt')
469 call assert_equal('bbb', getline(2))
470
Bram Moolenaar869e3522016-10-16 15:35:47 +0200471 iunmap <Right>
472 set completeopt&
473 q!
474endfunc
475
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200476func DummyCompleteFive(findstart, base)
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200477 if a:findstart
478 return 0
479 else
480 return [
481 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
482 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
483 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
484 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
485 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
486 \ ]
487 endif
488endfunc
489
490" Test that 'completefunc' on Scratch buffer with preview window works when
491" it's OK.
492func Test_completefunc_with_scratch_buffer()
493 new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile
494 set completeopt+=preview
495 setlocal completefunc=DummyCompleteFive
496 call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
497 call assert_equal(['April'], getline(1, '$'))
498 pclose
499 q!
500 set completeopt&
501endfunc
Bram Moolenaar869e3522016-10-16 15:35:47 +0200502
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100503" <C-E> - select original typed text before the completion started without
504" auto-wrap text.
505func Test_completion_ctrl_e_without_autowrap()
506 new
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100507 let tw_save = &tw
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100508 set tw=78
509 let li = [
510 \ '" zzz',
511 \ '" zzzyyyyyyyyyyyyyyyyyyy']
512 call setline(1, li)
513 0
514 call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx")
515 call assert_equal(li, getline(1, '$'))
516
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100517 let &tw = tw_save
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100518 q!
519endfunc
520
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200521func DummyCompleteSix()
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100522 call complete(1, ['Hello', 'World'])
523 return ''
524endfunction
525
526" complete() correctly clears the list of autocomplete candidates
527" See #1411
528func Test_completion_clear_candidate_list()
529 new
530 %d
531 " select first entry from the completion popup
532 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx")
533 call assert_equal('Hello', getline(1))
534 %d
535 " select second entry from the completion popup
536 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx")
537 call assert_equal('World', getline(1))
538 %d
539 " select original text
540 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx")
541 call assert_equal(' xxx', getline(1))
542 %d
543 " back at first entry from completion list
544 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx")
545 call assert_equal('Hello', getline(1))
546
547 bw!
548endfunc
549
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100550func Test_completion_respect_bs_option()
551 new
552 let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"]
553
554 set bs=indent,eol
555 call setline(1, li)
556 1
557 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
558 call assert_equal('aaa', getline(1))
559
560 %d
561 set bs=indent,eol,start
562 call setline(1, li)
563 1
564 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
565 call assert_equal('', getline(1))
566
567 bw!
568endfunc
569
Bram Moolenaard56a79d2017-02-19 15:26:18 +0100570func CompleteUndo() abort
571 call complete(1, g:months)
572 return ''
573endfunc
574
575func Test_completion_can_undo()
576 inoremap <Right> <c-r>=CompleteUndo()<cr>
577 set completeopt+=noinsert,noselect
578
579 new
580 call feedkeys("a\<Right>a\<Esc>", 'xt')
581 call assert_equal('a', getline(1))
582 undo
583 call assert_equal('', getline(1))
584
585 bwipe!
586 set completeopt&
587 iunmap <Right>
588endfunc
589
Bram Moolenaard099e032017-02-21 23:00:36 +0100590func Test_completion_comment_formatting()
591 new
592 setl formatoptions=tcqro
593 call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx')
594 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
595 %d
596 call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx')
597 call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4))
598 %d
599 try
600 call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx')
Bram Moolenaar37175402017-03-18 20:18:45 +0100601 call assert_report('completefunc not set, should have failed')
Bram Moolenaard099e032017-02-21 23:00:36 +0100602 catch
603 call assert_exception('E764:')
604 endtry
605 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
606 bwipe!
607endfunc
608
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200609func MessCompleteMonths()
Bram Moolenaar4475b622017-05-01 20:46:52 +0200610 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep")
611 call complete_add(m)
612 if complete_check()
613 break
614 endif
615 endfor
616 return []
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200617endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200618
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200619func MessCompleteMore()
Bram Moolenaar4475b622017-05-01 20:46:52 +0200620 call complete(1, split("Oct Nov Dec"))
621 return []
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200622endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200623
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200624func MessComplete(findstart, base)
Bram Moolenaar4475b622017-05-01 20:46:52 +0200625 if a:findstart
626 let line = getline('.')
627 let start = col('.') - 1
628 while start > 0 && line[start - 1] =~ '\a'
629 let start -= 1
630 endwhile
631 return start
632 else
633 call MessCompleteMonths()
634 call MessCompleteMore()
635 return []
636 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200637endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200638
639func Test_complete_func_mess()
640 " Calling complete() after complete_add() in 'completefunc' is wrong, but it
641 " should not crash.
642 set completefunc=MessComplete
643 new
644 call setline(1, 'Ju')
645 call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx')
646 call assert_equal('Oct/Oct', getline(1))
647 bwipe!
648 set completefunc=
649endfunc
650
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200651func Test_complete_CTRLN_startofbuffer()
652 new
653 call setline(1, [ 'organize(cupboard, 3, 2);',
654 \ 'prioritize(bureau, 8, 7);',
655 \ 'realize(bannister, 4, 4);',
656 \ 'moralize(railing, 3,9);'])
657 let expected=['cupboard.organize(3, 2);',
658 \ 'bureau.prioritize(8, 7);',
659 \ 'bannister.realize(4, 4);',
660 \ 'railing.moralize(3,9);']
661 call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx')
662 call assert_equal(expected, getline(1,'$'))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200663 bwipe!
664endfunc
665
666func Test_popup_and_window_resize()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200667 CheckFeature terminal
668 CheckNotGui
669
Bram Moolenaara5e66212017-09-29 22:42:33 +0200670 let h = winheight(0)
671 if h < 15
672 return
673 endif
Bram Moolenaarb3158762017-11-02 17:50:14 +0100674 let rows = h / 3
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100675 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows})
676 call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>")
Bram Moolenaarb3158762017-11-02 17:50:14 +0100677 " Wait for the nested Vim to exit insert mode, where it will show the ruler.
678 " Need to trigger a redraw.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100679 call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'})
Bram Moolenaarb3158762017-11-02 17:50:14 +0100680
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100681 call term_sendkeys(buf, "Gi\<c-x>")
682 call term_sendkeys(buf, "\<c-v>")
683 call term_wait(buf, 100)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200684 " popup first entry "!" must be at the top
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200685 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200686 exe 'resize +' . (h - 1)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100687 call term_wait(buf, 100)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200688 redraw!
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200689 " popup shifted down, first line is now empty
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200690 call WaitForAssert({-> assert_equal('', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200691 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200692 " popup is below cursor line and shows first match "!"
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200693 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))})
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200694 " cursor line also shows !
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100695 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200696 bwipe!
697endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200698
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200699func Test_popup_and_preview_autocommand()
700 " This used to crash Vim
701 if !has('python')
702 return
703 endif
704 let h = winheight(0)
705 if h < 15
706 return
707 endif
708 new
709 augroup MyBufAdd
710 au!
711 au BufAdd * nested tab sball
712 augroup END
713 set omnifunc=pythoncomplete#Complete
714 call setline(1, 'import os')
715 " make the line long
716 call setline(2, ' os.')
717 $
718 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<enter>\<esc>", 'tx')
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200719 call assert_equal("import os", getline(1))
720 call assert_match(' os.\(EX_IOERR\|O_CREAT\)$', getline(2))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200721 call assert_equal(1, winnr('$'))
722 " previewwindow option is not set
723 call assert_equal(0, &previewwindow)
724 norm! gt
725 call assert_equal(0, &previewwindow)
726 norm! gT
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100727 call assert_equal(10, tabpagenr('$'))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200728 tabonly
729 pclose
730 augroup MyBufAdd
731 au!
732 augroup END
733 augroup! MyBufAdd
734 bw!
735endfunc
736
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100737func Test_popup_and_previewwindow_dump()
738 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200739 throw 'Skipped: cannot make screendumps'
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100740 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200741 let lines =<< trim END
742 set previewheight=9
743 silent! pedit
744 call setline(1, map(repeat(["ab"], 10), "v:val. v:key"))
745 exec "norm! G\<C-E>\<C-E>"
746 END
747 call writefile(lines, 'Xscript')
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100748 let buf = RunVimInTerminal('-S Xscript', {})
749
750 " Test that popup and previewwindow do not overlap.
751 call term_sendkeys(buf, "o\<C-X>\<C-N>")
752 sleep 100m
753 call VerifyScreenDump(buf, 'Test_popup_and_previewwindow_01', {})
754
755 call term_sendkeys(buf, "\<Esc>u")
756 call StopVimInTerminal(buf)
757 call delete('Xscript')
758endfunc
759
Bram Moolenaar246fe032017-11-19 19:56:27 +0100760func Test_balloon_split()
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200761 CheckFunction balloon_split
762
Bram Moolenaar246fe032017-11-19 19:56:27 +0100763 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100764 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
765 \ ], balloon_split(
766 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
767 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100768 \ 'one two three four one two three four one two thre',
769 \ 'e four',
770 \ ], balloon_split(
771 \ 'one two three four one two three four one two three four'))
772
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200773 eval 'struct = {one = 1, two = 2, three = 3}'
774 \ ->balloon_split()
775 \ ->assert_equal([
776 \ 'struct = {',
777 \ ' one = 1,',
778 \ ' two = 2,',
779 \ ' three = 3}',
780 \ ])
Bram Moolenaar246fe032017-11-19 19:56:27 +0100781
782 call assert_equal([
783 \ 'struct = {',
784 \ ' one = 1,',
785 \ ' nested = {',
786 \ ' n1 = "yes",',
787 \ ' n2 = "no"}',
788 \ ' two = 2}',
789 \ ], balloon_split(
790 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
791 call assert_equal([
792 \ 'struct = 0x234 {',
793 \ ' long = 2343 "\\"some long string that will be wr',
794 \ 'apped in two\\"",',
795 \ ' next = 123}',
796 \ ], balloon_split(
797 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
798endfunc
799
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100800func Test_popup_position()
801 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200802 throw 'Skipped: cannot make screendumps'
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100803 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200804 let lines =<< trim END
805 123456789_123456789_123456789_a
806 123456789_123456789_123456789_b
807 123
808 END
809 call writefile(lines, 'Xtest')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100810 let buf = RunVimInTerminal('Xtest', {})
811 call term_sendkeys(buf, ":vsplit\<CR>")
812
813 " default pumwidth in left window: overlap in right window
814 call term_sendkeys(buf, "GA\<C-N>")
815 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
816 call term_sendkeys(buf, "\<Esc>u")
817
818 " default pumwidth: fill until right of window
819 call term_sendkeys(buf, "\<C-W>l")
820 call term_sendkeys(buf, "GA\<C-N>")
821 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
822
823 " larger pumwidth: used as minimum width
824 call term_sendkeys(buf, "\<Esc>u")
825 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
826 call term_sendkeys(buf, "GA\<C-N>")
827 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
828
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100829 " completed text wider than the window and 'pumwidth' smaller than available
830 " space
831 call term_sendkeys(buf, "\<Esc>u")
832 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
833 call term_sendkeys(buf, "ggI123456789_\<Esc>")
834 call term_sendkeys(buf, "jI123456789_\<Esc>")
835 call term_sendkeys(buf, "GA\<C-N>")
836 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
837
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100838 call term_sendkeys(buf, "\<Esc>u")
839 call StopVimInTerminal(buf)
840 call delete('Xtest')
841endfunc
842
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100843func Test_popup_command()
844 if !CanRunVimInTerminal() || !has('menu')
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200845 throw 'Skipped: cannot make screendumps and/or menu feature missing'
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100846 endif
847
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200848 let lines =<< trim END
849 one two three four five
850 and one two Xthree four five
851 one more two three four five
852 END
853 call writefile(lines, 'Xtest')
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100854 let buf = RunVimInTerminal('Xtest', {})
855 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
856 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
857 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
858
859 " Select a word
860 call term_sendkeys(buf, "jj")
861 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
862
863 " Select a word
864 call term_sendkeys(buf, "j\<CR>")
865 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
866
867 call term_sendkeys(buf, "\<Esc>")
868 call StopVimInTerminal(buf)
869 call delete('Xtest')
870endfunc
871
Bram Moolenaare87edf32018-04-17 22:14:32 +0200872func Test_popup_complete_backwards()
873 new
874 call setline(1, ['Post', 'Port', 'Po'])
875 let expected=['Post', 'Port', 'Port']
876 call cursor(3,2)
877 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<cr>", 'tx')
878 call assert_equal(expected, getline(1,'$'))
879 bwipe!
880endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100881
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200882func Test_popup_complete_backwards_ctrl_p()
883 new
884 call setline(1, ['Post', 'Port', 'Po'])
885 let expected=['Post', 'Port', 'Port']
886 call cursor(3,2)
887 call feedkeys("A\<C-P>\<C-N>rt\<cr>", 'tx')
888 call assert_equal(expected, getline(1,'$'))
889 bwipe!
890endfunc
891
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200892func Test_complete_o_tab()
Bram Moolenaar39de9522018-05-08 22:48:00 +0200893 let s:o_char_pressed = 0
894
895 fun! s:act_on_text_changed()
896 if s:o_char_pressed
897 let s:o_char_pressed = 0
898 call feedkeys("\<c-x>\<c-n>", 'i')
899 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200900 endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200901
902 set completeopt=menu,noselect
903 new
904 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
905 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
906 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
907 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
908 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
909 call cursor(4,1)
910 call test_override("char_avail", 1)
911 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
912 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
913 call assert_equal(l:expected, getline(1,'$'))
914
915 call test_override("char_avail", 0)
916 bwipe!
917 set completeopt&
918 delfunc s:act_on_text_changed
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200919endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200920
Bram Moolenaarf42b45d2019-01-06 13:11:05 +0100921func Test_menu_only_exists_in_terminal()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200922 CheckCommand tlmenu
923 CheckNotGui
924
Bram Moolenaarf42b45d2019-01-06 13:11:05 +0100925 tlnoremenu &Edit.&Paste<Tab>"+gP <C-W>"+
926 aunmenu *
927 try
928 popup Edit
929 call assert_false(1, 'command should have failed')
930 catch
931 call assert_exception('E328:')
932 endtry
933endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200934
Bram Moolenaarfd133322019-03-29 12:20:27 +0100935func Test_popup_complete_info_01()
936 new
937 inoremap <buffer><F5> <C-R>=complete_info().mode<CR>
938 func s:complTestEval() abort
939 call complete(1, ['aa', 'ab'])
940 return ''
941 endfunc
942 inoremap <buffer><F6> <C-R>=s:complTestEval()<CR>
943 call writefile([
944 \ 'dummy dummy.txt 1',
945 \], 'Xdummy.txt')
946 setlocal tags=Xdummy.txt
947 setlocal dictionary=Xdummy.txt
948 setlocal thesaurus=Xdummy.txt
949 setlocal omnifunc=syntaxcomplete#Complete
950 setlocal completefunc=syntaxcomplete#Complete
951 setlocal spell
952 for [keys, mode_name] in [
953 \ ["", ''],
954 \ ["\<C-X>", 'ctrl_x'],
955 \ ["\<C-X>\<C-N>", 'keyword'],
956 \ ["\<C-X>\<C-P>", 'keyword'],
957 \ ["\<C-X>\<C-L>", 'whole_line'],
958 \ ["\<C-X>\<C-F>", 'files'],
959 \ ["\<C-X>\<C-]>", 'tags'],
960 \ ["\<C-X>\<C-D>", 'path_defines'],
961 \ ["\<C-X>\<C-I>", 'path_patterns'],
962 \ ["\<C-X>\<C-K>", 'dictionary'],
963 \ ["\<C-X>\<C-T>", 'thesaurus'],
964 \ ["\<C-X>\<C-V>", 'cmdline'],
965 \ ["\<C-X>\<C-U>", 'function'],
966 \ ["\<C-X>\<C-O>", 'omni'],
967 \ ["\<C-X>s", 'spell'],
968 \ ["\<F6>", 'eval'],
969 \]
970 call feedkeys("i" . keys . "\<F5>\<Esc>", 'tx')
971 call assert_equal(mode_name, getline('.'))
972 %d
973 endfor
974 call delete('Xdummy.txt')
975 bwipe!
976endfunc
977
978func UserDefinedComplete(findstart, base)
979 if a:findstart
980 return 0
981 else
982 return [
983 \ { 'word': 'Jan', 'menu': 'January' },
984 \ { 'word': 'Feb', 'menu': 'February' },
985 \ { 'word': 'Mar', 'menu': 'March' },
986 \ { 'word': 'Apr', 'menu': 'April' },
987 \ { 'word': 'May', 'menu': 'May' },
988 \ ]
989 endif
990endfunc
991
992func GetCompleteInfo()
993 if empty(g:compl_what)
994 let g:compl_info = complete_info()
995 else
996 let g:compl_info = complete_info(g:compl_what)
997 endif
998 return ''
999endfunc
1000
1001func Test_popup_complete_info_02()
1002 new
1003 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
1004 setlocal completefunc=UserDefinedComplete
1005
1006 let d = {
1007 \ 'mode': 'function',
1008 \ 'pum_visible': 1,
1009 \ 'items': [
1010 \ {'word': 'Jan', 'menu': 'January', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1011 \ {'word': 'Feb', 'menu': 'February', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1012 \ {'word': 'Mar', 'menu': 'March', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1013 \ {'word': 'Apr', 'menu': 'April', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1014 \ {'word': 'May', 'menu': 'May', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}
1015 \ ],
1016 \ 'selected': 0,
1017 \ }
1018
1019 let g:compl_what = []
1020 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1021 call assert_equal(d, g:compl_info)
1022
1023 let g:compl_what = ['mode', 'pum_visible', 'selected']
1024 call remove(d, 'items')
1025 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1026 call assert_equal(d, g:compl_info)
1027
1028 let g:compl_what = ['mode']
1029 call remove(d, 'selected')
1030 call remove(d, 'pum_visible')
1031 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1032 call assert_equal(d, g:compl_info)
1033 bwipe!
1034endfunc
1035
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001036func Test_popup_complete_info_no_pum()
1037 new
1038 call assert_false( pumvisible() )
1039 let no_pum_info = complete_info()
1040 let d = {
1041 \ 'mode': '',
1042 \ 'pum_visible': 0,
1043 \ 'items': [],
1044 \ 'selected': -1,
1045 \ }
1046 call assert_equal( d, complete_info() )
1047 bwipe!
1048endfunc
1049
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001050func Test_CompleteChanged()
1051 new
1052 call setline(1, ['foo', 'bar', 'foobar', ''])
1053 set complete=. completeopt=noinsert,noselect,menuone
1054 function! OnPumChange()
1055 let g:event = copy(v:event)
1056 let g:item = get(v:event, 'completed_item', {})
1057 let g:word = get(g:item, 'word', v:null)
1058 endfunction
1059 augroup AAAAA_Group
1060 au!
1061 autocmd CompleteChanged * :call OnPumChange()
1062 augroup END
1063 call cursor(4, 1)
1064
1065 call feedkeys("Sf\<C-N>", 'tx')
1066 call assert_equal({'completed_item': {}, 'width': 15,
1067 \ 'height': 2, 'size': 2,
1068 \ 'col': 0, 'row': 4, 'scrollbar': v:false}, g:event)
1069 call feedkeys("a\<C-N>\<C-N>\<C-E>", 'tx')
1070 call assert_equal('foo', g:word)
1071 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1072 call assert_equal('foobar', g:word)
1073 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1074 call assert_equal(v:null, g:word)
1075 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 'tx')
1076 call assert_equal('foobar', g:word)
1077
1078 autocmd! AAAAA_Group
1079 set complete& completeopt&
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001080 delfunc! OnPumChange
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001081 bw!
1082endfunc
1083
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001084function! GetPumPosition()
1085 call assert_true( pumvisible() )
1086 let g:pum_pos = pum_getpos()
1087 return ''
1088endfunction
1089
1090func Test_pum_getpos()
1091 new
1092 inoremap <buffer><F5> <C-R>=GetPumPosition()<CR>
1093 setlocal completefunc=UserDefinedComplete
1094
1095 let d = {
1096 \ 'height': 5,
1097 \ 'width': 15,
1098 \ 'row': 1,
1099 \ 'col': 0,
1100 \ 'size': 5,
1101 \ 'scrollbar': v:false,
1102 \ }
1103 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1104 call assert_equal(d, g:pum_pos)
1105
1106 call assert_false( pumvisible() )
1107 call assert_equal( {}, pum_getpos() )
1108 bw!
1109 unlet g:pum_pos
1110endfunc
1111
Bram Moolenaar47247282016-08-02 22:36:02 +02001112" vim: shiftwidth=2 sts=2 expandtab