blob: 82d2ef0be4b5b9c600280dfe8964eb484be75c41 [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
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100751func s:run_popup_and_previewwindow_dump(lines, dumpfile)
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100752 CheckScreendump
753 CheckFeature quickfix
754
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100755 call writefile(a:lines, 'Xscript', 'D')
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100756 let buf = RunVimInTerminal('-S Xscript', {})
757
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100758 " wait for the script to finish
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200759 call TermWait(buf)
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100760
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100761 " Test that popup and previewwindow do not overlap.
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100762 call term_sendkeys(buf, "o")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200763 call TermWait(buf, 50)
Bram Moolenaar37bb0302020-03-27 20:24:14 +0100764 call term_sendkeys(buf, "\<C-X>\<C-N>")
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100765 call VerifyScreenDump(buf, a:dumpfile, {})
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100766
767 call term_sendkeys(buf, "\<Esc>u")
768 call StopVimInTerminal(buf)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100769endfunc
770
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100771func Test_popup_and_previewwindow_dump_pedit()
772 let lines =<< trim END
773 set previewheight=9
774 silent! pedit
775 call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
776 exec "norm! G\<C-E>\<C-E>"
777 END
778 call s:run_popup_and_previewwindow_dump(lines, 'Test_popup_and_previewwindow_pedit')
779endfunc
780
781func Test_popup_and_previewwindow_dump_pbuffer()
782 let lines =<< trim END
783 set previewheight=9
784 silent! pbuffer
785 call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
786 exec "norm! G\<C-E>\<C-E>\<C-E>"
787 END
788 call s:run_popup_and_previewwindow_dump(lines, 'Test_popup_and_previewwindow_pbuffer')
789endfunc
790
Bram Moolenaar246fe032017-11-19 19:56:27 +0100791func Test_balloon_split()
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200792 CheckFunction balloon_split
793
Bram Moolenaar246fe032017-11-19 19:56:27 +0100794 call assert_equal([
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100795 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
796 \ ], balloon_split(
797 \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"'))
798 call assert_equal([
Bram Moolenaar246fe032017-11-19 19:56:27 +0100799 \ 'one two three four one two three four one two thre',
800 \ 'e four',
801 \ ], balloon_split(
802 \ 'one two three four one two three four one two three four'))
803
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200804 eval 'struct = {one = 1, two = 2, three = 3}'
805 \ ->balloon_split()
806 \ ->assert_equal([
807 \ 'struct = {',
808 \ ' one = 1,',
809 \ ' two = 2,',
810 \ ' three = 3}',
811 \ ])
Bram Moolenaar246fe032017-11-19 19:56:27 +0100812
813 call assert_equal([
814 \ 'struct = {',
815 \ ' one = 1,',
816 \ ' nested = {',
817 \ ' n1 = "yes",',
818 \ ' n2 = "no"}',
819 \ ' two = 2}',
820 \ ], balloon_split(
821 \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}'))
822 call assert_equal([
823 \ 'struct = 0x234 {',
824 \ ' long = 2343 "\\"some long string that will be wr',
825 \ 'apped in two\\"",',
826 \ ' next = 123}',
827 \ ], balloon_split(
828 \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}'))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +0100829 call assert_equal([
830 \ 'Some comment',
831 \ '',
832 \ 'typedef this that;',
833 \ ], balloon_split(
834 \ "Some comment\n\ntypedef this that;"))
Bram Moolenaar246fe032017-11-19 19:56:27 +0100835endfunc
836
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100837func Test_popup_position()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100838 CheckScreendump
839
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200840 let lines =<< trim END
841 123456789_123456789_123456789_a
842 123456789_123456789_123456789_b
843 123
844 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100845 call writefile(lines, 'Xtest', 'D')
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100846 let buf = RunVimInTerminal('Xtest', {})
847 call term_sendkeys(buf, ":vsplit\<CR>")
848
849 " default pumwidth in left window: overlap in right window
850 call term_sendkeys(buf, "GA\<C-N>")
851 call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8})
852 call term_sendkeys(buf, "\<Esc>u")
853
854 " default pumwidth: fill until right of window
855 call term_sendkeys(buf, "\<C-W>l")
856 call term_sendkeys(buf, "GA\<C-N>")
857 call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8})
858
859 " larger pumwidth: used as minimum width
860 call term_sendkeys(buf, "\<Esc>u")
861 call term_sendkeys(buf, ":set pumwidth=30\<CR>")
862 call term_sendkeys(buf, "GA\<C-N>")
863 call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8})
864
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100865 " completed text wider than the window and 'pumwidth' smaller than available
866 " space
867 call term_sendkeys(buf, "\<Esc>u")
868 call term_sendkeys(buf, ":set pumwidth=20\<CR>")
869 call term_sendkeys(buf, "ggI123456789_\<Esc>")
870 call term_sendkeys(buf, "jI123456789_\<Esc>")
871 call term_sendkeys(buf, "GA\<C-N>")
872 call VerifyScreenDump(buf, 'Test_popup_position_04', {'rows': 10})
Bram Moolenaar94722c52023-01-28 19:19:03 +0000873
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100874 call term_sendkeys(buf, "\<Esc>u")
875 call StopVimInTerminal(buf)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100876endfunc
877
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100878func Test_popup_command()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100879 CheckFeature menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100880
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100881 menu Test.Foo Foo
882 call assert_fails('popup Test.Foo', 'E336:')
883 call assert_fails('popup Test.Foo.X', 'E327:')
884 call assert_fails('popup Foo', 'E337:')
885 unmenu Test.Foo
zeertzjqf7570f22022-11-12 17:30:25 +0000886endfunc
887
888func Test_popup_command_dump()
889 CheckFeature menu
890 CheckScreendump
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100891
Bram Moolenaar38455a92020-12-24 18:39:02 +0100892 let script =<< trim END
893 func StartTimer()
894 call timer_start(100, {-> ChangeMenu()})
895 endfunc
896 func ChangeMenu()
zeertzjqf7570f22022-11-12 17:30:25 +0000897 aunmenu PopUp.&Paste
Bram Moolenaar38455a92020-12-24 18:39:02 +0100898 nnoremenu 1.40 PopUp.&Paste :echomsg "pasted"<CR>
899 echomsg 'changed'
900 endfunc
901 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100902 call writefile(script, 'XtimerScript', 'D')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100903
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200904 let lines =<< trim END
905 one two three four five
906 and one two Xthree four five
907 one more two three four five
908 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100909 call writefile(lines, 'Xtest', 'D')
Bram Moolenaar38455a92020-12-24 18:39:02 +0100910 let buf = RunVimInTerminal('-S XtimerScript Xtest', {})
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100911 call term_sendkeys(buf, ":source $VIMRUNTIME/menu.vim\<CR>")
912 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
913 call VerifyScreenDump(buf, 'Test_popup_command_01', {})
914
Bram Moolenaar38455a92020-12-24 18:39:02 +0100915 " go to the Paste entry in the menu
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100916 call term_sendkeys(buf, "jj")
917 call VerifyScreenDump(buf, 'Test_popup_command_02', {})
918
919 " Select a word
920 call term_sendkeys(buf, "j\<CR>")
921 call VerifyScreenDump(buf, 'Test_popup_command_03', {})
922
923 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar38455a92020-12-24 18:39:02 +0100924
zeertzjq883018f2024-06-15 15:37:11 +0200925 if has('rightleft')
926 call term_sendkeys(buf, ":set rightleft\<CR>")
927 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
928 call VerifyScreenDump(buf, 'Test_popup_command_rl', {})
929 call term_sendkeys(buf, "\<Esc>:set norightleft\<CR>")
930 endif
931
Bram Moolenaar38455a92020-12-24 18:39:02 +0100932 " Set a timer to change a menu entry while it's displayed. The text should
933 " not change but the command does. Making the screendump also verifies that
zeertzjq4e1ca0d2023-04-27 19:36:55 +0100934 " "changed" shows up, which means the timer triggered.
Bram Moolenaar38455a92020-12-24 18:39:02 +0100935 call term_sendkeys(buf, "/X\<CR>:call StartTimer() | popup PopUp\<CR>")
936 call VerifyScreenDump(buf, 'Test_popup_command_04', {})
937
938 " Select the Paste entry, executes the changed menu item.
939 call term_sendkeys(buf, "jj\<CR>")
940 call VerifyScreenDump(buf, 'Test_popup_command_05', {})
941
zeertzjq4e1ca0d2023-04-27 19:36:55 +0100942 call term_sendkeys(buf, "\<Esc>")
943
944 " Add a window toolbar to the window and check the :popup menu position.
945 call term_sendkeys(buf, ":nnoremenu WinBar.TEST :\<CR>")
946 call term_sendkeys(buf, "/X\<CR>:popup PopUp\<CR>")
947 call VerifyScreenDump(buf, 'Test_popup_command_06', {})
948
949 call term_sendkeys(buf, "\<Esc>")
950
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100951 call StopVimInTerminal(buf)
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100952endfunc
953
zeertzjq883018f2024-06-15 15:37:11 +0200954" Test position of right-click menu when clicking near window edge.
955func Test_mouse_popup_position()
956 CheckFeature menu
957 CheckScreendump
958
959 let script =<< trim END
960 set mousemodel=popup_setpos
961 source $VIMRUNTIME/menu.vim
962 call setline(1, join(range(20)))
963 func Trigger(col)
964 call test_setmouse(1, a:col)
965 call feedkeys("\<RightMouse>", 't')
966 endfunc
967 END
968 call writefile(script, 'XmousePopupPosition', 'D')
969 let buf = RunVimInTerminal('-S XmousePopupPosition', #{rows: 20, cols: 50})
970
971 call term_sendkeys(buf, ":call Trigger(45)\<CR>")
972 call VerifyScreenDump(buf, 'Test_mouse_popup_position_01', {})
973 call term_sendkeys(buf, "\<Esc>")
974
975 if has('rightleft')
976 call term_sendkeys(buf, ":set rightleft\<CR>")
977 call term_sendkeys(buf, ":call Trigger(50 + 1 - 45)\<CR>")
978 call VerifyScreenDump(buf, 'Test_mouse_popup_position_02', {})
979 call term_sendkeys(buf, "\<Esc>:set norightleft\<CR>")
980 endif
981
982 call StopVimInTerminal(buf)
983endfunc
984
Bram Moolenaare87edf32018-04-17 22:14:32 +0200985func Test_popup_complete_backwards()
986 new
987 call setline(1, ['Post', 'Port', 'Po'])
988 let expected=['Post', 'Port', 'Port']
989 call cursor(3,2)
glepnir07f0dbe2025-02-18 20:27:30 +0100990 call feedkeys("A\<C-X>". repeat("\<C-P>", 3). "rt\<C-Y>", 'tx')
Bram Moolenaare87edf32018-04-17 22:14:32 +0200991 call assert_equal(expected, getline(1,'$'))
992 bwipe!
993endfunc
Bram Moolenaar69f5a302018-03-06 13:23:08 +0100994
Bram Moolenaarbad0ce72018-04-17 23:31:05 +0200995func Test_popup_complete_backwards_ctrl_p()
996 new
997 call setline(1, ['Post', 'Port', 'Po'])
998 let expected=['Post', 'Port', 'Port']
999 call cursor(3,2)
glepnir07f0dbe2025-02-18 20:27:30 +01001000 call feedkeys("A\<C-P>\<C-N>rt\<C-Y>", 'tx')
Bram Moolenaarbad0ce72018-04-17 23:31:05 +02001001 call assert_equal(expected, getline(1,'$'))
1002 bwipe!
1003endfunc
1004
Bram Moolenaarae0f30b2018-06-12 15:22:43 +02001005func Test_complete_o_tab()
Bram Moolenaar39de9522018-05-08 22:48:00 +02001006 let s:o_char_pressed = 0
1007
1008 fun! s:act_on_text_changed()
1009 if s:o_char_pressed
1010 let s:o_char_pressed = 0
1011 call feedkeys("\<c-x>\<c-n>", 'i')
1012 endif
Bram Moolenaarae0f30b2018-06-12 15:22:43 +02001013 endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +02001014
1015 set completeopt=menu,noselect
1016 new
1017 imap <expr> <buffer> <tab> pumvisible() ? "\<c-p>" : "X"
1018 autocmd! InsertCharPre <buffer> let s:o_char_pressed = (v:char ==# 'o')
1019 autocmd! TextChangedI <buffer> call <sid>act_on_text_changed()
1020 call setline(1, ['hoard', 'hoax', 'hoarse', ''])
1021 let l:expected = ['hoard', 'hoax', 'hoarse', 'hoax', 'hoax']
1022 call cursor(4,1)
1023 call test_override("char_avail", 1)
1024 call feedkeys("Ahoa\<tab>\<tab>\<c-y>\<esc>", 'tx')
1025 call feedkeys("oho\<tab>\<tab>\<c-y>\<esc>", 'tx')
1026 call assert_equal(l:expected, getline(1,'$'))
1027
1028 call test_override("char_avail", 0)
1029 bwipe!
1030 set completeopt&
1031 delfunc s:act_on_text_changed
Bram Moolenaarae0f30b2018-06-12 15:22:43 +02001032endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +02001033
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001034func Test_menu_only_exists_in_terminal()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001035 CheckCommand tlmenu
1036 CheckNotGui
1037
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001038 tlnoremenu &Edit.&Paste<Tab>"+gP <C-W>"+
1039 aunmenu *
1040 try
1041 popup Edit
1042 call assert_false(1, 'command should have failed')
1043 catch
1044 call assert_exception('E328:')
1045 endtry
1046endfunc
Bram Moolenaar39de9522018-05-08 22:48:00 +02001047
zeertzjq92a16782022-07-26 12:24:41 +01001048" This used to crash before patch 8.1.1424
1049func Test_popup_delete_when_shown()
1050 CheckFeature menu
1051 CheckNotGui
1052
1053 func Func()
1054 popup Foo
1055 return "\<Ignore>"
1056 endfunc
1057
1058 nmenu Foo.Bar :
1059 nnoremap <expr> <F2> Func()
1060 call feedkeys("\<F2>\<F2>\<Esc>", 'xt')
1061
1062 delfunc Func
1063 nunmenu Foo.Bar
1064 nunmap <F2>
1065endfunc
1066
Bram Moolenaarfd133322019-03-29 12:20:27 +01001067func Test_popup_complete_info_01()
1068 new
1069 inoremap <buffer><F5> <C-R>=complete_info().mode<CR>
1070 func s:complTestEval() abort
1071 call complete(1, ['aa', 'ab'])
1072 return ''
1073 endfunc
1074 inoremap <buffer><F6> <C-R>=s:complTestEval()<CR>
1075 call writefile([
1076 \ 'dummy dummy.txt 1',
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001077 \], 'Xdummy.txt', 'D')
Bram Moolenaarfd133322019-03-29 12:20:27 +01001078 setlocal tags=Xdummy.txt
1079 setlocal dictionary=Xdummy.txt
1080 setlocal thesaurus=Xdummy.txt
1081 setlocal omnifunc=syntaxcomplete#Complete
1082 setlocal completefunc=syntaxcomplete#Complete
1083 setlocal spell
1084 for [keys, mode_name] in [
1085 \ ["", ''],
1086 \ ["\<C-X>", 'ctrl_x'],
1087 \ ["\<C-X>\<C-N>", 'keyword'],
1088 \ ["\<C-X>\<C-P>", 'keyword'],
zeertzjq27fef592021-10-03 12:01:27 +01001089 \ ["\<C-X>\<C-E>", 'scroll'],
1090 \ ["\<C-X>\<C-Y>", 'scroll'],
1091 \ ["\<C-X>\<C-E>\<C-E>\<C-Y>", 'scroll'],
1092 \ ["\<C-X>\<C-Y>\<C-E>\<C-Y>", 'scroll'],
Bram Moolenaarfd133322019-03-29 12:20:27 +01001093 \ ["\<C-X>\<C-L>", 'whole_line'],
1094 \ ["\<C-X>\<C-F>", 'files'],
1095 \ ["\<C-X>\<C-]>", 'tags'],
1096 \ ["\<C-X>\<C-D>", 'path_defines'],
1097 \ ["\<C-X>\<C-I>", 'path_patterns'],
1098 \ ["\<C-X>\<C-K>", 'dictionary'],
1099 \ ["\<C-X>\<C-T>", 'thesaurus'],
1100 \ ["\<C-X>\<C-V>", 'cmdline'],
1101 \ ["\<C-X>\<C-U>", 'function'],
1102 \ ["\<C-X>\<C-O>", 'omni'],
1103 \ ["\<C-X>s", 'spell'],
1104 \ ["\<F6>", 'eval'],
1105 \]
1106 call feedkeys("i" . keys . "\<F5>\<Esc>", 'tx')
1107 call assert_equal(mode_name, getline('.'))
1108 %d
1109 endfor
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001110
Bram Moolenaarfd133322019-03-29 12:20:27 +01001111 bwipe!
1112endfunc
1113
1114func UserDefinedComplete(findstart, base)
1115 if a:findstart
1116 return 0
1117 else
1118 return [
1119 \ { 'word': 'Jan', 'menu': 'January' },
1120 \ { 'word': 'Feb', 'menu': 'February' },
1121 \ { 'word': 'Mar', 'menu': 'March' },
1122 \ { 'word': 'Apr', 'menu': 'April' },
1123 \ { 'word': 'May', 'menu': 'May' },
1124 \ ]
1125 endif
1126endfunc
1127
1128func GetCompleteInfo()
1129 if empty(g:compl_what)
1130 let g:compl_info = complete_info()
1131 else
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001132 let g:compl_info = g:compl_what->complete_info()
Bram Moolenaarfd133322019-03-29 12:20:27 +01001133 endif
1134 return ''
1135endfunc
1136
1137func Test_popup_complete_info_02()
1138 new
1139 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
1140 setlocal completefunc=UserDefinedComplete
1141
1142 let d = {
1143 \ 'mode': 'function',
1144 \ 'pum_visible': 1,
1145 \ 'items': [
1146 \ {'word': 'Jan', 'menu': 'January', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1147 \ {'word': 'Feb', 'menu': 'February', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1148 \ {'word': 'Mar', 'menu': 'March', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1149 \ {'word': 'Apr', 'menu': 'April', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
1150 \ {'word': 'May', 'menu': 'May', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}
1151 \ ],
1152 \ 'selected': 0,
1153 \ }
1154
1155 let g:compl_what = []
1156 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1157 call assert_equal(d, g:compl_info)
1158
1159 let g:compl_what = ['mode', 'pum_visible', 'selected']
1160 call remove(d, 'items')
1161 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1162 call assert_equal(d, g:compl_info)
1163
1164 let g:compl_what = ['mode']
1165 call remove(d, 'selected')
1166 call remove(d, 'pum_visible')
1167 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1168 call assert_equal(d, g:compl_info)
1169 bwipe!
1170endfunc
1171
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001172func Test_popup_complete_info_no_pum()
1173 new
1174 call assert_false( pumvisible() )
1175 let no_pum_info = complete_info()
1176 let d = {
1177 \ 'mode': '',
1178 \ 'pum_visible': 0,
1179 \ 'items': [],
1180 \ 'selected': -1,
1181 \ }
1182 call assert_equal( d, complete_info() )
1183 bwipe!
1184endfunc
1185
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001186func Test_CompleteChanged()
1187 new
1188 call setline(1, ['foo', 'bar', 'foobar', ''])
1189 set complete=. completeopt=noinsert,noselect,menuone
1190 function! OnPumChange()
1191 let g:event = copy(v:event)
1192 let g:item = get(v:event, 'completed_item', {})
1193 let g:word = get(g:item, 'word', v:null)
glepnir0d3c0a62024-02-11 17:52:40 +01001194 let l:line = getline('.')
1195 if g:word == v:null && l:line == "bc"
1196 let g:word = l:line
1197 endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001198 endfunction
1199 augroup AAAAA_Group
1200 au!
1201 autocmd CompleteChanged * :call OnPumChange()
1202 augroup END
1203 call cursor(4, 1)
1204
1205 call feedkeys("Sf\<C-N>", 'tx')
1206 call assert_equal({'completed_item': {}, 'width': 15,
1207 \ 'height': 2, 'size': 2,
1208 \ 'col': 0, 'row': 4, 'scrollbar': v:false}, g:event)
1209 call feedkeys("a\<C-N>\<C-N>\<C-E>", 'tx')
1210 call assert_equal('foo', g:word)
1211 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1212 call assert_equal('foobar', g:word)
1213 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-E>", 'tx')
1214 call assert_equal(v:null, g:word)
1215 call feedkeys("a\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 'tx')
1216 call assert_equal('foobar', g:word)
glepnir0d3c0a62024-02-11 17:52:40 +01001217 call feedkeys("S\<C-N>bc", 'tx')
1218 call assert_equal("bc", g:word)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001219
glepnircbb46b42024-02-03 18:11:13 +01001220 func Omni_test(findstart, base)
1221 if a:findstart
1222 return col(".")
1223 endif
1224 return [#{word: "one"}, #{word: "two"}, #{word: "five"}]
1225 endfunc
1226 set omnifunc=Omni_test
1227 set completeopt=menu,menuone
1228 call feedkeys("i\<C-X>\<C-O>\<BS>\<BS>\<BS>f", 'tx')
1229 call assert_equal('five', g:word)
glepnir53387c52024-05-27 15:11:01 +02001230 call feedkeys("i\<C-X>\<C-O>\<BS>\<BS>\<BS>f\<BS>", 'tx')
1231 call assert_equal('one', g:word)
glepnircbb46b42024-02-03 18:11:13 +01001232
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001233 autocmd! AAAAA_Group
1234 set complete& completeopt&
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001235 delfunc! OnPumChange
glepnircbb46b42024-02-03 18:11:13 +01001236 delfunc! Omni_test
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001237 bw!
1238endfunc
1239
Bram Moolenaar97f0eb12022-10-06 19:49:13 +01001240func GetPumPosition()
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001241 call assert_true( pumvisible() )
1242 let g:pum_pos = pum_getpos()
1243 return ''
Bram Moolenaar97f0eb12022-10-06 19:49:13 +01001244endfunc
Bram Moolenaare9bd5722019-08-17 19:36:06 +02001245
1246func Test_pum_getpos()
1247 new
1248 inoremap <buffer><F5> <C-R>=GetPumPosition()<CR>
1249 setlocal completefunc=UserDefinedComplete
1250
1251 let d = {
1252 \ 'height': 5,
1253 \ 'width': 15,
1254 \ 'row': 1,
1255 \ 'col': 0,
1256 \ 'size': 5,
1257 \ 'scrollbar': v:false,
1258 \ }
1259 call feedkeys("i\<C-X>\<C-U>\<F5>", 'tx')
1260 call assert_equal(d, g:pum_pos)
1261
1262 call assert_false( pumvisible() )
1263 call assert_equal( {}, pum_getpos() )
1264 bw!
1265 unlet g:pum_pos
1266endfunc
1267
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001268" Test for the popup menu with the 'rightleft' option set
1269func Test_pum_rightleft()
Dominique Pelle56cddb32021-06-04 21:09:55 +02001270 CheckFeature rightleft
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001271 CheckScreendump
Dominique Pelle56cddb32021-06-04 21:09:55 +02001272
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001273 let lines =<< trim END
1274 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
1275 vim
1276 victory
1277 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001278 call writefile(lines, 'Xtest1', 'D')
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001279 let buf = RunVimInTerminal('--cmd "set rightleft" Xtest1', {})
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001280 call term_sendkeys(buf, "Go\<C-P>")
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001281 call VerifyScreenDump(buf, 'Test_pum_rightleft_01', {'rows': 8})
1282 call term_sendkeys(buf, "\<C-P>\<C-Y>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001283 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001284 redraw!
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001285 call WaitForAssert({-> assert_match('\s*miv', Screenline(5))})
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001286
1287 " Test for expanding tabs to spaces in the popup menu
1288 let lines =<< trim END
1289 one two
1290 one three
1291 four
1292 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001293 call writefile(lines, 'Xtest2', 'D')
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001294 call term_sendkeys(buf, "\<Esc>:e! Xtest2\<CR>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001295 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001296 call term_sendkeys(buf, "Goone\<C-X>\<C-L>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001297 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001298 redraw!
1299 call VerifyScreenDump(buf, 'Test_pum_rightleft_02', {'rows': 7})
1300 call term_sendkeys(buf, "\<C-Y>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001301 call TermWait(buf, 30)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001302 redraw!
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001303 call WaitForAssert({-> assert_match('\s*eerht eno', Screenline(4))})
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001304
1305 call StopVimInTerminal(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001306endfunc
1307
1308" Test for a popup menu with a scrollbar
1309func Test_pum_scrollbar()
1310 CheckScreendump
1311 let lines =<< trim END
1312 one
1313 two
1314 three
1315 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001316 call writefile(lines, 'Xtest1', 'D')
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001317 let buf = RunVimInTerminal('--cmd "set pumheight=2" Xtest1', {})
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001318 call TermWait(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001319 call term_sendkeys(buf, "Go\<C-P>\<C-P>\<C-P>")
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001320 call VerifyScreenDump(buf, 'Test_pum_scrollbar_01', {'rows': 7})
1321 call term_sendkeys(buf, "\<C-E>\<Esc>dd")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001322 call TermWait(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001323
Dominique Pelle56cddb32021-06-04 21:09:55 +02001324 if has('rightleft')
1325 call term_sendkeys(buf, ":set rightleft\<CR>")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001326 call TermWait(buf)
Dominique Pelle56cddb32021-06-04 21:09:55 +02001327 call term_sendkeys(buf, "Go\<C-P>\<C-P>\<C-P>")
Dominique Pelle56cddb32021-06-04 21:09:55 +02001328 call VerifyScreenDump(buf, 'Test_pum_scrollbar_02', {'rows': 7})
1329 endif
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001330
1331 call StopVimInTerminal(buf)
Yegappan Lakshmanan1e615662021-05-22 17:12:46 +02001332endfunc
1333
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +00001334" Test default highlight groups for popup menu
1335func Test_pum_highlights_default()
1336 CheckScreendump
1337 let lines =<< trim END
1338 func CompleteFunc( findstart, base )
1339 if a:findstart
1340 return 0
1341 endif
1342 return {
1343 \ 'words': [
1344 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'W', },
1345 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'W', },
1346 \ { 'word': 'aword3', 'menu': 'extra text 3', 'kind': 'W', },
1347 \]}
1348 endfunc
1349 set completeopt=menu
1350 set completefunc=CompleteFunc
1351 END
1352 call writefile(lines, 'Xscript', 'D')
1353 let buf = RunVimInTerminal('-S Xscript', {})
1354 call TermWait(buf)
1355 call term_sendkeys(buf, "iaw\<C-X>\<C-u>")
1356 call TermWait(buf, 50)
1357 call VerifyScreenDump(buf, 'Test_pum_highlights_01', {})
1358 call term_sendkeys(buf, "\<C-E>\<Esc>u")
1359 call TermWait(buf)
1360 call StopVimInTerminal(buf)
1361endfunc
1362
1363" Test custom highlight groups for popup menu
1364func Test_pum_highlights_custom()
1365 CheckScreendump
1366 let lines =<< trim END
1367 func CompleteFunc( findstart, base )
1368 if a:findstart
1369 return 0
1370 endif
1371 return {
1372 \ 'words': [
1373 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'W', },
1374 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'W', },
1375 \ { 'word': 'aword3', 'menu': 'extra text 3', 'kind': 'W', },
1376 \]}
1377 endfunc
1378 set completeopt=menu
1379 set completefunc=CompleteFunc
1380 hi PmenuKind ctermfg=1 ctermbg=225
1381 hi PmenuKindSel ctermfg=1 ctermbg=7
1382 hi PmenuExtra ctermfg=243 ctermbg=225
1383 hi PmenuExtraSel ctermfg=0 ctermbg=7
1384 END
1385 call writefile(lines, 'Xscript', 'D')
1386 let buf = RunVimInTerminal('-S Xscript', {})
1387 call TermWait(buf)
1388 call term_sendkeys(buf, "iaw\<C-X>\<C-u>")
1389 call TermWait(buf, 50)
1390 call VerifyScreenDump(buf, 'Test_pum_highlights_02', {})
1391 call term_sendkeys(buf, "\<C-E>\<Esc>u")
1392 call TermWait(buf)
1393 call StopVimInTerminal(buf)
1394endfunc
1395
glepnir40c1c332024-06-11 19:37:04 +02001396" Test match relate highlight group in pmenu
1397func Test_pum_highlights_match()
1398 CheckScreendump
1399 let lines =<< trim END
1400 func Omni_test(findstart, base)
1401 if a:findstart
1402 return col(".")
1403 endif
1404 return {
1405 \ 'words': [
zeertzjqafbe5352024-06-14 20:24:22 +02001406 \ { 'word': 'foo', 'kind': 'fookind' },
zeertzjq63901e82024-06-16 16:51:25 +02001407 \ { 'word': 'foofoo', 'kind': 'fookind' },
zeertzjqafbe5352024-06-14 20:24:22 +02001408 \ { 'word': 'foobar', 'kind': 'fookind' },
1409 \ { 'word': 'fooBaz', 'kind': 'fookind' },
1410 \ { 'word': 'foobala', 'kind': 'fookind' },
1411 \ { 'word': '你好' },
1412 \ { 'word': '你好吗' },
1413 \ { 'word': '你不好吗' },
1414 \ { 'word': '你可好吗' },
glepnir40c1c332024-06-11 19:37:04 +02001415 \]}
1416 endfunc
glepnirf1891382024-06-17 18:35:25 +02001417
1418 func Comp()
1419 let col = col('.')
1420 if getline('.') == 'f'
1421 let col -= 1
1422 endif
1423 call complete(col, [
1424 \ #{word: "foo", icase: 1},
1425 \ #{word: "Foobar", icase: 1},
1426 \ #{word: "fooBaz", icase: 1},
1427 \])
1428 return ''
1429 endfunc
1430
glepnir40c1c332024-06-11 19:37:04 +02001431 set omnifunc=Omni_test
1432 set completeopt=menu,noinsert,fuzzy
zeertzjq63901e82024-06-16 16:51:25 +02001433 hi PmenuMatchSel ctermfg=6 ctermbg=7
glepnir40c1c332024-06-11 19:37:04 +02001434 hi PmenuMatch ctermfg=4 ctermbg=225
1435 END
1436 call writefile(lines, 'Xscript', 'D')
1437 let buf = RunVimInTerminal('-S Xscript', {})
1438 call TermWait(buf)
1439 call term_sendkeys(buf, "i\<C-X>\<C-O>")
1440 call TermWait(buf, 50)
1441 call term_sendkeys(buf, "fo")
1442 call TermWait(buf, 50)
1443 call VerifyScreenDump(buf, 'Test_pum_highlights_03', {})
zeertzjq63901e82024-06-16 16:51:25 +02001444 call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
glepnir40c1c332024-06-11 19:37:04 +02001445 call TermWait(buf, 50)
1446 call term_sendkeys(buf, "你")
1447 call TermWait(buf, 50)
1448 call VerifyScreenDump(buf, 'Test_pum_highlights_04', {})
1449 call term_sendkeys(buf, "吗")
1450 call TermWait(buf, 50)
1451 call VerifyScreenDump(buf, 'Test_pum_highlights_05', {})
zeertzjq63901e82024-06-16 16:51:25 +02001452 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir40c1c332024-06-11 19:37:04 +02001453
1454 if has('rightleft')
zeertzjq63901e82024-06-16 16:51:25 +02001455 call term_sendkeys(buf, ":set rightleft\<CR>")
glepnir40c1c332024-06-11 19:37:04 +02001456 call TermWait(buf, 50)
zeertzjq63901e82024-06-16 16:51:25 +02001457 call term_sendkeys(buf, "S\<C-X>\<C-O>")
glepnir40c1c332024-06-11 19:37:04 +02001458 call TermWait(buf, 50)
1459 call term_sendkeys(buf, "fo")
1460 call TermWait(buf, 50)
1461 call VerifyScreenDump(buf, 'Test_pum_highlights_06', {})
zeertzjq63901e82024-06-16 16:51:25 +02001462 call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
1463 call TermWait(buf, 50)
1464 call term_sendkeys(buf, "你")
1465 call VerifyScreenDump(buf, 'Test_pum_highlights_06a', {})
1466 call term_sendkeys(buf, "吗")
1467 call VerifyScreenDump(buf, 'Test_pum_highlights_06b', {})
1468 call term_sendkeys(buf, "\<C-E>\<Esc>")
1469 call term_sendkeys(buf, ":set norightleft\<CR>")
glepnir40c1c332024-06-11 19:37:04 +02001470 call TermWait(buf)
1471 endif
1472
1473 call term_sendkeys(buf, ":set completeopt-=fuzzy\<CR>")
1474 call TermWait(buf)
zeertzjq63901e82024-06-16 16:51:25 +02001475 call term_sendkeys(buf, "S\<C-X>\<C-O>")
glepnir40c1c332024-06-11 19:37:04 +02001476 call TermWait(buf, 50)
1477 call term_sendkeys(buf, "fo")
1478 call TermWait(buf, 50)
1479 call VerifyScreenDump(buf, 'Test_pum_highlights_07', {})
zeertzjq63901e82024-06-16 16:51:25 +02001480 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir40c1c332024-06-11 19:37:04 +02001481
zeertzjq63901e82024-06-16 16:51:25 +02001482 if has('rightleft')
1483 call term_sendkeys(buf, ":set rightleft\<CR>")
1484 call TermWait(buf, 50)
1485 call term_sendkeys(buf, "S\<C-X>\<C-O>")
1486 call TermWait(buf, 50)
1487 call term_sendkeys(buf, "fo")
1488 call TermWait(buf, 50)
1489 call VerifyScreenDump(buf, 'Test_pum_highlights_08', {})
1490 call term_sendkeys(buf, "\<C-E>\<Esc>")
1491 call term_sendkeys(buf, ":set norightleft\<CR>")
1492 endif
1493
glepnirf1891382024-06-17 18:35:25 +02001494 call term_sendkeys(buf, "S\<C-R>=Comp()\<CR>f")
1495 call VerifyScreenDump(buf, 'Test_pum_highlights_09', {})
1496 call term_sendkeys(buf, "o\<BS>\<C-R>=Comp()\<CR>")
1497 call VerifyScreenDump(buf, 'Test_pum_highlights_09', {})
1498
glepnir65407ce2024-07-06 16:09:19 +02001499 " issue #15095 wrong select
1500 call term_sendkeys(buf, "\<ESC>:set completeopt=fuzzy,menu\<CR>")
1501 call TermWait(buf)
1502 call term_sendkeys(buf, "S hello helio hero h\<C-X>\<C-P>")
1503 call TermWait(buf, 50)
1504 call VerifyScreenDump(buf, 'Test_pum_highlights_10', {})
1505
1506 call term_sendkeys(buf, "\<ESC>S hello helio hero h\<C-X>\<C-P>\<C-P>")
1507 call TermWait(buf, 50)
1508 call VerifyScreenDump(buf, 'Test_pum_highlights_11', {})
1509
glepnir6b6280c2024-07-27 16:25:45 +02001510 " issue #15357
zeertzjq58d70522024-08-31 17:05:39 +02001511 call term_sendkeys(buf, "\<ESC>S/non_existing_folder\<C-X>\<C-F>")
glepnir6b6280c2024-07-27 16:25:45 +02001512 call TermWait(buf, 50)
1513 call VerifyScreenDump(buf, 'Test_pum_highlights_15', {})
glepnir9eff3ee2025-01-11 16:47:34 +01001514 call term_sendkeys(buf, "\<C-E>\<Esc>")
1515
zeertzjqfaf250c2025-01-12 09:32:27 +01001516 call term_sendkeys(buf, ":hi PmenuMatchSel ctermfg=14 ctermbg=NONE\<CR>")
glepnir9eff3ee2025-01-11 16:47:34 +01001517 call TermWait(buf, 50)
zeertzjqfaf250c2025-01-12 09:32:27 +01001518 call term_sendkeys(buf, ":hi PmenuMatch ctermfg=12 ctermbg=NONE\<CR>")
glepnir9eff3ee2025-01-11 16:47:34 +01001519 call term_sendkeys(buf, ":set cot=menu,noinsert,fuzzy\<CR>")
1520 call term_sendkeys(buf, "S\<C-X>\<C-O>")
1521 call TermWait(buf, 50)
1522 call term_sendkeys(buf, "fb")
1523 call VerifyScreenDump(buf, 'Test_pum_highlights_18', {})
glepnir6b6280c2024-07-27 16:25:45 +02001524
glepnirf1891382024-06-17 18:35:25 +02001525 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir40c1c332024-06-11 19:37:04 +02001526 call TermWait(buf)
glepnirf1891382024-06-17 18:35:25 +02001527
glepnir40c1c332024-06-11 19:37:04 +02001528 call StopVimInTerminal(buf)
1529endfunc
1530
zeertzjq3a0cc362025-01-13 07:27:43 +01001531func Test_pum_highlights_match_with_abbr()
1532 CheckScreendump
1533 let lines =<< trim END
1534 func Omni_test(findstart, base)
1535 if a:findstart
1536 return col(".")
1537 endif
1538 return {
1539 \ 'words': [
1540 \ { 'word': 'foobar', 'abbr': "foobar\t\t!" },
1541 \ { 'word': 'foobaz', 'abbr': "foobaz\t\t!" },
1542 \]}
1543 endfunc
1544
1545 set omnifunc=Omni_test
1546 set completeopt=menuone,noinsert
1547 hi PmenuMatchSel ctermfg=6 ctermbg=7
1548 hi PmenuMatch ctermfg=4 ctermbg=225
1549 END
1550 call writefile(lines, 'Xscript', 'D')
1551 let buf = RunVimInTerminal('-S Xscript', {})
1552 call TermWait(buf)
1553 call term_sendkeys(buf, "i\<C-X>\<C-O>")
1554 call TermWait(buf, 50)
1555 call term_sendkeys(buf, "foo")
1556 call VerifyScreenDump(buf, 'Test_pum_highlights_19', {})
1557
1558 call term_sendkeys(buf, "\<C-E>\<Esc>")
1559 call TermWait(buf)
1560
1561 call StopVimInTerminal(buf)
1562endfunc
1563
glepnir0fe17f82024-10-08 22:26:44 +02001564func Test_pum_user_abbr_hlgroup()
glepnir508e7852024-07-25 21:39:08 +02001565 CheckScreendump
1566 let lines =<< trim END
glepnirbc10be72024-11-02 16:45:01 +01001567 let s:var = 0
1568 func CompleteFunc(findstart, base)
glepnir508e7852024-07-25 21:39:08 +02001569 if a:findstart
1570 return 0
1571 endif
glepnirbc10be72024-11-02 16:45:01 +01001572 if s:var == 1
1573 return {
1574 \ 'words': [
1575 \ { 'word': 'aword1', 'abbr_hlgroup': 'StrikeFake' },
1576 \ { 'word': '你好', 'abbr_hlgroup': 'StrikeFake' },
1577 \]}
1578 endif
glepnir508e7852024-07-25 21:39:08 +02001579 return {
1580 \ 'words': [
glepnir0fe17f82024-10-08 22:26:44 +02001581 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'W', 'abbr_hlgroup': 'StrikeFake' },
glepnir508e7852024-07-25 21:39:08 +02001582 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'W', },
glepnir0fe17f82024-10-08 22:26:44 +02001583 \ { 'word': '你好', 'menu': 'extra text 3', 'kind': 'W', 'abbr_hlgroup': 'StrikeFake' },
glepnir508e7852024-07-25 21:39:08 +02001584 \]}
1585 endfunc
glepnirbc10be72024-11-02 16:45:01 +01001586 func ChangeVar()
1587 let s:var = 1
1588 endfunc
glepnir508e7852024-07-25 21:39:08 +02001589 set completeopt=menu
1590 set completefunc=CompleteFunc
zeertzjq41008522024-07-27 13:21:49 +02001591
1592 hi StrikeFake ctermfg=9
1593 func HlMatch()
1594 hi PmenuMatchSel ctermfg=6 ctermbg=7 cterm=underline
1595 hi PmenuMatch ctermfg=4 ctermbg=225 cterm=underline
1596 endfunc
glepnir508e7852024-07-25 21:39:08 +02001597 END
1598 call writefile(lines, 'Xscript', 'D')
1599 let buf = RunVimInTerminal('-S Xscript', {})
zeertzjq41008522024-07-27 13:21:49 +02001600
glepnir508e7852024-07-25 21:39:08 +02001601 call TermWait(buf)
zeertzjq41008522024-07-27 13:21:49 +02001602 call term_sendkeys(buf, "Saw\<C-X>\<C-U>")
glepnir508e7852024-07-25 21:39:08 +02001603 call VerifyScreenDump(buf, 'Test_pum_highlights_12', {})
zeertzjq41008522024-07-27 13:21:49 +02001604 call term_sendkeys(buf, "\<C-E>\<Esc>")
1605
glepnir508e7852024-07-25 21:39:08 +02001606 call TermWait(buf)
zeertzjq41008522024-07-27 13:21:49 +02001607 call term_sendkeys(buf, ":call HlMatch()\<CR>")
1608
1609 call TermWait(buf)
1610 call term_sendkeys(buf, "Saw\<C-X>\<C-U>")
1611 call VerifyScreenDump(buf, 'Test_pum_highlights_13', {})
1612 call term_sendkeys(buf, "\<C-N>")
1613 call VerifyScreenDump(buf, 'Test_pum_highlights_14', {})
1614 call term_sendkeys(buf, "\<C-E>\<Esc>")
1615
glepnirbc10be72024-11-02 16:45:01 +01001616 call TermWait(buf)
1617 call term_sendkeys(buf, ":call ChangeVar()\<CR>")
1618 call TermWait(buf)
1619 call term_sendkeys(buf, "S\<C-X>\<C-U>")
1620 call VerifyScreenDump(buf, 'Test_pum_highlights_17', {})
1621 call term_sendkeys(buf, "\<C-E>\<Esc>")
1622
glepnir508e7852024-07-25 21:39:08 +02001623 call StopVimInTerminal(buf)
1624endfunc
1625
glepnir38f99a12024-08-23 18:31:06 +02001626func Test_pum_user_kind_hlgroup()
1627 CheckScreendump
1628 let lines =<< trim END
glepnirbc10be72024-11-02 16:45:01 +01001629 func CompleteFunc(findstart, base)
glepnir38f99a12024-08-23 18:31:06 +02001630 if a:findstart
1631 return 0
1632 endif
1633 return {
1634 \ 'words': [
glepnir0fe17f82024-10-08 22:26:44 +02001635 \ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'variable', 'kind_hlgroup': 'KindVar', 'abbr_hlgroup': 'StrikeFake' },
glepnir38f99a12024-08-23 18:31:06 +02001636 \ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'function', 'kind_hlgroup': 'KindFunc' },
1637 \ { 'word': '你好', 'menu': 'extra text 3', 'kind': 'class', 'kind_hlgroup': 'KindClass' },
1638 \]}
1639 endfunc
1640 set completeopt=menu
1641 set completefunc=CompleteFunc
1642
1643 hi StrikeFake ctermfg=9
1644 hi KindVar ctermfg=yellow
1645 hi KindFunc ctermfg=blue
1646 hi KindClass ctermfg=green
1647 END
1648 call writefile(lines, 'Xscript', 'D')
1649 let buf = RunVimInTerminal('-S Xscript', {})
1650
1651 call TermWait(buf)
1652 call term_sendkeys(buf, "S\<C-X>\<C-U>")
1653 call VerifyScreenDump(buf, 'Test_pum_highlights_16', {})
1654 call term_sendkeys(buf, "\<C-E>\<Esc>")
1655
1656 call StopVimInTerminal(buf)
1657endfunc
1658
glepnir6a89c942024-10-01 20:32:12 +02001659func Test_pum_completeitemalign()
1660 CheckScreendump
1661 let lines =<< trim END
1662 func Omni_test(findstart, base)
1663 if a:findstart
1664 return col(".")
1665 endif
1666 return {
1667 \ 'words': [
1668 \ { 'word': 'foo', 'kind': 'S', 'menu': 'menu' },
1669 \ { 'word': 'bar', 'kind': 'T', 'menu': 'menu' },
1670 \ { 'word': '你好', 'kind': 'C', 'menu': '中文' },
1671 \]}
1672 endfunc
glepnira6d9e3c2024-10-03 11:01:19 +02001673
1674 func Omni_long(findstart, base)
1675 if a:findstart
1676 return col(".")
1677 endif
1678 return {
1679 \ 'words': [
1680 \ { 'word': 'loooong_foo', 'kind': 'S', 'menu': 'menu' },
1681 \ { 'word': 'loooong_bar', 'kind': 'T', 'menu': 'menu' },
1682 \]}
1683 endfunc
glepnir6a89c942024-10-01 20:32:12 +02001684 set omnifunc=Omni_test
1685 command! -nargs=0 T1 set cia=abbr,kind,menu
1686 command! -nargs=0 T2 set cia=abbr,menu,kind
1687 command! -nargs=0 T3 set cia=kind,abbr,menu
1688 command! -nargs=0 T4 set cia=kind,menu,abbr
1689 command! -nargs=0 T5 set cia=menu,abbr,kind
1690 command! -nargs=0 T6 set cia=menu,kind,abbr
1691 command! -nargs=0 T7 set cia&
1692 END
1693 call writefile(lines, 'Xscript', 'D')
1694 let buf = RunVimInTerminal('-S Xscript', {})
1695 call TermWait(buf)
1696
1697 " T1 is default
1698 call term_sendkeys(buf, ":T1\<CR>S\<C-X>\<C-O>")
1699 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_01', {})
1700 call term_sendkeys(buf, "\<C-E>\<Esc>")
1701
1702 " T2
1703 call term_sendkeys(buf, ":T2\<CR>S\<C-X>\<C-O>")
1704 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_02', {})
1705 call term_sendkeys(buf, "\<C-E>\<Esc>")
1706
1707 " T3
1708 call term_sendkeys(buf, ":T3\<CR>S\<C-X>\<C-O>")
1709 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_03', {})
1710 call term_sendkeys(buf, "\<C-E>\<Esc>")
1711
1712 " T4
1713 call term_sendkeys(buf, ":T4\<CR>S\<C-X>\<C-O>")
1714 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_04', {})
1715 call term_sendkeys(buf, "\<C-E>\<Esc>")
1716
1717 " T5
1718 call term_sendkeys(buf, ":T5\<CR>S\<C-X>\<C-O>")
1719 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_05', {})
1720 call term_sendkeys(buf, "\<C-E>\<Esc>")
1721
1722 " T6
1723 call term_sendkeys(buf, ":T6\<CR>S\<C-X>\<C-O>")
1724 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_06', {})
glepnira6d9e3c2024-10-03 11:01:19 +02001725 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir6a89c942024-10-01 20:32:12 +02001726
glepnira6d9e3c2024-10-03 11:01:19 +02001727 call term_sendkeys(buf, ":set columns=12 cmdheight=2 omnifunc=Omni_long\<CR>S\<C-X>\<C-O>")
1728 call VerifyScreenDump(buf, 'Test_pum_completeitemalign_07', {})
1729 call term_sendkeys(buf, "\<C-E>\<Esc>:T7\<CR>")
glepnir6a89c942024-10-01 20:32:12 +02001730 call StopVimInTerminal(buf)
1731endfunc
1732
glepnira49c0772024-11-30 10:56:30 +01001733func Test_pum_keep_select()
1734 CheckScreendump
1735 let lines =<< trim END
1736 set completeopt=menu,menuone,noinsert
1737 END
1738 call writefile(lines, 'Xscript', 'D')
1739 let buf = RunVimInTerminal('-S Xscript', {})
1740 call TermWait(buf)
1741
1742 call term_sendkeys(buf, "ggSFab\<CR>Five\<CR>find\<CR>film\<CR>\<C-X>\<C-P>")
1743 call TermWait(buf, 50)
1744 call VerifyScreenDump(buf, 'Test_pum_keep_select_01', {})
1745 call term_sendkeys(buf, "\<C-E>\<Esc>")
1746 call TermWait(buf, 50)
1747
1748 call term_sendkeys(buf, "S\<C-X>\<C-P>")
1749 call TermWait(buf, 50)
1750 call term_sendkeys(buf, "F")
1751 call VerifyScreenDump(buf, 'Test_pum_keep_select_02', {})
1752 call term_sendkeys(buf, "\<C-E>\<Esc>")
1753
1754 call TermWait(buf, 50)
1755 call StopVimInTerminal(buf)
1756endfunc
1757
zeertzjqd32bf0a2024-12-17 20:55:13 +01001758func Test_pum_matchins_highlight()
glepnir6a38aff2024-12-16 21:56:16 +01001759 CheckScreendump
1760 let lines =<< trim END
glepnir8d0bb6d2024-12-24 09:44:35 +01001761 let g:change = 0
glepnir6a38aff2024-12-16 21:56:16 +01001762 func Omni_test(findstart, base)
1763 if a:findstart
1764 return col(".")
1765 endif
glepnir8d0bb6d2024-12-24 09:44:35 +01001766 if g:change == 0
1767 return [#{word: "foo"}, #{word: "bar"}, #{word: "你好"}]
1768 endif
1769 return [#{word: "foo", info: "info"}, #{word: "bar"}, #{word: "你好"}]
glepnir6a38aff2024-12-16 21:56:16 +01001770 endfunc
1771 set omnifunc=Omni_test
1772 hi ComplMatchIns ctermfg=red
1773 END
1774 call writefile(lines, 'Xscript', 'D')
1775 let buf = RunVimInTerminal('-S Xscript', {})
1776
1777 call TermWait(buf)
zeertzjqf4ccada2024-12-17 20:50:19 +01001778 call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>")
glepnir6a38aff2024-12-16 21:56:16 +01001779 call VerifyScreenDump(buf, 'Test_pum_matchins_01', {})
1780 call term_sendkeys(buf, "\<C-E>\<Esc>")
1781
1782 call TermWait(buf)
zeertzjqf4ccada2024-12-17 20:50:19 +01001783 call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>\<C-N>")
glepnir6a38aff2024-12-16 21:56:16 +01001784 call VerifyScreenDump(buf, 'Test_pum_matchins_02', {})
1785 call term_sendkeys(buf, "\<C-E>\<Esc>")
1786
1787 call TermWait(buf)
zeertzjqf4ccada2024-12-17 20:50:19 +01001788 call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>\<C-N>\<C-N>")
glepnir6a38aff2024-12-16 21:56:16 +01001789 call VerifyScreenDump(buf, 'Test_pum_matchins_03', {})
1790 call term_sendkeys(buf, "\<C-E>\<Esc>")
1791
1792 " restore after accept
1793 call TermWait(buf)
zeertzjqf4ccada2024-12-17 20:50:19 +01001794 call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>\<C-Y>")
glepnir6a38aff2024-12-16 21:56:16 +01001795 call VerifyScreenDump(buf, 'Test_pum_matchins_04', {})
zeertzjqf25d8f92024-12-18 21:12:25 +01001796 call term_sendkeys(buf, "\<Esc>")
glepnir6a38aff2024-12-16 21:56:16 +01001797
1798 " restore after cancel completion
1799 call TermWait(buf)
zeertzjqf4ccada2024-12-17 20:50:19 +01001800 call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>\<Space>")
glepnir6a38aff2024-12-16 21:56:16 +01001801 call VerifyScreenDump(buf, 'Test_pum_matchins_05', {})
zeertzjqf25d8f92024-12-18 21:12:25 +01001802 call term_sendkeys(buf, "\<Esc>")
1803
1804 " text after the inserted text shouldn't be highlighted
1805 call TermWait(buf)
1806 call term_sendkeys(buf, "0ea \<C-X>\<C-O>")
1807 call VerifyScreenDump(buf, 'Test_pum_matchins_07', {})
1808 call term_sendkeys(buf, "\<C-P>")
1809 call VerifyScreenDump(buf, 'Test_pum_matchins_08', {})
1810 call term_sendkeys(buf, "\<C-P>")
1811 call VerifyScreenDump(buf, 'Test_pum_matchins_09', {})
1812 call term_sendkeys(buf, "\<C-Y>")
1813 call VerifyScreenDump(buf, 'Test_pum_matchins_10', {})
1814 call term_sendkeys(buf, "\<Esc>")
1815
glepnir8d0bb6d2024-12-24 09:44:35 +01001816 call term_sendkeys(buf, ":let g:change=1\<CR>S\<C-X>\<C-O>")
1817 call VerifyScreenDump(buf, 'Test_pum_matchins_11', {})
1818 call term_sendkeys(buf, "\<Esc>")
1819
zeertzjqf25d8f92024-12-18 21:12:25 +01001820 call StopVimInTerminal(buf)
1821endfunc
1822
1823func Test_pum_matchins_highlight_combine()
1824 CheckScreendump
1825 let lines =<< trim END
1826 func Omni_test(findstart, base)
1827 if a:findstart
1828 return col(".")
1829 endif
1830 return [#{word: "foo"}, #{word: "bar"}, #{word: "你好"}]
1831 endfunc
1832 set omnifunc=Omni_test
1833 hi Normal ctermbg=blue
1834 hi CursorLine cterm=underline ctermbg=green
1835 set cursorline
1836 call setline(1, 'aaa bbb')
1837 END
1838 call writefile(lines, 'Xscript', 'D')
1839 let buf = RunVimInTerminal('-S Xscript', {})
1840
1841 " when ComplMatchIns is not set, CursorLine applies normally
1842 call term_sendkeys(buf, "0ea \<C-X>\<C-O>")
1843 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_01', {})
1844 call term_sendkeys(buf, "\<C-E>")
1845 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_02', {})
1846 call term_sendkeys(buf, "\<BS>\<Esc>")
1847
1848 " when ComplMatchIns is set, it is applied over CursorLine
1849 call TermWait(buf)
1850 call term_sendkeys(buf, ":hi ComplMatchIns ctermbg=red ctermfg=yellow\<CR>")
1851 call TermWait(buf)
1852 call term_sendkeys(buf, "0ea \<C-X>\<C-O>")
1853 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_03', {})
1854 call term_sendkeys(buf, "\<C-P>")
1855 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_04', {})
1856 call term_sendkeys(buf, "\<C-P>")
1857 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_05', {})
1858 call term_sendkeys(buf, "\<C-E>")
1859 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_06', {})
1860 call term_sendkeys(buf, "\<Esc>")
glepnir6a38aff2024-12-16 21:56:16 +01001861
glepnire8908872025-01-08 18:30:45 +01001862 " Does not highlight the compl leader
1863 call TermWait(buf)
1864 call term_sendkeys(buf, ":set cot+=menuone,noselect\<CR>")
1865 call TermWait(buf)
1866 call term_sendkeys(buf, "S\<C-X>\<C-O>f\<C-N>")
1867 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_07', {})
1868 call term_sendkeys(buf, "\<C-E>\<Esc>")
1869
1870 call term_sendkeys(buf, ":set cot+=fuzzy\<CR>")
1871 call TermWait(buf)
1872 call term_sendkeys(buf, "S\<C-X>\<C-O>f\<C-N>")
1873 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_08', {})
1874 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnir9fddb8a2025-01-11 16:42:50 +01001875 call TermWait(buf)
1876
1877 call term_sendkeys(buf, ":set cot-=fuzzy\<CR>")
1878 call TermWait(buf)
1879 call term_sendkeys(buf, "Sf\<C-N>")
1880 call VerifyScreenDump(buf, 'Test_pum_matchins_combine_09', {})
1881 call term_sendkeys(buf, "\<C-E>\<Esc>")
glepnire8908872025-01-08 18:30:45 +01001882
glepnir6a38aff2024-12-16 21:56:16 +01001883 call StopVimInTerminal(buf)
1884endfunc
1885
glepnir8d0bb6d2024-12-24 09:44:35 +01001886" this used to crash
1887func Test_popup_completion_many_ctrlp()
1888 new
1889 let candidates=repeat(['a0'], 99)
1890 call setline(1, candidates)
1891 exe ":norm! VGg\<C-A>"
1892 norm! G
1893 call feedkeys("o" .. repeat("\<c-p>", 100), 'tx')
1894 bw!
1895endfunc
1896
glepnir76bdb822025-02-08 19:04:51 +01001897func Test_pum_complete_with_special_characters()
1898 CheckScreendump
1899
1900 let lines =<< trim END
1901 func Omni_test(findstart, base)
1902 if a:findstart
1903 return col(".")
1904 endif
1905 return [#{word: "func ()\n\t\nend", abbr: "function ()",}, #{word: "foobar"}, #{word: "你好\n\t\n我好"}]
1906 endfunc
1907 set omnifunc=Omni_test
1908 END
1909
1910 call writefile(lines, 'Xpreviewscript', 'D')
1911 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
1912 call term_sendkeys(buf, "S\<C-X>\<C-O>")
1913 call TermWait(buf, 50)
1914 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_01', {})
1915
1916 call term_sendkeys(buf, "\<C-N>")
1917 call TermWait(buf, 50)
1918 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_02', {})
1919 call term_sendkeys(buf, "\<C-E>\<Esc>")
1920
1921 call term_sendkeys(buf, "Shello hero\<ESC>hhhhha\<C-X>\<C-O>")
1922 call TermWait(buf, 50)
1923 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_03', {})
1924
1925 call term_sendkeys(buf, "\<C-N>")
1926 call TermWait(buf, 50)
1927 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_04', {})
1928
1929 call term_sendkeys(buf, "\<C-N>")
1930 call TermWait(buf, 50)
1931 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_05', {})
1932
1933 call term_sendkeys(buf, "\<C-N>")
1934 call TermWait(buf, 50)
1935 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_06', {})
1936 call term_sendkeys(buf, "\<C-E>\<Esc>")
1937
1938 call term_sendkeys(buf, ":hi ComplMatchIns ctermfg=red\<CR>")
1939 call TermWait(buf, 50)
1940 call term_sendkeys(buf, "S\<C-X>\<C-O>")
1941 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_07', {})
1942 call term_sendkeys(buf, "\<C-E>\<Esc>")
1943
1944 call term_sendkeys(buf, "Shello hero\<ESC>hhhhha\<C-X>\<C-O>")
1945 call TermWait(buf, 50)
1946 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_08', {})
1947 call term_sendkeys(buf, "\<C-E>\<Esc>")
1948
glepnir5090a1f2025-02-24 19:10:37 +01001949 call term_sendkeys(buf, ":setlocal autoindent tabstop=2 shiftwidth=2\<CR>")
1950 call term_sendkeys(buf, "Slocal a = \<C-X>\<C-O>")
1951 call TermWait(buf, 50)
1952 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_09', {})
1953
1954 call term_sendkeys(buf, "\<C-Y>")
1955 call TermWait(buf, 50)
1956 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_10', {})
1957
1958 call term_sendkeys(buf, "\<ESC>kAlocal b = \<C-X>\<C-O>")
1959 call TermWait(buf, 50)
1960 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_11', {})
1961
1962 call term_sendkeys(buf, "\<C-Y>")
1963 call TermWait(buf, 50)
1964 call VerifyScreenDump(buf, 'Test_pum_with_special_characters_12', {})
1965
glepnir76bdb822025-02-08 19:04:51 +01001966 call StopVimInTerminal(buf)
1967endfunc
1968
Bram Moolenaar47247282016-08-02 22:36:02 +02001969" vim: shiftwidth=2 sts=2 expandtab