blob: 6bb499701b587ce6a943b4bf9a75601661ca5795 [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 Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
12 call mkdir('Xdir')
13 cd Xdir
14
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 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>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
102 call delete('Xtestfile')
103 call delete('Xtest11.one')
104 call delete('Xtest11.two')
105 call delete('Xtestdata')
106 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100107 cd ..
108 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200109endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100110
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000111func Test_ins_complete_invalid_byte()
112 if has('unix') && executable('base64')
113 " this weird command was causing an illegal memory access
114 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
115 call system('base64 -d Xinvalid64 > Xinvalid')
116 call writefile(['qa!'], 'Xexit')
117 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
118 call delete('Xinvalid64')
119 call delete('Xinvalid')
120 call delete('Xexit')
121 endif
122endfunc
123
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100124func Test_omni_dash()
125 func Omni(findstart, base)
126 if a:findstart
127 return 5
128 else
129 echom a:base
130 return ['-help', '-v']
131 endif
132 endfunc
133 set omnifunc=Omni
134 new
135 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100136 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100137
138 bwipe!
139 delfunc Omni
140 set omnifunc=
141endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100142
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200143func Test_omni_autoload()
144 let save_rtp = &rtp
145 set rtp=Xruntime/some
146 let dir = 'Xruntime/some/autoload'
147 call mkdir(dir, 'p')
148
149 let lines =<< trim END
150 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000151 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200152 if findstart
153 return 1
154 else
155 return ['match']
156 endif
157 enddef
158 {
159 eval 1 + 2
160 }
161 END
162 call writefile(lines, dir .. '/omni.vim')
163
164 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000165 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200166 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
167
168 bwipe!
169 call delete('Xruntime', 'rf')
170 set omnifunc=
171 let &rtp = save_rtp
172endfunc
173
Bram Moolenaarffa96842018-06-12 22:05:14 +0200174func Test_completefunc_args()
175 let s:args = []
176 func! CompleteFunc(findstart, base)
177 let s:args += [[a:findstart, empty(a:base)]]
178 endfunc
179 new
180
181 set completefunc=CompleteFunc
182 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200183 call assert_equal([1, 1], s:args[0])
184 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200185 set completefunc=
186
187 let s:args = []
188 set omnifunc=CompleteFunc
189 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200190 call assert_equal([1, 1], s:args[0])
191 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200192 set omnifunc=
193
194 bwipe!
195 unlet s:args
196 delfunc CompleteFunc
197endfunc
198
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100199func s:CompleteDone_CompleteFuncNone( findstart, base )
200 if a:findstart
201 return 0
202 endif
203
204 return v:none
205endfunc
206
Bram Moolenaar1e115362019-01-09 23:01:02 +0100207func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100208 if a:findstart
209 return 0
210 endif
211
212 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100213 \ 'words': [
214 \ {
215 \ 'word': 'aword',
216 \ 'abbr': 'wrd',
217 \ 'menu': 'extra text',
218 \ 'info': 'words are cool',
219 \ 'kind': 'W',
220 \ 'user_data': 'test'
221 \ }
222 \ ]
223 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100224endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100225
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100226func s:CompleteDone_CheckCompletedItemNone()
227 let s:called_completedone = 1
228endfunc
229
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100230func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100231 call assert_equal( 'aword', v:completed_item[ 'word' ] )
232 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
233 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
234 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
235 call assert_equal( 'W', v:completed_item[ 'kind' ] )
236 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
237
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100238 if a:pre
239 call assert_equal('function', complete_info().mode)
240 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100241
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100242 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100243endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100244
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100245func Test_CompleteDoneNone()
246 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200247 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100248
249 set completefunc=<SID>CompleteDone_CompleteFuncNone
250 execute "normal a\<C-X>\<C-U>\<C-Y>"
251 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200252 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100253
254 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200255 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100256
257 let s:called_completedone = 0
258 au! CompleteDone
259endfunc
260
261func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100262 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
263 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100264
265 set completefunc=<SID>CompleteDone_CompleteFuncDict
266 execute "normal a\<C-X>\<C-U>\<C-Y>"
267 set completefunc&
268
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100269 call assert_equal('test', v:completed_item[ 'user_data' ])
270 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100271
272 let s:called_completedone = 0
273 au! CompleteDone
274endfunc
275
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100276func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100277 if a:findstart
278 return 0
279 endif
280
281 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100282 \ 'words': [
283 \ {
284 \ 'word': 'aword',
285 \ 'abbr': 'wrd',
286 \ 'menu': 'extra text',
287 \ 'info': 'words are cool',
288 \ 'kind': 'W',
289 \ 'user_data': ['one', 'two'],
290 \ }
291 \ ]
292 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100293endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100294
Bram Moolenaar1e115362019-01-09 23:01:02 +0100295func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100296 call assert_equal( 'aword', v:completed_item[ 'word' ] )
297 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
298 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
299 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
300 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100301 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100302
303 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100304endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100305
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100306func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100307 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
308
309 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
310 execute "normal a\<C-X>\<C-U>\<C-Y>"
311 set completefunc&
312
Bram Moolenaar08928322020-01-04 14:32:48 +0100313 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100314 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100315
316 let s:called_completedone = 0
317 au! CompleteDone
318endfunc
319
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100320func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100321 if a:findstart
322 return 0
323 endif
324
325 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100326endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100327
Bram Moolenaar1e115362019-01-09 23:01:02 +0100328func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329 call assert_equal( 'aword', v:completed_item[ 'word' ] )
330 call assert_equal( '', v:completed_item[ 'abbr' ] )
331 call assert_equal( '', v:completed_item[ 'menu' ] )
332 call assert_equal( '', v:completed_item[ 'info' ] )
333 call assert_equal( '', v:completed_item[ 'kind' ] )
334 call assert_equal( '', v:completed_item[ 'user_data' ] )
335
336 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100337endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100338
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100339func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100340 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
341
342 set completefunc=<SID>CompleteDone_CompleteFuncList
343 execute "normal a\<C-X>\<C-U>\<C-Y>"
344 set completefunc&
345
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100346 call assert_equal('', v:completed_item[ 'user_data' ])
347 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100348
349 let s:called_completedone = 0
350 au! CompleteDone
351endfunc
352
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200353func Test_CompleteDone_undo()
354 au CompleteDone * call append(0, "prepend1")
355 new
356 call setline(1, ["line1", "line2"])
357 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
358 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
359 \ getline(1, '$'))
360 undo
361 call assert_equal(["line1", "line2"], getline(1, '$'))
362 bwipe!
363 au! CompleteDone
364endfunc
365
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200366func CompleteTest(findstart, query)
367 if a:findstart
368 return col('.')
369 endif
370 return ['matched']
371endfunc
372
373func Test_completefunc_info()
374 new
375 set completeopt=menuone
376 set completefunc=CompleteTest
377 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100378 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 +0200379 bwipe!
380 set completeopt&
381 set completefunc&
382endfunc
383
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100384" Check that when using feedkeys() typeahead does not interrupt searching for
385" completions.
386func Test_compl_feedkeys()
387 new
388 set completeopt=menuone,noselect
389 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
390 call assert_equal("jump jump", getline(1))
391 bwipe!
392 set completeopt&
393endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200394
mityua1198122021-11-20 19:13:39 +0000395func s:ComplInCmdwin_GlobalCompletion(a, l, p)
396 return 'global'
397endfunc
398
399func s:ComplInCmdwin_LocalCompletion(a, l, p)
400 return 'local'
401endfunc
402
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200403func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100404 CheckFeature cmdwin
405
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200406 set wildmenu wildchar=<Tab>
407 com! -nargs=1 -complete=command GetInput let input = <q-args>
408 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100409 let w:test_winvar = 'winvar'
410 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200411
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100412 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200413 let input = ''
414 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
415 call assert_equal('TestCommand', input)
416
417 let input = ''
418 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
419 call assert_equal('T', input)
420
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100421
422 com! -nargs=1 -complete=var GetInput let input = <q-args>
423 " Window-local variables
424 let input = ''
425 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
426 call assert_equal('w:test_winvar', input)
427
428 let input = ''
429 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
430 call assert_equal('w:test_', input)
431
432 " Buffer-local variables
433 let input = ''
434 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
435 call assert_equal('b:test_bufvar', input)
436
437 let input = ''
438 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
439 call assert_equal('b:test_', input)
440
mityua1198122021-11-20 19:13:39 +0000441
442 " Argument completion of buffer-local command
443 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
444 return ['global']
445 endfunc
446
447 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
448 return ['local']
449 endfunc
450
451 func s:ComplInCmdwin_CheckCompletion(arg)
452 call assert_equal('local', a:arg)
453 endfunc
454
455 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
456 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
457 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
458 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
459 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
460
461 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
462 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
463 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
464 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
465
466 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
467
468 func! s:ComplInCmdwin_CheckCompletion(arg)
469 call assert_equal('global', a:arg)
470 endfunc
471 new
472 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
473 quit
474
475 delfunc s:ComplInCmdwin_GlobalCompletion
476 delfunc s:ComplInCmdwin_LocalCompletion
477 delfunc s:ComplInCmdwin_GlobalCompletionList
478 delfunc s:ComplInCmdwin_LocalCompletionList
479 delfunc s:ComplInCmdwin_CheckCompletion
480
481 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200482 delcom TestCommand
483 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100484 unlet w:test_winvar
485 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200486 set wildmenu& wildchar&
487endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200488
489" Test for insert path completion with completeslash option
490func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200491 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200492
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200493 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200494 let orig_shellslash = &shellslash
495 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200496 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200497
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200498 set noshellslash
499
500 set completeslash=
501 exe "normal oXd\<C-X>\<C-F>"
502 call assert_equal('Xdir\', getline('.'))
503
504 set completeslash=backslash
505 exe "normal oXd\<C-X>\<C-F>"
506 call assert_equal('Xdir\', getline('.'))
507
508 set completeslash=slash
509 exe "normal oXd\<C-X>\<C-F>"
510 call assert_equal('Xdir/', getline('.'))
511
512 set shellslash
513
514 set completeslash=
515 exe "normal oXd\<C-X>\<C-F>"
516 call assert_equal('Xdir/', getline('.'))
517
518 set completeslash=backslash
519 exe "normal oXd\<C-X>\<C-F>"
520 call assert_equal('Xdir\', getline('.'))
521
522 set completeslash=slash
523 exe "normal oXd\<C-X>\<C-F>"
524 call assert_equal('Xdir/', getline('.'))
525 %bw!
526 call delete('Xdir', 'rf')
527
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200528 set noshellslash
529 set completeslash=slash
530 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
531
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200532 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200533 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200534endfunc
535
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200536func Test_pum_stopped_by_timer()
537 CheckScreendump
538
539 let lines =<< trim END
540 call setline(1, ['hello', 'hullo', 'heeee', ''])
541 func StartCompl()
542 call timer_start(100, { -> execute('stopinsert') })
543 call feedkeys("Gah\<C-N>")
544 endfunc
545 END
546
547 call writefile(lines, 'Xpumscript')
548 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
549 call term_sendkeys(buf, ":call StartCompl()\<CR>")
550 call TermWait(buf, 200)
551 call term_sendkeys(buf, "k")
552 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
553
554 call StopVimInTerminal(buf)
555 call delete('Xpumscript')
556endfunc
557
zeertzjqcd5dbad2022-05-04 17:51:50 +0100558func Test_complete_stopinsert_startinsert()
559 nnoremap <F2> <Cmd>startinsert<CR>
560 inoremap <F2> <Cmd>stopinsert<CR>
561 " This just checks if this causes an error
562 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
563 nunmap <F2>
564 iunmap <F2>
565endfunc
566
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100567func Test_pum_with_folds_two_tabs()
568 CheckScreendump
569
570 let lines =<< trim END
571 set fdm=marker
572 call setline(1, ['" x {{{1', '" a some text'])
573 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
574 norm! zm
575 tab sp
576 call feedkeys('2Gzv', 'xt')
577 call feedkeys("0fa", 'xt')
578 END
579
580 call writefile(lines, 'Xpumscript')
581 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200582 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100583 call term_sendkeys(buf, "a\<C-N>")
584 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
585
586 call term_sendkeys(buf, "\<Esc>")
587 call StopVimInTerminal(buf)
588 call delete('Xpumscript')
589endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100590
591func Test_pum_with_preview_win()
592 CheckScreendump
593
594 let lines =<< trim END
595 funct Omni_test(findstart, base)
596 if a:findstart
597 return col(".") - 1
598 endif
599 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
600 endfunc
601 set omnifunc=Omni_test
602 set completeopt+=longest
603 END
604
605 call writefile(lines, 'Xpreviewscript')
606 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200607 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100608 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200609 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100610 call term_sendkeys(buf, "\<C-N>")
611 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
612
613 call term_sendkeys(buf, "\<Esc>")
614 call StopVimInTerminal(buf)
615 call delete('Xpreviewscript')
616endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100617
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000618func Test_scrollbar_on_wide_char()
619 CheckScreendump
620
621 let lines =<< trim END
622 call setline(1, ['a', ' 啊啊啊',
623 \ ' 哦哦哦',
624 \ ' 呃呃呃'])
625 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
626 END
627 call writefile(lines, 'Xwidescript')
628 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
629 call term_sendkeys(buf, "A\<C-N>")
630 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
631
632 call StopVimInTerminal(buf)
633 call delete('Xwidescript')
634endfunc
635
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100636" Test for inserting the tag search pattern in insert mode
637func Test_ins_compl_tag_sft()
638 call writefile([
639 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
640 \ "first\tXfoo\t/^int first() {}$/",
641 \ "second\tXfoo\t/^int second() {}$/",
642 \ "third\tXfoo\t/^int third() {}$/"],
643 \ 'Xtags')
644 set tags=Xtags
645 let code =<< trim [CODE]
646 int first() {}
647 int second() {}
648 int third() {}
649 [CODE]
650 call writefile(code, 'Xfoo')
651
652 enew
653 set showfulltag
654 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
655 call assert_equal('int second() {}', getline(1))
656 set noshowfulltag
657
658 call delete('Xtags')
659 call delete('Xfoo')
660 set tags&
661 %bwipe!
662endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200663
664" Test for 'completefunc' deleting text
665func Test_completefunc_error()
666 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200667 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200668 func CompleteFunc(findstart, base)
669 if a:findstart == 1
670 normal dd
671 return col('.') - 1
672 endif
673 return ['a', 'b']
674 endfunc
675 set completefunc=CompleteFunc
676 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100677 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200678
679 " delete text when called for the second time
680 func CompleteFunc2(findstart, base)
681 if a:findstart == 1
682 return col('.') - 1
683 endif
684 normal dd
685 return ['a', 'b']
686 endfunc
687 set completefunc=CompleteFunc2
688 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200689 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200690
Bram Moolenaar97202d92021-01-28 18:34:35 +0100691 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100692 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100693 if a:findstart == 1
694 return col('.') - 1
695 endif
696 wincmd p
697 return ['a', 'b']
698 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100699 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100700 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100701 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100702 close!
703
704 set completefunc&
705 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100706 delfunc CompleteFunc2
707 delfunc CompleteFunc3
708 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100709endfunc
710
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200711" Test for returning non-string values from 'completefunc'
712func Test_completefunc_invalid_data()
713 new
714 func! CompleteFunc(findstart, base)
715 if a:findstart == 1
716 return col('.') - 1
717 endif
718 return [{}, '', 'moon']
719 endfunc
720 set completefunc=CompleteFunc
721 exe "normal i\<C-X>\<C-U>"
722 call assert_equal('moon', getline(1))
723 set completefunc&
724 close!
725endfunc
726
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200727" Test for errors in using complete() function
728func Test_complete_func_error()
729 call assert_fails('call complete(1, ["a"])', 'E785:')
730 func ListColors()
731 call complete(col('.'), "blue")
732 endfunc
733 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
734 func ListMonths()
735 call complete(col('.'), test_null_list())
736 endfunc
737 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
738 delfunc ListColors
739 delfunc ListMonths
740 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200741 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200742endfunc
743
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000744" Test for recursively starting completion mode using complete()
745func Test_recursive_complete_func()
746 func ListColors()
747 call complete(5, ["red", "blue"])
748 return ''
749 endfunc
750 new
751 call setline(1, ['a1', 'a2'])
752 set complete=.
753 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
754 call assert_equal('a2blue', getline(3))
755 delfunc ListColors
756 bw!
757endfunc
758
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200759" Test for completing words following a completed word in a line
760func Test_complete_wrapscan()
761 " complete words from another buffer
762 new
763 call setline(1, ['one two', 'three four'])
764 new
765 setlocal complete=w
766 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
767 call assert_equal('two three four', getline(1))
768 close!
769 " complete words from the current buffer
770 setlocal complete=.
771 %d
772 call setline(1, ['one two', ''])
773 call cursor(2, 1)
774 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
775 call assert_equal('one two one two', getline(2))
776 close!
777endfunc
778
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200779" Test for completing special characters
780func Test_complete_special_chars()
781 new
782 call setline(1, 'int .*[-\^$ func float')
783 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
784 call assert_equal('int .*[-\^$ func float', getline(2))
785 close!
786endfunc
787
788" Test for completion when text is wrapped across lines.
789func Test_complete_across_line()
790 new
791 call setline(1, ['red green blue', 'one two three'])
792 setlocal textwidth=20
793 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
794 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
795 close!
796endfunc
797
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000798" Test for completing words with a '.' at the end of a word.
799func Test_complete_joinspaces()
800 new
801 call setline(1, ['one two.', 'three. four'])
802 set joinspaces
803 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
804 call assert_equal("one two. three. four", getline(3))
805 set joinspaces&
806 bw!
807endfunc
808
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200809" Test for using CTRL-L to add one character when completing matching
810func Test_complete_add_onechar()
811 new
812 call setline(1, ['wool', 'woodwork'])
813 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
814 call assert_equal('woof', getline(3))
815
816 " use 'ignorecase' and backspace to erase characters from the prefix string
817 " and then add letters using CTRL-L
818 %d
819 set ignorecase backspace=2
820 setlocal complete=.
821 call setline(1, ['workhorse', 'workload'])
822 normal Go
823 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
824 call assert_equal('workh', getline(3))
825 set ignorecase& backspace&
826 close!
827endfunc
828
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000829" Test for using CTRL-X CTRL-L to complete whole lines lines
830func Test_complete_wholeline()
831 new
832 " complete one-line
833 call setline(1, ['a1', 'a2'])
834 exe "normal ggoa\<C-X>\<C-L>"
835 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
836 " go to the next match (wrapping around the buffer)
837 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
838 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
839 " go to the next match
840 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
841 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
842 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
843 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
844 " repeat the test using CTRL-L
845 " go to the next match (wrapping around the buffer)
846 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
847 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
848 " go to the next match
849 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
850 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
851 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
852 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
853 %d
854 " use CTRL-X CTRL-L to add one more line
855 call setline(1, ['a1', 'b1'])
856 setlocal complete=.
857 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
858 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
859 bw!
860endfunc
861
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200862" Test insert completion with 'cindent' (adjust the indent)
863func Test_complete_with_cindent()
864 new
865 setlocal cindent
866 call setline(1, ['if (i == 1)', " j = 2;"])
867 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
868 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
869
870 %d
871 call setline(1, ['when while', '{', ''])
872 setlocal cinkeys+==while
873 exe "normal Giwh\<C-P> "
874 call assert_equal("\twhile ", getline('$'))
875 close!
876endfunc
877
878" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
879func Test_complete_cmdline()
880 new
881 exe "normal icaddb\<C-X>\<C-V>"
882 call assert_equal('caddbuffer', getline(1))
883 exe "normal ocall getqf\<C-X>\<C-V>"
884 call assert_equal('call getqflist(', getline(2))
885 exe "normal oabcxyz(\<C-X>\<C-V>"
886 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200887 com! -buffer TestCommand1 echo 'TestCommand1'
888 com! -buffer TestCommand2 echo 'TestCommand2'
889 write TestCommand1Test
890 write TestCommand2Test
891 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
892 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
893 call assert_equal('TestCommand2Test', getline(4))
894 call delete('TestCommand1Test')
895 call delete('TestCommand2Test')
896 delcom TestCommand1
897 delcom TestCommand2
898 close!
899endfunc
900
901" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
902func Test_complete_stop()
903 new
904 func Save_mode1()
905 let g:mode1 = mode(1)
906 return ''
907 endfunc
908 func Save_mode2()
909 let g:mode2 = mode(1)
910 return ''
911 endfunc
912 inoremap <F1> <C-R>=Save_mode1()<CR>
913 inoremap <F2> <C-R>=Save_mode2()<CR>
914 call setline(1, ['aaa bbb ccc '])
915 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
916 call assert_equal('ic', g:mode1)
917 call assert_equal('i', g:mode2)
918 call assert_equal('aaa bbb ccc ', getline(1))
919 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
920 call assert_equal('ic', g:mode1)
921 call assert_equal('i', g:mode2)
922 call assert_equal('aaa bbb ccc aaa', getline(1))
923 set completeopt+=noselect
924 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
925 call assert_equal('ic', g:mode1)
926 call assert_equal('i', g:mode2)
927 call assert_equal('aaa bbb ccc aaa bb', getline(1))
928 set completeopt&
929 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
930 call assert_equal('ic', g:mode1)
931 call assert_equal('i', g:mode2)
932 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
933 com! -buffer TestCommand1 echo 'TestCommand1'
934 com! -buffer TestCommand2 echo 'TestCommand2'
935 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
936 call assert_equal('ic', g:mode1)
937 call assert_equal('i', g:mode2)
938 call assert_equal('TestCommand2', getline(2))
939 delcom TestCommand1
940 delcom TestCommand2
941 unlet g:mode1
942 unlet g:mode2
943 iunmap <F1>
944 iunmap <F2>
945 delfunc Save_mode1
946 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200947 close!
948endfunc
949
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000950" Test for typing CTRL-R in insert completion mode to insert a register
951" content.
952func Test_complete_reginsert()
953 new
954 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
955
956 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
957 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
958 " inserted.
959 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
960 call assert_equal('a123', getline(5))
961 let @r = "\<C-P>\<C-P>"
962 exe "normal GCa\<C-P>\<C-R>r"
963 call assert_equal('a12', getline(5))
964 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
965 call assert_equal('a1234x', getline(5))
966 bw!
967endfunc
968
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200969func Test_issue_7021()
970 CheckMSWindows
971
972 let orig_shellslash = &shellslash
973 set noshellslash
974
975 set completeslash=slash
976 call assert_false(expand('~') =~ '/')
977
978 let &shellslash = orig_shellslash
979 set completeslash=
980endfunc
981
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000982" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
983func Test_complete_longest_match()
984 for e in ['latin1', 'utf-8']
985 exe 'set encoding=' .. e
986 new
987 set complete=.
988 set completeopt=menu,longest
989 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
990 exe "normal Gopfx\<C-P>"
991 call assert_equal('pfx_', getline(5))
992 bw!
993 endfor
994
995 " Test for completing additional words with longest match set
996 new
997 call setline(1, ['abc1', 'abd2'])
998 exe "normal Goab\<C-P>\<C-X>\<C-P>"
999 call assert_equal('ab', getline(3))
1000 bw!
1001 set complete& completeopt&
1002endfunc
1003
1004" Test for removing the first displayed completion match and selecting the
1005" match just before that.
1006func Test_complete_erase_firstmatch()
1007 new
1008 call setline(1, ['a12', 'a34', 'a56'])
1009 set complete=.
1010 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1011 call assert_equal('a34', getline('$'))
1012 set complete&
1013 bw!
1014endfunc
1015
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001016" Test for completing words from unloaded buffers
1017func Test_complete_from_unloadedbuf()
1018 call writefile(['abc'], "Xfile1")
1019 call writefile(['def'], "Xfile2")
1020 edit Xfile1
1021 edit Xfile2
1022 new | close
1023 enew
1024 bunload Xfile1 Xfile2
1025 set complete=u
1026 " complete from an unloaded buffer
1027 exe "normal! ia\<C-P>"
1028 call assert_equal('abc', getline(1))
1029 exe "normal! od\<C-P>"
1030 call assert_equal('def', getline(2))
1031 set complete&
1032 %bw!
1033 call delete("Xfile1")
1034 call delete("Xfile2")
1035endfunc
1036
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001037" Test for completing whole lines from unloaded buffers
1038func Test_complete_wholeline_unloadedbuf()
1039 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1040 edit Xfile1
1041 enew
1042 set complete=u
1043 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1044 call assert_equal('a line2', getline(1))
1045 %d
1046 " completing from an unlisted buffer should fail
1047 bdel Xfile1
1048 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1049 call assert_equal('a', getline(1))
1050 set complete&
1051 %bw!
1052 call delete("Xfile1")
1053endfunc
1054
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001055" Test for completing words from unlisted buffers
1056func Test_complete_from_unlistedbuf()
1057 call writefile(['abc'], "Xfile1")
1058 call writefile(['def'], "Xfile2")
1059 edit Xfile1
1060 edit Xfile2
1061 new | close
1062 bdel Xfile1 Xfile2
1063 set complete=U
1064 " complete from an unlisted buffer
1065 exe "normal! ia\<C-P>"
1066 call assert_equal('abc', getline(1))
1067 exe "normal! od\<C-P>"
1068 call assert_equal('def', getline(2))
1069 set complete&
1070 %bw!
1071 call delete("Xfile1")
1072 call delete("Xfile2")
1073endfunc
1074
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001075" Test for completing whole lines from unlisted buffers
1076func Test_complete_wholeline_unlistedbuf()
1077 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1078 edit Xfile1
1079 enew
1080 set complete=U
1081 " completing from a unloaded buffer should fail
1082 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1083 call assert_equal('a', getline(1))
1084 %d
1085 bdel Xfile1
1086 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1087 call assert_equal('a line2', getline(1))
1088 set complete&
1089 %bw!
1090 call delete("Xfile1")
1091endfunc
1092
1093" Test for adding a multibyte character using CTRL-L in completion mode
1094func Test_complete_mbyte_char_add()
1095 new
1096 set complete=.
1097 call setline(1, 'abÄ—')
1098 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1099 call assert_equal('abÄ—', getline(2))
1100 " Test for a leader with multibyte character
1101 %d
1102 call setline(1, 'abÄ—Ä•')
1103 exe "normal! oabÄ—\<C-P>"
1104 call assert_equal('abÄ—Ä•', getline(2))
1105 bw!
1106endfunc
1107
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001108" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1109" not to complete matches from the local buffer. Also test using multiple
1110" <C-X> to cancel the current completion mode.
1111func Test_complete_local_expansion()
1112 new
1113 set complete=t
1114 call setline(1, ['abc', 'def'])
1115 exe "normal! Go\<C-X>\<C-P>"
1116 call assert_equal("def", getline(3))
1117 exe "normal! Go\<C-P>"
1118 call assert_equal("", getline(4))
1119 exe "normal! Go\<C-X>\<C-N>"
1120 call assert_equal("abc", getline(5))
1121 exe "normal! Go\<C-N>"
1122 call assert_equal("", getline(6))
1123
1124 " use multiple <C-X> to cancel the previous completion mode
1125 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1126 call assert_equal("", getline(7))
1127 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1128 call assert_equal("", getline(8))
1129 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1130 call assert_equal("abc", getline(9))
1131
1132 " interrupt the current completion mode
1133 set completeopt=menu,noinsert
1134 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1135 call assert_equal("abc", getline(10))
1136
1137 " when only one <C-X> is used to interrupt, do normal expansion
1138 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1139 call assert_equal("", getline(11))
1140 set completeopt&
1141
1142 " using two <C-X> in non-completion mode and restarting the same mode
1143 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1144 call assert_equal("def", getline(12))
1145
1146 " test for adding a match from the original empty text
1147 %d
1148 call setline(1, 'abc def g')
1149 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1150 call assert_equal('def', getline(2))
1151 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1152 call assert_equal('abc', getline(2))
1153
1154 bw!
1155endfunc
1156
1157" Test for undoing changes after a insert-mode completion
1158func Test_complete_undo()
1159 new
1160 set complete=.
1161 " undo with 'ignorecase'
1162 call setline(1, ['ABOVE', 'BELOW'])
1163 set ignorecase
1164 exe "normal! Goab\<C-G>u\<C-P>"
1165 call assert_equal("ABOVE", getline(3))
1166 undo
1167 call assert_equal("ab", getline(3))
1168 set ignorecase&
1169 %d
1170 " undo with longest match
1171 set completeopt=menu,longest
1172 call setline(1, ['above', 'about'])
1173 exe "normal! Goa\<C-G>u\<C-P>"
1174 call assert_equal("abo", getline(3))
1175 undo
1176 call assert_equal("a", getline(3))
1177 set completeopt&
1178 %d
1179 " undo for line completion
1180 call setline(1, ['above that change', 'below that change'])
1181 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1182 call assert_equal("above that change", getline(3))
1183 undo
1184 call assert_equal("above", getline(3))
1185
1186 bw!
1187endfunc
1188
1189" Test for completing a very long word
1190func Test_complete_long_word()
1191 set complete&
1192 new
1193 call setline(1, repeat('x', 950) .. ' one two three')
1194 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1195 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1196 %d
1197 " should fail when more than 950 characters are in a word
1198 call setline(1, repeat('x', 951) .. ' one two three')
1199 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1200 call assert_equal(repeat('x', 951), getline(2))
1201
1202 " Test for adding a very long word to an existing completion
1203 %d
1204 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1205 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1206 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1207 bw!
1208endfunc
1209
1210" Test for some fields in the complete items used by complete()
1211func Test_complete_items()
1212 func CompleteItems(idx)
1213 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1214 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1215 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1216 \ [#{user_data: 'u9'}],
1217 \ [#{word: "", user_data: 'u10'}],
1218 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1219 call complete(col('.'), items[a:idx])
1220 return ''
1221 endfunc
1222 new
1223 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1224 call assert_equal('u2', v:completed_item.user_data)
1225 call assert_equal('one', getline(1))
1226 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1227 call assert_equal('u3', v:completed_item.user_data)
1228 call assert_equal('one', getline(2))
1229 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1230 call assert_equal('', getline(3))
1231 set completeopt=menu,noinsert
1232 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1233 call assert_equal('oNE', getline(4))
1234 call assert_equal('u8', v:completed_item.user_data)
1235 set completeopt&
1236 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1237 call assert_equal('', getline(5))
1238 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1239 call assert_equal('', getline(6))
1240 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1241 call assert_equal('', getline(7))
1242 call assert_equal('u11', v:completed_item.user_data)
1243 " pass invalid argument to complete()
1244 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1245 call assert_fails('exe cmd', 'E730:')
1246 bw!
1247 delfunc CompleteItems
1248endfunc
1249
1250" Test for the "refresh" item in the dict returned by an insert completion
1251" function
1252func Test_complete_item_refresh_always()
1253 let g:CallCount = 0
1254 func! Tcomplete(findstart, base)
1255 if a:findstart
1256 " locate the start of the word
1257 let line = getline('.')
1258 let start = col('.') - 1
1259 while start > 0 && line[start - 1] =~ '\a'
1260 let start -= 1
1261 endwhile
1262 return start
1263 else
1264 let g:CallCount += 1
1265 let res = ["update1", "update12", "update123"]
1266 return #{words: res, refresh: 'always'}
1267 endif
1268 endfunc
1269 new
1270 set completeopt=menu,longest
1271 set completefunc=Tcomplete
1272 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1273 call assert_equal('up', getline(1))
1274 call assert_equal(2, g:CallCount)
1275 set completeopt&
1276 set completefunc&
1277 bw!
1278 delfunc Tcomplete
1279endfunc
1280
1281" Test for completing from a thesaurus file without read permission
1282func Test_complete_unreadable_thesaurus_file()
1283 CheckUnix
1284 CheckNotRoot
1285
1286 call writefile(['about', 'above'], 'Xfile')
1287 call setfperm('Xfile', '---r--r--')
1288 new
1289 set complete=sXfile
1290 exe "normal! ia\<C-P>"
1291 call assert_equal('a', getline(1))
1292 bw!
1293 call delete('Xfile')
1294 set complete&
1295endfunc
1296
Bram Moolenaarcc233582020-12-12 13:32:07 +01001297" Test to ensure 'Scanning...' messages are not recorded in messages history
1298func Test_z1_complete_no_history()
1299 new
1300 messages clear
1301 let currmess = execute('messages')
1302 setlocal dictionary=README.txt
1303 exe "normal owh\<C-X>\<C-K>"
1304 exe "normal owh\<C-N>"
1305 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001306 bwipe!
1307endfunc
1308
1309" A mapping is not used for the key after CTRL-X.
1310func Test_no_mapping_for_ctrl_x_key()
1311 new
1312 inoremap <C-K> <Cmd>let was_mapped = 'yes'<CR>
1313 setlocal dictionary=README.txt
1314 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1315 call assert_equal('example ', getline(1))
1316 call assert_false(exists('was_mapped'))
1317 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001318endfunc
1319
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001320" Test for different ways of setting the 'completefunc' option
1321func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001322 func CompleteFunc1(callnr, findstart, base)
1323 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1324 return a:findstart ? 0 : []
1325 endfunc
1326 func CompleteFunc2(findstart, base)
1327 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001328 return a:findstart ? 0 : []
1329 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001330
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001331 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001332 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001333 LET &completefunc = 'g:CompleteFunc2'
1334 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001335 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001336 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001337 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001338 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001339 bw!
1340
1341 #" Test for using a function()
1342 set completefunc=function('g:CompleteFunc1',\ [10])
1343 new
1344 call setline(1, 'one')
1345 LET g:CompleteFunc1Args = []
1346 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1347 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001349
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001350 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001353 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001354 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001355 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001356 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001357 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001358 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001359
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001360 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001361 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001362 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001363 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001364 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001365 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001366 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001367 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001368 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001369
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001370 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001371 set completefunc=funcref('g:CompleteFunc1',\ [13])
1372 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001374 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001375 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001376 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001378
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001380 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001382 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001384 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001385 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001386 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001387 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001388
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001389 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001390 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001391 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001392 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001393 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001394 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001396 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001398
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001400 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001401 LET optval = substitute(optval, ' ', '\\ ', 'g')
1402 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001403 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001405 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001407 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001408 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001409
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001411 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001412 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001414 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001416 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001417 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001418
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001419 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001420 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001421 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001422 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001423 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001424 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001425 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001426 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001427
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001428 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001429 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001430 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001431 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001433 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001434 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001435 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001436 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001437
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001438 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001439 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001440 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001441 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001442 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001443 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001444 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001445 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001446 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001447
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001448 #" Test for using a lambda function with incorrect return value
1449 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1450 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001451 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001452 call setline(1, 'eight')
1453 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1454 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001455
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001456 #" Test for clearing the 'completefunc' option
1457 set completefunc=''
1458 set completefunc&
1459 call assert_fails("set completefunc=function('abc')", "E700:")
1460 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001461
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001462 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001463 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001464 call setline(1, 'five')
1465 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1466 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001467 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001468 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001469 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001470 bw!
1471 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001472 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001473
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001474 " Test for using a script-local function name
1475 func s:CompleteFunc3(findstart, base)
1476 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1477 return a:findstart ? 0 : []
1478 endfunc
1479 set completefunc=s:CompleteFunc3
1480 new
1481 call setline(1, 'script1')
1482 let g:CompleteFunc3Args = []
1483 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1484 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1485 bw!
1486
1487 let &completefunc = 's:CompleteFunc3'
1488 new
1489 call setline(1, 'script2')
1490 let g:CompleteFunc3Args = []
1491 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1492 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1493 bw!
1494 delfunc s:CompleteFunc3
1495
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001496 " In Vim9 script s: can be omitted
1497 let lines =<< trim END
1498 vim9script
1499 var CompleteFunc4Args = []
1500 def CompleteFunc4(findstart: bool, base: string): any
1501 add(CompleteFunc4Args, [findstart, base])
1502 return findstart ? 0 : []
1503 enddef
1504 set completefunc=CompleteFunc4
1505 new
1506 setline(1, 'script1')
1507 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1508 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1509 bw!
1510 END
1511 call v9.CheckScriptSuccess(lines)
1512
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001513 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001514 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001515 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1516
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001517 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001518 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001519 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001520 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001521 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001522 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001523
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001524 " set 'completefunc' to a partial with dict. This used to cause a crash.
1525 func SetCompleteFunc()
1526 let params = {'complete': function('g:DictCompleteFunc')}
1527 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001528 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001529 func g:DictCompleteFunc(_) dict
1530 endfunc
1531 call SetCompleteFunc()
1532 new
1533 call SetCompleteFunc()
1534 bw
1535 call test_garbagecollect_now()
1536 new
1537 set completefunc=
1538 wincmd w
1539 set completefunc=
1540 %bw!
1541 delfunc g:DictCompleteFunc
1542 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001543
1544 " Vim9 tests
1545 let lines =<< trim END
1546 vim9script
1547
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001548 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1549 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001550 return findstart ? 0 : []
1551 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001552
1553 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001554 set completefunc=function('Vim9CompleteFunc',\ [60])
1555 new | only
1556 setline(1, 'one')
1557 g:Vim9completeFuncArgs = []
1558 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1559 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1560 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001561
1562 # Test for using a global function name
1563 &completefunc = g:CompleteFunc2
1564 new | only
1565 setline(1, 'two')
1566 g:CompleteFunc2Args = []
1567 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1568 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1569 bw!
1570
1571 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001572 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001573 add(g:LocalCompleteFuncArgs, [findstart, base])
1574 return findstart ? 0 : []
1575 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001576 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001577 new | only
1578 setline(1, 'three')
1579 g:LocalCompleteFuncArgs = []
1580 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1581 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1582 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001583 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001584 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001585
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001586 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001587 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001588 delfunc CompleteFunc1
1589 delfunc CompleteFunc2
1590 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001591 %bw!
1592endfunc
1593
1594" Test for different ways of setting the 'omnifunc' option
1595func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001596 func OmniFunc1(callnr, findstart, base)
1597 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1598 return a:findstart ? 0 : []
1599 endfunc
1600 func OmniFunc2(findstart, base)
1601 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001602 return a:findstart ? 0 : []
1603 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001604
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001606 #" Test for using a function name
1607 LET &omnifunc = 'g:OmniFunc2'
1608 new
1609 call setline(1, 'zero')
1610 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001611 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001612 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1613 bw!
1614
1615 #" Test for using a function()
1616 set omnifunc=function('g:OmniFunc1',\ [10])
1617 new
1618 call setline(1, 'one')
1619 LET g:OmniFunc1Args = []
1620 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1621 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001623
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001629 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001631 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001632 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001633
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001635 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001637 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001639 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001641 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001643
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001645 set omnifunc=funcref('g:OmniFunc1',\ [13])
1646 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001652
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001654 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001656 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001658 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001659 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001660 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001661 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001662
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001663 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001664 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001665 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001666 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001670 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001672
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001674 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 LET optval = substitute(optval, ' ', '\\ ', 'g')
1676 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001677 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001679 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001681 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001683
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001685 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001686 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001688 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001690 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001691 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001692
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001694 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001695 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001696 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001697 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001698 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001699 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001700 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001701
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001702 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001703 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001704 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001706 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001707 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001708 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001709 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001710 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001711
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001712 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001713 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001714 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001715 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001716 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001717 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001718 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001719 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001720 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001721
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001722 #" Test for using a lambda function with incorrect return value
1723 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1724 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001725 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001726 call setline(1, 'eight')
1727 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1728 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001729
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001730 #" Test for clearing the 'omnifunc' option
1731 set omnifunc=''
1732 set omnifunc&
1733 call assert_fails("set omnifunc=function('abc')", "E700:")
1734 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001735
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001736 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001737 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001738 call setline(1, 'nine')
1739 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1740 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001741 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001742 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001743 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001744 bw!
1745 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001746 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001747
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001748 " Test for using a script-local function name
1749 func s:OmniFunc3(findstart, base)
1750 call add(g:OmniFunc3Args, [a:findstart, a:base])
1751 return a:findstart ? 0 : []
1752 endfunc
1753 set omnifunc=s:OmniFunc3
1754 new
1755 call setline(1, 'script1')
1756 let g:OmniFunc3Args = []
1757 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1758 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1759 bw!
1760
1761 let &omnifunc = 's:OmniFunc3'
1762 new
1763 call setline(1, 'script2')
1764 let g:OmniFunc3Args = []
1765 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1766 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1767 bw!
1768 delfunc s:OmniFunc3
1769
1770 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001771 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001772 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1773
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001774 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001775 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001776 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001777 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001778 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001779 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001780
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001781 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1782 func SetOmniFunc()
1783 let params = {'omni': function('g:DictOmniFunc')}
1784 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001785 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001786 func g:DictOmniFunc(_) dict
1787 endfunc
1788 call SetOmniFunc()
1789 new
1790 call SetOmniFunc()
1791 bw
1792 call test_garbagecollect_now()
1793 new
1794 set omnifunc=
1795 wincmd w
1796 set omnifunc=
1797 %bw!
1798 delfunc g:DictOmniFunc
1799 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001800
1801 " Vim9 tests
1802 let lines =<< trim END
1803 vim9script
1804
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001805 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1806 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001807 return findstart ? 0 : []
1808 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001809
1810 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001811 set omnifunc=function('Vim9omniFunc',\ [60])
1812 new | only
1813 setline(1, 'one')
1814 g:Vim9omniFunc_Args = []
1815 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1816 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1817 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001818
1819 # Test for using a global function name
1820 &omnifunc = g:OmniFunc2
1821 new | only
1822 setline(1, 'two')
1823 g:OmniFunc2Args = []
1824 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1825 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1826 bw!
1827
1828 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001829 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001830 add(g:LocalOmniFuncArgs, [findstart, base])
1831 return findstart ? 0 : []
1832 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001833 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001834 new | only
1835 setline(1, 'three')
1836 g:LocalOmniFuncArgs = []
1837 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1838 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1839 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001840 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001841 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001842
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001843 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001844 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001845 delfunc OmniFunc1
1846 delfunc OmniFunc2
1847 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001848 %bw!
1849endfunc
1850
1851" Test for different ways of setting the 'thesaurusfunc' option
1852func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001853 func TsrFunc1(callnr, findstart, base)
1854 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001855 return a:findstart ? 0 : []
1856 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001857 func TsrFunc2(findstart, base)
1858 call add(g:TsrFunc2Args, [a:findstart, a:base])
1859 return a:findstart ? 0 : ['sunday']
1860 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001861
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001862 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001863 #" Test for using a function name
1864 LET &thesaurusfunc = 'g:TsrFunc2'
1865 new
1866 call setline(1, 'zero')
1867 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001868 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001869 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1870 bw!
1871
1872 #" Test for using a function()
1873 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1874 new
1875 call setline(1, 'one')
1876 LET g:TsrFunc1Args = []
1877 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1878 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001879 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001880
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001881 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001882 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001883 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001884 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001885 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001886 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001887 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001888 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001889 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001890
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001891 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001892 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001893 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001894 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001895 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001896 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001897 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001898 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001899 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001900
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001901 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001902 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1903 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001905 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001907 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001909
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001911 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001913 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001915 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001916 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001917 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001918 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001919
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001920 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001921 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001922 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001923 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001924 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001925 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001927 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001929
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001930 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001931 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001932 LET optval = substitute(optval, ' ', '\\ ', 'g')
1933 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001934 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001936 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001937 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001938 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001939 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001940
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001942 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001943 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001945 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001947 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001948 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001949
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001950 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001951 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001952 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001953 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001954 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001955 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001956 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001958
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001960 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001961 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001964 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001965 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001966 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001967 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001968
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001969 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001970 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001971 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001972 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001973 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001974 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001976 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001977 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001978
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001979 #" Test for using a lambda function with incorrect return value
1980 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1981 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001982 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001983 call setline(1, 'eight')
1984 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1985 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001986
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001987 #" Test for clearing the 'thesaurusfunc' option
1988 set thesaurusfunc=''
1989 set thesaurusfunc&
1990 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1991 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001992
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001993 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001994 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001995 call setline(1, 'ten')
1996 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1997 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001998 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001999 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002000 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002001 bw!
2002
2003 #" Use a buffer-local value and a global value
2004 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002005 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002006 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002007 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002008 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2009 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002010 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002011 new
2012 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002013 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002014 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2015 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002016 call assert_equal([], g:TsrFunc1Args)
2017 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002018 wincmd w
2019 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002020 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002021 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2022 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002023 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002024 :%bw!
2025 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002026 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002027
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002028 " Test for using a script-local function name
2029 func s:TsrFunc3(findstart, base)
2030 call add(g:TsrFunc3Args, [a:findstart, a:base])
2031 return a:findstart ? 0 : []
2032 endfunc
2033 set tsrfu=s:TsrFunc3
2034 new
2035 call setline(1, 'script1')
2036 let g:TsrFunc3Args = []
2037 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2038 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2039 bw!
2040
2041 let &tsrfu = 's:TsrFunc3'
2042 new
2043 call setline(1, 'script2')
2044 let g:TsrFunc3Args = []
2045 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2046 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2047 bw!
2048 delfunc s:TsrFunc3
2049
2050 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002051 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002052 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2053
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002054 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002055 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002056 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002057 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002058 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002059 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002060 bw!
2061
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002062 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2063 func SetTsrFunc()
2064 let params = {'thesaurus': function('g:DictTsrFunc')}
2065 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002066 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002067 func g:DictTsrFunc(_) dict
2068 endfunc
2069 call SetTsrFunc()
2070 new
2071 call SetTsrFunc()
2072 bw
2073 call test_garbagecollect_now()
2074 new
2075 set thesaurusfunc=
2076 wincmd w
2077 %bw!
2078 delfunc SetTsrFunc
2079
2080 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2081 " cause a crash.
2082 func SetLocalTsrFunc()
2083 let params = {'thesaurus': function('g:DictTsrFunc')}
2084 let &l:thesaurusfunc = params.thesaurus
2085 endfunc
2086 call SetLocalTsrFunc()
2087 call test_garbagecollect_now()
2088 call SetLocalTsrFunc()
2089 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002090 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002091 delfunc g:DictTsrFunc
2092 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002093
2094 " Vim9 tests
2095 let lines =<< trim END
2096 vim9script
2097
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002098 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2099 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002100 return findstart ? 0 : []
2101 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002102
2103 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002104 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2105 new | only
2106 setline(1, 'one')
2107 g:Vim9tsrFunc_Args = []
2108 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2109 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2110 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002111
2112 # Test for using a global function name
2113 &thesaurusfunc = g:TsrFunc2
2114 new | only
2115 setline(1, 'two')
2116 g:TsrFunc2Args = []
2117 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2118 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2119 bw!
2120
2121 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002122 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002123 add(g:LocalTsrFuncArgs, [findstart, base])
2124 return findstart ? 0 : []
2125 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002126 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002127 new | only
2128 setline(1, 'three')
2129 g:LocalTsrFuncArgs = []
2130 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2131 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2132 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002133 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002134 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002135
2136 " cleanup
2137 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002138 delfunc TsrFunc1
2139 delfunc TsrFunc2
2140 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002141 %bw!
2142endfunc
2143
Christian Brabandtac72c212022-04-07 21:00:53 +01002144func FooBarComplete(findstart, base)
2145 if a:findstart
2146 return col('.') - 1
2147 else
2148 return ["Foo", "Bar", "}"]
2149 endif
2150endfunc
2151
2152func Test_complete_smartindent()
2153 new
2154 setlocal smartindent completefunc=FooBarComplete
2155
2156 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2157 let result = getline(1,'$')
2158 call assert_equal(['', '{','}',''], result)
2159 bw!
2160 delfunction! FooBarComplete
2161endfunc
2162
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002163" vim: shiftwidth=2 sts=2 expandtab