blob: 7bebc5d8a996d0dcd2f7bfceb10e012f717bd14c [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
12 call mkdir('Xdir')
13 cd Xdir
14
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
102 call delete('Xtestfile')
103 call delete('Xtest11.one')
104 call delete('Xtest11.two')
105 call delete('Xtestdata')
106 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100107 cd ..
108 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200109endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100110
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000111func Test_ins_complete_invalid_byte()
112 if has('unix') && executable('base64')
113 " this weird command was causing an illegal memory access
114 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
115 call system('base64 -d Xinvalid64 > Xinvalid')
116 call writefile(['qa!'], 'Xexit')
117 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
118 call delete('Xinvalid64')
119 call delete('Xinvalid')
120 call delete('Xexit')
121 endif
122endfunc
123
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100124func Test_omni_dash()
125 func Omni(findstart, base)
126 if a:findstart
127 return 5
128 else
129 echom a:base
130 return ['-help', '-v']
131 endif
132 endfunc
133 set omnifunc=Omni
134 new
135 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100136 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100137
138 bwipe!
139 delfunc Omni
140 set omnifunc=
141endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100142
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100143func Test_omni_throw()
144 let g:CallCount = 0
145 func Omni(findstart, base)
146 let g:CallCount += 1
147 if a:findstart
148 throw "he he he"
149 endif
150 endfunc
151 set omnifunc=Omni
152 new
153 try
154 exe "normal ifoo\<C-x>\<C-o>"
155 call assert_false(v:true, 'command should have failed')
156 catch
157 call assert_exception('he he he')
158 call assert_equal(1, g:CallCount)
159 endtry
160
161 bwipe!
162 delfunc Omni
163 unlet g:CallCount
164 set omnifunc=
165endfunc
166
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200167func Test_omni_autoload()
168 let save_rtp = &rtp
169 set rtp=Xruntime/some
170 let dir = 'Xruntime/some/autoload'
171 call mkdir(dir, 'p')
172
173 let lines =<< trim END
174 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000175 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200176 if findstart
177 return 1
178 else
179 return ['match']
180 endif
181 enddef
182 {
183 eval 1 + 2
184 }
185 END
186 call writefile(lines, dir .. '/omni.vim')
187
188 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000189 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200190 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
191
192 bwipe!
193 call delete('Xruntime', 'rf')
194 set omnifunc=
195 let &rtp = save_rtp
196endfunc
197
Bram Moolenaarffa96842018-06-12 22:05:14 +0200198func Test_completefunc_args()
199 let s:args = []
200 func! CompleteFunc(findstart, base)
201 let s:args += [[a:findstart, empty(a:base)]]
202 endfunc
203 new
204
205 set completefunc=CompleteFunc
206 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200207 call assert_equal([1, 1], s:args[0])
208 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200209 set completefunc=
210
211 let s:args = []
212 set omnifunc=CompleteFunc
213 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200214 call assert_equal([1, 1], s:args[0])
215 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200216 set omnifunc=
217
218 bwipe!
219 unlet s:args
220 delfunc CompleteFunc
221endfunc
222
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100223func s:CompleteDone_CompleteFuncNone( findstart, base )
224 if a:findstart
225 return 0
226 endif
227
228 return v:none
229endfunc
230
Bram Moolenaar1e115362019-01-09 23:01:02 +0100231func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100232 if a:findstart
233 return 0
234 endif
235
236 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100237 \ 'words': [
238 \ {
239 \ 'word': 'aword',
240 \ 'abbr': 'wrd',
241 \ 'menu': 'extra text',
242 \ 'info': 'words are cool',
243 \ 'kind': 'W',
244 \ 'user_data': 'test'
245 \ }
246 \ ]
247 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100248endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100249
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100250func s:CompleteDone_CheckCompletedItemNone()
251 let s:called_completedone = 1
252endfunc
253
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100254func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100255 call assert_equal( 'aword', v:completed_item[ 'word' ] )
256 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
257 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
258 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
259 call assert_equal( 'W', v:completed_item[ 'kind' ] )
260 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
261
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100262 if a:pre
263 call assert_equal('function', complete_info().mode)
264 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100265
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100266 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100267endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100268
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100269func Test_CompleteDoneNone()
270 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 set completefunc=<SID>CompleteDone_CompleteFuncNone
274 execute "normal a\<C-X>\<C-U>\<C-Y>"
275 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200276 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100277
278 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200279 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100280
281 let s:called_completedone = 0
282 au! CompleteDone
283endfunc
284
285func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100286 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
287 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100288
289 set completefunc=<SID>CompleteDone_CompleteFuncDict
290 execute "normal a\<C-X>\<C-U>\<C-Y>"
291 set completefunc&
292
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100293 call assert_equal('test', v:completed_item[ 'user_data' ])
294 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100295
296 let s:called_completedone = 0
297 au! CompleteDone
298endfunc
299
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100300func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100301 if a:findstart
302 return 0
303 endif
304
305 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100306 \ 'words': [
307 \ {
308 \ 'word': 'aword',
309 \ 'abbr': 'wrd',
310 \ 'menu': 'extra text',
311 \ 'info': 'words are cool',
312 \ 'kind': 'W',
313 \ 'user_data': ['one', 'two'],
314 \ }
315 \ ]
316 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100317endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100318
Bram Moolenaar1e115362019-01-09 23:01:02 +0100319func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320 call assert_equal( 'aword', v:completed_item[ 'word' ] )
321 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
322 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
323 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
324 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100325 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100326
327 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100328endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100330func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100331 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
332
333 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
334 execute "normal a\<C-X>\<C-U>\<C-Y>"
335 set completefunc&
336
Bram Moolenaar08928322020-01-04 14:32:48 +0100337 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100338 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100339
340 let s:called_completedone = 0
341 au! CompleteDone
342endfunc
343
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100344func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100345 if a:findstart
346 return 0
347 endif
348
349 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100350endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100351
Bram Moolenaar1e115362019-01-09 23:01:02 +0100352func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100353 call assert_equal( 'aword', v:completed_item[ 'word' ] )
354 call assert_equal( '', v:completed_item[ 'abbr' ] )
355 call assert_equal( '', v:completed_item[ 'menu' ] )
356 call assert_equal( '', v:completed_item[ 'info' ] )
357 call assert_equal( '', v:completed_item[ 'kind' ] )
358 call assert_equal( '', v:completed_item[ 'user_data' ] )
359
360 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100361endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100362
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100363func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100364 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
365
366 set completefunc=<SID>CompleteDone_CompleteFuncList
367 execute "normal a\<C-X>\<C-U>\<C-Y>"
368 set completefunc&
369
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100370 call assert_equal('', v:completed_item[ 'user_data' ])
371 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100372
373 let s:called_completedone = 0
374 au! CompleteDone
375endfunc
376
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200377func Test_CompleteDone_undo()
378 au CompleteDone * call append(0, "prepend1")
379 new
380 call setline(1, ["line1", "line2"])
381 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
382 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
383 \ getline(1, '$'))
384 undo
385 call assert_equal(["line1", "line2"], getline(1, '$'))
386 bwipe!
387 au! CompleteDone
388endfunc
389
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200390func CompleteTest(findstart, query)
391 if a:findstart
392 return col('.')
393 endif
394 return ['matched']
395endfunc
396
397func Test_completefunc_info()
398 new
399 set completeopt=menuone
400 set completefunc=CompleteTest
401 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100402 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 +0200403 bwipe!
404 set completeopt&
405 set completefunc&
406endfunc
407
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100408" Check that when using feedkeys() typeahead does not interrupt searching for
409" completions.
410func Test_compl_feedkeys()
411 new
412 set completeopt=menuone,noselect
413 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
414 call assert_equal("jump jump", getline(1))
415 bwipe!
416 set completeopt&
417endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200418
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200419" Test for insert path completion with completeslash option
420func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200421 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200422
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200423 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200424 let orig_shellslash = &shellslash
425 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200426 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200427
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200428 set noshellslash
429
430 set completeslash=
431 exe "normal oXd\<C-X>\<C-F>"
432 call assert_equal('Xdir\', getline('.'))
433
434 set completeslash=backslash
435 exe "normal oXd\<C-X>\<C-F>"
436 call assert_equal('Xdir\', getline('.'))
437
438 set completeslash=slash
439 exe "normal oXd\<C-X>\<C-F>"
440 call assert_equal('Xdir/', getline('.'))
441
442 set shellslash
443
444 set completeslash=
445 exe "normal oXd\<C-X>\<C-F>"
446 call assert_equal('Xdir/', getline('.'))
447
448 set completeslash=backslash
449 exe "normal oXd\<C-X>\<C-F>"
450 call assert_equal('Xdir\', getline('.'))
451
452 set completeslash=slash
453 exe "normal oXd\<C-X>\<C-F>"
454 call assert_equal('Xdir/', getline('.'))
455 %bw!
456 call delete('Xdir', 'rf')
457
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200458 set noshellslash
459 set completeslash=slash
460 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
461
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200462 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200463 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200464endfunc
465
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200466func Test_pum_stopped_by_timer()
467 CheckScreendump
468
469 let lines =<< trim END
470 call setline(1, ['hello', 'hullo', 'heeee', ''])
471 func StartCompl()
472 call timer_start(100, { -> execute('stopinsert') })
473 call feedkeys("Gah\<C-N>")
474 endfunc
475 END
476
477 call writefile(lines, 'Xpumscript')
478 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
479 call term_sendkeys(buf, ":call StartCompl()\<CR>")
480 call TermWait(buf, 200)
481 call term_sendkeys(buf, "k")
482 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
483
484 call StopVimInTerminal(buf)
485 call delete('Xpumscript')
486endfunc
487
zeertzjqcd5dbad2022-05-04 17:51:50 +0100488func Test_complete_stopinsert_startinsert()
489 nnoremap <F2> <Cmd>startinsert<CR>
490 inoremap <F2> <Cmd>stopinsert<CR>
491 " This just checks if this causes an error
492 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
493 nunmap <F2>
494 iunmap <F2>
495endfunc
496
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100497func Test_pum_with_folds_two_tabs()
498 CheckScreendump
499
500 let lines =<< trim END
501 set fdm=marker
502 call setline(1, ['" x {{{1', '" a some text'])
503 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
504 norm! zm
505 tab sp
506 call feedkeys('2Gzv', 'xt')
507 call feedkeys("0fa", 'xt')
508 END
509
510 call writefile(lines, 'Xpumscript')
511 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200512 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100513 call term_sendkeys(buf, "a\<C-N>")
514 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
515
516 call term_sendkeys(buf, "\<Esc>")
517 call StopVimInTerminal(buf)
518 call delete('Xpumscript')
519endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100520
521func Test_pum_with_preview_win()
522 CheckScreendump
523
524 let lines =<< trim END
525 funct Omni_test(findstart, base)
526 if a:findstart
527 return col(".") - 1
528 endif
529 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
530 endfunc
531 set omnifunc=Omni_test
532 set completeopt+=longest
533 END
534
535 call writefile(lines, 'Xpreviewscript')
536 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200537 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100538 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200539 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100540 call term_sendkeys(buf, "\<C-N>")
541 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
542
543 call term_sendkeys(buf, "\<Esc>")
544 call StopVimInTerminal(buf)
545 call delete('Xpreviewscript')
546endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100547
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000548func Test_scrollbar_on_wide_char()
549 CheckScreendump
550
551 let lines =<< trim END
552 call setline(1, ['a', ' 啊啊啊',
553 \ ' 哦哦哦',
554 \ ' 呃呃呃'])
555 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
556 END
557 call writefile(lines, 'Xwidescript')
558 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
559 call term_sendkeys(buf, "A\<C-N>")
560 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
561
562 call StopVimInTerminal(buf)
563 call delete('Xwidescript')
564endfunc
565
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100566" Test for inserting the tag search pattern in insert mode
567func Test_ins_compl_tag_sft()
568 call writefile([
569 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
570 \ "first\tXfoo\t/^int first() {}$/",
571 \ "second\tXfoo\t/^int second() {}$/",
572 \ "third\tXfoo\t/^int third() {}$/"],
573 \ 'Xtags')
574 set tags=Xtags
575 let code =<< trim [CODE]
576 int first() {}
577 int second() {}
578 int third() {}
579 [CODE]
580 call writefile(code, 'Xfoo')
581
582 enew
583 set showfulltag
584 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
585 call assert_equal('int second() {}', getline(1))
586 set noshowfulltag
587
588 call delete('Xtags')
589 call delete('Xfoo')
590 set tags&
591 %bwipe!
592endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200593
594" Test for 'completefunc' deleting text
595func Test_completefunc_error()
596 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200597 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200598 func CompleteFunc(findstart, base)
599 if a:findstart == 1
600 normal dd
601 return col('.') - 1
602 endif
603 return ['a', 'b']
604 endfunc
605 set completefunc=CompleteFunc
606 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100607 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200608
609 " delete text when called for the second time
610 func CompleteFunc2(findstart, base)
611 if a:findstart == 1
612 return col('.') - 1
613 endif
614 normal dd
615 return ['a', 'b']
616 endfunc
617 set completefunc=CompleteFunc2
618 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100619 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200620
Bram Moolenaar97202d92021-01-28 18:34:35 +0100621 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100622 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100623 if a:findstart == 1
624 return col('.') - 1
625 endif
626 wincmd p
627 return ['a', 'b']
628 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100629 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100630 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100631 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100632 close!
633
634 set completefunc&
635 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100636 delfunc CompleteFunc2
637 delfunc CompleteFunc3
638 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100639endfunc
640
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200641" Test for returning non-string values from 'completefunc'
642func Test_completefunc_invalid_data()
643 new
644 func! CompleteFunc(findstart, base)
645 if a:findstart == 1
646 return col('.') - 1
647 endif
648 return [{}, '', 'moon']
649 endfunc
650 set completefunc=CompleteFunc
651 exe "normal i\<C-X>\<C-U>"
652 call assert_equal('moon', getline(1))
653 set completefunc&
654 close!
655endfunc
656
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200657" Test for errors in using complete() function
658func Test_complete_func_error()
659 call assert_fails('call complete(1, ["a"])', 'E785:')
660 func ListColors()
661 call complete(col('.'), "blue")
662 endfunc
663 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
664 func ListMonths()
665 call complete(col('.'), test_null_list())
666 endfunc
667 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
668 delfunc ListColors
669 delfunc ListMonths
670 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200671 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200672endfunc
673
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000674" Test for recursively starting completion mode using complete()
675func Test_recursive_complete_func()
676 func ListColors()
677 call complete(5, ["red", "blue"])
678 return ''
679 endfunc
680 new
681 call setline(1, ['a1', 'a2'])
682 set complete=.
683 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
684 call assert_equal('a2blue', getline(3))
685 delfunc ListColors
686 bw!
687endfunc
688
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200689" Test for completing words following a completed word in a line
690func Test_complete_wrapscan()
691 " complete words from another buffer
692 new
693 call setline(1, ['one two', 'three four'])
694 new
695 setlocal complete=w
696 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
697 call assert_equal('two three four', getline(1))
698 close!
699 " complete words from the current buffer
700 setlocal complete=.
701 %d
702 call setline(1, ['one two', ''])
703 call cursor(2, 1)
704 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
705 call assert_equal('one two one two', getline(2))
706 close!
707endfunc
708
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200709" Test for completing special characters
710func Test_complete_special_chars()
711 new
712 call setline(1, 'int .*[-\^$ func float')
713 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
714 call assert_equal('int .*[-\^$ func float', getline(2))
715 close!
716endfunc
717
718" Test for completion when text is wrapped across lines.
719func Test_complete_across_line()
720 new
721 call setline(1, ['red green blue', 'one two three'])
722 setlocal textwidth=20
723 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
724 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
725 close!
726endfunc
727
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000728" Test for completing words with a '.' at the end of a word.
729func Test_complete_joinspaces()
730 new
731 call setline(1, ['one two.', 'three. four'])
732 set joinspaces
733 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
734 call assert_equal("one two. three. four", getline(3))
735 set joinspaces&
736 bw!
737endfunc
738
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200739" Test for using CTRL-L to add one character when completing matching
740func Test_complete_add_onechar()
741 new
742 call setline(1, ['wool', 'woodwork'])
743 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
744 call assert_equal('woof', getline(3))
745
746 " use 'ignorecase' and backspace to erase characters from the prefix string
747 " and then add letters using CTRL-L
748 %d
749 set ignorecase backspace=2
750 setlocal complete=.
751 call setline(1, ['workhorse', 'workload'])
752 normal Go
753 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
754 call assert_equal('workh', getline(3))
755 set ignorecase& backspace&
756 close!
757endfunc
758
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000759" Test for using CTRL-X CTRL-L to complete whole lines lines
760func Test_complete_wholeline()
761 new
762 " complete one-line
763 call setline(1, ['a1', 'a2'])
764 exe "normal ggoa\<C-X>\<C-L>"
765 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
766 " go to the next match (wrapping around the buffer)
767 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
768 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
769 " go to the next match
770 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
771 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
772 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
773 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
774 " repeat the test using CTRL-L
775 " go to the next match (wrapping around the buffer)
776 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
777 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
778 " go to the next match
779 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
780 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
781 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
782 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
783 %d
784 " use CTRL-X CTRL-L to add one more line
785 call setline(1, ['a1', 'b1'])
786 setlocal complete=.
787 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
788 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
789 bw!
790endfunc
791
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200792" Test insert completion with 'cindent' (adjust the indent)
793func Test_complete_with_cindent()
794 new
795 setlocal cindent
796 call setline(1, ['if (i == 1)', " j = 2;"])
797 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
798 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
799
800 %d
801 call setline(1, ['when while', '{', ''])
802 setlocal cinkeys+==while
803 exe "normal Giwh\<C-P> "
804 call assert_equal("\twhile ", getline('$'))
805 close!
806endfunc
807
808" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
809func Test_complete_cmdline()
810 new
811 exe "normal icaddb\<C-X>\<C-V>"
812 call assert_equal('caddbuffer', getline(1))
813 exe "normal ocall getqf\<C-X>\<C-V>"
814 call assert_equal('call getqflist(', getline(2))
815 exe "normal oabcxyz(\<C-X>\<C-V>"
816 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200817 com! -buffer TestCommand1 echo 'TestCommand1'
818 com! -buffer TestCommand2 echo 'TestCommand2'
819 write TestCommand1Test
820 write TestCommand2Test
821 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
822 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
823 call assert_equal('TestCommand2Test', getline(4))
824 call delete('TestCommand1Test')
825 call delete('TestCommand2Test')
826 delcom TestCommand1
827 delcom TestCommand2
828 close!
829endfunc
830
831" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
832func Test_complete_stop()
833 new
834 func Save_mode1()
835 let g:mode1 = mode(1)
836 return ''
837 endfunc
838 func Save_mode2()
839 let g:mode2 = mode(1)
840 return ''
841 endfunc
842 inoremap <F1> <C-R>=Save_mode1()<CR>
843 inoremap <F2> <C-R>=Save_mode2()<CR>
844 call setline(1, ['aaa bbb ccc '])
845 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
846 call assert_equal('ic', g:mode1)
847 call assert_equal('i', g:mode2)
848 call assert_equal('aaa bbb ccc ', getline(1))
849 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
850 call assert_equal('ic', g:mode1)
851 call assert_equal('i', g:mode2)
852 call assert_equal('aaa bbb ccc aaa', getline(1))
853 set completeopt+=noselect
854 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
855 call assert_equal('ic', g:mode1)
856 call assert_equal('i', g:mode2)
857 call assert_equal('aaa bbb ccc aaa bb', getline(1))
858 set completeopt&
859 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
860 call assert_equal('ic', g:mode1)
861 call assert_equal('i', g:mode2)
862 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
863 com! -buffer TestCommand1 echo 'TestCommand1'
864 com! -buffer TestCommand2 echo 'TestCommand2'
865 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
866 call assert_equal('ic', g:mode1)
867 call assert_equal('i', g:mode2)
868 call assert_equal('TestCommand2', getline(2))
869 delcom TestCommand1
870 delcom TestCommand2
871 unlet g:mode1
872 unlet g:mode2
873 iunmap <F1>
874 iunmap <F2>
875 delfunc Save_mode1
876 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200877 close!
878endfunc
879
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000880" Test for typing CTRL-R in insert completion mode to insert a register
881" content.
882func Test_complete_reginsert()
883 new
884 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
885
886 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
887 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
888 " inserted.
889 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
890 call assert_equal('a123', getline(5))
891 let @r = "\<C-P>\<C-P>"
892 exe "normal GCa\<C-P>\<C-R>r"
893 call assert_equal('a12', getline(5))
894 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
895 call assert_equal('a1234x', getline(5))
896 bw!
897endfunc
898
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200899func Test_issue_7021()
900 CheckMSWindows
901
902 let orig_shellslash = &shellslash
903 set noshellslash
904
905 set completeslash=slash
906 call assert_false(expand('~') =~ '/')
907
908 let &shellslash = orig_shellslash
909 set completeslash=
910endfunc
911
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000912" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
913func Test_complete_longest_match()
914 for e in ['latin1', 'utf-8']
915 exe 'set encoding=' .. e
916 new
917 set complete=.
918 set completeopt=menu,longest
919 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
920 exe "normal Gopfx\<C-P>"
921 call assert_equal('pfx_', getline(5))
922 bw!
923 endfor
924
925 " Test for completing additional words with longest match set
926 new
927 call setline(1, ['abc1', 'abd2'])
928 exe "normal Goab\<C-P>\<C-X>\<C-P>"
929 call assert_equal('ab', getline(3))
930 bw!
931 set complete& completeopt&
932endfunc
933
934" Test for removing the first displayed completion match and selecting the
935" match just before that.
936func Test_complete_erase_firstmatch()
937 new
938 call setline(1, ['a12', 'a34', 'a56'])
939 set complete=.
940 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
941 call assert_equal('a34', getline('$'))
942 set complete&
943 bw!
944endfunc
945
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000946" Test for completing words from unloaded buffers
947func Test_complete_from_unloadedbuf()
948 call writefile(['abc'], "Xfile1")
949 call writefile(['def'], "Xfile2")
950 edit Xfile1
951 edit Xfile2
952 new | close
953 enew
954 bunload Xfile1 Xfile2
955 set complete=u
956 " complete from an unloaded buffer
957 exe "normal! ia\<C-P>"
958 call assert_equal('abc', getline(1))
959 exe "normal! od\<C-P>"
960 call assert_equal('def', getline(2))
961 set complete&
962 %bw!
963 call delete("Xfile1")
964 call delete("Xfile2")
965endfunc
966
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000967" Test for completing whole lines from unloaded buffers
968func Test_complete_wholeline_unloadedbuf()
969 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
970 edit Xfile1
971 enew
972 set complete=u
973 exe "normal! ia\<C-X>\<C-L>\<C-P>"
974 call assert_equal('a line2', getline(1))
975 %d
976 " completing from an unlisted buffer should fail
977 bdel Xfile1
978 exe "normal! ia\<C-X>\<C-L>\<C-P>"
979 call assert_equal('a', getline(1))
980 set complete&
981 %bw!
982 call delete("Xfile1")
983endfunc
984
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000985" Test for completing words from unlisted buffers
986func Test_complete_from_unlistedbuf()
987 call writefile(['abc'], "Xfile1")
988 call writefile(['def'], "Xfile2")
989 edit Xfile1
990 edit Xfile2
991 new | close
992 bdel Xfile1 Xfile2
993 set complete=U
994 " complete from an unlisted buffer
995 exe "normal! ia\<C-P>"
996 call assert_equal('abc', getline(1))
997 exe "normal! od\<C-P>"
998 call assert_equal('def', getline(2))
999 set complete&
1000 %bw!
1001 call delete("Xfile1")
1002 call delete("Xfile2")
1003endfunc
1004
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001005" Test for completing whole lines from unlisted buffers
1006func Test_complete_wholeline_unlistedbuf()
1007 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1008 edit Xfile1
1009 enew
1010 set complete=U
1011 " completing from a unloaded buffer should fail
1012 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1013 call assert_equal('a', getline(1))
1014 %d
1015 bdel Xfile1
1016 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1017 call assert_equal('a line2', getline(1))
1018 set complete&
1019 %bw!
1020 call delete("Xfile1")
1021endfunc
1022
1023" Test for adding a multibyte character using CTRL-L in completion mode
1024func Test_complete_mbyte_char_add()
1025 new
1026 set complete=.
1027 call setline(1, 'abė')
1028 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1029 call assert_equal('abė', getline(2))
1030 " Test for a leader with multibyte character
1031 %d
1032 call setline(1, 'abėĕ')
1033 exe "normal! oabė\<C-P>"
1034 call assert_equal('abėĕ', getline(2))
1035 bw!
1036endfunc
1037
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001038" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1039" not to complete matches from the local buffer. Also test using multiple
1040" <C-X> to cancel the current completion mode.
1041func Test_complete_local_expansion()
1042 new
1043 set complete=t
1044 call setline(1, ['abc', 'def'])
1045 exe "normal! Go\<C-X>\<C-P>"
1046 call assert_equal("def", getline(3))
1047 exe "normal! Go\<C-P>"
1048 call assert_equal("", getline(4))
1049 exe "normal! Go\<C-X>\<C-N>"
1050 call assert_equal("abc", getline(5))
1051 exe "normal! Go\<C-N>"
1052 call assert_equal("", getline(6))
1053
1054 " use multiple <C-X> to cancel the previous completion mode
1055 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1056 call assert_equal("", getline(7))
1057 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1058 call assert_equal("", getline(8))
1059 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1060 call assert_equal("abc", getline(9))
1061
1062 " interrupt the current completion mode
1063 set completeopt=menu,noinsert
1064 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1065 call assert_equal("abc", getline(10))
1066
1067 " when only one <C-X> is used to interrupt, do normal expansion
1068 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1069 call assert_equal("", getline(11))
1070 set completeopt&
1071
1072 " using two <C-X> in non-completion mode and restarting the same mode
1073 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1074 call assert_equal("def", getline(12))
1075
1076 " test for adding a match from the original empty text
1077 %d
1078 call setline(1, 'abc def g')
1079 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1080 call assert_equal('def', getline(2))
1081 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1082 call assert_equal('abc', getline(2))
1083
1084 bw!
1085endfunc
1086
1087" Test for undoing changes after a insert-mode completion
1088func Test_complete_undo()
1089 new
1090 set complete=.
1091 " undo with 'ignorecase'
1092 call setline(1, ['ABOVE', 'BELOW'])
1093 set ignorecase
1094 exe "normal! Goab\<C-G>u\<C-P>"
1095 call assert_equal("ABOVE", getline(3))
1096 undo
1097 call assert_equal("ab", getline(3))
1098 set ignorecase&
1099 %d
1100 " undo with longest match
1101 set completeopt=menu,longest
1102 call setline(1, ['above', 'about'])
1103 exe "normal! Goa\<C-G>u\<C-P>"
1104 call assert_equal("abo", getline(3))
1105 undo
1106 call assert_equal("a", getline(3))
1107 set completeopt&
1108 %d
1109 " undo for line completion
1110 call setline(1, ['above that change', 'below that change'])
1111 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1112 call assert_equal("above that change", getline(3))
1113 undo
1114 call assert_equal("above", getline(3))
1115
1116 bw!
1117endfunc
1118
1119" Test for completing a very long word
1120func Test_complete_long_word()
1121 set complete&
1122 new
1123 call setline(1, repeat('x', 950) .. ' one two three')
1124 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1125 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1126 %d
1127 " should fail when more than 950 characters are in a word
1128 call setline(1, repeat('x', 951) .. ' one two three')
1129 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1130 call assert_equal(repeat('x', 951), getline(2))
1131
1132 " Test for adding a very long word to an existing completion
1133 %d
1134 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1135 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1136 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1137 bw!
1138endfunc
1139
1140" Test for some fields in the complete items used by complete()
1141func Test_complete_items()
1142 func CompleteItems(idx)
1143 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1144 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1145 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1146 \ [#{user_data: 'u9'}],
1147 \ [#{word: "", user_data: 'u10'}],
1148 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1149 call complete(col('.'), items[a:idx])
1150 return ''
1151 endfunc
1152 new
1153 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1154 call assert_equal('u2', v:completed_item.user_data)
1155 call assert_equal('one', getline(1))
1156 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1157 call assert_equal('u3', v:completed_item.user_data)
1158 call assert_equal('one', getline(2))
1159 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1160 call assert_equal('', getline(3))
1161 set completeopt=menu,noinsert
1162 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1163 call assert_equal('oNE', getline(4))
1164 call assert_equal('u8', v:completed_item.user_data)
1165 set completeopt&
1166 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1167 call assert_equal('', getline(5))
1168 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1169 call assert_equal('', getline(6))
1170 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1171 call assert_equal('', getline(7))
1172 call assert_equal('u11', v:completed_item.user_data)
1173 " pass invalid argument to complete()
1174 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1175 call assert_fails('exe cmd', 'E730:')
1176 bw!
1177 delfunc CompleteItems
1178endfunc
1179
1180" Test for the "refresh" item in the dict returned by an insert completion
1181" function
1182func Test_complete_item_refresh_always()
1183 let g:CallCount = 0
1184 func! Tcomplete(findstart, base)
1185 if a:findstart
1186 " locate the start of the word
1187 let line = getline('.')
1188 let start = col('.') - 1
1189 while start > 0 && line[start - 1] =~ '\a'
1190 let start -= 1
1191 endwhile
1192 return start
1193 else
1194 let g:CallCount += 1
1195 let res = ["update1", "update12", "update123"]
1196 return #{words: res, refresh: 'always'}
1197 endif
1198 endfunc
1199 new
1200 set completeopt=menu,longest
1201 set completefunc=Tcomplete
1202 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1203 call assert_equal('up', getline(1))
1204 call assert_equal(2, g:CallCount)
1205 set completeopt&
1206 set completefunc&
1207 bw!
1208 delfunc Tcomplete
1209endfunc
1210
1211" Test for completing from a thesaurus file without read permission
1212func Test_complete_unreadable_thesaurus_file()
1213 CheckUnix
1214 CheckNotRoot
1215
1216 call writefile(['about', 'above'], 'Xfile')
1217 call setfperm('Xfile', '---r--r--')
1218 new
1219 set complete=sXfile
1220 exe "normal! ia\<C-P>"
1221 call assert_equal('a', getline(1))
1222 bw!
1223 call delete('Xfile')
1224 set complete&
1225endfunc
1226
Bram Moolenaarcc233582020-12-12 13:32:07 +01001227" Test to ensure 'Scanning...' messages are not recorded in messages history
1228func Test_z1_complete_no_history()
1229 new
1230 messages clear
1231 let currmess = execute('messages')
1232 setlocal dictionary=README.txt
1233 exe "normal owh\<C-X>\<C-K>"
1234 exe "normal owh\<C-N>"
1235 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001236 bwipe!
1237endfunc
1238
1239" A mapping is not used for the key after CTRL-X.
1240func Test_no_mapping_for_ctrl_x_key()
1241 new
1242 inoremap <C-K> <Cmd>let was_mapped = 'yes'<CR>
1243 setlocal dictionary=README.txt
1244 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1245 call assert_equal('example ', getline(1))
1246 call assert_false(exists('was_mapped'))
1247 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001248endfunc
1249
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001250" Test for different ways of setting the 'completefunc' option
1251func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001252 func CompleteFunc1(callnr, findstart, base)
1253 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1254 return a:findstart ? 0 : []
1255 endfunc
1256 func CompleteFunc2(findstart, base)
1257 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001258 return a:findstart ? 0 : []
1259 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001260
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001261 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001262 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001263 LET &completefunc = 'g:CompleteFunc2'
1264 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001265 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001266 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001267 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001268 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001269 bw!
1270
1271 #" Test for using a function()
1272 set completefunc=function('g:CompleteFunc1',\ [10])
1273 new
1274 call setline(1, 'one')
1275 LET g:CompleteFunc1Args = []
1276 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1277 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001278 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001279
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001280 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001281 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001282 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001283 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001284 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001285 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001286 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001287 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001288 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001289
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001290 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001291 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001292 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001293 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001294 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001295 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001296 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001297 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001298 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001299
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001300 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001301 set completefunc=funcref('g:CompleteFunc1',\ [13])
1302 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001303 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001304 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001305 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001306 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001307 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001308
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001309 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001310 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001311 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001312 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001313 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001314 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001315 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001316 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001317 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001318
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001319 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001320 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001321 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001322 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001323 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001324 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001325 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001326 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001327 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001328
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001329 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001330 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001331 LET optval = substitute(optval, ' ', '\\ ', 'g')
1332 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001333 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001334 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001337 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001338 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001339
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001340 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001341 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001342 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001343 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001344 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001345 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001346 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001347 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001348
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001349 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001350 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001353 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001354 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001355 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001356 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001357
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001358 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001359 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001360 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001361 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001362 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001363 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001364 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001365 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001366 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001367
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001368 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001369 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001370 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001371 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001372 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001373 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001374 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001375 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001376 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001377
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001378 #" Test for using a lambda function with incorrect return value
1379 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1380 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001381 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001382 call setline(1, 'eight')
1383 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1384 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001385
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001386 #" Test for clearing the 'completefunc' option
1387 set completefunc=''
1388 set completefunc&
1389 call assert_fails("set completefunc=function('abc')", "E700:")
1390 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001391
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001392 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001393 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001394 call setline(1, 'five')
1395 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1396 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001397 LET g:CompleteFunc2Args = []
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([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001400 bw!
1401 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001402 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001403
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001404 " Test for using a script-local function name
1405 func s:CompleteFunc3(findstart, base)
1406 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1407 return a:findstart ? 0 : []
1408 endfunc
1409 set completefunc=s:CompleteFunc3
1410 new
1411 call setline(1, 'script1')
1412 let g:CompleteFunc3Args = []
1413 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1414 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1415 bw!
1416
1417 let &completefunc = 's:CompleteFunc3'
1418 new
1419 call setline(1, 'script2')
1420 let g:CompleteFunc3Args = []
1421 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1422 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1423 bw!
1424 delfunc s:CompleteFunc3
1425
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001426 " In Vim9 script s: can be omitted
1427 let lines =<< trim END
1428 vim9script
1429 var CompleteFunc4Args = []
1430 def CompleteFunc4(findstart: bool, base: string): any
1431 add(CompleteFunc4Args, [findstart, base])
1432 return findstart ? 0 : []
1433 enddef
1434 set completefunc=CompleteFunc4
1435 new
1436 setline(1, 'script1')
1437 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1438 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1439 bw!
1440 END
1441 call v9.CheckScriptSuccess(lines)
1442
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001443 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001444 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001445 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1446
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001447 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001448 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001449 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001450 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001451 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001452 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001453
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001454 " set 'completefunc' to a partial with dict. This used to cause a crash.
1455 func SetCompleteFunc()
1456 let params = {'complete': function('g:DictCompleteFunc')}
1457 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001458 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001459 func g:DictCompleteFunc(_) dict
1460 endfunc
1461 call SetCompleteFunc()
1462 new
1463 call SetCompleteFunc()
1464 bw
1465 call test_garbagecollect_now()
1466 new
1467 set completefunc=
1468 wincmd w
1469 set completefunc=
1470 %bw!
1471 delfunc g:DictCompleteFunc
1472 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001473
1474 " Vim9 tests
1475 let lines =<< trim END
1476 vim9script
1477
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001478 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1479 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001480 return findstart ? 0 : []
1481 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001482
1483 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001484 set completefunc=function('Vim9CompleteFunc',\ [60])
1485 new | only
1486 setline(1, 'one')
1487 g:Vim9completeFuncArgs = []
1488 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1489 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1490 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001491
1492 # Test for using a global function name
1493 &completefunc = g:CompleteFunc2
1494 new | only
1495 setline(1, 'two')
1496 g:CompleteFunc2Args = []
1497 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1498 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1499 bw!
1500
1501 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001502 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001503 add(g:LocalCompleteFuncArgs, [findstart, base])
1504 return findstart ? 0 : []
1505 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001506 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001507 new | only
1508 setline(1, 'three')
1509 g:LocalCompleteFuncArgs = []
1510 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1511 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1512 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001513 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001514 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001515
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001516 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001517 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001518 delfunc CompleteFunc1
1519 delfunc CompleteFunc2
1520 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001521 %bw!
1522endfunc
1523
1524" Test for different ways of setting the 'omnifunc' option
1525func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001526 func OmniFunc1(callnr, findstart, base)
1527 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1528 return a:findstart ? 0 : []
1529 endfunc
1530 func OmniFunc2(findstart, base)
1531 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001532 return a:findstart ? 0 : []
1533 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001534
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001535 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001536 #" Test for using a function name
1537 LET &omnifunc = 'g:OmniFunc2'
1538 new
1539 call setline(1, 'zero')
1540 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001541 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001542 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1543 bw!
1544
1545 #" Test for using a function()
1546 set omnifunc=function('g:OmniFunc1',\ [10])
1547 new
1548 call setline(1, 'one')
1549 LET g:OmniFunc1Args = []
1550 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1551 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001552 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001553
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001554 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001555 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001556 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001557 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001558 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001559 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001560 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001561 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001562 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001563
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001564 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001565 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001566 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001567 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001568 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001569 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001570 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001571 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001572 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001573
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001574 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001575 set omnifunc=funcref('g:OmniFunc1',\ [13])
1576 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001577 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001578 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001579 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001580 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001581 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001582
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001583 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001584 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001585 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001586 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001587 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001588 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001589 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001590 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001591 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001592
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001594 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001595 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001596 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001598 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001599 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001600 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001602
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001603 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001604 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 LET optval = substitute(optval, ' ', '\\ ', 'g')
1606 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001607 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001608 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001609 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001610 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001611 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001612 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001613
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001614 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001615 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001616 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001617 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001618 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001619 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001620 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001621 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001622
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001623 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001624 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001629 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001631
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001632 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001633 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001635 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001637 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001639 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001641
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001643 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001645 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001646 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001647 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001648 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001649 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001650 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001651
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001652 #" Test for using a lambda function with incorrect return value
1653 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1654 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001655 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001656 call setline(1, 'eight')
1657 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1658 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001659
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 #" Test for clearing the 'omnifunc' option
1661 set omnifunc=''
1662 set omnifunc&
1663 call assert_fails("set omnifunc=function('abc')", "E700:")
1664 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001665
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001666 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001667 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001668 call setline(1, 'nine')
1669 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1670 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001671 LET g:OmniFunc2Args = []
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([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001674 bw!
1675 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001676 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001677
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001678 " Test for using a script-local function name
1679 func s:OmniFunc3(findstart, base)
1680 call add(g:OmniFunc3Args, [a:findstart, a:base])
1681 return a:findstart ? 0 : []
1682 endfunc
1683 set omnifunc=s:OmniFunc3
1684 new
1685 call setline(1, 'script1')
1686 let g:OmniFunc3Args = []
1687 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1688 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1689 bw!
1690
1691 let &omnifunc = 's:OmniFunc3'
1692 new
1693 call setline(1, 'script2')
1694 let g:OmniFunc3Args = []
1695 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1696 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1697 bw!
1698 delfunc s:OmniFunc3
1699
1700 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001701 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001702 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1703
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001704 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001706 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001707 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001708 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001709 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001710
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001711 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1712 func SetOmniFunc()
1713 let params = {'omni': function('g:DictOmniFunc')}
1714 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001715 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001716 func g:DictOmniFunc(_) dict
1717 endfunc
1718 call SetOmniFunc()
1719 new
1720 call SetOmniFunc()
1721 bw
1722 call test_garbagecollect_now()
1723 new
1724 set omnifunc=
1725 wincmd w
1726 set omnifunc=
1727 %bw!
1728 delfunc g:DictOmniFunc
1729 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001730
1731 " Vim9 tests
1732 let lines =<< trim END
1733 vim9script
1734
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001735 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1736 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001737 return findstart ? 0 : []
1738 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001739
1740 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001741 set omnifunc=function('Vim9omniFunc',\ [60])
1742 new | only
1743 setline(1, 'one')
1744 g:Vim9omniFunc_Args = []
1745 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1746 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1747 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001748
1749 # Test for using a global function name
1750 &omnifunc = g:OmniFunc2
1751 new | only
1752 setline(1, 'two')
1753 g:OmniFunc2Args = []
1754 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1755 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1756 bw!
1757
1758 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001759 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001760 add(g:LocalOmniFuncArgs, [findstart, base])
1761 return findstart ? 0 : []
1762 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001763 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001764 new | only
1765 setline(1, 'three')
1766 g:LocalOmniFuncArgs = []
1767 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1768 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1769 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001770 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001771 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001772
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001773 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001774 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001775 delfunc OmniFunc1
1776 delfunc OmniFunc2
1777 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001778 %bw!
1779endfunc
1780
1781" Test for different ways of setting the 'thesaurusfunc' option
1782func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001783 func TsrFunc1(callnr, findstart, base)
1784 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001785 return a:findstart ? 0 : []
1786 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001787 func TsrFunc2(findstart, base)
1788 call add(g:TsrFunc2Args, [a:findstart, a:base])
1789 return a:findstart ? 0 : ['sunday']
1790 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001791
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001792 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001793 #" Test for using a function name
1794 LET &thesaurusfunc = 'g:TsrFunc2'
1795 new
1796 call setline(1, 'zero')
1797 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001798 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001799 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1800 bw!
1801
1802 #" Test for using a function()
1803 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1804 new
1805 call setline(1, 'one')
1806 LET g:TsrFunc1Args = []
1807 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1808 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001809 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001810
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001811 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001812 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001813 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001814 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001815 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001816 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001817 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001818 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001819 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001820
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001821 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001822 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001823 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001824 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001825 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001826 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001827 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001828 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001829 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001830
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001831 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001832 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1833 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001834 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001835 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001836 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001837 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001838 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001839
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001840 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001841 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001842 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001843 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001844 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001845 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001846 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001847 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001848 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001849
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001850 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001851 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001852 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001853 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001854 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001855 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001856 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001857 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001858 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001859
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001860 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001861 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001862 LET optval = substitute(optval, ' ', '\\ ', 'g')
1863 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001864 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001865 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001866 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001868 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001869 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001870
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001871 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001872 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001873 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001874 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001875 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001876 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001877 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001878 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001879
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001880 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001881 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001882 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001883 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001884 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001885 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001886 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001887 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001888
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001889 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001890 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001891 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001892 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001893 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001894 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001895 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001896 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001897 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001898
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001899 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001900 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001901 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001902 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001903 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001904 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001905 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001906 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001907 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001908
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001909 #" Test for using a lambda function with incorrect return value
1910 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1911 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001912 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001913 call setline(1, 'eight')
1914 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1915 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001916
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001917 #" Test for clearing the 'thesaurusfunc' option
1918 set thesaurusfunc=''
1919 set thesaurusfunc&
1920 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1921 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001922
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001924 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001925 call setline(1, 'ten')
1926 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1927 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001928 LET g:TsrFunc2Args = []
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([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001931 bw!
1932
1933 #" Use a buffer-local value and a global value
1934 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001935 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001936 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001937 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001938 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1939 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001940 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 new
1942 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001943 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1945 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001946 call assert_equal([], g:TsrFunc1Args)
1947 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001948 wincmd w
1949 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001950 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1952 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001953 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001954 :%bw!
1955 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001956 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001958 " Test for using a script-local function name
1959 func s:TsrFunc3(findstart, base)
1960 call add(g:TsrFunc3Args, [a:findstart, a:base])
1961 return a:findstart ? 0 : []
1962 endfunc
1963 set tsrfu=s:TsrFunc3
1964 new
1965 call setline(1, 'script1')
1966 let g:TsrFunc3Args = []
1967 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1968 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1969 bw!
1970
1971 let &tsrfu = 's:TsrFunc3'
1972 new
1973 call setline(1, 'script2')
1974 let g:TsrFunc3Args = []
1975 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1976 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1977 bw!
1978 delfunc s:TsrFunc3
1979
1980 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001981 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001982 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1983
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001984 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001985 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001986 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001987 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001988 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001989 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001990 bw!
1991
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001992 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
1993 func SetTsrFunc()
1994 let params = {'thesaurus': function('g:DictTsrFunc')}
1995 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001996 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001997 func g:DictTsrFunc(_) dict
1998 endfunc
1999 call SetTsrFunc()
2000 new
2001 call SetTsrFunc()
2002 bw
2003 call test_garbagecollect_now()
2004 new
2005 set thesaurusfunc=
2006 wincmd w
2007 %bw!
2008 delfunc SetTsrFunc
2009
2010 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2011 " cause a crash.
2012 func SetLocalTsrFunc()
2013 let params = {'thesaurus': function('g:DictTsrFunc')}
2014 let &l:thesaurusfunc = params.thesaurus
2015 endfunc
2016 call SetLocalTsrFunc()
2017 call test_garbagecollect_now()
2018 call SetLocalTsrFunc()
2019 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002020 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002021 delfunc g:DictTsrFunc
2022 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002023
2024 " Vim9 tests
2025 let lines =<< trim END
2026 vim9script
2027
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002028 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2029 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002030 return findstart ? 0 : []
2031 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002032
2033 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002034 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2035 new | only
2036 setline(1, 'one')
2037 g:Vim9tsrFunc_Args = []
2038 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2039 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2040 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002041
2042 # Test for using a global function name
2043 &thesaurusfunc = g:TsrFunc2
2044 new | only
2045 setline(1, 'two')
2046 g:TsrFunc2Args = []
2047 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2048 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2049 bw!
2050
2051 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002052 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002053 add(g:LocalTsrFuncArgs, [findstart, base])
2054 return findstart ? 0 : []
2055 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002056 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002057 new | only
2058 setline(1, 'three')
2059 g:LocalTsrFuncArgs = []
2060 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2061 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2062 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002063 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002064 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002065
2066 " cleanup
2067 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002068 delfunc TsrFunc1
2069 delfunc TsrFunc2
2070 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002071 %bw!
2072endfunc
2073
Christian Brabandtac72c212022-04-07 21:00:53 +01002074func FooBarComplete(findstart, base)
2075 if a:findstart
2076 return col('.') - 1
2077 else
2078 return ["Foo", "Bar", "}"]
2079 endif
2080endfunc
2081
2082func Test_complete_smartindent()
2083 new
2084 setlocal smartindent completefunc=FooBarComplete
2085
2086 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2087 let result = getline(1,'$')
2088 call assert_equal(['', '{','}',''], result)
2089 bw!
2090 delfunction! FooBarComplete
2091endfunc
2092
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002093func Test_complete_overrun()
2094 " this was going past the end of the copied text
2095 new
2096 sil norm si”0s0 
2097 bwipe!
2098endfunc
2099
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002100func Test_infercase_very_long_line()
2101 " this was truncating the line when inferring case
2102 new
2103 let longLine = "blah "->repeat(300)
2104 let verylongLine = "blah "->repeat(400)
2105 call setline(1, verylongLine)
2106 call setline(2, longLine)
2107 set ic infercase
2108 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2109 call assert_equal(longLine, getline(3))
2110
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002111 " check that the too long text is NUL terminated
2112 %del
2113 norm o
2114 norm 1987ax
2115 exec "norm ox\<C-X>\<C-L>"
2116 call assert_equal(repeat('x', 1987), getline(3))
2117
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002118 bwipe!
2119 set noic noinfercase
2120endfunc
2121
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002122func Test_ins_complete_add()
2123 " this was reading past the end of allocated memory
2124 new
2125 norm o
2126 norm 7o€€
2127 sil! norm o
2128
2129 bwipe!
2130endfunc
2131
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002132
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002133" vim: shiftwidth=2 sts=2 expandtab