blob: 033bb37f1217b5b2c6418c453a6b23ceb3d58a80 [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'))
1297 close!
1298endfunc
1299
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001300" Test for different ways of setting the 'completefunc' option
1301func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001302 func CompleteFunc1(callnr, findstart, base)
1303 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1304 return a:findstart ? 0 : []
1305 endfunc
1306 func CompleteFunc2(findstart, base)
1307 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001308 return a:findstart ? 0 : []
1309 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001310
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001311 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001312 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001313 LET &completefunc = 'g:CompleteFunc2'
1314 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001315 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001316 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001317 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001318 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001319 bw!
1320
1321 #" Test for using a function()
1322 set completefunc=function('g:CompleteFunc1',\ [10])
1323 new
1324 call setline(1, 'one')
1325 LET g:CompleteFunc1Args = []
1326 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1327 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001329
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001330 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001331 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001332 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001333 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001334 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001337 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001338 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001339
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001340 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001341 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001342 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001343 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001344 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001345 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001346 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001347 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001349
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001350 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 set completefunc=funcref('g:CompleteFunc1',\ [13])
1352 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001353 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001354 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001355 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001356 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001357 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001358
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001359 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001360 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001361 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001362 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001363 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001364 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001365 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001366 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001367 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001368
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001369 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001370 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001371 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001372 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001374 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001375 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001376 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001378
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001380 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 LET optval = substitute(optval, ' ', '\\ ', 'g')
1382 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001383 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001384 call setline(1, 'five')
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([[16, 1, ''], [16, 0, 'five']], 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 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001391 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001392 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001393 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001394 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001396 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001398
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001400 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001401 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001402 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001403 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001405 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001407
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001408 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001409 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001411 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001412 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001413 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001414 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001415 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001416 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001417
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001418 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001419 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001420 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001421 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001422 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001423 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001424 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001425 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001426 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001427
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001428 #" Test for using a lambda function with incorrect return value
1429 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1430 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001431 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 call setline(1, 'eight')
1433 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1434 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001435
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001436 #" Test for clearing the 'completefunc' option
1437 set completefunc=''
1438 set completefunc&
1439 call assert_fails("set completefunc=function('abc')", "E700:")
1440 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001441
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001442 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001443 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001444 call setline(1, 'five')
1445 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1446 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001447 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001448 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001449 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001450 bw!
1451 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001452 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001453
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001454 " Test for using a script-local function name
1455 func s:CompleteFunc3(findstart, base)
1456 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1457 return a:findstart ? 0 : []
1458 endfunc
1459 set completefunc=s:CompleteFunc3
1460 new
1461 call setline(1, 'script1')
1462 let g:CompleteFunc3Args = []
1463 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1464 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1465 bw!
1466
1467 let &completefunc = 's:CompleteFunc3'
1468 new
1469 call setline(1, 'script2')
1470 let g:CompleteFunc3Args = []
1471 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1472 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1473 bw!
1474 delfunc s:CompleteFunc3
1475
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001476 " In Vim9 script s: can be omitted
1477 let lines =<< trim END
1478 vim9script
1479 var CompleteFunc4Args = []
1480 def CompleteFunc4(findstart: bool, base: string): any
1481 add(CompleteFunc4Args, [findstart, base])
1482 return findstart ? 0 : []
1483 enddef
1484 set completefunc=CompleteFunc4
1485 new
1486 setline(1, 'script1')
1487 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1488 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1489 bw!
1490 END
1491 call v9.CheckScriptSuccess(lines)
1492
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001493 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001494 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001495 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1496
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001497 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001498 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001499 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001500 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001501 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001502 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001503
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001504 " set 'completefunc' to a partial with dict. This used to cause a crash.
1505 func SetCompleteFunc()
1506 let params = {'complete': function('g:DictCompleteFunc')}
1507 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001508 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001509 func g:DictCompleteFunc(_) dict
1510 endfunc
1511 call SetCompleteFunc()
1512 new
1513 call SetCompleteFunc()
1514 bw
1515 call test_garbagecollect_now()
1516 new
1517 set completefunc=
1518 wincmd w
1519 set completefunc=
1520 %bw!
1521 delfunc g:DictCompleteFunc
1522 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001523
1524 " Vim9 tests
1525 let lines =<< trim END
1526 vim9script
1527
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001528 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1529 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001530 return findstart ? 0 : []
1531 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001532
1533 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001534 set completefunc=function('Vim9CompleteFunc',\ [60])
1535 new | only
1536 setline(1, 'one')
1537 g:Vim9completeFuncArgs = []
1538 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1539 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1540 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001541
1542 # Test for using a global function name
1543 &completefunc = g:CompleteFunc2
1544 new | only
1545 setline(1, 'two')
1546 g:CompleteFunc2Args = []
1547 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1548 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1549 bw!
1550
1551 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001552 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001553 add(g:LocalCompleteFuncArgs, [findstart, base])
1554 return findstart ? 0 : []
1555 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001556 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001557 new | only
1558 setline(1, 'three')
1559 g:LocalCompleteFuncArgs = []
1560 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1561 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1562 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001563 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001564 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001565
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001566 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001567 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001568 delfunc CompleteFunc1
1569 delfunc CompleteFunc2
1570 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001571 %bw!
1572endfunc
1573
1574" Test for different ways of setting the 'omnifunc' option
1575func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001576 func OmniFunc1(callnr, findstart, base)
1577 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1578 return a:findstart ? 0 : []
1579 endfunc
1580 func OmniFunc2(findstart, base)
1581 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001582 return a:findstart ? 0 : []
1583 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001584
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001585 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001586 #" Test for using a function name
1587 LET &omnifunc = 'g:OmniFunc2'
1588 new
1589 call setline(1, 'zero')
1590 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001591 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001592 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1593 bw!
1594
1595 #" Test for using a function()
1596 set omnifunc=function('g:OmniFunc1',\ [10])
1597 new
1598 call setline(1, 'one')
1599 LET g:OmniFunc1Args = []
1600 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1601 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001603
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001604 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001605 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001606 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001607 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001608 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001609 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001610 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001611 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001612 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001613
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001614 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001615 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001616 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001617 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001619 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001620 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001621 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001623
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 set omnifunc=funcref('g:OmniFunc1',\ [13])
1626 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001627 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001628 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001629 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001631 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001632
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001633 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001634 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001635 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001636 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001637 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001638 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001639 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001640 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001641 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001642
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001643 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001644 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001646 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001652
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001654 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 LET optval = substitute(optval, ' ', '\\ ', 'g')
1656 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001657 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 call setline(1, 'five')
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([[16, 1, ''], [16, 0, 'five']], 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 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001665 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001666 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001670 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001672
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001674 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001675 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001676 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001677 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001679 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001681
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001683 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001685 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001686 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001687 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001688 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001689 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001690 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001691
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001692 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001693 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001694 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001695 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001696 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001697 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001698 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001699 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001700 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001701
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001702 #" Test for using a lambda function with incorrect return value
1703 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1704 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001706 call setline(1, 'eight')
1707 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1708 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001709
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001710 #" Test for clearing the 'omnifunc' option
1711 set omnifunc=''
1712 set omnifunc&
1713 call assert_fails("set omnifunc=function('abc')", "E700:")
1714 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001715
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001716 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001717 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001718 call setline(1, 'nine')
1719 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1720 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001721 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001722 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001723 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001724 bw!
1725 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001726 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001727
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001728 " Test for using a script-local function name
1729 func s:OmniFunc3(findstart, base)
1730 call add(g:OmniFunc3Args, [a:findstart, a:base])
1731 return a:findstart ? 0 : []
1732 endfunc
1733 set omnifunc=s:OmniFunc3
1734 new
1735 call setline(1, 'script1')
1736 let g:OmniFunc3Args = []
1737 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1738 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1739 bw!
1740
1741 let &omnifunc = 's:OmniFunc3'
1742 new
1743 call setline(1, 'script2')
1744 let g:OmniFunc3Args = []
1745 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1746 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1747 bw!
1748 delfunc s:OmniFunc3
1749
1750 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001751 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001752 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1753
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001754 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001755 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001756 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001757 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001758 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001759 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001760
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001761 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1762 func SetOmniFunc()
1763 let params = {'omni': function('g:DictOmniFunc')}
1764 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001765 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001766 func g:DictOmniFunc(_) dict
1767 endfunc
1768 call SetOmniFunc()
1769 new
1770 call SetOmniFunc()
1771 bw
1772 call test_garbagecollect_now()
1773 new
1774 set omnifunc=
1775 wincmd w
1776 set omnifunc=
1777 %bw!
1778 delfunc g:DictOmniFunc
1779 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001780
1781 " Vim9 tests
1782 let lines =<< trim END
1783 vim9script
1784
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001785 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1786 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001787 return findstart ? 0 : []
1788 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001789
1790 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001791 set omnifunc=function('Vim9omniFunc',\ [60])
1792 new | only
1793 setline(1, 'one')
1794 g:Vim9omniFunc_Args = []
1795 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1796 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1797 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001798
1799 # Test for using a global function name
1800 &omnifunc = g:OmniFunc2
1801 new | only
1802 setline(1, 'two')
1803 g:OmniFunc2Args = []
1804 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1805 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1806 bw!
1807
1808 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001809 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001810 add(g:LocalOmniFuncArgs, [findstart, base])
1811 return findstart ? 0 : []
1812 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001813 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001814 new | only
1815 setline(1, 'three')
1816 g:LocalOmniFuncArgs = []
1817 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1818 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1819 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001820 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001821 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001822
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001823 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001824 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001825 delfunc OmniFunc1
1826 delfunc OmniFunc2
1827 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001828 %bw!
1829endfunc
1830
1831" Test for different ways of setting the 'thesaurusfunc' option
1832func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001833 func TsrFunc1(callnr, findstart, base)
1834 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001835 return a:findstart ? 0 : []
1836 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001837 func TsrFunc2(findstart, base)
1838 call add(g:TsrFunc2Args, [a:findstart, a:base])
1839 return a:findstart ? 0 : ['sunday']
1840 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001841
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001842 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001843 #" Test for using a function name
1844 LET &thesaurusfunc = 'g:TsrFunc2'
1845 new
1846 call setline(1, 'zero')
1847 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001848 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001849 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1850 bw!
1851
1852 #" Test for using a function()
1853 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1854 new
1855 call setline(1, 'one')
1856 LET g:TsrFunc1Args = []
1857 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1858 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001859 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001860
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001861 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001862 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001863 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001864 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001865 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001866 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001868 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001869 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001870
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001871 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001872 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001873 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001874 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001875 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001876 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001877 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001878 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001879 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001880
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001881 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001882 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1883 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001884 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001885 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001886 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001887 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001888 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001889
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001890 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001891 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001892 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001893 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001894 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001895 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001897 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001898 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001899
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001900 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001901 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001903 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001905 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001907 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001909
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001911 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 LET optval = substitute(optval, ' ', '\\ ', 'g')
1913 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001914 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001915 call setline(1, 'five')
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([[16, 1, ''], [16, 0, 'five']], 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 with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001922 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001923 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001924 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001925 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001927 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001929
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001930 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001931 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001932 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001933 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001934 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001936 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001937 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001938
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001939 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001940 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001942 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001943 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001944 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001945 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001946 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001947 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001948
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001949 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001950 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001952 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001953 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001954 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001955 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001956 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001958
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 #" Test for using a lambda function with incorrect return value
1960 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1961 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 call setline(1, 'eight')
1964 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1965 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001966
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001967 #" Test for clearing the 'thesaurusfunc' option
1968 set thesaurusfunc=''
1969 set thesaurusfunc&
1970 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1971 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001972
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001973 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001974 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975 call setline(1, 'ten')
1976 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1977 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001978 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001979 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001980 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001981 bw!
1982
1983 #" Use a buffer-local value and a global value
1984 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001985 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001986 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001987 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001988 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1989 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001990 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001991 new
1992 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001993 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001994 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1995 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001996 call assert_equal([], g:TsrFunc1Args)
1997 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001998 wincmd w
1999 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002000 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002001 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2002 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002003 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002004 :%bw!
2005 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002006 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002007
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002008 " Test for using a script-local function name
2009 func s:TsrFunc3(findstart, base)
2010 call add(g:TsrFunc3Args, [a:findstart, a:base])
2011 return a:findstart ? 0 : []
2012 endfunc
2013 set tsrfu=s:TsrFunc3
2014 new
2015 call setline(1, 'script1')
2016 let g:TsrFunc3Args = []
2017 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2018 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2019 bw!
2020
2021 let &tsrfu = 's:TsrFunc3'
2022 new
2023 call setline(1, 'script2')
2024 let g:TsrFunc3Args = []
2025 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2026 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2027 bw!
2028 delfunc s:TsrFunc3
2029
2030 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002031 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002032 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2033
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002034 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002035 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002036 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002037 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002038 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002039 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002040 bw!
2041
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002042 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2043 func SetTsrFunc()
2044 let params = {'thesaurus': function('g:DictTsrFunc')}
2045 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002046 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002047 func g:DictTsrFunc(_) dict
2048 endfunc
2049 call SetTsrFunc()
2050 new
2051 call SetTsrFunc()
2052 bw
2053 call test_garbagecollect_now()
2054 new
2055 set thesaurusfunc=
2056 wincmd w
2057 %bw!
2058 delfunc SetTsrFunc
2059
2060 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2061 " cause a crash.
2062 func SetLocalTsrFunc()
2063 let params = {'thesaurus': function('g:DictTsrFunc')}
2064 let &l:thesaurusfunc = params.thesaurus
2065 endfunc
2066 call SetLocalTsrFunc()
2067 call test_garbagecollect_now()
2068 call SetLocalTsrFunc()
2069 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002070 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002071 delfunc g:DictTsrFunc
2072 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002073
2074 " Vim9 tests
2075 let lines =<< trim END
2076 vim9script
2077
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002078 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2079 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002080 return findstart ? 0 : []
2081 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002082
2083 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002084 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2085 new | only
2086 setline(1, 'one')
2087 g:Vim9tsrFunc_Args = []
2088 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2089 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2090 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002091
2092 # Test for using a global function name
2093 &thesaurusfunc = g:TsrFunc2
2094 new | only
2095 setline(1, 'two')
2096 g:TsrFunc2Args = []
2097 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2098 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2099 bw!
2100
2101 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002102 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002103 add(g:LocalTsrFuncArgs, [findstart, base])
2104 return findstart ? 0 : []
2105 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002106 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002107 new | only
2108 setline(1, 'three')
2109 g:LocalTsrFuncArgs = []
2110 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2111 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2112 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002113 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002114 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002115
2116 " cleanup
2117 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002118 delfunc TsrFunc1
2119 delfunc TsrFunc2
2120 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002121 %bw!
2122endfunc
2123
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002124" vim: shiftwidth=2 sts=2 expandtab