blob: 613e9520d9dc2a45905689e31e63a3ef7c7e0874 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +02005
6" Test for insert expansion
7func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02008 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +01009 " The files in the current directory interferes with the files
10 " used by this test. So use a separate directory for the test.
11 call mkdir('Xdir')
12 cd Xdir
13
Bram Moolenaar15993ce2017-10-26 20:21:44 +020014 set ff=unix
15 call writefile(["test11\t36Gepeto\t/Tag/",
16 \ "asd\ttest11file\t36G",
17 \ "Makefile\tto\trun"], 'Xtestfile')
18 call writefile(['', 'start of testfile',
19 \ 'ru',
20 \ 'run1',
21 \ 'run2',
22 \ 'STARTTEST',
23 \ 'ENDTEST',
24 \ 'end of testfile'], 'Xtestdata')
25 set ff&
26
27 enew!
28 edit Xtestdata
29 new
30 call append(0, ['#include "Xtestfile"', ''])
31 call cursor(2, 1)
32
33 set cot=
34 set cpt=.,w
35 " add-expands (word from next line) from other window
36 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
37 call assert_equal('run1 run3', getline('.'))
38 " add-expands (current buffer first)
39 exe "normal o\<C-P>\<C-X>\<C-N>"
40 call assert_equal('run3 run3', getline('.'))
41 " Local expansion, ends in an empty line (unless it becomes a global
42 " expansion)
43 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
44 call assert_equal('', getline('.'))
45 " starts Local and switches to global add-expansion
46 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
47 call assert_equal('run1 run2', getline('.'))
48
49 set cpt=.,w,i
50 " i-add-expands and switches to local
51 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
52 call assert_equal("Makefile\tto\trun3", getline('.'))
53 " add-expands lines (it would end in an empty line if it didn't ignored
54 " itself)
55 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
56 call assert_equal("Makefile\tto\trun3", getline('.'))
57 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
58
59 set cpt=kXtestfile
60 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
61 " because TEST11.OUT may match first on DOS)
62 write Xtest11.one
63 write Xtest11.two
64 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
65 call assert_equal('Xtest11.two', getline('.'))
66
67 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
68 " CTRL-F again to verify this doesn't cause trouble.
69 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
70 call assert_equal('Xtest11.one', getline('.'))
71 normal ddk
72
73 set cpt=w
74 " checks make_cyclic in other window
75 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
76 call assert_equal('STARTTEST', getline('.'))
77
78 set cpt=u nohid
79 " checks unloaded buffer expansion
80 only
81 exe "normal oEN\<C-N>"
82 call assert_equal('ENDTEST', getline('.'))
83 " checks adding mode abortion
84 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
85 call assert_equal('unless', getline('.'))
86
87 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
88 " tag expansion, define add-expansion interrupted
89 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
90 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
91 " t-expansion
92 exe "normal oa\<C-N>\<Esc>"
93 call assert_equal('asd', getline('.'))
94
95 %bw!
96 call delete('Xtestfile')
97 call delete('Xtest11.one')
98 call delete('Xtest11.two')
99 call delete('Xtestdata')
100 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100101 cd ..
102 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200103endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100104
105func Test_omni_dash()
106 func Omni(findstart, base)
107 if a:findstart
108 return 5
109 else
110 echom a:base
111 return ['-help', '-v']
112 endif
113 endfunc
114 set omnifunc=Omni
115 new
116 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100117 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100118
119 bwipe!
120 delfunc Omni
121 set omnifunc=
122endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100123
Bram Moolenaarffa96842018-06-12 22:05:14 +0200124func Test_completefunc_args()
125 let s:args = []
126 func! CompleteFunc(findstart, base)
127 let s:args += [[a:findstart, empty(a:base)]]
128 endfunc
129 new
130
131 set completefunc=CompleteFunc
132 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200133 call assert_equal([1, 1], s:args[0])
134 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200135 set completefunc=
136
137 let s:args = []
138 set omnifunc=CompleteFunc
139 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200140 call assert_equal([1, 1], s:args[0])
141 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200142 set omnifunc=
143
144 bwipe!
145 unlet s:args
146 delfunc CompleteFunc
147endfunc
148
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100149func s:CompleteDone_CompleteFuncNone( findstart, base )
150 if a:findstart
151 return 0
152 endif
153
154 return v:none
155endfunc
156
Bram Moolenaar1e115362019-01-09 23:01:02 +0100157func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100158 if a:findstart
159 return 0
160 endif
161
162 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100163 \ 'words': [
164 \ {
165 \ 'word': 'aword',
166 \ 'abbr': 'wrd',
167 \ 'menu': 'extra text',
168 \ 'info': 'words are cool',
169 \ 'kind': 'W',
170 \ 'user_data': 'test'
171 \ }
172 \ ]
173 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100174endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100175
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100176func s:CompleteDone_CheckCompletedItemNone()
177 let s:called_completedone = 1
178endfunc
179
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100180func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100181 call assert_equal( 'aword', v:completed_item[ 'word' ] )
182 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
183 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
184 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
185 call assert_equal( 'W', v:completed_item[ 'kind' ] )
186 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
187
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100188 if a:pre
189 call assert_equal('function', complete_info().mode)
190 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100191
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100192 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100193endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100194
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100195func Test_CompleteDoneNone()
196 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200197 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100198
199 set completefunc=<SID>CompleteDone_CompleteFuncNone
200 execute "normal a\<C-X>\<C-U>\<C-Y>"
201 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200202 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100203
204 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200205 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100206
207 let s:called_completedone = 0
208 au! CompleteDone
209endfunc
210
211func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100212 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
213 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100214
215 set completefunc=<SID>CompleteDone_CompleteFuncDict
216 execute "normal a\<C-X>\<C-U>\<C-Y>"
217 set completefunc&
218
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100219 call assert_equal('test', v:completed_item[ 'user_data' ])
220 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100221
222 let s:called_completedone = 0
223 au! CompleteDone
224endfunc
225
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100226func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100227 if a:findstart
228 return 0
229 endif
230
231 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100232 \ 'words': [
233 \ {
234 \ 'word': 'aword',
235 \ 'abbr': 'wrd',
236 \ 'menu': 'extra text',
237 \ 'info': 'words are cool',
238 \ 'kind': 'W',
239 \ 'user_data': ['one', 'two'],
240 \ }
241 \ ]
242 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100243endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100244
Bram Moolenaar1e115362019-01-09 23:01:02 +0100245func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100246 call assert_equal( 'aword', v:completed_item[ 'word' ] )
247 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
248 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
249 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
250 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100251 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100252
253 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100254endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100255
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100256func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100257 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
258
259 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
260 execute "normal a\<C-X>\<C-U>\<C-Y>"
261 set completefunc&
262
Bram Moolenaar08928322020-01-04 14:32:48 +0100263 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100264 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100265
266 let s:called_completedone = 0
267 au! CompleteDone
268endfunc
269
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100270func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100271 if a:findstart
272 return 0
273 endif
274
275 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100276endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100277
Bram Moolenaar1e115362019-01-09 23:01:02 +0100278func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100279 call assert_equal( 'aword', v:completed_item[ 'word' ] )
280 call assert_equal( '', v:completed_item[ 'abbr' ] )
281 call assert_equal( '', v:completed_item[ 'menu' ] )
282 call assert_equal( '', v:completed_item[ 'info' ] )
283 call assert_equal( '', v:completed_item[ 'kind' ] )
284 call assert_equal( '', v:completed_item[ 'user_data' ] )
285
286 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100287endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100288
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100289func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100290 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
291
292 set completefunc=<SID>CompleteDone_CompleteFuncList
293 execute "normal a\<C-X>\<C-U>\<C-Y>"
294 set completefunc&
295
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100296 call assert_equal('', v:completed_item[ 'user_data' ])
297 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100298
299 let s:called_completedone = 0
300 au! CompleteDone
301endfunc
302
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200303func Test_CompleteDone_undo()
304 au CompleteDone * call append(0, "prepend1")
305 new
306 call setline(1, ["line1", "line2"])
307 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
308 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
309 \ getline(1, '$'))
310 undo
311 call assert_equal(["line1", "line2"], getline(1, '$'))
312 bwipe!
313 au! CompleteDone
314endfunc
315
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200316func CompleteTest(findstart, query)
317 if a:findstart
318 return col('.')
319 endif
320 return ['matched']
321endfunc
322
323func Test_completefunc_info()
324 new
325 set completeopt=menuone
326 set completefunc=CompleteTest
327 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100328 call assert_equal("matched{'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}", getline(1))
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200329 bwipe!
330 set completeopt&
331 set completefunc&
332endfunc
333
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100334" Check that when using feedkeys() typeahead does not interrupt searching for
335" completions.
336func Test_compl_feedkeys()
337 new
338 set completeopt=menuone,noselect
339 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
340 call assert_equal("jump jump", getline(1))
341 bwipe!
342 set completeopt&
343endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200344
345func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100346 CheckFeature cmdwin
347
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200348 set wildmenu wildchar=<Tab>
349 com! -nargs=1 -complete=command GetInput let input = <q-args>
350 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100351 let w:test_winvar = 'winvar'
352 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200353
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100354 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200355 let input = ''
356 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
357 call assert_equal('TestCommand', input)
358
359 let input = ''
360 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
361 call assert_equal('T', input)
362
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100363
364 com! -nargs=1 -complete=var GetInput let input = <q-args>
365 " Window-local variables
366 let input = ''
367 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
368 call assert_equal('w:test_winvar', input)
369
370 let input = ''
371 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
372 call assert_equal('w:test_', input)
373
374 " Buffer-local variables
375 let input = ''
376 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
377 call assert_equal('b:test_bufvar', input)
378
379 let input = ''
380 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
381 call assert_equal('b:test_', input)
382
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200383 delcom TestCommand
384 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100385 unlet w:test_winvar
386 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200387 set wildmenu& wildchar&
388endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200389
390" Test for insert path completion with completeslash option
391func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200392 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200393
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200394 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200395 let orig_shellslash = &shellslash
396 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200397 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200398
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200399 set noshellslash
400
401 set completeslash=
402 exe "normal oXd\<C-X>\<C-F>"
403 call assert_equal('Xdir\', getline('.'))
404
405 set completeslash=backslash
406 exe "normal oXd\<C-X>\<C-F>"
407 call assert_equal('Xdir\', getline('.'))
408
409 set completeslash=slash
410 exe "normal oXd\<C-X>\<C-F>"
411 call assert_equal('Xdir/', getline('.'))
412
413 set shellslash
414
415 set completeslash=
416 exe "normal oXd\<C-X>\<C-F>"
417 call assert_equal('Xdir/', getline('.'))
418
419 set completeslash=backslash
420 exe "normal oXd\<C-X>\<C-F>"
421 call assert_equal('Xdir\', getline('.'))
422
423 set completeslash=slash
424 exe "normal oXd\<C-X>\<C-F>"
425 call assert_equal('Xdir/', getline('.'))
426 %bw!
427 call delete('Xdir', 'rf')
428
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200429 set noshellslash
430 set completeslash=slash
431 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
432
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200433 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200434 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200435endfunc
436
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200437func Test_pum_stopped_by_timer()
438 CheckScreendump
439
440 let lines =<< trim END
441 call setline(1, ['hello', 'hullo', 'heeee', ''])
442 func StartCompl()
443 call timer_start(100, { -> execute('stopinsert') })
444 call feedkeys("Gah\<C-N>")
445 endfunc
446 END
447
448 call writefile(lines, 'Xpumscript')
449 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
450 call term_sendkeys(buf, ":call StartCompl()\<CR>")
451 call TermWait(buf, 200)
452 call term_sendkeys(buf, "k")
453 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
454
455 call StopVimInTerminal(buf)
456 call delete('Xpumscript')
457endfunc
458
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100459func Test_pum_with_folds_two_tabs()
460 CheckScreendump
461
462 let lines =<< trim END
463 set fdm=marker
464 call setline(1, ['" x {{{1', '" a some text'])
465 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
466 norm! zm
467 tab sp
468 call feedkeys('2Gzv', 'xt')
469 call feedkeys("0fa", 'xt')
470 END
471
472 call writefile(lines, 'Xpumscript')
473 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200474 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100475 call term_sendkeys(buf, "a\<C-N>")
476 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
477
478 call term_sendkeys(buf, "\<Esc>")
479 call StopVimInTerminal(buf)
480 call delete('Xpumscript')
481endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100482
483func Test_pum_with_preview_win()
484 CheckScreendump
485
486 let lines =<< trim END
487 funct Omni_test(findstart, base)
488 if a:findstart
489 return col(".") - 1
490 endif
491 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
492 endfunc
493 set omnifunc=Omni_test
494 set completeopt+=longest
495 END
496
497 call writefile(lines, 'Xpreviewscript')
498 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200499 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100500 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200501 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100502 call term_sendkeys(buf, "\<C-N>")
503 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
504
505 call term_sendkeys(buf, "\<Esc>")
506 call StopVimInTerminal(buf)
507 call delete('Xpreviewscript')
508endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100509
510" Test for inserting the tag search pattern in insert mode
511func Test_ins_compl_tag_sft()
512 call writefile([
513 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
514 \ "first\tXfoo\t/^int first() {}$/",
515 \ "second\tXfoo\t/^int second() {}$/",
516 \ "third\tXfoo\t/^int third() {}$/"],
517 \ 'Xtags')
518 set tags=Xtags
519 let code =<< trim [CODE]
520 int first() {}
521 int second() {}
522 int third() {}
523 [CODE]
524 call writefile(code, 'Xfoo')
525
526 enew
527 set showfulltag
528 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
529 call assert_equal('int second() {}', getline(1))
530 set noshowfulltag
531
532 call delete('Xtags')
533 call delete('Xfoo')
534 set tags&
535 %bwipe!
536endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200537
538" Test for 'completefunc' deleting text
539func Test_completefunc_error()
540 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200541 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200542 func CompleteFunc(findstart, base)
543 if a:findstart == 1
544 normal dd
545 return col('.') - 1
546 endif
547 return ['a', 'b']
548 endfunc
549 set completefunc=CompleteFunc
550 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100551 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200552
553 " delete text when called for the second time
554 func CompleteFunc2(findstart, base)
555 if a:findstart == 1
556 return col('.') - 1
557 endif
558 normal dd
559 return ['a', 'b']
560 endfunc
561 set completefunc=CompleteFunc2
562 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200563 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200564
Bram Moolenaar97202d92021-01-28 18:34:35 +0100565 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100566 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100567 if a:findstart == 1
568 return col('.') - 1
569 endif
570 wincmd p
571 return ['a', 'b']
572 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100573 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100574 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100575 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100576 close!
577
578 set completefunc&
579 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100580 delfunc CompleteFunc2
581 delfunc CompleteFunc3
582 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100583endfunc
584
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200585" Test for returning non-string values from 'completefunc'
586func Test_completefunc_invalid_data()
587 new
588 func! CompleteFunc(findstart, base)
589 if a:findstart == 1
590 return col('.') - 1
591 endif
592 return [{}, '', 'moon']
593 endfunc
594 set completefunc=CompleteFunc
595 exe "normal i\<C-X>\<C-U>"
596 call assert_equal('moon', getline(1))
597 set completefunc&
598 close!
599endfunc
600
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200601" Test for errors in using complete() function
602func Test_complete_func_error()
603 call assert_fails('call complete(1, ["a"])', 'E785:')
604 func ListColors()
605 call complete(col('.'), "blue")
606 endfunc
607 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
608 func ListMonths()
609 call complete(col('.'), test_null_list())
610 endfunc
611 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
612 delfunc ListColors
613 delfunc ListMonths
614 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200615 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200616endfunc
617
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200618" Test for completing words following a completed word in a line
619func Test_complete_wrapscan()
620 " complete words from another buffer
621 new
622 call setline(1, ['one two', 'three four'])
623 new
624 setlocal complete=w
625 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
626 call assert_equal('two three four', getline(1))
627 close!
628 " complete words from the current buffer
629 setlocal complete=.
630 %d
631 call setline(1, ['one two', ''])
632 call cursor(2, 1)
633 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
634 call assert_equal('one two one two', getline(2))
635 close!
636endfunc
637
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200638" Test for completing special characters
639func Test_complete_special_chars()
640 new
641 call setline(1, 'int .*[-\^$ func float')
642 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
643 call assert_equal('int .*[-\^$ func float', getline(2))
644 close!
645endfunc
646
647" Test for completion when text is wrapped across lines.
648func Test_complete_across_line()
649 new
650 call setline(1, ['red green blue', 'one two three'])
651 setlocal textwidth=20
652 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
653 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
654 close!
655endfunc
656
657" Test for using CTRL-L to add one character when completing matching
658func Test_complete_add_onechar()
659 new
660 call setline(1, ['wool', 'woodwork'])
661 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
662 call assert_equal('woof', getline(3))
663
664 " use 'ignorecase' and backspace to erase characters from the prefix string
665 " and then add letters using CTRL-L
666 %d
667 set ignorecase backspace=2
668 setlocal complete=.
669 call setline(1, ['workhorse', 'workload'])
670 normal Go
671 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
672 call assert_equal('workh', getline(3))
673 set ignorecase& backspace&
674 close!
675endfunc
676
677" Test insert completion with 'cindent' (adjust the indent)
678func Test_complete_with_cindent()
679 new
680 setlocal cindent
681 call setline(1, ['if (i == 1)', " j = 2;"])
682 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
683 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
684
685 %d
686 call setline(1, ['when while', '{', ''])
687 setlocal cinkeys+==while
688 exe "normal Giwh\<C-P> "
689 call assert_equal("\twhile ", getline('$'))
690 close!
691endfunc
692
693" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
694func Test_complete_cmdline()
695 new
696 exe "normal icaddb\<C-X>\<C-V>"
697 call assert_equal('caddbuffer', getline(1))
698 exe "normal ocall getqf\<C-X>\<C-V>"
699 call assert_equal('call getqflist(', getline(2))
700 exe "normal oabcxyz(\<C-X>\<C-V>"
701 call assert_equal('abcxyz(', getline(3))
702 close!
703endfunc
704
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200705func Test_issue_7021()
706 CheckMSWindows
707
708 let orig_shellslash = &shellslash
709 set noshellslash
710
711 set completeslash=slash
712 call assert_false(expand('~') =~ '/')
713
714 let &shellslash = orig_shellslash
715 set completeslash=
716endfunc
717
Bram Moolenaarcc233582020-12-12 13:32:07 +0100718" Test to ensure 'Scanning...' messages are not recorded in messages history
719func Test_z1_complete_no_history()
720 new
721 messages clear
722 let currmess = execute('messages')
723 setlocal dictionary=README.txt
724 exe "normal owh\<C-X>\<C-K>"
725 exe "normal owh\<C-N>"
726 call assert_equal(currmess, execute('messages'))
727 close!
728endfunc
729
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200730" vim: shiftwidth=2 sts=2 expandtab