blob: b0914d906792fed34bdea909bb05122ff6b37b6d [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 Moolenaarff34bee2021-07-25 20:27:06 +0200124func Test_omni_autoload()
125 let save_rtp = &rtp
126 set rtp=Xruntime/some
127 let dir = 'Xruntime/some/autoload'
128 call mkdir(dir, 'p')
129
130 let lines =<< trim END
131 vim9script
132 def omni#func(findstart: bool, base: string): any
133 if findstart
134 return 1
135 else
136 return ['match']
137 endif
138 enddef
139 {
140 eval 1 + 2
141 }
142 END
143 call writefile(lines, dir .. '/omni.vim')
144
145 new
146 setlocal omnifunc=omni#func
147 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
148
149 bwipe!
150 call delete('Xruntime', 'rf')
151 set omnifunc=
152 let &rtp = save_rtp
153endfunc
154
Bram Moolenaarffa96842018-06-12 22:05:14 +0200155func Test_completefunc_args()
156 let s:args = []
157 func! CompleteFunc(findstart, base)
158 let s:args += [[a:findstart, empty(a:base)]]
159 endfunc
160 new
161
162 set completefunc=CompleteFunc
163 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200164 call assert_equal([1, 1], s:args[0])
165 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200166 set completefunc=
167
168 let s:args = []
169 set omnifunc=CompleteFunc
170 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200171 call assert_equal([1, 1], s:args[0])
172 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200173 set omnifunc=
174
175 bwipe!
176 unlet s:args
177 delfunc CompleteFunc
178endfunc
179
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100180func s:CompleteDone_CompleteFuncNone( findstart, base )
181 if a:findstart
182 return 0
183 endif
184
185 return v:none
186endfunc
187
Bram Moolenaar1e115362019-01-09 23:01:02 +0100188func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100189 if a:findstart
190 return 0
191 endif
192
193 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100194 \ 'words': [
195 \ {
196 \ 'word': 'aword',
197 \ 'abbr': 'wrd',
198 \ 'menu': 'extra text',
199 \ 'info': 'words are cool',
200 \ 'kind': 'W',
201 \ 'user_data': 'test'
202 \ }
203 \ ]
204 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100205endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100206
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100207func s:CompleteDone_CheckCompletedItemNone()
208 let s:called_completedone = 1
209endfunc
210
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100211func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100212 call assert_equal( 'aword', v:completed_item[ 'word' ] )
213 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
214 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
215 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
216 call assert_equal( 'W', v:completed_item[ 'kind' ] )
217 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
218
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100219 if a:pre
220 call assert_equal('function', complete_info().mode)
221 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100222
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100223 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100224endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100225
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100226func Test_CompleteDoneNone()
227 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200228 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100229
230 set completefunc=<SID>CompleteDone_CompleteFuncNone
231 execute "normal a\<C-X>\<C-U>\<C-Y>"
232 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200233 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100234
235 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200236 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100237
238 let s:called_completedone = 0
239 au! CompleteDone
240endfunc
241
242func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100243 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
244 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100245
246 set completefunc=<SID>CompleteDone_CompleteFuncDict
247 execute "normal a\<C-X>\<C-U>\<C-Y>"
248 set completefunc&
249
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100250 call assert_equal('test', v:completed_item[ 'user_data' ])
251 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100252
253 let s:called_completedone = 0
254 au! CompleteDone
255endfunc
256
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100257func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100258 if a:findstart
259 return 0
260 endif
261
262 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100263 \ 'words': [
264 \ {
265 \ 'word': 'aword',
266 \ 'abbr': 'wrd',
267 \ 'menu': 'extra text',
268 \ 'info': 'words are cool',
269 \ 'kind': 'W',
270 \ 'user_data': ['one', 'two'],
271 \ }
272 \ ]
273 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100274endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100275
Bram Moolenaar1e115362019-01-09 23:01:02 +0100276func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100277 call assert_equal( 'aword', v:completed_item[ 'word' ] )
278 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
279 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
280 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
281 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100282 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100283
284 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100285endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100286
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100287func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100288 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
289
290 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
291 execute "normal a\<C-X>\<C-U>\<C-Y>"
292 set completefunc&
293
Bram Moolenaar08928322020-01-04 14:32:48 +0100294 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100295 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100296
297 let s:called_completedone = 0
298 au! CompleteDone
299endfunc
300
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100301func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100302 if a:findstart
303 return 0
304 endif
305
306 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100307endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100308
Bram Moolenaar1e115362019-01-09 23:01:02 +0100309func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100310 call assert_equal( 'aword', v:completed_item[ 'word' ] )
311 call assert_equal( '', v:completed_item[ 'abbr' ] )
312 call assert_equal( '', v:completed_item[ 'menu' ] )
313 call assert_equal( '', v:completed_item[ 'info' ] )
314 call assert_equal( '', v:completed_item[ 'kind' ] )
315 call assert_equal( '', v:completed_item[ 'user_data' ] )
316
317 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100318endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100319
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100320func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100321 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
322
323 set completefunc=<SID>CompleteDone_CompleteFuncList
324 execute "normal a\<C-X>\<C-U>\<C-Y>"
325 set completefunc&
326
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100327 call assert_equal('', v:completed_item[ 'user_data' ])
328 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329
330 let s:called_completedone = 0
331 au! CompleteDone
332endfunc
333
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200334func Test_CompleteDone_undo()
335 au CompleteDone * call append(0, "prepend1")
336 new
337 call setline(1, ["line1", "line2"])
338 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
339 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
340 \ getline(1, '$'))
341 undo
342 call assert_equal(["line1", "line2"], getline(1, '$'))
343 bwipe!
344 au! CompleteDone
345endfunc
346
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200347func CompleteTest(findstart, query)
348 if a:findstart
349 return col('.')
350 endif
351 return ['matched']
352endfunc
353
354func Test_completefunc_info()
355 new
356 set completeopt=menuone
357 set completefunc=CompleteTest
358 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100359 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 +0200360 bwipe!
361 set completeopt&
362 set completefunc&
363endfunc
364
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100365" Check that when using feedkeys() typeahead does not interrupt searching for
366" completions.
367func Test_compl_feedkeys()
368 new
369 set completeopt=menuone,noselect
370 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
371 call assert_equal("jump jump", getline(1))
372 bwipe!
373 set completeopt&
374endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200375
376func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100377 CheckFeature cmdwin
378
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200379 set wildmenu wildchar=<Tab>
380 com! -nargs=1 -complete=command GetInput let input = <q-args>
381 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100382 let w:test_winvar = 'winvar'
383 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200384
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100385 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200386 let input = ''
387 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
388 call assert_equal('TestCommand', input)
389
390 let input = ''
391 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
392 call assert_equal('T', input)
393
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100394
395 com! -nargs=1 -complete=var GetInput let input = <q-args>
396 " Window-local variables
397 let input = ''
398 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
399 call assert_equal('w:test_winvar', input)
400
401 let input = ''
402 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
403 call assert_equal('w:test_', input)
404
405 " Buffer-local variables
406 let input = ''
407 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
408 call assert_equal('b:test_bufvar', input)
409
410 let input = ''
411 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
412 call assert_equal('b:test_', input)
413
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200414 delcom TestCommand
415 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100416 unlet w:test_winvar
417 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200418 set wildmenu& wildchar&
419endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200420
421" Test for insert path completion with completeslash option
422func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200423 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200424
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200425 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200426 let orig_shellslash = &shellslash
427 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200428 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200429
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200430 set noshellslash
431
432 set completeslash=
433 exe "normal oXd\<C-X>\<C-F>"
434 call assert_equal('Xdir\', getline('.'))
435
436 set completeslash=backslash
437 exe "normal oXd\<C-X>\<C-F>"
438 call assert_equal('Xdir\', getline('.'))
439
440 set completeslash=slash
441 exe "normal oXd\<C-X>\<C-F>"
442 call assert_equal('Xdir/', getline('.'))
443
444 set shellslash
445
446 set completeslash=
447 exe "normal oXd\<C-X>\<C-F>"
448 call assert_equal('Xdir/', getline('.'))
449
450 set completeslash=backslash
451 exe "normal oXd\<C-X>\<C-F>"
452 call assert_equal('Xdir\', getline('.'))
453
454 set completeslash=slash
455 exe "normal oXd\<C-X>\<C-F>"
456 call assert_equal('Xdir/', getline('.'))
457 %bw!
458 call delete('Xdir', 'rf')
459
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200460 set noshellslash
461 set completeslash=slash
462 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
463
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200464 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200465 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200466endfunc
467
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200468func Test_pum_stopped_by_timer()
469 CheckScreendump
470
471 let lines =<< trim END
472 call setline(1, ['hello', 'hullo', 'heeee', ''])
473 func StartCompl()
474 call timer_start(100, { -> execute('stopinsert') })
475 call feedkeys("Gah\<C-N>")
476 endfunc
477 END
478
479 call writefile(lines, 'Xpumscript')
480 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
481 call term_sendkeys(buf, ":call StartCompl()\<CR>")
482 call TermWait(buf, 200)
483 call term_sendkeys(buf, "k")
484 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
485
486 call StopVimInTerminal(buf)
487 call delete('Xpumscript')
488endfunc
489
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100490func Test_pum_with_folds_two_tabs()
491 CheckScreendump
492
493 let lines =<< trim END
494 set fdm=marker
495 call setline(1, ['" x {{{1', '" a some text'])
496 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
497 norm! zm
498 tab sp
499 call feedkeys('2Gzv', 'xt')
500 call feedkeys("0fa", 'xt')
501 END
502
503 call writefile(lines, 'Xpumscript')
504 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200505 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100506 call term_sendkeys(buf, "a\<C-N>")
507 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
508
509 call term_sendkeys(buf, "\<Esc>")
510 call StopVimInTerminal(buf)
511 call delete('Xpumscript')
512endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100513
514func Test_pum_with_preview_win()
515 CheckScreendump
516
517 let lines =<< trim END
518 funct Omni_test(findstart, base)
519 if a:findstart
520 return col(".") - 1
521 endif
522 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
523 endfunc
524 set omnifunc=Omni_test
525 set completeopt+=longest
526 END
527
528 call writefile(lines, 'Xpreviewscript')
529 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200530 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100531 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200532 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100533 call term_sendkeys(buf, "\<C-N>")
534 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
535
536 call term_sendkeys(buf, "\<Esc>")
537 call StopVimInTerminal(buf)
538 call delete('Xpreviewscript')
539endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100540
541" Test for inserting the tag search pattern in insert mode
542func Test_ins_compl_tag_sft()
543 call writefile([
544 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
545 \ "first\tXfoo\t/^int first() {}$/",
546 \ "second\tXfoo\t/^int second() {}$/",
547 \ "third\tXfoo\t/^int third() {}$/"],
548 \ 'Xtags')
549 set tags=Xtags
550 let code =<< trim [CODE]
551 int first() {}
552 int second() {}
553 int third() {}
554 [CODE]
555 call writefile(code, 'Xfoo')
556
557 enew
558 set showfulltag
559 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
560 call assert_equal('int second() {}', getline(1))
561 set noshowfulltag
562
563 call delete('Xtags')
564 call delete('Xfoo')
565 set tags&
566 %bwipe!
567endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200568
569" Test for 'completefunc' deleting text
570func Test_completefunc_error()
571 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200572 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200573 func CompleteFunc(findstart, base)
574 if a:findstart == 1
575 normal dd
576 return col('.') - 1
577 endif
578 return ['a', 'b']
579 endfunc
580 set completefunc=CompleteFunc
581 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100582 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200583
584 " delete text when called for the second time
585 func CompleteFunc2(findstart, base)
586 if a:findstart == 1
587 return col('.') - 1
588 endif
589 normal dd
590 return ['a', 'b']
591 endfunc
592 set completefunc=CompleteFunc2
593 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200594 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200595
Bram Moolenaar97202d92021-01-28 18:34:35 +0100596 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100597 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100598 if a:findstart == 1
599 return col('.') - 1
600 endif
601 wincmd p
602 return ['a', 'b']
603 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100604 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100605 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100606 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100607 close!
608
609 set completefunc&
610 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100611 delfunc CompleteFunc2
612 delfunc CompleteFunc3
613 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100614endfunc
615
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200616" Test for returning non-string values from 'completefunc'
617func Test_completefunc_invalid_data()
618 new
619 func! CompleteFunc(findstart, base)
620 if a:findstart == 1
621 return col('.') - 1
622 endif
623 return [{}, '', 'moon']
624 endfunc
625 set completefunc=CompleteFunc
626 exe "normal i\<C-X>\<C-U>"
627 call assert_equal('moon', getline(1))
628 set completefunc&
629 close!
630endfunc
631
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200632" Test for errors in using complete() function
633func Test_complete_func_error()
634 call assert_fails('call complete(1, ["a"])', 'E785:')
635 func ListColors()
636 call complete(col('.'), "blue")
637 endfunc
638 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
639 func ListMonths()
640 call complete(col('.'), test_null_list())
641 endfunc
642 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
643 delfunc ListColors
644 delfunc ListMonths
645 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200646 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200647endfunc
648
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200649" Test for completing words following a completed word in a line
650func Test_complete_wrapscan()
651 " complete words from another buffer
652 new
653 call setline(1, ['one two', 'three four'])
654 new
655 setlocal complete=w
656 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
657 call assert_equal('two three four', getline(1))
658 close!
659 " complete words from the current buffer
660 setlocal complete=.
661 %d
662 call setline(1, ['one two', ''])
663 call cursor(2, 1)
664 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
665 call assert_equal('one two one two', getline(2))
666 close!
667endfunc
668
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200669" Test for completing special characters
670func Test_complete_special_chars()
671 new
672 call setline(1, 'int .*[-\^$ func float')
673 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
674 call assert_equal('int .*[-\^$ func float', getline(2))
675 close!
676endfunc
677
678" Test for completion when text is wrapped across lines.
679func Test_complete_across_line()
680 new
681 call setline(1, ['red green blue', 'one two three'])
682 setlocal textwidth=20
683 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
684 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
685 close!
686endfunc
687
688" Test for using CTRL-L to add one character when completing matching
689func Test_complete_add_onechar()
690 new
691 call setline(1, ['wool', 'woodwork'])
692 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
693 call assert_equal('woof', getline(3))
694
695 " use 'ignorecase' and backspace to erase characters from the prefix string
696 " and then add letters using CTRL-L
697 %d
698 set ignorecase backspace=2
699 setlocal complete=.
700 call setline(1, ['workhorse', 'workload'])
701 normal Go
702 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
703 call assert_equal('workh', getline(3))
704 set ignorecase& backspace&
705 close!
706endfunc
707
708" Test insert completion with 'cindent' (adjust the indent)
709func Test_complete_with_cindent()
710 new
711 setlocal cindent
712 call setline(1, ['if (i == 1)', " j = 2;"])
713 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
714 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
715
716 %d
717 call setline(1, ['when while', '{', ''])
718 setlocal cinkeys+==while
719 exe "normal Giwh\<C-P> "
720 call assert_equal("\twhile ", getline('$'))
721 close!
722endfunc
723
724" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
725func Test_complete_cmdline()
726 new
727 exe "normal icaddb\<C-X>\<C-V>"
728 call assert_equal('caddbuffer', getline(1))
729 exe "normal ocall getqf\<C-X>\<C-V>"
730 call assert_equal('call getqflist(', getline(2))
731 exe "normal oabcxyz(\<C-X>\<C-V>"
732 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200733 com! -buffer TestCommand1 echo 'TestCommand1'
734 com! -buffer TestCommand2 echo 'TestCommand2'
735 write TestCommand1Test
736 write TestCommand2Test
737 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
738 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
739 call assert_equal('TestCommand2Test', getline(4))
740 call delete('TestCommand1Test')
741 call delete('TestCommand2Test')
742 delcom TestCommand1
743 delcom TestCommand2
744 close!
745endfunc
746
747" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
748func Test_complete_stop()
749 new
750 func Save_mode1()
751 let g:mode1 = mode(1)
752 return ''
753 endfunc
754 func Save_mode2()
755 let g:mode2 = mode(1)
756 return ''
757 endfunc
758 inoremap <F1> <C-R>=Save_mode1()<CR>
759 inoremap <F2> <C-R>=Save_mode2()<CR>
760 call setline(1, ['aaa bbb ccc '])
761 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
762 call assert_equal('ic', g:mode1)
763 call assert_equal('i', g:mode2)
764 call assert_equal('aaa bbb ccc ', getline(1))
765 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
766 call assert_equal('ic', g:mode1)
767 call assert_equal('i', g:mode2)
768 call assert_equal('aaa bbb ccc aaa', getline(1))
769 set completeopt+=noselect
770 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
771 call assert_equal('ic', g:mode1)
772 call assert_equal('i', g:mode2)
773 call assert_equal('aaa bbb ccc aaa bb', getline(1))
774 set completeopt&
775 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
776 call assert_equal('ic', g:mode1)
777 call assert_equal('i', g:mode2)
778 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
779 com! -buffer TestCommand1 echo 'TestCommand1'
780 com! -buffer TestCommand2 echo 'TestCommand2'
781 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
782 call assert_equal('ic', g:mode1)
783 call assert_equal('i', g:mode2)
784 call assert_equal('TestCommand2', getline(2))
785 delcom TestCommand1
786 delcom TestCommand2
787 unlet g:mode1
788 unlet g:mode2
789 iunmap <F1>
790 iunmap <F2>
791 delfunc Save_mode1
792 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200793 close!
794endfunc
795
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200796func Test_issue_7021()
797 CheckMSWindows
798
799 let orig_shellslash = &shellslash
800 set noshellslash
801
802 set completeslash=slash
803 call assert_false(expand('~') =~ '/')
804
805 let &shellslash = orig_shellslash
806 set completeslash=
807endfunc
808
Bram Moolenaarcc233582020-12-12 13:32:07 +0100809" Test to ensure 'Scanning...' messages are not recorded in messages history
810func Test_z1_complete_no_history()
811 new
812 messages clear
813 let currmess = execute('messages')
814 setlocal dictionary=README.txt
815 exe "normal owh\<C-X>\<C-K>"
816 exe "normal owh\<C-N>"
817 call assert_equal(currmess, execute('messages'))
818 close!
819endfunc
820
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200821" vim: shiftwidth=2 sts=2 expandtab