blob: aca97d047e54f73da8f0b88755fbd210ae172775 [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.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010012 call mkdir('Xcpldir')
13 cd Xcpldir
Bram Moolenaarfb094e12017-11-05 20:59:28 +010014
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 ..
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100108 call delete('Xcpldir', '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
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100143func Test_omni_throw()
144 let g:CallCount = 0
145 func Omni(findstart, base)
146 let g:CallCount += 1
147 if a:findstart
148 throw "he he he"
149 endif
150 endfunc
151 set omnifunc=Omni
152 new
153 try
154 exe "normal ifoo\<C-x>\<C-o>"
155 call assert_false(v:true, 'command should have failed')
156 catch
157 call assert_exception('he he he')
158 call assert_equal(1, g:CallCount)
159 endtry
160
161 bwipe!
162 delfunc Omni
163 unlet g:CallCount
164 set omnifunc=
165endfunc
166
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200167func Test_omni_autoload()
168 let save_rtp = &rtp
169 set rtp=Xruntime/some
170 let dir = 'Xruntime/some/autoload'
171 call mkdir(dir, 'p')
172
173 let lines =<< trim END
174 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000175 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200176 if findstart
177 return 1
178 else
179 return ['match']
180 endif
181 enddef
182 {
183 eval 1 + 2
184 }
185 END
186 call writefile(lines, dir .. '/omni.vim')
187
188 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000189 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200190 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
191
192 bwipe!
193 call delete('Xruntime', 'rf')
194 set omnifunc=
195 let &rtp = save_rtp
196endfunc
197
Bram Moolenaarffa96842018-06-12 22:05:14 +0200198func Test_completefunc_args()
199 let s:args = []
200 func! CompleteFunc(findstart, base)
201 let s:args += [[a:findstart, empty(a:base)]]
202 endfunc
203 new
204
205 set completefunc=CompleteFunc
206 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200207 call assert_equal([1, 1], s:args[0])
208 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200209 set completefunc=
210
211 let s:args = []
212 set omnifunc=CompleteFunc
213 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200214 call assert_equal([1, 1], s:args[0])
215 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200216 set omnifunc=
217
218 bwipe!
219 unlet s:args
220 delfunc CompleteFunc
221endfunc
222
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100223func s:CompleteDone_CompleteFuncNone( findstart, base )
224 if a:findstart
225 return 0
226 endif
227
228 return v:none
229endfunc
230
Bram Moolenaar1e115362019-01-09 23:01:02 +0100231func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100232 if a:findstart
233 return 0
234 endif
235
236 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100237 \ 'words': [
238 \ {
239 \ 'word': 'aword',
240 \ 'abbr': 'wrd',
241 \ 'menu': 'extra text',
242 \ 'info': 'words are cool',
243 \ 'kind': 'W',
244 \ 'user_data': 'test'
245 \ }
246 \ ]
247 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100248endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100249
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100250func s:CompleteDone_CheckCompletedItemNone()
251 let s:called_completedone = 1
252endfunc
253
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100254func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100255 call assert_equal( 'aword', v:completed_item[ 'word' ] )
256 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
257 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
258 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
259 call assert_equal( 'W', v:completed_item[ 'kind' ] )
260 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
261
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100262 if a:pre
263 call assert_equal('function', complete_info().mode)
264 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100265
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100266 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100267endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100268
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100269func Test_CompleteDoneNone()
270 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 set completefunc=<SID>CompleteDone_CompleteFuncNone
274 execute "normal a\<C-X>\<C-U>\<C-Y>"
275 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200276 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100277
278 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200279 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100280
281 let s:called_completedone = 0
282 au! CompleteDone
283endfunc
284
285func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100286 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
287 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100288
289 set completefunc=<SID>CompleteDone_CompleteFuncDict
290 execute "normal a\<C-X>\<C-U>\<C-Y>"
291 set completefunc&
292
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100293 call assert_equal('test', v:completed_item[ 'user_data' ])
294 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100295
296 let s:called_completedone = 0
297 au! CompleteDone
298endfunc
299
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100300func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100301 if a:findstart
302 return 0
303 endif
304
305 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100306 \ 'words': [
307 \ {
308 \ 'word': 'aword',
309 \ 'abbr': 'wrd',
310 \ 'menu': 'extra text',
311 \ 'info': 'words are cool',
312 \ 'kind': 'W',
313 \ 'user_data': ['one', 'two'],
314 \ }
315 \ ]
316 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100317endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100318
Bram Moolenaar1e115362019-01-09 23:01:02 +0100319func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320 call assert_equal( 'aword', v:completed_item[ 'word' ] )
321 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
322 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
323 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
324 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100325 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100326
327 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100328endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100330func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100331 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
332
333 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
334 execute "normal a\<C-X>\<C-U>\<C-Y>"
335 set completefunc&
336
Bram Moolenaar08928322020-01-04 14:32:48 +0100337 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100338 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100339
340 let s:called_completedone = 0
341 au! CompleteDone
342endfunc
343
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100344func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100345 if a:findstart
346 return 0
347 endif
348
349 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100350endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100351
Bram Moolenaar1e115362019-01-09 23:01:02 +0100352func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100353 call assert_equal( 'aword', v:completed_item[ 'word' ] )
354 call assert_equal( '', v:completed_item[ 'abbr' ] )
355 call assert_equal( '', v:completed_item[ 'menu' ] )
356 call assert_equal( '', v:completed_item[ 'info' ] )
357 call assert_equal( '', v:completed_item[ 'kind' ] )
358 call assert_equal( '', v:completed_item[ 'user_data' ] )
359
360 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100361endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100362
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100363func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100364 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
365
366 set completefunc=<SID>CompleteDone_CompleteFuncList
367 execute "normal a\<C-X>\<C-U>\<C-Y>"
368 set completefunc&
369
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100370 call assert_equal('', v:completed_item[ 'user_data' ])
371 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100372
373 let s:called_completedone = 0
374 au! CompleteDone
375endfunc
376
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200377func Test_CompleteDone_undo()
378 au CompleteDone * call append(0, "prepend1")
379 new
380 call setline(1, ["line1", "line2"])
381 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
382 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
383 \ getline(1, '$'))
384 undo
385 call assert_equal(["line1", "line2"], getline(1, '$'))
386 bwipe!
387 au! CompleteDone
388endfunc
389
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100390func Test_CompleteDone_modify()
391 let value = {
392 \ 'word': '',
393 \ 'abbr': '',
394 \ 'menu': '',
395 \ 'info': '',
396 \ 'kind': '',
397 \ 'user_data': '',
398 \ }
399 let v:completed_item = value
zeertzjq75020942022-07-31 11:37:20 +0100400 call assert_equal(value, v:completed_item)
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100401endfunc
402
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200403func CompleteTest(findstart, query)
404 if a:findstart
405 return col('.')
406 endif
407 return ['matched']
408endfunc
409
410func Test_completefunc_info()
411 new
412 set completeopt=menuone
413 set completefunc=CompleteTest
414 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100415 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 +0200416 bwipe!
417 set completeopt&
418 set completefunc&
419endfunc
420
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100421" Check that when using feedkeys() typeahead does not interrupt searching for
422" completions.
423func Test_compl_feedkeys()
424 new
425 set completeopt=menuone,noselect
426 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
427 call assert_equal("jump jump", getline(1))
428 bwipe!
429 set completeopt&
430endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200431
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200432" Test for insert path completion with completeslash option
433func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200434 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200435
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100436 call mkdir('Xcpldir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200437 let orig_shellslash = &shellslash
438 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200439 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200440
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200441 set noshellslash
442
443 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100444 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100445 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200446
447 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100448 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100449 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200450
451 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100452 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100453 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200454
455 set shellslash
456
457 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100458 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100459 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200460
461 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100462 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100463 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200464
465 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100466 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100467 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200468 %bw!
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100469 call delete('Xcpldir', 'rf')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200470
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200471 set noshellslash
472 set completeslash=slash
473 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
474
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200475 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200476 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200477endfunc
478
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200479func Test_pum_stopped_by_timer()
480 CheckScreendump
481
482 let lines =<< trim END
483 call setline(1, ['hello', 'hullo', 'heeee', ''])
484 func StartCompl()
485 call timer_start(100, { -> execute('stopinsert') })
486 call feedkeys("Gah\<C-N>")
487 endfunc
488 END
489
490 call writefile(lines, 'Xpumscript')
491 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
492 call term_sendkeys(buf, ":call StartCompl()\<CR>")
493 call TermWait(buf, 200)
494 call term_sendkeys(buf, "k")
495 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
496
497 call StopVimInTerminal(buf)
498 call delete('Xpumscript')
499endfunc
500
zeertzjqcd5dbad2022-05-04 17:51:50 +0100501func Test_complete_stopinsert_startinsert()
502 nnoremap <F2> <Cmd>startinsert<CR>
503 inoremap <F2> <Cmd>stopinsert<CR>
504 " This just checks if this causes an error
505 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
506 nunmap <F2>
507 iunmap <F2>
508endfunc
509
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100510func Test_pum_with_folds_two_tabs()
511 CheckScreendump
512
513 let lines =<< trim END
514 set fdm=marker
515 call setline(1, ['" x {{{1', '" a some text'])
516 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
517 norm! zm
518 tab sp
519 call feedkeys('2Gzv', 'xt')
520 call feedkeys("0fa", 'xt')
521 END
522
523 call writefile(lines, 'Xpumscript')
524 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200525 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100526 call term_sendkeys(buf, "a\<C-N>")
527 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
528
529 call term_sendkeys(buf, "\<Esc>")
530 call StopVimInTerminal(buf)
531 call delete('Xpumscript')
532endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100533
534func Test_pum_with_preview_win()
535 CheckScreendump
536
537 let lines =<< trim END
538 funct Omni_test(findstart, base)
539 if a:findstart
540 return col(".") - 1
541 endif
542 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
543 endfunc
544 set omnifunc=Omni_test
545 set completeopt+=longest
546 END
547
548 call writefile(lines, 'Xpreviewscript')
549 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200550 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100551 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200552 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100553 call term_sendkeys(buf, "\<C-N>")
554 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
555
556 call term_sendkeys(buf, "\<Esc>")
557 call StopVimInTerminal(buf)
558 call delete('Xpreviewscript')
559endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100560
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000561func Test_scrollbar_on_wide_char()
562 CheckScreendump
563
564 let lines =<< trim END
565 call setline(1, ['a', ' 啊啊啊',
566 \ ' 哦哦哦',
567 \ ' 呃呃呃'])
568 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
569 END
570 call writefile(lines, 'Xwidescript')
571 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
572 call term_sendkeys(buf, "A\<C-N>")
573 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
574
575 call StopVimInTerminal(buf)
576 call delete('Xwidescript')
577endfunc
578
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100579" Test for inserting the tag search pattern in insert mode
580func Test_ins_compl_tag_sft()
581 call writefile([
582 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
583 \ "first\tXfoo\t/^int first() {}$/",
584 \ "second\tXfoo\t/^int second() {}$/",
585 \ "third\tXfoo\t/^int third() {}$/"],
586 \ 'Xtags')
587 set tags=Xtags
588 let code =<< trim [CODE]
589 int first() {}
590 int second() {}
591 int third() {}
592 [CODE]
593 call writefile(code, 'Xfoo')
594
595 enew
596 set showfulltag
597 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
598 call assert_equal('int second() {}', getline(1))
599 set noshowfulltag
600
601 call delete('Xtags')
602 call delete('Xfoo')
603 set tags&
604 %bwipe!
605endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200606
607" Test for 'completefunc' deleting text
608func Test_completefunc_error()
609 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200610 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200611 func CompleteFunc(findstart, base)
612 if a:findstart == 1
613 normal dd
614 return col('.') - 1
615 endif
616 return ['a', 'b']
617 endfunc
618 set completefunc=CompleteFunc
619 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100620 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200621
622 " delete text when called for the second time
623 func CompleteFunc2(findstart, base)
624 if a:findstart == 1
625 return col('.') - 1
626 endif
627 normal dd
628 return ['a', 'b']
629 endfunc
630 set completefunc=CompleteFunc2
631 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100632 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200633
Bram Moolenaar97202d92021-01-28 18:34:35 +0100634 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100635 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100636 if a:findstart == 1
637 return col('.') - 1
638 endif
639 wincmd p
640 return ['a', 'b']
641 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100642 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100643 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100644 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100645 close!
646
647 set completefunc&
648 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100649 delfunc CompleteFunc2
650 delfunc CompleteFunc3
651 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100652endfunc
653
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200654" Test for returning non-string values from 'completefunc'
655func Test_completefunc_invalid_data()
656 new
657 func! CompleteFunc(findstart, base)
658 if a:findstart == 1
659 return col('.') - 1
660 endif
661 return [{}, '', 'moon']
662 endfunc
663 set completefunc=CompleteFunc
664 exe "normal i\<C-X>\<C-U>"
665 call assert_equal('moon', getline(1))
666 set completefunc&
667 close!
668endfunc
669
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200670" Test for errors in using complete() function
671func Test_complete_func_error()
672 call assert_fails('call complete(1, ["a"])', 'E785:')
673 func ListColors()
674 call complete(col('.'), "blue")
675 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100676 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200677 func ListMonths()
678 call complete(col('.'), test_null_list())
679 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100680 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200681 delfunc ListColors
682 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100683 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200684 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200685endfunc
686
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000687" Test for recursively starting completion mode using complete()
688func Test_recursive_complete_func()
689 func ListColors()
690 call complete(5, ["red", "blue"])
691 return ''
692 endfunc
693 new
694 call setline(1, ['a1', 'a2'])
695 set complete=.
696 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
697 call assert_equal('a2blue', getline(3))
698 delfunc ListColors
699 bw!
700endfunc
701
bfredl87af60c2022-09-24 11:17:51 +0100702" Test for using complete() with completeopt+=longest
703func Test_complete_with_longest()
704 inoremap <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
705 new
706
707 " default: insert first match
708 set completeopt&
709 call setline(1, ['i'])
710 exe "normal Aa\<f3>\<esc>"
711 call assert_equal('iaax', getline(1))
712
713 " with longest: insert longest prefix
714 set completeopt+=longest
715 call setline(1, ['i'])
716 exe "normal Aa\<f3>\<esc>"
717 call assert_equal('iaa', getline(1))
718 set completeopt&
719endfunc
720
721
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200722" Test for completing words following a completed word in a line
723func Test_complete_wrapscan()
724 " complete words from another buffer
725 new
726 call setline(1, ['one two', 'three four'])
727 new
728 setlocal complete=w
729 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
730 call assert_equal('two three four', getline(1))
731 close!
732 " complete words from the current buffer
733 setlocal complete=.
734 %d
735 call setline(1, ['one two', ''])
736 call cursor(2, 1)
737 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
738 call assert_equal('one two one two', getline(2))
739 close!
740endfunc
741
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200742" Test for completing special characters
743func Test_complete_special_chars()
744 new
745 call setline(1, 'int .*[-\^$ func float')
746 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
747 call assert_equal('int .*[-\^$ func float', getline(2))
748 close!
749endfunc
750
751" Test for completion when text is wrapped across lines.
752func Test_complete_across_line()
753 new
754 call setline(1, ['red green blue', 'one two three'])
755 setlocal textwidth=20
756 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
757 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
758 close!
759endfunc
760
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000761" Test for completing words with a '.' at the end of a word.
762func Test_complete_joinspaces()
763 new
764 call setline(1, ['one two.', 'three. four'])
765 set joinspaces
766 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
767 call assert_equal("one two. three. four", getline(3))
768 set joinspaces&
769 bw!
770endfunc
771
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200772" Test for using CTRL-L to add one character when completing matching
773func Test_complete_add_onechar()
774 new
775 call setline(1, ['wool', 'woodwork'])
776 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
777 call assert_equal('woof', getline(3))
778
779 " use 'ignorecase' and backspace to erase characters from the prefix string
780 " and then add letters using CTRL-L
781 %d
782 set ignorecase backspace=2
783 setlocal complete=.
784 call setline(1, ['workhorse', 'workload'])
785 normal Go
786 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
787 call assert_equal('workh', getline(3))
788 set ignorecase& backspace&
789 close!
790endfunc
791
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000792" Test for using CTRL-X CTRL-L to complete whole lines lines
793func Test_complete_wholeline()
794 new
795 " complete one-line
796 call setline(1, ['a1', 'a2'])
797 exe "normal ggoa\<C-X>\<C-L>"
798 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
799 " go to the next match (wrapping around the buffer)
800 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
801 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
802 " go to the next match
803 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
804 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
805 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
806 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
807 " repeat the test using CTRL-L
808 " go to the next match (wrapping around the buffer)
809 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
810 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
811 " go to the next match
812 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
813 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
814 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
815 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
816 %d
817 " use CTRL-X CTRL-L to add one more line
818 call setline(1, ['a1', 'b1'])
819 setlocal complete=.
820 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
821 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
822 bw!
823endfunc
824
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200825" Test insert completion with 'cindent' (adjust the indent)
826func Test_complete_with_cindent()
827 new
828 setlocal cindent
829 call setline(1, ['if (i == 1)', " j = 2;"])
830 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
831 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
832
833 %d
834 call setline(1, ['when while', '{', ''])
835 setlocal cinkeys+==while
836 exe "normal Giwh\<C-P> "
837 call assert_equal("\twhile ", getline('$'))
838 close!
839endfunc
840
841" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
842func Test_complete_cmdline()
843 new
844 exe "normal icaddb\<C-X>\<C-V>"
845 call assert_equal('caddbuffer', getline(1))
846 exe "normal ocall getqf\<C-X>\<C-V>"
847 call assert_equal('call getqflist(', getline(2))
848 exe "normal oabcxyz(\<C-X>\<C-V>"
849 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200850 com! -buffer TestCommand1 echo 'TestCommand1'
851 com! -buffer TestCommand2 echo 'TestCommand2'
852 write TestCommand1Test
853 write TestCommand2Test
854 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
855 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
856 call assert_equal('TestCommand2Test', getline(4))
857 call delete('TestCommand1Test')
858 call delete('TestCommand2Test')
859 delcom TestCommand1
860 delcom TestCommand2
861 close!
862endfunc
863
864" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
865func Test_complete_stop()
866 new
867 func Save_mode1()
868 let g:mode1 = mode(1)
869 return ''
870 endfunc
871 func Save_mode2()
872 let g:mode2 = mode(1)
873 return ''
874 endfunc
875 inoremap <F1> <C-R>=Save_mode1()<CR>
876 inoremap <F2> <C-R>=Save_mode2()<CR>
877 call setline(1, ['aaa bbb ccc '])
878 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
879 call assert_equal('ic', g:mode1)
880 call assert_equal('i', g:mode2)
881 call assert_equal('aaa bbb ccc ', getline(1))
882 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
883 call assert_equal('ic', g:mode1)
884 call assert_equal('i', g:mode2)
885 call assert_equal('aaa bbb ccc aaa', getline(1))
886 set completeopt+=noselect
887 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
888 call assert_equal('ic', g:mode1)
889 call assert_equal('i', g:mode2)
890 call assert_equal('aaa bbb ccc aaa bb', getline(1))
891 set completeopt&
892 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
893 call assert_equal('ic', g:mode1)
894 call assert_equal('i', g:mode2)
895 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
896 com! -buffer TestCommand1 echo 'TestCommand1'
897 com! -buffer TestCommand2 echo 'TestCommand2'
898 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
899 call assert_equal('ic', g:mode1)
900 call assert_equal('i', g:mode2)
901 call assert_equal('TestCommand2', getline(2))
902 delcom TestCommand1
903 delcom TestCommand2
904 unlet g:mode1
905 unlet g:mode2
906 iunmap <F1>
907 iunmap <F2>
908 delfunc Save_mode1
909 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200910 close!
911endfunc
912
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000913" Test for typing CTRL-R in insert completion mode to insert a register
914" content.
915func Test_complete_reginsert()
916 new
917 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
918
919 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
920 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
921 " inserted.
922 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
923 call assert_equal('a123', getline(5))
924 let @r = "\<C-P>\<C-P>"
925 exe "normal GCa\<C-P>\<C-R>r"
926 call assert_equal('a12', getline(5))
927 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
928 call assert_equal('a1234x', getline(5))
929 bw!
930endfunc
931
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200932func Test_issue_7021()
933 CheckMSWindows
934
935 let orig_shellslash = &shellslash
936 set noshellslash
937
938 set completeslash=slash
939 call assert_false(expand('~') =~ '/')
940
941 let &shellslash = orig_shellslash
942 set completeslash=
943endfunc
944
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000945" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
946func Test_complete_longest_match()
947 for e in ['latin1', 'utf-8']
948 exe 'set encoding=' .. e
949 new
950 set complete=.
951 set completeopt=menu,longest
952 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
953 exe "normal Gopfx\<C-P>"
954 call assert_equal('pfx_', getline(5))
955 bw!
956 endfor
957
958 " Test for completing additional words with longest match set
959 new
960 call setline(1, ['abc1', 'abd2'])
961 exe "normal Goab\<C-P>\<C-X>\<C-P>"
962 call assert_equal('ab', getline(3))
963 bw!
964 set complete& completeopt&
965endfunc
966
967" Test for removing the first displayed completion match and selecting the
968" match just before that.
969func Test_complete_erase_firstmatch()
970 new
971 call setline(1, ['a12', 'a34', 'a56'])
972 set complete=.
973 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
974 call assert_equal('a34', getline('$'))
975 set complete&
976 bw!
977endfunc
978
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000979" Test for completing words from unloaded buffers
980func Test_complete_from_unloadedbuf()
981 call writefile(['abc'], "Xfile1")
982 call writefile(['def'], "Xfile2")
983 edit Xfile1
984 edit Xfile2
985 new | close
986 enew
987 bunload Xfile1 Xfile2
988 set complete=u
989 " complete from an unloaded buffer
990 exe "normal! ia\<C-P>"
991 call assert_equal('abc', getline(1))
992 exe "normal! od\<C-P>"
993 call assert_equal('def', getline(2))
994 set complete&
995 %bw!
996 call delete("Xfile1")
997 call delete("Xfile2")
998endfunc
999
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001000" Test for completing whole lines from unloaded buffers
1001func Test_complete_wholeline_unloadedbuf()
1002 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1003 edit Xfile1
1004 enew
1005 set complete=u
1006 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1007 call assert_equal('a line2', getline(1))
1008 %d
1009 " completing from an unlisted buffer should fail
1010 bdel Xfile1
1011 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1012 call assert_equal('a', getline(1))
1013 set complete&
1014 %bw!
1015 call delete("Xfile1")
1016endfunc
1017
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001018" Test for completing words from unlisted buffers
1019func Test_complete_from_unlistedbuf()
1020 call writefile(['abc'], "Xfile1")
1021 call writefile(['def'], "Xfile2")
1022 edit Xfile1
1023 edit Xfile2
1024 new | close
1025 bdel Xfile1 Xfile2
1026 set complete=U
1027 " complete from an unlisted buffer
1028 exe "normal! ia\<C-P>"
1029 call assert_equal('abc', getline(1))
1030 exe "normal! od\<C-P>"
1031 call assert_equal('def', getline(2))
1032 set complete&
1033 %bw!
1034 call delete("Xfile1")
1035 call delete("Xfile2")
1036endfunc
1037
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001038" Test for completing whole lines from unlisted buffers
1039func Test_complete_wholeline_unlistedbuf()
1040 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1041 edit Xfile1
1042 enew
1043 set complete=U
1044 " completing from a unloaded buffer should fail
1045 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1046 call assert_equal('a', getline(1))
1047 %d
1048 bdel Xfile1
1049 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1050 call assert_equal('a line2', getline(1))
1051 set complete&
1052 %bw!
1053 call delete("Xfile1")
1054endfunc
1055
1056" Test for adding a multibyte character using CTRL-L in completion mode
1057func Test_complete_mbyte_char_add()
1058 new
1059 set complete=.
1060 call setline(1, 'abė')
1061 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1062 call assert_equal('abė', getline(2))
1063 " Test for a leader with multibyte character
1064 %d
1065 call setline(1, 'abėĕ')
1066 exe "normal! oabė\<C-P>"
1067 call assert_equal('abėĕ', getline(2))
1068 bw!
1069endfunc
1070
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001071" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1072" not to complete matches from the local buffer. Also test using multiple
1073" <C-X> to cancel the current completion mode.
1074func Test_complete_local_expansion()
1075 new
1076 set complete=t
1077 call setline(1, ['abc', 'def'])
1078 exe "normal! Go\<C-X>\<C-P>"
1079 call assert_equal("def", getline(3))
1080 exe "normal! Go\<C-P>"
1081 call assert_equal("", getline(4))
1082 exe "normal! Go\<C-X>\<C-N>"
1083 call assert_equal("abc", getline(5))
1084 exe "normal! Go\<C-N>"
1085 call assert_equal("", getline(6))
1086
1087 " use multiple <C-X> to cancel the previous completion mode
1088 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1089 call assert_equal("", getline(7))
1090 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1091 call assert_equal("", getline(8))
1092 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1093 call assert_equal("abc", getline(9))
1094
1095 " interrupt the current completion mode
1096 set completeopt=menu,noinsert
1097 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1098 call assert_equal("abc", getline(10))
1099
1100 " when only one <C-X> is used to interrupt, do normal expansion
1101 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1102 call assert_equal("", getline(11))
1103 set completeopt&
1104
1105 " using two <C-X> in non-completion mode and restarting the same mode
1106 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1107 call assert_equal("def", getline(12))
1108
1109 " test for adding a match from the original empty text
1110 %d
1111 call setline(1, 'abc def g')
1112 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1113 call assert_equal('def', getline(2))
1114 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1115 call assert_equal('abc', getline(2))
1116
1117 bw!
1118endfunc
1119
1120" Test for undoing changes after a insert-mode completion
1121func Test_complete_undo()
1122 new
1123 set complete=.
1124 " undo with 'ignorecase'
1125 call setline(1, ['ABOVE', 'BELOW'])
1126 set ignorecase
1127 exe "normal! Goab\<C-G>u\<C-P>"
1128 call assert_equal("ABOVE", getline(3))
1129 undo
1130 call assert_equal("ab", getline(3))
1131 set ignorecase&
1132 %d
1133 " undo with longest match
1134 set completeopt=menu,longest
1135 call setline(1, ['above', 'about'])
1136 exe "normal! Goa\<C-G>u\<C-P>"
1137 call assert_equal("abo", getline(3))
1138 undo
1139 call assert_equal("a", getline(3))
1140 set completeopt&
1141 %d
1142 " undo for line completion
1143 call setline(1, ['above that change', 'below that change'])
1144 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1145 call assert_equal("above that change", getline(3))
1146 undo
1147 call assert_equal("above", getline(3))
1148
1149 bw!
1150endfunc
1151
1152" Test for completing a very long word
1153func Test_complete_long_word()
1154 set complete&
1155 new
1156 call setline(1, repeat('x', 950) .. ' one two three')
1157 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1158 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1159 %d
1160 " should fail when more than 950 characters are in a word
1161 call setline(1, repeat('x', 951) .. ' one two three')
1162 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1163 call assert_equal(repeat('x', 951), getline(2))
1164
1165 " Test for adding a very long word to an existing completion
1166 %d
1167 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1168 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1169 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1170 bw!
1171endfunc
1172
1173" Test for some fields in the complete items used by complete()
1174func Test_complete_items()
1175 func CompleteItems(idx)
1176 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1177 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1178 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1179 \ [#{user_data: 'u9'}],
1180 \ [#{word: "", user_data: 'u10'}],
1181 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1182 call complete(col('.'), items[a:idx])
1183 return ''
1184 endfunc
1185 new
1186 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1187 call assert_equal('u2', v:completed_item.user_data)
1188 call assert_equal('one', getline(1))
1189 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1190 call assert_equal('u3', v:completed_item.user_data)
1191 call assert_equal('one', getline(2))
1192 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1193 call assert_equal('', getline(3))
1194 set completeopt=menu,noinsert
1195 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1196 call assert_equal('oNE', getline(4))
1197 call assert_equal('u8', v:completed_item.user_data)
1198 set completeopt&
1199 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1200 call assert_equal('', getline(5))
1201 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1202 call assert_equal('', getline(6))
1203 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1204 call assert_equal('', getline(7))
1205 call assert_equal('u11', v:completed_item.user_data)
1206 " pass invalid argument to complete()
1207 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1208 call assert_fails('exe cmd', 'E730:')
1209 bw!
1210 delfunc CompleteItems
1211endfunc
1212
1213" Test for the "refresh" item in the dict returned by an insert completion
1214" function
1215func Test_complete_item_refresh_always()
1216 let g:CallCount = 0
1217 func! Tcomplete(findstart, base)
1218 if a:findstart
1219 " locate the start of the word
1220 let line = getline('.')
1221 let start = col('.') - 1
1222 while start > 0 && line[start - 1] =~ '\a'
1223 let start -= 1
1224 endwhile
1225 return start
1226 else
1227 let g:CallCount += 1
1228 let res = ["update1", "update12", "update123"]
1229 return #{words: res, refresh: 'always'}
1230 endif
1231 endfunc
1232 new
1233 set completeopt=menu,longest
1234 set completefunc=Tcomplete
1235 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1236 call assert_equal('up', getline(1))
1237 call assert_equal(2, g:CallCount)
1238 set completeopt&
1239 set completefunc&
1240 bw!
1241 delfunc Tcomplete
1242endfunc
1243
1244" Test for completing from a thesaurus file without read permission
1245func Test_complete_unreadable_thesaurus_file()
1246 CheckUnix
1247 CheckNotRoot
1248
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001249 call writefile(['about', 'above'], 'Xunrfile')
1250 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001251 new
1252 set complete=sXfile
1253 exe "normal! ia\<C-P>"
1254 call assert_equal('a', getline(1))
1255 bw!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001256 call delete('Xunrfile')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001257 set complete&
1258endfunc
1259
Bram Moolenaarcc233582020-12-12 13:32:07 +01001260" Test to ensure 'Scanning...' messages are not recorded in messages history
1261func Test_z1_complete_no_history()
1262 new
1263 messages clear
1264 let currmess = execute('messages')
1265 setlocal dictionary=README.txt
1266 exe "normal owh\<C-X>\<C-K>"
1267 exe "normal owh\<C-N>"
1268 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001269 bwipe!
1270endfunc
1271
1272" A mapping is not used for the key after CTRL-X.
1273func Test_no_mapping_for_ctrl_x_key()
1274 new
1275 inoremap <C-K> <Cmd>let was_mapped = 'yes'<CR>
1276 setlocal dictionary=README.txt
1277 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1278 call assert_equal('example ', getline(1))
1279 call assert_false(exists('was_mapped'))
1280 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001281endfunc
1282
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001283" Test for different ways of setting the 'completefunc' option
1284func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001285 func CompleteFunc1(callnr, findstart, base)
1286 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1287 return a:findstart ? 0 : []
1288 endfunc
1289 func CompleteFunc2(findstart, base)
1290 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001291 return a:findstart ? 0 : []
1292 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001293
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001294 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001295 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001296 LET &completefunc = 'g:CompleteFunc2'
1297 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001298 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001299 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001300 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001301 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001302 bw!
1303
1304 #" Test for using a function()
1305 set completefunc=function('g:CompleteFunc1',\ [10])
1306 new
1307 call setline(1, 'one')
1308 LET g:CompleteFunc1Args = []
1309 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1310 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001311 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001312
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001313 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001314 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001315 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001316 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001317 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001318 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001319 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001320 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001321 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001322
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001323 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001324 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001325 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001326 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001327 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001328 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001329 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001330 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001331 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001332
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001333 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001334 set completefunc=funcref('g:CompleteFunc1',\ [13])
1335 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001337 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001338 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001339 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001340 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001341
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001342 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001343 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001344 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001345 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001346 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001347 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001349 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001350 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001351
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001353 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001354 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001355 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001356 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001357 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001358 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001359 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001360 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001361
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001362 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001363 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001364 LET optval = substitute(optval, ' ', '\\ ', 'g')
1365 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001366 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001367 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001368 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001369 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001370 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001371 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001372
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001374 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001375 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001376 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001377 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001378 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001379 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001380 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001381
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001382 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001383 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001384 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001385 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001386 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001387 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001388 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001389 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001390
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001391 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001392 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001393 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001394 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001396 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001398 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001400
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001401 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001402 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001403 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001404 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001405 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001406 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001407 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001408 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001409 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001410
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001411 #" Test for using a lambda function with incorrect return value
1412 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1413 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001414 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 call setline(1, 'eight')
1416 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1417 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001418
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001419 #" Test for clearing the 'completefunc' option
1420 set completefunc=''
1421 set completefunc&
1422 call assert_fails("set completefunc=function('abc')", "E700:")
1423 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001424
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001425 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001426 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001427 call setline(1, 'five')
1428 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1429 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001430 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001431 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001432 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001433 bw!
1434 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001435 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001436
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001437 " Test for using a script-local function name
1438 func s:CompleteFunc3(findstart, base)
1439 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1440 return a:findstart ? 0 : []
1441 endfunc
1442 set completefunc=s:CompleteFunc3
1443 new
1444 call setline(1, 'script1')
1445 let g:CompleteFunc3Args = []
1446 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1447 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1448 bw!
1449
1450 let &completefunc = 's:CompleteFunc3'
1451 new
1452 call setline(1, 'script2')
1453 let g:CompleteFunc3Args = []
1454 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1455 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1456 bw!
1457 delfunc s:CompleteFunc3
1458
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001459 " In Vim9 script s: can be omitted
1460 let lines =<< trim END
1461 vim9script
1462 var CompleteFunc4Args = []
1463 def CompleteFunc4(findstart: bool, base: string): any
1464 add(CompleteFunc4Args, [findstart, base])
1465 return findstart ? 0 : []
1466 enddef
1467 set completefunc=CompleteFunc4
1468 new
1469 setline(1, 'script1')
1470 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1471 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1472 bw!
1473 END
1474 call v9.CheckScriptSuccess(lines)
1475
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001476 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001477 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001478 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1479
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001480 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001481 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001482 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001483 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001484 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001486
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001487 " set 'completefunc' to a partial with dict. This used to cause a crash.
1488 func SetCompleteFunc()
1489 let params = {'complete': function('g:DictCompleteFunc')}
1490 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001491 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001492 func g:DictCompleteFunc(_) dict
1493 endfunc
1494 call SetCompleteFunc()
1495 new
1496 call SetCompleteFunc()
1497 bw
1498 call test_garbagecollect_now()
1499 new
1500 set completefunc=
1501 wincmd w
1502 set completefunc=
1503 %bw!
1504 delfunc g:DictCompleteFunc
1505 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001506
1507 " Vim9 tests
1508 let lines =<< trim END
1509 vim9script
1510
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001511 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1512 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001513 return findstart ? 0 : []
1514 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001515
1516 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001517 set completefunc=function('Vim9CompleteFunc',\ [60])
1518 new | only
1519 setline(1, 'one')
1520 g:Vim9completeFuncArgs = []
1521 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1522 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1523 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001524
1525 # Test for using a global function name
1526 &completefunc = g:CompleteFunc2
1527 new | only
1528 setline(1, 'two')
1529 g:CompleteFunc2Args = []
1530 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1531 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1532 bw!
1533
1534 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001535 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001536 add(g:LocalCompleteFuncArgs, [findstart, base])
1537 return findstart ? 0 : []
1538 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001539 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001540 new | only
1541 setline(1, 'three')
1542 g:LocalCompleteFuncArgs = []
1543 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1544 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1545 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001546 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001547 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001548
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001549 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001550 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001551 delfunc CompleteFunc1
1552 delfunc CompleteFunc2
1553 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001554 %bw!
1555endfunc
1556
1557" Test for different ways of setting the 'omnifunc' option
1558func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001559 func OmniFunc1(callnr, findstart, base)
1560 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1561 return a:findstart ? 0 : []
1562 endfunc
1563 func OmniFunc2(findstart, base)
1564 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001565 return a:findstart ? 0 : []
1566 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001567
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001568 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001569 #" Test for using a function name
1570 LET &omnifunc = 'g:OmniFunc2'
1571 new
1572 call setline(1, 'zero')
1573 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001574 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001575 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1576 bw!
1577
1578 #" Test for using a function()
1579 set omnifunc=function('g:OmniFunc1',\ [10])
1580 new
1581 call setline(1, 'one')
1582 LET g:OmniFunc1Args = []
1583 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1584 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001585 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001586
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001587 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001588 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001589 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001590 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001591 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001592 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001594 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001595 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001596
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001598 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001599 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001600 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001602 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001603 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001604 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001606
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001607 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001608 set omnifunc=funcref('g:OmniFunc1',\ [13])
1609 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001610 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001611 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001612 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001613 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001614 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001615
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001616 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001617 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001619 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001620 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001621 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001623 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001625
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001629 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001631 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001632 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001633 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001635
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001637 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 LET optval = substitute(optval, ' ', '\\ ', 'g')
1639 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001640 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001641 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001642 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001643 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001644 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001646
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001648 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001649 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001650 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001651 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001652 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001653 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001654 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001655
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001656 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001657 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001658 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001659 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001660 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001661 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001662 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001663 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001664
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001665 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001666 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001670 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001672 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001674
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001676 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001677 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001678 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001679 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001680 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001681 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001682 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001683 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001684
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 #" Test for using a lambda function with incorrect return value
1686 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1687 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001688 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 call setline(1, 'eight')
1690 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1691 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001692
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 #" Test for clearing the 'omnifunc' option
1694 set omnifunc=''
1695 set omnifunc&
1696 call assert_fails("set omnifunc=function('abc')", "E700:")
1697 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001698
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001699 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001700 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001701 call setline(1, 'nine')
1702 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1703 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001704 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001705 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001706 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001707 bw!
1708 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001709 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001710
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001711 " Test for using a script-local function name
1712 func s:OmniFunc3(findstart, base)
1713 call add(g:OmniFunc3Args, [a:findstart, a:base])
1714 return a:findstart ? 0 : []
1715 endfunc
1716 set omnifunc=s:OmniFunc3
1717 new
1718 call setline(1, 'script1')
1719 let g:OmniFunc3Args = []
1720 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1721 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1722 bw!
1723
1724 let &omnifunc = 's:OmniFunc3'
1725 new
1726 call setline(1, 'script2')
1727 let g:OmniFunc3Args = []
1728 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1729 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1730 bw!
1731 delfunc s:OmniFunc3
1732
1733 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001734 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001735 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1736
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001737 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001738 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001739 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001740 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001741 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001742 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001743
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001744 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1745 func SetOmniFunc()
1746 let params = {'omni': function('g:DictOmniFunc')}
1747 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001748 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001749 func g:DictOmniFunc(_) dict
1750 endfunc
1751 call SetOmniFunc()
1752 new
1753 call SetOmniFunc()
1754 bw
1755 call test_garbagecollect_now()
1756 new
1757 set omnifunc=
1758 wincmd w
1759 set omnifunc=
1760 %bw!
1761 delfunc g:DictOmniFunc
1762 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001763
1764 " Vim9 tests
1765 let lines =<< trim END
1766 vim9script
1767
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001768 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1769 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001770 return findstart ? 0 : []
1771 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001772
1773 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001774 set omnifunc=function('Vim9omniFunc',\ [60])
1775 new | only
1776 setline(1, 'one')
1777 g:Vim9omniFunc_Args = []
1778 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1779 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1780 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001781
1782 # Test for using a global function name
1783 &omnifunc = g:OmniFunc2
1784 new | only
1785 setline(1, 'two')
1786 g:OmniFunc2Args = []
1787 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1788 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1789 bw!
1790
1791 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001792 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001793 add(g:LocalOmniFuncArgs, [findstart, base])
1794 return findstart ? 0 : []
1795 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001796 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001797 new | only
1798 setline(1, 'three')
1799 g:LocalOmniFuncArgs = []
1800 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1801 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1802 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001803 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001804 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001805
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001806 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001807 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001808 delfunc OmniFunc1
1809 delfunc OmniFunc2
1810 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001811 %bw!
1812endfunc
1813
1814" Test for different ways of setting the 'thesaurusfunc' option
1815func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001816 func TsrFunc1(callnr, findstart, base)
1817 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001818 return a:findstart ? 0 : []
1819 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001820 func TsrFunc2(findstart, base)
1821 call add(g:TsrFunc2Args, [a:findstart, a:base])
1822 return a:findstart ? 0 : ['sunday']
1823 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001824
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001825 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001826 #" Test for using a function name
1827 LET &thesaurusfunc = 'g:TsrFunc2'
1828 new
1829 call setline(1, 'zero')
1830 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001831 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001832 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1833 bw!
1834
1835 #" Test for using a function()
1836 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1837 new
1838 call setline(1, 'one')
1839 LET g:TsrFunc1Args = []
1840 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1841 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001842 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001843
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001844 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001845 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001846 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001847 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001848 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001849 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001850 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001851 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001852 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001853
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001854 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001855 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001856 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001857 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001858 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001859 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001860 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001861 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001862 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001863
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001864 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001865 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1866 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001868 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001869 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001870 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001871 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001872
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001873 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001874 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001875 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001876 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001877 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001878 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001879 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001880 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001881 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001882
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001883 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001884 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001885 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001886 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001887 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001888 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001889 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001890 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001891 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001892
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001893 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001894 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001895 LET optval = substitute(optval, ' ', '\\ ', 'g')
1896 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001897 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001898 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001899 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001900 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001901 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001903
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001905 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001906 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001907 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001908 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001909 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001910 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001911 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001912
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001913 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001914 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001915 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001916 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001917 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001918 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001919 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001920 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001921
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001922 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001923 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001924 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001925 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001927 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001929 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001930 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001931
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001932 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001933 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001934 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001935 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001936 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001937 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001938 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001939 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001940 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001941
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001942 #" Test for using a lambda function with incorrect return value
1943 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1944 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001945 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 call setline(1, 'eight')
1947 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1948 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001949
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001950 #" Test for clearing the 'thesaurusfunc' option
1951 set thesaurusfunc=''
1952 set thesaurusfunc&
1953 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1954 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001955
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001956 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001957 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001958 call setline(1, 'ten')
1959 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1960 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001961 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001962 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001963 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001964 bw!
1965
1966 #" Use a buffer-local value and a global value
1967 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001968 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001969 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001970 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001971 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1972 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001973 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001974 new
1975 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001976 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001977 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1978 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001979 call assert_equal([], g:TsrFunc1Args)
1980 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001981 wincmd w
1982 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001983 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001984 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1985 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001986 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001987 :%bw!
1988 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001989 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001990
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001991 " Test for using a script-local function name
1992 func s:TsrFunc3(findstart, base)
1993 call add(g:TsrFunc3Args, [a:findstart, a:base])
1994 return a:findstart ? 0 : []
1995 endfunc
1996 set tsrfu=s:TsrFunc3
1997 new
1998 call setline(1, 'script1')
1999 let g:TsrFunc3Args = []
2000 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2001 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2002 bw!
2003
2004 let &tsrfu = 's:TsrFunc3'
2005 new
2006 call setline(1, 'script2')
2007 let g:TsrFunc3Args = []
2008 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2009 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2010 bw!
2011 delfunc s:TsrFunc3
2012
2013 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002014 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002015 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2016
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002017 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002018 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002019 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002020 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002021 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002022 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002023 bw!
2024
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002025 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2026 func SetTsrFunc()
2027 let params = {'thesaurus': function('g:DictTsrFunc')}
2028 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002029 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002030 func g:DictTsrFunc(_) dict
2031 endfunc
2032 call SetTsrFunc()
2033 new
2034 call SetTsrFunc()
2035 bw
2036 call test_garbagecollect_now()
2037 new
2038 set thesaurusfunc=
2039 wincmd w
2040 %bw!
2041 delfunc SetTsrFunc
2042
2043 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2044 " cause a crash.
2045 func SetLocalTsrFunc()
2046 let params = {'thesaurus': function('g:DictTsrFunc')}
2047 let &l:thesaurusfunc = params.thesaurus
2048 endfunc
2049 call SetLocalTsrFunc()
2050 call test_garbagecollect_now()
2051 call SetLocalTsrFunc()
2052 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002053 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002054 delfunc g:DictTsrFunc
2055 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002056
2057 " Vim9 tests
2058 let lines =<< trim END
2059 vim9script
2060
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002061 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2062 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002063 return findstart ? 0 : []
2064 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002065
2066 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002067 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2068 new | only
2069 setline(1, 'one')
2070 g:Vim9tsrFunc_Args = []
2071 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2072 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2073 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002074
2075 # Test for using a global function name
2076 &thesaurusfunc = g:TsrFunc2
2077 new | only
2078 setline(1, 'two')
2079 g:TsrFunc2Args = []
2080 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2081 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2082 bw!
2083
2084 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002085 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002086 add(g:LocalTsrFuncArgs, [findstart, base])
2087 return findstart ? 0 : []
2088 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002089 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002090 new | only
2091 setline(1, 'three')
2092 g:LocalTsrFuncArgs = []
2093 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2094 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2095 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002096 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002097 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002098
2099 " cleanup
2100 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002101 delfunc TsrFunc1
2102 delfunc TsrFunc2
2103 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002104 %bw!
2105endfunc
2106
Christian Brabandtac72c212022-04-07 21:00:53 +01002107func FooBarComplete(findstart, base)
2108 if a:findstart
2109 return col('.') - 1
2110 else
2111 return ["Foo", "Bar", "}"]
2112 endif
2113endfunc
2114
2115func Test_complete_smartindent()
2116 new
2117 setlocal smartindent completefunc=FooBarComplete
2118
2119 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2120 let result = getline(1,'$')
2121 call assert_equal(['', '{','}',''], result)
2122 bw!
2123 delfunction! FooBarComplete
2124endfunc
2125
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002126func Test_complete_overrun()
2127 " this was going past the end of the copied text
2128 new
2129 sil norm si”0s0 
2130 bwipe!
2131endfunc
2132
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002133func Test_infercase_very_long_line()
2134 " this was truncating the line when inferring case
2135 new
2136 let longLine = "blah "->repeat(300)
2137 let verylongLine = "blah "->repeat(400)
2138 call setline(1, verylongLine)
2139 call setline(2, longLine)
2140 set ic infercase
2141 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2142 call assert_equal(longLine, getline(3))
2143
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002144 " check that the too long text is NUL terminated
2145 %del
2146 norm o
2147 norm 1987ax
2148 exec "norm ox\<C-X>\<C-L>"
2149 call assert_equal(repeat('x', 1987), getline(3))
2150
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002151 bwipe!
2152 set noic noinfercase
2153endfunc
2154
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002155func Test_ins_complete_add()
2156 " this was reading past the end of allocated memory
2157 new
2158 norm o
2159 norm 7o€€
2160 sil! norm o
2161
2162 bwipe!
2163endfunc
2164
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002165func Test_ins_complete_end_of_line()
2166 " this was reading past the end of the line
2167 new
2168 norm 8o€ý 
2169 sil! norm o
2170
2171 bwipe!
2172endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002173
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002174" vim: shiftwidth=2 sts=2 expandtab