blob: e3d23e7598ebd22d0bcc4734b5cfe9aacb203712 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005source vim9.vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
12 call mkdir('Xdir')
13 cd Xdir
14
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
50 set cpt=.,w,i
51 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
74 set cpt=w
75 " checks make_cyclic in other window
76 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
77 call assert_equal('STARTTEST', getline('.'))
78
79 set cpt=u nohid
80 " checks unloaded buffer expansion
81 only
82 exe "normal oEN\<C-N>"
83 call assert_equal('ENDTEST', getline('.'))
84 " checks adding mode abortion
85 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
86 call assert_equal('unless', getline('.'))
87
88 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
89 " tag expansion, define add-expansion interrupted
90 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
91 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
92 " t-expansion
93 exe "normal oa\<C-N>\<Esc>"
94 call assert_equal('asd', getline('.'))
95
96 %bw!
97 call delete('Xtestfile')
98 call delete('Xtest11.one')
99 call delete('Xtest11.two')
100 call delete('Xtestdata')
101 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100102 cd ..
103 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200104endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100105
106func Test_omni_dash()
107 func Omni(findstart, base)
108 if a:findstart
109 return 5
110 else
111 echom a:base
112 return ['-help', '-v']
113 endif
114 endfunc
115 set omnifunc=Omni
116 new
117 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100118 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100119
120 bwipe!
121 delfunc Omni
122 set omnifunc=
123endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100124
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200125func Test_omni_autoload()
126 let save_rtp = &rtp
127 set rtp=Xruntime/some
128 let dir = 'Xruntime/some/autoload'
129 call mkdir(dir, 'p')
130
131 let lines =<< trim END
132 vim9script
133 def omni#func(findstart: bool, base: string): any
134 if findstart
135 return 1
136 else
137 return ['match']
138 endif
139 enddef
140 {
141 eval 1 + 2
142 }
143 END
144 call writefile(lines, dir .. '/omni.vim')
145
146 new
147 setlocal omnifunc=omni#func
148 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
149
150 bwipe!
151 call delete('Xruntime', 'rf')
152 set omnifunc=
153 let &rtp = save_rtp
154endfunc
155
Bram Moolenaarffa96842018-06-12 22:05:14 +0200156func Test_completefunc_args()
157 let s:args = []
158 func! CompleteFunc(findstart, base)
159 let s:args += [[a:findstart, empty(a:base)]]
160 endfunc
161 new
162
163 set completefunc=CompleteFunc
164 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200165 call assert_equal([1, 1], s:args[0])
166 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200167 set completefunc=
168
169 let s:args = []
170 set omnifunc=CompleteFunc
171 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200172 call assert_equal([1, 1], s:args[0])
173 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200174 set omnifunc=
175
176 bwipe!
177 unlet s:args
178 delfunc CompleteFunc
179endfunc
180
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100181func s:CompleteDone_CompleteFuncNone( findstart, base )
182 if a:findstart
183 return 0
184 endif
185
186 return v:none
187endfunc
188
Bram Moolenaar1e115362019-01-09 23:01:02 +0100189func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100190 if a:findstart
191 return 0
192 endif
193
194 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100195 \ 'words': [
196 \ {
197 \ 'word': 'aword',
198 \ 'abbr': 'wrd',
199 \ 'menu': 'extra text',
200 \ 'info': 'words are cool',
201 \ 'kind': 'W',
202 \ 'user_data': 'test'
203 \ }
204 \ ]
205 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100206endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100207
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100208func s:CompleteDone_CheckCompletedItemNone()
209 let s:called_completedone = 1
210endfunc
211
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100212func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100213 call assert_equal( 'aword', v:completed_item[ 'word' ] )
214 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
215 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
216 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
217 call assert_equal( 'W', v:completed_item[ 'kind' ] )
218 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
219
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100220 if a:pre
221 call assert_equal('function', complete_info().mode)
222 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100223
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100224 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100225endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100226
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100227func Test_CompleteDoneNone()
228 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200229 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100230
231 set completefunc=<SID>CompleteDone_CompleteFuncNone
232 execute "normal a\<C-X>\<C-U>\<C-Y>"
233 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200234 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100235
236 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200237 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100238
239 let s:called_completedone = 0
240 au! CompleteDone
241endfunc
242
243func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100244 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
245 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100246
247 set completefunc=<SID>CompleteDone_CompleteFuncDict
248 execute "normal a\<C-X>\<C-U>\<C-Y>"
249 set completefunc&
250
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100251 call assert_equal('test', v:completed_item[ 'user_data' ])
252 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100253
254 let s:called_completedone = 0
255 au! CompleteDone
256endfunc
257
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100258func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100259 if a:findstart
260 return 0
261 endif
262
263 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100264 \ 'words': [
265 \ {
266 \ 'word': 'aword',
267 \ 'abbr': 'wrd',
268 \ 'menu': 'extra text',
269 \ 'info': 'words are cool',
270 \ 'kind': 'W',
271 \ 'user_data': ['one', 'two'],
272 \ }
273 \ ]
274 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100275endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100276
Bram Moolenaar1e115362019-01-09 23:01:02 +0100277func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100278 call assert_equal( 'aword', v:completed_item[ 'word' ] )
279 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
280 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
281 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
282 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100283 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100284
285 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100286endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100287
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100288func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100289 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
290
291 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
292 execute "normal a\<C-X>\<C-U>\<C-Y>"
293 set completefunc&
294
Bram Moolenaar08928322020-01-04 14:32:48 +0100295 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100296 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100297
298 let s:called_completedone = 0
299 au! CompleteDone
300endfunc
301
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100302func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100303 if a:findstart
304 return 0
305 endif
306
307 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100308endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100309
Bram Moolenaar1e115362019-01-09 23:01:02 +0100310func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100311 call assert_equal( 'aword', v:completed_item[ 'word' ] )
312 call assert_equal( '', v:completed_item[ 'abbr' ] )
313 call assert_equal( '', v:completed_item[ 'menu' ] )
314 call assert_equal( '', v:completed_item[ 'info' ] )
315 call assert_equal( '', v:completed_item[ 'kind' ] )
316 call assert_equal( '', v:completed_item[ 'user_data' ] )
317
318 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100319endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100321func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100322 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
323
324 set completefunc=<SID>CompleteDone_CompleteFuncList
325 execute "normal a\<C-X>\<C-U>\<C-Y>"
326 set completefunc&
327
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100328 call assert_equal('', v:completed_item[ 'user_data' ])
329 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100330
331 let s:called_completedone = 0
332 au! CompleteDone
333endfunc
334
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200335func Test_CompleteDone_undo()
336 au CompleteDone * call append(0, "prepend1")
337 new
338 call setline(1, ["line1", "line2"])
339 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
340 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
341 \ getline(1, '$'))
342 undo
343 call assert_equal(["line1", "line2"], getline(1, '$'))
344 bwipe!
345 au! CompleteDone
346endfunc
347
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200348func CompleteTest(findstart, query)
349 if a:findstart
350 return col('.')
351 endif
352 return ['matched']
353endfunc
354
355func Test_completefunc_info()
356 new
357 set completeopt=menuone
358 set completefunc=CompleteTest
359 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100360 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 +0200361 bwipe!
362 set completeopt&
363 set completefunc&
364endfunc
365
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100366" Check that when using feedkeys() typeahead does not interrupt searching for
367" completions.
368func Test_compl_feedkeys()
369 new
370 set completeopt=menuone,noselect
371 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
372 call assert_equal("jump jump", getline(1))
373 bwipe!
374 set completeopt&
375endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200376
mityua1198122021-11-20 19:13:39 +0000377func s:ComplInCmdwin_GlobalCompletion(a, l, p)
378 return 'global'
379endfunc
380
381func s:ComplInCmdwin_LocalCompletion(a, l, p)
382 return 'local'
383endfunc
384
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200385func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100386 CheckFeature cmdwin
387
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200388 set wildmenu wildchar=<Tab>
389 com! -nargs=1 -complete=command GetInput let input = <q-args>
390 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100391 let w:test_winvar = 'winvar'
392 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200393
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100394 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200395 let input = ''
396 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
397 call assert_equal('TestCommand', input)
398
399 let input = ''
400 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
401 call assert_equal('T', input)
402
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100403
404 com! -nargs=1 -complete=var GetInput let input = <q-args>
405 " Window-local variables
406 let input = ''
407 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
408 call assert_equal('w:test_winvar', input)
409
410 let input = ''
411 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
412 call assert_equal('w:test_', input)
413
414 " Buffer-local variables
415 let input = ''
416 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
417 call assert_equal('b:test_bufvar', input)
418
419 let input = ''
420 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
421 call assert_equal('b:test_', input)
422
mityua1198122021-11-20 19:13:39 +0000423
424 " Argument completion of buffer-local command
425 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
426 return ['global']
427 endfunc
428
429 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
430 return ['local']
431 endfunc
432
433 func s:ComplInCmdwin_CheckCompletion(arg)
434 call assert_equal('local', a:arg)
435 endfunc
436
437 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
438 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
439 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
440 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
441 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
442
443 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
444 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
445 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
446 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
447
448 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
449
450 func! s:ComplInCmdwin_CheckCompletion(arg)
451 call assert_equal('global', a:arg)
452 endfunc
453 new
454 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
455 quit
456
457 delfunc s:ComplInCmdwin_GlobalCompletion
458 delfunc s:ComplInCmdwin_LocalCompletion
459 delfunc s:ComplInCmdwin_GlobalCompletionList
460 delfunc s:ComplInCmdwin_LocalCompletionList
461 delfunc s:ComplInCmdwin_CheckCompletion
462
463 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200464 delcom TestCommand
465 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100466 unlet w:test_winvar
467 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200468 set wildmenu& wildchar&
469endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200470
471" Test for insert path completion with completeslash option
472func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200473 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200474
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200475 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200476 let orig_shellslash = &shellslash
477 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200478 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200479
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200480 set noshellslash
481
482 set completeslash=
483 exe "normal oXd\<C-X>\<C-F>"
484 call assert_equal('Xdir\', getline('.'))
485
486 set completeslash=backslash
487 exe "normal oXd\<C-X>\<C-F>"
488 call assert_equal('Xdir\', getline('.'))
489
490 set completeslash=slash
491 exe "normal oXd\<C-X>\<C-F>"
492 call assert_equal('Xdir/', getline('.'))
493
494 set shellslash
495
496 set completeslash=
497 exe "normal oXd\<C-X>\<C-F>"
498 call assert_equal('Xdir/', getline('.'))
499
500 set completeslash=backslash
501 exe "normal oXd\<C-X>\<C-F>"
502 call assert_equal('Xdir\', getline('.'))
503
504 set completeslash=slash
505 exe "normal oXd\<C-X>\<C-F>"
506 call assert_equal('Xdir/', getline('.'))
507 %bw!
508 call delete('Xdir', 'rf')
509
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200510 set noshellslash
511 set completeslash=slash
512 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
513
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200514 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200515 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200516endfunc
517
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200518func Test_pum_stopped_by_timer()
519 CheckScreendump
520
521 let lines =<< trim END
522 call setline(1, ['hello', 'hullo', 'heeee', ''])
523 func StartCompl()
524 call timer_start(100, { -> execute('stopinsert') })
525 call feedkeys("Gah\<C-N>")
526 endfunc
527 END
528
529 call writefile(lines, 'Xpumscript')
530 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
531 call term_sendkeys(buf, ":call StartCompl()\<CR>")
532 call TermWait(buf, 200)
533 call term_sendkeys(buf, "k")
534 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
535
536 call StopVimInTerminal(buf)
537 call delete('Xpumscript')
538endfunc
539
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100540func Test_pum_with_folds_two_tabs()
541 CheckScreendump
542
543 let lines =<< trim END
544 set fdm=marker
545 call setline(1, ['" x {{{1', '" a some text'])
546 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
547 norm! zm
548 tab sp
549 call feedkeys('2Gzv', 'xt')
550 call feedkeys("0fa", 'xt')
551 END
552
553 call writefile(lines, 'Xpumscript')
554 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200555 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100556 call term_sendkeys(buf, "a\<C-N>")
557 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
558
559 call term_sendkeys(buf, "\<Esc>")
560 call StopVimInTerminal(buf)
561 call delete('Xpumscript')
562endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100563
564func Test_pum_with_preview_win()
565 CheckScreendump
566
567 let lines =<< trim END
568 funct Omni_test(findstart, base)
569 if a:findstart
570 return col(".") - 1
571 endif
572 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
573 endfunc
574 set omnifunc=Omni_test
575 set completeopt+=longest
576 END
577
578 call writefile(lines, 'Xpreviewscript')
579 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200580 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100581 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200582 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100583 call term_sendkeys(buf, "\<C-N>")
584 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
585
586 call term_sendkeys(buf, "\<Esc>")
587 call StopVimInTerminal(buf)
588 call delete('Xpreviewscript')
589endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100590
591" Test for inserting the tag search pattern in insert mode
592func Test_ins_compl_tag_sft()
593 call writefile([
594 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
595 \ "first\tXfoo\t/^int first() {}$/",
596 \ "second\tXfoo\t/^int second() {}$/",
597 \ "third\tXfoo\t/^int third() {}$/"],
598 \ 'Xtags')
599 set tags=Xtags
600 let code =<< trim [CODE]
601 int first() {}
602 int second() {}
603 int third() {}
604 [CODE]
605 call writefile(code, 'Xfoo')
606
607 enew
608 set showfulltag
609 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
610 call assert_equal('int second() {}', getline(1))
611 set noshowfulltag
612
613 call delete('Xtags')
614 call delete('Xfoo')
615 set tags&
616 %bwipe!
617endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200618
619" Test for 'completefunc' deleting text
620func Test_completefunc_error()
621 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200622 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200623 func CompleteFunc(findstart, base)
624 if a:findstart == 1
625 normal dd
626 return col('.') - 1
627 endif
628 return ['a', 'b']
629 endfunc
630 set completefunc=CompleteFunc
631 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100632 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200633
634 " delete text when called for the second time
635 func CompleteFunc2(findstart, base)
636 if a:findstart == 1
637 return col('.') - 1
638 endif
639 normal dd
640 return ['a', 'b']
641 endfunc
642 set completefunc=CompleteFunc2
643 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200644 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200645
Bram Moolenaar97202d92021-01-28 18:34:35 +0100646 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100647 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100648 if a:findstart == 1
649 return col('.') - 1
650 endif
651 wincmd p
652 return ['a', 'b']
653 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100654 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100655 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100656 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100657 close!
658
659 set completefunc&
660 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100661 delfunc CompleteFunc2
662 delfunc CompleteFunc3
663 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100664endfunc
665
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200666" Test for returning non-string values from 'completefunc'
667func Test_completefunc_invalid_data()
668 new
669 func! CompleteFunc(findstart, base)
670 if a:findstart == 1
671 return col('.') - 1
672 endif
673 return [{}, '', 'moon']
674 endfunc
675 set completefunc=CompleteFunc
676 exe "normal i\<C-X>\<C-U>"
677 call assert_equal('moon', getline(1))
678 set completefunc&
679 close!
680endfunc
681
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200682" Test for errors in using complete() function
683func Test_complete_func_error()
684 call assert_fails('call complete(1, ["a"])', 'E785:')
685 func ListColors()
686 call complete(col('.'), "blue")
687 endfunc
688 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
689 func ListMonths()
690 call complete(col('.'), test_null_list())
691 endfunc
692 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
693 delfunc ListColors
694 delfunc ListMonths
695 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200696 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200697endfunc
698
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200699" Test for completing words following a completed word in a line
700func Test_complete_wrapscan()
701 " complete words from another buffer
702 new
703 call setline(1, ['one two', 'three four'])
704 new
705 setlocal complete=w
706 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
707 call assert_equal('two three four', getline(1))
708 close!
709 " complete words from the current buffer
710 setlocal complete=.
711 %d
712 call setline(1, ['one two', ''])
713 call cursor(2, 1)
714 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
715 call assert_equal('one two one two', getline(2))
716 close!
717endfunc
718
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200719" Test for completing special characters
720func Test_complete_special_chars()
721 new
722 call setline(1, 'int .*[-\^$ func float')
723 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
724 call assert_equal('int .*[-\^$ func float', getline(2))
725 close!
726endfunc
727
728" Test for completion when text is wrapped across lines.
729func Test_complete_across_line()
730 new
731 call setline(1, ['red green blue', 'one two three'])
732 setlocal textwidth=20
733 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
734 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
735 close!
736endfunc
737
738" Test for using CTRL-L to add one character when completing matching
739func Test_complete_add_onechar()
740 new
741 call setline(1, ['wool', 'woodwork'])
742 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
743 call assert_equal('woof', getline(3))
744
745 " use 'ignorecase' and backspace to erase characters from the prefix string
746 " and then add letters using CTRL-L
747 %d
748 set ignorecase backspace=2
749 setlocal complete=.
750 call setline(1, ['workhorse', 'workload'])
751 normal Go
752 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
753 call assert_equal('workh', getline(3))
754 set ignorecase& backspace&
755 close!
756endfunc
757
758" Test insert completion with 'cindent' (adjust the indent)
759func Test_complete_with_cindent()
760 new
761 setlocal cindent
762 call setline(1, ['if (i == 1)', " j = 2;"])
763 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
764 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
765
766 %d
767 call setline(1, ['when while', '{', ''])
768 setlocal cinkeys+==while
769 exe "normal Giwh\<C-P> "
770 call assert_equal("\twhile ", getline('$'))
771 close!
772endfunc
773
774" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
775func Test_complete_cmdline()
776 new
777 exe "normal icaddb\<C-X>\<C-V>"
778 call assert_equal('caddbuffer', getline(1))
779 exe "normal ocall getqf\<C-X>\<C-V>"
780 call assert_equal('call getqflist(', getline(2))
781 exe "normal oabcxyz(\<C-X>\<C-V>"
782 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200783 com! -buffer TestCommand1 echo 'TestCommand1'
784 com! -buffer TestCommand2 echo 'TestCommand2'
785 write TestCommand1Test
786 write TestCommand2Test
787 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
788 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
789 call assert_equal('TestCommand2Test', getline(4))
790 call delete('TestCommand1Test')
791 call delete('TestCommand2Test')
792 delcom TestCommand1
793 delcom TestCommand2
794 close!
795endfunc
796
797" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
798func Test_complete_stop()
799 new
800 func Save_mode1()
801 let g:mode1 = mode(1)
802 return ''
803 endfunc
804 func Save_mode2()
805 let g:mode2 = mode(1)
806 return ''
807 endfunc
808 inoremap <F1> <C-R>=Save_mode1()<CR>
809 inoremap <F2> <C-R>=Save_mode2()<CR>
810 call setline(1, ['aaa bbb ccc '])
811 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
812 call assert_equal('ic', g:mode1)
813 call assert_equal('i', g:mode2)
814 call assert_equal('aaa bbb ccc ', getline(1))
815 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
816 call assert_equal('ic', g:mode1)
817 call assert_equal('i', g:mode2)
818 call assert_equal('aaa bbb ccc aaa', getline(1))
819 set completeopt+=noselect
820 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
821 call assert_equal('ic', g:mode1)
822 call assert_equal('i', g:mode2)
823 call assert_equal('aaa bbb ccc aaa bb', getline(1))
824 set completeopt&
825 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
826 call assert_equal('ic', g:mode1)
827 call assert_equal('i', g:mode2)
828 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
829 com! -buffer TestCommand1 echo 'TestCommand1'
830 com! -buffer TestCommand2 echo 'TestCommand2'
831 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
832 call assert_equal('ic', g:mode1)
833 call assert_equal('i', g:mode2)
834 call assert_equal('TestCommand2', getline(2))
835 delcom TestCommand1
836 delcom TestCommand2
837 unlet g:mode1
838 unlet g:mode2
839 iunmap <F1>
840 iunmap <F2>
841 delfunc Save_mode1
842 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200843 close!
844endfunc
845
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200846func Test_issue_7021()
847 CheckMSWindows
848
849 let orig_shellslash = &shellslash
850 set noshellslash
851
852 set completeslash=slash
853 call assert_false(expand('~') =~ '/')
854
855 let &shellslash = orig_shellslash
856 set completeslash=
857endfunc
858
Bram Moolenaarcc233582020-12-12 13:32:07 +0100859" Test to ensure 'Scanning...' messages are not recorded in messages history
860func Test_z1_complete_no_history()
861 new
862 messages clear
863 let currmess = execute('messages')
864 setlocal dictionary=README.txt
865 exe "normal owh\<C-X>\<C-K>"
866 exe "normal owh\<C-N>"
867 call assert_equal(currmess, execute('messages'))
868 close!
869endfunc
870
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000871" Test for different ways of setting the 'completefunc' option
872func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000873 func CompleteFunc1(callnr, findstart, base)
874 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
875 return a:findstart ? 0 : []
876 endfunc
877 func CompleteFunc2(findstart, base)
878 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000879 return a:findstart ? 0 : []
880 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +0000881
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000882 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000883 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000884 LET &completefunc = 'g:CompleteFunc2'
885 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000886 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000887 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000888 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +0000889 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000890 bw!
891
892 #" Test for using a function()
893 set completefunc=function('g:CompleteFunc1',\ [10])
894 new
895 call setline(1, 'one')
896 LET g:CompleteFunc1Args = []
897 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
898 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000899 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000900
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000901 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000902 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000903 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000904 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000905 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000906 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000907 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000908 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000909 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000910
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000911 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000912 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000913 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000914 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000915 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000916 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000917 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000918 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000919 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000920
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000921 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000922 set completefunc=funcref('g:CompleteFunc1',\ [13])
923 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000924 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000925 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000926 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000927 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000928 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000929
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000930 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000931 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000932 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000933 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000934 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000935 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000936 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000937 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000938 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000939
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000940 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000941 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000942 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000943 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000944 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000945 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000946 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000947 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000948 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000949
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000950 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000951 VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000952 LET optval = substitute(optval, ' ', '\\ ', 'g')
953 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000954 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000955 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000956 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000957 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000958 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000959 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000960
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000961 #" Set 'completefunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000962 LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND
963 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000964 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000965 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000966 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000967 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000968 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000969
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000970 #" Set 'completefunc' to string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000971 LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND'
972 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000973 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000974 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000975 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000976 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000977 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000978
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000979 #" Set 'completefunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000980 VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000981 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000982 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000983 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000984 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000985 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000986 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000987 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000988
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000989 #" Set 'completefunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000990 LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000991 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000992 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000993 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000994 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000995 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000996 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000997 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000998
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000999 #" Test for using a lambda function with incorrect return value
1000 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1001 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001002 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001003 call setline(1, 'eight')
1004 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1005 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001006
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001007 #" Test for clearing the 'completefunc' option
1008 set completefunc=''
1009 set completefunc&
1010 call assert_fails("set completefunc=function('abc')", "E700:")
1011 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001012
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001013 #" set 'completefunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001014 set completefunc=CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001015 call setline(1, 'five')
1016 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1017 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001018 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001019 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001020 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001021 bw!
1022 END
1023 call CheckLegacyAndVim9Success(lines)
1024
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001025 " Test for using a script-local function name
1026 func s:CompleteFunc3(findstart, base)
1027 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1028 return a:findstart ? 0 : []
1029 endfunc
1030 set completefunc=s:CompleteFunc3
1031 new
1032 call setline(1, 'script1')
1033 let g:CompleteFunc3Args = []
1034 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1035 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1036 bw!
1037
1038 let &completefunc = 's:CompleteFunc3'
1039 new
1040 call setline(1, 'script2')
1041 let g:CompleteFunc3Args = []
1042 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1043 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1044 bw!
1045 delfunc s:CompleteFunc3
1046
1047 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001048 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001049 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1050
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001051 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001052 set completefunc=(a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001053 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001054 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001055 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001056 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001057
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001058 " set 'completefunc' to a partial with dict. This used to cause a crash.
1059 func SetCompleteFunc()
1060 let params = {'complete': function('g:DictCompleteFunc')}
1061 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001062 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001063 func g:DictCompleteFunc(_) dict
1064 endfunc
1065 call SetCompleteFunc()
1066 new
1067 call SetCompleteFunc()
1068 bw
1069 call test_garbagecollect_now()
1070 new
1071 set completefunc=
1072 wincmd w
1073 set completefunc=
1074 %bw!
1075 delfunc g:DictCompleteFunc
1076 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001077
1078 " Vim9 tests
1079 let lines =<< trim END
1080 vim9script
1081
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001082 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1083 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001084 return findstart ? 0 : []
1085 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001086
1087 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001088 set completefunc=function('Vim9CompleteFunc',\ [60])
1089 new | only
1090 setline(1, 'one')
1091 g:Vim9completeFuncArgs = []
1092 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1093 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1094 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001095
1096 # Test for using a global function name
1097 &completefunc = g:CompleteFunc2
1098 new | only
1099 setline(1, 'two')
1100 g:CompleteFunc2Args = []
1101 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1102 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1103 bw!
1104
1105 # Test for using a script-local function name
1106 def s:LocalCompleteFunc(findstart: number, base: string): any
1107 add(g:LocalCompleteFuncArgs, [findstart, base])
1108 return findstart ? 0 : []
1109 enddef
1110 &completefunc = s:LocalCompleteFunc
1111 new | only
1112 setline(1, 'three')
1113 g:LocalCompleteFuncArgs = []
1114 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1115 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1116 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001117 END
1118 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001119
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001120 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001121 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001122 delfunc CompleteFunc1
1123 delfunc CompleteFunc2
1124 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001125 %bw!
1126endfunc
1127
1128" Test for different ways of setting the 'omnifunc' option
1129func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001130 func OmniFunc1(callnr, findstart, base)
1131 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1132 return a:findstart ? 0 : []
1133 endfunc
1134 func OmniFunc2(findstart, base)
1135 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001136 return a:findstart ? 0 : []
1137 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001138
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001139 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001140 #" Test for using a function name
1141 LET &omnifunc = 'g:OmniFunc2'
1142 new
1143 call setline(1, 'zero')
1144 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001145 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001146 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1147 bw!
1148
1149 #" Test for using a function()
1150 set omnifunc=function('g:OmniFunc1',\ [10])
1151 new
1152 call setline(1, 'one')
1153 LET g:OmniFunc1Args = []
1154 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1155 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001156 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001157
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001158 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001159 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001160 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001161 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001162 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001163 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001164 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001165 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001166 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001167
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001168 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001169 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001170 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001171 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001172 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001173 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001174 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001175 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001176 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001177
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001178 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001179 set omnifunc=funcref('g:OmniFunc1',\ [13])
1180 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001181 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001182 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001183 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001184 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001185 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001186
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001187 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001188 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001189 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001190 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001191 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001192 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001193 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001194 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001195 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001196
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001197 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001198 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001199 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001200 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001201 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001202 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001203 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001204 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001205 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001206
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001207 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001208 VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001209 LET optval = substitute(optval, ' ', '\\ ', 'g')
1210 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001211 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001212 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001213 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001214 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001215 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001216 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001217
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001218 #" Set 'omnifunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001219 LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND
1220 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001221 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001222 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001223 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001224 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001225 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001226
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001227 #" Set 'omnifunc' to a string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001228 LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND'
1229 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001230 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001231 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001232 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001233 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001234 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001235
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001236 #" Set 'omnifunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001237 VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001238 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001239 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001240 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001241 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001242 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001243 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001244 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001245
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001246 #" Set 'omnifunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001247 LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001248 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001249 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001250 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001251 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001252 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001253 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001254 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001255
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001256 #" Test for using a lambda function with incorrect return value
1257 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1258 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001259 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001260 call setline(1, 'eight')
1261 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1262 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001263
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001264 #" Test for clearing the 'omnifunc' option
1265 set omnifunc=''
1266 set omnifunc&
1267 call assert_fails("set omnifunc=function('abc')", "E700:")
1268 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001269
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001270 #" set 'omnifunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001271 set omnifunc=OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001272 call setline(1, 'nine')
1273 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1274 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001275 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001276 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001277 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001278 bw!
1279 END
1280 call CheckLegacyAndVim9Success(lines)
1281
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001282 " Test for using a script-local function name
1283 func s:OmniFunc3(findstart, base)
1284 call add(g:OmniFunc3Args, [a:findstart, a:base])
1285 return a:findstart ? 0 : []
1286 endfunc
1287 set omnifunc=s:OmniFunc3
1288 new
1289 call setline(1, 'script1')
1290 let g:OmniFunc3Args = []
1291 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1292 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1293 bw!
1294
1295 let &omnifunc = 's:OmniFunc3'
1296 new
1297 call setline(1, 'script2')
1298 let g:OmniFunc3Args = []
1299 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1300 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1301 bw!
1302 delfunc s:OmniFunc3
1303
1304 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001305 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001306 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1307
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001308 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001309 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001310 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001311 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001312 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001313 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001314
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001315 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1316 func SetOmniFunc()
1317 let params = {'omni': function('g:DictOmniFunc')}
1318 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001319 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001320 func g:DictOmniFunc(_) dict
1321 endfunc
1322 call SetOmniFunc()
1323 new
1324 call SetOmniFunc()
1325 bw
1326 call test_garbagecollect_now()
1327 new
1328 set omnifunc=
1329 wincmd w
1330 set omnifunc=
1331 %bw!
1332 delfunc g:DictOmniFunc
1333 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001334
1335 " Vim9 tests
1336 let lines =<< trim END
1337 vim9script
1338
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001339 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1340 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001341 return findstart ? 0 : []
1342 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001343
1344 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001345 set omnifunc=function('Vim9omniFunc',\ [60])
1346 new | only
1347 setline(1, 'one')
1348 g:Vim9omniFunc_Args = []
1349 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1350 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1351 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001352
1353 # Test for using a global function name
1354 &omnifunc = g:OmniFunc2
1355 new | only
1356 setline(1, 'two')
1357 g:OmniFunc2Args = []
1358 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1359 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1360 bw!
1361
1362 # Test for using a script-local function name
1363 def s:LocalOmniFunc(findstart: number, base: string): any
1364 add(g:LocalOmniFuncArgs, [findstart, base])
1365 return findstart ? 0 : []
1366 enddef
1367 &omnifunc = s:LocalOmniFunc
1368 new | only
1369 setline(1, 'three')
1370 g:LocalOmniFuncArgs = []
1371 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1372 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1373 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001374 END
1375 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001376
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001377 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001378 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001379 delfunc OmniFunc1
1380 delfunc OmniFunc2
1381 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001382 %bw!
1383endfunc
1384
1385" Test for different ways of setting the 'thesaurusfunc' option
1386func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001387 func TsrFunc1(callnr, findstart, base)
1388 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001389 return a:findstart ? 0 : []
1390 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001391 func TsrFunc2(findstart, base)
1392 call add(g:TsrFunc2Args, [a:findstart, a:base])
1393 return a:findstart ? 0 : ['sunday']
1394 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001395
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001396 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001397 #" Test for using a function name
1398 LET &thesaurusfunc = 'g:TsrFunc2'
1399 new
1400 call setline(1, 'zero')
1401 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001402 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001403 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1404 bw!
1405
1406 #" Test for using a function()
1407 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1408 new
1409 call setline(1, 'one')
1410 LET g:TsrFunc1Args = []
1411 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1412 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001414
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001416 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001417 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001418 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001419 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001420 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001421 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001422 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001423 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001424
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001425 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001426 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001427 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001428 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001429 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001430 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001431 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001432 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001433 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001434
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001435 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001436 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1437 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001438 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001439 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001440 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001441 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001442 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001443
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001444 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001445 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001446 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001447 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001448 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001449 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001450 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001451 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001452 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001453
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001454 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001455 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001456 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001457 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001458 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001459 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001460 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001461 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001462 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001463
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001464 #" Test for using a lambda function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001465 VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001466 LET optval = substitute(optval, ' ', '\\ ', 'g')
1467 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001468 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001469 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001470 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001471 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001472 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001473 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001474
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001475 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001476 LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND
1477 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001478 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001479 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001480 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001481 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001482 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001483
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001484 #" Set 'thesaurusfunc' to a string(lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND'
1486 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001487 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001488 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001489 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001490 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001491 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001492
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001493 #" Set 'thesaurusfunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001494 VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001495 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001496 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001497 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001498 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001499 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001500 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001501 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001502
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001503 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001504 LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001505 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001506 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001507 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001508 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001509 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001510 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001511 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001512
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001513 #" Test for using a lambda function with incorrect return value
1514 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1515 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001516 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001517 call setline(1, 'eight')
1518 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1519 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001520
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001521 #" Test for clearing the 'thesaurusfunc' option
1522 set thesaurusfunc=''
1523 set thesaurusfunc&
1524 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1525 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001526
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001527 #" set 'thesaurusfunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001528 set thesaurusfunc=TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001529 call setline(1, 'ten')
1530 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1531 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001532 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001533 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001534 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001535 bw!
1536
1537 #" Use a buffer-local value and a global value
1538 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001539 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001540 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001541 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001542 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1543 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001544 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001545 new
1546 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001547 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001548 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1549 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001550 call assert_equal([], g:TsrFunc1Args)
1551 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001552 wincmd w
1553 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001554 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001555 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1556 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001557 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001558 :%bw!
1559 END
1560 call CheckLegacyAndVim9Success(lines)
1561
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001562 " Test for using a script-local function name
1563 func s:TsrFunc3(findstart, base)
1564 call add(g:TsrFunc3Args, [a:findstart, a:base])
1565 return a:findstart ? 0 : []
1566 endfunc
1567 set tsrfu=s:TsrFunc3
1568 new
1569 call setline(1, 'script1')
1570 let g:TsrFunc3Args = []
1571 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1572 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1573 bw!
1574
1575 let &tsrfu = 's:TsrFunc3'
1576 new
1577 call setline(1, 'script2')
1578 let g:TsrFunc3Args = []
1579 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1580 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1581 bw!
1582 delfunc s:TsrFunc3
1583
1584 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001585 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001586 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1587
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001588 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001589 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001590 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001591 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001592 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001593 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001594 bw!
1595
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001596 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
1597 func SetTsrFunc()
1598 let params = {'thesaurus': function('g:DictTsrFunc')}
1599 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001600 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 func g:DictTsrFunc(_) dict
1602 endfunc
1603 call SetTsrFunc()
1604 new
1605 call SetTsrFunc()
1606 bw
1607 call test_garbagecollect_now()
1608 new
1609 set thesaurusfunc=
1610 wincmd w
1611 %bw!
1612 delfunc SetTsrFunc
1613
1614 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
1615 " cause a crash.
1616 func SetLocalTsrFunc()
1617 let params = {'thesaurus': function('g:DictTsrFunc')}
1618 let &l:thesaurusfunc = params.thesaurus
1619 endfunc
1620 call SetLocalTsrFunc()
1621 call test_garbagecollect_now()
1622 call SetLocalTsrFunc()
1623 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001624 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001625 delfunc g:DictTsrFunc
1626 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001627
1628 " Vim9 tests
1629 let lines =<< trim END
1630 vim9script
1631
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001632 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
1633 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001634 return findstart ? 0 : []
1635 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001636
1637 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001638 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
1639 new | only
1640 setline(1, 'one')
1641 g:Vim9tsrFunc_Args = []
1642 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1643 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
1644 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001645
1646 # Test for using a global function name
1647 &thesaurusfunc = g:TsrFunc2
1648 new | only
1649 setline(1, 'two')
1650 g:TsrFunc2Args = []
1651 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1652 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
1653 bw!
1654
1655 # Test for using a script-local function name
1656 def s:LocalTsrFunc(findstart: number, base: string): any
1657 add(g:LocalTsrFuncArgs, [findstart, base])
1658 return findstart ? 0 : []
1659 enddef
1660 &thesaurusfunc = s:LocalTsrFunc
1661 new | only
1662 setline(1, 'three')
1663 g:LocalTsrFuncArgs = []
1664 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1665 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
1666 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001667 END
1668 call CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001669
1670 " cleanup
1671 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001672 delfunc TsrFunc1
1673 delfunc TsrFunc2
1674 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001675 %bw!
1676endfunc
1677
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001678" vim: shiftwidth=2 sts=2 expandtab