blob: f775c1c45af158aa3f2eb7fabacd9a42c6c8ff3f [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
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005source vim9.vim
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
50 set cpt=.,w,i
51 " 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
74 set cpt=w
75 " checks make_cyclic in other window
76 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
77 call assert_equal('STARTTEST', getline('.'))
78
79 set cpt=u nohid
80 " checks unloaded buffer expansion
81 only
82 exe "normal oEN\<C-N>"
83 call assert_equal('ENDTEST', getline('.'))
84 " checks adding mode abortion
85 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
86 call assert_equal('unless', getline('.'))
87
88 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
89 " tag expansion, define add-expansion interrupted
90 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>"
91 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
92 " t-expansion
93 exe "normal oa\<C-N>\<Esc>"
94 call assert_equal('asd', getline('.'))
95
96 %bw!
97 call delete('Xtestfile')
98 call delete('Xtest11.one')
99 call delete('Xtest11.two')
100 call delete('Xtestdata')
101 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100102 cd ..
103 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200104endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100105
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000106func Test_ins_complete_invalid_byte()
107 if has('unix') && executable('base64')
108 " this weird command was causing an illegal memory access
109 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
110 call system('base64 -d Xinvalid64 > Xinvalid')
111 call writefile(['qa!'], 'Xexit')
112 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
113 call delete('Xinvalid64')
114 call delete('Xinvalid')
115 call delete('Xexit')
116 endif
117endfunc
118
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100119func Test_omni_dash()
120 func Omni(findstart, base)
121 if a:findstart
122 return 5
123 else
124 echom a:base
125 return ['-help', '-v']
126 endif
127 endfunc
128 set omnifunc=Omni
129 new
130 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100131 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100132
133 bwipe!
134 delfunc Omni
135 set omnifunc=
136endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100137
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200138func Test_omni_autoload()
139 let save_rtp = &rtp
140 set rtp=Xruntime/some
141 let dir = 'Xruntime/some/autoload'
142 call mkdir(dir, 'p')
143
144 let lines =<< trim END
145 vim9script
146 def omni#func(findstart: bool, base: string): any
147 if findstart
148 return 1
149 else
150 return ['match']
151 endif
152 enddef
153 {
154 eval 1 + 2
155 }
156 END
157 call writefile(lines, dir .. '/omni.vim')
158
159 new
160 setlocal omnifunc=omni#func
161 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
162
163 bwipe!
164 call delete('Xruntime', 'rf')
165 set omnifunc=
166 let &rtp = save_rtp
167endfunc
168
Bram Moolenaarffa96842018-06-12 22:05:14 +0200169func Test_completefunc_args()
170 let s:args = []
171 func! CompleteFunc(findstart, base)
172 let s:args += [[a:findstart, empty(a:base)]]
173 endfunc
174 new
175
176 set completefunc=CompleteFunc
177 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200178 call assert_equal([1, 1], s:args[0])
179 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200180 set completefunc=
181
182 let s:args = []
183 set omnifunc=CompleteFunc
184 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200185 call assert_equal([1, 1], s:args[0])
186 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200187 set omnifunc=
188
189 bwipe!
190 unlet s:args
191 delfunc CompleteFunc
192endfunc
193
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100194func s:CompleteDone_CompleteFuncNone( findstart, base )
195 if a:findstart
196 return 0
197 endif
198
199 return v:none
200endfunc
201
Bram Moolenaar1e115362019-01-09 23:01:02 +0100202func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100203 if a:findstart
204 return 0
205 endif
206
207 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100208 \ 'words': [
209 \ {
210 \ 'word': 'aword',
211 \ 'abbr': 'wrd',
212 \ 'menu': 'extra text',
213 \ 'info': 'words are cool',
214 \ 'kind': 'W',
215 \ 'user_data': 'test'
216 \ }
217 \ ]
218 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100219endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100220
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100221func s:CompleteDone_CheckCompletedItemNone()
222 let s:called_completedone = 1
223endfunc
224
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100225func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100226 call assert_equal( 'aword', v:completed_item[ 'word' ] )
227 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
228 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
229 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
230 call assert_equal( 'W', v:completed_item[ 'kind' ] )
231 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
232
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100233 if a:pre
234 call assert_equal('function', complete_info().mode)
235 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100236
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100237 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100238endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100239
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100240func Test_CompleteDoneNone()
241 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200242 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100243
244 set completefunc=<SID>CompleteDone_CompleteFuncNone
245 execute "normal a\<C-X>\<C-U>\<C-Y>"
246 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200247 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100248
249 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200250 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100251
252 let s:called_completedone = 0
253 au! CompleteDone
254endfunc
255
256func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100257 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
258 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100259
260 set completefunc=<SID>CompleteDone_CompleteFuncDict
261 execute "normal a\<C-X>\<C-U>\<C-Y>"
262 set completefunc&
263
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100264 call assert_equal('test', v:completed_item[ 'user_data' ])
265 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100266
267 let s:called_completedone = 0
268 au! CompleteDone
269endfunc
270
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100271func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100272 if a:findstart
273 return 0
274 endif
275
276 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100277 \ 'words': [
278 \ {
279 \ 'word': 'aword',
280 \ 'abbr': 'wrd',
281 \ 'menu': 'extra text',
282 \ 'info': 'words are cool',
283 \ 'kind': 'W',
284 \ 'user_data': ['one', 'two'],
285 \ }
286 \ ]
287 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100288endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100289
Bram Moolenaar1e115362019-01-09 23:01:02 +0100290func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100291 call assert_equal( 'aword', v:completed_item[ 'word' ] )
292 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
293 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
294 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
295 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100296 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100297
298 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100299endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100300
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100301func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100302 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
303
304 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
305 execute "normal a\<C-X>\<C-U>\<C-Y>"
306 set completefunc&
307
Bram Moolenaar08928322020-01-04 14:32:48 +0100308 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100309 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100310
311 let s:called_completedone = 0
312 au! CompleteDone
313endfunc
314
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100315func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100316 if a:findstart
317 return 0
318 endif
319
320 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100321endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100322
Bram Moolenaar1e115362019-01-09 23:01:02 +0100323func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100324 call assert_equal( 'aword', v:completed_item[ 'word' ] )
325 call assert_equal( '', v:completed_item[ 'abbr' ] )
326 call assert_equal( '', v:completed_item[ 'menu' ] )
327 call assert_equal( '', v:completed_item[ 'info' ] )
328 call assert_equal( '', v:completed_item[ 'kind' ] )
329 call assert_equal( '', v:completed_item[ 'user_data' ] )
330
331 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100332endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100333
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100334func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100335 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
336
337 set completefunc=<SID>CompleteDone_CompleteFuncList
338 execute "normal a\<C-X>\<C-U>\<C-Y>"
339 set completefunc&
340
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100341 call assert_equal('', v:completed_item[ 'user_data' ])
342 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100343
344 let s:called_completedone = 0
345 au! CompleteDone
346endfunc
347
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200348func Test_CompleteDone_undo()
349 au CompleteDone * call append(0, "prepend1")
350 new
351 call setline(1, ["line1", "line2"])
352 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
353 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
354 \ getline(1, '$'))
355 undo
356 call assert_equal(["line1", "line2"], getline(1, '$'))
357 bwipe!
358 au! CompleteDone
359endfunc
360
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200361func CompleteTest(findstart, query)
362 if a:findstart
363 return col('.')
364 endif
365 return ['matched']
366endfunc
367
368func Test_completefunc_info()
369 new
370 set completeopt=menuone
371 set completefunc=CompleteTest
372 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100373 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 +0200374 bwipe!
375 set completeopt&
376 set completefunc&
377endfunc
378
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100379" Check that when using feedkeys() typeahead does not interrupt searching for
380" completions.
381func Test_compl_feedkeys()
382 new
383 set completeopt=menuone,noselect
384 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
385 call assert_equal("jump jump", getline(1))
386 bwipe!
387 set completeopt&
388endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200389
mityua1198122021-11-20 19:13:39 +0000390func s:ComplInCmdwin_GlobalCompletion(a, l, p)
391 return 'global'
392endfunc
393
394func s:ComplInCmdwin_LocalCompletion(a, l, p)
395 return 'local'
396endfunc
397
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200398func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100399 CheckFeature cmdwin
400
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200401 set wildmenu wildchar=<Tab>
402 com! -nargs=1 -complete=command GetInput let input = <q-args>
403 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100404 let w:test_winvar = 'winvar'
405 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200406
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100407 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200408 let input = ''
409 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
410 call assert_equal('TestCommand', input)
411
412 let input = ''
413 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
414 call assert_equal('T', input)
415
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100416
417 com! -nargs=1 -complete=var GetInput let input = <q-args>
418 " Window-local variables
419 let input = ''
420 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
421 call assert_equal('w:test_winvar', input)
422
423 let input = ''
424 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
425 call assert_equal('w:test_', input)
426
427 " Buffer-local variables
428 let input = ''
429 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
430 call assert_equal('b:test_bufvar', input)
431
432 let input = ''
433 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
434 call assert_equal('b:test_', input)
435
mityua1198122021-11-20 19:13:39 +0000436
437 " Argument completion of buffer-local command
438 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
439 return ['global']
440 endfunc
441
442 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
443 return ['local']
444 endfunc
445
446 func s:ComplInCmdwin_CheckCompletion(arg)
447 call assert_equal('local', a:arg)
448 endfunc
449
450 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
451 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
452 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
453 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
454 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
455
456 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
457 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
458 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
459 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
460
461 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
462
463 func! s:ComplInCmdwin_CheckCompletion(arg)
464 call assert_equal('global', a:arg)
465 endfunc
466 new
467 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
468 quit
469
470 delfunc s:ComplInCmdwin_GlobalCompletion
471 delfunc s:ComplInCmdwin_LocalCompletion
472 delfunc s:ComplInCmdwin_GlobalCompletionList
473 delfunc s:ComplInCmdwin_LocalCompletionList
474 delfunc s:ComplInCmdwin_CheckCompletion
475
476 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200477 delcom TestCommand
478 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100479 unlet w:test_winvar
480 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200481 set wildmenu& wildchar&
482endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200483
484" Test for insert path completion with completeslash option
485func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200486 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200487
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200488 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200489 let orig_shellslash = &shellslash
490 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200491 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200492
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200493 set noshellslash
494
495 set completeslash=
496 exe "normal oXd\<C-X>\<C-F>"
497 call assert_equal('Xdir\', getline('.'))
498
499 set completeslash=backslash
500 exe "normal oXd\<C-X>\<C-F>"
501 call assert_equal('Xdir\', getline('.'))
502
503 set completeslash=slash
504 exe "normal oXd\<C-X>\<C-F>"
505 call assert_equal('Xdir/', getline('.'))
506
507 set shellslash
508
509 set completeslash=
510 exe "normal oXd\<C-X>\<C-F>"
511 call assert_equal('Xdir/', getline('.'))
512
513 set completeslash=backslash
514 exe "normal oXd\<C-X>\<C-F>"
515 call assert_equal('Xdir\', getline('.'))
516
517 set completeslash=slash
518 exe "normal oXd\<C-X>\<C-F>"
519 call assert_equal('Xdir/', getline('.'))
520 %bw!
521 call delete('Xdir', 'rf')
522
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200523 set noshellslash
524 set completeslash=slash
525 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
526
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200527 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200528 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200529endfunc
530
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200531func Test_pum_stopped_by_timer()
532 CheckScreendump
533
534 let lines =<< trim END
535 call setline(1, ['hello', 'hullo', 'heeee', ''])
536 func StartCompl()
537 call timer_start(100, { -> execute('stopinsert') })
538 call feedkeys("Gah\<C-N>")
539 endfunc
540 END
541
542 call writefile(lines, 'Xpumscript')
543 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
544 call term_sendkeys(buf, ":call StartCompl()\<CR>")
545 call TermWait(buf, 200)
546 call term_sendkeys(buf, "k")
547 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
548
549 call StopVimInTerminal(buf)
550 call delete('Xpumscript')
551endfunc
552
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100553func Test_pum_with_folds_two_tabs()
554 CheckScreendump
555
556 let lines =<< trim END
557 set fdm=marker
558 call setline(1, ['" x {{{1', '" a some text'])
559 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
560 norm! zm
561 tab sp
562 call feedkeys('2Gzv', 'xt')
563 call feedkeys("0fa", 'xt')
564 END
565
566 call writefile(lines, 'Xpumscript')
567 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200568 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100569 call term_sendkeys(buf, "a\<C-N>")
570 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
571
572 call term_sendkeys(buf, "\<Esc>")
573 call StopVimInTerminal(buf)
574 call delete('Xpumscript')
575endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100576
577func Test_pum_with_preview_win()
578 CheckScreendump
579
580 let lines =<< trim END
581 funct Omni_test(findstart, base)
582 if a:findstart
583 return col(".") - 1
584 endif
585 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
586 endfunc
587 set omnifunc=Omni_test
588 set completeopt+=longest
589 END
590
591 call writefile(lines, 'Xpreviewscript')
592 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200593 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100594 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200595 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100596 call term_sendkeys(buf, "\<C-N>")
597 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
598
599 call term_sendkeys(buf, "\<Esc>")
600 call StopVimInTerminal(buf)
601 call delete('Xpreviewscript')
602endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100603
604" Test for inserting the tag search pattern in insert mode
605func Test_ins_compl_tag_sft()
606 call writefile([
607 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
608 \ "first\tXfoo\t/^int first() {}$/",
609 \ "second\tXfoo\t/^int second() {}$/",
610 \ "third\tXfoo\t/^int third() {}$/"],
611 \ 'Xtags')
612 set tags=Xtags
613 let code =<< trim [CODE]
614 int first() {}
615 int second() {}
616 int third() {}
617 [CODE]
618 call writefile(code, 'Xfoo')
619
620 enew
621 set showfulltag
622 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
623 call assert_equal('int second() {}', getline(1))
624 set noshowfulltag
625
626 call delete('Xtags')
627 call delete('Xfoo')
628 set tags&
629 %bwipe!
630endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200631
632" Test for 'completefunc' deleting text
633func Test_completefunc_error()
634 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200635 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200636 func CompleteFunc(findstart, base)
637 if a:findstart == 1
638 normal dd
639 return col('.') - 1
640 endif
641 return ['a', 'b']
642 endfunc
643 set completefunc=CompleteFunc
644 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100645 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200646
647 " delete text when called for the second time
648 func CompleteFunc2(findstart, base)
649 if a:findstart == 1
650 return col('.') - 1
651 endif
652 normal dd
653 return ['a', 'b']
654 endfunc
655 set completefunc=CompleteFunc2
656 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200657 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200658
Bram Moolenaar97202d92021-01-28 18:34:35 +0100659 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100660 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100661 if a:findstart == 1
662 return col('.') - 1
663 endif
664 wincmd p
665 return ['a', 'b']
666 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100667 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100668 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100669 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100670 close!
671
672 set completefunc&
673 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100674 delfunc CompleteFunc2
675 delfunc CompleteFunc3
676 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100677endfunc
678
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200679" Test for returning non-string values from 'completefunc'
680func Test_completefunc_invalid_data()
681 new
682 func! CompleteFunc(findstart, base)
683 if a:findstart == 1
684 return col('.') - 1
685 endif
686 return [{}, '', 'moon']
687 endfunc
688 set completefunc=CompleteFunc
689 exe "normal i\<C-X>\<C-U>"
690 call assert_equal('moon', getline(1))
691 set completefunc&
692 close!
693endfunc
694
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200695" Test for errors in using complete() function
696func Test_complete_func_error()
697 call assert_fails('call complete(1, ["a"])', 'E785:')
698 func ListColors()
699 call complete(col('.'), "blue")
700 endfunc
701 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
702 func ListMonths()
703 call complete(col('.'), test_null_list())
704 endfunc
705 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
706 delfunc ListColors
707 delfunc ListMonths
708 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200709 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200710endfunc
711
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200712" Test for completing words following a completed word in a line
713func Test_complete_wrapscan()
714 " complete words from another buffer
715 new
716 call setline(1, ['one two', 'three four'])
717 new
718 setlocal complete=w
719 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
720 call assert_equal('two three four', getline(1))
721 close!
722 " complete words from the current buffer
723 setlocal complete=.
724 %d
725 call setline(1, ['one two', ''])
726 call cursor(2, 1)
727 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
728 call assert_equal('one two one two', getline(2))
729 close!
730endfunc
731
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200732" Test for completing special characters
733func Test_complete_special_chars()
734 new
735 call setline(1, 'int .*[-\^$ func float')
736 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
737 call assert_equal('int .*[-\^$ func float', getline(2))
738 close!
739endfunc
740
741" Test for completion when text is wrapped across lines.
742func Test_complete_across_line()
743 new
744 call setline(1, ['red green blue', 'one two three'])
745 setlocal textwidth=20
746 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
747 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
748 close!
749endfunc
750
751" Test for using CTRL-L to add one character when completing matching
752func Test_complete_add_onechar()
753 new
754 call setline(1, ['wool', 'woodwork'])
755 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
756 call assert_equal('woof', getline(3))
757
758 " use 'ignorecase' and backspace to erase characters from the prefix string
759 " and then add letters using CTRL-L
760 %d
761 set ignorecase backspace=2
762 setlocal complete=.
763 call setline(1, ['workhorse', 'workload'])
764 normal Go
765 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
766 call assert_equal('workh', getline(3))
767 set ignorecase& backspace&
768 close!
769endfunc
770
771" Test insert completion with 'cindent' (adjust the indent)
772func Test_complete_with_cindent()
773 new
774 setlocal cindent
775 call setline(1, ['if (i == 1)', " j = 2;"])
776 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
777 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
778
779 %d
780 call setline(1, ['when while', '{', ''])
781 setlocal cinkeys+==while
782 exe "normal Giwh\<C-P> "
783 call assert_equal("\twhile ", getline('$'))
784 close!
785endfunc
786
787" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
788func Test_complete_cmdline()
789 new
790 exe "normal icaddb\<C-X>\<C-V>"
791 call assert_equal('caddbuffer', getline(1))
792 exe "normal ocall getqf\<C-X>\<C-V>"
793 call assert_equal('call getqflist(', getline(2))
794 exe "normal oabcxyz(\<C-X>\<C-V>"
795 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200796 com! -buffer TestCommand1 echo 'TestCommand1'
797 com! -buffer TestCommand2 echo 'TestCommand2'
798 write TestCommand1Test
799 write TestCommand2Test
800 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
801 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
802 call assert_equal('TestCommand2Test', getline(4))
803 call delete('TestCommand1Test')
804 call delete('TestCommand2Test')
805 delcom TestCommand1
806 delcom TestCommand2
807 close!
808endfunc
809
810" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
811func Test_complete_stop()
812 new
813 func Save_mode1()
814 let g:mode1 = mode(1)
815 return ''
816 endfunc
817 func Save_mode2()
818 let g:mode2 = mode(1)
819 return ''
820 endfunc
821 inoremap <F1> <C-R>=Save_mode1()<CR>
822 inoremap <F2> <C-R>=Save_mode2()<CR>
823 call setline(1, ['aaa bbb ccc '])
824 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
825 call assert_equal('ic', g:mode1)
826 call assert_equal('i', g:mode2)
827 call assert_equal('aaa bbb ccc ', getline(1))
828 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
829 call assert_equal('ic', g:mode1)
830 call assert_equal('i', g:mode2)
831 call assert_equal('aaa bbb ccc aaa', getline(1))
832 set completeopt+=noselect
833 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
834 call assert_equal('ic', g:mode1)
835 call assert_equal('i', g:mode2)
836 call assert_equal('aaa bbb ccc aaa bb', getline(1))
837 set completeopt&
838 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
839 call assert_equal('ic', g:mode1)
840 call assert_equal('i', g:mode2)
841 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
842 com! -buffer TestCommand1 echo 'TestCommand1'
843 com! -buffer TestCommand2 echo 'TestCommand2'
844 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
845 call assert_equal('ic', g:mode1)
846 call assert_equal('i', g:mode2)
847 call assert_equal('TestCommand2', getline(2))
848 delcom TestCommand1
849 delcom TestCommand2
850 unlet g:mode1
851 unlet g:mode2
852 iunmap <F1>
853 iunmap <F2>
854 delfunc Save_mode1
855 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200856 close!
857endfunc
858
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200859func Test_issue_7021()
860 CheckMSWindows
861
862 let orig_shellslash = &shellslash
863 set noshellslash
864
865 set completeslash=slash
866 call assert_false(expand('~') =~ '/')
867
868 let &shellslash = orig_shellslash
869 set completeslash=
870endfunc
871
Bram Moolenaarcc233582020-12-12 13:32:07 +0100872" Test to ensure 'Scanning...' messages are not recorded in messages history
873func Test_z1_complete_no_history()
874 new
875 messages clear
876 let currmess = execute('messages')
877 setlocal dictionary=README.txt
878 exe "normal owh\<C-X>\<C-K>"
879 exe "normal owh\<C-N>"
880 call assert_equal(currmess, execute('messages'))
881 close!
882endfunc
883
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000884" Test for different ways of setting the 'completefunc' option
885func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000886 func CompleteFunc1(callnr, findstart, base)
887 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
888 return a:findstart ? 0 : []
889 endfunc
890 func CompleteFunc2(findstart, base)
891 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000892 return a:findstart ? 0 : []
893 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000894
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000895 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000896 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000897 LET &completefunc = 'g:CompleteFunc2'
898 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000899 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000900 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000901 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000902 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000903 bw!
904
905 #" Test for using a function()
906 set completefunc=function('g:CompleteFunc1',\ [10])
907 new
908 call setline(1, 'one')
909 LET g:CompleteFunc1Args = []
910 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
911 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000912 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000913
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000914 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000915 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000916 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000917 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000918 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000919 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000920 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000921 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000922 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000923
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000924 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000925 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000926 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000927 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000928 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000929 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000930 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000931 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000932 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000933
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000934 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000935 set completefunc=funcref('g:CompleteFunc1',\ [13])
936 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000937 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000938 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000939 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000940 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000941 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000942
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000943 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000944 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000945 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000946 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000947 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000948 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000949 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000950 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000951 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000952
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000953 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000954 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000955 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000956 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000957 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000958 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000959 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000960 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000961 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000962
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000963 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000964 VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000965 LET optval = substitute(optval, ' ', '\\ ', 'g')
966 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000967 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000968 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000969 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000970 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000971 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000972 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000973
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000974 #" Set 'completefunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000975 LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND
976 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000977 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000978 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000979 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000980 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000981 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000982
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000983 #" Set 'completefunc' to string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000984 LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND'
985 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000986 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000987 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000988 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000989 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000990 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000991
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000992 #" Set 'completefunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000993 VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000994 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000995 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000996 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000997 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000998 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000999 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001000 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001001
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001002 #" Set 'completefunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001003 LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001004 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001005 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001006 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001007 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001008 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001009 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001010 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001011
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001012 #" Test for using a lambda function with incorrect return value
1013 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1014 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001015 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001016 call setline(1, 'eight')
1017 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1018 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001019
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001020 #" Test for clearing the 'completefunc' option
1021 set completefunc=''
1022 set completefunc&
1023 call assert_fails("set completefunc=function('abc')", "E700:")
1024 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001025
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001026 #" set 'completefunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001027 set completefunc=CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001028 call setline(1, 'five')
1029 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1030 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001031 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001032 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001033 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001034 bw!
1035 END
1036 call CheckLegacyAndVim9Success(lines)
1037
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001038 " Test for using a script-local function name
1039 func s:CompleteFunc3(findstart, base)
1040 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1041 return a:findstart ? 0 : []
1042 endfunc
1043 set completefunc=s:CompleteFunc3
1044 new
1045 call setline(1, 'script1')
1046 let g:CompleteFunc3Args = []
1047 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1048 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1049 bw!
1050
1051 let &completefunc = 's:CompleteFunc3'
1052 new
1053 call setline(1, 'script2')
1054 let g:CompleteFunc3Args = []
1055 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1056 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1057 bw!
1058 delfunc s:CompleteFunc3
1059
1060 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001061 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001062 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1063
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001064 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001065 set completefunc=(a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001066 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001067 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001068 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001069 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001070
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001071 " set 'completefunc' to a partial with dict. This used to cause a crash.
1072 func SetCompleteFunc()
1073 let params = {'complete': function('g:DictCompleteFunc')}
1074 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001075 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001076 func g:DictCompleteFunc(_) dict
1077 endfunc
1078 call SetCompleteFunc()
1079 new
1080 call SetCompleteFunc()
1081 bw
1082 call test_garbagecollect_now()
1083 new
1084 set completefunc=
1085 wincmd w
1086 set completefunc=
1087 %bw!
1088 delfunc g:DictCompleteFunc
1089 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001090
1091 " Vim9 tests
1092 let lines =<< trim END
1093 vim9script
1094
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001095 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1096 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001097 return findstart ? 0 : []
1098 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001099
1100 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001101 set completefunc=function('Vim9CompleteFunc',\ [60])
1102 new | only
1103 setline(1, 'one')
1104 g:Vim9completeFuncArgs = []
1105 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1106 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1107 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001108
1109 # Test for using a global function name
1110 &completefunc = g:CompleteFunc2
1111 new | only
1112 setline(1, 'two')
1113 g:CompleteFunc2Args = []
1114 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1115 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1116 bw!
1117
1118 # Test for using a script-local function name
1119 def s:LocalCompleteFunc(findstart: number, base: string): any
1120 add(g:LocalCompleteFuncArgs, [findstart, base])
1121 return findstart ? 0 : []
1122 enddef
1123 &completefunc = s:LocalCompleteFunc
1124 new | only
1125 setline(1, 'three')
1126 g:LocalCompleteFuncArgs = []
1127 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1128 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1129 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001130 END
1131 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001132
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001133 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001134 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001135 delfunc CompleteFunc1
1136 delfunc CompleteFunc2
1137 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001138 %bw!
1139endfunc
1140
1141" Test for different ways of setting the 'omnifunc' option
1142func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001143 func OmniFunc1(callnr, findstart, base)
1144 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1145 return a:findstart ? 0 : []
1146 endfunc
1147 func OmniFunc2(findstart, base)
1148 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001149 return a:findstart ? 0 : []
1150 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001151
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001152 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001153 #" Test for using a function name
1154 LET &omnifunc = 'g:OmniFunc2'
1155 new
1156 call setline(1, 'zero')
1157 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001158 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001159 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1160 bw!
1161
1162 #" Test for using a function()
1163 set omnifunc=function('g:OmniFunc1',\ [10])
1164 new
1165 call setline(1, 'one')
1166 LET g:OmniFunc1Args = []
1167 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1168 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001169 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001170
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001171 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001172 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001173 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001174 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001175 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001176 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001177 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001178 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001179 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001180
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001181 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001182 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001183 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001184 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001185 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001186 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001187 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001188 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001189 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001190
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001191 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001192 set omnifunc=funcref('g:OmniFunc1',\ [13])
1193 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001194 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001195 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001196 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001197 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001198 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001199
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001200 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001201 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001202 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001203 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001204 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001205 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001206 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001207 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001208 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001209
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001210 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001211 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001212 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001213 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001214 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001215 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001216 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001217 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001218 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001219
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001220 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001221 VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001222 LET optval = substitute(optval, ' ', '\\ ', 'g')
1223 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001224 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001225 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001226 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001227 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001228 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001229 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001230
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001231 #" Set 'omnifunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001232 LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND
1233 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001234 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001235 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001236 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001237 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001238 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001239
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001240 #" Set 'omnifunc' to a string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001241 LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND'
1242 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001243 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001244 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001245 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001246 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001247 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001248
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001249 #" Set 'omnifunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001250 VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001251 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001252 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001253 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001254 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001255 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001256 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001257 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001258
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001259 #" Set 'omnifunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001260 LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001261 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001262 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001263 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001264 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001265 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001266 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001267 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001268
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001269 #" Test for using a lambda function with incorrect return value
1270 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1271 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001272 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001273 call setline(1, 'eight')
1274 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1275 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001276
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001277 #" Test for clearing the 'omnifunc' option
1278 set omnifunc=''
1279 set omnifunc&
1280 call assert_fails("set omnifunc=function('abc')", "E700:")
1281 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001282
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001283 #" set 'omnifunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001284 set omnifunc=OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001285 call setline(1, 'nine')
1286 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1287 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001288 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001289 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001290 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001291 bw!
1292 END
1293 call CheckLegacyAndVim9Success(lines)
1294
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001295 " Test for using a script-local function name
1296 func s:OmniFunc3(findstart, base)
1297 call add(g:OmniFunc3Args, [a:findstart, a:base])
1298 return a:findstart ? 0 : []
1299 endfunc
1300 set omnifunc=s:OmniFunc3
1301 new
1302 call setline(1, 'script1')
1303 let g:OmniFunc3Args = []
1304 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1305 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1306 bw!
1307
1308 let &omnifunc = 's:OmniFunc3'
1309 new
1310 call setline(1, 'script2')
1311 let g:OmniFunc3Args = []
1312 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1313 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1314 bw!
1315 delfunc s:OmniFunc3
1316
1317 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001318 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001319 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1320
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001321 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001322 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001323 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001324 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001325 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001326 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001327
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1329 func SetOmniFunc()
1330 let params = {'omni': function('g:DictOmniFunc')}
1331 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001332 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001333 func g:DictOmniFunc(_) dict
1334 endfunc
1335 call SetOmniFunc()
1336 new
1337 call SetOmniFunc()
1338 bw
1339 call test_garbagecollect_now()
1340 new
1341 set omnifunc=
1342 wincmd w
1343 set omnifunc=
1344 %bw!
1345 delfunc g:DictOmniFunc
1346 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001347
1348 " Vim9 tests
1349 let lines =<< trim END
1350 vim9script
1351
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001352 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1353 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001354 return findstart ? 0 : []
1355 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001356
1357 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001358 set omnifunc=function('Vim9omniFunc',\ [60])
1359 new | only
1360 setline(1, 'one')
1361 g:Vim9omniFunc_Args = []
1362 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1363 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1364 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001365
1366 # Test for using a global function name
1367 &omnifunc = g:OmniFunc2
1368 new | only
1369 setline(1, 'two')
1370 g:OmniFunc2Args = []
1371 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1372 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1373 bw!
1374
1375 # Test for using a script-local function name
1376 def s:LocalOmniFunc(findstart: number, base: string): any
1377 add(g:LocalOmniFuncArgs, [findstart, base])
1378 return findstart ? 0 : []
1379 enddef
1380 &omnifunc = s:LocalOmniFunc
1381 new | only
1382 setline(1, 'three')
1383 g:LocalOmniFuncArgs = []
1384 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1385 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1386 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001387 END
1388 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001389
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001390 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001391 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001392 delfunc OmniFunc1
1393 delfunc OmniFunc2
1394 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001395 %bw!
1396endfunc
1397
1398" Test for different ways of setting the 'thesaurusfunc' option
1399func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001400 func TsrFunc1(callnr, findstart, base)
1401 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001402 return a:findstart ? 0 : []
1403 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001404 func TsrFunc2(findstart, base)
1405 call add(g:TsrFunc2Args, [a:findstart, a:base])
1406 return a:findstart ? 0 : ['sunday']
1407 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001408
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001409 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001410 #" Test for using a function name
1411 LET &thesaurusfunc = 'g:TsrFunc2'
1412 new
1413 call setline(1, 'zero')
1414 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001416 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1417 bw!
1418
1419 #" Test for using a function()
1420 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1421 new
1422 call setline(1, 'one')
1423 LET g:TsrFunc1Args = []
1424 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1425 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
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 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001429 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001430 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001431 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001433 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001434 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001435 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
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 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001439 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001440 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001441 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001442 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001443 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001444 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001445 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
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 funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001449 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1450 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001451 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001452 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001453 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001454 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001455 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001456
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001457 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001458 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001459 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001460 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001461 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001462 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001463 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001464 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001465 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001466
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001467 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001468 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001469 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001470 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001471 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001472 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001473 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001474 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001475 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001476
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001477 #" Test for using a lambda function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001478 VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001479 LET optval = substitute(optval, ' ', '\\ ', 'g')
1480 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001481 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001482 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001483 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001484 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001486 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001487
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001488 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001489 LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND
1490 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001491 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001492 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001493 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001494 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001495 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001496
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001497 #" Set 'thesaurusfunc' to a string(lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001498 LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND'
1499 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001500 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001501 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001502 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001503 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001504 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001505
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001506 #" Set 'thesaurusfunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001507 VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001508 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001509 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001510 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001511 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001512 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001513 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001514 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001515
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001516 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001517 LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001518 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001519 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001520 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001521 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001522 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001523 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001524 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001525
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001526 #" Test for using a lambda function with incorrect return value
1527 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1528 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001529 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001530 call setline(1, 'eight')
1531 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1532 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001533
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001534 #" Test for clearing the 'thesaurusfunc' option
1535 set thesaurusfunc=''
1536 set thesaurusfunc&
1537 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1538 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001539
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001540 #" set 'thesaurusfunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001541 set thesaurusfunc=TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001542 call setline(1, 'ten')
1543 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1544 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001545 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001546 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001547 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001548 bw!
1549
1550 #" Use a buffer-local value and a global value
1551 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001552 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001553 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001554 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001555 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1556 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001557 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001558 new
1559 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001560 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001561 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1562 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001563 call assert_equal([], g:TsrFunc1Args)
1564 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001565 wincmd w
1566 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001567 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001568 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1569 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001570 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001571 :%bw!
1572 END
1573 call CheckLegacyAndVim9Success(lines)
1574
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001575 " Test for using a script-local function name
1576 func s:TsrFunc3(findstart, base)
1577 call add(g:TsrFunc3Args, [a:findstart, a:base])
1578 return a:findstart ? 0 : []
1579 endfunc
1580 set tsrfu=s:TsrFunc3
1581 new
1582 call setline(1, 'script1')
1583 let g:TsrFunc3Args = []
1584 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1585 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1586 bw!
1587
1588 let &tsrfu = 's:TsrFunc3'
1589 new
1590 call setline(1, 'script2')
1591 let g:TsrFunc3Args = []
1592 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1593 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1594 bw!
1595 delfunc s:TsrFunc3
1596
1597 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001598 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001599 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1600
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001601 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001602 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001603 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001604 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001605 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001606 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001607 bw!
1608
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001609 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
1610 func SetTsrFunc()
1611 let params = {'thesaurus': function('g:DictTsrFunc')}
1612 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001613 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001614 func g:DictTsrFunc(_) dict
1615 endfunc
1616 call SetTsrFunc()
1617 new
1618 call SetTsrFunc()
1619 bw
1620 call test_garbagecollect_now()
1621 new
1622 set thesaurusfunc=
1623 wincmd w
1624 %bw!
1625 delfunc SetTsrFunc
1626
1627 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
1628 " cause a crash.
1629 func SetLocalTsrFunc()
1630 let params = {'thesaurus': function('g:DictTsrFunc')}
1631 let &l:thesaurusfunc = params.thesaurus
1632 endfunc
1633 call SetLocalTsrFunc()
1634 call test_garbagecollect_now()
1635 call SetLocalTsrFunc()
1636 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001637 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 delfunc g:DictTsrFunc
1639 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001640
1641 " Vim9 tests
1642 let lines =<< trim END
1643 vim9script
1644
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001645 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
1646 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001647 return findstart ? 0 : []
1648 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001649
1650 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001651 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
1652 new | only
1653 setline(1, 'one')
1654 g:Vim9tsrFunc_Args = []
1655 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1656 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
1657 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001658
1659 # Test for using a global function name
1660 &thesaurusfunc = g:TsrFunc2
1661 new | only
1662 setline(1, 'two')
1663 g:TsrFunc2Args = []
1664 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1665 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
1666 bw!
1667
1668 # Test for using a script-local function name
1669 def s:LocalTsrFunc(findstart: number, base: string): any
1670 add(g:LocalTsrFuncArgs, [findstart, base])
1671 return findstart ? 0 : []
1672 enddef
1673 &thesaurusfunc = s:LocalTsrFunc
1674 new | only
1675 setline(1, 'three')
1676 g:LocalTsrFuncArgs = []
1677 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1678 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
1679 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001680 END
1681 call CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001682
1683 " cleanup
1684 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001685 delfunc TsrFunc1
1686 delfunc TsrFunc2
1687 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001688 %bw!
1689endfunc
1690
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001691" vim: shiftwidth=2 sts=2 expandtab