blob: 3712abe69085e9c8bd189a9caf69782e5a12b926 [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
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
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
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 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>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
102 call delete('Xtestfile')
103 call delete('Xtest11.one')
104 call delete('Xtest11.two')
105 call delete('Xtestdata')
106 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100107 cd ..
108 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200109endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100110
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000111func Test_ins_complete_invalid_byte()
112 if has('unix') && executable('base64')
113 " this weird command was causing an illegal memory access
114 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
115 call system('base64 -d Xinvalid64 > Xinvalid')
116 call writefile(['qa!'], 'Xexit')
117 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
118 call delete('Xinvalid64')
119 call delete('Xinvalid')
120 call delete('Xexit')
121 endif
122endfunc
123
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100124func Test_omni_dash()
125 func Omni(findstart, base)
126 if a:findstart
127 return 5
128 else
129 echom a:base
130 return ['-help', '-v']
131 endif
132 endfunc
133 set omnifunc=Omni
134 new
135 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100136 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100137
138 bwipe!
139 delfunc Omni
140 set omnifunc=
141endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100142
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200143func Test_omni_autoload()
144 let save_rtp = &rtp
145 set rtp=Xruntime/some
146 let dir = 'Xruntime/some/autoload'
147 call mkdir(dir, 'p')
148
149 let lines =<< trim END
150 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000151 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200152 if findstart
153 return 1
154 else
155 return ['match']
156 endif
157 enddef
158 {
159 eval 1 + 2
160 }
161 END
162 call writefile(lines, dir .. '/omni.vim')
163
164 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000165 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200166 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
167
168 bwipe!
169 call delete('Xruntime', 'rf')
170 set omnifunc=
171 let &rtp = save_rtp
172endfunc
173
Bram Moolenaarffa96842018-06-12 22:05:14 +0200174func Test_completefunc_args()
175 let s:args = []
176 func! CompleteFunc(findstart, base)
177 let s:args += [[a:findstart, empty(a:base)]]
178 endfunc
179 new
180
181 set completefunc=CompleteFunc
182 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200183 call assert_equal([1, 1], s:args[0])
184 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200185 set completefunc=
186
187 let s:args = []
188 set omnifunc=CompleteFunc
189 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200190 call assert_equal([1, 1], s:args[0])
191 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200192 set omnifunc=
193
194 bwipe!
195 unlet s:args
196 delfunc CompleteFunc
197endfunc
198
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100199func s:CompleteDone_CompleteFuncNone( findstart, base )
200 if a:findstart
201 return 0
202 endif
203
204 return v:none
205endfunc
206
Bram Moolenaar1e115362019-01-09 23:01:02 +0100207func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100208 if a:findstart
209 return 0
210 endif
211
212 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100213 \ 'words': [
214 \ {
215 \ 'word': 'aword',
216 \ 'abbr': 'wrd',
217 \ 'menu': 'extra text',
218 \ 'info': 'words are cool',
219 \ 'kind': 'W',
220 \ 'user_data': 'test'
221 \ }
222 \ ]
223 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100224endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100225
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100226func s:CompleteDone_CheckCompletedItemNone()
227 let s:called_completedone = 1
228endfunc
229
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100230func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100231 call assert_equal( 'aword', v:completed_item[ 'word' ] )
232 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
233 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
234 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
235 call assert_equal( 'W', v:completed_item[ 'kind' ] )
236 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
237
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100238 if a:pre
239 call assert_equal('function', complete_info().mode)
240 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100241
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100242 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100243endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100244
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100245func Test_CompleteDoneNone()
246 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200247 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100248
249 set completefunc=<SID>CompleteDone_CompleteFuncNone
250 execute "normal a\<C-X>\<C-U>\<C-Y>"
251 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200252 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100253
254 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200255 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100256
257 let s:called_completedone = 0
258 au! CompleteDone
259endfunc
260
261func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100262 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
263 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100264
265 set completefunc=<SID>CompleteDone_CompleteFuncDict
266 execute "normal a\<C-X>\<C-U>\<C-Y>"
267 set completefunc&
268
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100269 call assert_equal('test', v:completed_item[ 'user_data' ])
270 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100271
272 let s:called_completedone = 0
273 au! CompleteDone
274endfunc
275
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100276func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100277 if a:findstart
278 return 0
279 endif
280
281 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100282 \ 'words': [
283 \ {
284 \ 'word': 'aword',
285 \ 'abbr': 'wrd',
286 \ 'menu': 'extra text',
287 \ 'info': 'words are cool',
288 \ 'kind': 'W',
289 \ 'user_data': ['one', 'two'],
290 \ }
291 \ ]
292 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100293endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100294
Bram Moolenaar1e115362019-01-09 23:01:02 +0100295func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100296 call assert_equal( 'aword', v:completed_item[ 'word' ] )
297 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
298 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
299 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
300 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100301 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100302
303 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100304endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100305
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100306func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100307 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
308
309 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
310 execute "normal a\<C-X>\<C-U>\<C-Y>"
311 set completefunc&
312
Bram Moolenaar08928322020-01-04 14:32:48 +0100313 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100314 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100315
316 let s:called_completedone = 0
317 au! CompleteDone
318endfunc
319
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100320func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100321 if a:findstart
322 return 0
323 endif
324
325 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100326endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100327
Bram Moolenaar1e115362019-01-09 23:01:02 +0100328func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329 call assert_equal( 'aword', v:completed_item[ 'word' ] )
330 call assert_equal( '', v:completed_item[ 'abbr' ] )
331 call assert_equal( '', v:completed_item[ 'menu' ] )
332 call assert_equal( '', v:completed_item[ 'info' ] )
333 call assert_equal( '', v:completed_item[ 'kind' ] )
334 call assert_equal( '', v:completed_item[ 'user_data' ] )
335
336 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100337endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100338
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100339func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100340 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
341
342 set completefunc=<SID>CompleteDone_CompleteFuncList
343 execute "normal a\<C-X>\<C-U>\<C-Y>"
344 set completefunc&
345
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100346 call assert_equal('', v:completed_item[ 'user_data' ])
347 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100348
349 let s:called_completedone = 0
350 au! CompleteDone
351endfunc
352
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200353func Test_CompleteDone_undo()
354 au CompleteDone * call append(0, "prepend1")
355 new
356 call setline(1, ["line1", "line2"])
357 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
358 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
359 \ getline(1, '$'))
360 undo
361 call assert_equal(["line1", "line2"], getline(1, '$'))
362 bwipe!
363 au! CompleteDone
364endfunc
365
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200366func CompleteTest(findstart, query)
367 if a:findstart
368 return col('.')
369 endif
370 return ['matched']
371endfunc
372
373func Test_completefunc_info()
374 new
375 set completeopt=menuone
376 set completefunc=CompleteTest
377 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100378 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 +0200379 bwipe!
380 set completeopt&
381 set completefunc&
382endfunc
383
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100384" Check that when using feedkeys() typeahead does not interrupt searching for
385" completions.
386func Test_compl_feedkeys()
387 new
388 set completeopt=menuone,noselect
389 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
390 call assert_equal("jump jump", getline(1))
391 bwipe!
392 set completeopt&
393endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200394
mityua1198122021-11-20 19:13:39 +0000395func s:ComplInCmdwin_GlobalCompletion(a, l, p)
396 return 'global'
397endfunc
398
399func s:ComplInCmdwin_LocalCompletion(a, l, p)
400 return 'local'
401endfunc
402
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200403func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100404 CheckFeature cmdwin
405
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200406 set wildmenu wildchar=<Tab>
407 com! -nargs=1 -complete=command GetInput let input = <q-args>
408 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100409 let w:test_winvar = 'winvar'
410 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200411
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100412 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200413 let input = ''
414 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
415 call assert_equal('TestCommand', input)
416
417 let input = ''
418 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
419 call assert_equal('T', input)
420
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100421
422 com! -nargs=1 -complete=var GetInput let input = <q-args>
423 " Window-local variables
424 let input = ''
425 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
426 call assert_equal('w:test_winvar', input)
427
428 let input = ''
429 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
430 call assert_equal('w:test_', input)
431
432 " Buffer-local variables
433 let input = ''
434 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
435 call assert_equal('b:test_bufvar', input)
436
437 let input = ''
438 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
439 call assert_equal('b:test_', input)
440
mityua1198122021-11-20 19:13:39 +0000441
442 " Argument completion of buffer-local command
443 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
444 return ['global']
445 endfunc
446
447 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
448 return ['local']
449 endfunc
450
451 func s:ComplInCmdwin_CheckCompletion(arg)
452 call assert_equal('local', a:arg)
453 endfunc
454
455 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
456 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
457 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
458 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
459 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
460
461 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
462 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
463 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
464 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
465
466 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
467
468 func! s:ComplInCmdwin_CheckCompletion(arg)
469 call assert_equal('global', a:arg)
470 endfunc
471 new
472 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
473 quit
474
475 delfunc s:ComplInCmdwin_GlobalCompletion
476 delfunc s:ComplInCmdwin_LocalCompletion
477 delfunc s:ComplInCmdwin_GlobalCompletionList
478 delfunc s:ComplInCmdwin_LocalCompletionList
479 delfunc s:ComplInCmdwin_CheckCompletion
480
481 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200482 delcom TestCommand
483 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100484 unlet w:test_winvar
485 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200486 set wildmenu& wildchar&
487endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200488
489" Test for insert path completion with completeslash option
490func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200491 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200492
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200493 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200494 let orig_shellslash = &shellslash
495 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200496 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200497
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200498 set noshellslash
499
500 set completeslash=
501 exe "normal oXd\<C-X>\<C-F>"
502 call assert_equal('Xdir\', getline('.'))
503
504 set completeslash=backslash
505 exe "normal oXd\<C-X>\<C-F>"
506 call assert_equal('Xdir\', getline('.'))
507
508 set completeslash=slash
509 exe "normal oXd\<C-X>\<C-F>"
510 call assert_equal('Xdir/', getline('.'))
511
512 set shellslash
513
514 set completeslash=
515 exe "normal oXd\<C-X>\<C-F>"
516 call assert_equal('Xdir/', getline('.'))
517
518 set completeslash=backslash
519 exe "normal oXd\<C-X>\<C-F>"
520 call assert_equal('Xdir\', getline('.'))
521
522 set completeslash=slash
523 exe "normal oXd\<C-X>\<C-F>"
524 call assert_equal('Xdir/', getline('.'))
525 %bw!
526 call delete('Xdir', 'rf')
527
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200528 set noshellslash
529 set completeslash=slash
530 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
531
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200532 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200533 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200534endfunc
535
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200536func Test_pum_stopped_by_timer()
537 CheckScreendump
538
539 let lines =<< trim END
540 call setline(1, ['hello', 'hullo', 'heeee', ''])
541 func StartCompl()
542 call timer_start(100, { -> execute('stopinsert') })
543 call feedkeys("Gah\<C-N>")
544 endfunc
545 END
546
547 call writefile(lines, 'Xpumscript')
548 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
549 call term_sendkeys(buf, ":call StartCompl()\<CR>")
550 call TermWait(buf, 200)
551 call term_sendkeys(buf, "k")
552 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
553
554 call StopVimInTerminal(buf)
555 call delete('Xpumscript')
556endfunc
557
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100558func Test_pum_with_folds_two_tabs()
559 CheckScreendump
560
561 let lines =<< trim END
562 set fdm=marker
563 call setline(1, ['" x {{{1', '" a some text'])
564 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
565 norm! zm
566 tab sp
567 call feedkeys('2Gzv', 'xt')
568 call feedkeys("0fa", 'xt')
569 END
570
571 call writefile(lines, 'Xpumscript')
572 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200573 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100574 call term_sendkeys(buf, "a\<C-N>")
575 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
576
577 call term_sendkeys(buf, "\<Esc>")
578 call StopVimInTerminal(buf)
579 call delete('Xpumscript')
580endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100581
582func Test_pum_with_preview_win()
583 CheckScreendump
584
585 let lines =<< trim END
586 funct Omni_test(findstart, base)
587 if a:findstart
588 return col(".") - 1
589 endif
590 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
591 endfunc
592 set omnifunc=Omni_test
593 set completeopt+=longest
594 END
595
596 call writefile(lines, 'Xpreviewscript')
597 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200598 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100599 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200600 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100601 call term_sendkeys(buf, "\<C-N>")
602 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
603
604 call term_sendkeys(buf, "\<Esc>")
605 call StopVimInTerminal(buf)
606 call delete('Xpreviewscript')
607endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100608
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000609func Test_scrollbar_on_wide_char()
610 CheckScreendump
611
612 let lines =<< trim END
613 call setline(1, ['a', ' 啊啊啊',
614 \ ' 哦哦哦',
615 \ ' 呃呃呃'])
616 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
617 END
618 call writefile(lines, 'Xwidescript')
619 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
620 call term_sendkeys(buf, "A\<C-N>")
621 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
622
623 call StopVimInTerminal(buf)
624 call delete('Xwidescript')
625endfunc
626
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100627" Test for inserting the tag search pattern in insert mode
628func Test_ins_compl_tag_sft()
629 call writefile([
630 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
631 \ "first\tXfoo\t/^int first() {}$/",
632 \ "second\tXfoo\t/^int second() {}$/",
633 \ "third\tXfoo\t/^int third() {}$/"],
634 \ 'Xtags')
635 set tags=Xtags
636 let code =<< trim [CODE]
637 int first() {}
638 int second() {}
639 int third() {}
640 [CODE]
641 call writefile(code, 'Xfoo')
642
643 enew
644 set showfulltag
645 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
646 call assert_equal('int second() {}', getline(1))
647 set noshowfulltag
648
649 call delete('Xtags')
650 call delete('Xfoo')
651 set tags&
652 %bwipe!
653endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200654
655" Test for 'completefunc' deleting text
656func Test_completefunc_error()
657 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200658 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200659 func CompleteFunc(findstart, base)
660 if a:findstart == 1
661 normal dd
662 return col('.') - 1
663 endif
664 return ['a', 'b']
665 endfunc
666 set completefunc=CompleteFunc
667 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100668 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200669
670 " delete text when called for the second time
671 func CompleteFunc2(findstart, base)
672 if a:findstart == 1
673 return col('.') - 1
674 endif
675 normal dd
676 return ['a', 'b']
677 endfunc
678 set completefunc=CompleteFunc2
679 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200680 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200681
Bram Moolenaar97202d92021-01-28 18:34:35 +0100682 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100683 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100684 if a:findstart == 1
685 return col('.') - 1
686 endif
687 wincmd p
688 return ['a', 'b']
689 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100690 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100691 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100692 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100693 close!
694
695 set completefunc&
696 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100697 delfunc CompleteFunc2
698 delfunc CompleteFunc3
699 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100700endfunc
701
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200702" Test for returning non-string values from 'completefunc'
703func Test_completefunc_invalid_data()
704 new
705 func! CompleteFunc(findstart, base)
706 if a:findstart == 1
707 return col('.') - 1
708 endif
709 return [{}, '', 'moon']
710 endfunc
711 set completefunc=CompleteFunc
712 exe "normal i\<C-X>\<C-U>"
713 call assert_equal('moon', getline(1))
714 set completefunc&
715 close!
716endfunc
717
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200718" Test for errors in using complete() function
719func Test_complete_func_error()
720 call assert_fails('call complete(1, ["a"])', 'E785:')
721 func ListColors()
722 call complete(col('.'), "blue")
723 endfunc
724 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
725 func ListMonths()
726 call complete(col('.'), test_null_list())
727 endfunc
728 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
729 delfunc ListColors
730 delfunc ListMonths
731 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200732 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200733endfunc
734
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000735" Test for recursively starting completion mode using complete()
736func Test_recursive_complete_func()
737 func ListColors()
738 call complete(5, ["red", "blue"])
739 return ''
740 endfunc
741 new
742 call setline(1, ['a1', 'a2'])
743 set complete=.
744 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
745 call assert_equal('a2blue', getline(3))
746 delfunc ListColors
747 bw!
748endfunc
749
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200750" Test for completing words following a completed word in a line
751func Test_complete_wrapscan()
752 " complete words from another buffer
753 new
754 call setline(1, ['one two', 'three four'])
755 new
756 setlocal complete=w
757 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
758 call assert_equal('two three four', getline(1))
759 close!
760 " complete words from the current buffer
761 setlocal complete=.
762 %d
763 call setline(1, ['one two', ''])
764 call cursor(2, 1)
765 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
766 call assert_equal('one two one two', getline(2))
767 close!
768endfunc
769
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200770" Test for completing special characters
771func Test_complete_special_chars()
772 new
773 call setline(1, 'int .*[-\^$ func float')
774 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
775 call assert_equal('int .*[-\^$ func float', getline(2))
776 close!
777endfunc
778
779" Test for completion when text is wrapped across lines.
780func Test_complete_across_line()
781 new
782 call setline(1, ['red green blue', 'one two three'])
783 setlocal textwidth=20
784 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
785 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
786 close!
787endfunc
788
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000789" Test for completing words with a '.' at the end of a word.
790func Test_complete_joinspaces()
791 new
792 call setline(1, ['one two.', 'three. four'])
793 set joinspaces
794 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
795 call assert_equal("one two. three. four", getline(3))
796 set joinspaces&
797 bw!
798endfunc
799
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200800" Test for using CTRL-L to add one character when completing matching
801func Test_complete_add_onechar()
802 new
803 call setline(1, ['wool', 'woodwork'])
804 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
805 call assert_equal('woof', getline(3))
806
807 " use 'ignorecase' and backspace to erase characters from the prefix string
808 " and then add letters using CTRL-L
809 %d
810 set ignorecase backspace=2
811 setlocal complete=.
812 call setline(1, ['workhorse', 'workload'])
813 normal Go
814 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
815 call assert_equal('workh', getline(3))
816 set ignorecase& backspace&
817 close!
818endfunc
819
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000820" Test for using CTRL-X CTRL-L to complete whole lines lines
821func Test_complete_wholeline()
822 new
823 " complete one-line
824 call setline(1, ['a1', 'a2'])
825 exe "normal ggoa\<C-X>\<C-L>"
826 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
827 " go to the next match (wrapping around the buffer)
828 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
829 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
830 " go to the next match
831 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
832 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
833 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
834 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
835 " repeat the test using CTRL-L
836 " go to the next match (wrapping around the buffer)
837 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
838 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
839 " go to the next match
840 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
841 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
842 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
843 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
844 %d
845 " use CTRL-X CTRL-L to add one more line
846 call setline(1, ['a1', 'b1'])
847 setlocal complete=.
848 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
849 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
850 bw!
851endfunc
852
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200853" Test insert completion with 'cindent' (adjust the indent)
854func Test_complete_with_cindent()
855 new
856 setlocal cindent
857 call setline(1, ['if (i == 1)', " j = 2;"])
858 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
859 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
860
861 %d
862 call setline(1, ['when while', '{', ''])
863 setlocal cinkeys+==while
864 exe "normal Giwh\<C-P> "
865 call assert_equal("\twhile ", getline('$'))
866 close!
867endfunc
868
869" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
870func Test_complete_cmdline()
871 new
872 exe "normal icaddb\<C-X>\<C-V>"
873 call assert_equal('caddbuffer', getline(1))
874 exe "normal ocall getqf\<C-X>\<C-V>"
875 call assert_equal('call getqflist(', getline(2))
876 exe "normal oabcxyz(\<C-X>\<C-V>"
877 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200878 com! -buffer TestCommand1 echo 'TestCommand1'
879 com! -buffer TestCommand2 echo 'TestCommand2'
880 write TestCommand1Test
881 write TestCommand2Test
882 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
883 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
884 call assert_equal('TestCommand2Test', getline(4))
885 call delete('TestCommand1Test')
886 call delete('TestCommand2Test')
887 delcom TestCommand1
888 delcom TestCommand2
889 close!
890endfunc
891
892" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
893func Test_complete_stop()
894 new
895 func Save_mode1()
896 let g:mode1 = mode(1)
897 return ''
898 endfunc
899 func Save_mode2()
900 let g:mode2 = mode(1)
901 return ''
902 endfunc
903 inoremap <F1> <C-R>=Save_mode1()<CR>
904 inoremap <F2> <C-R>=Save_mode2()<CR>
905 call setline(1, ['aaa bbb ccc '])
906 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
907 call assert_equal('ic', g:mode1)
908 call assert_equal('i', g:mode2)
909 call assert_equal('aaa bbb ccc ', getline(1))
910 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
911 call assert_equal('ic', g:mode1)
912 call assert_equal('i', g:mode2)
913 call assert_equal('aaa bbb ccc aaa', getline(1))
914 set completeopt+=noselect
915 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
916 call assert_equal('ic', g:mode1)
917 call assert_equal('i', g:mode2)
918 call assert_equal('aaa bbb ccc aaa bb', getline(1))
919 set completeopt&
920 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
921 call assert_equal('ic', g:mode1)
922 call assert_equal('i', g:mode2)
923 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
924 com! -buffer TestCommand1 echo 'TestCommand1'
925 com! -buffer TestCommand2 echo 'TestCommand2'
926 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
927 call assert_equal('ic', g:mode1)
928 call assert_equal('i', g:mode2)
929 call assert_equal('TestCommand2', getline(2))
930 delcom TestCommand1
931 delcom TestCommand2
932 unlet g:mode1
933 unlet g:mode2
934 iunmap <F1>
935 iunmap <F2>
936 delfunc Save_mode1
937 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200938 close!
939endfunc
940
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000941" Test for typing CTRL-R in insert completion mode to insert a register
942" content.
943func Test_complete_reginsert()
944 new
945 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
946
947 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
948 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
949 " inserted.
950 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
951 call assert_equal('a123', getline(5))
952 let @r = "\<C-P>\<C-P>"
953 exe "normal GCa\<C-P>\<C-R>r"
954 call assert_equal('a12', getline(5))
955 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
956 call assert_equal('a1234x', getline(5))
957 bw!
958endfunc
959
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200960func Test_issue_7021()
961 CheckMSWindows
962
963 let orig_shellslash = &shellslash
964 set noshellslash
965
966 set completeslash=slash
967 call assert_false(expand('~') =~ '/')
968
969 let &shellslash = orig_shellslash
970 set completeslash=
971endfunc
972
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000973" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
974func Test_complete_longest_match()
975 for e in ['latin1', 'utf-8']
976 exe 'set encoding=' .. e
977 new
978 set complete=.
979 set completeopt=menu,longest
980 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
981 exe "normal Gopfx\<C-P>"
982 call assert_equal('pfx_', getline(5))
983 bw!
984 endfor
985
986 " Test for completing additional words with longest match set
987 new
988 call setline(1, ['abc1', 'abd2'])
989 exe "normal Goab\<C-P>\<C-X>\<C-P>"
990 call assert_equal('ab', getline(3))
991 bw!
992 set complete& completeopt&
993endfunc
994
995" Test for removing the first displayed completion match and selecting the
996" match just before that.
997func Test_complete_erase_firstmatch()
998 new
999 call setline(1, ['a12', 'a34', 'a56'])
1000 set complete=.
1001 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1002 call assert_equal('a34', getline('$'))
1003 set complete&
1004 bw!
1005endfunc
1006
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001007" Test for completing words from unloaded buffers
1008func Test_complete_from_unloadedbuf()
1009 call writefile(['abc'], "Xfile1")
1010 call writefile(['def'], "Xfile2")
1011 edit Xfile1
1012 edit Xfile2
1013 new | close
1014 enew
1015 bunload Xfile1 Xfile2
1016 set complete=u
1017 " complete from an unloaded buffer
1018 exe "normal! ia\<C-P>"
1019 call assert_equal('abc', getline(1))
1020 exe "normal! od\<C-P>"
1021 call assert_equal('def', getline(2))
1022 set complete&
1023 %bw!
1024 call delete("Xfile1")
1025 call delete("Xfile2")
1026endfunc
1027
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001028" Test for completing whole lines from unloaded buffers
1029func Test_complete_wholeline_unloadedbuf()
1030 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1031 edit Xfile1
1032 enew
1033 set complete=u
1034 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1035 call assert_equal('a line2', getline(1))
1036 %d
1037 " completing from an unlisted buffer should fail
1038 bdel Xfile1
1039 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1040 call assert_equal('a', getline(1))
1041 set complete&
1042 %bw!
1043 call delete("Xfile1")
1044endfunc
1045
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001046" Test for completing words from unlisted buffers
1047func Test_complete_from_unlistedbuf()
1048 call writefile(['abc'], "Xfile1")
1049 call writefile(['def'], "Xfile2")
1050 edit Xfile1
1051 edit Xfile2
1052 new | close
1053 bdel Xfile1 Xfile2
1054 set complete=U
1055 " complete from an unlisted buffer
1056 exe "normal! ia\<C-P>"
1057 call assert_equal('abc', getline(1))
1058 exe "normal! od\<C-P>"
1059 call assert_equal('def', getline(2))
1060 set complete&
1061 %bw!
1062 call delete("Xfile1")
1063 call delete("Xfile2")
1064endfunc
1065
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001066" Test for completing whole lines from unlisted buffers
1067func Test_complete_wholeline_unlistedbuf()
1068 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1069 edit Xfile1
1070 enew
1071 set complete=U
1072 " completing from a unloaded buffer should fail
1073 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1074 call assert_equal('a', getline(1))
1075 %d
1076 bdel Xfile1
1077 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1078 call assert_equal('a line2', getline(1))
1079 set complete&
1080 %bw!
1081 call delete("Xfile1")
1082endfunc
1083
1084" Test for adding a multibyte character using CTRL-L in completion mode
1085func Test_complete_mbyte_char_add()
1086 new
1087 set complete=.
1088 call setline(1, 'abÄ—')
1089 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1090 call assert_equal('abÄ—', getline(2))
1091 " Test for a leader with multibyte character
1092 %d
1093 call setline(1, 'abÄ—Ä•')
1094 exe "normal! oabÄ—\<C-P>"
1095 call assert_equal('abÄ—Ä•', getline(2))
1096 bw!
1097endfunc
1098
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001099" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1100" not to complete matches from the local buffer. Also test using multiple
1101" <C-X> to cancel the current completion mode.
1102func Test_complete_local_expansion()
1103 new
1104 set complete=t
1105 call setline(1, ['abc', 'def'])
1106 exe "normal! Go\<C-X>\<C-P>"
1107 call assert_equal("def", getline(3))
1108 exe "normal! Go\<C-P>"
1109 call assert_equal("", getline(4))
1110 exe "normal! Go\<C-X>\<C-N>"
1111 call assert_equal("abc", getline(5))
1112 exe "normal! Go\<C-N>"
1113 call assert_equal("", getline(6))
1114
1115 " use multiple <C-X> to cancel the previous completion mode
1116 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1117 call assert_equal("", getline(7))
1118 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1119 call assert_equal("", getline(8))
1120 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1121 call assert_equal("abc", getline(9))
1122
1123 " interrupt the current completion mode
1124 set completeopt=menu,noinsert
1125 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1126 call assert_equal("abc", getline(10))
1127
1128 " when only one <C-X> is used to interrupt, do normal expansion
1129 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1130 call assert_equal("", getline(11))
1131 set completeopt&
1132
1133 " using two <C-X> in non-completion mode and restarting the same mode
1134 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1135 call assert_equal("def", getline(12))
1136
1137 " test for adding a match from the original empty text
1138 %d
1139 call setline(1, 'abc def g')
1140 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1141 call assert_equal('def', getline(2))
1142 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1143 call assert_equal('abc', getline(2))
1144
1145 bw!
1146endfunc
1147
1148" Test for undoing changes after a insert-mode completion
1149func Test_complete_undo()
1150 new
1151 set complete=.
1152 " undo with 'ignorecase'
1153 call setline(1, ['ABOVE', 'BELOW'])
1154 set ignorecase
1155 exe "normal! Goab\<C-G>u\<C-P>"
1156 call assert_equal("ABOVE", getline(3))
1157 undo
1158 call assert_equal("ab", getline(3))
1159 set ignorecase&
1160 %d
1161 " undo with longest match
1162 set completeopt=menu,longest
1163 call setline(1, ['above', 'about'])
1164 exe "normal! Goa\<C-G>u\<C-P>"
1165 call assert_equal("abo", getline(3))
1166 undo
1167 call assert_equal("a", getline(3))
1168 set completeopt&
1169 %d
1170 " undo for line completion
1171 call setline(1, ['above that change', 'below that change'])
1172 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1173 call assert_equal("above that change", getline(3))
1174 undo
1175 call assert_equal("above", getline(3))
1176
1177 bw!
1178endfunc
1179
1180" Test for completing a very long word
1181func Test_complete_long_word()
1182 set complete&
1183 new
1184 call setline(1, repeat('x', 950) .. ' one two three')
1185 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1186 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1187 %d
1188 " should fail when more than 950 characters are in a word
1189 call setline(1, repeat('x', 951) .. ' one two three')
1190 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1191 call assert_equal(repeat('x', 951), getline(2))
1192
1193 " Test for adding a very long word to an existing completion
1194 %d
1195 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1196 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1197 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1198 bw!
1199endfunc
1200
1201" Test for some fields in the complete items used by complete()
1202func Test_complete_items()
1203 func CompleteItems(idx)
1204 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1205 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1206 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1207 \ [#{user_data: 'u9'}],
1208 \ [#{word: "", user_data: 'u10'}],
1209 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1210 call complete(col('.'), items[a:idx])
1211 return ''
1212 endfunc
1213 new
1214 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1215 call assert_equal('u2', v:completed_item.user_data)
1216 call assert_equal('one', getline(1))
1217 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1218 call assert_equal('u3', v:completed_item.user_data)
1219 call assert_equal('one', getline(2))
1220 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1221 call assert_equal('', getline(3))
1222 set completeopt=menu,noinsert
1223 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1224 call assert_equal('oNE', getline(4))
1225 call assert_equal('u8', v:completed_item.user_data)
1226 set completeopt&
1227 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1228 call assert_equal('', getline(5))
1229 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1230 call assert_equal('', getline(6))
1231 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1232 call assert_equal('', getline(7))
1233 call assert_equal('u11', v:completed_item.user_data)
1234 " pass invalid argument to complete()
1235 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1236 call assert_fails('exe cmd', 'E730:')
1237 bw!
1238 delfunc CompleteItems
1239endfunc
1240
1241" Test for the "refresh" item in the dict returned by an insert completion
1242" function
1243func Test_complete_item_refresh_always()
1244 let g:CallCount = 0
1245 func! Tcomplete(findstart, base)
1246 if a:findstart
1247 " locate the start of the word
1248 let line = getline('.')
1249 let start = col('.') - 1
1250 while start > 0 && line[start - 1] =~ '\a'
1251 let start -= 1
1252 endwhile
1253 return start
1254 else
1255 let g:CallCount += 1
1256 let res = ["update1", "update12", "update123"]
1257 return #{words: res, refresh: 'always'}
1258 endif
1259 endfunc
1260 new
1261 set completeopt=menu,longest
1262 set completefunc=Tcomplete
1263 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1264 call assert_equal('up', getline(1))
1265 call assert_equal(2, g:CallCount)
1266 set completeopt&
1267 set completefunc&
1268 bw!
1269 delfunc Tcomplete
1270endfunc
1271
1272" Test for completing from a thesaurus file without read permission
1273func Test_complete_unreadable_thesaurus_file()
1274 CheckUnix
1275 CheckNotRoot
1276
1277 call writefile(['about', 'above'], 'Xfile')
1278 call setfperm('Xfile', '---r--r--')
1279 new
1280 set complete=sXfile
1281 exe "normal! ia\<C-P>"
1282 call assert_equal('a', getline(1))
1283 bw!
1284 call delete('Xfile')
1285 set complete&
1286endfunc
1287
Bram Moolenaarcc233582020-12-12 13:32:07 +01001288" Test to ensure 'Scanning...' messages are not recorded in messages history
1289func Test_z1_complete_no_history()
1290 new
1291 messages clear
1292 let currmess = execute('messages')
1293 setlocal dictionary=README.txt
1294 exe "normal owh\<C-X>\<C-K>"
1295 exe "normal owh\<C-N>"
1296 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001297 bwipe!
1298endfunc
1299
1300" A mapping is not used for the key after CTRL-X.
1301func Test_no_mapping_for_ctrl_x_key()
1302 new
1303 inoremap <C-K> <Cmd>let was_mapped = 'yes'<CR>
1304 setlocal dictionary=README.txt
1305 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1306 call assert_equal('example ', getline(1))
1307 call assert_false(exists('was_mapped'))
1308 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001309endfunc
1310
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001311" Test for different ways of setting the 'completefunc' option
1312func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001313 func CompleteFunc1(callnr, findstart, base)
1314 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1315 return a:findstart ? 0 : []
1316 endfunc
1317 func CompleteFunc2(findstart, base)
1318 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001319 return a:findstart ? 0 : []
1320 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001321
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001322 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001323 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001324 LET &completefunc = 'g:CompleteFunc2'
1325 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001326 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001327 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001329 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001330 bw!
1331
1332 #" Test for using a function()
1333 set completefunc=function('g:CompleteFunc1',\ [10])
1334 new
1335 call setline(1, 'one')
1336 LET g:CompleteFunc1Args = []
1337 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1338 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001339 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001340
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001341 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001342 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001343 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001344 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001345 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001346 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001347 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001348 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001349 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001350
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001351 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001352 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001353 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001354 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001355 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001356 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001357 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001358 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001359 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001360
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001361 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001362 set completefunc=funcref('g:CompleteFunc1',\ [13])
1363 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001364 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001365 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001366 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001367 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001368 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001369
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001370 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001371 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001372 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001373 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001374 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001375 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001376 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001377 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001378 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001379
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001380 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001381 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001382 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001383 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001384 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001385 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001386 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001387 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001388 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001389
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001390 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001391 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001392 LET optval = substitute(optval, ' ', '\\ ', 'g')
1393 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001394 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001396 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001398 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001400
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001401 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001402 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001403 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001405 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001407 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001408 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001409
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001411 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001412 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001414 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001416 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001417 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001418
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001419 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001420 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001421 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001422 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001423 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001424 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001425 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001426 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001427 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001428
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001429 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001430 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001431 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001432 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001433 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001434 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001435 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001436 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001437 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001438
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001439 #" Test for using a lambda function with incorrect return value
1440 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1441 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001442 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001443 call setline(1, 'eight')
1444 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1445 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001446
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001447 #" Test for clearing the 'completefunc' option
1448 set completefunc=''
1449 set completefunc&
1450 call assert_fails("set completefunc=function('abc')", "E700:")
1451 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001452
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001453 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001454 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001455 call setline(1, 'five')
1456 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1457 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001458 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001459 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001460 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001461 bw!
1462 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001463 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001464
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001465 " Test for using a script-local function name
1466 func s:CompleteFunc3(findstart, base)
1467 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1468 return a:findstart ? 0 : []
1469 endfunc
1470 set completefunc=s:CompleteFunc3
1471 new
1472 call setline(1, 'script1')
1473 let g:CompleteFunc3Args = []
1474 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1475 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1476 bw!
1477
1478 let &completefunc = 's:CompleteFunc3'
1479 new
1480 call setline(1, 'script2')
1481 let g:CompleteFunc3Args = []
1482 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1483 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1484 bw!
1485 delfunc s:CompleteFunc3
1486
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001487 " In Vim9 script s: can be omitted
1488 let lines =<< trim END
1489 vim9script
1490 var CompleteFunc4Args = []
1491 def CompleteFunc4(findstart: bool, base: string): any
1492 add(CompleteFunc4Args, [findstart, base])
1493 return findstart ? 0 : []
1494 enddef
1495 set completefunc=CompleteFunc4
1496 new
1497 setline(1, 'script1')
1498 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1499 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1500 bw!
1501 END
1502 call v9.CheckScriptSuccess(lines)
1503
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001504 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001505 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001506 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1507
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001508 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001509 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001510 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001511 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001512 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001513 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001514
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001515 " set 'completefunc' to a partial with dict. This used to cause a crash.
1516 func SetCompleteFunc()
1517 let params = {'complete': function('g:DictCompleteFunc')}
1518 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001519 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001520 func g:DictCompleteFunc(_) dict
1521 endfunc
1522 call SetCompleteFunc()
1523 new
1524 call SetCompleteFunc()
1525 bw
1526 call test_garbagecollect_now()
1527 new
1528 set completefunc=
1529 wincmd w
1530 set completefunc=
1531 %bw!
1532 delfunc g:DictCompleteFunc
1533 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001534
1535 " Vim9 tests
1536 let lines =<< trim END
1537 vim9script
1538
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001539 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1540 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001541 return findstart ? 0 : []
1542 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001543
1544 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001545 set completefunc=function('Vim9CompleteFunc',\ [60])
1546 new | only
1547 setline(1, 'one')
1548 g:Vim9completeFuncArgs = []
1549 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1550 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1551 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001552
1553 # Test for using a global function name
1554 &completefunc = g:CompleteFunc2
1555 new | only
1556 setline(1, 'two')
1557 g:CompleteFunc2Args = []
1558 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1559 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1560 bw!
1561
1562 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001563 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001564 add(g:LocalCompleteFuncArgs, [findstart, base])
1565 return findstart ? 0 : []
1566 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001567 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001568 new | only
1569 setline(1, 'three')
1570 g:LocalCompleteFuncArgs = []
1571 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1572 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1573 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001574 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001575 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001576
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001577 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001578 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001579 delfunc CompleteFunc1
1580 delfunc CompleteFunc2
1581 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001582 %bw!
1583endfunc
1584
1585" Test for different ways of setting the 'omnifunc' option
1586func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001587 func OmniFunc1(callnr, findstart, base)
1588 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1589 return a:findstart ? 0 : []
1590 endfunc
1591 func OmniFunc2(findstart, base)
1592 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001593 return a:findstart ? 0 : []
1594 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001595
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001596 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001597 #" Test for using a function name
1598 LET &omnifunc = 'g:OmniFunc2'
1599 new
1600 call setline(1, 'zero')
1601 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001603 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1604 bw!
1605
1606 #" Test for using a function()
1607 set omnifunc=function('g:OmniFunc1',\ [10])
1608 new
1609 call setline(1, 'one')
1610 LET g:OmniFunc1Args = []
1611 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1612 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001613 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001614
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001615 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001616 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001617 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001618 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001619 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001620 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001621 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001622 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001623 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001624
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001625 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001626 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001627 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001628 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001629 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001631 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001632 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001633 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001634
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001635 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001636 set omnifunc=funcref('g:OmniFunc1',\ [13])
1637 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001639 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001641 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001643
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001645 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001646 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001647 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001648 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001649 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001650 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001651 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001652 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001653
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001654 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001655 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001656 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001657 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001659 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001661 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001662 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001663
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001664 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001665 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001666 LET optval = substitute(optval, ' ', '\\ ', 'g')
1667 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001670 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001672 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001674
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001676 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001677 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001679 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001681 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001683
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001685 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001686 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001688 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001690 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001691 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001692
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001694 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001695 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001696 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001697 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001698 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001699 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001700 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001701 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001702
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001703 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001704 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001705 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001706 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001707 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001708 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001709 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001710 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001711 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001712
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001713 #" Test for using a lambda function with incorrect return value
1714 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1715 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001716 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001717 call setline(1, 'eight')
1718 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1719 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001720
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001721 #" Test for clearing the 'omnifunc' option
1722 set omnifunc=''
1723 set omnifunc&
1724 call assert_fails("set omnifunc=function('abc')", "E700:")
1725 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001726
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001727 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001728 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001729 call setline(1, 'nine')
1730 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1731 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001732 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001733 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001734 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001735 bw!
1736 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001737 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001738
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001739 " Test for using a script-local function name
1740 func s:OmniFunc3(findstart, base)
1741 call add(g:OmniFunc3Args, [a:findstart, a:base])
1742 return a:findstart ? 0 : []
1743 endfunc
1744 set omnifunc=s:OmniFunc3
1745 new
1746 call setline(1, 'script1')
1747 let g:OmniFunc3Args = []
1748 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1749 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1750 bw!
1751
1752 let &omnifunc = 's:OmniFunc3'
1753 new
1754 call setline(1, 'script2')
1755 let g:OmniFunc3Args = []
1756 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1757 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1758 bw!
1759 delfunc s:OmniFunc3
1760
1761 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001762 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001763 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1764
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001765 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001766 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001767 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001768 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001769 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001770 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001771
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001772 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1773 func SetOmniFunc()
1774 let params = {'omni': function('g:DictOmniFunc')}
1775 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001776 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001777 func g:DictOmniFunc(_) dict
1778 endfunc
1779 call SetOmniFunc()
1780 new
1781 call SetOmniFunc()
1782 bw
1783 call test_garbagecollect_now()
1784 new
1785 set omnifunc=
1786 wincmd w
1787 set omnifunc=
1788 %bw!
1789 delfunc g:DictOmniFunc
1790 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001791
1792 " Vim9 tests
1793 let lines =<< trim END
1794 vim9script
1795
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001796 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1797 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001798 return findstart ? 0 : []
1799 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001800
1801 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001802 set omnifunc=function('Vim9omniFunc',\ [60])
1803 new | only
1804 setline(1, 'one')
1805 g:Vim9omniFunc_Args = []
1806 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1807 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1808 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001809
1810 # Test for using a global function name
1811 &omnifunc = g:OmniFunc2
1812 new | only
1813 setline(1, 'two')
1814 g:OmniFunc2Args = []
1815 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1816 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1817 bw!
1818
1819 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001820 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001821 add(g:LocalOmniFuncArgs, [findstart, base])
1822 return findstart ? 0 : []
1823 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001824 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001825 new | only
1826 setline(1, 'three')
1827 g:LocalOmniFuncArgs = []
1828 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1829 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1830 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001831 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001832 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001833
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001834 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001835 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001836 delfunc OmniFunc1
1837 delfunc OmniFunc2
1838 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001839 %bw!
1840endfunc
1841
1842" Test for different ways of setting the 'thesaurusfunc' option
1843func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001844 func TsrFunc1(callnr, findstart, base)
1845 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001846 return a:findstart ? 0 : []
1847 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001848 func TsrFunc2(findstart, base)
1849 call add(g:TsrFunc2Args, [a:findstart, a:base])
1850 return a:findstart ? 0 : ['sunday']
1851 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001852
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001853 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001854 #" Test for using a function name
1855 LET &thesaurusfunc = 'g:TsrFunc2'
1856 new
1857 call setline(1, 'zero')
1858 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001859 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001860 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1861 bw!
1862
1863 #" Test for using a function()
1864 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1865 new
1866 call setline(1, 'one')
1867 LET g:TsrFunc1Args = []
1868 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1869 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001870 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001871
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001872 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001873 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001874 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001875 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001876 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001877 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001878 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001879 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001880 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001881
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001882 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001883 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001884 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001885 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001886 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001887 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001888 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001889 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001890 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001891
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001892 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001893 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1894 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001895 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001896 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001897 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001898 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001899 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001900
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001901 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001902 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001903 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001904 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001905 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001906 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001907 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001908 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001909 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001910
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001911 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001912 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001913 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001914 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001915 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001916 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001917 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001918 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001919 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001920
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001921 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001922 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 LET optval = substitute(optval, ' ', '\\ ', 'g')
1924 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001925 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001927 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001929 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001930 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001931
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001932 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001933 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001934 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001936 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001937 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001938 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001939 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001940
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001942 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001943 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001945 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001947 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001948 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001949
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001950 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001951 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001952 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001953 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001954 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001955 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001956 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001957 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001958 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001959
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001960 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001961 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001962 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001963 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001964 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001965 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001966 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001967 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001968 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001969
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001970 #" Test for using a lambda function with incorrect return value
1971 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1972 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001973 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001974 call setline(1, 'eight')
1975 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1976 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001977
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001978 #" Test for clearing the 'thesaurusfunc' option
1979 set thesaurusfunc=''
1980 set thesaurusfunc&
1981 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1982 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001983
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001984 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001985 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001986 call setline(1, 'ten')
1987 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1988 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001989 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001990 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001991 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001992 bw!
1993
1994 #" Use a buffer-local value and a global value
1995 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001996 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001997 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001998 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001999 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2000 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002001 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002002 new
2003 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002004 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002005 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2006 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002007 call assert_equal([], g:TsrFunc1Args)
2008 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002009 wincmd w
2010 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002011 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002012 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2013 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002014 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002015 :%bw!
2016 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002017 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002018
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002019 " Test for using a script-local function name
2020 func s:TsrFunc3(findstart, base)
2021 call add(g:TsrFunc3Args, [a:findstart, a:base])
2022 return a:findstart ? 0 : []
2023 endfunc
2024 set tsrfu=s:TsrFunc3
2025 new
2026 call setline(1, 'script1')
2027 let g:TsrFunc3Args = []
2028 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2029 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2030 bw!
2031
2032 let &tsrfu = 's:TsrFunc3'
2033 new
2034 call setline(1, 'script2')
2035 let g:TsrFunc3Args = []
2036 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2037 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2038 bw!
2039 delfunc s:TsrFunc3
2040
2041 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002042 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002043 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2044
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002045 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002046 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002047 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002048 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002049 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002050 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002051 bw!
2052
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002053 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2054 func SetTsrFunc()
2055 let params = {'thesaurus': function('g:DictTsrFunc')}
2056 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002057 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002058 func g:DictTsrFunc(_) dict
2059 endfunc
2060 call SetTsrFunc()
2061 new
2062 call SetTsrFunc()
2063 bw
2064 call test_garbagecollect_now()
2065 new
2066 set thesaurusfunc=
2067 wincmd w
2068 %bw!
2069 delfunc SetTsrFunc
2070
2071 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2072 " cause a crash.
2073 func SetLocalTsrFunc()
2074 let params = {'thesaurus': function('g:DictTsrFunc')}
2075 let &l:thesaurusfunc = params.thesaurus
2076 endfunc
2077 call SetLocalTsrFunc()
2078 call test_garbagecollect_now()
2079 call SetLocalTsrFunc()
2080 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002081 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002082 delfunc g:DictTsrFunc
2083 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002084
2085 " Vim9 tests
2086 let lines =<< trim END
2087 vim9script
2088
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002089 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2090 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002091 return findstart ? 0 : []
2092 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002093
2094 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002095 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2096 new | only
2097 setline(1, 'one')
2098 g:Vim9tsrFunc_Args = []
2099 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2100 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2101 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002102
2103 # Test for using a global function name
2104 &thesaurusfunc = g:TsrFunc2
2105 new | only
2106 setline(1, 'two')
2107 g:TsrFunc2Args = []
2108 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2109 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2110 bw!
2111
2112 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002113 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002114 add(g:LocalTsrFuncArgs, [findstart, base])
2115 return findstart ? 0 : []
2116 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002117 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002118 new | only
2119 setline(1, 'three')
2120 g:LocalTsrFuncArgs = []
2121 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2122 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2123 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002124 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002125 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002126
2127 " cleanup
2128 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002129 delfunc TsrFunc1
2130 delfunc TsrFunc2
2131 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002132 %bw!
2133endfunc
2134
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002135" vim: shiftwidth=2 sts=2 expandtab