blob: caec8ff0f5540c9eb210799adb8b8ae5cd7fce16 [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
Bram Moolenaar94722c52023-01-28 19:19:03 +000019 call complete(1, mth)
Bram Moolenaar47247282016-08-02 22:36:02 +020020 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
Bram Moolenaar94722c52023-01-28 19:19:03 +000077
Bram Moolenaar47247282016-08-02 22:36:02 +020078 " 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
Bram Moolenaar94722c52023-01-28 19:19:03 +000099
Bram Moolenaar47247282016-08-02 22:36:02 +0200100 " <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
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100761 call writefile(lines, 'Xscript', 'D')
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)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100775endfunc
776
Bram Moolenaar246fe032017-11-19 19:56:27 +0100777func Test_balloon_split()
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200778 CheckFunction balloon_split
779
Bram Moolenaar246fe032017-11-19 19:56:27 +0100780 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100781 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
782 \ ], balloon_split(
783 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
784 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100785 \ 'one two three four one two three four one two thre',
786 \ 'e four',
787 \ ], balloon_split(
788 \ 'one two three four one two three four one two three four'))
789
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200790 eval 'struct = {one = 1, two = 2, three = 3}'
791 \ ->balloon_split()
792 \ ->assert_equal([
793 \ 'struct = {',
794 \ ' one = 1,',
795 \ ' two = 2,',
796 \ ' three = 3}',
797 \ ])
Bram Moolenaar246fe032017-11-19 19:56:27 +0100798
799 call assert_equal([
800 \ 'struct = {',
801 \ ' one = 1,',
802 \ ' nested = {',
803 \ ' n1 = "yes",',
804 \ ' n2 = "no"}',
805 \ ' two = 2}',
806 \ ], balloon_split(
807 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
808 call assert_equal([
809 \ 'struct = 0x234 {',
810 \ ' long = 2343 "\\"some long string that will be wr',
811 \ 'apped in two\\"",',
812 \ ' next = 123}',
813 \ ], balloon_split(
814 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +0100815 call assert_equal([
816 \ 'Some comment',
817 \ '',
818 \ 'typedef this that;',
819 \ ], balloon_split(
820 \ "Some comment\n\ntypedef this that;"))
Bram Moolenaar246fe032017-11-19 19:56:27 +0100821endfunc
822
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100823func Test_popup_position()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100824 CheckScreendump
825
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200826 let lines =<< trim END
827 123456789_123456789_123456789_a
828 123456789_123456789_123456789_b
829 123
830 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100831 call writefile(lines, 'Xtest', 'D')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100832 let buf = RunVimInTerminal('Xtest', {})
833 call term_sendkeys(buf, ":vsplit\<CR>")
834
835 " default pumwidth in left window: overlap in right window
836 call term_sendkeys(buf, "GA\<C-N>")
837 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
838 call term_sendkeys(buf, "\<Esc>u")
839
840 " default pumwidth: fill until right of window
841 call term_sendkeys(buf, "\<C-W>l")
842 call term_sendkeys(buf, "GA\<C-N>")
843 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
844
845 " larger pumwidth: used as minimum width
846 call term_sendkeys(buf, "\<Esc>u")
847 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
848 call term_sendkeys(buf, "GA\<C-N>")
849 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
850
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100851 " completed text wider than the window and 'pumwidth' smaller than available
852 " space
853 call term_sendkeys(buf, "\<Esc>u")
854 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
855 call term_sendkeys(buf, "ggI123456789_\<Esc>")
856 call term_sendkeys(buf, "jI123456789_\<Esc>")
857 call term_sendkeys(buf, "GA\<C-N>")
858 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
Bram Moolenaar94722c52023-01-28 19:19:03 +0000859
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100860 call term_sendkeys(buf, "\<Esc>u")
861 call StopVimInTerminal(buf)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100862endfunc
863
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100864func Test_popup_command()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100865 CheckFeature menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100866
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100867 menu Test.Foo Foo
868 call assert_fails('popup Test.Foo', 'E336:')
869 call assert_fails('popup Test.Foo.X', 'E327:')
870 call assert_fails('popup Foo', 'E337:')
871 unmenu Test.Foo
zeertzjqf7570f22022-11-12 17:30:25 +0000872endfunc
873
874func Test_popup_command_dump()
875 CheckFeature menu
876 CheckScreendump
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100877
Bram Moolenaar38455a92020-12-24 18:39:02 +0100878 let script =<< trim END
879 func StartTimer()
880 call timer_start(100, {-> ChangeMenu()})
881 endfunc
882 func ChangeMenu()
zeertzjqf7570f22022-11-12 17:30:25 +0000883 aunmenu PopUp.&Paste
Bram Moolenaar38455a92020-12-24 18:39:02 +0100884 nnoremenu 1.40 PopUp.&Paste :echomsg "pasted"<CR>
885 echomsg 'changed'
886 endfunc
887 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100888 call writefile(script, 'XtimerScript', 'D')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100889
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200890 let lines =<< trim END
891 one two three four five
892 and one two Xthree four five
893 one more two three four five
894 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100895 call writefile(lines, 'Xtest', 'D')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100896 let buf = RunVimInTerminal('-S XtimerScript Xtest', {})
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100897 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
898 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
899 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
900
Bram Moolenaar38455a92020-12-24 18:39:02 +0100901 " go to the Paste entry in the menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100902 call term_sendkeys(buf, "jj")
903 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
904
905 " Select a word
906 call term_sendkeys(buf, "j\<CR>")
907 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
908
909 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar38455a92020-12-24 18:39:02 +0100910
zeertzjq883018f2024-06-15 15:37:11 +0200911 if has('rightleft')
912 call term_sendkeys(buf, ":set rightleft\<CR>")
913 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
914 call VerifyScreenDump(buf, 'Test_popup_command_rl', {})
915 call term_sendkeys(buf, "\<Esc>:set norightleft\<CR>")
916 endif
917
Bram Moolenaar38455a92020-12-24 18:39:02 +0100918 " Set a timer to change a menu entry while it's displayed. The text should
919 " not change but the command does. Making the screendump also verifies that
zeertzjq4e1ca0d2023-04-27 19:36:55 +0100920 " "changed" shows up, which means the timer triggered.
Bram Moolenaar38455a92020-12-24 18:39:02 +0100921 call term_sendkeys(buf, "/X\<CR>:call StartTimer() | popup PopUp\<CR>")
922 call VerifyScreenDump(buf, 'Test_popup_command_04', {})
923
924 " Select the Paste entry, executes the changed menu item.
925 call term_sendkeys(buf, "jj\<CR>")
926 call VerifyScreenDump(buf, 'Test_popup_command_05', {})
927
zeertzjq4e1ca0d2023-04-27 19:36:55 +0100928 call term_sendkeys(buf, "\<Esc>")
929
930 " Add a window toolbar to the window and check the :popup menu position.
931 call term_sendkeys(buf, ":nnoremenu WinBar.TEST :\<CR>")
932 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
933 call VerifyScreenDump(buf, 'Test_popup_command_06', {})
934
935 call term_sendkeys(buf, "\<Esc>")
936
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100937 call StopVimInTerminal(buf)
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100938endfunc
939
zeertzjq883018f2024-06-15 15:37:11 +0200940" Test position of right-click menu when clicking near window edge.
941func Test_mouse_popup_position()
942 CheckFeature menu
943 CheckScreendump
944
945 let script =<< trim END
946 set mousemodel=popup_setpos
947 source $VIMRUNTIME/menu.vim
948 call setline(1, join(range(20)))
949 func Trigger(col)
950 call test_setmouse(1, a:col)
951 call feedkeys("\<RightMouse>", 't')
952 endfunc
953 END
954 call writefile(script, 'XmousePopupPosition', 'D')
955 let buf = RunVimInTerminal('-S XmousePopupPosition', #{rows: 20, cols: 50})
956
957 call term_sendkeys(buf, ":call Trigger(45)\<CR>")
958 call VerifyScreenDump(buf, 'Test_mouse_popup_position_01', {})
959 call term_sendkeys(buf, "\<Esc>")
960
961 if has('rightleft')
962 call term_sendkeys(buf, ":set rightleft\<CR>")
963 call term_sendkeys(buf, ":call Trigger(50 + 1 - 45)\<CR>")
964 call VerifyScreenDump(buf, 'Test_mouse_popup_position_02', {})
965 call term_sendkeys(buf, "\<Esc>:set norightleft\<CR>")
966 endif
967
968 call StopVimInTerminal(buf)
969endfunc
970
Bram Moolenaare87edf32018-04-17 22:14:32 +0200971func Test_popup_complete_backwards()
972 new
973 call setline(1, ['Post', 'Port', 'Po'])
974 let expected=['Post', 'Port', 'Port']
975 call cursor(3,2)
976 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<cr>", 'tx')
977 call assert_equal(expected, getline(1,'$'))
978 bwipe!
979endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100980
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200981func Test_popup_complete_backwards_ctrl_p()
982 new
983 call setline(1, ['Post', 'Port', 'Po'])
984 let expected=['Post', 'Port', 'Port']
985 call cursor(3,2)
986 call feedkeys("A\<C-P>\<C-N>rt\<cr>", 'tx')
987 call assert_equal(expected, getline(1,'$'))
988 bwipe!
989endfunc
990
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200991func Test_complete_o_tab()
Bram Moolenaar39de9522018-05-08 22:48:00 +0200992 let s:o_char_pressed = 0
993
994 fun! s:act_on_text_changed()
995 if s:o_char_pressed
996 let s:o_char_pressed = 0
997 call feedkeys("\<c-x>\<c-n>", 'i')
998 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +0200999 endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +02001000
1001 set completeopt=menu,noselect
1002 new
1003 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
1004 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
1005 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
1006 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
1007 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
1008 call cursor(4,1)
1009 call test_override("char_avail", 1)
1010 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
1011 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
1012 call assert_equal(l:expected, getline(1,'$'))
1013
1014 call test_override("char_avail", 0)
1015 bwipe!
1016 set completeopt&
1017 delfunc s:act_on_text_changed
Bram Moolenaarae0f30b2018-06-12 15:22:43 +02001018endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +02001019
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001020func Test_menu_only_exists_in_terminal()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001021 CheckCommand tlmenu
1022 CheckNotGui
1023
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001024 tlnoremenu &Edit.&Paste<Tab>"+gP <C-W>"+
1025 aunmenu *
1026 try
1027 popup Edit
1028 call assert_false(1, 'command should have failed')
1029 catch
1030 call assert_exception('E328:')
1031 endtry
1032endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +02001033
zeertzjq92a16782022-07-26 12:24:41 +01001034" This used to crash before patch 8.1.1424
1035func Test_popup_delete_when_shown()
1036 CheckFeature menu
1037 CheckNotGui
1038
1039 func Func()
1040 popup Foo
1041 return "\<Ignore>"
1042 endfunc
1043
1044 nmenu Foo.Bar :
1045 nnoremap <expr> <F2> Func()
1046 call feedkeys("\<F2>\<F2>\<Esc>", 'xt')
1047
1048 delfunc Func
1049 nunmenu Foo.Bar
1050 nunmap <F2>
1051endfunc
1052
Bram Moolenaarfd133322019-03-29 12:20:27 +01001053func Test_popup_complete_info_01()
1054 new
1055 inoremap <buffer><F5> <C-R>=complete_info().mode<CR>
1056 func s:complTestEval() abort
1057 call complete(1, ['aa', 'ab'])
1058 return ''
1059 endfunc
1060 inoremap <buffer><F6> <C-R>=s:complTestEval()<CR>
1061 call writefile([
1062 \ 'dummy dummy.txt 1',
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001063 \], 'Xdummy.txt', 'D')
Bram Moolenaarfd133322019-03-29 12:20:27 +01001064 setlocal tags=Xdummy.txt
1065 setlocal dictionary=Xdummy.txt
1066 setlocal thesaurus=Xdummy.txt
1067 setlocal omnifunc=syntaxcomplete#Complete
1068 setlocal completefunc=syntaxcomplete#Complete
1069 setlocal spell
1070 for [keys, mode_name] in [
1071 \ ["", ''],
1072 \ ["\<C-X>", 'ctrl_x'],
1073 \ ["\<C-X>\<C-N>", 'keyword'],
1074 \ ["\<C-X>\<C-P>", 'keyword'],
zeertzjq27fef592021-10-03 12:01:27 +01001075 \ ["\<C-X>\<C-E>", 'scroll'],
1076 \ ["\<C-X>\<C-Y>", 'scroll'],
1077 \ ["\<C-X>\<C-E>\<C-E>\<C-Y>", 'scroll'],
1078 \ ["\<C-X>\<C-Y>\<C-E>\<C-Y>", 'scroll'],
Bram Moolenaarfd133322019-03-29 12:20:27 +01001079 \ ["\<C-X>\<C-L>", 'whole_line'],
1080 \ ["\<C-X>\<C-F>", 'files'],
1081 \ ["\<C-X>\<C-]>", 'tags'],
1082 \ ["\<C-X>\<C-D>", 'path_defines'],
1083 \ ["\<C-X>\<C-I>", 'path_patterns'],
1084 \ ["\<C-X>\<C-K>", 'dictionary'],
1085 \ ["\<C-X>\<C-T>", 'thesaurus'],
1086 \ ["\<C-X>\<C-V>", 'cmdline'],
1087 \ ["\<C-X>\<C-U>", 'function'],
1088 \ ["\<C-X>\<C-O>", 'omni'],
1089 \ ["\<C-X>s", 'spell'],
1090 \ ["\<F6>", 'eval'],
1091 \]
1092 call feedkeys("i" . keys . "\<F5>\<Esc>", 'tx')
1093 call assert_equal(mode_name, getline('.'))
1094 %d
1095 endfor
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001096
Bram Moolenaarfd133322019-03-29 12:20:27 +01001097 bwipe!
1098endfunc
1099
1100func UserDefinedComplete(findstart, base)
1101 if a:findstart
1102 return 0
1103 else
1104 return [
1105 \ { 'word': 'Jan', 'menu': 'January' },
1106 \ { 'word': 'Feb', 'menu': 'February' },
1107 \ { 'word': 'Mar', 'menu': 'March' },
1108 \ { 'word': 'Apr', 'menu': 'April' },
1109 \ { 'word': 'May', 'menu': 'May' },
1110 \ ]
1111 endif
1112endfunc
1113
1114func GetCompleteInfo()
1115 if empty(g:compl_what)
1116 let g:compl_info = complete_info()
1117 else
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001118 let g:compl_info = g:compl_what->complete_info()
Bram Moolenaarfd133322019-03-29 12:20:27 +01001119 endif
1120 return ''
1121endfunc
1122
1123func Test_popup_complete_info_02()
1124 new
1125 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
1126 setlocal completefunc=UserDefinedComplete
1127
1128 let d = {
1129 \ 'mode': 'function',
1130 \ 'pum_visible': 1,
1131 \ 'items': [
1132 \ {'word': 'Jan', 'menu': 'January', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1133 \ {'word': 'Feb', 'menu': 'February', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1134 \ {'word': 'Mar', 'menu': 'March', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1135 \ {'word': 'Apr', 'menu': 'April', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1136 \ {'word': 'May', 'menu': 'May', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}
1137 \ ],
1138 \ 'selected': 0,
1139 \ }
1140
1141 let g:compl_what = []
1142 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1143 call assert_equal(d, g:compl_info)
1144
1145 let g:compl_what = ['mode', 'pum_visible', 'selected']
1146 call remove(d, 'items')
1147 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1148 call assert_equal(d, g:compl_info)
1149
1150 let g:compl_what = ['mode']
1151 call remove(d, 'selected')
1152 call remove(d, 'pum_visible')
1153 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1154 call assert_equal(d, g:compl_info)
1155 bwipe!
1156endfunc
1157
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001158func Test_popup_complete_info_no_pum()
1159 new
1160 call assert_false( pumvisible() )
1161 let no_pum_info = complete_info()
1162 let d = {
1163 \ 'mode': '',
1164 \ 'pum_visible': 0,
1165 \ 'items': [],
1166 \ 'selected': -1,
1167 \ }
1168 call assert_equal( d, complete_info() )
1169 bwipe!
1170endfunc
1171
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001172func Test_CompleteChanged()
1173 new
1174 call setline(1, ['foo', 'bar', 'foobar', ''])
1175 set complete=. completeopt=noinsert,noselect,menuone
1176 function! OnPumChange()
1177 let g:event = copy(v:event)
1178 let g:item = get(v:event, 'completed_item', {})
1179 let g:word = get(g:item, 'word', v:null)
glepnir0d3c0a62024-02-11 17:52:40 +01001180 let l:line = getline('.')
1181 if g:word == v:null && l:line == "bc"
1182 let g:word = l:line
1183 endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001184 endfunction
1185 augroup AAAAA_Group
1186 au!
1187 autocmd CompleteChanged * :call OnPumChange()
1188 augroup END
1189 call cursor(4, 1)
1190
1191 call feedkeys("Sf\<C-N>", 'tx')
1192 call assert_equal({'completed_item': {}, 'width': 15,
1193 \ 'height': 2, 'size': 2,
1194 \ 'col': 0, 'row': 4, 'scrollbar': v:false}, g:event)
1195 call feedkeys("a\<C-N>\<C-N>\<C-E>", 'tx')
1196 call assert_equal('foo', g:word)
1197 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1198 call assert_equal('foobar', g:word)
1199 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1200 call assert_equal(v:null, g:word)
1201 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 'tx')
1202 call assert_equal('foobar', g:word)
glepnir0d3c0a62024-02-11 17:52:40 +01001203 call feedkeys("S\<C-N>bc", 'tx')
1204 call assert_equal("bc", g:word)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001205
glepnircbb46b42024-02-03 18:11:13 +01001206 func Omni_test(findstart, base)
1207 if a:findstart
1208 return col(".")
1209 endif
1210 return [#{word: "one"}, #{word: "two"}, #{word: "five"}]
1211 endfunc
1212 set omnifunc=Omni_test
1213 set completeopt=menu,menuone
1214 call feedkeys("i\<C-X>\<C-O>\<BS>\<BS>\<BS>f", 'tx')
1215 call assert_equal('five', g:word)
glepnir53387c52024-05-27 15:11:01 +02001216 call feedkeys("i\<C-X>\<C-O>\<BS>\<BS>\<BS>f\<BS>", 'tx')
1217 call assert_equal('one', g:word)
glepnircbb46b42024-02-03 18:11:13 +01001218
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001219 autocmd! AAAAA_Group
1220 set complete& completeopt&
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001221 delfunc! OnPumChange
glepnircbb46b42024-02-03 18:11:13 +01001222 delfunc! Omni_test
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001223 bw!
1224endfunc
1225
Bram Moolenaar97f0eb12022-10-06 19:49:13 +01001226func GetPumPosition()
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001227 call assert_true( pumvisible() )
1228 let g:pum_pos = pum_getpos()
1229 return ''
Bram Moolenaar97f0eb12022-10-06 19:49:13 +01001230endfunc
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001231
1232func Test_pum_getpos()
1233 new
1234 inoremap <buffer><F5> <C-R>=GetPumPosition()<CR>
1235 setlocal completefunc=UserDefinedComplete
1236
1237 let d = {
1238 \ 'height': 5,
1239 \ 'width': 15,
1240 \ 'row': 1,
1241 \ 'col': 0,
1242 \ 'size': 5,
1243 \ 'scrollbar': v:false,
1244 \ }
1245 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1246 call assert_equal(d, g:pum_pos)
1247
1248 call assert_false( pumvisible() )
1249 call assert_equal( {}, pum_getpos() )
1250 bw!
1251 unlet g:pum_pos
1252endfunc
1253
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001254" Test for the popup menu with the 'rightleft' option set
1255func Test_pum_rightleft()
Dominique Pelle56cddb32021-06-04 21:09:55 +02001256 CheckFeature rightleft
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001257 CheckScreendump
Dominique Pelle56cddb32021-06-04 21:09:55 +02001258
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001259 let lines =<< trim END
1260 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
1261 vim
1262 victory
1263 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001264 call writefile(lines, 'Xtest1', 'D')
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001265 let buf = RunVimInTerminal('--cmd "set rightleft" Xtest1', {})
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001266 call term_sendkeys(buf, "Go\<C-P>")
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001267 call VerifyScreenDump(buf, 'Test_pum_rightleft_01', {'rows': 8})
1268 call term_sendkeys(buf, "\<C-P>\<C-Y>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001269 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001270 redraw!
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001271 call WaitForAssert({-> assert_match('\s*miv', Screenline(5))})
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001272
1273 " Test for expanding tabs to spaces in the popup menu
1274 let lines =<< trim END
1275 one two
1276 one three
1277 four
1278 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001279 call writefile(lines, 'Xtest2', 'D')
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001280 call term_sendkeys(buf, "\<Esc>:e! Xtest2\<CR>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001281 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001282 call term_sendkeys(buf, "Goone\<C-X>\<C-L>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001283 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001284 redraw!
1285 call VerifyScreenDump(buf, 'Test_pum_rightleft_02', {'rows': 7})
1286 call term_sendkeys(buf, "\<C-Y>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001287 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001288 redraw!
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001289 call WaitForAssert({-> assert_match('\s*eerht eno', Screenline(4))})
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001290
1291 call StopVimInTerminal(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001292endfunc
1293
1294" Test for a popup menu with a scrollbar
1295func Test_pum_scrollbar()
1296 CheckScreendump
1297 let lines =<< trim END
1298 one
1299 two
1300 three
1301 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001302 call writefile(lines, 'Xtest1', 'D')
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001303 let buf = RunVimInTerminal('--cmd "set pumheight=2" Xtest1', {})
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001304 call TermWait(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001305 call term_sendkeys(buf, "Go\<C-P>\<C-P>\<C-P>")
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001306 call VerifyScreenDump(buf, 'Test_pum_scrollbar_01', {'rows': 7})
1307 call term_sendkeys(buf, "\<C-E>\<Esc>dd")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001308 call TermWait(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001309
Dominique Pelle56cddb32021-06-04 21:09:55 +02001310 if has('rightleft')
1311 call term_sendkeys(buf, ":set rightleft\<CR>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001312 call TermWait(buf)
Dominique Pelle56cddb32021-06-04 21:09:55 +02001313 call term_sendkeys(buf, "Go\<C-P>\<C-P>\<C-P>")
Dominique Pelle56cddb32021-06-04 21:09:55 +02001314 call VerifyScreenDump(buf, 'Test_pum_scrollbar_02', {'rows': 7})
1315 endif
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001316
1317 call StopVimInTerminal(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001318endfunc
1319
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +00001320" Test default highlight groups for popup menu
1321func Test_pum_highlights_default()
1322 CheckScreendump
1323 let lines =<< trim END
1324 func CompleteFunc( findstart, base )
1325 if a:findstart
1326 return 0
1327 endif
1328 return {
1329 \ 'words': [
1330 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'W', },
1331 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'W', },
1332 \ { 'word': 'aword3', 'menu': 'extra text 3', 'kind': 'W', },
1333 \]}
1334 endfunc
1335 set completeopt=menu
1336 set completefunc=CompleteFunc
1337 END
1338 call writefile(lines, 'Xscript', 'D')
1339 let buf = RunVimInTerminal('-S Xscript', {})
1340 call TermWait(buf)
1341 call term_sendkeys(buf, "iaw\<C-X>\<C-u>")
1342 call TermWait(buf, 50)
1343 call VerifyScreenDump(buf, 'Test_pum_highlights_01', {})
1344 call term_sendkeys(buf, "\<C-E>\<Esc>u")
1345 call TermWait(buf)
1346 call StopVimInTerminal(buf)
1347endfunc
1348
1349" Test custom highlight groups for popup menu
1350func Test_pum_highlights_custom()
1351 CheckScreendump
1352 let lines =<< trim END
1353 func CompleteFunc( findstart, base )
1354 if a:findstart
1355 return 0
1356 endif
1357 return {
1358 \ 'words': [
1359 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'W', },
1360 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'W', },
1361 \ { 'word': 'aword3', 'menu': 'extra text 3', 'kind': 'W', },
1362 \]}
1363 endfunc
1364 set completeopt=menu
1365 set completefunc=CompleteFunc
1366 hi PmenuKind ctermfg=1 ctermbg=225
1367 hi PmenuKindSel ctermfg=1 ctermbg=7
1368 hi PmenuExtra ctermfg=243 ctermbg=225
1369 hi PmenuExtraSel ctermfg=0 ctermbg=7
1370 END
1371 call writefile(lines, 'Xscript', 'D')
1372 let buf = RunVimInTerminal('-S Xscript', {})
1373 call TermWait(buf)
1374 call term_sendkeys(buf, "iaw\<C-X>\<C-u>")
1375 call TermWait(buf, 50)
1376 call VerifyScreenDump(buf, 'Test_pum_highlights_02', {})
1377 call term_sendkeys(buf, "\<C-E>\<Esc>u")
1378 call TermWait(buf)
1379 call StopVimInTerminal(buf)
1380endfunc
1381
glepnir40c1c332024-06-11 19:37:04 +02001382" Test match relate highlight group in pmenu
1383func Test_pum_highlights_match()
1384 CheckScreendump
1385 let lines =<< trim END
1386 func Omni_test(findstart, base)
1387 if a:findstart
1388 return col(".")
1389 endif
1390 return {
1391 \ 'words': [
zeertzjqafbe5352024-06-14 20:24:22 +02001392 \ { 'word': 'foo', 'kind': 'fookind' },
zeertzjq63901e82024-06-16 16:51:25 +02001393 \ { 'word': 'foofoo', 'kind': 'fookind' },
zeertzjqafbe5352024-06-14 20:24:22 +02001394 \ { 'word': 'foobar', 'kind': 'fookind' },
1395 \ { 'word': 'fooBaz', 'kind': 'fookind' },
1396 \ { 'word': 'foobala', 'kind': 'fookind' },
1397 \ { 'word': '你好' },
1398 \ { 'word': '你好吗' },
1399 \ { 'word': '你不好吗' },
1400 \ { 'word': '你可好吗' },
glepnir40c1c332024-06-11 19:37:04 +02001401 \]}
1402 endfunc
glepnirf1891382024-06-17 18:35:25 +02001403
1404 func Comp()
1405 let col = col('.')
1406 if getline('.') == 'f'
1407 let col -= 1
1408 endif
1409 call complete(col, [
1410 \ #{word: "foo", icase: 1},
1411 \ #{word: "Foobar", icase: 1},
1412 \ #{word: "fooBaz", icase: 1},
1413 \])
1414 return ''
1415 endfunc
1416
glepnir40c1c332024-06-11 19:37:04 +02001417 set omnifunc=Omni_test
1418 set completeopt=menu,noinsert,fuzzy
zeertzjq63901e82024-06-16 16:51:25 +02001419 hi PmenuMatchSel ctermfg=6 ctermbg=7
glepnir40c1c332024-06-11 19:37:04 +02001420 hi PmenuMatch ctermfg=4 ctermbg=225
1421 END
1422 call writefile(lines, 'Xscript', 'D')
1423 let buf = RunVimInTerminal('-S Xscript', {})
1424 call TermWait(buf)
1425 call term_sendkeys(buf, "i\<C-X>\<C-O>")
1426 call TermWait(buf, 50)
1427 call term_sendkeys(buf, "fo")
1428 call TermWait(buf, 50)
1429 call VerifyScreenDump(buf, 'Test_pum_highlights_03', {})
zeertzjq63901e82024-06-16 16:51:25 +02001430 call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
glepnir40c1c332024-06-11 19:37:04 +02001431 call TermWait(buf, 50)
1432 call term_sendkeys(buf, "你")
1433 call TermWait(buf, 50)
1434 call VerifyScreenDump(buf, 'Test_pum_highlights_04', {})
1435 call term_sendkeys(buf, "吗")
1436 call TermWait(buf, 50)
1437 call VerifyScreenDump(buf, 'Test_pum_highlights_05', {})
zeertzjq63901e82024-06-16 16:51:25 +02001438 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir40c1c332024-06-11 19:37:04 +02001439
1440 if has('rightleft')
zeertzjq63901e82024-06-16 16:51:25 +02001441 call term_sendkeys(buf, ":set rightleft\<CR>")
glepnir40c1c332024-06-11 19:37:04 +02001442 call TermWait(buf, 50)
zeertzjq63901e82024-06-16 16:51:25 +02001443 call term_sendkeys(buf, "S\<C-X>\<C-O>")
glepnir40c1c332024-06-11 19:37:04 +02001444 call TermWait(buf, 50)
1445 call term_sendkeys(buf, "fo")
1446 call TermWait(buf, 50)
1447 call VerifyScreenDump(buf, 'Test_pum_highlights_06', {})
zeertzjq63901e82024-06-16 16:51:25 +02001448 call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
1449 call TermWait(buf, 50)
1450 call term_sendkeys(buf, "你")
1451 call VerifyScreenDump(buf, 'Test_pum_highlights_06a', {})
1452 call term_sendkeys(buf, "吗")
1453 call VerifyScreenDump(buf, 'Test_pum_highlights_06b', {})
1454 call term_sendkeys(buf, "\<C-E>\<Esc>")
1455 call term_sendkeys(buf, ":set norightleft\<CR>")
glepnir40c1c332024-06-11 19:37:04 +02001456 call TermWait(buf)
1457 endif
1458
1459 call term_sendkeys(buf, ":set completeopt-=fuzzy\<CR>")
1460 call TermWait(buf)
zeertzjq63901e82024-06-16 16:51:25 +02001461 call term_sendkeys(buf, "S\<C-X>\<C-O>")
glepnir40c1c332024-06-11 19:37:04 +02001462 call TermWait(buf, 50)
1463 call term_sendkeys(buf, "fo")
1464 call TermWait(buf, 50)
1465 call VerifyScreenDump(buf, 'Test_pum_highlights_07', {})
zeertzjq63901e82024-06-16 16:51:25 +02001466 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir40c1c332024-06-11 19:37:04 +02001467
zeertzjq63901e82024-06-16 16:51:25 +02001468 if has('rightleft')
1469 call term_sendkeys(buf, ":set rightleft\<CR>")
1470 call TermWait(buf, 50)
1471 call term_sendkeys(buf, "S\<C-X>\<C-O>")
1472 call TermWait(buf, 50)
1473 call term_sendkeys(buf, "fo")
1474 call TermWait(buf, 50)
1475 call VerifyScreenDump(buf, 'Test_pum_highlights_08', {})
1476 call term_sendkeys(buf, "\<C-E>\<Esc>")
1477 call term_sendkeys(buf, ":set norightleft\<CR>")
1478 endif
1479
glepnirf1891382024-06-17 18:35:25 +02001480 call term_sendkeys(buf, "S\<C-R>=Comp()\<CR>f")
1481 call VerifyScreenDump(buf, 'Test_pum_highlights_09', {})
1482 call term_sendkeys(buf, "o\<BS>\<C-R>=Comp()\<CR>")
1483 call VerifyScreenDump(buf, 'Test_pum_highlights_09', {})
1484
glepnir65407ce2024-07-06 16:09:19 +02001485 " issue #15095 wrong select
1486 call term_sendkeys(buf, "\<ESC>:set completeopt=fuzzy,menu\<CR>")
1487 call TermWait(buf)
1488 call term_sendkeys(buf, "S hello helio hero h\<C-X>\<C-P>")
1489 call TermWait(buf, 50)
1490 call VerifyScreenDump(buf, 'Test_pum_highlights_10', {})
1491
1492 call term_sendkeys(buf, "\<ESC>S hello helio hero h\<C-X>\<C-P>\<C-P>")
1493 call TermWait(buf, 50)
1494 call VerifyScreenDump(buf, 'Test_pum_highlights_11', {})
1495
glepnir6b6280c2024-07-27 16:25:45 +02001496 " issue #15357
zeertzjq58d70522024-08-31 17:05:39 +02001497 call term_sendkeys(buf, "\<ESC>S/non_existing_folder\<C-X>\<C-F>")
glepnir6b6280c2024-07-27 16:25:45 +02001498 call TermWait(buf, 50)
1499 call VerifyScreenDump(buf, 'Test_pum_highlights_15', {})
1500
glepnirf1891382024-06-17 18:35:25 +02001501 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir40c1c332024-06-11 19:37:04 +02001502 call TermWait(buf)
glepnirf1891382024-06-17 18:35:25 +02001503
glepnir40c1c332024-06-11 19:37:04 +02001504 call StopVimInTerminal(buf)
1505endfunc
1506
zeertzjq41008522024-07-27 13:21:49 +02001507func Test_pum_user_hl_group()
glepnir508e7852024-07-25 21:39:08 +02001508 CheckScreendump
1509 let lines =<< trim END
glepnir508e7852024-07-25 21:39:08 +02001510 func CompleteFunc( findstart, base )
1511 if a:findstart
1512 return 0
1513 endif
1514 return {
1515 \ 'words': [
glepnir8754efe2024-07-26 18:15:27 +02001516 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'W', 'hl_group': 'StrikeFake' },
glepnir508e7852024-07-25 21:39:08 +02001517 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'W', },
glepnir8754efe2024-07-26 18:15:27 +02001518 \ { 'word': '你好', 'menu': 'extra text 3', 'kind': 'W', 'hl_group': 'StrikeFake' },
glepnir508e7852024-07-25 21:39:08 +02001519 \]}
1520 endfunc
1521 set completeopt=menu
1522 set completefunc=CompleteFunc
zeertzjq41008522024-07-27 13:21:49 +02001523
1524 hi StrikeFake ctermfg=9
1525 func HlMatch()
1526 hi PmenuMatchSel ctermfg=6 ctermbg=7 cterm=underline
1527 hi PmenuMatch ctermfg=4 ctermbg=225 cterm=underline
1528 endfunc
glepnir508e7852024-07-25 21:39:08 +02001529 END
1530 call writefile(lines, 'Xscript', 'D')
1531 let buf = RunVimInTerminal('-S Xscript', {})
zeertzjq41008522024-07-27 13:21:49 +02001532
glepnir508e7852024-07-25 21:39:08 +02001533 call TermWait(buf)
zeertzjq41008522024-07-27 13:21:49 +02001534 call term_sendkeys(buf, "Saw\<C-X>\<C-U>")
glepnir508e7852024-07-25 21:39:08 +02001535 call VerifyScreenDump(buf, 'Test_pum_highlights_12', {})
zeertzjq41008522024-07-27 13:21:49 +02001536 call term_sendkeys(buf, "\<C-E>\<Esc>")
1537
glepnir508e7852024-07-25 21:39:08 +02001538 call TermWait(buf)
zeertzjq41008522024-07-27 13:21:49 +02001539 call term_sendkeys(buf, ":call HlMatch()\<CR>")
1540
1541 call TermWait(buf)
1542 call term_sendkeys(buf, "Saw\<C-X>\<C-U>")
1543 call VerifyScreenDump(buf, 'Test_pum_highlights_13', {})
1544 call term_sendkeys(buf, "\<C-N>")
1545 call VerifyScreenDump(buf, 'Test_pum_highlights_14', {})
1546 call term_sendkeys(buf, "\<C-E>\<Esc>")
1547
glepnir508e7852024-07-25 21:39:08 +02001548 call StopVimInTerminal(buf)
1549endfunc
1550
glepnir38f99a12024-08-23 18:31:06 +02001551func Test_pum_user_kind_hlgroup()
1552 CheckScreendump
1553 let lines =<< trim END
1554 func CompleteFunc( findstart, base )
1555 if a:findstart
1556 return 0
1557 endif
1558 return {
1559 \ 'words': [
1560 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'variable', 'kind_hlgroup': 'KindVar', 'hl_group': 'StrikeFake' },
1561 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'function', 'kind_hlgroup': 'KindFunc' },
1562 \ { 'word': '你好', 'menu': 'extra text 3', 'kind': 'class', 'kind_hlgroup': 'KindClass' },
1563 \]}
1564 endfunc
1565 set completeopt=menu
1566 set completefunc=CompleteFunc
1567
1568 hi StrikeFake ctermfg=9
1569 hi KindVar ctermfg=yellow
1570 hi KindFunc ctermfg=blue
1571 hi KindClass ctermfg=green
1572 END
1573 call writefile(lines, 'Xscript', 'D')
1574 let buf = RunVimInTerminal('-S Xscript', {})
1575
1576 call TermWait(buf)
1577 call term_sendkeys(buf, "S\<C-X>\<C-U>")
1578 call VerifyScreenDump(buf, 'Test_pum_highlights_16', {})
1579 call term_sendkeys(buf, "\<C-E>\<Esc>")
1580
1581 call StopVimInTerminal(buf)
1582endfunc
1583
Bram Moolenaar47247282016-08-02 22:36:02 +02001584" vim: shiftwidth=2 sts=2 expandtab