blob: 379db7fe7965b97fb7de5ee3e0e542721c41526a [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010012 call mkdir('Xcpldir')
13 cd Xcpldir
Bram Moolenaarfb094e12017-11-05 20:59:28 +010014
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
102 call delete('Xtestfile')
103 call delete('Xtest11.one')
104 call delete('Xtest11.two')
105 call delete('Xtestdata')
106 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100107 cd ..
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100108 call delete('Xcpldir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200109endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100110
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000111func Test_ins_complete_invalid_byte()
112 if has('unix') && executable('base64')
113 " this weird command was causing an illegal memory access
114 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
115 call system('base64 -d Xinvalid64 > Xinvalid')
116 call writefile(['qa!'], 'Xexit')
117 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
118 call delete('Xinvalid64')
119 call delete('Xinvalid')
120 call delete('Xexit')
121 endif
122endfunc
123
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100124func Test_omni_dash()
125 func Omni(findstart, base)
126 if a:findstart
127 return 5
128 else
129 echom a:base
130 return ['-help', '-v']
131 endif
132 endfunc
133 set omnifunc=Omni
134 new
135 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100136 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100137
138 bwipe!
139 delfunc Omni
140 set omnifunc=
141endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100142
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100143func Test_omni_throw()
144 let g:CallCount = 0
145 func Omni(findstart, base)
146 let g:CallCount += 1
147 if a:findstart
148 throw "he he he"
149 endif
150 endfunc
151 set omnifunc=Omni
152 new
153 try
154 exe "normal ifoo\<C-x>\<C-o>"
155 call assert_false(v:true, 'command should have failed')
156 catch
157 call assert_exception('he he he')
158 call assert_equal(1, g:CallCount)
159 endtry
160
161 bwipe!
162 delfunc Omni
163 unlet g:CallCount
164 set omnifunc=
165endfunc
166
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200167func Test_omni_autoload()
168 let save_rtp = &rtp
169 set rtp=Xruntime/some
170 let dir = 'Xruntime/some/autoload'
171 call mkdir(dir, 'p')
172
173 let lines =<< trim END
174 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000175 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200176 if findstart
177 return 1
178 else
179 return ['match']
180 endif
181 enddef
182 {
183 eval 1 + 2
184 }
185 END
186 call writefile(lines, dir .. '/omni.vim')
187
188 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000189 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200190 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
191
192 bwipe!
193 call delete('Xruntime', 'rf')
194 set omnifunc=
195 let &rtp = save_rtp
196endfunc
197
Bram Moolenaarffa96842018-06-12 22:05:14 +0200198func Test_completefunc_args()
199 let s:args = []
200 func! CompleteFunc(findstart, base)
201 let s:args += [[a:findstart, empty(a:base)]]
202 endfunc
203 new
204
205 set completefunc=CompleteFunc
206 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200207 call assert_equal([1, 1], s:args[0])
208 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200209 set completefunc=
210
211 let s:args = []
212 set omnifunc=CompleteFunc
213 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200214 call assert_equal([1, 1], s:args[0])
215 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200216 set omnifunc=
217
218 bwipe!
219 unlet s:args
220 delfunc CompleteFunc
221endfunc
222
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100223func s:CompleteDone_CompleteFuncNone( findstart, base )
224 if a:findstart
225 return 0
226 endif
227
228 return v:none
229endfunc
230
Bram Moolenaar1e115362019-01-09 23:01:02 +0100231func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100232 if a:findstart
233 return 0
234 endif
235
236 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100237 \ 'words': [
238 \ {
239 \ 'word': 'aword',
240 \ 'abbr': 'wrd',
241 \ 'menu': 'extra text',
242 \ 'info': 'words are cool',
243 \ 'kind': 'W',
244 \ 'user_data': 'test'
245 \ }
246 \ ]
247 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100248endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100249
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100250func s:CompleteDone_CheckCompletedItemNone()
251 let s:called_completedone = 1
252endfunc
253
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100254func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100255 call assert_equal( 'aword', v:completed_item[ 'word' ] )
256 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
257 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
258 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
259 call assert_equal( 'W', v:completed_item[ 'kind' ] )
260 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
261
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100262 if a:pre
263 call assert_equal('function', complete_info().mode)
264 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100265
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100266 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100267endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100268
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100269func Test_CompleteDoneNone()
270 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 set completefunc=<SID>CompleteDone_CompleteFuncNone
274 execute "normal a\<C-X>\<C-U>\<C-Y>"
275 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200276 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100277
278 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200279 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100280
281 let s:called_completedone = 0
282 au! CompleteDone
283endfunc
284
285func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100286 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
287 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100288
289 set completefunc=<SID>CompleteDone_CompleteFuncDict
290 execute "normal a\<C-X>\<C-U>\<C-Y>"
291 set completefunc&
292
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100293 call assert_equal('test', v:completed_item[ 'user_data' ])
294 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100295
296 let s:called_completedone = 0
297 au! CompleteDone
298endfunc
299
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100300func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100301 if a:findstart
302 return 0
303 endif
304
305 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100306 \ 'words': [
307 \ {
308 \ 'word': 'aword',
309 \ 'abbr': 'wrd',
310 \ 'menu': 'extra text',
311 \ 'info': 'words are cool',
312 \ 'kind': 'W',
313 \ 'user_data': ['one', 'two'],
314 \ }
315 \ ]
316 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100317endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100318
Bram Moolenaar1e115362019-01-09 23:01:02 +0100319func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320 call assert_equal( 'aword', v:completed_item[ 'word' ] )
321 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
322 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
323 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
324 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100325 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100326
327 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100328endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100330func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100331 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
332
333 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
334 execute "normal a\<C-X>\<C-U>\<C-Y>"
335 set completefunc&
336
Bram Moolenaar08928322020-01-04 14:32:48 +0100337 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100338 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100339
340 let s:called_completedone = 0
341 au! CompleteDone
342endfunc
343
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100344func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100345 if a:findstart
346 return 0
347 endif
348
349 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100350endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100351
Bram Moolenaar1e115362019-01-09 23:01:02 +0100352func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100353 call assert_equal( 'aword', v:completed_item[ 'word' ] )
354 call assert_equal( '', v:completed_item[ 'abbr' ] )
355 call assert_equal( '', v:completed_item[ 'menu' ] )
356 call assert_equal( '', v:completed_item[ 'info' ] )
357 call assert_equal( '', v:completed_item[ 'kind' ] )
358 call assert_equal( '', v:completed_item[ 'user_data' ] )
359
360 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100361endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100362
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100363func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100364 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
365
366 set completefunc=<SID>CompleteDone_CompleteFuncList
367 execute "normal a\<C-X>\<C-U>\<C-Y>"
368 set completefunc&
369
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100370 call assert_equal('', v:completed_item[ 'user_data' ])
371 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100372
373 let s:called_completedone = 0
374 au! CompleteDone
375endfunc
376
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200377func Test_CompleteDone_undo()
378 au CompleteDone * call append(0, "prepend1")
379 new
380 call setline(1, ["line1", "line2"])
381 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
382 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
383 \ getline(1, '$'))
384 undo
385 call assert_equal(["line1", "line2"], getline(1, '$'))
386 bwipe!
387 au! CompleteDone
388endfunc
389
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100390func Test_CompleteDone_modify()
391 let value = {
392 \ 'word': '',
393 \ 'abbr': '',
394 \ 'menu': '',
395 \ 'info': '',
396 \ 'kind': '',
397 \ 'user_data': '',
398 \ }
399 let v:completed_item = value
zeertzjq75020942022-07-31 11:37:20 +0100400 call assert_equal(value, v:completed_item)
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100401endfunc
402
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200403func CompleteTest(findstart, query)
404 if a:findstart
405 return col('.')
406 endif
407 return ['matched']
408endfunc
409
410func Test_completefunc_info()
411 new
412 set completeopt=menuone
413 set completefunc=CompleteTest
414 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100415 call assert_equal("matched{'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}", getline(1))
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200416 bwipe!
417 set completeopt&
418 set completefunc&
419endfunc
420
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100421" Check that when using feedkeys() typeahead does not interrupt searching for
422" completions.
423func Test_compl_feedkeys()
424 new
425 set completeopt=menuone,noselect
426 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
427 call assert_equal("jump jump", getline(1))
428 bwipe!
429 set completeopt&
430endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200431
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200432" Test for insert path completion with completeslash option
433func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200434 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200435
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100436 call mkdir('Xcpldir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200437 let orig_shellslash = &shellslash
438 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200439 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200440
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200441 set noshellslash
442
443 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100444 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100445 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200446
447 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100448 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100449 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200450
451 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100452 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100453 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200454
455 set shellslash
456
457 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100458 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100459 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200460
461 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100462 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100463 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200464
465 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100466 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100467 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200468 %bw!
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100469 call delete('Xcpldir', 'rf')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200470
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200471 set noshellslash
472 set completeslash=slash
473 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
474
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200475 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200476 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200477endfunc
478
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200479func Test_pum_stopped_by_timer()
480 CheckScreendump
481
482 let lines =<< trim END
483 call setline(1, ['hello', 'hullo', 'heeee', ''])
484 func StartCompl()
485 call timer_start(100, { -> execute('stopinsert') })
486 call feedkeys("Gah\<C-N>")
487 endfunc
488 END
489
490 call writefile(lines, 'Xpumscript')
491 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
492 call term_sendkeys(buf, ":call StartCompl()\<CR>")
493 call TermWait(buf, 200)
494 call term_sendkeys(buf, "k")
495 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
496
497 call StopVimInTerminal(buf)
498 call delete('Xpumscript')
499endfunc
500
zeertzjqcd5dbad2022-05-04 17:51:50 +0100501func Test_complete_stopinsert_startinsert()
502 nnoremap <F2> <Cmd>startinsert<CR>
503 inoremap <F2> <Cmd>stopinsert<CR>
504 " This just checks if this causes an error
505 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
506 nunmap <F2>
507 iunmap <F2>
508endfunc
509
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100510func Test_pum_with_folds_two_tabs()
511 CheckScreendump
512
513 let lines =<< trim END
514 set fdm=marker
515 call setline(1, ['" x {{{1', '" a some text'])
516 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
517 norm! zm
518 tab sp
519 call feedkeys('2Gzv', 'xt')
520 call feedkeys("0fa", 'xt')
521 END
522
523 call writefile(lines, 'Xpumscript')
524 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200525 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100526 call term_sendkeys(buf, "a\<C-N>")
527 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
528
529 call term_sendkeys(buf, "\<Esc>")
530 call StopVimInTerminal(buf)
531 call delete('Xpumscript')
532endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100533
534func Test_pum_with_preview_win()
535 CheckScreendump
536
537 let lines =<< trim END
538 funct Omni_test(findstart, base)
539 if a:findstart
540 return col(".") - 1
541 endif
542 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
543 endfunc
544 set omnifunc=Omni_test
545 set completeopt+=longest
546 END
547
548 call writefile(lines, 'Xpreviewscript')
549 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200550 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100551 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200552 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100553 call term_sendkeys(buf, "\<C-N>")
554 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
555
556 call term_sendkeys(buf, "\<Esc>")
557 call StopVimInTerminal(buf)
558 call delete('Xpreviewscript')
559endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100560
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000561func Test_scrollbar_on_wide_char()
562 CheckScreendump
563
564 let lines =<< trim END
565 call setline(1, ['a', ' 啊啊啊',
566 \ ' 哦哦哦',
567 \ ' 呃呃呃'])
568 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
569 END
570 call writefile(lines, 'Xwidescript')
571 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
572 call term_sendkeys(buf, "A\<C-N>")
573 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
574
575 call StopVimInTerminal(buf)
576 call delete('Xwidescript')
577endfunc
578
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100579" Test for inserting the tag search pattern in insert mode
580func Test_ins_compl_tag_sft()
581 call writefile([
582 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
583 \ "first\tXfoo\t/^int first() {}$/",
584 \ "second\tXfoo\t/^int second() {}$/",
585 \ "third\tXfoo\t/^int third() {}$/"],
586 \ 'Xtags')
587 set tags=Xtags
588 let code =<< trim [CODE]
589 int first() {}
590 int second() {}
591 int third() {}
592 [CODE]
593 call writefile(code, 'Xfoo')
594
595 enew
596 set showfulltag
597 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
598 call assert_equal('int second() {}', getline(1))
599 set noshowfulltag
600
601 call delete('Xtags')
602 call delete('Xfoo')
603 set tags&
604 %bwipe!
605endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200606
607" Test for 'completefunc' deleting text
608func Test_completefunc_error()
609 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200610 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200611 func CompleteFunc(findstart, base)
612 if a:findstart == 1
613 normal dd
614 return col('.') - 1
615 endif
616 return ['a', 'b']
617 endfunc
618 set completefunc=CompleteFunc
619 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100620 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200621
622 " delete text when called for the second time
623 func CompleteFunc2(findstart, base)
624 if a:findstart == 1
625 return col('.') - 1
626 endif
627 normal dd
628 return ['a', 'b']
629 endfunc
630 set completefunc=CompleteFunc2
631 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100632 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200633
Bram Moolenaar97202d92021-01-28 18:34:35 +0100634 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100635 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100636 if a:findstart == 1
637 return col('.') - 1
638 endif
639 wincmd p
640 return ['a', 'b']
641 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100642 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100643 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100644 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100645 close!
646
647 set completefunc&
648 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100649 delfunc CompleteFunc2
650 delfunc CompleteFunc3
651 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100652endfunc
653
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200654" Test for returning non-string values from 'completefunc'
655func Test_completefunc_invalid_data()
656 new
657 func! CompleteFunc(findstart, base)
658 if a:findstart == 1
659 return col('.') - 1
660 endif
661 return [{}, '', 'moon']
662 endfunc
663 set completefunc=CompleteFunc
664 exe "normal i\<C-X>\<C-U>"
665 call assert_equal('moon', getline(1))
666 set completefunc&
667 close!
668endfunc
669
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200670" Test for errors in using complete() function
671func Test_complete_func_error()
672 call assert_fails('call complete(1, ["a"])', 'E785:')
673 func ListColors()
674 call complete(col('.'), "blue")
675 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100676 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200677 func ListMonths()
678 call complete(col('.'), test_null_list())
679 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100680 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200681 delfunc ListColors
682 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100683 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200684 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200685endfunc
686
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000687" Test for recursively starting completion mode using complete()
688func Test_recursive_complete_func()
689 func ListColors()
690 call complete(5, ["red", "blue"])
691 return ''
692 endfunc
693 new
694 call setline(1, ['a1', 'a2'])
695 set complete=.
696 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
697 call assert_equal('a2blue', getline(3))
698 delfunc ListColors
699 bw!
700endfunc
701
bfredl87af60c2022-09-24 11:17:51 +0100702" Test for using complete() with completeopt+=longest
703func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100704 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100705 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100706
707 " default: insert first match
708 set completeopt&
709 call setline(1, ['i'])
710 exe "normal Aa\<f3>\<esc>"
711 call assert_equal('iaax', getline(1))
712
713 " with longest: insert longest prefix
714 set completeopt+=longest
715 call setline(1, ['i'])
716 exe "normal Aa\<f3>\<esc>"
717 call assert_equal('iaa', getline(1))
718 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100719 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100720endfunc
721
722
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200723" Test for completing words following a completed word in a line
724func Test_complete_wrapscan()
725 " complete words from another buffer
726 new
727 call setline(1, ['one two', 'three four'])
728 new
729 setlocal complete=w
730 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
731 call assert_equal('two three four', getline(1))
732 close!
733 " complete words from the current buffer
734 setlocal complete=.
735 %d
736 call setline(1, ['one two', ''])
737 call cursor(2, 1)
738 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
739 call assert_equal('one two one two', getline(2))
740 close!
741endfunc
742
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200743" Test for completing special characters
744func Test_complete_special_chars()
745 new
746 call setline(1, 'int .*[-\^$ func float')
747 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
748 call assert_equal('int .*[-\^$ func float', getline(2))
749 close!
750endfunc
751
752" Test for completion when text is wrapped across lines.
753func Test_complete_across_line()
754 new
755 call setline(1, ['red green blue', 'one two three'])
756 setlocal textwidth=20
757 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
758 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
759 close!
760endfunc
761
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000762" Test for completing words with a '.' at the end of a word.
763func Test_complete_joinspaces()
764 new
765 call setline(1, ['one two.', 'three. four'])
766 set joinspaces
767 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
768 call assert_equal("one two. three. four", getline(3))
769 set joinspaces&
770 bw!
771endfunc
772
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200773" Test for using CTRL-L to add one character when completing matching
774func Test_complete_add_onechar()
775 new
776 call setline(1, ['wool', 'woodwork'])
777 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
778 call assert_equal('woof', getline(3))
779
780 " use 'ignorecase' and backspace to erase characters from the prefix string
781 " and then add letters using CTRL-L
782 %d
783 set ignorecase backspace=2
784 setlocal complete=.
785 call setline(1, ['workhorse', 'workload'])
786 normal Go
787 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
788 call assert_equal('workh', getline(3))
789 set ignorecase& backspace&
790 close!
791endfunc
792
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000793" Test for using CTRL-X CTRL-L to complete whole lines lines
794func Test_complete_wholeline()
795 new
796 " complete one-line
797 call setline(1, ['a1', 'a2'])
798 exe "normal ggoa\<C-X>\<C-L>"
799 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
800 " go to the next match (wrapping around the buffer)
801 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
802 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
803 " go to the next match
804 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
805 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
806 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
807 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
808 " repeat the test using CTRL-L
809 " go to the next match (wrapping around the buffer)
810 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
811 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
812 " go to the next match
813 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
814 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
815 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
816 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
817 %d
818 " use CTRL-X CTRL-L to add one more line
819 call setline(1, ['a1', 'b1'])
820 setlocal complete=.
821 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
822 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
823 bw!
824endfunc
825
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200826" Test insert completion with 'cindent' (adjust the indent)
827func Test_complete_with_cindent()
828 new
829 setlocal cindent
830 call setline(1, ['if (i == 1)', " j = 2;"])
831 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
832 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
833
834 %d
835 call setline(1, ['when while', '{', ''])
836 setlocal cinkeys+==while
837 exe "normal Giwh\<C-P> "
838 call assert_equal("\twhile ", getline('$'))
839 close!
840endfunc
841
842" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
843func Test_complete_cmdline()
844 new
845 exe "normal icaddb\<C-X>\<C-V>"
846 call assert_equal('caddbuffer', getline(1))
847 exe "normal ocall getqf\<C-X>\<C-V>"
848 call assert_equal('call getqflist(', getline(2))
849 exe "normal oabcxyz(\<C-X>\<C-V>"
850 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200851 com! -buffer TestCommand1 echo 'TestCommand1'
852 com! -buffer TestCommand2 echo 'TestCommand2'
853 write TestCommand1Test
854 write TestCommand2Test
855 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
856 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
857 call assert_equal('TestCommand2Test', getline(4))
858 call delete('TestCommand1Test')
859 call delete('TestCommand2Test')
860 delcom TestCommand1
861 delcom TestCommand2
862 close!
863endfunc
864
865" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
866func Test_complete_stop()
867 new
868 func Save_mode1()
869 let g:mode1 = mode(1)
870 return ''
871 endfunc
872 func Save_mode2()
873 let g:mode2 = mode(1)
874 return ''
875 endfunc
876 inoremap <F1> <C-R>=Save_mode1()<CR>
877 inoremap <F2> <C-R>=Save_mode2()<CR>
878 call setline(1, ['aaa bbb ccc '])
879 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
880 call assert_equal('ic', g:mode1)
881 call assert_equal('i', g:mode2)
882 call assert_equal('aaa bbb ccc ', getline(1))
883 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
884 call assert_equal('ic', g:mode1)
885 call assert_equal('i', g:mode2)
886 call assert_equal('aaa bbb ccc aaa', getline(1))
887 set completeopt+=noselect
888 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
889 call assert_equal('ic', g:mode1)
890 call assert_equal('i', g:mode2)
891 call assert_equal('aaa bbb ccc aaa bb', getline(1))
892 set completeopt&
893 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
894 call assert_equal('ic', g:mode1)
895 call assert_equal('i', g:mode2)
896 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
897 com! -buffer TestCommand1 echo 'TestCommand1'
898 com! -buffer TestCommand2 echo 'TestCommand2'
899 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
900 call assert_equal('ic', g:mode1)
901 call assert_equal('i', g:mode2)
902 call assert_equal('TestCommand2', getline(2))
903 delcom TestCommand1
904 delcom TestCommand2
905 unlet g:mode1
906 unlet g:mode2
907 iunmap <F1>
908 iunmap <F2>
909 delfunc Save_mode1
910 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200911 close!
912endfunc
913
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000914" Test for typing CTRL-R in insert completion mode to insert a register
915" content.
916func Test_complete_reginsert()
917 new
918 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
919
920 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
921 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
922 " inserted.
923 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
924 call assert_equal('a123', getline(5))
925 let @r = "\<C-P>\<C-P>"
926 exe "normal GCa\<C-P>\<C-R>r"
927 call assert_equal('a12', getline(5))
928 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
929 call assert_equal('a1234x', getline(5))
930 bw!
931endfunc
932
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200933func Test_issue_7021()
934 CheckMSWindows
935
936 let orig_shellslash = &shellslash
937 set noshellslash
938
939 set completeslash=slash
940 call assert_false(expand('~') =~ '/')
941
942 let &shellslash = orig_shellslash
943 set completeslash=
944endfunc
945
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000946" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
947func Test_complete_longest_match()
948 for e in ['latin1', 'utf-8']
949 exe 'set encoding=' .. e
950 new
951 set complete=.
952 set completeopt=menu,longest
953 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
954 exe "normal Gopfx\<C-P>"
955 call assert_equal('pfx_', getline(5))
956 bw!
957 endfor
958
959 " Test for completing additional words with longest match set
960 new
961 call setline(1, ['abc1', 'abd2'])
962 exe "normal Goab\<C-P>\<C-X>\<C-P>"
963 call assert_equal('ab', getline(3))
964 bw!
965 set complete& completeopt&
966endfunc
967
968" Test for removing the first displayed completion match and selecting the
969" match just before that.
970func Test_complete_erase_firstmatch()
971 new
972 call setline(1, ['a12', 'a34', 'a56'])
973 set complete=.
974 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
975 call assert_equal('a34', getline('$'))
976 set complete&
977 bw!
978endfunc
979
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000980" Test for completing words from unloaded buffers
981func Test_complete_from_unloadedbuf()
982 call writefile(['abc'], "Xfile1")
983 call writefile(['def'], "Xfile2")
984 edit Xfile1
985 edit Xfile2
986 new | close
987 enew
988 bunload Xfile1 Xfile2
989 set complete=u
990 " complete from an unloaded buffer
991 exe "normal! ia\<C-P>"
992 call assert_equal('abc', getline(1))
993 exe "normal! od\<C-P>"
994 call assert_equal('def', getline(2))
995 set complete&
996 %bw!
997 call delete("Xfile1")
998 call delete("Xfile2")
999endfunc
1000
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001001" Test for completing whole lines from unloaded buffers
1002func Test_complete_wholeline_unloadedbuf()
1003 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1004 edit Xfile1
1005 enew
1006 set complete=u
1007 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1008 call assert_equal('a line2', getline(1))
1009 %d
1010 " completing from an unlisted buffer should fail
1011 bdel Xfile1
1012 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1013 call assert_equal('a', getline(1))
1014 set complete&
1015 %bw!
1016 call delete("Xfile1")
1017endfunc
1018
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001019" Test for completing words from unlisted buffers
1020func Test_complete_from_unlistedbuf()
1021 call writefile(['abc'], "Xfile1")
1022 call writefile(['def'], "Xfile2")
1023 edit Xfile1
1024 edit Xfile2
1025 new | close
1026 bdel Xfile1 Xfile2
1027 set complete=U
1028 " complete from an unlisted buffer
1029 exe "normal! ia\<C-P>"
1030 call assert_equal('abc', getline(1))
1031 exe "normal! od\<C-P>"
1032 call assert_equal('def', getline(2))
1033 set complete&
1034 %bw!
1035 call delete("Xfile1")
1036 call delete("Xfile2")
1037endfunc
1038
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001039" Test for completing whole lines from unlisted buffers
1040func Test_complete_wholeline_unlistedbuf()
1041 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1042 edit Xfile1
1043 enew
1044 set complete=U
1045 " completing from a unloaded buffer should fail
1046 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1047 call assert_equal('a', getline(1))
1048 %d
1049 bdel Xfile1
1050 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1051 call assert_equal('a line2', getline(1))
1052 set complete&
1053 %bw!
1054 call delete("Xfile1")
1055endfunc
1056
1057" Test for adding a multibyte character using CTRL-L in completion mode
1058func Test_complete_mbyte_char_add()
1059 new
1060 set complete=.
1061 call setline(1, 'abė')
1062 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1063 call assert_equal('abė', getline(2))
1064 " Test for a leader with multibyte character
1065 %d
1066 call setline(1, 'abėĕ')
1067 exe "normal! oabė\<C-P>"
1068 call assert_equal('abėĕ', getline(2))
1069 bw!
1070endfunc
1071
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001072" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1073" not to complete matches from the local buffer. Also test using multiple
1074" <C-X> to cancel the current completion mode.
1075func Test_complete_local_expansion()
1076 new
1077 set complete=t
1078 call setline(1, ['abc', 'def'])
1079 exe "normal! Go\<C-X>\<C-P>"
1080 call assert_equal("def", getline(3))
1081 exe "normal! Go\<C-P>"
1082 call assert_equal("", getline(4))
1083 exe "normal! Go\<C-X>\<C-N>"
1084 call assert_equal("abc", getline(5))
1085 exe "normal! Go\<C-N>"
1086 call assert_equal("", getline(6))
1087
1088 " use multiple <C-X> to cancel the previous completion mode
1089 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1090 call assert_equal("", getline(7))
1091 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1092 call assert_equal("", getline(8))
1093 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1094 call assert_equal("abc", getline(9))
1095
1096 " interrupt the current completion mode
1097 set completeopt=menu,noinsert
1098 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1099 call assert_equal("abc", getline(10))
1100
1101 " when only one <C-X> is used to interrupt, do normal expansion
1102 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1103 call assert_equal("", getline(11))
1104 set completeopt&
1105
1106 " using two <C-X> in non-completion mode and restarting the same mode
1107 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1108 call assert_equal("def", getline(12))
1109
1110 " test for adding a match from the original empty text
1111 %d
1112 call setline(1, 'abc def g')
1113 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1114 call assert_equal('def', getline(2))
1115 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1116 call assert_equal('abc', getline(2))
1117
1118 bw!
1119endfunc
1120
1121" Test for undoing changes after a insert-mode completion
1122func Test_complete_undo()
1123 new
1124 set complete=.
1125 " undo with 'ignorecase'
1126 call setline(1, ['ABOVE', 'BELOW'])
1127 set ignorecase
1128 exe "normal! Goab\<C-G>u\<C-P>"
1129 call assert_equal("ABOVE", getline(3))
1130 undo
1131 call assert_equal("ab", getline(3))
1132 set ignorecase&
1133 %d
1134 " undo with longest match
1135 set completeopt=menu,longest
1136 call setline(1, ['above', 'about'])
1137 exe "normal! Goa\<C-G>u\<C-P>"
1138 call assert_equal("abo", getline(3))
1139 undo
1140 call assert_equal("a", getline(3))
1141 set completeopt&
1142 %d
1143 " undo for line completion
1144 call setline(1, ['above that change', 'below that change'])
1145 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1146 call assert_equal("above that change", getline(3))
1147 undo
1148 call assert_equal("above", getline(3))
1149
1150 bw!
1151endfunc
1152
1153" Test for completing a very long word
1154func Test_complete_long_word()
1155 set complete&
1156 new
1157 call setline(1, repeat('x', 950) .. ' one two three')
1158 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1159 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1160 %d
1161 " should fail when more than 950 characters are in a word
1162 call setline(1, repeat('x', 951) .. ' one two three')
1163 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1164 call assert_equal(repeat('x', 951), getline(2))
1165
1166 " Test for adding a very long word to an existing completion
1167 %d
1168 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1169 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1170 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1171 bw!
1172endfunc
1173
1174" Test for some fields in the complete items used by complete()
1175func Test_complete_items()
1176 func CompleteItems(idx)
1177 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1178 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1179 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1180 \ [#{user_data: 'u9'}],
1181 \ [#{word: "", user_data: 'u10'}],
1182 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1183 call complete(col('.'), items[a:idx])
1184 return ''
1185 endfunc
1186 new
1187 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1188 call assert_equal('u2', v:completed_item.user_data)
1189 call assert_equal('one', getline(1))
1190 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1191 call assert_equal('u3', v:completed_item.user_data)
1192 call assert_equal('one', getline(2))
1193 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1194 call assert_equal('', getline(3))
1195 set completeopt=menu,noinsert
1196 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1197 call assert_equal('oNE', getline(4))
1198 call assert_equal('u8', v:completed_item.user_data)
1199 set completeopt&
1200 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1201 call assert_equal('', getline(5))
1202 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1203 call assert_equal('', getline(6))
1204 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1205 call assert_equal('', getline(7))
1206 call assert_equal('u11', v:completed_item.user_data)
1207 " pass invalid argument to complete()
1208 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1209 call assert_fails('exe cmd', 'E730:')
1210 bw!
1211 delfunc CompleteItems
1212endfunc
1213
1214" Test for the "refresh" item in the dict returned by an insert completion
1215" function
1216func Test_complete_item_refresh_always()
1217 let g:CallCount = 0
1218 func! Tcomplete(findstart, base)
1219 if a:findstart
1220 " locate the start of the word
1221 let line = getline('.')
1222 let start = col('.') - 1
1223 while start > 0 && line[start - 1] =~ '\a'
1224 let start -= 1
1225 endwhile
1226 return start
1227 else
1228 let g:CallCount += 1
1229 let res = ["update1", "update12", "update123"]
1230 return #{words: res, refresh: 'always'}
1231 endif
1232 endfunc
1233 new
1234 set completeopt=menu,longest
1235 set completefunc=Tcomplete
1236 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1237 call assert_equal('up', getline(1))
1238 call assert_equal(2, g:CallCount)
1239 set completeopt&
1240 set completefunc&
1241 bw!
1242 delfunc Tcomplete
1243endfunc
1244
1245" Test for completing from a thesaurus file without read permission
1246func Test_complete_unreadable_thesaurus_file()
1247 CheckUnix
1248 CheckNotRoot
1249
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001250 call writefile(['about', 'above'], 'Xunrfile')
1251 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001252 new
1253 set complete=sXfile
1254 exe "normal! ia\<C-P>"
1255 call assert_equal('a', getline(1))
1256 bw!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001257 call delete('Xunrfile')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001258 set complete&
1259endfunc
1260
Bram Moolenaarcc233582020-12-12 13:32:07 +01001261" Test to ensure 'Scanning...' messages are not recorded in messages history
1262func Test_z1_complete_no_history()
1263 new
1264 messages clear
1265 let currmess = execute('messages')
1266 setlocal dictionary=README.txt
1267 exe "normal owh\<C-X>\<C-K>"
1268 exe "normal owh\<C-N>"
1269 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001270 bwipe!
1271endfunc
1272
1273" A mapping is not used for the key after CTRL-X.
1274func Test_no_mapping_for_ctrl_x_key()
1275 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001276 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001277 setlocal dictionary=README.txt
1278 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1279 call assert_equal('example ', getline(1))
1280 call assert_false(exists('was_mapped'))
1281 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001282endfunc
1283
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001284" Test for different ways of setting the 'completefunc' option
1285func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001286 func CompleteFunc1(callnr, findstart, base)
1287 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1288 return a:findstart ? 0 : []
1289 endfunc
1290 func CompleteFunc2(findstart, base)
1291 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001292 return a:findstart ? 0 : []
1293 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001294
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001295 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001296 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001297 LET &completefunc = 'g:CompleteFunc2'
1298 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001299 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001300 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001301 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001302 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001303 bw!
1304
1305 #" Test for using a function()
1306 set completefunc=function('g:CompleteFunc1',\ [10])
1307 new
1308 call setline(1, 'one')
1309 LET g:CompleteFunc1Args = []
1310 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1311 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001312 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001313
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001314 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001315 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001316 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001317 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001318 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001319 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001320 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001321 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001322 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001323
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001324 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001325 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001326 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001327 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001329 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001330 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001331 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001332 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001333
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001334 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 set completefunc=funcref('g:CompleteFunc1',\ [13])
1336 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001337 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001338 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001339 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001340 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001341 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001342
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001343 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001344 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001345 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001346 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001347 call setline(1, 'four')
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([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001351 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001352
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001353 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001354 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001355 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001356 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001357 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001358 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001359 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001360 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001361 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001362
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001363 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001364 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001365 LET optval = substitute(optval, ' ', '\\ ', 'g')
1366 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001367 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001368 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001369 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001370 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001371 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001372 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001373
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001374 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001375 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001376 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001378 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001380 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001382
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001384 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001385 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001386 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001387 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001388 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001389 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001390 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001391
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001392 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001393 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001394 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001395 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001396 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001397 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001398 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001399 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001400 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001401
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001402 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001403 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001405 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001407 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001408 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001409 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001411
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001412 #" Test for using a lambda function with incorrect return value
1413 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1414 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001415 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001416 call setline(1, 'eight')
1417 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1418 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001419
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001420 #" Test for clearing the 'completefunc' option
1421 set completefunc=''
1422 set completefunc&
1423 call assert_fails("set completefunc=function('abc')", "E700:")
1424 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001425
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001426 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001427 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001428 call setline(1, 'five')
1429 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1430 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001431 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001433 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001434 bw!
1435 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001436 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001437
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001438 " Test for using a script-local function name
1439 func s:CompleteFunc3(findstart, base)
1440 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1441 return a:findstart ? 0 : []
1442 endfunc
1443 set completefunc=s:CompleteFunc3
1444 new
1445 call setline(1, 'script1')
1446 let g:CompleteFunc3Args = []
1447 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1448 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1449 bw!
1450
1451 let &completefunc = 's:CompleteFunc3'
1452 new
1453 call setline(1, 'script2')
1454 let g:CompleteFunc3Args = []
1455 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1456 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1457 bw!
1458 delfunc s:CompleteFunc3
1459
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001460 " In Vim9 script s: can be omitted
1461 let lines =<< trim END
1462 vim9script
1463 var CompleteFunc4Args = []
1464 def CompleteFunc4(findstart: bool, base: string): any
1465 add(CompleteFunc4Args, [findstart, base])
1466 return findstart ? 0 : []
1467 enddef
1468 set completefunc=CompleteFunc4
1469 new
1470 setline(1, 'script1')
1471 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1472 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1473 bw!
1474 END
1475 call v9.CheckScriptSuccess(lines)
1476
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001477 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001478 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001479 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1480
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001481 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001482 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001483 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001484 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001485 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001486 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001487
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001488 " set 'completefunc' to a partial with dict. This used to cause a crash.
1489 func SetCompleteFunc()
1490 let params = {'complete': function('g:DictCompleteFunc')}
1491 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001492 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001493 func g:DictCompleteFunc(_) dict
1494 endfunc
1495 call SetCompleteFunc()
1496 new
1497 call SetCompleteFunc()
1498 bw
1499 call test_garbagecollect_now()
1500 new
1501 set completefunc=
1502 wincmd w
1503 set completefunc=
1504 %bw!
1505 delfunc g:DictCompleteFunc
1506 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001507
1508 " Vim9 tests
1509 let lines =<< trim END
1510 vim9script
1511
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001512 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1513 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001514 return findstart ? 0 : []
1515 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001516
1517 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001518 set completefunc=function('Vim9CompleteFunc',\ [60])
1519 new | only
1520 setline(1, 'one')
1521 g:Vim9completeFuncArgs = []
1522 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1523 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1524 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001525
1526 # Test for using a global function name
1527 &completefunc = g:CompleteFunc2
1528 new | only
1529 setline(1, 'two')
1530 g:CompleteFunc2Args = []
1531 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1532 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1533 bw!
1534
1535 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001536 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001537 add(g:LocalCompleteFuncArgs, [findstart, base])
1538 return findstart ? 0 : []
1539 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001540 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001541 new | only
1542 setline(1, 'three')
1543 g:LocalCompleteFuncArgs = []
1544 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1545 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1546 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001547 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001548 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001549
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001550 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001551 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001552 delfunc CompleteFunc1
1553 delfunc CompleteFunc2
1554 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001555 %bw!
1556endfunc
1557
1558" Test for different ways of setting the 'omnifunc' option
1559func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001560 func OmniFunc1(callnr, findstart, base)
1561 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1562 return a:findstart ? 0 : []
1563 endfunc
1564 func OmniFunc2(findstart, base)
1565 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001566 return a:findstart ? 0 : []
1567 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001568
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001569 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001570 #" Test for using a function name
1571 LET &omnifunc = 'g:OmniFunc2'
1572 new
1573 call setline(1, 'zero')
1574 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001575 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001576 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1577 bw!
1578
1579 #" Test for using a function()
1580 set omnifunc=function('g:OmniFunc1',\ [10])
1581 new
1582 call setline(1, 'one')
1583 LET g:OmniFunc1Args = []
1584 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1585 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001586 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001587
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001588 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001589 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001590 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001591 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001592 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001593 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001594 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001595 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001596 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001597
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001598 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001599 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001600 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001601 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001603 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001604 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001605 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001606 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001607
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001608 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001609 set omnifunc=funcref('g:OmniFunc1',\ [13])
1610 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001611 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001612 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001613 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001614 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001615 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001616
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001617 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001618 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001619 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001620 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001621 call setline(1, 'four')
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([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001625 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001626
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001627 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001628 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001629 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001631 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001632 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001633 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001634 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001635 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001636
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001637 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001638 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001639 LET optval = substitute(optval, ' ', '\\ ', 'g')
1640 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001641 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001643 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001645 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001646 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001647
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001648 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001649 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001652 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001654 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001656
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001658 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001659 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001661 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001662 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001663 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001664 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001665
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001666 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001667 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001668 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001669 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001670 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001671 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001672 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001673 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001674 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001675
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001676 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001677 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001679 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001681 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001683 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001685
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001686 #" Test for using a lambda function with incorrect return value
1687 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1688 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001689 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001690 call setline(1, 'eight')
1691 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1692 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001693
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001694 #" Test for clearing the 'omnifunc' option
1695 set omnifunc=''
1696 set omnifunc&
1697 call assert_fails("set omnifunc=function('abc')", "E700:")
1698 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001699
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001700 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001701 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001702 call setline(1, 'nine')
1703 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1704 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001706 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001707 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001708 bw!
1709 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001710 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001711
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001712 " Test for using a script-local function name
1713 func s:OmniFunc3(findstart, base)
1714 call add(g:OmniFunc3Args, [a:findstart, a:base])
1715 return a:findstart ? 0 : []
1716 endfunc
1717 set omnifunc=s:OmniFunc3
1718 new
1719 call setline(1, 'script1')
1720 let g:OmniFunc3Args = []
1721 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1722 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1723 bw!
1724
1725 let &omnifunc = 's:OmniFunc3'
1726 new
1727 call setline(1, 'script2')
1728 let g:OmniFunc3Args = []
1729 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1730 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1731 bw!
1732 delfunc s:OmniFunc3
1733
1734 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001735 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001736 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1737
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001738 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001739 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001740 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001741 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001742 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001743 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001744
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001745 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1746 func SetOmniFunc()
1747 let params = {'omni': function('g:DictOmniFunc')}
1748 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001749 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001750 func g:DictOmniFunc(_) dict
1751 endfunc
1752 call SetOmniFunc()
1753 new
1754 call SetOmniFunc()
1755 bw
1756 call test_garbagecollect_now()
1757 new
1758 set omnifunc=
1759 wincmd w
1760 set omnifunc=
1761 %bw!
1762 delfunc g:DictOmniFunc
1763 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001764
1765 " Vim9 tests
1766 let lines =<< trim END
1767 vim9script
1768
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001769 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1770 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001771 return findstart ? 0 : []
1772 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001773
1774 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001775 set omnifunc=function('Vim9omniFunc',\ [60])
1776 new | only
1777 setline(1, 'one')
1778 g:Vim9omniFunc_Args = []
1779 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1780 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1781 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001782
1783 # Test for using a global function name
1784 &omnifunc = g:OmniFunc2
1785 new | only
1786 setline(1, 'two')
1787 g:OmniFunc2Args = []
1788 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1789 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1790 bw!
1791
1792 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001793 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001794 add(g:LocalOmniFuncArgs, [findstart, base])
1795 return findstart ? 0 : []
1796 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001797 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001798 new | only
1799 setline(1, 'three')
1800 g:LocalOmniFuncArgs = []
1801 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1802 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1803 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001804 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001805 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001806
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001807 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001808 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001809 delfunc OmniFunc1
1810 delfunc OmniFunc2
1811 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001812 %bw!
1813endfunc
1814
1815" Test for different ways of setting the 'thesaurusfunc' option
1816func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001817 func TsrFunc1(callnr, findstart, base)
1818 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001819 return a:findstart ? 0 : []
1820 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001821 func TsrFunc2(findstart, base)
1822 call add(g:TsrFunc2Args, [a:findstart, a:base])
1823 return a:findstart ? 0 : ['sunday']
1824 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001825
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001826 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001827 #" Test for using a function name
1828 LET &thesaurusfunc = 'g:TsrFunc2'
1829 new
1830 call setline(1, 'zero')
1831 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001832 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001833 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1834 bw!
1835
1836 #" Test for using a function()
1837 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1838 new
1839 call setline(1, 'one')
1840 LET g:TsrFunc1Args = []
1841 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1842 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001843 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001844
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001845 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001846 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001847 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001848 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001849 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001850 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001851 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001852 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001853 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001854
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001855 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001856 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001857 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001858 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001859 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001860 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001861 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001862 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001863 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001864
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001865 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001866 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1867 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001868 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001869 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001870 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001871 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001872 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001873
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001874 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001875 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001876 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001877 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001878 call setline(1, 'four')
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([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001882 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001883
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001884 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001885 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001886 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001887 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001888 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001889 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001890 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001891 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001892 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001893
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001894 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001895 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 LET optval = substitute(optval, ' ', '\\ ', 'g')
1897 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001898 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001899 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001900 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001901 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001902 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001903 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001904
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001905 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001906 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001907 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001909 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001911 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001913
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001915 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001916 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001917 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001918 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001919 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001920 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001921 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001922
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001924 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001925 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001926 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001927 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001928 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001929 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001930 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001931 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001932
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001933 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001934 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001936 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001937 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001938 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001939 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001940 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001942
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001943 #" Test for using a lambda function with incorrect return value
1944 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1945 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001946 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001947 call setline(1, 'eight')
1948 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1949 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001950
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 #" Test for clearing the 'thesaurusfunc' option
1952 set thesaurusfunc=''
1953 set thesaurusfunc&
1954 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1955 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001956
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001958 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 call setline(1, 'ten')
1960 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1961 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001964 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001965 bw!
1966
1967 #" Use a buffer-local value and a global value
1968 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001969 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001970 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001971 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001972 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1973 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001974 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975 new
1976 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001977 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001978 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1979 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001980 call assert_equal([], g:TsrFunc1Args)
1981 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001982 wincmd w
1983 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001984 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001985 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1986 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001987 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001988 :%bw!
1989 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001990 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001991
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001992 " Test for using a script-local function name
1993 func s:TsrFunc3(findstart, base)
1994 call add(g:TsrFunc3Args, [a:findstart, a:base])
1995 return a:findstart ? 0 : []
1996 endfunc
1997 set tsrfu=s:TsrFunc3
1998 new
1999 call setline(1, 'script1')
2000 let g:TsrFunc3Args = []
2001 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2002 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2003 bw!
2004
2005 let &tsrfu = 's:TsrFunc3'
2006 new
2007 call setline(1, 'script2')
2008 let g:TsrFunc3Args = []
2009 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2010 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2011 bw!
2012 delfunc s:TsrFunc3
2013
2014 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002015 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002016 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2017
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002018 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002019 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002020 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002021 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002022 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002023 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002024 bw!
2025
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002026 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2027 func SetTsrFunc()
2028 let params = {'thesaurus': function('g:DictTsrFunc')}
2029 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002030 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002031 func g:DictTsrFunc(_) dict
2032 endfunc
2033 call SetTsrFunc()
2034 new
2035 call SetTsrFunc()
2036 bw
2037 call test_garbagecollect_now()
2038 new
2039 set thesaurusfunc=
2040 wincmd w
2041 %bw!
2042 delfunc SetTsrFunc
2043
2044 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2045 " cause a crash.
2046 func SetLocalTsrFunc()
2047 let params = {'thesaurus': function('g:DictTsrFunc')}
2048 let &l:thesaurusfunc = params.thesaurus
2049 endfunc
2050 call SetLocalTsrFunc()
2051 call test_garbagecollect_now()
2052 call SetLocalTsrFunc()
2053 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002054 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002055 delfunc g:DictTsrFunc
2056 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002057
2058 " Vim9 tests
2059 let lines =<< trim END
2060 vim9script
2061
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002062 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2063 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002064 return findstart ? 0 : []
2065 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002066
2067 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002068 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2069 new | only
2070 setline(1, 'one')
2071 g:Vim9tsrFunc_Args = []
2072 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2073 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2074 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002075
2076 # Test for using a global function name
2077 &thesaurusfunc = g:TsrFunc2
2078 new | only
2079 setline(1, 'two')
2080 g:TsrFunc2Args = []
2081 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2082 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2083 bw!
2084
2085 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002086 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002087 add(g:LocalTsrFuncArgs, [findstart, base])
2088 return findstart ? 0 : []
2089 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002090 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002091 new | only
2092 setline(1, 'three')
2093 g:LocalTsrFuncArgs = []
2094 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2095 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2096 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002097 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002098 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002099
2100 " cleanup
2101 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002102 delfunc TsrFunc1
2103 delfunc TsrFunc2
2104 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002105 %bw!
2106endfunc
2107
Christian Brabandtac72c212022-04-07 21:00:53 +01002108func FooBarComplete(findstart, base)
2109 if a:findstart
2110 return col('.') - 1
2111 else
2112 return ["Foo", "Bar", "}"]
2113 endif
2114endfunc
2115
2116func Test_complete_smartindent()
2117 new
2118 setlocal smartindent completefunc=FooBarComplete
2119
2120 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2121 let result = getline(1,'$')
2122 call assert_equal(['', '{','}',''], result)
2123 bw!
2124 delfunction! FooBarComplete
2125endfunc
2126
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002127func Test_complete_overrun()
2128 " this was going past the end of the copied text
2129 new
2130 sil norm si”0s0 
2131 bwipe!
2132endfunc
2133
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002134func Test_infercase_very_long_line()
2135 " this was truncating the line when inferring case
2136 new
2137 let longLine = "blah "->repeat(300)
2138 let verylongLine = "blah "->repeat(400)
2139 call setline(1, verylongLine)
2140 call setline(2, longLine)
2141 set ic infercase
2142 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2143 call assert_equal(longLine, getline(3))
2144
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002145 " check that the too long text is NUL terminated
2146 %del
2147 norm o
2148 norm 1987ax
2149 exec "norm ox\<C-X>\<C-L>"
2150 call assert_equal(repeat('x', 1987), getline(3))
2151
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002152 bwipe!
2153 set noic noinfercase
2154endfunc
2155
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002156func Test_ins_complete_add()
2157 " this was reading past the end of allocated memory
2158 new
2159 norm o
2160 norm 7o€€
2161 sil! norm o
2162
2163 bwipe!
2164endfunc
2165
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002166func Test_ins_complete_end_of_line()
2167 " this was reading past the end of the line
2168 new
2169 norm 8o€ý 
2170 sil! norm o
2171
2172 bwipe!
2173endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002174
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002175" vim: shiftwidth=2 sts=2 expandtab