blob: 2d0625bc0931e12d83a6e90a3b66f32a7ae29d30 [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 Moolenaar1a3a8912019-08-23 22:31:37 +0200253 eval ['source', 'soundfold']->complete(1)
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200254 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
Bram Moolenaarff06f282020-04-21 22:01:14 +0200337" Test that nothing happens if the 'completefunc' tries to open
338" a new window (fails to open window, continues)
Bram Moolenaar472e8592016-10-15 17:06:47 +0200339func Test_completefunc_opens_new_window_one()
340 new
341 let winid = win_getid()
342 setlocal completefunc=DummyCompleteOne
343 call setline(1, 'one')
344 /^one
Bram Moolenaar28976e22021-01-29 21:07:07 +0100345 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E565:')
Bram Moolenaar472e8592016-10-15 17:06:47 +0200346 call assert_equal(winid, win_getid())
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100347 call assert_equal('onedef', getline(1))
Bram Moolenaar472e8592016-10-15 17:06:47 +0200348 q!
349endfunc
350
351" Test that nothing happens if the 'completefunc' opens
352" a new window (no completion, no crash)
353func DummyCompleteTwo(findstart, base)
354 if a:findstart
355 wincmd n
356 return 0
357 else
358 return ['twodef', 'twoDEF']
359 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200360endfunc
Bram Moolenaar472e8592016-10-15 17:06:47 +0200361
362" Test that nothing happens if the 'completefunc' opens
363" a new window (no completion, no crash)
364func Test_completefunc_opens_new_window_two()
365 new
366 let winid = win_getid()
367 setlocal completefunc=DummyCompleteTwo
368 call setline(1, 'two')
369 /^two
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200370 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:')
Bram Moolenaar472e8592016-10-15 17:06:47 +0200371 call assert_notequal(winid, win_getid())
372 q!
373 call assert_equal(winid, win_getid())
374 call assert_equal('two', getline(1))
375 q!
376endfunc
377
378func DummyCompleteThree(findstart, base)
379 if a:findstart
380 return 0
381 else
382 return ['threedef', 'threeDEF']
383 endif
384endfunc
385
386:"Test that 'completefunc' works when it's OK.
387func Test_completefunc_works()
388 new
389 let winid = win_getid()
390 setlocal completefunc=DummyCompleteThree
391 call setline(1, 'three')
392 /^three
393 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
394 call assert_equal(winid, win_getid())
395 call assert_equal('threeDEF', getline(1))
396 q!
397endfunc
398
399func DummyCompleteFour(findstart, base)
400 if a:findstart
401 return 0
402 else
403 call complete_add('four1')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200404 eval 'four2'->complete_add()
Bram Moolenaar472e8592016-10-15 17:06:47 +0200405 call complete_check()
406 call complete_add('four3')
407 call complete_add('four4')
408 call complete_check()
409 call complete_add('four5')
410 call complete_add('four6')
411 return []
412 endif
413endfunc
414
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200415" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200416func Test_omnifunc_with_check()
417 new
418 setlocal omnifunc=DummyCompleteFour
419 call setline(1, 'four')
420 /^four
421 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
422 call assert_equal('four2', getline(1))
423
424 call setline(1, 'four')
425 /^four
426 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
427 call assert_equal('four3', getline(1))
428
429 call setline(1, 'four')
430 /^four
431 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
432 call assert_equal('four5', getline(1))
433
434 q!
435endfunc
436
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200437func UndoComplete()
Bram Moolenaar869e3522016-10-16 15:35:47 +0200438 call complete(1, ['January', 'February', 'March',
439 \ 'April', 'May', 'June', 'July', 'August', 'September',
440 \ 'October', 'November', 'December'])
441 return ''
442endfunc
443
444" Test that no undo item is created when no completion is inserted
445func Test_complete_no_undo()
446 set completeopt=menu,preview,noinsert,noselect
447 inoremap <Right> <C-R>=UndoComplete()<CR>
448 new
449 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
450 call feedkeys("iaaa\<Esc>0", 'xt')
451 call assert_equal('aaa', getline(2))
452 call feedkeys("i\<Right>\<Esc>", 'xt')
453 call assert_equal('aaa', getline(2))
454 call feedkeys("u", 'xt')
455 call assert_equal('', getline(2))
456
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200457 call feedkeys("ibbb\<Esc>0", 'xt')
458 call assert_equal('bbb', getline(2))
459 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
460 call assert_equal('January', getline(2))
461 call feedkeys("u", 'xt')
462 call assert_equal('bbb', getline(2))
463
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200464 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
465 call assert_equal('January', getline(2))
466 call feedkeys("u", 'xt')
467 call assert_equal('bbb', getline(2))
468
Bram Moolenaar869e3522016-10-16 15:35:47 +0200469 iunmap <Right>
470 set completeopt&
471 q!
472endfunc
473
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200474func DummyCompleteFive(findstart, base)
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200475 if a:findstart
476 return 0
477 else
478 return [
479 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
480 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
481 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
482 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
483 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
484 \ ]
485 endif
486endfunc
487
488" Test that 'completefunc' on Scratch buffer with preview window works when
489" it's OK.
490func Test_completefunc_with_scratch_buffer()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100491 CheckFeature quickfix
492
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200493 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')
Bram Moolenaar28976e22021-01-29 21:07:07 +0100645 call assert_fails('call feedkeys("A\<c-x>\<c-u>/\<esc>", "tx")', 'E578:')
646 call assert_equal('Jan/', getline(1))
Bram Moolenaar4475b622017-05-01 20:46:52 +0200647 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
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100668 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200669 CheckNotGui
670
Bram Moolenaara5e66212017-09-29 22:42:33 +0200671 let h = winheight(0)
672 if h < 15
673 return
674 endif
Bram Moolenaarb3158762017-11-02 17:50:14 +0100675 let rows = h / 3
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100676 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows})
677 call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>")
Bram Moolenaarb3158762017-11-02 17:50:14 +0100678 " Wait for the nested Vim to exit insert mode, where it will show the ruler.
679 " Need to trigger a redraw.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100680 call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'})
Bram Moolenaarb3158762017-11-02 17:50:14 +0100681
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100682 call term_sendkeys(buf, "Gi\<c-x>")
683 call term_sendkeys(buf, "\<c-v>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200684 call TermWait(buf, 50)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200685 " popup first entry "!" must be at the top
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200686 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200687 exe 'resize +' . (h - 1)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200688 call TermWait(buf, 50)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200689 redraw!
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200690 " popup shifted down, first line is now empty
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200691 call WaitForAssert({-> assert_equal('', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200692 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200693 " popup is below cursor line and shows first match "!"
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200694 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))})
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200695 " cursor line also shows !
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100696 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200697 bwipe!
698endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200699
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200700func Test_popup_and_preview_autocommand()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100701 CheckFeature python
702 CheckFeature quickfix
703 if winheight(0) < 15
704 throw 'Skipped: window height insufficient'
705 endif
706
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200707 " This used to crash Vim
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200708 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()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100738 CheckScreendump
739 CheckFeature quickfix
740
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200741 let lines =<< trim END
742 set previewheight=9
743 silent! pedit
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100744 call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200745 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
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100750 " wait for the script to finish
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200751 call TermWait(buf)
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100752
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100753 " Test that popup and previewwindow do not overlap.
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100754 call term_sendkeys(buf, "o")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200755 call TermWait(buf, 50)
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100756 call term_sendkeys(buf, "\<C-X>\<C-N>")
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100757 call VerifyScreenDump(buf, 'Test_popup_and_previewwindow_01', {})
758
759 call term_sendkeys(buf, "\<Esc>u")
760 call StopVimInTerminal(buf)
761 call delete('Xscript')
762endfunc
763
Bram Moolenaar246fe032017-11-19 19:56:27 +0100764func Test_balloon_split()
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200765 CheckFunction balloon_split
766
Bram Moolenaar246fe032017-11-19 19:56:27 +0100767 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100768 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
769 \ ], balloon_split(
770 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
771 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100772 \ 'one two three four one two three four one two thre',
773 \ 'e four',
774 \ ], balloon_split(
775 \ 'one two three four one two three four one two three four'))
776
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200777 eval 'struct = {one = 1, two = 2, three = 3}'
778 \ ->balloon_split()
779 \ ->assert_equal([
780 \ 'struct = {',
781 \ ' one = 1,',
782 \ ' two = 2,',
783 \ ' three = 3}',
784 \ ])
Bram Moolenaar246fe032017-11-19 19:56:27 +0100785
786 call assert_equal([
787 \ 'struct = {',
788 \ ' one = 1,',
789 \ ' nested = {',
790 \ ' n1 = "yes",',
791 \ ' n2 = "no"}',
792 \ ' two = 2}',
793 \ ], balloon_split(
794 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
795 call assert_equal([
796 \ 'struct = 0x234 {',
797 \ ' long = 2343 "\\"some long string that will be wr',
798 \ 'apped in two\\"",',
799 \ ' next = 123}',
800 \ ], balloon_split(
801 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +0100802 call assert_equal([
803 \ 'Some comment',
804 \ '',
805 \ 'typedef this that;',
806 \ ], balloon_split(
807 \ "Some comment\n\ntypedef this that;"))
Bram Moolenaar246fe032017-11-19 19:56:27 +0100808endfunc
809
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100810func Test_popup_position()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100811 CheckScreendump
812
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200813 let lines =<< trim END
814 123456789_123456789_123456789_a
815 123456789_123456789_123456789_b
816 123
817 END
818 call writefile(lines, 'Xtest')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100819 let buf = RunVimInTerminal('Xtest', {})
820 call term_sendkeys(buf, ":vsplit\<CR>")
821
822 " default pumwidth in left window: overlap in right window
823 call term_sendkeys(buf, "GA\<C-N>")
824 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
825 call term_sendkeys(buf, "\<Esc>u")
826
827 " default pumwidth: fill until right of window
828 call term_sendkeys(buf, "\<C-W>l")
829 call term_sendkeys(buf, "GA\<C-N>")
830 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
831
832 " larger pumwidth: used as minimum width
833 call term_sendkeys(buf, "\<Esc>u")
834 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
835 call term_sendkeys(buf, "GA\<C-N>")
836 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
837
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100838 " completed text wider than the window and 'pumwidth' smaller than available
839 " space
840 call term_sendkeys(buf, "\<Esc>u")
841 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
842 call term_sendkeys(buf, "ggI123456789_\<Esc>")
843 call term_sendkeys(buf, "jI123456789_\<Esc>")
844 call term_sendkeys(buf, "GA\<C-N>")
845 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
846
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100847 call term_sendkeys(buf, "\<Esc>u")
848 call StopVimInTerminal(buf)
849 call delete('Xtest')
850endfunc
851
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100852func Test_popup_command()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100853 CheckScreendump
854 CheckFeature menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100855
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100856 menu Test.Foo Foo
857 call assert_fails('popup Test.Foo', 'E336:')
858 call assert_fails('popup Test.Foo.X', 'E327:')
859 call assert_fails('popup Foo', 'E337:')
860 unmenu Test.Foo
861
Bram Moolenaar38455a92020-12-24 18:39:02 +0100862 let script =<< trim END
863 func StartTimer()
864 call timer_start(100, {-> ChangeMenu()})
865 endfunc
866 func ChangeMenu()
867 nunmenu PopUp.&Paste
868 nnoremenu 1.40 PopUp.&Paste :echomsg "pasted"<CR>
869 echomsg 'changed'
870 endfunc
871 END
872 call writefile(script, 'XtimerScript')
873
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200874 let lines =<< trim END
875 one two three four five
876 and one two Xthree four five
877 one more two three four five
878 END
879 call writefile(lines, 'Xtest')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100880 let buf = RunVimInTerminal('-S XtimerScript Xtest', {})
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100881 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
882 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
883 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
884
Bram Moolenaar38455a92020-12-24 18:39:02 +0100885 " go to the Paste entry in the menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100886 call term_sendkeys(buf, "jj")
887 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
888
889 " Select a word
890 call term_sendkeys(buf, "j\<CR>")
891 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
892
893 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar38455a92020-12-24 18:39:02 +0100894
895 " Set a timer to change a menu entry while it's displayed. The text should
896 " not change but the command does. Making the screendump also verifies that
897 " "changed" shows up, which means the timer triggered
898 call term_sendkeys(buf, "/X\<CR>:call StartTimer() | popup PopUp\<CR>")
899 call VerifyScreenDump(buf, 'Test_popup_command_04', {})
900
901 " Select the Paste entry, executes the changed menu item.
902 call term_sendkeys(buf, "jj\<CR>")
903 call VerifyScreenDump(buf, 'Test_popup_command_05', {})
904
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100905 call StopVimInTerminal(buf)
906 call delete('Xtest')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100907 call delete('XtimerScript')
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100908endfunc
909
Bram Moolenaare87edf32018-04-17 22:14:32 +0200910func Test_popup_complete_backwards()
911 new
912 call setline(1, ['Post', 'Port', 'Po'])
913 let expected=['Post', 'Port', 'Port']
914 call cursor(3,2)
915 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<cr>", 'tx')
916 call assert_equal(expected, getline(1,'$'))
917 bwipe!
918endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100919
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200920func Test_popup_complete_backwards_ctrl_p()
921 new
922 call setline(1, ['Post', 'Port', 'Po'])
923 let expected=['Post', 'Port', 'Port']
924 call cursor(3,2)
925 call feedkeys("A\<C-P>\<C-N>rt\<cr>", 'tx')
926 call assert_equal(expected, getline(1,'$'))
927 bwipe!
928endfunc
929
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200930func Test_complete_o_tab()
Bram Moolenaar39de9522018-05-08 22:48:00 +0200931 let s:o_char_pressed = 0
932
933 fun! s:act_on_text_changed()
934 if s:o_char_pressed
935 let s:o_char_pressed = 0
936 call feedkeys("\<c-x>\<c-n>", 'i')
937 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200938 endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200939
940 set completeopt=menu,noselect
941 new
942 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
943 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
944 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
945 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
946 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
947 call cursor(4,1)
948 call test_override("char_avail", 1)
949 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
950 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
951 call assert_equal(l:expected, getline(1,'$'))
952
953 call test_override("char_avail", 0)
954 bwipe!
955 set completeopt&
956 delfunc s:act_on_text_changed
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200957endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200958
Bram Moolenaarf42b45d2019-01-06 13:11:05 +0100959func Test_menu_only_exists_in_terminal()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200960 CheckCommand tlmenu
961 CheckNotGui
962
Bram Moolenaarf42b45d2019-01-06 13:11:05 +0100963 tlnoremenu &Edit.&Paste<Tab>"+gP <C-W>"+
964 aunmenu *
965 try
966 popup Edit
967 call assert_false(1, 'command should have failed')
968 catch
969 call assert_exception('E328:')
970 endtry
971endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200972
Bram Moolenaarfd133322019-03-29 12:20:27 +0100973func Test_popup_complete_info_01()
974 new
975 inoremap <buffer><F5> <C-R>=complete_info().mode<CR>
976 func s:complTestEval() abort
977 call complete(1, ['aa', 'ab'])
978 return ''
979 endfunc
980 inoremap <buffer><F6> <C-R>=s:complTestEval()<CR>
981 call writefile([
982 \ 'dummy dummy.txt 1',
983 \], 'Xdummy.txt')
984 setlocal tags=Xdummy.txt
985 setlocal dictionary=Xdummy.txt
986 setlocal thesaurus=Xdummy.txt
987 setlocal omnifunc=syntaxcomplete#Complete
988 setlocal completefunc=syntaxcomplete#Complete
989 setlocal spell
990 for [keys, mode_name] in [
991 \ ["", ''],
992 \ ["\<C-X>", 'ctrl_x'],
993 \ ["\<C-X>\<C-N>", 'keyword'],
994 \ ["\<C-X>\<C-P>", 'keyword'],
995 \ ["\<C-X>\<C-L>", 'whole_line'],
996 \ ["\<C-X>\<C-F>", 'files'],
997 \ ["\<C-X>\<C-]>", 'tags'],
998 \ ["\<C-X>\<C-D>", 'path_defines'],
999 \ ["\<C-X>\<C-I>", 'path_patterns'],
1000 \ ["\<C-X>\<C-K>", 'dictionary'],
1001 \ ["\<C-X>\<C-T>", 'thesaurus'],
1002 \ ["\<C-X>\<C-V>", 'cmdline'],
1003 \ ["\<C-X>\<C-U>", 'function'],
1004 \ ["\<C-X>\<C-O>", 'omni'],
1005 \ ["\<C-X>s", 'spell'],
1006 \ ["\<F6>", 'eval'],
1007 \]
1008 call feedkeys("i" . keys . "\<F5>\<Esc>", 'tx')
1009 call assert_equal(mode_name, getline('.'))
1010 %d
1011 endfor
1012 call delete('Xdummy.txt')
1013 bwipe!
1014endfunc
1015
1016func UserDefinedComplete(findstart, base)
1017 if a:findstart
1018 return 0
1019 else
1020 return [
1021 \ { 'word': 'Jan', 'menu': 'January' },
1022 \ { 'word': 'Feb', 'menu': 'February' },
1023 \ { 'word': 'Mar', 'menu': 'March' },
1024 \ { 'word': 'Apr', 'menu': 'April' },
1025 \ { 'word': 'May', 'menu': 'May' },
1026 \ ]
1027 endif
1028endfunc
1029
1030func GetCompleteInfo()
1031 if empty(g:compl_what)
1032 let g:compl_info = complete_info()
1033 else
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001034 let g:compl_info = g:compl_what->complete_info()
Bram Moolenaarfd133322019-03-29 12:20:27 +01001035 endif
1036 return ''
1037endfunc
1038
1039func Test_popup_complete_info_02()
1040 new
1041 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
1042 setlocal completefunc=UserDefinedComplete
1043
1044 let d = {
1045 \ 'mode': 'function',
1046 \ 'pum_visible': 1,
1047 \ 'items': [
1048 \ {'word': 'Jan', 'menu': 'January', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1049 \ {'word': 'Feb', 'menu': 'February', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1050 \ {'word': 'Mar', 'menu': 'March', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1051 \ {'word': 'Apr', 'menu': 'April', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1052 \ {'word': 'May', 'menu': 'May', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}
1053 \ ],
1054 \ 'selected': 0,
1055 \ }
1056
1057 let g:compl_what = []
1058 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1059 call assert_equal(d, g:compl_info)
1060
1061 let g:compl_what = ['mode', 'pum_visible', 'selected']
1062 call remove(d, 'items')
1063 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1064 call assert_equal(d, g:compl_info)
1065
1066 let g:compl_what = ['mode']
1067 call remove(d, 'selected')
1068 call remove(d, 'pum_visible')
1069 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1070 call assert_equal(d, g:compl_info)
1071 bwipe!
1072endfunc
1073
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001074func Test_popup_complete_info_no_pum()
1075 new
1076 call assert_false( pumvisible() )
1077 let no_pum_info = complete_info()
1078 let d = {
1079 \ 'mode': '',
1080 \ 'pum_visible': 0,
1081 \ 'items': [],
1082 \ 'selected': -1,
1083 \ }
1084 call assert_equal( d, complete_info() )
1085 bwipe!
1086endfunc
1087
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001088func Test_CompleteChanged()
1089 new
1090 call setline(1, ['foo', 'bar', 'foobar', ''])
1091 set complete=. completeopt=noinsert,noselect,menuone
1092 function! OnPumChange()
1093 let g:event = copy(v:event)
1094 let g:item = get(v:event, 'completed_item', {})
1095 let g:word = get(g:item, 'word', v:null)
1096 endfunction
1097 augroup AAAAA_Group
1098 au!
1099 autocmd CompleteChanged * :call OnPumChange()
1100 augroup END
1101 call cursor(4, 1)
1102
1103 call feedkeys("Sf\<C-N>", 'tx')
1104 call assert_equal({'completed_item': {}, 'width': 15,
1105 \ 'height': 2, 'size': 2,
1106 \ 'col': 0, 'row': 4, 'scrollbar': v:false}, g:event)
1107 call feedkeys("a\<C-N>\<C-N>\<C-E>", 'tx')
1108 call assert_equal('foo', g:word)
1109 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1110 call assert_equal('foobar', g:word)
1111 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1112 call assert_equal(v:null, g:word)
1113 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 'tx')
1114 call assert_equal('foobar', g:word)
1115
1116 autocmd! AAAAA_Group
1117 set complete& completeopt&
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001118 delfunc! OnPumChange
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001119 bw!
1120endfunc
1121
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001122function! GetPumPosition()
1123 call assert_true( pumvisible() )
1124 let g:pum_pos = pum_getpos()
1125 return ''
1126endfunction
1127
1128func Test_pum_getpos()
1129 new
1130 inoremap <buffer><F5> <C-R>=GetPumPosition()<CR>
1131 setlocal completefunc=UserDefinedComplete
1132
1133 let d = {
1134 \ 'height': 5,
1135 \ 'width': 15,
1136 \ 'row': 1,
1137 \ 'col': 0,
1138 \ 'size': 5,
1139 \ 'scrollbar': v:false,
1140 \ }
1141 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1142 call assert_equal(d, g:pum_pos)
1143
1144 call assert_false( pumvisible() )
1145 call assert_equal( {}, pum_getpos() )
1146 bw!
1147 unlet g:pum_pos
1148endfunc
1149
Bram Moolenaar47247282016-08-02 22:36:02 +02001150" vim: shiftwidth=2 sts=2 expandtab