blob: c389f63f559c4c4549439b5fe3f714397d39e813 [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
zeertzjqee446032022-04-30 15:02:22 +0100328func Test_compl_ignore_mappings()
329 call setline(1, ['foo', 'bar', 'baz', 'foobar'])
330 inoremap <C-P> (C-P)
331 inoremap <C-N> (C-N)
332 normal! G
333 call feedkeys("o\<C-X>\<C-N>\<C-N>\<C-N>\<C-P>\<C-N>\<C-Y>", 'tx')
334 call assert_equal('baz', getline('.'))
335 " Also test with unsimplified keys
336 call feedkeys("o\<C-X>\<*C-N>\<*C-N>\<*C-N>\<*C-P>\<*C-N>\<C-Y>", 'tx')
337 call assert_equal('baz', getline('.'))
338 iunmap <C-P>
339 iunmap <C-N>
340 bwipe!
341endfunc
342
Bram Moolenaar472e8592016-10-15 17:06:47 +0200343func DummyCompleteOne(findstart, base)
344 if a:findstart
345 return 0
346 else
347 wincmd n
348 return ['onedef', 'oneDEF']
349 endif
350endfunc
351
Bram Moolenaarff06f282020-04-21 22:01:14 +0200352" Test that nothing happens if the 'completefunc' tries to open
353" a new window (fails to open window, continues)
Bram Moolenaar472e8592016-10-15 17:06:47 +0200354func Test_completefunc_opens_new_window_one()
355 new
356 let winid = win_getid()
357 setlocal completefunc=DummyCompleteOne
358 call setline(1, 'one')
359 /^one
Bram Moolenaar28976e22021-01-29 21:07:07 +0100360 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E565:')
Bram Moolenaar472e8592016-10-15 17:06:47 +0200361 call assert_equal(winid, win_getid())
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100362 call assert_equal('onedef', getline(1))
Bram Moolenaar472e8592016-10-15 17:06:47 +0200363 q!
364endfunc
365
366" Test that nothing happens if the 'completefunc' opens
367" a new window (no completion, no crash)
368func DummyCompleteTwo(findstart, base)
369 if a:findstart
370 wincmd n
371 return 0
372 else
373 return ['twodef', 'twoDEF']
374 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200375endfunc
Bram Moolenaar472e8592016-10-15 17:06:47 +0200376
377" Test that nothing happens if the 'completefunc' opens
378" a new window (no completion, no crash)
379func Test_completefunc_opens_new_window_two()
380 new
381 let winid = win_getid()
382 setlocal completefunc=DummyCompleteTwo
383 call setline(1, 'two')
384 /^two
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100385 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E565:')
Bram Moolenaar472e8592016-10-15 17:06:47 +0200386 call assert_equal(winid, win_getid())
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100387 call assert_equal('twodef', getline(1))
Bram Moolenaar472e8592016-10-15 17:06:47 +0200388 q!
389endfunc
390
391func DummyCompleteThree(findstart, base)
392 if a:findstart
393 return 0
394 else
395 return ['threedef', 'threeDEF']
396 endif
397endfunc
398
399:"Test that 'completefunc' works when it's OK.
400func Test_completefunc_works()
401 new
402 let winid = win_getid()
403 setlocal completefunc=DummyCompleteThree
404 call setline(1, 'three')
405 /^three
406 call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")
407 call assert_equal(winid, win_getid())
408 call assert_equal('threeDEF', getline(1))
409 q!
410endfunc
411
412func DummyCompleteFour(findstart, base)
413 if a:findstart
414 return 0
415 else
416 call complete_add('four1')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200417 eval 'four2'->complete_add()
Bram Moolenaar472e8592016-10-15 17:06:47 +0200418 call complete_check()
419 call complete_add('four3')
420 call complete_add('four4')
421 call complete_check()
422 call complete_add('four5')
423 call complete_add('four6')
424 return []
425 endif
426endfunc
427
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200428" Test that 'omnifunc' works when it's OK.
Bram Moolenaar472e8592016-10-15 17:06:47 +0200429func Test_omnifunc_with_check()
430 new
431 setlocal omnifunc=DummyCompleteFour
432 call setline(1, 'four')
433 /^four
434 call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x")
435 call assert_equal('four2', getline(1))
436
437 call setline(1, 'four')
438 /^four
439 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x")
440 call assert_equal('four3', getline(1))
441
442 call setline(1, 'four')
443 /^four
444 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
445 call assert_equal('four5', getline(1))
446
447 q!
448endfunc
449
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200450func UndoComplete()
Bram Moolenaar869e3522016-10-16 15:35:47 +0200451 call complete(1, ['January', 'February', 'March',
452 \ 'April', 'May', 'June', 'July', 'August', 'September',
453 \ 'October', 'November', 'December'])
454 return ''
455endfunc
456
457" Test that no undo item is created when no completion is inserted
458func Test_complete_no_undo()
459 set completeopt=menu,preview,noinsert,noselect
460 inoremap <Right> <C-R>=UndoComplete()<CR>
461 new
462 call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt')
463 call feedkeys("iaaa\<Esc>0", 'xt')
464 call assert_equal('aaa', getline(2))
465 call feedkeys("i\<Right>\<Esc>", 'xt')
466 call assert_equal('aaa', getline(2))
467 call feedkeys("u", 'xt')
468 call assert_equal('', getline(2))
469
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200470 call feedkeys("ibbb\<Esc>0", 'xt')
471 call assert_equal('bbb', getline(2))
472 call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt')
473 call assert_equal('January', getline(2))
474 call feedkeys("u", 'xt')
475 call assert_equal('bbb', getline(2))
476
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +0200477 call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt')
478 call assert_equal('January', getline(2))
479 call feedkeys("u", 'xt')
480 call assert_equal('bbb', getline(2))
481
Bram Moolenaar869e3522016-10-16 15:35:47 +0200482 iunmap <Right>
483 set completeopt&
484 q!
485endfunc
486
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200487func DummyCompleteFive(findstart, base)
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200488 if a:findstart
489 return 0
490 else
491 return [
492 \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" },
493 \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" },
494 \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" },
495 \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" },
496 \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" },
497 \ ]
498 endif
499endfunc
500
501" Test that 'completefunc' on Scratch buffer with preview window works when
502" it's OK.
503func Test_completefunc_with_scratch_buffer()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100504 CheckFeature quickfix
505
Bram Moolenaar60ef3e82016-10-29 14:37:56 +0200506 new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile
507 set completeopt+=preview
508 setlocal completefunc=DummyCompleteFive
509 call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x")
510 call assert_equal(['April'], getline(1, '$'))
511 pclose
512 q!
513 set completeopt&
514endfunc
Bram Moolenaar869e3522016-10-16 15:35:47 +0200515
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100516" <C-E> - select original typed text before the completion started without
517" auto-wrap text.
518func Test_completion_ctrl_e_without_autowrap()
519 new
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100520 let tw_save = &tw
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100521 set tw=78
522 let li = [
523 \ '" zzz',
524 \ '" zzzyyyyyyyyyyyyyyyyyyy']
525 call setline(1, li)
526 0
527 call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx")
528 call assert_equal(li, getline(1, '$'))
529
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100530 let &tw = tw_save
Bram Moolenaar73fd4982016-12-09 19:36:56 +0100531 q!
532endfunc
533
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200534func DummyCompleteSix()
Bram Moolenaaraed6d0b2017-01-27 21:48:54 +0100535 call complete(1, ['Hello', 'World'])
536 return ''
537endfunction
538
539" complete() correctly clears the list of autocomplete candidates
540" See #1411
541func Test_completion_clear_candidate_list()
542 new
543 %d
544 " select first entry from the completion popup
545 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx")
546 call assert_equal('Hello', getline(1))
547 %d
548 " select second entry from the completion popup
549 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx")
550 call assert_equal('World', getline(1))
551 %d
552 " select original text
553 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx")
554 call assert_equal(' xxx', getline(1))
555 %d
556 " back at first entry from completion list
557 call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx")
558 call assert_equal('Hello', getline(1))
559
560 bw!
561endfunc
562
Bram Moolenaar190b04c2017-02-09 17:37:03 +0100563func Test_completion_respect_bs_option()
564 new
565 let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"]
566
567 set bs=indent,eol
568 call setline(1, li)
569 1
570 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
571 call assert_equal('aaa', getline(1))
572
573 %d
574 set bs=indent,eol,start
575 call setline(1, li)
576 1
577 call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx")
578 call assert_equal('', getline(1))
579
580 bw!
581endfunc
582
Bram Moolenaard56a79d2017-02-19 15:26:18 +0100583func CompleteUndo() abort
584 call complete(1, g:months)
585 return ''
586endfunc
587
588func Test_completion_can_undo()
589 inoremap <Right> <c-r>=CompleteUndo()<cr>
590 set completeopt+=noinsert,noselect
591
592 new
593 call feedkeys("a\<Right>a\<Esc>", 'xt')
594 call assert_equal('a', getline(1))
595 undo
596 call assert_equal('', getline(1))
597
598 bwipe!
599 set completeopt&
600 iunmap <Right>
601endfunc
602
Bram Moolenaard099e032017-02-21 23:00:36 +0100603func Test_completion_comment_formatting()
604 new
605 setl formatoptions=tcqro
606 call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx')
607 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
608 %d
609 call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx')
610 call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4))
611 %d
612 try
613 call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx')
Bram Moolenaar37175402017-03-18 20:18:45 +0100614 call assert_report('completefunc not set, should have failed')
Bram Moolenaard099e032017-02-21 23:00:36 +0100615 catch
616 call assert_exception('E764:')
617 endtry
618 call assert_equal(['', '/*', ' *', ' */'], getline(1,4))
619 bwipe!
620endfunc
621
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200622func MessCompleteMonths()
Bram Moolenaar4475b622017-05-01 20:46:52 +0200623 for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep")
624 call complete_add(m)
625 if complete_check()
626 break
627 endif
628 endfor
629 return []
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200630endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200631
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200632func MessCompleteMore()
Bram Moolenaar4475b622017-05-01 20:46:52 +0200633 call complete(1, split("Oct Nov Dec"))
634 return []
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200635endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200636
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200637func MessComplete(findstart, base)
Bram Moolenaar4475b622017-05-01 20:46:52 +0200638 if a:findstart
639 let line = getline('.')
640 let start = col('.') - 1
641 while start > 0 && line[start - 1] =~ '\a'
642 let start -= 1
643 endwhile
644 return start
645 else
646 call MessCompleteMonths()
647 call MessCompleteMore()
648 return []
649 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200650endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200651
652func Test_complete_func_mess()
653 " Calling complete() after complete_add() in 'completefunc' is wrong, but it
654 " should not crash.
655 set completefunc=MessComplete
656 new
657 call setline(1, 'Ju')
zeertzjqcfe45652022-05-27 17:26:55 +0100658 call assert_fails('call feedkeys("A\<c-x>\<c-u>/\<esc>", "tx")', 'E565:')
Bram Moolenaar28976e22021-01-29 21:07:07 +0100659 call assert_equal('Jan/', getline(1))
Bram Moolenaar4475b622017-05-01 20:46:52 +0200660 bwipe!
661 set completefunc=
662endfunc
663
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200664func Test_complete_CTRLN_startofbuffer()
665 new
666 call setline(1, [ 'organize(cupboard, 3, 2);',
667 \ 'prioritize(bureau, 8, 7);',
668 \ 'realize(bannister, 4, 4);',
669 \ 'moralize(railing, 3,9);'])
670 let expected=['cupboard.organize(3, 2);',
671 \ 'bureau.prioritize(8, 7);',
672 \ 'bannister.realize(4, 4);',
673 \ 'railing.moralize(3,9);']
674 call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx')
675 call assert_equal(expected, getline(1,'$'))
Bram Moolenaara5e66212017-09-29 22:42:33 +0200676 bwipe!
677endfunc
678
679func Test_popup_and_window_resize()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200680 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100681 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200682 CheckNotGui
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100683 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200684
Bram Moolenaara5e66212017-09-29 22:42:33 +0200685 let h = winheight(0)
686 if h < 15
687 return
688 endif
Bram Moolenaarb3158762017-11-02 17:50:14 +0100689 let rows = h / 3
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100690 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows})
691 call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>")
Bram Moolenaarb3158762017-11-02 17:50:14 +0100692 " Wait for the nested Vim to exit insert mode, where it will show the ruler.
693 " Need to trigger a redraw.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100694 call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'})
Bram Moolenaarb3158762017-11-02 17:50:14 +0100695
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100696 call term_sendkeys(buf, "Gi\<c-x>")
697 call term_sendkeys(buf, "\<c-v>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200698 call TermWait(buf, 50)
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200699 " popup first entry "!" must be at the top
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200700 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200701 exe 'resize +' . (h - 1)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200702 call TermWait(buf, 50)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200703 redraw!
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200704 " popup shifted down, first line is now empty
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200705 call WaitForAssert({-> assert_equal('', term_getline(buf, 1))})
Bram Moolenaara5e66212017-09-29 22:42:33 +0200706 sleep 100m
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200707 " popup is below cursor line and shows first match "!"
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200708 call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))})
Bram Moolenaarf52c3832017-09-30 16:49:19 +0200709 " cursor line also shows !
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100710 call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0]))
Bram Moolenaar24a9e342017-06-24 15:39:07 +0200711 bwipe!
712endfunc
Bram Moolenaar4475b622017-05-01 20:46:52 +0200713
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200714func Test_popup_and_preview_autocommand()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100715 CheckFeature python
716 CheckFeature quickfix
717 if winheight(0) < 15
718 throw 'Skipped: window height insufficient'
719 endif
720
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200721 " This used to crash Vim
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200722 new
723 augroup MyBufAdd
724 au!
725 au BufAdd * nested tab sball
726 augroup END
727 set omnifunc=pythoncomplete#Complete
728 call setline(1, 'import os')
729 " make the line long
730 call setline(2, ' os.')
731 $
732 call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<enter>\<esc>", 'tx')
Bram Moolenaar2a45d642017-10-27 01:35:00 +0200733 call assert_equal("import os", getline(1))
734 call assert_match(' os.\(EX_IOERR\|O_CREAT\)$', getline(2))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200735 call assert_equal(1, winnr('$'))
736 " previewwindow option is not set
737 call assert_equal(0, &previewwindow)
738 norm! gt
739 call assert_equal(0, &previewwindow)
740 norm! gT
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100741 call assert_equal(10, tabpagenr('$'))
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200742 tabonly
743 pclose
744 augroup MyBufAdd
745 au!
746 augroup END
747 augroup! MyBufAdd
748 bw!
749endfunc
750
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100751func Test_popup_and_previewwindow_dump()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100752 CheckScreendump
753 CheckFeature quickfix
754
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200755 let lines =<< trim END
756 set previewheight=9
757 silent! pedit
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100758 call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200759 exec "norm! G\<C-E>\<C-E>"
760 END
761 call writefile(lines, 'Xscript')
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100762 let buf = RunVimInTerminal('-S Xscript', {})
763
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100764 " wait for the script to finish
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200765 call TermWait(buf)
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100766
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100767 " Test that popup and previewwindow do not overlap.
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100768 call term_sendkeys(buf, "o")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200769 call TermWait(buf, 50)
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100770 call term_sendkeys(buf, "\<C-X>\<C-N>")
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100771 call VerifyScreenDump(buf, 'Test_popup_and_previewwindow_01', {})
772
773 call term_sendkeys(buf, "\<Esc>u")
774 call StopVimInTerminal(buf)
775 call delete('Xscript')
776endfunc
777
Bram Moolenaar246fe032017-11-19 19:56:27 +0100778func Test_balloon_split()
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200779 CheckFunction balloon_split
780
Bram Moolenaar246fe032017-11-19 19:56:27 +0100781 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100782 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
783 \ ], balloon_split(
784 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
785 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100786 \ 'one two three four one two three four one two thre',
787 \ 'e four',
788 \ ], balloon_split(
789 \ 'one two three four one two three four one two three four'))
790
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200791 eval 'struct = {one = 1, two = 2, three = 3}'
792 \ ->balloon_split()
793 \ ->assert_equal([
794 \ 'struct = {',
795 \ ' one = 1,',
796 \ ' two = 2,',
797 \ ' three = 3}',
798 \ ])
Bram Moolenaar246fe032017-11-19 19:56:27 +0100799
800 call assert_equal([
801 \ 'struct = {',
802 \ ' one = 1,',
803 \ ' nested = {',
804 \ ' n1 = "yes",',
805 \ ' n2 = "no"}',
806 \ ' two = 2}',
807 \ ], balloon_split(
808 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
809 call assert_equal([
810 \ 'struct = 0x234 {',
811 \ ' long = 2343 "\\"some long string that will be wr',
812 \ 'apped in two\\"",',
813 \ ' next = 123}',
814 \ ], balloon_split(
815 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +0100816 call assert_equal([
817 \ 'Some comment',
818 \ '',
819 \ 'typedef this that;',
820 \ ], balloon_split(
821 \ "Some comment\n\ntypedef this that;"))
Bram Moolenaar246fe032017-11-19 19:56:27 +0100822endfunc
823
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100824func Test_popup_position()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100825 CheckScreendump
826
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200827 let lines =<< trim END
828 123456789_123456789_123456789_a
829 123456789_123456789_123456789_b
830 123
831 END
832 call writefile(lines, 'Xtest')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100833 let buf = RunVimInTerminal('Xtest', {})
834 call term_sendkeys(buf, ":vsplit\<CR>")
835
836 " default pumwidth in left window: overlap in right window
837 call term_sendkeys(buf, "GA\<C-N>")
838 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
839 call term_sendkeys(buf, "\<Esc>u")
840
841 " default pumwidth: fill until right of window
842 call term_sendkeys(buf, "\<C-W>l")
843 call term_sendkeys(buf, "GA\<C-N>")
844 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
845
846 " larger pumwidth: used as minimum width
847 call term_sendkeys(buf, "\<Esc>u")
848 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
849 call term_sendkeys(buf, "GA\<C-N>")
850 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
851
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100852 " completed text wider than the window and 'pumwidth' smaller than available
853 " space
854 call term_sendkeys(buf, "\<Esc>u")
855 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
856 call term_sendkeys(buf, "ggI123456789_\<Esc>")
857 call term_sendkeys(buf, "jI123456789_\<Esc>")
858 call term_sendkeys(buf, "GA\<C-N>")
859 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
860
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100861 call term_sendkeys(buf, "\<Esc>u")
862 call StopVimInTerminal(buf)
863 call delete('Xtest')
864endfunc
865
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100866func Test_popup_command()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100867 CheckScreendump
868 CheckFeature menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100869
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100870 menu Test.Foo Foo
871 call assert_fails('popup Test.Foo', 'E336:')
872 call assert_fails('popup Test.Foo.X', 'E327:')
873 call assert_fails('popup Foo', 'E337:')
874 unmenu Test.Foo
875
Bram Moolenaar38455a92020-12-24 18:39:02 +0100876 let script =<< trim END
877 func StartTimer()
878 call timer_start(100, {-> ChangeMenu()})
879 endfunc
880 func ChangeMenu()
881 nunmenu PopUp.&Paste
882 nnoremenu 1.40 PopUp.&Paste :echomsg "pasted"<CR>
883 echomsg 'changed'
884 endfunc
885 END
886 call writefile(script, 'XtimerScript')
887
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200888 let lines =<< trim END
889 one two three four five
890 and one two Xthree four five
891 one more two three four five
892 END
893 call writefile(lines, 'Xtest')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100894 let buf = RunVimInTerminal('-S XtimerScript Xtest', {})
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100895 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
896 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
897 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
898
Bram Moolenaar38455a92020-12-24 18:39:02 +0100899 " go to the Paste entry in the menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100900 call term_sendkeys(buf, "jj")
901 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
902
903 " Select a word
904 call term_sendkeys(buf, "j\<CR>")
905 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
906
907 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar38455a92020-12-24 18:39:02 +0100908
909 " Set a timer to change a menu entry while it's displayed. The text should
910 " not change but the command does. Making the screendump also verifies that
911 " "changed" shows up, which means the timer triggered
912 call term_sendkeys(buf, "/X\<CR>:call StartTimer() | popup PopUp\<CR>")
913 call VerifyScreenDump(buf, 'Test_popup_command_04', {})
914
915 " Select the Paste entry, executes the changed menu item.
916 call term_sendkeys(buf, "jj\<CR>")
917 call VerifyScreenDump(buf, 'Test_popup_command_05', {})
918
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100919 call StopVimInTerminal(buf)
920 call delete('Xtest')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100921 call delete('XtimerScript')
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100922endfunc
923
Bram Moolenaare87edf32018-04-17 22:14:32 +0200924func Test_popup_complete_backwards()
925 new
926 call setline(1, ['Post', 'Port', 'Po'])
927 let expected=['Post', 'Port', 'Port']
928 call cursor(3,2)
929 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<cr>", 'tx')
930 call assert_equal(expected, getline(1,'$'))
931 bwipe!
932endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100933
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200934func Test_popup_complete_backwards_ctrl_p()
935 new
936 call setline(1, ['Post', 'Port', 'Po'])
937 let expected=['Post', 'Port', 'Port']
938 call cursor(3,2)
939 call feedkeys("A\<C-P>\<C-N>rt\<cr>", 'tx')
940 call assert_equal(expected, getline(1,'$'))
941 bwipe!
942endfunc
943
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200944func Test_complete_o_tab()
Bram Moolenaar39de9522018-05-08 22:48:00 +0200945 let s:o_char_pressed = 0
946
947 fun! s:act_on_text_changed()
948 if s:o_char_pressed
949 let s:o_char_pressed = 0
950 call feedkeys("\<c-x>\<c-n>", 'i')
951 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200952 endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200953
954 set completeopt=menu,noselect
955 new
956 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
957 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
958 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
959 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
960 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
961 call cursor(4,1)
962 call test_override("char_avail", 1)
963 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
964 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
965 call assert_equal(l:expected, getline(1,'$'))
966
967 call test_override("char_avail", 0)
968 bwipe!
969 set completeopt&
970 delfunc s:act_on_text_changed
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200971endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200972
Bram Moolenaarf42b45d2019-01-06 13:11:05 +0100973func Test_menu_only_exists_in_terminal()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200974 CheckCommand tlmenu
975 CheckNotGui
976
Bram Moolenaarf42b45d2019-01-06 13:11:05 +0100977 tlnoremenu &Edit.&Paste<Tab>"+gP <C-W>"+
978 aunmenu *
979 try
980 popup Edit
981 call assert_false(1, 'command should have failed')
982 catch
983 call assert_exception('E328:')
984 endtry
985endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +0200986
Bram Moolenaarfd133322019-03-29 12:20:27 +0100987func Test_popup_complete_info_01()
988 new
989 inoremap <buffer><F5> <C-R>=complete_info().mode<CR>
990 func s:complTestEval() abort
991 call complete(1, ['aa', 'ab'])
992 return ''
993 endfunc
994 inoremap <buffer><F6> <C-R>=s:complTestEval()<CR>
995 call writefile([
996 \ 'dummy dummy.txt 1',
997 \], 'Xdummy.txt')
998 setlocal tags=Xdummy.txt
999 setlocal dictionary=Xdummy.txt
1000 setlocal thesaurus=Xdummy.txt
1001 setlocal omnifunc=syntaxcomplete#Complete
1002 setlocal completefunc=syntaxcomplete#Complete
1003 setlocal spell
1004 for [keys, mode_name] in [
1005 \ ["", ''],
1006 \ ["\<C-X>", 'ctrl_x'],
1007 \ ["\<C-X>\<C-N>", 'keyword'],
1008 \ ["\<C-X>\<C-P>", 'keyword'],
zeertzjq27fef592021-10-03 12:01:27 +01001009 \ ["\<C-X>\<C-E>", 'scroll'],
1010 \ ["\<C-X>\<C-Y>", 'scroll'],
1011 \ ["\<C-X>\<C-E>\<C-E>\<C-Y>", 'scroll'],
1012 \ ["\<C-X>\<C-Y>\<C-E>\<C-Y>", 'scroll'],
Bram Moolenaarfd133322019-03-29 12:20:27 +01001013 \ ["\<C-X>\<C-L>", 'whole_line'],
1014 \ ["\<C-X>\<C-F>", 'files'],
1015 \ ["\<C-X>\<C-]>", 'tags'],
1016 \ ["\<C-X>\<C-D>", 'path_defines'],
1017 \ ["\<C-X>\<C-I>", 'path_patterns'],
1018 \ ["\<C-X>\<C-K>", 'dictionary'],
1019 \ ["\<C-X>\<C-T>", 'thesaurus'],
1020 \ ["\<C-X>\<C-V>", 'cmdline'],
1021 \ ["\<C-X>\<C-U>", 'function'],
1022 \ ["\<C-X>\<C-O>", 'omni'],
1023 \ ["\<C-X>s", 'spell'],
1024 \ ["\<F6>", 'eval'],
1025 \]
1026 call feedkeys("i" . keys . "\<F5>\<Esc>", 'tx')
1027 call assert_equal(mode_name, getline('.'))
1028 %d
1029 endfor
1030 call delete('Xdummy.txt')
1031 bwipe!
1032endfunc
1033
1034func UserDefinedComplete(findstart, base)
1035 if a:findstart
1036 return 0
1037 else
1038 return [
1039 \ { 'word': 'Jan', 'menu': 'January' },
1040 \ { 'word': 'Feb', 'menu': 'February' },
1041 \ { 'word': 'Mar', 'menu': 'March' },
1042 \ { 'word': 'Apr', 'menu': 'April' },
1043 \ { 'word': 'May', 'menu': 'May' },
1044 \ ]
1045 endif
1046endfunc
1047
1048func GetCompleteInfo()
1049 if empty(g:compl_what)
1050 let g:compl_info = complete_info()
1051 else
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001052 let g:compl_info = g:compl_what->complete_info()
Bram Moolenaarfd133322019-03-29 12:20:27 +01001053 endif
1054 return ''
1055endfunc
1056
1057func Test_popup_complete_info_02()
1058 new
1059 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
1060 setlocal completefunc=UserDefinedComplete
1061
1062 let d = {
1063 \ 'mode': 'function',
1064 \ 'pum_visible': 1,
1065 \ 'items': [
1066 \ {'word': 'Jan', 'menu': 'January', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1067 \ {'word': 'Feb', 'menu': 'February', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1068 \ {'word': 'Mar', 'menu': 'March', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1069 \ {'word': 'Apr', 'menu': 'April', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1070 \ {'word': 'May', 'menu': 'May', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}
1071 \ ],
1072 \ 'selected': 0,
1073 \ }
1074
1075 let g:compl_what = []
1076 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1077 call assert_equal(d, g:compl_info)
1078
1079 let g:compl_what = ['mode', 'pum_visible', 'selected']
1080 call remove(d, 'items')
1081 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1082 call assert_equal(d, g:compl_info)
1083
1084 let g:compl_what = ['mode']
1085 call remove(d, 'selected')
1086 call remove(d, 'pum_visible')
1087 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1088 call assert_equal(d, g:compl_info)
1089 bwipe!
1090endfunc
1091
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001092func Test_popup_complete_info_no_pum()
1093 new
1094 call assert_false( pumvisible() )
1095 let no_pum_info = complete_info()
1096 let d = {
1097 \ 'mode': '',
1098 \ 'pum_visible': 0,
1099 \ 'items': [],
1100 \ 'selected': -1,
1101 \ }
1102 call assert_equal( d, complete_info() )
1103 bwipe!
1104endfunc
1105
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001106func Test_CompleteChanged()
1107 new
1108 call setline(1, ['foo', 'bar', 'foobar', ''])
1109 set complete=. completeopt=noinsert,noselect,menuone
1110 function! OnPumChange()
1111 let g:event = copy(v:event)
1112 let g:item = get(v:event, 'completed_item', {})
1113 let g:word = get(g:item, 'word', v:null)
1114 endfunction
1115 augroup AAAAA_Group
1116 au!
1117 autocmd CompleteChanged * :call OnPumChange()
1118 augroup END
1119 call cursor(4, 1)
1120
1121 call feedkeys("Sf\<C-N>", 'tx')
1122 call assert_equal({'completed_item': {}, 'width': 15,
1123 \ 'height': 2, 'size': 2,
1124 \ 'col': 0, 'row': 4, 'scrollbar': v:false}, g:event)
1125 call feedkeys("a\<C-N>\<C-N>\<C-E>", 'tx')
1126 call assert_equal('foo', g:word)
1127 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1128 call assert_equal('foobar', g:word)
1129 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1130 call assert_equal(v:null, g:word)
1131 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 'tx')
1132 call assert_equal('foobar', g:word)
1133
1134 autocmd! AAAAA_Group
1135 set complete& completeopt&
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001136 delfunc! OnPumChange
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001137 bw!
1138endfunc
1139
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001140function! GetPumPosition()
1141 call assert_true( pumvisible() )
1142 let g:pum_pos = pum_getpos()
1143 return ''
1144endfunction
1145
1146func Test_pum_getpos()
1147 new
1148 inoremap <buffer><F5> <C-R>=GetPumPosition()<CR>
1149 setlocal completefunc=UserDefinedComplete
1150
1151 let d = {
1152 \ 'height': 5,
1153 \ 'width': 15,
1154 \ 'row': 1,
1155 \ 'col': 0,
1156 \ 'size': 5,
1157 \ 'scrollbar': v:false,
1158 \ }
1159 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1160 call assert_equal(d, g:pum_pos)
1161
1162 call assert_false( pumvisible() )
1163 call assert_equal( {}, pum_getpos() )
1164 bw!
1165 unlet g:pum_pos
1166endfunc
1167
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001168" Test for the popup menu with the 'rightleft' option set
1169func Test_pum_rightleft()
Dominique Pelle56cddb32021-06-04 21:09:55 +02001170 CheckFeature rightleft
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001171 CheckScreendump
Dominique Pelle56cddb32021-06-04 21:09:55 +02001172
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001173 let lines =<< trim END
1174 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
1175 vim
1176 victory
1177 END
1178 call writefile(lines, 'Xtest1')
1179 let buf = RunVimInTerminal('--cmd "set rightleft" Xtest1', {})
1180 call term_wait(buf)
1181 call term_sendkeys(buf, "Go\<C-P>")
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001182 call VerifyScreenDump(buf, 'Test_pum_rightleft_01', {'rows': 8})
1183 call term_sendkeys(buf, "\<C-P>\<C-Y>")
1184 call term_wait(buf)
1185 redraw!
1186 call assert_match('\s*miv', Screenline(5))
1187
1188 " Test for expanding tabs to spaces in the popup menu
1189 let lines =<< trim END
1190 one two
1191 one three
1192 four
1193 END
1194 call writefile(lines, 'Xtest2')
1195 call term_sendkeys(buf, "\<Esc>:e! Xtest2\<CR>")
1196 call term_wait(buf)
1197 call term_sendkeys(buf, "Goone\<C-X>\<C-L>")
1198 call term_wait(buf)
1199 redraw!
1200 call VerifyScreenDump(buf, 'Test_pum_rightleft_02', {'rows': 7})
1201 call term_sendkeys(buf, "\<C-Y>")
1202 call term_wait(buf)
1203 redraw!
1204 call assert_match('\s*eerht eno', Screenline(4))
1205
1206 call StopVimInTerminal(buf)
1207 call delete('Xtest1')
1208 call delete('Xtest2')
1209endfunc
1210
1211" Test for a popup menu with a scrollbar
1212func Test_pum_scrollbar()
1213 CheckScreendump
1214 let lines =<< trim END
1215 one
1216 two
1217 three
1218 END
1219 call writefile(lines, 'Xtest1')
1220 let buf = RunVimInTerminal('--cmd "set pumheight=2" Xtest1', {})
1221 call term_wait(buf)
1222 call term_sendkeys(buf, "Go\<C-P>\<C-P>\<C-P>")
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001223 call VerifyScreenDump(buf, 'Test_pum_scrollbar_01', {'rows': 7})
1224 call term_sendkeys(buf, "\<C-E>\<Esc>dd")
1225 call term_wait(buf)
1226
Dominique Pelle56cddb32021-06-04 21:09:55 +02001227 if has('rightleft')
1228 call term_sendkeys(buf, ":set rightleft\<CR>")
1229 call term_wait(buf)
1230 call term_sendkeys(buf, "Go\<C-P>\<C-P>\<C-P>")
Dominique Pelle56cddb32021-06-04 21:09:55 +02001231 call VerifyScreenDump(buf, 'Test_pum_scrollbar_02', {'rows': 7})
1232 endif
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001233
1234 call StopVimInTerminal(buf)
1235 call delete('Xtest1')
1236endfunc
1237
Bram Moolenaar47247282016-08-02 22:36:02 +02001238" vim: shiftwidth=2 sts=2 expandtab