blob: 35c5785d3a099ec0ff3be1ac8bcae024e5301431 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
12 call mkdir('Xdir')
13 cd Xdir
14
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
102 call delete('Xtestfile')
103 call delete('Xtest11.one')
104 call delete('Xtest11.two')
105 call delete('Xtestdata')
106 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100107 cd ..
108 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200109endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100110
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000111func Test_ins_complete_invalid_byte()
112 if has('unix') && executable('base64')
113 " this weird command was causing an illegal memory access
114 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
115 call system('base64 -d Xinvalid64 > Xinvalid')
116 call writefile(['qa!'], 'Xexit')
117 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
118 call delete('Xinvalid64')
119 call delete('Xinvalid')
120 call delete('Xexit')
121 endif
122endfunc
123
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100124func Test_omni_dash()
125 func Omni(findstart, base)
126 if a:findstart
127 return 5
128 else
129 echom a:base
130 return ['-help', '-v']
131 endif
132 endfunc
133 set omnifunc=Omni
134 new
135 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100136 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100137
138 bwipe!
139 delfunc Omni
140 set omnifunc=
141endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100142
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
400 call assert_equal(v:completed_item, value)
401endfunc
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 Moolenaarac3150d2019-07-28 16:36:39 +0200436 call mkdir('Xdir')
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=
444 exe "normal oXd\<C-X>\<C-F>"
445 call assert_equal('Xdir\', getline('.'))
446
447 set completeslash=backslash
448 exe "normal oXd\<C-X>\<C-F>"
449 call assert_equal('Xdir\', getline('.'))
450
451 set completeslash=slash
452 exe "normal oXd\<C-X>\<C-F>"
453 call assert_equal('Xdir/', getline('.'))
454
455 set shellslash
456
457 set completeslash=
458 exe "normal oXd\<C-X>\<C-F>"
459 call assert_equal('Xdir/', getline('.'))
460
461 set completeslash=backslash
462 exe "normal oXd\<C-X>\<C-F>"
463 call assert_equal('Xdir\', getline('.'))
464
465 set completeslash=slash
466 exe "normal oXd\<C-X>\<C-F>"
467 call assert_equal('Xdir/', getline('.'))
468 %bw!
469 call delete('Xdir', 'rf')
470
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
676 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
677 func ListMonths()
678 call complete(col('.'), test_null_list())
679 endfunc
680 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
681 delfunc ListColors
682 delfunc ListMonths
683 call assert_fails('call complete_info({})', 'E714:')
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
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200702" Test for completing words following a completed word in a line
703func Test_complete_wrapscan()
704 " complete words from another buffer
705 new
706 call setline(1, ['one two', 'three four'])
707 new
708 setlocal complete=w
709 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
710 call assert_equal('two three four', getline(1))
711 close!
712 " complete words from the current buffer
713 setlocal complete=.
714 %d
715 call setline(1, ['one two', ''])
716 call cursor(2, 1)
717 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
718 call assert_equal('one two one two', getline(2))
719 close!
720endfunc
721
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200722" Test for completing special characters
723func Test_complete_special_chars()
724 new
725 call setline(1, 'int .*[-\^$ func float')
726 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
727 call assert_equal('int .*[-\^$ func float', getline(2))
728 close!
729endfunc
730
731" Test for completion when text is wrapped across lines.
732func Test_complete_across_line()
733 new
734 call setline(1, ['red green blue', 'one two three'])
735 setlocal textwidth=20
736 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
737 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
738 close!
739endfunc
740
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000741" Test for completing words with a '.' at the end of a word.
742func Test_complete_joinspaces()
743 new
744 call setline(1, ['one two.', 'three. four'])
745 set joinspaces
746 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
747 call assert_equal("one two. three. four", getline(3))
748 set joinspaces&
749 bw!
750endfunc
751
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200752" Test for using CTRL-L to add one character when completing matching
753func Test_complete_add_onechar()
754 new
755 call setline(1, ['wool', 'woodwork'])
756 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
757 call assert_equal('woof', getline(3))
758
759 " use 'ignorecase' and backspace to erase characters from the prefix string
760 " and then add letters using CTRL-L
761 %d
762 set ignorecase backspace=2
763 setlocal complete=.
764 call setline(1, ['workhorse', 'workload'])
765 normal Go
766 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
767 call assert_equal('workh', getline(3))
768 set ignorecase& backspace&
769 close!
770endfunc
771
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000772" Test for using CTRL-X CTRL-L to complete whole lines lines
773func Test_complete_wholeline()
774 new
775 " complete one-line
776 call setline(1, ['a1', 'a2'])
777 exe "normal ggoa\<C-X>\<C-L>"
778 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
779 " go to the next match (wrapping around the buffer)
780 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
781 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
782 " go to the next match
783 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
784 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
785 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
786 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
787 " repeat the test using CTRL-L
788 " go to the next match (wrapping around the buffer)
789 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
790 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
791 " go to the next match
792 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
793 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
794 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
795 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
796 %d
797 " use CTRL-X CTRL-L to add one more line
798 call setline(1, ['a1', 'b1'])
799 setlocal complete=.
800 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
801 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
802 bw!
803endfunc
804
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200805" Test insert completion with 'cindent' (adjust the indent)
806func Test_complete_with_cindent()
807 new
808 setlocal cindent
809 call setline(1, ['if (i == 1)', " j = 2;"])
810 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
811 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
812
813 %d
814 call setline(1, ['when while', '{', ''])
815 setlocal cinkeys+==while
816 exe "normal Giwh\<C-P> "
817 call assert_equal("\twhile ", getline('$'))
818 close!
819endfunc
820
821" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
822func Test_complete_cmdline()
823 new
824 exe "normal icaddb\<C-X>\<C-V>"
825 call assert_equal('caddbuffer', getline(1))
826 exe "normal ocall getqf\<C-X>\<C-V>"
827 call assert_equal('call getqflist(', getline(2))
828 exe "normal oabcxyz(\<C-X>\<C-V>"
829 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200830 com! -buffer TestCommand1 echo 'TestCommand1'
831 com! -buffer TestCommand2 echo 'TestCommand2'
832 write TestCommand1Test
833 write TestCommand2Test
834 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
835 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
836 call assert_equal('TestCommand2Test', getline(4))
837 call delete('TestCommand1Test')
838 call delete('TestCommand2Test')
839 delcom TestCommand1
840 delcom TestCommand2
841 close!
842endfunc
843
844" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
845func Test_complete_stop()
846 new
847 func Save_mode1()
848 let g:mode1 = mode(1)
849 return ''
850 endfunc
851 func Save_mode2()
852 let g:mode2 = mode(1)
853 return ''
854 endfunc
855 inoremap <F1> <C-R>=Save_mode1()<CR>
856 inoremap <F2> <C-R>=Save_mode2()<CR>
857 call setline(1, ['aaa bbb ccc '])
858 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
859 call assert_equal('ic', g:mode1)
860 call assert_equal('i', g:mode2)
861 call assert_equal('aaa bbb ccc ', getline(1))
862 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
863 call assert_equal('ic', g:mode1)
864 call assert_equal('i', g:mode2)
865 call assert_equal('aaa bbb ccc aaa', getline(1))
866 set completeopt+=noselect
867 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
868 call assert_equal('ic', g:mode1)
869 call assert_equal('i', g:mode2)
870 call assert_equal('aaa bbb ccc aaa bb', getline(1))
871 set completeopt&
872 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
873 call assert_equal('ic', g:mode1)
874 call assert_equal('i', g:mode2)
875 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
876 com! -buffer TestCommand1 echo 'TestCommand1'
877 com! -buffer TestCommand2 echo 'TestCommand2'
878 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<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('TestCommand2', getline(2))
882 delcom TestCommand1
883 delcom TestCommand2
884 unlet g:mode1
885 unlet g:mode2
886 iunmap <F1>
887 iunmap <F2>
888 delfunc Save_mode1
889 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200890 close!
891endfunc
892
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000893" Test for typing CTRL-R in insert completion mode to insert a register
894" content.
895func Test_complete_reginsert()
896 new
897 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
898
899 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
900 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
901 " inserted.
902 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
903 call assert_equal('a123', getline(5))
904 let @r = "\<C-P>\<C-P>"
905 exe "normal GCa\<C-P>\<C-R>r"
906 call assert_equal('a12', getline(5))
907 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
908 call assert_equal('a1234x', getline(5))
909 bw!
910endfunc
911
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200912func Test_issue_7021()
913 CheckMSWindows
914
915 let orig_shellslash = &shellslash
916 set noshellslash
917
918 set completeslash=slash
919 call assert_false(expand('~') =~ '/')
920
921 let &shellslash = orig_shellslash
922 set completeslash=
923endfunc
924
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000925" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
926func Test_complete_longest_match()
927 for e in ['latin1', 'utf-8']
928 exe 'set encoding=' .. e
929 new
930 set complete=.
931 set completeopt=menu,longest
932 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
933 exe "normal Gopfx\<C-P>"
934 call assert_equal('pfx_', getline(5))
935 bw!
936 endfor
937
938 " Test for completing additional words with longest match set
939 new
940 call setline(1, ['abc1', 'abd2'])
941 exe "normal Goab\<C-P>\<C-X>\<C-P>"
942 call assert_equal('ab', getline(3))
943 bw!
944 set complete& completeopt&
945endfunc
946
947" Test for removing the first displayed completion match and selecting the
948" match just before that.
949func Test_complete_erase_firstmatch()
950 new
951 call setline(1, ['a12', 'a34', 'a56'])
952 set complete=.
953 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
954 call assert_equal('a34', getline('$'))
955 set complete&
956 bw!
957endfunc
958
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000959" Test for completing words from unloaded buffers
960func Test_complete_from_unloadedbuf()
961 call writefile(['abc'], "Xfile1")
962 call writefile(['def'], "Xfile2")
963 edit Xfile1
964 edit Xfile2
965 new | close
966 enew
967 bunload Xfile1 Xfile2
968 set complete=u
969 " complete from an unloaded buffer
970 exe "normal! ia\<C-P>"
971 call assert_equal('abc', getline(1))
972 exe "normal! od\<C-P>"
973 call assert_equal('def', getline(2))
974 set complete&
975 %bw!
976 call delete("Xfile1")
977 call delete("Xfile2")
978endfunc
979
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000980" Test for completing whole lines from unloaded buffers
981func Test_complete_wholeline_unloadedbuf()
982 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
983 edit Xfile1
984 enew
985 set complete=u
986 exe "normal! ia\<C-X>\<C-L>\<C-P>"
987 call assert_equal('a line2', getline(1))
988 %d
989 " completing from an unlisted buffer should fail
990 bdel Xfile1
991 exe "normal! ia\<C-X>\<C-L>\<C-P>"
992 call assert_equal('a', getline(1))
993 set complete&
994 %bw!
995 call delete("Xfile1")
996endfunc
997
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000998" Test for completing words from unlisted buffers
999func Test_complete_from_unlistedbuf()
1000 call writefile(['abc'], "Xfile1")
1001 call writefile(['def'], "Xfile2")
1002 edit Xfile1
1003 edit Xfile2
1004 new | close
1005 bdel Xfile1 Xfile2
1006 set complete=U
1007 " complete from an unlisted buffer
1008 exe "normal! ia\<C-P>"
1009 call assert_equal('abc', getline(1))
1010 exe "normal! od\<C-P>"
1011 call assert_equal('def', getline(2))
1012 set complete&
1013 %bw!
1014 call delete("Xfile1")
1015 call delete("Xfile2")
1016endfunc
1017
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001018" Test for completing whole lines from unlisted buffers
1019func Test_complete_wholeline_unlistedbuf()
1020 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1021 edit Xfile1
1022 enew
1023 set complete=U
1024 " completing from a unloaded buffer should fail
1025 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1026 call assert_equal('a', getline(1))
1027 %d
1028 bdel Xfile1
1029 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1030 call assert_equal('a line2', getline(1))
1031 set complete&
1032 %bw!
1033 call delete("Xfile1")
1034endfunc
1035
1036" Test for adding a multibyte character using CTRL-L in completion mode
1037func Test_complete_mbyte_char_add()
1038 new
1039 set complete=.
1040 call setline(1, 'abė')
1041 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1042 call assert_equal('abė', getline(2))
1043 " Test for a leader with multibyte character
1044 %d
1045 call setline(1, 'abėĕ')
1046 exe "normal! oabė\<C-P>"
1047 call assert_equal('abėĕ', getline(2))
1048 bw!
1049endfunc
1050
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001051" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1052" not to complete matches from the local buffer. Also test using multiple
1053" <C-X> to cancel the current completion mode.
1054func Test_complete_local_expansion()
1055 new
1056 set complete=t
1057 call setline(1, ['abc', 'def'])
1058 exe "normal! Go\<C-X>\<C-P>"
1059 call assert_equal("def", getline(3))
1060 exe "normal! Go\<C-P>"
1061 call assert_equal("", getline(4))
1062 exe "normal! Go\<C-X>\<C-N>"
1063 call assert_equal("abc", getline(5))
1064 exe "normal! Go\<C-N>"
1065 call assert_equal("", getline(6))
1066
1067 " use multiple <C-X> to cancel the previous completion mode
1068 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1069 call assert_equal("", getline(7))
1070 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1071 call assert_equal("", getline(8))
1072 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1073 call assert_equal("abc", getline(9))
1074
1075 " interrupt the current completion mode
1076 set completeopt=menu,noinsert
1077 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1078 call assert_equal("abc", getline(10))
1079
1080 " when only one <C-X> is used to interrupt, do normal expansion
1081 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1082 call assert_equal("", getline(11))
1083 set completeopt&
1084
1085 " using two <C-X> in non-completion mode and restarting the same mode
1086 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1087 call assert_equal("def", getline(12))
1088
1089 " test for adding a match from the original empty text
1090 %d
1091 call setline(1, 'abc def g')
1092 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1093 call assert_equal('def', getline(2))
1094 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1095 call assert_equal('abc', getline(2))
1096
1097 bw!
1098endfunc
1099
1100" Test for undoing changes after a insert-mode completion
1101func Test_complete_undo()
1102 new
1103 set complete=.
1104 " undo with 'ignorecase'
1105 call setline(1, ['ABOVE', 'BELOW'])
1106 set ignorecase
1107 exe "normal! Goab\<C-G>u\<C-P>"
1108 call assert_equal("ABOVE", getline(3))
1109 undo
1110 call assert_equal("ab", getline(3))
1111 set ignorecase&
1112 %d
1113 " undo with longest match
1114 set completeopt=menu,longest
1115 call setline(1, ['above', 'about'])
1116 exe "normal! Goa\<C-G>u\<C-P>"
1117 call assert_equal("abo", getline(3))
1118 undo
1119 call assert_equal("a", getline(3))
1120 set completeopt&
1121 %d
1122 " undo for line completion
1123 call setline(1, ['above that change', 'below that change'])
1124 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1125 call assert_equal("above that change", getline(3))
1126 undo
1127 call assert_equal("above", getline(3))
1128
1129 bw!
1130endfunc
1131
1132" Test for completing a very long word
1133func Test_complete_long_word()
1134 set complete&
1135 new
1136 call setline(1, repeat('x', 950) .. ' one two three')
1137 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1138 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1139 %d
1140 " should fail when more than 950 characters are in a word
1141 call setline(1, repeat('x', 951) .. ' one two three')
1142 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1143 call assert_equal(repeat('x', 951), getline(2))
1144
1145 " Test for adding a very long word to an existing completion
1146 %d
1147 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1148 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1149 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1150 bw!
1151endfunc
1152
1153" Test for some fields in the complete items used by complete()
1154func Test_complete_items()
1155 func CompleteItems(idx)
1156 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1157 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1158 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1159 \ [#{user_data: 'u9'}],
1160 \ [#{word: "", user_data: 'u10'}],
1161 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1162 call complete(col('.'), items[a:idx])
1163 return ''
1164 endfunc
1165 new
1166 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1167 call assert_equal('u2', v:completed_item.user_data)
1168 call assert_equal('one', getline(1))
1169 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1170 call assert_equal('u3', v:completed_item.user_data)
1171 call assert_equal('one', getline(2))
1172 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1173 call assert_equal('', getline(3))
1174 set completeopt=menu,noinsert
1175 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1176 call assert_equal('oNE', getline(4))
1177 call assert_equal('u8', v:completed_item.user_data)
1178 set completeopt&
1179 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1180 call assert_equal('', getline(5))
1181 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1182 call assert_equal('', getline(6))
1183 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1184 call assert_equal('', getline(7))
1185 call assert_equal('u11', v:completed_item.user_data)
1186 " pass invalid argument to complete()
1187 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1188 call assert_fails('exe cmd', 'E730:')
1189 bw!
1190 delfunc CompleteItems
1191endfunc
1192
1193" Test for the "refresh" item in the dict returned by an insert completion
1194" function
1195func Test_complete_item_refresh_always()
1196 let g:CallCount = 0
1197 func! Tcomplete(findstart, base)
1198 if a:findstart
1199 " locate the start of the word
1200 let line = getline('.')
1201 let start = col('.') - 1
1202 while start > 0 && line[start - 1] =~ '\a'
1203 let start -= 1
1204 endwhile
1205 return start
1206 else
1207 let g:CallCount += 1
1208 let res = ["update1", "update12", "update123"]
1209 return #{words: res, refresh: 'always'}
1210 endif
1211 endfunc
1212 new
1213 set completeopt=menu,longest
1214 set completefunc=Tcomplete
1215 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1216 call assert_equal('up', getline(1))
1217 call assert_equal(2, g:CallCount)
1218 set completeopt&
1219 set completefunc&
1220 bw!
1221 delfunc Tcomplete
1222endfunc
1223
1224" Test for completing from a thesaurus file without read permission
1225func Test_complete_unreadable_thesaurus_file()
1226 CheckUnix
1227 CheckNotRoot
1228
1229 call writefile(['about', 'above'], 'Xfile')
1230 call setfperm('Xfile', '---r--r--')
1231 new
1232 set complete=sXfile
1233 exe "normal! ia\<C-P>"
1234 call assert_equal('a', getline(1))
1235 bw!
1236 call delete('Xfile')
1237 set complete&
1238endfunc
1239
Bram Moolenaarcc233582020-12-12 13:32:07 +01001240" Test to ensure 'Scanning...' messages are not recorded in messages history
1241func Test_z1_complete_no_history()
1242 new
1243 messages clear
1244 let currmess = execute('messages')
1245 setlocal dictionary=README.txt
1246 exe "normal owh\<C-X>\<C-K>"
1247 exe "normal owh\<C-N>"
1248 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001249 bwipe!
1250endfunc
1251
1252" A mapping is not used for the key after CTRL-X.
1253func Test_no_mapping_for_ctrl_x_key()
1254 new
1255 inoremap <C-K> <Cmd>let was_mapped = 'yes'<CR>
1256 setlocal dictionary=README.txt
1257 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1258 call assert_equal('example ', getline(1))
1259 call assert_false(exists('was_mapped'))
1260 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001261endfunc
1262
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001263" Test for different ways of setting the 'completefunc' option
1264func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001265 func CompleteFunc1(callnr, findstart, base)
1266 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1267 return a:findstart ? 0 : []
1268 endfunc
1269 func CompleteFunc2(findstart, base)
1270 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001271 return a:findstart ? 0 : []
1272 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001273
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001274 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001275 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001276 LET &completefunc = 'g:CompleteFunc2'
1277 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001278 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001279 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001280 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001281 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001282 bw!
1283
1284 #" Test for using a function()
1285 set completefunc=function('g:CompleteFunc1',\ [10])
1286 new
1287 call setline(1, 'one')
1288 LET g:CompleteFunc1Args = []
1289 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1290 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001291 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001292
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001293 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001294 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001295 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001296 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001297 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001298 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001299 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001300 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001301 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001302
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001303 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001304 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001305 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001306 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001307 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001308 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001309 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001310 call assert_equal([[12, 1, ''], [12, 0, 'two']], 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 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001314 set completefunc=funcref('g:CompleteFunc1',\ [13])
1315 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001316 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001317 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001318 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001319 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001320 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001321
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001322 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001323 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001324 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001325 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001326 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001327 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001329 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001330 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001331
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001332 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001333 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001334 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 call setline(1, 'four')
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([[15, 1, ''], [15, 0, 'four']], 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 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001343 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001344 LET optval = substitute(optval, ' ', '\\ ', 'g')
1345 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001346 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001347 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001348 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001349 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001350 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001351 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001352
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001353 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001354 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001355 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001356 call setline(1, 'six')
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([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001360 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001361
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001362 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001363 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001364 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001365 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001366 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001367 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001368 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001369 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001370
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001371 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001372 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001374 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001375 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001376 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001378 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001380
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001382 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001384 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001385 call setline(1, 'seven')
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([[20, 1, ''], [20, 0, 'seven']], 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 #" Test for using a lambda function with incorrect return value
1392 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1393 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001394 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 call setline(1, 'eight')
1396 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1397 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001398
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 #" Test for clearing the 'completefunc' option
1400 set completefunc=''
1401 set completefunc&
1402 call assert_fails("set completefunc=function('abc')", "E700:")
1403 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001404
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001405 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001406 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001407 call setline(1, 'five')
1408 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1409 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001410 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001411 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001412 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 bw!
1414 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001415 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001416
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001417 " Test for using a script-local function name
1418 func s:CompleteFunc3(findstart, base)
1419 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1420 return a:findstart ? 0 : []
1421 endfunc
1422 set completefunc=s:CompleteFunc3
1423 new
1424 call setline(1, 'script1')
1425 let g:CompleteFunc3Args = []
1426 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1427 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1428 bw!
1429
1430 let &completefunc = 's:CompleteFunc3'
1431 new
1432 call setline(1, 'script2')
1433 let g:CompleteFunc3Args = []
1434 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1435 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1436 bw!
1437 delfunc s:CompleteFunc3
1438
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001439 " In Vim9 script s: can be omitted
1440 let lines =<< trim END
1441 vim9script
1442 var CompleteFunc4Args = []
1443 def CompleteFunc4(findstart: bool, base: string): any
1444 add(CompleteFunc4Args, [findstart, base])
1445 return findstart ? 0 : []
1446 enddef
1447 set completefunc=CompleteFunc4
1448 new
1449 setline(1, 'script1')
1450 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1451 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1452 bw!
1453 END
1454 call v9.CheckScriptSuccess(lines)
1455
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001456 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001457 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001458 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1459
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001460 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001461 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001462 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001463 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001464 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001465 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001466
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001467 " set 'completefunc' to a partial with dict. This used to cause a crash.
1468 func SetCompleteFunc()
1469 let params = {'complete': function('g:DictCompleteFunc')}
1470 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001471 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001472 func g:DictCompleteFunc(_) dict
1473 endfunc
1474 call SetCompleteFunc()
1475 new
1476 call SetCompleteFunc()
1477 bw
1478 call test_garbagecollect_now()
1479 new
1480 set completefunc=
1481 wincmd w
1482 set completefunc=
1483 %bw!
1484 delfunc g:DictCompleteFunc
1485 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001486
1487 " Vim9 tests
1488 let lines =<< trim END
1489 vim9script
1490
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001491 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1492 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001493 return findstart ? 0 : []
1494 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001495
1496 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001497 set completefunc=function('Vim9CompleteFunc',\ [60])
1498 new | only
1499 setline(1, 'one')
1500 g:Vim9completeFuncArgs = []
1501 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1502 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1503 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001504
1505 # Test for using a global function name
1506 &completefunc = g:CompleteFunc2
1507 new | only
1508 setline(1, 'two')
1509 g:CompleteFunc2Args = []
1510 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1511 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1512 bw!
1513
1514 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001515 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001516 add(g:LocalCompleteFuncArgs, [findstart, base])
1517 return findstart ? 0 : []
1518 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001519 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001520 new | only
1521 setline(1, 'three')
1522 g:LocalCompleteFuncArgs = []
1523 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1524 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1525 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001526 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001527 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001528
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001529 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001530 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001531 delfunc CompleteFunc1
1532 delfunc CompleteFunc2
1533 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001534 %bw!
1535endfunc
1536
1537" Test for different ways of setting the 'omnifunc' option
1538func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001539 func OmniFunc1(callnr, findstart, base)
1540 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1541 return a:findstart ? 0 : []
1542 endfunc
1543 func OmniFunc2(findstart, base)
1544 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001545 return a:findstart ? 0 : []
1546 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001547
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001548 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001549 #" Test for using a function name
1550 LET &omnifunc = 'g:OmniFunc2'
1551 new
1552 call setline(1, 'zero')
1553 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001554 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001555 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1556 bw!
1557
1558 #" Test for using a function()
1559 set omnifunc=function('g:OmniFunc1',\ [10])
1560 new
1561 call setline(1, 'one')
1562 LET g:OmniFunc1Args = []
1563 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1564 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001565 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001566
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001567 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001568 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001569 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001570 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001571 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001572 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001573 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001574 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001575 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001576
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001577 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001578 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001579 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001580 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001581 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001582 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001583 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001584 call assert_equal([[12, 1, ''], [12, 0, 'two']], 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 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001588 set omnifunc=funcref('g:OmniFunc1',\ [13])
1589 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001590 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001591 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001592 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001593 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001594 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001595
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001596 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001597 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001598 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001599 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001600 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001601 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001603 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001604 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001605
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001606 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001607 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001608 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001609 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001610 call setline(1, 'four')
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([[15, 1, ''], [15, 0, 'four']], 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 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001617 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 LET optval = substitute(optval, ' ', '\\ ', 'g')
1619 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001620 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001621 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001622 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001623 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001624 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001625 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001626
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001627 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001628 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001629 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 call setline(1, 'six')
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([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001635
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001637 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001638 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001639 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001640 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001641 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001642 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001643 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001644
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001646 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001652 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001654
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001656 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001658 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001659 call setline(1, 'seven')
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([[20, 1, ''], [20, 0, 'seven']], 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 #" Test for using a lambda function with incorrect return value
1666 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1667 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 call setline(1, 'eight')
1670 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1671 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001672
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 #" Test for clearing the 'omnifunc' option
1674 set omnifunc=''
1675 set omnifunc&
1676 call assert_fails("set omnifunc=function('abc')", "E700:")
1677 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001678
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001679 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001680 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001681 call setline(1, 'nine')
1682 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1683 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001684 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001686 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 bw!
1688 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001689 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001690
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001691 " Test for using a script-local function name
1692 func s:OmniFunc3(findstart, base)
1693 call add(g:OmniFunc3Args, [a:findstart, a:base])
1694 return a:findstart ? 0 : []
1695 endfunc
1696 set omnifunc=s:OmniFunc3
1697 new
1698 call setline(1, 'script1')
1699 let g:OmniFunc3Args = []
1700 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1701 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1702 bw!
1703
1704 let &omnifunc = 's:OmniFunc3'
1705 new
1706 call setline(1, 'script2')
1707 let g:OmniFunc3Args = []
1708 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1709 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1710 bw!
1711 delfunc s:OmniFunc3
1712
1713 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001714 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001715 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1716
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001717 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001718 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001719 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001720 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001721 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001722 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001723
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001724 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1725 func SetOmniFunc()
1726 let params = {'omni': function('g:DictOmniFunc')}
1727 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001728 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001729 func g:DictOmniFunc(_) dict
1730 endfunc
1731 call SetOmniFunc()
1732 new
1733 call SetOmniFunc()
1734 bw
1735 call test_garbagecollect_now()
1736 new
1737 set omnifunc=
1738 wincmd w
1739 set omnifunc=
1740 %bw!
1741 delfunc g:DictOmniFunc
1742 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001743
1744 " Vim9 tests
1745 let lines =<< trim END
1746 vim9script
1747
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001748 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1749 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001750 return findstart ? 0 : []
1751 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001752
1753 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001754 set omnifunc=function('Vim9omniFunc',\ [60])
1755 new | only
1756 setline(1, 'one')
1757 g:Vim9omniFunc_Args = []
1758 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1759 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1760 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001761
1762 # Test for using a global function name
1763 &omnifunc = g:OmniFunc2
1764 new | only
1765 setline(1, 'two')
1766 g:OmniFunc2Args = []
1767 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1768 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1769 bw!
1770
1771 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001772 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001773 add(g:LocalOmniFuncArgs, [findstart, base])
1774 return findstart ? 0 : []
1775 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001776 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001777 new | only
1778 setline(1, 'three')
1779 g:LocalOmniFuncArgs = []
1780 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1781 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1782 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001783 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001784 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001785
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001786 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001787 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001788 delfunc OmniFunc1
1789 delfunc OmniFunc2
1790 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001791 %bw!
1792endfunc
1793
1794" Test for different ways of setting the 'thesaurusfunc' option
1795func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001796 func TsrFunc1(callnr, findstart, base)
1797 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001798 return a:findstart ? 0 : []
1799 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001800 func TsrFunc2(findstart, base)
1801 call add(g:TsrFunc2Args, [a:findstart, a:base])
1802 return a:findstart ? 0 : ['sunday']
1803 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001804
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001805 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001806 #" Test for using a function name
1807 LET &thesaurusfunc = 'g:TsrFunc2'
1808 new
1809 call setline(1, 'zero')
1810 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001811 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001812 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1813 bw!
1814
1815 #" Test for using a function()
1816 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1817 new
1818 call setline(1, 'one')
1819 LET g:TsrFunc1Args = []
1820 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1821 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001822 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001823
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001824 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001825 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001826 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001827 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001828 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001829 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001830 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001831 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001832 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001833
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001834 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001835 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001836 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001837 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001838 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001839 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001840 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001841 call assert_equal([[12, 1, ''], [12, 0, 'two']], 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 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001845 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1846 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001847 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001848 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001849 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001850 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001851 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001852
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001853 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001854 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001855 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001856 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001857 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001858 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001859 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001860 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001861 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001862
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001863 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001864 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001865 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001866 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867 call setline(1, 'four')
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([[15, 1, ''], [15, 0, 'four']], 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 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001874 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001875 LET optval = substitute(optval, ' ', '\\ ', 'g')
1876 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001877 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001878 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001879 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001880 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001881 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001882 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001883
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001884 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001885 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001886 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001887 call setline(1, 'six')
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([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001891 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001892
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001893 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001894 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001895 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001897 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001898 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001899 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001900 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001901
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001903 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001905 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001907 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001909 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001911
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001913 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001915 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001916 call setline(1, 'seven')
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([[20, 1, ''], [20, 0, 'seven']], 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 #" Test for using a lambda function with incorrect return value
1923 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1924 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001925 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 call setline(1, 'eight')
1927 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1928 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001929
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001930 #" Test for clearing the 'thesaurusfunc' option
1931 set thesaurusfunc=''
1932 set thesaurusfunc&
1933 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1934 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001935
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001936 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001937 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001938 call setline(1, 'ten')
1939 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1940 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001941 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001942 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001943 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 bw!
1945
1946 #" Use a buffer-local value and a global value
1947 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001948 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001949 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001950 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1952 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001953 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001954 new
1955 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001956 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1958 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001959 call assert_equal([], g:TsrFunc1Args)
1960 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001961 wincmd w
1962 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001963 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001964 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1965 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001966 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001967 :%bw!
1968 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001969 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001970
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001971 " Test for using a script-local function name
1972 func s:TsrFunc3(findstart, base)
1973 call add(g:TsrFunc3Args, [a:findstart, a:base])
1974 return a:findstart ? 0 : []
1975 endfunc
1976 set tsrfu=s:TsrFunc3
1977 new
1978 call setline(1, 'script1')
1979 let g:TsrFunc3Args = []
1980 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1981 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1982 bw!
1983
1984 let &tsrfu = 's:TsrFunc3'
1985 new
1986 call setline(1, 'script2')
1987 let g:TsrFunc3Args = []
1988 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1989 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1990 bw!
1991 delfunc s:TsrFunc3
1992
1993 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001994 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001995 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1996
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001997 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001998 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001999 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002000 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002001 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002002 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002003 bw!
2004
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002005 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2006 func SetTsrFunc()
2007 let params = {'thesaurus': function('g:DictTsrFunc')}
2008 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002009 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002010 func g:DictTsrFunc(_) dict
2011 endfunc
2012 call SetTsrFunc()
2013 new
2014 call SetTsrFunc()
2015 bw
2016 call test_garbagecollect_now()
2017 new
2018 set thesaurusfunc=
2019 wincmd w
2020 %bw!
2021 delfunc SetTsrFunc
2022
2023 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2024 " cause a crash.
2025 func SetLocalTsrFunc()
2026 let params = {'thesaurus': function('g:DictTsrFunc')}
2027 let &l:thesaurusfunc = params.thesaurus
2028 endfunc
2029 call SetLocalTsrFunc()
2030 call test_garbagecollect_now()
2031 call SetLocalTsrFunc()
2032 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002033 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002034 delfunc g:DictTsrFunc
2035 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002036
2037 " Vim9 tests
2038 let lines =<< trim END
2039 vim9script
2040
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002041 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2042 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002043 return findstart ? 0 : []
2044 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002045
2046 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002047 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2048 new | only
2049 setline(1, 'one')
2050 g:Vim9tsrFunc_Args = []
2051 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2052 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2053 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002054
2055 # Test for using a global function name
2056 &thesaurusfunc = g:TsrFunc2
2057 new | only
2058 setline(1, 'two')
2059 g:TsrFunc2Args = []
2060 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2061 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2062 bw!
2063
2064 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002065 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002066 add(g:LocalTsrFuncArgs, [findstart, base])
2067 return findstart ? 0 : []
2068 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002069 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002070 new | only
2071 setline(1, 'three')
2072 g:LocalTsrFuncArgs = []
2073 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2074 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2075 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002076 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002077 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002078
2079 " cleanup
2080 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002081 delfunc TsrFunc1
2082 delfunc TsrFunc2
2083 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002084 %bw!
2085endfunc
2086
Christian Brabandtac72c212022-04-07 21:00:53 +01002087func FooBarComplete(findstart, base)
2088 if a:findstart
2089 return col('.') - 1
2090 else
2091 return ["Foo", "Bar", "}"]
2092 endif
2093endfunc
2094
2095func Test_complete_smartindent()
2096 new
2097 setlocal smartindent completefunc=FooBarComplete
2098
2099 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2100 let result = getline(1,'$')
2101 call assert_equal(['', '{','}',''], result)
2102 bw!
2103 delfunction! FooBarComplete
2104endfunc
2105
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002106func Test_complete_overrun()
2107 " this was going past the end of the copied text
2108 new
2109 sil norm si”0s0 
2110 bwipe!
2111endfunc
2112
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002113func Test_infercase_very_long_line()
2114 " this was truncating the line when inferring case
2115 new
2116 let longLine = "blah "->repeat(300)
2117 let verylongLine = "blah "->repeat(400)
2118 call setline(1, verylongLine)
2119 call setline(2, longLine)
2120 set ic infercase
2121 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2122 call assert_equal(longLine, getline(3))
2123
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002124 " check that the too long text is NUL terminated
2125 %del
2126 norm o
2127 norm 1987ax
2128 exec "norm ox\<C-X>\<C-L>"
2129 call assert_equal(repeat('x', 1987), getline(3))
2130
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002131 bwipe!
2132 set noic noinfercase
2133endfunc
2134
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002135func Test_ins_complete_add()
2136 " this was reading past the end of allocated memory
2137 new
2138 norm o
2139 norm 7o€€
2140 sil! norm o
2141
2142 bwipe!
2143endfunc
2144
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002145
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002146" vim: shiftwidth=2 sts=2 expandtab