blob: b0b97bbc223361d75f04c201daef399244b8b046 [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
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000903" Test for typing CTRL-R in insert completion mode to insert a register
904" content.
905func Test_complete_reginsert()
906 new
907 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
908
909 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
910 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
911 " inserted.
912 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
913 call assert_equal('a123', getline(5))
914 let @r = "\<C-P>\<C-P>"
915 exe "normal GCa\<C-P>\<C-R>r"
916 call assert_equal('a12', getline(5))
917 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
918 call assert_equal('a1234x', getline(5))
919 bw!
920endfunc
921
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200922func Test_issue_7021()
923 CheckMSWindows
924
925 let orig_shellslash = &shellslash
926 set noshellslash
927
928 set completeslash=slash
929 call assert_false(expand('~') =~ '/')
930
931 let &shellslash = orig_shellslash
932 set completeslash=
933endfunc
934
Bram Moolenaarcc233582020-12-12 13:32:07 +0100935" Test to ensure 'Scanning...' messages are not recorded in messages history
936func Test_z1_complete_no_history()
937 new
938 messages clear
939 let currmess = execute('messages')
940 setlocal dictionary=README.txt
941 exe "normal owh\<C-X>\<C-K>"
942 exe "normal owh\<C-N>"
943 call assert_equal(currmess, execute('messages'))
944 close!
945endfunc
946
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000947" Test for different ways of setting the 'completefunc' option
948func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000949 func CompleteFunc1(callnr, findstart, base)
950 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
951 return a:findstart ? 0 : []
952 endfunc
953 func CompleteFunc2(findstart, base)
954 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000955 return a:findstart ? 0 : []
956 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000957
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000958 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000959 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000960 LET &completefunc = 'g:CompleteFunc2'
961 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000962 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000963 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000964 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000965 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000966 bw!
967
968 #" Test for using a function()
969 set completefunc=function('g:CompleteFunc1',\ [10])
970 new
971 call setline(1, 'one')
972 LET g:CompleteFunc1Args = []
973 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
974 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000975 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000976
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000977 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000978 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000979 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000980 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000981 call setline(1, 'two')
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([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000985 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000986
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000987 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000988 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000989 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000990 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000991 call setline(1, 'two')
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([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000995 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000996
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000997 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000998 set completefunc=funcref('g:CompleteFunc1',\ [13])
999 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001000 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001001 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001002 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001003 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001004 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001005
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001006 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001007 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001008 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001009 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001010 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001011 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001012 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001013 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001014 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001015
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001016 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001017 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001018 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001019 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001020 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001021 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001022 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001023 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001024 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001025
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001026 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001027 VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001028 LET optval = substitute(optval, ' ', '\\ ', 'g')
1029 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001030 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001031 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001032 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001033 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001034 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001035 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001036
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001037 #" Set 'completefunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001038 LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND
1039 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001040 call setline(1, 'six')
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([[17, 1, ''], [17, 0, 'six']], 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 string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001047 LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND'
1048 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001049 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001050 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001051 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001052 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001053 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001054
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001055 #" Set 'completefunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001056 VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001057 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001058 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001059 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001060 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001061 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001062 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001063 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001064
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001065 #" Set 'completefunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001066 LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001067 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001068 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001069 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001070 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001071 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001072 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001073 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001074
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001075 #" Test for using a lambda function with incorrect return value
1076 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1077 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001078 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001079 call setline(1, 'eight')
1080 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1081 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001082
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001083 #" Test for clearing the 'completefunc' option
1084 set completefunc=''
1085 set completefunc&
1086 call assert_fails("set completefunc=function('abc')", "E700:")
1087 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001088
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001089 #" set 'completefunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001090 set completefunc=CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001091 call setline(1, 'five')
1092 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1093 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001094 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001095 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001096 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001097 bw!
1098 END
1099 call CheckLegacyAndVim9Success(lines)
1100
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001101 " Test for using a script-local function name
1102 func s:CompleteFunc3(findstart, base)
1103 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1104 return a:findstart ? 0 : []
1105 endfunc
1106 set completefunc=s:CompleteFunc3
1107 new
1108 call setline(1, 'script1')
1109 let g:CompleteFunc3Args = []
1110 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1111 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1112 bw!
1113
1114 let &completefunc = 's:CompleteFunc3'
1115 new
1116 call setline(1, 'script2')
1117 let g:CompleteFunc3Args = []
1118 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1119 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1120 bw!
1121 delfunc s:CompleteFunc3
1122
1123 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001124 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001125 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1126
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001127 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001128 set completefunc=(a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001129 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001130 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001131 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001132 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001133
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001134 " set 'completefunc' to a partial with dict. This used to cause a crash.
1135 func SetCompleteFunc()
1136 let params = {'complete': function('g:DictCompleteFunc')}
1137 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001138 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001139 func g:DictCompleteFunc(_) dict
1140 endfunc
1141 call SetCompleteFunc()
1142 new
1143 call SetCompleteFunc()
1144 bw
1145 call test_garbagecollect_now()
1146 new
1147 set completefunc=
1148 wincmd w
1149 set completefunc=
1150 %bw!
1151 delfunc g:DictCompleteFunc
1152 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001153
1154 " Vim9 tests
1155 let lines =<< trim END
1156 vim9script
1157
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001158 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1159 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001160 return findstart ? 0 : []
1161 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001162
1163 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001164 set completefunc=function('Vim9CompleteFunc',\ [60])
1165 new | only
1166 setline(1, 'one')
1167 g:Vim9completeFuncArgs = []
1168 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1169 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1170 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001171
1172 # Test for using a global function name
1173 &completefunc = g:CompleteFunc2
1174 new | only
1175 setline(1, 'two')
1176 g:CompleteFunc2Args = []
1177 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1178 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1179 bw!
1180
1181 # Test for using a script-local function name
1182 def s:LocalCompleteFunc(findstart: number, base: string): any
1183 add(g:LocalCompleteFuncArgs, [findstart, base])
1184 return findstart ? 0 : []
1185 enddef
1186 &completefunc = s:LocalCompleteFunc
1187 new | only
1188 setline(1, 'three')
1189 g:LocalCompleteFuncArgs = []
1190 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1191 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1192 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001193 END
1194 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001195
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001196 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001197 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001198 delfunc CompleteFunc1
1199 delfunc CompleteFunc2
1200 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001201 %bw!
1202endfunc
1203
1204" Test for different ways of setting the 'omnifunc' option
1205func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001206 func OmniFunc1(callnr, findstart, base)
1207 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1208 return a:findstart ? 0 : []
1209 endfunc
1210 func OmniFunc2(findstart, base)
1211 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001212 return a:findstart ? 0 : []
1213 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001214
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001215 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001216 #" Test for using a function name
1217 LET &omnifunc = 'g:OmniFunc2'
1218 new
1219 call setline(1, 'zero')
1220 LET g:OmniFunc2Args = []
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([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1223 bw!
1224
1225 #" Test for using a function()
1226 set omnifunc=function('g:OmniFunc1',\ [10])
1227 new
1228 call setline(1, 'one')
1229 LET g:OmniFunc1Args = []
1230 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1231 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001232 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001233
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001234 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001235 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001236 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001237 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001238 call setline(1, 'two')
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([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001242 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001243
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001244 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001245 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001246 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001247 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001248 call setline(1, 'two')
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([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001252 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001253
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001254 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001255 set omnifunc=funcref('g:OmniFunc1',\ [13])
1256 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001257 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001258 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001259 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001260 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001261 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001262
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001263 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001264 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001265 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001266 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001267 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001268 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001269 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001270 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001271 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001272
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001273 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001274 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001275 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001276 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001277 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001278 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001279 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001280 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001281 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001282
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001283 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001284 VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001285 LET optval = substitute(optval, ' ', '\\ ', 'g')
1286 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001287 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001288 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001289 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001290 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001291 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001292 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001293
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001294 #" Set 'omnifunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001295 LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND
1296 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001297 call setline(1, 'six')
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([[17, 1, ''], [17, 0, 'six']], 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(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001304 LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND'
1305 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001306 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001307 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001308 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001309 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001310 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001311
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001312 #" Set 'omnifunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001313 VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001314 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001315 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001316 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001317 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001318 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001319 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001320 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001321
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001322 #" Set 'omnifunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001323 LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001324 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001325 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001326 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001327 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001329 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001330 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001331
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001332 #" Test for using a lambda function with incorrect return value
1333 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1334 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 call setline(1, 'eight')
1337 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1338 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001339
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001340 #" Test for clearing the 'omnifunc' option
1341 set omnifunc=''
1342 set omnifunc&
1343 call assert_fails("set omnifunc=function('abc')", "E700:")
1344 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001345
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001346 #" set 'omnifunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001347 set omnifunc=OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 call setline(1, 'nine')
1349 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1350 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001353 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001354 bw!
1355 END
1356 call CheckLegacyAndVim9Success(lines)
1357
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001358 " Test for using a script-local function name
1359 func s:OmniFunc3(findstart, base)
1360 call add(g:OmniFunc3Args, [a:findstart, a:base])
1361 return a:findstart ? 0 : []
1362 endfunc
1363 set omnifunc=s:OmniFunc3
1364 new
1365 call setline(1, 'script1')
1366 let g:OmniFunc3Args = []
1367 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1368 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1369 bw!
1370
1371 let &omnifunc = 's:OmniFunc3'
1372 new
1373 call setline(1, 'script2')
1374 let g:OmniFunc3Args = []
1375 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1376 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1377 bw!
1378 delfunc s:OmniFunc3
1379
1380 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001381 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001382 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1383
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001384 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001385 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001386 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001387 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001388 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001389 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001390
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001391 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1392 func SetOmniFunc()
1393 let params = {'omni': function('g:DictOmniFunc')}
1394 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001395 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001396 func g:DictOmniFunc(_) dict
1397 endfunc
1398 call SetOmniFunc()
1399 new
1400 call SetOmniFunc()
1401 bw
1402 call test_garbagecollect_now()
1403 new
1404 set omnifunc=
1405 wincmd w
1406 set omnifunc=
1407 %bw!
1408 delfunc g:DictOmniFunc
1409 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001410
1411 " Vim9 tests
1412 let lines =<< trim END
1413 vim9script
1414
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001415 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1416 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001417 return findstart ? 0 : []
1418 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001419
1420 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001421 set omnifunc=function('Vim9omniFunc',\ [60])
1422 new | only
1423 setline(1, 'one')
1424 g:Vim9omniFunc_Args = []
1425 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1426 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1427 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001428
1429 # Test for using a global function name
1430 &omnifunc = g:OmniFunc2
1431 new | only
1432 setline(1, 'two')
1433 g:OmniFunc2Args = []
1434 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1435 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1436 bw!
1437
1438 # Test for using a script-local function name
1439 def s:LocalOmniFunc(findstart: number, base: string): any
1440 add(g:LocalOmniFuncArgs, [findstart, base])
1441 return findstart ? 0 : []
1442 enddef
1443 &omnifunc = s:LocalOmniFunc
1444 new | only
1445 setline(1, 'three')
1446 g:LocalOmniFuncArgs = []
1447 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1448 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1449 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001450 END
1451 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001452
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001453 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001454 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001455 delfunc OmniFunc1
1456 delfunc OmniFunc2
1457 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001458 %bw!
1459endfunc
1460
1461" Test for different ways of setting the 'thesaurusfunc' option
1462func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001463 func TsrFunc1(callnr, findstart, base)
1464 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001465 return a:findstart ? 0 : []
1466 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001467 func TsrFunc2(findstart, base)
1468 call add(g:TsrFunc2Args, [a:findstart, a:base])
1469 return a:findstart ? 0 : ['sunday']
1470 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001471
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001472 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001473 #" Test for using a function name
1474 LET &thesaurusfunc = 'g:TsrFunc2'
1475 new
1476 call setline(1, 'zero')
1477 LET g:TsrFunc2Args = []
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([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1480 bw!
1481
1482 #" Test for using a function()
1483 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1484 new
1485 call setline(1, 'one')
1486 LET g:TsrFunc1Args = []
1487 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1488 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001489 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001490
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001491 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001492 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001493 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001494 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001495 call setline(1, 'two')
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([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001499 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001500
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001501 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001502 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001503 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001504 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001505 call setline(1, 'two')
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([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001509 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001510
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001511 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001512 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1513 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001514 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001515 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001516 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001517 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001518 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001519
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001520 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001521 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001522 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001523 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001524 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001525 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001526 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001527 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001528 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001529
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001530 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001531 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001532 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001533 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001534 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001535 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001536 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001537 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001538 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001539
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001540 #" Test for using a lambda function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001541 VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001542 LET optval = substitute(optval, ' ', '\\ ', 'g')
1543 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001544 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001545 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001546 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001547 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001548 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001549 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001550
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001551 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001552 LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND
1553 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001554 call setline(1, 'six')
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([[17, 1, ''], [17, 0, 'six']], 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(lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001561 LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND'
1562 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001563 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001564 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001565 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001566 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001567 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001568
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001569 #" Set 'thesaurusfunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001570 VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001571 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001572 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001573 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001574 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001575 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001576 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001577 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001578
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001579 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001580 LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001581 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001582 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001583 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001584 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001585 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001586 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001587 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001588
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001589 #" Test for using a lambda function with incorrect return value
1590 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1591 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001592 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593 call setline(1, 'eight')
1594 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1595 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001596
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 #" Test for clearing the 'thesaurusfunc' option
1598 set thesaurusfunc=''
1599 set thesaurusfunc&
1600 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1601 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001602
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001603 #" set 'thesaurusfunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001604 set thesaurusfunc=TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 call setline(1, 'ten')
1606 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1607 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001608 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001609 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001610 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001611 bw!
1612
1613 #" Use a buffer-local value and a global value
1614 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001615 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001616 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001617 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1619 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001620 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001621 new
1622 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001623 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1625 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001626 call assert_equal([], g:TsrFunc1Args)
1627 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 wincmd w
1629 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001631 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1632 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001633 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 :%bw!
1635 END
1636 call CheckLegacyAndVim9Success(lines)
1637
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001638 " Test for using a script-local function name
1639 func s:TsrFunc3(findstart, base)
1640 call add(g:TsrFunc3Args, [a:findstart, a:base])
1641 return a:findstart ? 0 : []
1642 endfunc
1643 set tsrfu=s:TsrFunc3
1644 new
1645 call setline(1, 'script1')
1646 let g:TsrFunc3Args = []
1647 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1648 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1649 bw!
1650
1651 let &tsrfu = 's:TsrFunc3'
1652 new
1653 call setline(1, 'script2')
1654 let g:TsrFunc3Args = []
1655 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1656 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1657 bw!
1658 delfunc s:TsrFunc3
1659
1660 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001661 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001662 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1663
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001664 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001665 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001666 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001667 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001668 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001669 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001670 bw!
1671
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001672 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
1673 func SetTsrFunc()
1674 let params = {'thesaurus': function('g:DictTsrFunc')}
1675 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001676 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001677 func g:DictTsrFunc(_) dict
1678 endfunc
1679 call SetTsrFunc()
1680 new
1681 call SetTsrFunc()
1682 bw
1683 call test_garbagecollect_now()
1684 new
1685 set thesaurusfunc=
1686 wincmd w
1687 %bw!
1688 delfunc SetTsrFunc
1689
1690 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
1691 " cause a crash.
1692 func SetLocalTsrFunc()
1693 let params = {'thesaurus': function('g:DictTsrFunc')}
1694 let &l:thesaurusfunc = params.thesaurus
1695 endfunc
1696 call SetLocalTsrFunc()
1697 call test_garbagecollect_now()
1698 call SetLocalTsrFunc()
1699 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001700 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001701 delfunc g:DictTsrFunc
1702 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001703
1704 " Vim9 tests
1705 let lines =<< trim END
1706 vim9script
1707
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001708 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
1709 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001710 return findstart ? 0 : []
1711 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001712
1713 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001714 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
1715 new | only
1716 setline(1, 'one')
1717 g:Vim9tsrFunc_Args = []
1718 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1719 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
1720 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001721
1722 # Test for using a global function name
1723 &thesaurusfunc = g:TsrFunc2
1724 new | only
1725 setline(1, 'two')
1726 g:TsrFunc2Args = []
1727 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1728 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
1729 bw!
1730
1731 # Test for using a script-local function name
1732 def s:LocalTsrFunc(findstart: number, base: string): any
1733 add(g:LocalTsrFuncArgs, [findstart, base])
1734 return findstart ? 0 : []
1735 enddef
1736 &thesaurusfunc = s:LocalTsrFunc
1737 new | only
1738 setline(1, 'three')
1739 g:LocalTsrFuncArgs = []
1740 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1741 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
1742 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001743 END
1744 call CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001745
1746 " cleanup
1747 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001748 delfunc TsrFunc1
1749 delfunc TsrFunc2
1750 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001751 %bw!
1752endfunc
1753
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001754" vim: shiftwidth=2 sts=2 expandtab