blob: aa7b24bf85b2a15d8942f4ad303679730289e768 [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()
873 " Test for using a function()
874 func MycompleteFunc1(findstart, base)
875 call add(g:MycompleteFunc1_args, [a:findstart, a:base])
876 return a:findstart ? 0 : []
877 endfunc
878 set completefunc=function('MycompleteFunc1')
879 new | only
880 call setline(1, 'one')
881 let g:MycompleteFunc1_args = []
882 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
883 call assert_equal([[1, ''], [0, 'one']], g:MycompleteFunc1_args)
884 bw!
885
886 " Using a funcref variable to set 'completefunc'
887 let Fn = function('MycompleteFunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000888 let &completefunc = Fn
889 new | only
890 call setline(1, 'two')
891 let g:MycompleteFunc1_args = []
892 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
893 call assert_equal([[1, ''], [0, 'two']], g:MycompleteFunc1_args)
894 bw!
895
896 " Using string(funcref_variable) to set 'completefunc'
897 let Fn = function('MycompleteFunc1')
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000898 let &completefunc = string(Fn)
899 new | only
900 call setline(1, 'two')
901 let g:MycompleteFunc1_args = []
902 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
903 call assert_equal([[1, ''], [0, 'two']], g:MycompleteFunc1_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000904 bw!
905
906 " Test for using a funcref()
907 func MycompleteFunc2(findstart, base)
908 call add(g:MycompleteFunc2_args, [a:findstart, a:base])
909 return a:findstart ? 0 : []
910 endfunc
911 set completefunc=funcref('MycompleteFunc2')
912 new | only
913 call setline(1, 'three')
914 let g:MycompleteFunc2_args = []
915 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
916 call assert_equal([[1, ''], [0, 'three']], g:MycompleteFunc2_args)
917 bw!
918
919 " Using a funcref variable to set 'completefunc'
920 let Fn = funcref('MycompleteFunc2')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000921 let &completefunc = Fn
922 new | only
923 call setline(1, 'four')
924 let g:MycompleteFunc2_args = []
925 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
926 call assert_equal([[1, ''], [0, 'four']], g:MycompleteFunc2_args)
927 bw!
928
929 " Using a string(funcref_variable) to set 'completefunc'
930 let Fn = funcref('MycompleteFunc2')
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000931 let &completefunc = string(Fn)
932 new | only
933 call setline(1, 'four')
934 let g:MycompleteFunc2_args = []
935 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
936 call assert_equal([[1, ''], [0, 'four']], g:MycompleteFunc2_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000937 bw!
938
939 " Test for using a lambda function
940 func MycompleteFunc3(findstart, base)
941 call add(g:MycompleteFunc3_args, [a:findstart, a:base])
942 return a:findstart ? 0 : []
943 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000944 set completefunc={a,\ b\ ->\ MycompleteFunc3(a,\ b)}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000945 new | only
946 call setline(1, 'five')
947 let g:MycompleteFunc3_args = []
948 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
949 call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc3_args)
950 bw!
951
952 " Set 'completefunc' to a lambda expression
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000953 let &completefunc = {a, b -> MycompleteFunc3(a, b)}
954 new | only
955 call setline(1, 'six')
956 let g:MycompleteFunc3_args = []
957 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
958 call assert_equal([[1, ''], [0, 'six']], g:MycompleteFunc3_args)
959 bw!
960
961 " Set 'completefunc' to string(lambda_expression)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000962 let &completefunc = '{a, b -> MycompleteFunc3(a, b)}'
963 new | only
964 call setline(1, 'six')
965 let g:MycompleteFunc3_args = []
966 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
967 call assert_equal([[1, ''], [0, 'six']], g:MycompleteFunc3_args)
968 bw!
969
970 " Set 'completefunc' to a variable with a lambda expression
971 let Lambda = {a, b -> MycompleteFunc3(a, b)}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000972 let &completefunc = Lambda
973 new | only
974 call setline(1, 'seven')
975 let g:MycompleteFunc3_args = []
976 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
977 call assert_equal([[1, ''], [0, 'seven']], g:MycompleteFunc3_args)
978 bw!
979
980 " Set 'completefunc' to a string(variable with a lambda expression)
981 let Lambda = {a, b -> MycompleteFunc3(a, b)}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000982 let &completefunc = string(Lambda)
983 new | only
984 call setline(1, 'seven')
985 let g:MycompleteFunc3_args = []
986 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
987 call assert_equal([[1, ''], [0, 'seven']], g:MycompleteFunc3_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000988 bw!
989
990 " Test for using a lambda function with incorrect return value
991 let Lambda = {s -> strlen(s)}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000992 let &completefunc = Lambda
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +0000993 new | only
994 call setline(1, 'eight')
995 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
996 bw!
997
998 " Test for clearing the 'completefunc' option
999 set completefunc=''
1000 set completefunc&
1001
1002 call assert_fails("set completefunc=function('abc')", "E700:")
1003 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001004 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001005 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1006
1007 " Vim9 tests
1008 let lines =<< trim END
1009 vim9script
1010
1011 # Test for using function()
1012 def MycompleteFunc1(findstart: number, base: string): any
1013 add(g:MycompleteFunc1_args, [findstart, base])
1014 return findstart ? 0 : []
1015 enddef
1016 set completefunc=function('MycompleteFunc1')
1017 new | only
1018 setline(1, 'one')
1019 g:MycompleteFunc1_args = []
1020 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1021 assert_equal([[1, ''], [0, 'one']], g:MycompleteFunc1_args)
1022 bw!
1023
1024 # Test for using a lambda
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001025 def LambdaComplete1(findstart: number, base: string): any
1026 add(g:LambdaComplete1_args, [findstart, base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001027 return findstart ? 0 : []
1028 enddef
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001029 &completefunc = (a, b) => LambdaComplete1(a, b)
1030 new | only
1031 setline(1, 'two')
1032 g:LambdaComplete1_args = []
1033 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1034 assert_equal([[1, ''], [0, 'two']], g:LambdaComplete1_args)
1035 bw!
1036
1037 # Test for using a string(lambda)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001038 &completefunc = '(a, b) => LambdaComplete1(a, b)'
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001039 new | only
1040 setline(1, 'two')
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001041 g:LambdaComplete1_args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001042 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001043 assert_equal([[1, ''], [0, 'two']], g:LambdaComplete1_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001044 bw!
1045
1046 # Test for using a variable with a lambda expression
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001047 var Fn: func = (findstart, base) => {
1048 add(g:LambdaComplete2_args, [findstart, base])
1049 return findstart ? 0 : []
1050 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001051 &completefunc = Fn
1052 new | only
1053 setline(1, 'three')
1054 g:LambdaComplete2_args = []
1055 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1056 assert_equal([[1, ''], [0, 'three']], g:LambdaComplete2_args)
1057 bw!
1058
1059 # Test for using a string(variable with a lambda expression)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001060 &completefunc = string(Fn)
1061 new | only
1062 setline(1, 'three')
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001063 g:LambdaComplete2_args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001064 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001065 assert_equal([[1, ''], [0, 'three']], g:LambdaComplete2_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001066 bw!
1067 END
1068 call CheckScriptSuccess(lines)
1069
1070 " Using Vim9 lambda expression in legacy context should fail
1071 set completefunc=(a,\ b)\ =>\ g:MycompleteFunc2(a,\ b)
1072 new | only
1073 let g:MycompleteFunc2_args = []
1074 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
1075 call assert_equal([], g:MycompleteFunc2_args)
1076
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001077 " set 'completefunc' to a non-existing function
1078 set completefunc=MycompleteFunc2
1079 call setline(1, 'five')
1080 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1081 call assert_fails("let &completefunc = function('NonExistingFunc')", 'E700:')
1082 let g:MycompleteFunc2_args = []
1083 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1084 call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args)
1085
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001086 " cleanup
1087 delfunc MycompleteFunc1
1088 delfunc MycompleteFunc2
1089 delfunc MycompleteFunc3
1090 set completefunc&
1091 %bw!
1092endfunc
1093
1094" Test for different ways of setting the 'omnifunc' option
1095func Test_omnifunc_callback()
1096 " Test for using a function()
1097 func MyomniFunc1(findstart, base)
1098 call add(g:MyomniFunc1_args, [a:findstart, a:base])
1099 return a:findstart ? 0 : []
1100 endfunc
1101 set omnifunc=function('MyomniFunc1')
1102 new | only
1103 call setline(1, 'one')
1104 let g:MyomniFunc1_args = []
1105 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1106 call assert_equal([[1, ''], [0, 'one']], g:MyomniFunc1_args)
1107 bw!
1108
1109 " Using a funcref variable to set 'omnifunc'
1110 let Fn = function('MyomniFunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001111 let &omnifunc = Fn
1112 new | only
1113 call setline(1, 'two')
1114 let g:MyomniFunc1_args = []
1115 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1116 call assert_equal([[1, ''], [0, 'two']], g:MyomniFunc1_args)
1117 bw!
1118
1119 " Using a string(funcref_variable) to set 'omnifunc'
1120 let Fn = function('MyomniFunc1')
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001121 let &omnifunc = string(Fn)
1122 new | only
1123 call setline(1, 'two')
1124 let g:MyomniFunc1_args = []
1125 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1126 call assert_equal([[1, ''], [0, 'two']], g:MyomniFunc1_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001127 bw!
1128
1129 " Test for using a funcref()
1130 func MyomniFunc2(findstart, base)
1131 call add(g:MyomniFunc2_args, [a:findstart, a:base])
1132 return a:findstart ? 0 : []
1133 endfunc
1134 set omnifunc=funcref('MyomniFunc2')
1135 new | only
1136 call setline(1, 'three')
1137 let g:MyomniFunc2_args = []
1138 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1139 call assert_equal([[1, ''], [0, 'three']], g:MyomniFunc2_args)
1140 bw!
1141
1142 " Using a funcref variable to set 'omnifunc'
1143 let Fn = funcref('MyomniFunc2')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001144 let &omnifunc = Fn
1145 new | only
1146 call setline(1, 'four')
1147 let g:MyomniFunc2_args = []
1148 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1149 call assert_equal([[1, ''], [0, 'four']], g:MyomniFunc2_args)
1150 bw!
1151
1152 " Using a string(funcref_variable) to set 'omnifunc'
1153 let Fn = funcref('MyomniFunc2')
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001154 let &omnifunc = string(Fn)
1155 new | only
1156 call setline(1, 'four')
1157 let g:MyomniFunc2_args = []
1158 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1159 call assert_equal([[1, ''], [0, 'four']], g:MyomniFunc2_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001160 bw!
1161
1162 " Test for using a lambda function
1163 func MyomniFunc3(findstart, base)
1164 call add(g:MyomniFunc3_args, [a:findstart, a:base])
1165 return a:findstart ? 0 : []
1166 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001167 set omnifunc={a,\ b\ ->\ MyomniFunc3(a,\ b)}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001168 new | only
1169 call setline(1, 'five')
1170 let g:MyomniFunc3_args = []
1171 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1172 call assert_equal([[1, ''], [0, 'five']], g:MyomniFunc3_args)
1173 bw!
1174
1175 " Set 'omnifunc' to a lambda expression
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001176 let &omnifunc = {a, b -> MyomniFunc3(a, b)}
1177 new | only
1178 call setline(1, 'six')
1179 let g:MyomniFunc3_args = []
1180 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1181 call assert_equal([[1, ''], [0, 'six']], g:MyomniFunc3_args)
1182 bw!
1183
1184 " Set 'omnifunc' to a string(lambda_expression)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001185 let &omnifunc = '{a, b -> MyomniFunc3(a, b)}'
1186 new | only
1187 call setline(1, 'six')
1188 let g:MyomniFunc3_args = []
1189 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1190 call assert_equal([[1, ''], [0, 'six']], g:MyomniFunc3_args)
1191 bw!
1192
1193 " Set 'omnifunc' to a variable with a lambda expression
1194 let Lambda = {a, b -> MyomniFunc3(a, b)}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001195 let &omnifunc = Lambda
1196 new | only
1197 call setline(1, 'seven')
1198 let g:MyomniFunc3_args = []
1199 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1200 call assert_equal([[1, ''], [0, 'seven']], g:MyomniFunc3_args)
1201 bw!
1202
1203 " Set 'omnifunc' to a string(variable with a lambda expression)
1204 let Lambda = {a, b -> MyomniFunc3(a, b)}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001205 let &omnifunc = string(Lambda)
1206 new | only
1207 call setline(1, 'seven')
1208 let g:MyomniFunc3_args = []
1209 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1210 call assert_equal([[1, ''], [0, 'seven']], g:MyomniFunc3_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001211 bw!
1212
1213 " Test for using a lambda function with incorrect return value
1214 let Lambda = {s -> strlen(s)}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001215 let &omnifunc = Lambda
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001216 new | only
1217 call setline(1, 'eight')
1218 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1219 bw!
1220
1221 " Test for clearing the 'omnifunc' option
1222 set omnifunc=''
1223 set omnifunc&
1224
1225 call assert_fails("set omnifunc=function('abc')", "E700:")
1226 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001227 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001228 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1229
1230 " Vim9 tests
1231 let lines =<< trim END
1232 vim9script
1233
1234 # Test for using function()
1235 def MyomniFunc1(findstart: number, base: string): any
1236 add(g:MyomniFunc1_args, [findstart, base])
1237 return findstart ? 0 : []
1238 enddef
1239 set omnifunc=function('MyomniFunc1')
1240 new | only
1241 setline(1, 'one')
1242 g:MyomniFunc1_args = []
1243 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1244 assert_equal([[1, ''], [0, 'one']], g:MyomniFunc1_args)
1245 bw!
1246
1247 # Test for using a lambda
1248 def MyomniFunc2(findstart: number, base: string): any
1249 add(g:MyomniFunc2_args, [findstart, base])
1250 return findstart ? 0 : []
1251 enddef
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001252 &omnifunc = (a, b) => MyomniFunc2(a, b)
1253 new | only
1254 setline(1, 'two')
1255 g:MyomniFunc2_args = []
1256 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1257 assert_equal([[1, ''], [0, 'two']], g:MyomniFunc2_args)
1258 bw!
1259
1260 # Test for using a string(lambda)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001261 &omnifunc = '(a, b) => MyomniFunc2(a, b)'
1262 new | only
1263 setline(1, 'two')
1264 g:MyomniFunc2_args = []
1265 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1266 assert_equal([[1, ''], [0, 'two']], g:MyomniFunc2_args)
1267 bw!
1268
1269 # Test for using a variable with a lambda expression
1270 var Fn: func = (a, b) => MyomniFunc2(a, b)
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001271 &omnifunc = Fn
1272 new | only
1273 setline(1, 'three')
1274 g:MyomniFunc2_args = []
1275 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1276 assert_equal([[1, ''], [0, 'three']], g:MyomniFunc2_args)
1277 bw!
1278
1279 # Test for using a string(variable with a lambda expression)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001280 &omnifunc = string(Fn)
1281 new | only
1282 setline(1, 'three')
1283 g:MyomniFunc2_args = []
1284 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1285 assert_equal([[1, ''], [0, 'three']], g:MyomniFunc2_args)
1286 bw!
1287 END
1288 call CheckScriptSuccess(lines)
1289
1290 " Using Vim9 lambda expression in legacy context should fail
1291 set omnifunc=(a,\ b)\ =>\ g:MyomniFunc2(a,\ b)
1292 new | only
1293 let g:MyomniFunc2_args = []
1294 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
1295 call assert_equal([], g:MyomniFunc2_args)
1296
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001297 " set 'omnifunc' to a non-existing function
1298 set omnifunc=MyomniFunc2
1299 call setline(1, 'nine')
1300 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1301 call assert_fails("let &omnifunc = function('NonExistingFunc')", 'E700:')
1302 let g:MyomniFunc2_args = []
1303 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1304 call assert_equal([[1, ''], [0, 'nine']], g:MyomniFunc2_args)
1305
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001306 " cleanup
1307 delfunc MyomniFunc1
1308 delfunc MyomniFunc2
1309 delfunc MyomniFunc3
1310 set omnifunc&
1311 %bw!
1312endfunc
1313
1314" Test for different ways of setting the 'thesaurusfunc' option
1315func Test_thesaurusfunc_callback()
1316 " Test for using a function()
1317 func MytsrFunc1(findstart, base)
1318 call add(g:MytsrFunc1_args, [a:findstart, a:base])
1319 return a:findstart ? 0 : []
1320 endfunc
1321 set thesaurusfunc=function('MytsrFunc1')
1322 new | only
1323 call setline(1, 'one')
1324 let g:MytsrFunc1_args = []
1325 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1326 call assert_equal([[1, ''], [0, 'one']], g:MytsrFunc1_args)
1327 bw!
1328
1329 " Using a funcref variable to set 'thesaurusfunc'
1330 let Fn = function('MytsrFunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001331 let &thesaurusfunc = Fn
1332 new | only
1333 call setline(1, 'two')
1334 let g:MytsrFunc1_args = []
1335 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1336 call assert_equal([[1, ''], [0, 'two']], g:MytsrFunc1_args)
1337 bw!
1338
1339 " Using a string(funcref_variable) to set 'thesaurusfunc'
1340 let Fn = function('MytsrFunc1')
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001341 let &thesaurusfunc = string(Fn)
1342 new | only
1343 call setline(1, 'two')
1344 let g:MytsrFunc1_args = []
1345 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1346 call assert_equal([[1, ''], [0, 'two']], g:MytsrFunc1_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001347 bw!
1348
1349 " Test for using a funcref()
1350 func MytsrFunc2(findstart, base)
1351 call add(g:MytsrFunc2_args, [a:findstart, a:base])
1352 return a:findstart ? 0 : []
1353 endfunc
1354 set thesaurusfunc=funcref('MytsrFunc2')
1355 new | only
1356 call setline(1, 'three')
1357 let g:MytsrFunc2_args = []
1358 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1359 call assert_equal([[1, ''], [0, 'three']], g:MytsrFunc2_args)
1360 bw!
1361
1362 " Using a funcref variable to set 'thesaurusfunc'
1363 let Fn = funcref('MytsrFunc2')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001364 let &thesaurusfunc = Fn
1365 new | only
1366 call setline(1, 'four')
1367 let g:MytsrFunc2_args = []
1368 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1369 call assert_equal([[1, ''], [0, 'four']], g:MytsrFunc2_args)
1370 bw!
1371
1372 " Using a string(funcref_variable) to set 'thesaurusfunc'
1373 let Fn = funcref('MytsrFunc2')
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001374 let &thesaurusfunc = string(Fn)
1375 new | only
1376 call setline(1, 'four')
1377 let g:MytsrFunc2_args = []
1378 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1379 call assert_equal([[1, ''], [0, 'four']], g:MytsrFunc2_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001380 bw!
1381
1382 " Test for using a lambda function
1383 func MytsrFunc3(findstart, base)
1384 call add(g:MytsrFunc3_args, [a:findstart, a:base])
1385 return a:findstart ? 0 : []
1386 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00001387 set thesaurusfunc={a,\ b\ ->\ MytsrFunc3(a,\ b)}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001388 new | only
1389 call setline(1, 'five')
1390 let g:MytsrFunc3_args = []
1391 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1392 call assert_equal([[1, ''], [0, 'five']], g:MytsrFunc3_args)
1393 bw!
1394
1395 " Set 'thesaurusfunc' to a lambda expression
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001396 let &thesaurusfunc = {a, b -> MytsrFunc3(a, b)}
1397 new | only
1398 call setline(1, 'six')
1399 let g:MytsrFunc3_args = []
1400 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1401 call assert_equal([[1, ''], [0, 'six']], g:MytsrFunc3_args)
1402 bw!
1403
1404 " Set 'thesaurusfunc' to a string(lambda expression)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001405 let &thesaurusfunc = '{a, b -> MytsrFunc3(a, b)}'
1406 new | only
1407 call setline(1, 'six')
1408 let g:MytsrFunc3_args = []
1409 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1410 call assert_equal([[1, ''], [0, 'six']], g:MytsrFunc3_args)
1411 bw!
1412
1413 " Set 'thesaurusfunc' to a variable with a lambda expression
1414 let Lambda = {a, b -> MytsrFunc3(a, b)}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001415 let &thesaurusfunc = Lambda
1416 new | only
1417 call setline(1, 'seven')
1418 let g:MytsrFunc3_args = []
1419 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1420 call assert_equal([[1, ''], [0, 'seven']], g:MytsrFunc3_args)
1421 bw!
1422
1423 " Set 'thesaurusfunc' to a string(variable with a lambda expression)
1424 let Lambda = {a, b -> MytsrFunc3(a, b)}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001425 let &thesaurusfunc = string(Lambda)
1426 new | only
1427 call setline(1, 'seven')
1428 let g:MytsrFunc3_args = []
1429 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1430 call assert_equal([[1, ''], [0, 'seven']], g:MytsrFunc3_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001431 bw!
1432
1433 " Test for using a lambda function with incorrect return value
1434 let Lambda = {s -> strlen(s)}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001435 let &thesaurusfunc = Lambda
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001436 new | only
1437 call setline(1, 'eight')
1438 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1439 bw!
1440
1441 " Test for clearing the 'thesaurusfunc' option
1442 set thesaurusfunc=''
1443 set thesaurusfunc&
1444
1445 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1446 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001447 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001448 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1449
1450 " Vim9 tests
1451 let lines =<< trim END
1452 vim9script
1453
1454 # Test for using function()
1455 def MytsrFunc1(findstart: number, base: string): any
1456 add(g:MytsrFunc1_args, [findstart, base])
1457 return findstart ? 0 : []
1458 enddef
1459 set thesaurusfunc=function('MytsrFunc1')
1460 new | only
1461 setline(1, 'one')
1462 g:MytsrFunc1_args = []
1463 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1464 assert_equal([[1, ''], [0, 'one']], g:MytsrFunc1_args)
1465 bw!
1466
1467 # Test for using a lambda
1468 def MytsrFunc2(findstart: number, base: string): any
1469 add(g:MytsrFunc2_args, [findstart, base])
1470 return findstart ? 0 : []
1471 enddef
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001472 &thesaurusfunc = (a, b) => MytsrFunc2(a, b)
1473 new | only
1474 setline(1, 'two')
1475 g:MytsrFunc2_args = []
1476 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1477 assert_equal([[1, ''], [0, 'two']], g:MytsrFunc2_args)
1478 bw!
1479
1480 # Test for using a string(lambda)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001481 &thesaurusfunc = '(a, b) => MytsrFunc2(a, b)'
1482 new | only
1483 setline(1, 'two')
1484 g:MytsrFunc2_args = []
1485 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1486 assert_equal([[1, ''], [0, 'two']], g:MytsrFunc2_args)
1487 bw!
1488
1489 # Test for using a variable with a lambda expression
1490 var Fn: func = (a, b) => MytsrFunc2(a, b)
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001491 &thesaurusfunc = Fn
1492 new | only
1493 setline(1, 'three')
1494 g:MytsrFunc2_args = []
1495 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1496 assert_equal([[1, ''], [0, 'three']], g:MytsrFunc2_args)
1497 bw!
1498
1499 # Test for using a string(variable with a lambda expression)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001500 &thesaurusfunc = string(Fn)
1501 new | only
1502 setline(1, 'three')
1503 g:MytsrFunc2_args = []
1504 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1505 assert_equal([[1, ''], [0, 'three']], g:MytsrFunc2_args)
1506 bw!
1507 END
1508 call CheckScriptSuccess(lines)
1509
1510 " Using Vim9 lambda expression in legacy context should fail
1511 set thesaurusfunc=(a,\ b)\ =>\ g:MytsrFunc2(a,\ b)
1512 new | only
1513 let g:MytsrFunc2_args = []
1514 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
1515 call assert_equal([], g:MytsrFunc2_args)
1516 bw!
1517
1518 " Use a buffer-local value and a global value
1519 func MytsrFunc4(findstart, base)
1520 call add(g:MytsrFunc4_args, [a:findstart, a:base])
1521 return a:findstart ? 0 : ['sunday']
1522 endfunc
1523 set thesaurusfunc&
1524 setlocal thesaurusfunc=function('MytsrFunc4')
1525 call setline(1, 'sun')
1526 let g:MytsrFunc4_args = []
1527 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1528 call assert_equal('sunday', getline(1))
1529 call assert_equal([[1, ''], [0, 'sun']], g:MytsrFunc4_args)
1530 new
1531 call setline(1, 'sun')
1532 let g:MytsrFunc4_args = []
1533 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1534 call assert_equal('sun', getline(1))
1535 call assert_equal([], g:MytsrFunc4_args)
1536 set thesaurusfunc=function('MytsrFunc1')
1537 wincmd w
1538 call setline(1, 'sun')
1539 let g:MytsrFunc4_args = []
1540 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1541 call assert_equal('sunday', getline(1))
1542 call assert_equal([[1, ''], [0, 'sun']], g:MytsrFunc4_args)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001543 %bw!
1544
1545 " set 'thesaurusfunc' to a non-existing function
1546 set thesaurusfunc=MytsrFunc2
1547 call setline(1, 'ten')
1548 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1549 call assert_fails("let &thesaurusfunc = function('NonExistingFunc')", 'E700:')
1550 let g:MytsrFunc2_args = []
1551 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1552 call assert_equal([[1, ''], [0, 'ten']], g:MytsrFunc2_args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001553
1554 " cleanup
1555 set thesaurusfunc&
1556 delfunc MytsrFunc1
1557 delfunc MytsrFunc2
1558 delfunc MytsrFunc3
1559 delfunc MytsrFunc4
1560 %bw!
1561endfunc
1562
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001563" vim: shiftwidth=2 sts=2 expandtab