blob: 86ad6098c9d6f09a72bdabdd5c1af1575fda5cfe [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005source vim9.vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
12 call mkdir('Xdir')
13 cd Xdir
14
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
74 set cpt=w
75 " checks make_cyclic in other window
76 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
77 call assert_equal('STARTTEST', getline('.'))
78
79 set cpt=u nohid
80 " checks unloaded buffer expansion
81 only
82 exe "normal oEN\<C-N>"
83 call assert_equal('ENDTEST', getline('.'))
84 " checks adding mode abortion
85 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
86 call assert_equal('unless', getline('.'))
87
88 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
89 " tag expansion, define add-expansion interrupted
90 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
91 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
92 " t-expansion
93 exe "normal oa\<C-N>\<Esc>"
94 call assert_equal('asd', getline('.'))
95
96 %bw!
97 call delete('Xtestfile')
98 call delete('Xtest11.one')
99 call delete('Xtest11.two')
100 call delete('Xtestdata')
101 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100102 cd ..
103 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200104endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100105
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000106func Test_ins_complete_invalid_byte()
107 if has('unix') && executable('base64')
108 " this weird command was causing an illegal memory access
109 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
110 call system('base64 -d Xinvalid64 > Xinvalid')
111 call writefile(['qa!'], 'Xexit')
112 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
113 call delete('Xinvalid64')
114 call delete('Xinvalid')
115 call delete('Xexit')
116 endif
117endfunc
118
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100119func Test_omni_dash()
120 func Omni(findstart, base)
121 if a:findstart
122 return 5
123 else
124 echom a:base
125 return ['-help', '-v']
126 endif
127 endfunc
128 set omnifunc=Omni
129 new
130 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100131 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100132
133 bwipe!
134 delfunc Omni
135 set omnifunc=
136endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100137
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200138func Test_omni_autoload()
139 let save_rtp = &rtp
140 set rtp=Xruntime/some
141 let dir = 'Xruntime/some/autoload'
142 call mkdir(dir, 'p')
143
144 let lines =<< trim END
145 vim9script
146 def omni#func(findstart: bool, base: string): any
147 if findstart
148 return 1
149 else
150 return ['match']
151 endif
152 enddef
153 {
154 eval 1 + 2
155 }
156 END
157 call writefile(lines, dir .. '/omni.vim')
158
159 new
160 setlocal omnifunc=omni#func
161 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
162
163 bwipe!
164 call delete('Xruntime', 'rf')
165 set omnifunc=
166 let &rtp = save_rtp
167endfunc
168
Bram Moolenaarffa96842018-06-12 22:05:14 +0200169func Test_completefunc_args()
170 let s:args = []
171 func! CompleteFunc(findstart, base)
172 let s:args += [[a:findstart, empty(a:base)]]
173 endfunc
174 new
175
176 set completefunc=CompleteFunc
177 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200178 call assert_equal([1, 1], s:args[0])
179 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200180 set completefunc=
181
182 let s:args = []
183 set omnifunc=CompleteFunc
184 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200185 call assert_equal([1, 1], s:args[0])
186 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200187 set omnifunc=
188
189 bwipe!
190 unlet s:args
191 delfunc CompleteFunc
192endfunc
193
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100194func s:CompleteDone_CompleteFuncNone( findstart, base )
195 if a:findstart
196 return 0
197 endif
198
199 return v:none
200endfunc
201
Bram Moolenaar1e115362019-01-09 23:01:02 +0100202func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100203 if a:findstart
204 return 0
205 endif
206
207 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100208 \ 'words': [
209 \ {
210 \ 'word': 'aword',
211 \ 'abbr': 'wrd',
212 \ 'menu': 'extra text',
213 \ 'info': 'words are cool',
214 \ 'kind': 'W',
215 \ 'user_data': 'test'
216 \ }
217 \ ]
218 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100219endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100220
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100221func s:CompleteDone_CheckCompletedItemNone()
222 let s:called_completedone = 1
223endfunc
224
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100225func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100226 call assert_equal( 'aword', v:completed_item[ 'word' ] )
227 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
228 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
229 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
230 call assert_equal( 'W', v:completed_item[ 'kind' ] )
231 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
232
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100233 if a:pre
234 call assert_equal('function', complete_info().mode)
235 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100236
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100237 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100238endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100239
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100240func Test_CompleteDoneNone()
241 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200242 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100243
244 set completefunc=<SID>CompleteDone_CompleteFuncNone
245 execute "normal a\<C-X>\<C-U>\<C-Y>"
246 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200247 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100248
249 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200250 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100251
252 let s:called_completedone = 0
253 au! CompleteDone
254endfunc
255
256func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100257 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
258 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100259
260 set completefunc=<SID>CompleteDone_CompleteFuncDict
261 execute "normal a\<C-X>\<C-U>\<C-Y>"
262 set completefunc&
263
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100264 call assert_equal('test', v:completed_item[ 'user_data' ])
265 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100266
267 let s:called_completedone = 0
268 au! CompleteDone
269endfunc
270
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100271func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100272 if a:findstart
273 return 0
274 endif
275
276 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100277 \ 'words': [
278 \ {
279 \ 'word': 'aword',
280 \ 'abbr': 'wrd',
281 \ 'menu': 'extra text',
282 \ 'info': 'words are cool',
283 \ 'kind': 'W',
284 \ 'user_data': ['one', 'two'],
285 \ }
286 \ ]
287 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100288endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100289
Bram Moolenaar1e115362019-01-09 23:01:02 +0100290func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100291 call assert_equal( 'aword', v:completed_item[ 'word' ] )
292 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
293 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
294 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
295 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100296 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100297
298 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100299endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100300
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100301func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100302 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
303
304 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
305 execute "normal a\<C-X>\<C-U>\<C-Y>"
306 set completefunc&
307
Bram Moolenaar08928322020-01-04 14:32:48 +0100308 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100309 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100310
311 let s:called_completedone = 0
312 au! CompleteDone
313endfunc
314
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100315func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100316 if a:findstart
317 return 0
318 endif
319
320 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100321endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100322
Bram Moolenaar1e115362019-01-09 23:01:02 +0100323func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100324 call assert_equal( 'aword', v:completed_item[ 'word' ] )
325 call assert_equal( '', v:completed_item[ 'abbr' ] )
326 call assert_equal( '', v:completed_item[ 'menu' ] )
327 call assert_equal( '', v:completed_item[ 'info' ] )
328 call assert_equal( '', v:completed_item[ 'kind' ] )
329 call assert_equal( '', v:completed_item[ 'user_data' ] )
330
331 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100332endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100333
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100334func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100335 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
336
337 set completefunc=<SID>CompleteDone_CompleteFuncList
338 execute "normal a\<C-X>\<C-U>\<C-Y>"
339 set completefunc&
340
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100341 call assert_equal('', v:completed_item[ 'user_data' ])
342 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100343
344 let s:called_completedone = 0
345 au! CompleteDone
346endfunc
347
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200348func Test_CompleteDone_undo()
349 au CompleteDone * call append(0, "prepend1")
350 new
351 call setline(1, ["line1", "line2"])
352 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
353 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
354 \ getline(1, '$'))
355 undo
356 call assert_equal(["line1", "line2"], getline(1, '$'))
357 bwipe!
358 au! CompleteDone
359endfunc
360
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200361func CompleteTest(findstart, query)
362 if a:findstart
363 return col('.')
364 endif
365 return ['matched']
366endfunc
367
368func Test_completefunc_info()
369 new
370 set completeopt=menuone
371 set completefunc=CompleteTest
372 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100373 call assert_equal("matched{'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}", getline(1))
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200374 bwipe!
375 set completeopt&
376 set completefunc&
377endfunc
378
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100379" Check that when using feedkeys() typeahead does not interrupt searching for
380" completions.
381func Test_compl_feedkeys()
382 new
383 set completeopt=menuone,noselect
384 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
385 call assert_equal("jump jump", getline(1))
386 bwipe!
387 set completeopt&
388endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200389
mityua1198122021-11-20 19:13:39 +0000390func s:ComplInCmdwin_GlobalCompletion(a, l, p)
391 return 'global'
392endfunc
393
394func s:ComplInCmdwin_LocalCompletion(a, l, p)
395 return 'local'
396endfunc
397
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200398func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100399 CheckFeature cmdwin
400
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200401 set wildmenu wildchar=<Tab>
402 com! -nargs=1 -complete=command GetInput let input = <q-args>
403 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100404 let w:test_winvar = 'winvar'
405 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200406
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100407 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200408 let input = ''
409 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
410 call assert_equal('TestCommand', input)
411
412 let input = ''
413 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
414 call assert_equal('T', input)
415
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100416
417 com! -nargs=1 -complete=var GetInput let input = <q-args>
418 " Window-local variables
419 let input = ''
420 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
421 call assert_equal('w:test_winvar', input)
422
423 let input = ''
424 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
425 call assert_equal('w:test_', input)
426
427 " Buffer-local variables
428 let input = ''
429 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
430 call assert_equal('b:test_bufvar', input)
431
432 let input = ''
433 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
434 call assert_equal('b:test_', input)
435
mityua1198122021-11-20 19:13:39 +0000436
437 " Argument completion of buffer-local command
438 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
439 return ['global']
440 endfunc
441
442 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
443 return ['local']
444 endfunc
445
446 func s:ComplInCmdwin_CheckCompletion(arg)
447 call assert_equal('local', a:arg)
448 endfunc
449
450 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
451 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
452 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
453 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
454 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
455
456 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
457 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
458 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
459 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
460
461 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
462
463 func! s:ComplInCmdwin_CheckCompletion(arg)
464 call assert_equal('global', a:arg)
465 endfunc
466 new
467 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
468 quit
469
470 delfunc s:ComplInCmdwin_GlobalCompletion
471 delfunc s:ComplInCmdwin_LocalCompletion
472 delfunc s:ComplInCmdwin_GlobalCompletionList
473 delfunc s:ComplInCmdwin_LocalCompletionList
474 delfunc s:ComplInCmdwin_CheckCompletion
475
476 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200477 delcom TestCommand
478 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100479 unlet w:test_winvar
480 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200481 set wildmenu& wildchar&
482endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200483
484" Test for insert path completion with completeslash option
485func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200486 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200487
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200488 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200489 let orig_shellslash = &shellslash
490 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200491 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200492
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200493 set noshellslash
494
495 set completeslash=
496 exe "normal oXd\<C-X>\<C-F>"
497 call assert_equal('Xdir\', getline('.'))
498
499 set completeslash=backslash
500 exe "normal oXd\<C-X>\<C-F>"
501 call assert_equal('Xdir\', getline('.'))
502
503 set completeslash=slash
504 exe "normal oXd\<C-X>\<C-F>"
505 call assert_equal('Xdir/', getline('.'))
506
507 set shellslash
508
509 set completeslash=
510 exe "normal oXd\<C-X>\<C-F>"
511 call assert_equal('Xdir/', getline('.'))
512
513 set completeslash=backslash
514 exe "normal oXd\<C-X>\<C-F>"
515 call assert_equal('Xdir\', getline('.'))
516
517 set completeslash=slash
518 exe "normal oXd\<C-X>\<C-F>"
519 call assert_equal('Xdir/', getline('.'))
520 %bw!
521 call delete('Xdir', 'rf')
522
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200523 set noshellslash
524 set completeslash=slash
525 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
526
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200527 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200528 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200529endfunc
530
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200531func Test_pum_stopped_by_timer()
532 CheckScreendump
533
534 let lines =<< trim END
535 call setline(1, ['hello', 'hullo', 'heeee', ''])
536 func StartCompl()
537 call timer_start(100, { -> execute('stopinsert') })
538 call feedkeys("Gah\<C-N>")
539 endfunc
540 END
541
542 call writefile(lines, 'Xpumscript')
543 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
544 call term_sendkeys(buf, ":call StartCompl()\<CR>")
545 call TermWait(buf, 200)
546 call term_sendkeys(buf, "k")
547 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
548
549 call StopVimInTerminal(buf)
550 call delete('Xpumscript')
551endfunc
552
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100553func Test_pum_with_folds_two_tabs()
554 CheckScreendump
555
556 let lines =<< trim END
557 set fdm=marker
558 call setline(1, ['" x {{{1', '" a some text'])
559 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
560 norm! zm
561 tab sp
562 call feedkeys('2Gzv', 'xt')
563 call feedkeys("0fa", 'xt')
564 END
565
566 call writefile(lines, 'Xpumscript')
567 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200568 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100569 call term_sendkeys(buf, "a\<C-N>")
570 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
571
572 call term_sendkeys(buf, "\<Esc>")
573 call StopVimInTerminal(buf)
574 call delete('Xpumscript')
575endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100576
577func Test_pum_with_preview_win()
578 CheckScreendump
579
580 let lines =<< trim END
581 funct Omni_test(findstart, base)
582 if a:findstart
583 return col(".") - 1
584 endif
585 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
586 endfunc
587 set omnifunc=Omni_test
588 set completeopt+=longest
589 END
590
591 call writefile(lines, 'Xpreviewscript')
592 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200593 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100594 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200595 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100596 call term_sendkeys(buf, "\<C-N>")
597 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
598
599 call term_sendkeys(buf, "\<Esc>")
600 call StopVimInTerminal(buf)
601 call delete('Xpreviewscript')
602endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100603
604" Test for inserting the tag search pattern in insert mode
605func Test_ins_compl_tag_sft()
606 call writefile([
607 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
608 \ "first\tXfoo\t/^int first() {}$/",
609 \ "second\tXfoo\t/^int second() {}$/",
610 \ "third\tXfoo\t/^int third() {}$/"],
611 \ 'Xtags')
612 set tags=Xtags
613 let code =<< trim [CODE]
614 int first() {}
615 int second() {}
616 int third() {}
617 [CODE]
618 call writefile(code, 'Xfoo')
619
620 enew
621 set showfulltag
622 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
623 call assert_equal('int second() {}', getline(1))
624 set noshowfulltag
625
626 call delete('Xtags')
627 call delete('Xfoo')
628 set tags&
629 %bwipe!
630endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200631
632" Test for 'completefunc' deleting text
633func Test_completefunc_error()
634 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200635 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200636 func CompleteFunc(findstart, base)
637 if a:findstart == 1
638 normal dd
639 return col('.') - 1
640 endif
641 return ['a', 'b']
642 endfunc
643 set completefunc=CompleteFunc
644 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100645 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200646
647 " delete text when called for the second time
648 func CompleteFunc2(findstart, base)
649 if a:findstart == 1
650 return col('.') - 1
651 endif
652 normal dd
653 return ['a', 'b']
654 endfunc
655 set completefunc=CompleteFunc2
656 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200657 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200658
Bram Moolenaar97202d92021-01-28 18:34:35 +0100659 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100660 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100661 if a:findstart == 1
662 return col('.') - 1
663 endif
664 wincmd p
665 return ['a', 'b']
666 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100667 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100668 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100669 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100670 close!
671
672 set completefunc&
673 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100674 delfunc CompleteFunc2
675 delfunc CompleteFunc3
676 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100677endfunc
678
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200679" Test for returning non-string values from 'completefunc'
680func Test_completefunc_invalid_data()
681 new
682 func! CompleteFunc(findstart, base)
683 if a:findstart == 1
684 return col('.') - 1
685 endif
686 return [{}, '', 'moon']
687 endfunc
688 set completefunc=CompleteFunc
689 exe "normal i\<C-X>\<C-U>"
690 call assert_equal('moon', getline(1))
691 set completefunc&
692 close!
693endfunc
694
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200695" Test for errors in using complete() function
696func Test_complete_func_error()
697 call assert_fails('call complete(1, ["a"])', 'E785:')
698 func ListColors()
699 call complete(col('.'), "blue")
700 endfunc
701 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
702 func ListMonths()
703 call complete(col('.'), test_null_list())
704 endfunc
705 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
706 delfunc ListColors
707 delfunc ListMonths
708 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200709 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200710endfunc
711
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000712" Test for recursively starting completion mode using complete()
713func Test_recursive_complete_func()
714 func ListColors()
715 call complete(5, ["red", "blue"])
716 return ''
717 endfunc
718 new
719 call setline(1, ['a1', 'a2'])
720 set complete=.
721 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
722 call assert_equal('a2blue', getline(3))
723 delfunc ListColors
724 bw!
725endfunc
726
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200727" Test for completing words following a completed word in a line
728func Test_complete_wrapscan()
729 " complete words from another buffer
730 new
731 call setline(1, ['one two', 'three four'])
732 new
733 setlocal complete=w
734 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
735 call assert_equal('two three four', getline(1))
736 close!
737 " complete words from the current buffer
738 setlocal complete=.
739 %d
740 call setline(1, ['one two', ''])
741 call cursor(2, 1)
742 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
743 call assert_equal('one two one two', getline(2))
744 close!
745endfunc
746
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200747" Test for completing special characters
748func Test_complete_special_chars()
749 new
750 call setline(1, 'int .*[-\^$ func float')
751 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
752 call assert_equal('int .*[-\^$ func float', getline(2))
753 close!
754endfunc
755
756" Test for completion when text is wrapped across lines.
757func Test_complete_across_line()
758 new
759 call setline(1, ['red green blue', 'one two three'])
760 setlocal textwidth=20
761 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
762 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
763 close!
764endfunc
765
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000766" Test for completing words with a '.' at the end of a word.
767func Test_complete_joinspaces()
768 new
769 call setline(1, ['one two.', 'three. four'])
770 set joinspaces
771 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
772 call assert_equal("one two. three. four", getline(3))
773 set joinspaces&
774 bw!
775endfunc
776
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200777" Test for using CTRL-L to add one character when completing matching
778func Test_complete_add_onechar()
779 new
780 call setline(1, ['wool', 'woodwork'])
781 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
782 call assert_equal('woof', getline(3))
783
784 " use 'ignorecase' and backspace to erase characters from the prefix string
785 " and then add letters using CTRL-L
786 %d
787 set ignorecase backspace=2
788 setlocal complete=.
789 call setline(1, ['workhorse', 'workload'])
790 normal Go
791 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
792 call assert_equal('workh', getline(3))
793 set ignorecase& backspace&
794 close!
795endfunc
796
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000797" Test for using CTRL-X CTRL-L to complete whole lines lines
798func Test_complete_wholeline()
799 new
800 " complete one-line
801 call setline(1, ['a1', 'a2'])
802 exe "normal ggoa\<C-X>\<C-L>"
803 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
804 " go to the next match (wrapping around the buffer)
805 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
806 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
807 " go to the next match
808 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
809 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
810 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
811 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
812 " repeat the test using CTRL-L
813 " go to the next match (wrapping around the buffer)
814 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
815 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
816 " go to the next match
817 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
818 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
819 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
820 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
821 %d
822 " use CTRL-X CTRL-L to add one more line
823 call setline(1, ['a1', 'b1'])
824 setlocal complete=.
825 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
826 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
827 bw!
828endfunc
829
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200830" Test insert completion with 'cindent' (adjust the indent)
831func Test_complete_with_cindent()
832 new
833 setlocal cindent
834 call setline(1, ['if (i == 1)', " j = 2;"])
835 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
836 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
837
838 %d
839 call setline(1, ['when while', '{', ''])
840 setlocal cinkeys+==while
841 exe "normal Giwh\<C-P> "
842 call assert_equal("\twhile ", getline('$'))
843 close!
844endfunc
845
846" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
847func Test_complete_cmdline()
848 new
849 exe "normal icaddb\<C-X>\<C-V>"
850 call assert_equal('caddbuffer', getline(1))
851 exe "normal ocall getqf\<C-X>\<C-V>"
852 call assert_equal('call getqflist(', getline(2))
853 exe "normal oabcxyz(\<C-X>\<C-V>"
854 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200855 com! -buffer TestCommand1 echo 'TestCommand1'
856 com! -buffer TestCommand2 echo 'TestCommand2'
857 write TestCommand1Test
858 write TestCommand2Test
859 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
860 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
861 call assert_equal('TestCommand2Test', getline(4))
862 call delete('TestCommand1Test')
863 call delete('TestCommand2Test')
864 delcom TestCommand1
865 delcom TestCommand2
866 close!
867endfunc
868
869" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
870func Test_complete_stop()
871 new
872 func Save_mode1()
873 let g:mode1 = mode(1)
874 return ''
875 endfunc
876 func Save_mode2()
877 let g:mode2 = mode(1)
878 return ''
879 endfunc
880 inoremap <F1> <C-R>=Save_mode1()<CR>
881 inoremap <F2> <C-R>=Save_mode2()<CR>
882 call setline(1, ['aaa bbb ccc '])
883 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
884 call assert_equal('ic', g:mode1)
885 call assert_equal('i', g:mode2)
886 call assert_equal('aaa bbb ccc ', getline(1))
887 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
888 call assert_equal('ic', g:mode1)
889 call assert_equal('i', g:mode2)
890 call assert_equal('aaa bbb ccc aaa', getline(1))
891 set completeopt+=noselect
892 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
893 call assert_equal('ic', g:mode1)
894 call assert_equal('i', g:mode2)
895 call assert_equal('aaa bbb ccc aaa bb', getline(1))
896 set completeopt&
897 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
898 call assert_equal('ic', g:mode1)
899 call assert_equal('i', g:mode2)
900 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
901 com! -buffer TestCommand1 echo 'TestCommand1'
902 com! -buffer TestCommand2 echo 'TestCommand2'
903 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
904 call assert_equal('ic', g:mode1)
905 call assert_equal('i', g:mode2)
906 call assert_equal('TestCommand2', getline(2))
907 delcom TestCommand1
908 delcom TestCommand2
909 unlet g:mode1
910 unlet g:mode2
911 iunmap <F1>
912 iunmap <F2>
913 delfunc Save_mode1
914 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200915 close!
916endfunc
917
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000918" Test for typing CTRL-R in insert completion mode to insert a register
919" content.
920func Test_complete_reginsert()
921 new
922 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
923
924 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
925 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
926 " inserted.
927 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
928 call assert_equal('a123', getline(5))
929 let @r = "\<C-P>\<C-P>"
930 exe "normal GCa\<C-P>\<C-R>r"
931 call assert_equal('a12', getline(5))
932 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
933 call assert_equal('a1234x', getline(5))
934 bw!
935endfunc
936
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200937func Test_issue_7021()
938 CheckMSWindows
939
940 let orig_shellslash = &shellslash
941 set noshellslash
942
943 set completeslash=slash
944 call assert_false(expand('~') =~ '/')
945
946 let &shellslash = orig_shellslash
947 set completeslash=
948endfunc
949
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000950" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
951func Test_complete_longest_match()
952 for e in ['latin1', 'utf-8']
953 exe 'set encoding=' .. e
954 new
955 set complete=.
956 set completeopt=menu,longest
957 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
958 exe "normal Gopfx\<C-P>"
959 call assert_equal('pfx_', getline(5))
960 bw!
961 endfor
962
963 " Test for completing additional words with longest match set
964 new
965 call setline(1, ['abc1', 'abd2'])
966 exe "normal Goab\<C-P>\<C-X>\<C-P>"
967 call assert_equal('ab', getline(3))
968 bw!
969 set complete& completeopt&
970endfunc
971
972" Test for removing the first displayed completion match and selecting the
973" match just before that.
974func Test_complete_erase_firstmatch()
975 new
976 call setline(1, ['a12', 'a34', 'a56'])
977 set complete=.
978 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
979 call assert_equal('a34', getline('$'))
980 set complete&
981 bw!
982endfunc
983
984" Test for completing whole lines from unloaded buffers
985func Test_complete_wholeline_unloadedbuf()
986 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
987 edit Xfile1
988 enew
989 set complete=u
990 exe "normal! ia\<C-X>\<C-L>\<C-P>"
991 call assert_equal('a line2', getline(1))
992 %d
993 " completing from an unlisted buffer should fail
994 bdel Xfile1
995 exe "normal! ia\<C-X>\<C-L>\<C-P>"
996 call assert_equal('a', getline(1))
997 set complete&
998 %bw!
999 call delete("Xfile1")
1000endfunc
1001
1002" Test for completing whole lines from unlisted buffers
1003func Test_complete_wholeline_unlistedbuf()
1004 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1005 edit Xfile1
1006 enew
1007 set complete=U
1008 " completing from a unloaded buffer should fail
1009 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1010 call assert_equal('a', getline(1))
1011 %d
1012 bdel Xfile1
1013 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1014 call assert_equal('a line2', getline(1))
1015 set complete&
1016 %bw!
1017 call delete("Xfile1")
1018endfunc
1019
1020" Test for adding a multibyte character using CTRL-L in completion mode
1021func Test_complete_mbyte_char_add()
1022 new
1023 set complete=.
1024 call setline(1, 'abÄ—')
1025 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1026 call assert_equal('abÄ—', getline(2))
1027 " Test for a leader with multibyte character
1028 %d
1029 call setline(1, 'abÄ—Ä•')
1030 exe "normal! oabÄ—\<C-P>"
1031 call assert_equal('abÄ—Ä•', getline(2))
1032 bw!
1033endfunc
1034
Bram Moolenaarcc233582020-12-12 13:32:07 +01001035" Test to ensure 'Scanning...' messages are not recorded in messages history
1036func Test_z1_complete_no_history()
1037 new
1038 messages clear
1039 let currmess = execute('messages')
1040 setlocal dictionary=README.txt
1041 exe "normal owh\<C-X>\<C-K>"
1042 exe "normal owh\<C-N>"
1043 call assert_equal(currmess, execute('messages'))
1044 close!
1045endfunc
1046
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001047" Test for different ways of setting the 'completefunc' option
1048func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001049 func CompleteFunc1(callnr, findstart, base)
1050 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1051 return a:findstart ? 0 : []
1052 endfunc
1053 func CompleteFunc2(findstart, base)
1054 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001055 return a:findstart ? 0 : []
1056 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001057
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001058 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001059 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001060 LET &completefunc = 'g:CompleteFunc2'
1061 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001062 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001063 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001064 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001065 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001066 bw!
1067
1068 #" Test for using a function()
1069 set completefunc=function('g:CompleteFunc1',\ [10])
1070 new
1071 call setline(1, 'one')
1072 LET g:CompleteFunc1Args = []
1073 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1074 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001075 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001076
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001077 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001078 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001079 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001080 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001081 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001082 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001083 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001084 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001085 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001086
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001087 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001088 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001089 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001090 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001091 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001092 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001093 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001094 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001095 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001096
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001097 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001098 set completefunc=funcref('g:CompleteFunc1',\ [13])
1099 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001100 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001101 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001102 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001103 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001104 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001105
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001106 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001107 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001108 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001109 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001110 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001111 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001112 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001113 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001114 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001115
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001116 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001117 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001118 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001119 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001120 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001121 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001122 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001123 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001124 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001125
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001126 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001127 VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001128 LET optval = substitute(optval, ' ', '\\ ', 'g')
1129 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001130 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001131 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001132 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001133 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001134 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001135 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001136
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001137 #" Set 'completefunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001138 LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND
1139 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001140 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001141 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001142 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001143 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001144 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001145
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001146 #" Set 'completefunc' to string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001147 LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND'
1148 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001149 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001150 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001151 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001152 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001153 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001154
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001155 #" Set 'completefunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001156 VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001157 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001158 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001159 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001160 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001161 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001162 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001163 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001164
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001165 #" Set 'completefunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001166 LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001167 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001168 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001169 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001170 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001171 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001172 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001173 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001174
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001175 #" Test for using a lambda function with incorrect return value
1176 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1177 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001178 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001179 call setline(1, 'eight')
1180 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1181 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001182
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001183 #" Test for clearing the 'completefunc' option
1184 set completefunc=''
1185 set completefunc&
1186 call assert_fails("set completefunc=function('abc')", "E700:")
1187 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001188
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001189 #" set 'completefunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001190 set completefunc=CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001191 call setline(1, 'five')
1192 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1193 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001194 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001195 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001196 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001197 bw!
1198 END
1199 call CheckLegacyAndVim9Success(lines)
1200
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001201 " Test for using a script-local function name
1202 func s:CompleteFunc3(findstart, base)
1203 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1204 return a:findstart ? 0 : []
1205 endfunc
1206 set completefunc=s:CompleteFunc3
1207 new
1208 call setline(1, 'script1')
1209 let g:CompleteFunc3Args = []
1210 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1211 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1212 bw!
1213
1214 let &completefunc = 's:CompleteFunc3'
1215 new
1216 call setline(1, 'script2')
1217 let g:CompleteFunc3Args = []
1218 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1219 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1220 bw!
1221 delfunc s:CompleteFunc3
1222
1223 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001224 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001225 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1226
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001227 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001228 set completefunc=(a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001229 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001230 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001231 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001232 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001233
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001234 " set 'completefunc' to a partial with dict. This used to cause a crash.
1235 func SetCompleteFunc()
1236 let params = {'complete': function('g:DictCompleteFunc')}
1237 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001238 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001239 func g:DictCompleteFunc(_) dict
1240 endfunc
1241 call SetCompleteFunc()
1242 new
1243 call SetCompleteFunc()
1244 bw
1245 call test_garbagecollect_now()
1246 new
1247 set completefunc=
1248 wincmd w
1249 set completefunc=
1250 %bw!
1251 delfunc g:DictCompleteFunc
1252 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001253
1254 " Vim9 tests
1255 let lines =<< trim END
1256 vim9script
1257
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001258 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1259 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001260 return findstart ? 0 : []
1261 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001262
1263 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001264 set completefunc=function('Vim9CompleteFunc',\ [60])
1265 new | only
1266 setline(1, 'one')
1267 g:Vim9completeFuncArgs = []
1268 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1269 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1270 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001271
1272 # Test for using a global function name
1273 &completefunc = g:CompleteFunc2
1274 new | only
1275 setline(1, 'two')
1276 g:CompleteFunc2Args = []
1277 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1278 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1279 bw!
1280
1281 # Test for using a script-local function name
1282 def s:LocalCompleteFunc(findstart: number, base: string): any
1283 add(g:LocalCompleteFuncArgs, [findstart, base])
1284 return findstart ? 0 : []
1285 enddef
1286 &completefunc = s:LocalCompleteFunc
1287 new | only
1288 setline(1, 'three')
1289 g:LocalCompleteFuncArgs = []
1290 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1291 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1292 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001293 END
1294 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001295
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001296 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001297 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001298 delfunc CompleteFunc1
1299 delfunc CompleteFunc2
1300 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001301 %bw!
1302endfunc
1303
1304" Test for different ways of setting the 'omnifunc' option
1305func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001306 func OmniFunc1(callnr, findstart, base)
1307 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1308 return a:findstart ? 0 : []
1309 endfunc
1310 func OmniFunc2(findstart, base)
1311 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001312 return a:findstart ? 0 : []
1313 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001314
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001315 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001316 #" Test for using a function name
1317 LET &omnifunc = 'g:OmniFunc2'
1318 new
1319 call setline(1, 'zero')
1320 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001321 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001322 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1323 bw!
1324
1325 #" Test for using a function()
1326 set omnifunc=function('g:OmniFunc1',\ [10])
1327 new
1328 call setline(1, 'one')
1329 LET g:OmniFunc1Args = []
1330 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1331 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001332 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001333
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001334 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001337 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001338 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001339 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001340 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001341 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001342 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001343
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001344 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001345 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001346 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001347 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001349 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001350 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001353
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001354 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001355 set omnifunc=funcref('g:OmniFunc1',\ [13])
1356 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001357 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001358 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001359 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001360 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001361 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001362
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001363 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001364 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001365 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001366 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001367 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001368 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001369 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001370 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001371 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001372
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001374 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001375 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001376 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001378 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001380 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001382
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001384 VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001385 LET optval = substitute(optval, ' ', '\\ ', 'g')
1386 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001387 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001388 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001389 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001390 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001391 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001392 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001393
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001394 #" Set 'omnifunc' to a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001395 LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND
1396 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001398 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001400 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001401 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001402
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001403 #" Set 'omnifunc' to a string(lambda_expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001404 LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND'
1405 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001407 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001408 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001409 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001411
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001412 #" Set 'omnifunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001413 VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001414 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001415 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001416 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001417 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001418 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001419 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001420 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001421
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001422 #" Set 'omnifunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001423 LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001424 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001425 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001426 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001427 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001428 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001429 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001430 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001431
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 #" Test for using a lambda function with incorrect return value
1433 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1434 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001435 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001436 call setline(1, 'eight')
1437 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1438 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001439
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001440 #" Test for clearing the 'omnifunc' option
1441 set omnifunc=''
1442 set omnifunc&
1443 call assert_fails("set omnifunc=function('abc')", "E700:")
1444 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001445
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001446 #" set 'omnifunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001447 set omnifunc=OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001448 call setline(1, 'nine')
1449 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1450 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001451 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001452 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001453 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001454 bw!
1455 END
1456 call CheckLegacyAndVim9Success(lines)
1457
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001458 " Test for using a script-local function name
1459 func s:OmniFunc3(findstart, base)
1460 call add(g:OmniFunc3Args, [a:findstart, a:base])
1461 return a:findstart ? 0 : []
1462 endfunc
1463 set omnifunc=s:OmniFunc3
1464 new
1465 call setline(1, 'script1')
1466 let g:OmniFunc3Args = []
1467 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1468 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1469 bw!
1470
1471 let &omnifunc = 's:OmniFunc3'
1472 new
1473 call setline(1, 'script2')
1474 let g:OmniFunc3Args = []
1475 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1476 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1477 bw!
1478 delfunc s:OmniFunc3
1479
1480 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001481 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001482 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1483
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001484 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001486 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001487 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001488 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001489 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001490
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001491 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1492 func SetOmniFunc()
1493 let params = {'omni': function('g:DictOmniFunc')}
1494 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001495 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001496 func g:DictOmniFunc(_) dict
1497 endfunc
1498 call SetOmniFunc()
1499 new
1500 call SetOmniFunc()
1501 bw
1502 call test_garbagecollect_now()
1503 new
1504 set omnifunc=
1505 wincmd w
1506 set omnifunc=
1507 %bw!
1508 delfunc g:DictOmniFunc
1509 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001510
1511 " Vim9 tests
1512 let lines =<< trim END
1513 vim9script
1514
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001515 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1516 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001517 return findstart ? 0 : []
1518 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001519
1520 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001521 set omnifunc=function('Vim9omniFunc',\ [60])
1522 new | only
1523 setline(1, 'one')
1524 g:Vim9omniFunc_Args = []
1525 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1526 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1527 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001528
1529 # Test for using a global function name
1530 &omnifunc = g:OmniFunc2
1531 new | only
1532 setline(1, 'two')
1533 g:OmniFunc2Args = []
1534 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1535 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1536 bw!
1537
1538 # Test for using a script-local function name
1539 def s:LocalOmniFunc(findstart: number, base: string): any
1540 add(g:LocalOmniFuncArgs, [findstart, base])
1541 return findstart ? 0 : []
1542 enddef
1543 &omnifunc = s:LocalOmniFunc
1544 new | only
1545 setline(1, 'three')
1546 g:LocalOmniFuncArgs = []
1547 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1548 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1549 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001550 END
1551 call CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001552
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001553 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001554 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001555 delfunc OmniFunc1
1556 delfunc OmniFunc2
1557 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001558 %bw!
1559endfunc
1560
1561" Test for different ways of setting the 'thesaurusfunc' option
1562func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001563 func TsrFunc1(callnr, findstart, base)
1564 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001565 return a:findstart ? 0 : []
1566 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001567 func TsrFunc2(findstart, base)
1568 call add(g:TsrFunc2Args, [a:findstart, a:base])
1569 return a:findstart ? 0 : ['sunday']
1570 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001571
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001572 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001573 #" Test for using a function name
1574 LET &thesaurusfunc = 'g:TsrFunc2'
1575 new
1576 call setline(1, 'zero')
1577 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001578 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001579 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1580 bw!
1581
1582 #" Test for using a function()
1583 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1584 new
1585 call setline(1, 'one')
1586 LET g:TsrFunc1Args = []
1587 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1588 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001589 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001590
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001591 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001592 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001594 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001595 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001596 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001598 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001599 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001600
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001602 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001603 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001604 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001606 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001607 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001608 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001609 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001610
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001611 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001612 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1613 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001614 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001615 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001616 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001617 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001619
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001620 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001621 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001623 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001629
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001631 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001632 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001633 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001635 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001637 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001639
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 #" Test for using a lambda function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001641 VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 LET optval = substitute(optval, ' ', '\\ ', 'g')
1643 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001644 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001646 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001650
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 #" Test for using a lambda function with set
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001652 LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND
1653 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001654 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001655 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001656 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001657 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001659
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 #" Set 'thesaurusfunc' to a string(lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001661 LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND'
1662 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001663 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001664 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001665 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001666 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001668
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 #" Set 'thesaurusfunc' to a variable with a lambda expression
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001670 VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001672 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001674 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001676 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001677 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001678
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001679 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001680 LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001681 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001682 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001683 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001684 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001686 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001688
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 #" Test for using a lambda function with incorrect return value
1690 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1691 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001692 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 call setline(1, 'eight')
1694 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1695 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001696
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001697 #" Test for clearing the 'thesaurusfunc' option
1698 set thesaurusfunc=''
1699 set thesaurusfunc&
1700 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1701 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001702
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001703 #" set 'thesaurusfunc' to a non-existing function
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001704 set thesaurusfunc=TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001705 call setline(1, 'ten')
1706 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1707 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001708 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001709 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001710 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001711 bw!
1712
1713 #" Use a buffer-local value and a global value
1714 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001715 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001716 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001717 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001718 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1719 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001720 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001721 new
1722 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001723 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001724 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1725 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001726 call assert_equal([], g:TsrFunc1Args)
1727 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001728 wincmd w
1729 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001730 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001731 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1732 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001733 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001734 :%bw!
1735 END
1736 call CheckLegacyAndVim9Success(lines)
1737
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001738 " Test for using a script-local function name
1739 func s:TsrFunc3(findstart, base)
1740 call add(g:TsrFunc3Args, [a:findstart, a:base])
1741 return a:findstart ? 0 : []
1742 endfunc
1743 set tsrfu=s:TsrFunc3
1744 new
1745 call setline(1, 'script1')
1746 let g:TsrFunc3Args = []
1747 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1748 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1749 bw!
1750
1751 let &tsrfu = 's:TsrFunc3'
1752 new
1753 call setline(1, 'script2')
1754 let g:TsrFunc3Args = []
1755 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1756 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1757 bw!
1758 delfunc s:TsrFunc3
1759
1760 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001761 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001762 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1763
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001764 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001765 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001766 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001767 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001768 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001769 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001770 bw!
1771
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001772 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
1773 func SetTsrFunc()
1774 let params = {'thesaurus': function('g:DictTsrFunc')}
1775 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001776 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001777 func g:DictTsrFunc(_) dict
1778 endfunc
1779 call SetTsrFunc()
1780 new
1781 call SetTsrFunc()
1782 bw
1783 call test_garbagecollect_now()
1784 new
1785 set thesaurusfunc=
1786 wincmd w
1787 %bw!
1788 delfunc SetTsrFunc
1789
1790 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
1791 " cause a crash.
1792 func SetLocalTsrFunc()
1793 let params = {'thesaurus': function('g:DictTsrFunc')}
1794 let &l:thesaurusfunc = params.thesaurus
1795 endfunc
1796 call SetLocalTsrFunc()
1797 call test_garbagecollect_now()
1798 call SetLocalTsrFunc()
1799 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001800 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001801 delfunc g:DictTsrFunc
1802 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001803
1804 " Vim9 tests
1805 let lines =<< trim END
1806 vim9script
1807
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001808 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
1809 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001810 return findstart ? 0 : []
1811 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001812
1813 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001814 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
1815 new | only
1816 setline(1, 'one')
1817 g:Vim9tsrFunc_Args = []
1818 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1819 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
1820 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001821
1822 # Test for using a global function name
1823 &thesaurusfunc = g:TsrFunc2
1824 new | only
1825 setline(1, 'two')
1826 g:TsrFunc2Args = []
1827 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1828 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
1829 bw!
1830
1831 # Test for using a script-local function name
1832 def s:LocalTsrFunc(findstart: number, base: string): any
1833 add(g:LocalTsrFuncArgs, [findstart, base])
1834 return findstart ? 0 : []
1835 enddef
1836 &thesaurusfunc = s:LocalTsrFunc
1837 new | only
1838 setline(1, 'three')
1839 g:LocalTsrFuncArgs = []
1840 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1841 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
1842 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001843 END
1844 call CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001845
1846 " cleanup
1847 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001848 delfunc TsrFunc1
1849 delfunc TsrFunc2
1850 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001851 %bw!
1852endfunc
1853
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001854" vim: shiftwidth=2 sts=2 expandtab