blob: 638a774d303b2ccf59bc483ca8321fd711a15738 [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
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
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
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000751" Test for completing words with a '.' at the end of a word.
752func Test_complete_joinspaces()
753 new
754 call setline(1, ['one two.', 'three. four'])
755 set joinspaces
756 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
757 call assert_equal("one two. three. four", getline(3))
758 set joinspaces&
759 bw!
760endfunc
761
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200762" Test for using CTRL-L to add one character when completing matching
763func Test_complete_add_onechar()
764 new
765 call setline(1, ['wool', 'woodwork'])
766 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
767 call assert_equal('woof', getline(3))
768
769 " use 'ignorecase' and backspace to erase characters from the prefix string
770 " and then add letters using CTRL-L
771 %d
772 set ignorecase backspace=2
773 setlocal complete=.
774 call setline(1, ['workhorse', 'workload'])
775 normal Go
776 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
777 call assert_equal('workh', getline(3))
778 set ignorecase& backspace&
779 close!
780endfunc
781
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000782" Test for using CTRL-X CTRL-L to complete whole lines lines
783func Test_complete_wholeline()
784 new
785 " complete one-line
786 call setline(1, ['a1', 'a2'])
787 exe "normal ggoa\<C-X>\<C-L>"
788 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
789 " go to the next match (wrapping around the buffer)
790 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
791 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
792 " go to the next match
793 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
794 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
795 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
796 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
797 " repeat the test using CTRL-L
798 " go to the next match (wrapping around the buffer)
799 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
800 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
801 " go to the next match
802 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
803 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
804 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
805 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
806 %d
807 " use CTRL-X CTRL-L to add one more line
808 call setline(1, ['a1', 'b1'])
809 setlocal complete=.
810 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
811 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
812 bw!
813endfunc
814
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200815" Test insert completion with 'cindent' (adjust the indent)
816func Test_complete_with_cindent()
817 new
818 setlocal cindent
819 call setline(1, ['if (i == 1)', " j = 2;"])
820 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
821 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
822
823 %d
824 call setline(1, ['when while', '{', ''])
825 setlocal cinkeys+==while
826 exe "normal Giwh\<C-P> "
827 call assert_equal("\twhile ", getline('$'))
828 close!
829endfunc
830
831" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
832func Test_complete_cmdline()
833 new
834 exe "normal icaddb\<C-X>\<C-V>"
835 call assert_equal('caddbuffer', getline(1))
836 exe "normal ocall getqf\<C-X>\<C-V>"
837 call assert_equal('call getqflist(', getline(2))
838 exe "normal oabcxyz(\<C-X>\<C-V>"
839 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200840 com! -buffer TestCommand1 echo 'TestCommand1'
841 com! -buffer TestCommand2 echo 'TestCommand2'
842 write TestCommand1Test
843 write TestCommand2Test
844 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
845 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
846 call assert_equal('TestCommand2Test', getline(4))
847 call delete('TestCommand1Test')
848 call delete('TestCommand2Test')
849 delcom TestCommand1
850 delcom TestCommand2
851 close!
852endfunc
853
854" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
855func Test_complete_stop()
856 new
857 func Save_mode1()
858 let g:mode1 = mode(1)
859 return ''
860 endfunc
861 func Save_mode2()
862 let g:mode2 = mode(1)
863 return ''
864 endfunc
865 inoremap <F1> <C-R>=Save_mode1()<CR>
866 inoremap <F2> <C-R>=Save_mode2()<CR>
867 call setline(1, ['aaa bbb ccc '])
868 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
869 call assert_equal('ic', g:mode1)
870 call assert_equal('i', g:mode2)
871 call assert_equal('aaa bbb ccc ', getline(1))
872 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
873 call assert_equal('ic', g:mode1)
874 call assert_equal('i', g:mode2)
875 call assert_equal('aaa bbb ccc aaa', getline(1))
876 set completeopt+=noselect
877 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
878 call assert_equal('ic', g:mode1)
879 call assert_equal('i', g:mode2)
880 call assert_equal('aaa bbb ccc aaa bb', getline(1))
881 set completeopt&
882 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
883 call assert_equal('ic', g:mode1)
884 call assert_equal('i', g:mode2)
885 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
886 com! -buffer TestCommand1 echo 'TestCommand1'
887 com! -buffer TestCommand2 echo 'TestCommand2'
888 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
889 call assert_equal('ic', g:mode1)
890 call assert_equal('i', g:mode2)
891 call assert_equal('TestCommand2', getline(2))
892 delcom TestCommand1
893 delcom TestCommand2
894 unlet g:mode1
895 unlet g:mode2
896 iunmap <F1>
897 iunmap <F2>
898 delfunc Save_mode1
899 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200900 close!
901endfunc
902
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200903func Test_issue_7021()
904 CheckMSWindows
905
906 let orig_shellslash = &shellslash
907 set noshellslash
908
909 set completeslash=slash
910 call assert_false(expand('~') =~ '/')
911
912 let &shellslash = orig_shellslash
913 set completeslash=
914endfunc
915
Bram Moolenaarcc233582020-12-12 13:32:07 +0100916" Test to ensure 'Scanning...' messages are not recorded in messages history
917func Test_z1_complete_no_history()
918 new
919 messages clear
920 let currmess = execute('messages')
921 setlocal dictionary=README.txt
922 exe "normal owh\<C-X>\<C-K>"
923 exe "normal owh\<C-N>"
924 call assert_equal(currmess, execute('messages'))
925 close!
926endfunc
927
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000928" Test for different ways of setting the 'completefunc' option
929func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000930 func CompleteFunc1(callnr, findstart, base)
931 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
932 return a:findstart ? 0 : []
933 endfunc
934 func CompleteFunc2(findstart, base)
935 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000936 return a:findstart ? 0 : []
937 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000938
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000939 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000940 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000941 LET &completefunc = 'g:CompleteFunc2'
942 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000943 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000944 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000945 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000946 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000947 bw!
948
949 #" Test for using a function()
950 set completefunc=function('g:CompleteFunc1',\ [10])
951 new
952 call setline(1, 'one')
953 LET g:CompleteFunc1Args = []
954 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
955 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000956 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000957
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000958 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000959 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000960 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000961 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000962 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000963 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000964 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000965 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000966 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000967
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000968 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000969 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000970 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000971 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000972 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000973 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000974 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000975 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000976 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000977
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000978 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000979 set completefunc=funcref('g:CompleteFunc1',\ [13])
980 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000981 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000982 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000983 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000984 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000985 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000986
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000987 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000988 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000989 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000990 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000991 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000992 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000993 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000994 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000995 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000996
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000997 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000998 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000999 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001000 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001001 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001002 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001003 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001004 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001005 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001006
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001007 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001008 VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001009 LET optval = substitute(optval, ' ', '\\ ', 'g')
1010 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001011 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001012 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001013 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001014 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001015 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001016 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001017
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001018 #" Set 'completefunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001019 LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND
1020 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001021 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001022 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001023 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001024 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001025 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001026
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001027 #" Set 'completefunc' to string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001028 LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND'
1029 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001030 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001031 LET g:CompleteFunc1Args = []
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([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001034 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001035
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001036 #" Set 'completefunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001037 VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001038 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001039 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001040 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001041 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001042 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001043 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001044 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001045
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001046 #" Set 'completefunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001047 LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001048 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001049 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001050 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001051 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001052 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001053 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001054 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001055
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001056 #" Test for using a lambda function with incorrect return value
1057 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1058 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001059 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001060 call setline(1, 'eight')
1061 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1062 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001063
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001064 #" Test for clearing the 'completefunc' option
1065 set completefunc=''
1066 set completefunc&
1067 call assert_fails("set completefunc=function('abc')", "E700:")
1068 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001069
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001070 #" set 'completefunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001071 set completefunc=CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001072 call setline(1, 'five')
1073 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1074 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001075 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001076 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001077 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001078 bw!
1079 END
1080 call CheckLegacyAndVim9Success(lines)
1081
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001082 " Test for using a script-local function name
1083 func s:CompleteFunc3(findstart, base)
1084 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1085 return a:findstart ? 0 : []
1086 endfunc
1087 set completefunc=s:CompleteFunc3
1088 new
1089 call setline(1, 'script1')
1090 let g:CompleteFunc3Args = []
1091 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1092 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1093 bw!
1094
1095 let &completefunc = 's:CompleteFunc3'
1096 new
1097 call setline(1, 'script2')
1098 let g:CompleteFunc3Args = []
1099 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1100 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1101 bw!
1102 delfunc s:CompleteFunc3
1103
1104 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001105 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001106 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1107
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001108 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001109 set completefunc=(a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001110 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001111 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001112 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001113 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001114
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001115 " set 'completefunc' to a partial with dict. This used to cause a crash.
1116 func SetCompleteFunc()
1117 let params = {'complete': function('g:DictCompleteFunc')}
1118 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001119 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001120 func g:DictCompleteFunc(_) dict
1121 endfunc
1122 call SetCompleteFunc()
1123 new
1124 call SetCompleteFunc()
1125 bw
1126 call test_garbagecollect_now()
1127 new
1128 set completefunc=
1129 wincmd w
1130 set completefunc=
1131 %bw!
1132 delfunc g:DictCompleteFunc
1133 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001134
1135 " Vim9 tests
1136 let lines =<< trim END
1137 vim9script
1138
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001139 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1140 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001141 return findstart ? 0 : []
1142 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001143
1144 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001145 set completefunc=function('Vim9CompleteFunc',\ [60])
1146 new | only
1147 setline(1, 'one')
1148 g:Vim9completeFuncArgs = []
1149 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1150 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1151 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001152
1153 # Test for using a global function name
1154 &completefunc = g:CompleteFunc2
1155 new | only
1156 setline(1, 'two')
1157 g:CompleteFunc2Args = []
1158 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1159 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1160 bw!
1161
1162 # Test for using a script-local function name
1163 def s:LocalCompleteFunc(findstart: number, base: string): any
1164 add(g:LocalCompleteFuncArgs, [findstart, base])
1165 return findstart ? 0 : []
1166 enddef
1167 &completefunc = s:LocalCompleteFunc
1168 new | only
1169 setline(1, 'three')
1170 g:LocalCompleteFuncArgs = []
1171 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1172 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1173 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001174 END
1175 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001176
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001177 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001178 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001179 delfunc CompleteFunc1
1180 delfunc CompleteFunc2
1181 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001182 %bw!
1183endfunc
1184
1185" Test for different ways of setting the 'omnifunc' option
1186func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001187 func OmniFunc1(callnr, findstart, base)
1188 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1189 return a:findstart ? 0 : []
1190 endfunc
1191 func OmniFunc2(findstart, base)
1192 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001193 return a:findstart ? 0 : []
1194 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001195
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001196 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001197 #" Test for using a function name
1198 LET &omnifunc = 'g:OmniFunc2'
1199 new
1200 call setline(1, 'zero')
1201 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001202 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001203 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1204 bw!
1205
1206 #" Test for using a function()
1207 set omnifunc=function('g:OmniFunc1',\ [10])
1208 new
1209 call setline(1, 'one')
1210 LET g:OmniFunc1Args = []
1211 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1212 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001213 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001214
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001215 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001216 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001217 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001218 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001219 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001220 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001221 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001222 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001223 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001224
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001225 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001226 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001227 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001228 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001229 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001230 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001231 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001232 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001233 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001234
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001235 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001236 set omnifunc=funcref('g:OmniFunc1',\ [13])
1237 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001238 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001239 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001240 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001241 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001242 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001243
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001244 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001245 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001246 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001247 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001248 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001249 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001250 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001251 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001252 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001253
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001254 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001255 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001256 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001257 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001258 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001259 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001260 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001261 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001262 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001263
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001264 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001265 VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001266 LET optval = substitute(optval, ' ', '\\ ', 'g')
1267 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001268 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001269 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001270 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001271 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001272 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001273 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001274
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001275 #" Set 'omnifunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001276 LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND
1277 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001278 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001279 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001280 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001281 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001282 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001283
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001284 #" Set 'omnifunc' to a string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001285 LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND'
1286 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001287 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001288 LET g:OmniFunc1Args = []
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([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001291 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001292
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001293 #" Set 'omnifunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001294 VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001295 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001296 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001297 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001298 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001299 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001300 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001301 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001302
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001303 #" Set 'omnifunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001304 LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001305 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001306 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001307 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001308 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001309 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001310 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001311 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001312
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001313 #" Test for using a lambda function with incorrect return value
1314 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1315 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001316 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001317 call setline(1, 'eight')
1318 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1319 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001320
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001321 #" Test for clearing the 'omnifunc' option
1322 set omnifunc=''
1323 set omnifunc&
1324 call assert_fails("set omnifunc=function('abc')", "E700:")
1325 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001326
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001327 #" set 'omnifunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001328 set omnifunc=OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001329 call setline(1, 'nine')
1330 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1331 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001332 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001333 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001334 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001335 bw!
1336 END
1337 call CheckLegacyAndVim9Success(lines)
1338
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001339 " Test for using a script-local function name
1340 func s:OmniFunc3(findstart, base)
1341 call add(g:OmniFunc3Args, [a:findstart, a:base])
1342 return a:findstart ? 0 : []
1343 endfunc
1344 set omnifunc=s:OmniFunc3
1345 new
1346 call setline(1, 'script1')
1347 let g:OmniFunc3Args = []
1348 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1349 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1350 bw!
1351
1352 let &omnifunc = 's:OmniFunc3'
1353 new
1354 call setline(1, 'script2')
1355 let g:OmniFunc3Args = []
1356 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1357 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1358 bw!
1359 delfunc s:OmniFunc3
1360
1361 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001362 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001363 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1364
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001365 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001366 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001367 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001368 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001369 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001370 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001371
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001372 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1373 func SetOmniFunc()
1374 let params = {'omni': function('g:DictOmniFunc')}
1375 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001376 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 func g:DictOmniFunc(_) dict
1378 endfunc
1379 call SetOmniFunc()
1380 new
1381 call SetOmniFunc()
1382 bw
1383 call test_garbagecollect_now()
1384 new
1385 set omnifunc=
1386 wincmd w
1387 set omnifunc=
1388 %bw!
1389 delfunc g:DictOmniFunc
1390 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001391
1392 " Vim9 tests
1393 let lines =<< trim END
1394 vim9script
1395
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001396 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1397 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001398 return findstart ? 0 : []
1399 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001400
1401 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001402 set omnifunc=function('Vim9omniFunc',\ [60])
1403 new | only
1404 setline(1, 'one')
1405 g:Vim9omniFunc_Args = []
1406 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1407 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1408 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001409
1410 # Test for using a global function name
1411 &omnifunc = g:OmniFunc2
1412 new | only
1413 setline(1, 'two')
1414 g:OmniFunc2Args = []
1415 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1416 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1417 bw!
1418
1419 # Test for using a script-local function name
1420 def s:LocalOmniFunc(findstart: number, base: string): any
1421 add(g:LocalOmniFuncArgs, [findstart, base])
1422 return findstart ? 0 : []
1423 enddef
1424 &omnifunc = s:LocalOmniFunc
1425 new | only
1426 setline(1, 'three')
1427 g:LocalOmniFuncArgs = []
1428 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1429 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1430 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001431 END
1432 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001433
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001434 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001435 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001436 delfunc OmniFunc1
1437 delfunc OmniFunc2
1438 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001439 %bw!
1440endfunc
1441
1442" Test for different ways of setting the 'thesaurusfunc' option
1443func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001444 func TsrFunc1(callnr, findstart, base)
1445 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001446 return a:findstart ? 0 : []
1447 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001448 func TsrFunc2(findstart, base)
1449 call add(g:TsrFunc2Args, [a:findstart, a:base])
1450 return a:findstart ? 0 : ['sunday']
1451 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001452
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001453 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001454 #" Test for using a function name
1455 LET &thesaurusfunc = 'g:TsrFunc2'
1456 new
1457 call setline(1, 'zero')
1458 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001459 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001460 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1461 bw!
1462
1463 #" Test for using a function()
1464 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1465 new
1466 call setline(1, 'one')
1467 LET g:TsrFunc1Args = []
1468 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1469 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001470 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001471
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001472 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001473 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001474 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001475 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001476 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001477 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001478 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001479 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001480 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001481
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001482 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001483 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001484 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001486 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001487 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001488 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001489 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001490 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001491
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001492 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001493 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1494 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001495 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001496 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001497 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001498 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001499 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001500
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001501 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001502 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001503 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001504 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001505 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001506 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001507 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001508 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001509 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001510
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001511 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001512 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001513 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001514 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001515 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001516 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001517 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001518 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001519 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001520
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001521 #" Test for using a lambda function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001522 VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001523 LET optval = substitute(optval, ' ', '\\ ', 'g')
1524 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001525 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001526 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001527 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001528 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001529 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001530 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001531
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001532 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001533 LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND
1534 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001535 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001536 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001537 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001538 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001539 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001540
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001541 #" Set 'thesaurusfunc' to a string(lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001542 LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND'
1543 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001544 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001545 LET g:TsrFunc1Args = []
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([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001548 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001549
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001550 #" Set 'thesaurusfunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001551 VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001552 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001553 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001554 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001555 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001556 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001557 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001558 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001559
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001560 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001561 LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001562 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001563 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001564 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001565 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001566 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001567 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001568 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001569
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001570 #" Test for using a lambda function with incorrect return value
1571 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1572 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001573 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001574 call setline(1, 'eight')
1575 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1576 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001577
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001578 #" Test for clearing the 'thesaurusfunc' option
1579 set thesaurusfunc=''
1580 set thesaurusfunc&
1581 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1582 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001583
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001584 #" set 'thesaurusfunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001585 set thesaurusfunc=TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001586 call setline(1, 'ten')
1587 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1588 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001589 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001590 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001591 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001592 bw!
1593
1594 #" Use a buffer-local value and a global value
1595 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001596 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001598 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001599 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1600 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001601 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 new
1603 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001604 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1606 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001607 call assert_equal([], g:TsrFunc1Args)
1608 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001609 wincmd w
1610 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001611 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001612 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1613 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001614 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001615 :%bw!
1616 END
1617 call CheckLegacyAndVim9Success(lines)
1618
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001619 " Test for using a script-local function name
1620 func s:TsrFunc3(findstart, base)
1621 call add(g:TsrFunc3Args, [a:findstart, a:base])
1622 return a:findstart ? 0 : []
1623 endfunc
1624 set tsrfu=s:TsrFunc3
1625 new
1626 call setline(1, 'script1')
1627 let g:TsrFunc3Args = []
1628 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1629 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1630 bw!
1631
1632 let &tsrfu = 's:TsrFunc3'
1633 new
1634 call setline(1, 'script2')
1635 let g:TsrFunc3Args = []
1636 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1637 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1638 bw!
1639 delfunc s:TsrFunc3
1640
1641 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001642 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001643 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1644
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001645 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001646 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001647 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001649 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001651 bw!
1652
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
1654 func SetTsrFunc()
1655 let params = {'thesaurus': function('g:DictTsrFunc')}
1656 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001657 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 func g:DictTsrFunc(_) dict
1659 endfunc
1660 call SetTsrFunc()
1661 new
1662 call SetTsrFunc()
1663 bw
1664 call test_garbagecollect_now()
1665 new
1666 set thesaurusfunc=
1667 wincmd w
1668 %bw!
1669 delfunc SetTsrFunc
1670
1671 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
1672 " cause a crash.
1673 func SetLocalTsrFunc()
1674 let params = {'thesaurus': function('g:DictTsrFunc')}
1675 let &l:thesaurusfunc = params.thesaurus
1676 endfunc
1677 call SetLocalTsrFunc()
1678 call test_garbagecollect_now()
1679 call SetLocalTsrFunc()
1680 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001681 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 delfunc g:DictTsrFunc
1683 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001684
1685 " Vim9 tests
1686 let lines =<< trim END
1687 vim9script
1688
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001689 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
1690 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001691 return findstart ? 0 : []
1692 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001693
1694 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001695 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
1696 new | only
1697 setline(1, 'one')
1698 g:Vim9tsrFunc_Args = []
1699 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1700 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
1701 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001702
1703 # Test for using a global function name
1704 &thesaurusfunc = g:TsrFunc2
1705 new | only
1706 setline(1, 'two')
1707 g:TsrFunc2Args = []
1708 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1709 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
1710 bw!
1711
1712 # Test for using a script-local function name
1713 def s:LocalTsrFunc(findstart: number, base: string): any
1714 add(g:LocalTsrFuncArgs, [findstart, base])
1715 return findstart ? 0 : []
1716 enddef
1717 &thesaurusfunc = s:LocalTsrFunc
1718 new | only
1719 setline(1, 'three')
1720 g:LocalTsrFuncArgs = []
1721 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1722 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
1723 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001724 END
1725 call CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001726
1727 " cleanup
1728 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001729 delfunc TsrFunc1
1730 delfunc TsrFunc2
1731 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001732 %bw!
1733endfunc
1734
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001735" vim: shiftwidth=2 sts=2 expandtab