blob: ee6641835be7e1a6ef74cb344ea426518a61df42 [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
mityua1198122021-11-20 19:13:39 +0000376func s:ComplInCmdwin_GlobalCompletion(a, l, p)
377 return 'global'
378endfunc
379
380func s:ComplInCmdwin_LocalCompletion(a, l, p)
381 return 'local'
382endfunc
383
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200384func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100385 CheckFeature cmdwin
386
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200387 set wildmenu wildchar=<Tab>
388 com! -nargs=1 -complete=command GetInput let input = <q-args>
389 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100390 let w:test_winvar = 'winvar'
391 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200392
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100393 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200394 let input = ''
395 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
396 call assert_equal('TestCommand', input)
397
398 let input = ''
399 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
400 call assert_equal('T', input)
401
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100402
403 com! -nargs=1 -complete=var GetInput let input = <q-args>
404 " Window-local variables
405 let input = ''
406 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
407 call assert_equal('w:test_winvar', input)
408
409 let input = ''
410 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
411 call assert_equal('w:test_', input)
412
413 " Buffer-local variables
414 let input = ''
415 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
416 call assert_equal('b:test_bufvar', input)
417
418 let input = ''
419 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
420 call assert_equal('b:test_', input)
421
mityua1198122021-11-20 19:13:39 +0000422
423 " Argument completion of buffer-local command
424 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
425 return ['global']
426 endfunc
427
428 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
429 return ['local']
430 endfunc
431
432 func s:ComplInCmdwin_CheckCompletion(arg)
433 call assert_equal('local', a:arg)
434 endfunc
435
436 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
437 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
438 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
439 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
440 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
441
442 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
443 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
444 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
445 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
446
447 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
448
449 func! s:ComplInCmdwin_CheckCompletion(arg)
450 call assert_equal('global', a:arg)
451 endfunc
452 new
453 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
454 quit
455
456 delfunc s:ComplInCmdwin_GlobalCompletion
457 delfunc s:ComplInCmdwin_LocalCompletion
458 delfunc s:ComplInCmdwin_GlobalCompletionList
459 delfunc s:ComplInCmdwin_LocalCompletionList
460 delfunc s:ComplInCmdwin_CheckCompletion
461
462 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200463 delcom TestCommand
464 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100465 unlet w:test_winvar
466 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200467 set wildmenu& wildchar&
468endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200469
470" Test for insert path completion with completeslash option
471func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200472 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200473
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200474 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200475 let orig_shellslash = &shellslash
476 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200477 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200478
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200479 set noshellslash
480
481 set completeslash=
482 exe "normal oXd\<C-X>\<C-F>"
483 call assert_equal('Xdir\', getline('.'))
484
485 set completeslash=backslash
486 exe "normal oXd\<C-X>\<C-F>"
487 call assert_equal('Xdir\', getline('.'))
488
489 set completeslash=slash
490 exe "normal oXd\<C-X>\<C-F>"
491 call assert_equal('Xdir/', getline('.'))
492
493 set shellslash
494
495 set completeslash=
496 exe "normal oXd\<C-X>\<C-F>"
497 call assert_equal('Xdir/', getline('.'))
498
499 set completeslash=backslash
500 exe "normal oXd\<C-X>\<C-F>"
501 call assert_equal('Xdir\', getline('.'))
502
503 set completeslash=slash
504 exe "normal oXd\<C-X>\<C-F>"
505 call assert_equal('Xdir/', getline('.'))
506 %bw!
507 call delete('Xdir', 'rf')
508
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200509 set noshellslash
510 set completeslash=slash
511 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
512
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200513 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200514 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200515endfunc
516
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200517func Test_pum_stopped_by_timer()
518 CheckScreendump
519
520 let lines =<< trim END
521 call setline(1, ['hello', 'hullo', 'heeee', ''])
522 func StartCompl()
523 call timer_start(100, { -> execute('stopinsert') })
524 call feedkeys("Gah\<C-N>")
525 endfunc
526 END
527
528 call writefile(lines, 'Xpumscript')
529 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
530 call term_sendkeys(buf, ":call StartCompl()\<CR>")
531 call TermWait(buf, 200)
532 call term_sendkeys(buf, "k")
533 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
534
535 call StopVimInTerminal(buf)
536 call delete('Xpumscript')
537endfunc
538
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100539func Test_pum_with_folds_two_tabs()
540 CheckScreendump
541
542 let lines =<< trim END
543 set fdm=marker
544 call setline(1, ['" x {{{1', '" a some text'])
545 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
546 norm! zm
547 tab sp
548 call feedkeys('2Gzv', 'xt')
549 call feedkeys("0fa", 'xt')
550 END
551
552 call writefile(lines, 'Xpumscript')
553 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200554 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100555 call term_sendkeys(buf, "a\<C-N>")
556 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
557
558 call term_sendkeys(buf, "\<Esc>")
559 call StopVimInTerminal(buf)
560 call delete('Xpumscript')
561endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100562
563func Test_pum_with_preview_win()
564 CheckScreendump
565
566 let lines =<< trim END
567 funct Omni_test(findstart, base)
568 if a:findstart
569 return col(".") - 1
570 endif
571 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
572 endfunc
573 set omnifunc=Omni_test
574 set completeopt+=longest
575 END
576
577 call writefile(lines, 'Xpreviewscript')
578 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200579 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100580 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200581 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100582 call term_sendkeys(buf, "\<C-N>")
583 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
584
585 call term_sendkeys(buf, "\<Esc>")
586 call StopVimInTerminal(buf)
587 call delete('Xpreviewscript')
588endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100589
590" Test for inserting the tag search pattern in insert mode
591func Test_ins_compl_tag_sft()
592 call writefile([
593 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
594 \ "first\tXfoo\t/^int first() {}$/",
595 \ "second\tXfoo\t/^int second() {}$/",
596 \ "third\tXfoo\t/^int third() {}$/"],
597 \ 'Xtags')
598 set tags=Xtags
599 let code =<< trim [CODE]
600 int first() {}
601 int second() {}
602 int third() {}
603 [CODE]
604 call writefile(code, 'Xfoo')
605
606 enew
607 set showfulltag
608 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
609 call assert_equal('int second() {}', getline(1))
610 set noshowfulltag
611
612 call delete('Xtags')
613 call delete('Xfoo')
614 set tags&
615 %bwipe!
616endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200617
618" Test for 'completefunc' deleting text
619func Test_completefunc_error()
620 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200621 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200622 func CompleteFunc(findstart, base)
623 if a:findstart == 1
624 normal dd
625 return col('.') - 1
626 endif
627 return ['a', 'b']
628 endfunc
629 set completefunc=CompleteFunc
630 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100631 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200632
633 " delete text when called for the second time
634 func CompleteFunc2(findstart, base)
635 if a:findstart == 1
636 return col('.') - 1
637 endif
638 normal dd
639 return ['a', 'b']
640 endfunc
641 set completefunc=CompleteFunc2
642 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200643 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200644
Bram Moolenaar97202d92021-01-28 18:34:35 +0100645 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100646 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100647 if a:findstart == 1
648 return col('.') - 1
649 endif
650 wincmd p
651 return ['a', 'b']
652 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100653 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100654 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100655 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100656 close!
657
658 set completefunc&
659 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100660 delfunc CompleteFunc2
661 delfunc CompleteFunc3
662 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100663endfunc
664
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200665" Test for returning non-string values from 'completefunc'
666func Test_completefunc_invalid_data()
667 new
668 func! CompleteFunc(findstart, base)
669 if a:findstart == 1
670 return col('.') - 1
671 endif
672 return [{}, '', 'moon']
673 endfunc
674 set completefunc=CompleteFunc
675 exe "normal i\<C-X>\<C-U>"
676 call assert_equal('moon', getline(1))
677 set completefunc&
678 close!
679endfunc
680
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200681" Test for errors in using complete() function
682func Test_complete_func_error()
683 call assert_fails('call complete(1, ["a"])', 'E785:')
684 func ListColors()
685 call complete(col('.'), "blue")
686 endfunc
687 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
688 func ListMonths()
689 call complete(col('.'), test_null_list())
690 endfunc
691 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
692 delfunc ListColors
693 delfunc ListMonths
694 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200695 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200696endfunc
697
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200698" Test for completing words following a completed word in a line
699func Test_complete_wrapscan()
700 " complete words from another buffer
701 new
702 call setline(1, ['one two', 'three four'])
703 new
704 setlocal complete=w
705 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
706 call assert_equal('two three four', getline(1))
707 close!
708 " complete words from the current buffer
709 setlocal complete=.
710 %d
711 call setline(1, ['one two', ''])
712 call cursor(2, 1)
713 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
714 call assert_equal('one two one two', getline(2))
715 close!
716endfunc
717
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200718" Test for completing special characters
719func Test_complete_special_chars()
720 new
721 call setline(1, 'int .*[-\^$ func float')
722 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
723 call assert_equal('int .*[-\^$ func float', getline(2))
724 close!
725endfunc
726
727" Test for completion when text is wrapped across lines.
728func Test_complete_across_line()
729 new
730 call setline(1, ['red green blue', 'one two three'])
731 setlocal textwidth=20
732 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
733 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
734 close!
735endfunc
736
737" Test for using CTRL-L to add one character when completing matching
738func Test_complete_add_onechar()
739 new
740 call setline(1, ['wool', 'woodwork'])
741 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
742 call assert_equal('woof', getline(3))
743
744 " use 'ignorecase' and backspace to erase characters from the prefix string
745 " and then add letters using CTRL-L
746 %d
747 set ignorecase backspace=2
748 setlocal complete=.
749 call setline(1, ['workhorse', 'workload'])
750 normal Go
751 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
752 call assert_equal('workh', getline(3))
753 set ignorecase& backspace&
754 close!
755endfunc
756
757" Test insert completion with 'cindent' (adjust the indent)
758func Test_complete_with_cindent()
759 new
760 setlocal cindent
761 call setline(1, ['if (i == 1)', " j = 2;"])
762 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
763 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
764
765 %d
766 call setline(1, ['when while', '{', ''])
767 setlocal cinkeys+==while
768 exe "normal Giwh\<C-P> "
769 call assert_equal("\twhile ", getline('$'))
770 close!
771endfunc
772
773" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
774func Test_complete_cmdline()
775 new
776 exe "normal icaddb\<C-X>\<C-V>"
777 call assert_equal('caddbuffer', getline(1))
778 exe "normal ocall getqf\<C-X>\<C-V>"
779 call assert_equal('call getqflist(', getline(2))
780 exe "normal oabcxyz(\<C-X>\<C-V>"
781 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200782 com! -buffer TestCommand1 echo 'TestCommand1'
783 com! -buffer TestCommand2 echo 'TestCommand2'
784 write TestCommand1Test
785 write TestCommand2Test
786 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
787 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
788 call assert_equal('TestCommand2Test', getline(4))
789 call delete('TestCommand1Test')
790 call delete('TestCommand2Test')
791 delcom TestCommand1
792 delcom TestCommand2
793 close!
794endfunc
795
796" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
797func Test_complete_stop()
798 new
799 func Save_mode1()
800 let g:mode1 = mode(1)
801 return ''
802 endfunc
803 func Save_mode2()
804 let g:mode2 = mode(1)
805 return ''
806 endfunc
807 inoremap <F1> <C-R>=Save_mode1()<CR>
808 inoremap <F2> <C-R>=Save_mode2()<CR>
809 call setline(1, ['aaa bbb ccc '])
810 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
811 call assert_equal('ic', g:mode1)
812 call assert_equal('i', g:mode2)
813 call assert_equal('aaa bbb ccc ', getline(1))
814 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
815 call assert_equal('ic', g:mode1)
816 call assert_equal('i', g:mode2)
817 call assert_equal('aaa bbb ccc aaa', getline(1))
818 set completeopt+=noselect
819 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
820 call assert_equal('ic', g:mode1)
821 call assert_equal('i', g:mode2)
822 call assert_equal('aaa bbb ccc aaa bb', getline(1))
823 set completeopt&
824 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
825 call assert_equal('ic', g:mode1)
826 call assert_equal('i', g:mode2)
827 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
828 com! -buffer TestCommand1 echo 'TestCommand1'
829 com! -buffer TestCommand2 echo 'TestCommand2'
830 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
831 call assert_equal('ic', g:mode1)
832 call assert_equal('i', g:mode2)
833 call assert_equal('TestCommand2', getline(2))
834 delcom TestCommand1
835 delcom TestCommand2
836 unlet g:mode1
837 unlet g:mode2
838 iunmap <F1>
839 iunmap <F2>
840 delfunc Save_mode1
841 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200842 close!
843endfunc
844
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200845func Test_issue_7021()
846 CheckMSWindows
847
848 let orig_shellslash = &shellslash
849 set noshellslash
850
851 set completeslash=slash
852 call assert_false(expand('~') =~ '/')
853
854 let &shellslash = orig_shellslash
855 set completeslash=
856endfunc
857
Bram Moolenaarcc233582020-12-12 13:32:07 +0100858" Test to ensure 'Scanning...' messages are not recorded in messages history
859func Test_z1_complete_no_history()
860 new
861 messages clear
862 let currmess = execute('messages')
863 setlocal dictionary=README.txt
864 exe "normal owh\<C-X>\<C-K>"
865 exe "normal owh\<C-N>"
866 call assert_equal(currmess, execute('messages'))
867 close!
868endfunc
869
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200870" vim: shiftwidth=2 sts=2 expandtab