blob: 1452e4d31ba3f9bbed1e8e41d18b94bd8cb66df6 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010012 call mkdir('Xcpldir')
13 cd Xcpldir
Bram Moolenaarfb094e12017-11-05 20:59:28 +010014
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010018 \ "Makefile\tto\trun"], 'Xtestfile', 'D')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020019 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010025 \ 'end of testfile'], 'Xtestdata', 'D')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020026 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!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200102 call delete('Xtest11.one')
103 call delete('Xtest11.two')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200104 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100105 cd ..
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100106 call delete('Xcpldir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200107endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100108
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000109func Test_ins_complete_invalid_byte()
110 if has('unix') && executable('base64')
111 " this weird command was causing an illegal memory access
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100112 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64', 'D')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000113 call system('base64 -d Xinvalid64 > Xinvalid')
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100114 call writefile(['qa!'], 'Xexit', 'D')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000115 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000116 call delete('Xinvalid')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000117 endif
118endfunc
119
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100120func Test_omni_dash()
121 func Omni(findstart, base)
122 if a:findstart
123 return 5
124 else
125 echom a:base
126 return ['-help', '-v']
127 endif
128 endfunc
129 set omnifunc=Omni
130 new
131 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100132 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100133
134 bwipe!
135 delfunc Omni
136 set omnifunc=
137endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100138
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100139func Test_omni_throw()
140 let g:CallCount = 0
141 func Omni(findstart, base)
142 let g:CallCount += 1
143 if a:findstart
144 throw "he he he"
145 endif
146 endfunc
147 set omnifunc=Omni
148 new
149 try
150 exe "normal ifoo\<C-x>\<C-o>"
151 call assert_false(v:true, 'command should have failed')
152 catch
153 call assert_exception('he he he')
154 call assert_equal(1, g:CallCount)
155 endtry
156
157 bwipe!
158 delfunc Omni
159 unlet g:CallCount
160 set omnifunc=
161endfunc
162
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200163func Test_omni_autoload()
164 let save_rtp = &rtp
165 set rtp=Xruntime/some
166 let dir = 'Xruntime/some/autoload'
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100167 call mkdir(dir, 'pR')
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200168
169 let lines =<< trim END
170 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000171 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200172 if findstart
173 return 1
174 else
175 return ['match']
176 endif
177 enddef
178 {
179 eval 1 + 2
180 }
181 END
182 call writefile(lines, dir .. '/omni.vim')
183
184 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000185 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200186 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
187
188 bwipe!
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200189 set omnifunc=
190 let &rtp = save_rtp
191endfunc
192
Bram Moolenaarffa96842018-06-12 22:05:14 +0200193func Test_completefunc_args()
194 let s:args = []
195 func! CompleteFunc(findstart, base)
196 let s:args += [[a:findstart, empty(a:base)]]
197 endfunc
198 new
199
200 set completefunc=CompleteFunc
201 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200202 call assert_equal([1, 1], s:args[0])
203 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200204 set completefunc=
205
206 let s:args = []
207 set omnifunc=CompleteFunc
208 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200209 call assert_equal([1, 1], s:args[0])
210 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200211 set omnifunc=
212
213 bwipe!
214 unlet s:args
215 delfunc CompleteFunc
216endfunc
217
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100218func s:CompleteDone_CompleteFuncNone( findstart, base )
219 if a:findstart
220 return 0
221 endif
222
223 return v:none
224endfunc
225
Bram Moolenaar1e115362019-01-09 23:01:02 +0100226func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100227 if a:findstart
228 return 0
229 endif
230
231 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100232 \ 'words': [
233 \ {
234 \ 'word': 'aword',
235 \ 'abbr': 'wrd',
236 \ 'menu': 'extra text',
237 \ 'info': 'words are cool',
238 \ 'kind': 'W',
zeertzjq4cd45f12022-12-15 13:48:30 +0000239 \ 'user_data': ['one', 'two']
Bram Moolenaar08928322020-01-04 14:32:48 +0100240 \ }
241 \ ]
242 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100243endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100244
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100245func s:CompleteDone_CheckCompletedItemNone()
246 let s:called_completedone = 1
247endfunc
248
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100249func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100250 call assert_equal( 'aword', v:completed_item[ 'word' ] )
251 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
252 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
253 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
254 call assert_equal( 'W', v:completed_item[ 'kind' ] )
zeertzjq4cd45f12022-12-15 13:48:30 +0000255 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100256
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100257 if a:pre
258 call assert_equal('function', complete_info().mode)
259 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100260
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100261 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100262endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100263
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100264func Test_CompleteDoneNone()
265 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200266 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100267
268 set completefunc=<SID>CompleteDone_CompleteFuncNone
269 execute "normal a\<C-X>\<C-U>\<C-Y>"
270 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200274 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100275
276 let s:called_completedone = 0
277 au! CompleteDone
278endfunc
279
280func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100281 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
282 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100283
284 set completefunc=<SID>CompleteDone_CompleteFuncDict
285 execute "normal a\<C-X>\<C-U>\<C-Y>"
286 set completefunc&
287
zeertzjq4cd45f12022-12-15 13:48:30 +0000288 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100289 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100290
291 let s:called_completedone = 0
292 au! CompleteDone
293endfunc
294
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100295func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100296 if a:findstart
297 return 0
298 endif
299
300 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100301 \ 'words': [
302 \ {
303 \ 'word': 'aword',
304 \ 'abbr': 'wrd',
305 \ 'menu': 'extra text',
306 \ 'info': 'words are cool',
307 \ 'kind': 'W',
Bram Moolenaar08928322020-01-04 14:32:48 +0100308 \ }
309 \ ]
310 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100311endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100312
Bram Moolenaar1e115362019-01-09 23:01:02 +0100313func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100314 call assert_equal( 'aword', v:completed_item[ 'word' ] )
315 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
316 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
317 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
318 call assert_equal( 'W', v:completed_item[ 'kind' ] )
zeertzjq4cd45f12022-12-15 13:48:30 +0000319 call assert_equal( '', v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320
321 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100322endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100323
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100324func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100325 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
326
327 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
328 execute "normal a\<C-X>\<C-U>\<C-Y>"
329 set completefunc&
330
zeertzjq4cd45f12022-12-15 13:48:30 +0000331 call assert_equal('', v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100332 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100333
334 let s:called_completedone = 0
335 au! CompleteDone
336endfunc
337
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100338func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100339 if a:findstart
340 return 0
341 endif
342
343 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100344endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100345
Bram Moolenaar1e115362019-01-09 23:01:02 +0100346func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100347 call assert_equal( 'aword', v:completed_item[ 'word' ] )
348 call assert_equal( '', v:completed_item[ 'abbr' ] )
349 call assert_equal( '', v:completed_item[ 'menu' ] )
350 call assert_equal( '', v:completed_item[ 'info' ] )
351 call assert_equal( '', v:completed_item[ 'kind' ] )
352 call assert_equal( '', v:completed_item[ 'user_data' ] )
353
354 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100355endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100356
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100357func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100358 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
359
360 set completefunc=<SID>CompleteDone_CompleteFuncList
361 execute "normal a\<C-X>\<C-U>\<C-Y>"
362 set completefunc&
363
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100364 call assert_equal('', v:completed_item[ 'user_data' ])
365 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100366
367 let s:called_completedone = 0
368 au! CompleteDone
369endfunc
370
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200371func Test_CompleteDone_undo()
372 au CompleteDone * call append(0, "prepend1")
373 new
374 call setline(1, ["line1", "line2"])
375 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
376 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
377 \ getline(1, '$'))
378 undo
379 call assert_equal(["line1", "line2"], getline(1, '$'))
380 bwipe!
381 au! CompleteDone
382endfunc
383
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100384func Test_CompleteDone_modify()
385 let value = {
386 \ 'word': '',
387 \ 'abbr': '',
388 \ 'menu': '',
389 \ 'info': '',
390 \ 'kind': '',
391 \ 'user_data': '',
392 \ }
393 let v:completed_item = value
zeertzjq75020942022-07-31 11:37:20 +0100394 call assert_equal(value, v:completed_item)
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100395endfunc
396
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200397func CompleteTest(findstart, query)
398 if a:findstart
399 return col('.')
400 endif
401 return ['matched']
402endfunc
403
404func Test_completefunc_info()
405 new
406 set completeopt=menuone
407 set completefunc=CompleteTest
408 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100409 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 +0200410 bwipe!
411 set completeopt&
412 set completefunc&
413endfunc
414
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100415" Check that when using feedkeys() typeahead does not interrupt searching for
416" completions.
417func Test_compl_feedkeys()
418 new
419 set completeopt=menuone,noselect
420 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
421 call assert_equal("jump jump", getline(1))
422 bwipe!
423 set completeopt&
424endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200425
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200426" Test for insert path completion with completeslash option
427func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200428 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200429
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100430 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200431 let orig_shellslash = &shellslash
432 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200433 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200434
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200435 set noshellslash
436
437 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100438 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100439 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200440
441 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100442 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100443 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200444
445 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100446 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100447 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200448
449 set shellslash
450
451 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100452 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100453 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200454
455 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100456 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100457 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200458
459 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100460 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100461 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200462 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200463
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200464 set noshellslash
465 set completeslash=slash
466 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
467
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200468 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200469 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200470endfunc
471
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200472func Test_pum_stopped_by_timer()
473 CheckScreendump
474
475 let lines =<< trim END
476 call setline(1, ['hello', 'hullo', 'heeee', ''])
477 func StartCompl()
478 call timer_start(100, { -> execute('stopinsert') })
479 call feedkeys("Gah\<C-N>")
480 endfunc
481 END
482
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100483 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200484 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
485 call term_sendkeys(buf, ":call StartCompl()\<CR>")
486 call TermWait(buf, 200)
487 call term_sendkeys(buf, "k")
488 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
489
490 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200491endfunc
492
zeertzjqcd5dbad2022-05-04 17:51:50 +0100493func Test_complete_stopinsert_startinsert()
494 nnoremap <F2> <Cmd>startinsert<CR>
495 inoremap <F2> <Cmd>stopinsert<CR>
496 " This just checks if this causes an error
497 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
498 nunmap <F2>
499 iunmap <F2>
500endfunc
501
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100502func Test_pum_with_folds_two_tabs()
503 CheckScreendump
504
505 let lines =<< trim END
506 set fdm=marker
507 call setline(1, ['" x {{{1', '" a some text'])
508 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
509 norm! zm
510 tab sp
511 call feedkeys('2Gzv', 'xt')
512 call feedkeys("0fa", 'xt')
513 END
514
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100515 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100516 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200517 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100518 call term_sendkeys(buf, "a\<C-N>")
519 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
520
521 call term_sendkeys(buf, "\<Esc>")
522 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100523endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100524
525func Test_pum_with_preview_win()
526 CheckScreendump
527
528 let lines =<< trim END
529 funct Omni_test(findstart, base)
530 if a:findstart
531 return col(".") - 1
532 endif
533 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
534 endfunc
535 set omnifunc=Omni_test
536 set completeopt+=longest
537 END
538
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100539 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100540 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100541 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100542 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100543 call term_sendkeys(buf, "\<C-N>")
544 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
545
546 call term_sendkeys(buf, "\<Esc>")
547 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100548endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100549
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000550func Test_scrollbar_on_wide_char()
551 CheckScreendump
552
553 let lines =<< trim END
554 call setline(1, ['a', ' 啊啊啊',
555 \ ' 哦哦哦',
556 \ ' 呃呃呃'])
557 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
558 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100559 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000560 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
561 call term_sendkeys(buf, "A\<C-N>")
562 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
563
564 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000565endfunc
566
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100567" Test for inserting the tag search pattern in insert mode
568func Test_ins_compl_tag_sft()
569 call writefile([
570 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
571 \ "first\tXfoo\t/^int first() {}$/",
572 \ "second\tXfoo\t/^int second() {}$/",
573 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100574 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100575 set tags=Xtags
576 let code =<< trim [CODE]
577 int first() {}
578 int second() {}
579 int third() {}
580 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100581 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100582
583 enew
584 set showfulltag
585 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
586 call assert_equal('int second() {}', getline(1))
587 set noshowfulltag
588
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100589 set tags&
590 %bwipe!
591endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200592
593" Test for 'completefunc' deleting text
594func Test_completefunc_error()
595 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200596 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200597 func CompleteFunc(findstart, base)
598 if a:findstart == 1
599 normal dd
600 return col('.') - 1
601 endif
602 return ['a', 'b']
603 endfunc
604 set completefunc=CompleteFunc
605 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100606 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200607
608 " delete text when called for the second time
609 func CompleteFunc2(findstart, base)
610 if a:findstart == 1
611 return col('.') - 1
612 endif
613 normal dd
614 return ['a', 'b']
615 endfunc
616 set completefunc=CompleteFunc2
617 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100618 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200619
Bram Moolenaar97202d92021-01-28 18:34:35 +0100620 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100621 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100622 if a:findstart == 1
623 return col('.') - 1
624 endif
625 wincmd p
626 return ['a', 'b']
627 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100628 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100629 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100630 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100631 close!
632
633 set completefunc&
634 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100635 delfunc CompleteFunc2
636 delfunc CompleteFunc3
637 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100638endfunc
639
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200640" Test for returning non-string values from 'completefunc'
641func Test_completefunc_invalid_data()
642 new
643 func! CompleteFunc(findstart, base)
644 if a:findstart == 1
645 return col('.') - 1
646 endif
647 return [{}, '', 'moon']
648 endfunc
649 set completefunc=CompleteFunc
650 exe "normal i\<C-X>\<C-U>"
651 call assert_equal('moon', getline(1))
652 set completefunc&
653 close!
654endfunc
655
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200656" Test for errors in using complete() function
657func Test_complete_func_error()
658 call assert_fails('call complete(1, ["a"])', 'E785:')
659 func ListColors()
660 call complete(col('.'), "blue")
661 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100662 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200663 func ListMonths()
664 call complete(col('.'), test_null_list())
665 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100666 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200667 delfunc ListColors
668 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100669 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200670 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200671endfunc
672
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000673" Test for recursively starting completion mode using complete()
674func Test_recursive_complete_func()
675 func ListColors()
676 call complete(5, ["red", "blue"])
677 return ''
678 endfunc
679 new
680 call setline(1, ['a1', 'a2'])
681 set complete=.
682 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
683 call assert_equal('a2blue', getline(3))
684 delfunc ListColors
685 bw!
686endfunc
687
bfredl87af60c2022-09-24 11:17:51 +0100688" Test for using complete() with completeopt+=longest
689func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100690 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100691 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100692
693 " default: insert first match
694 set completeopt&
695 call setline(1, ['i'])
696 exe "normal Aa\<f3>\<esc>"
697 call assert_equal('iaax', getline(1))
698
699 " with longest: insert longest prefix
700 set completeopt+=longest
701 call setline(1, ['i'])
702 exe "normal Aa\<f3>\<esc>"
703 call assert_equal('iaa', getline(1))
704 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100705 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100706endfunc
707
708
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200709" Test for completing words following a completed word in a line
710func Test_complete_wrapscan()
711 " complete words from another buffer
712 new
713 call setline(1, ['one two', 'three four'])
714 new
715 setlocal complete=w
716 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
717 call assert_equal('two three four', getline(1))
718 close!
719 " complete words from the current buffer
720 setlocal complete=.
721 %d
722 call setline(1, ['one two', ''])
723 call cursor(2, 1)
724 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
725 call assert_equal('one two one two', getline(2))
726 close!
727endfunc
728
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200729" Test for completing special characters
730func Test_complete_special_chars()
731 new
732 call setline(1, 'int .*[-\^$ func float')
733 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
734 call assert_equal('int .*[-\^$ func float', getline(2))
735 close!
736endfunc
737
738" Test for completion when text is wrapped across lines.
739func Test_complete_across_line()
740 new
741 call setline(1, ['red green blue', 'one two three'])
742 setlocal textwidth=20
743 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
744 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
745 close!
746endfunc
747
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000748" Test for completing words with a '.' at the end of a word.
749func Test_complete_joinspaces()
750 new
751 call setline(1, ['one two.', 'three. four'])
752 set joinspaces
753 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
754 call assert_equal("one two. three. four", getline(3))
755 set joinspaces&
756 bw!
757endfunc
758
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200759" Test for using CTRL-L to add one character when completing matching
760func Test_complete_add_onechar()
761 new
762 call setline(1, ['wool', 'woodwork'])
763 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
764 call assert_equal('woof', getline(3))
765
766 " use 'ignorecase' and backspace to erase characters from the prefix string
767 " and then add letters using CTRL-L
768 %d
769 set ignorecase backspace=2
770 setlocal complete=.
771 call setline(1, ['workhorse', 'workload'])
772 normal Go
773 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
774 call assert_equal('workh', getline(3))
775 set ignorecase& backspace&
776 close!
777endfunc
778
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000779" Test for using CTRL-X CTRL-L to complete whole lines lines
780func Test_complete_wholeline()
781 new
782 " complete one-line
783 call setline(1, ['a1', 'a2'])
784 exe "normal ggoa\<C-X>\<C-L>"
785 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
786 " go to the next match (wrapping around the buffer)
787 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
788 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
789 " go to the next match
790 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
791 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
792 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
793 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
794 " repeat the test using CTRL-L
795 " go to the next match (wrapping around the buffer)
796 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
797 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
798 " go to the next match
799 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
800 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
801 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
802 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
803 %d
804 " use CTRL-X CTRL-L to add one more line
805 call setline(1, ['a1', 'b1'])
806 setlocal complete=.
807 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
808 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
809 bw!
810endfunc
811
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200812" Test insert completion with 'cindent' (adjust the indent)
813func Test_complete_with_cindent()
814 new
815 setlocal cindent
816 call setline(1, ['if (i == 1)', " j = 2;"])
817 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
818 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
819
820 %d
821 call setline(1, ['when while', '{', ''])
822 setlocal cinkeys+==while
823 exe "normal Giwh\<C-P> "
824 call assert_equal("\twhile ", getline('$'))
825 close!
826endfunc
827
828" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
829func Test_complete_cmdline()
830 new
831 exe "normal icaddb\<C-X>\<C-V>"
832 call assert_equal('caddbuffer', getline(1))
833 exe "normal ocall getqf\<C-X>\<C-V>"
834 call assert_equal('call getqflist(', getline(2))
835 exe "normal oabcxyz(\<C-X>\<C-V>"
836 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200837 com! -buffer TestCommand1 echo 'TestCommand1'
838 com! -buffer TestCommand2 echo 'TestCommand2'
839 write TestCommand1Test
840 write TestCommand2Test
841 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
842 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
843 call assert_equal('TestCommand2Test', getline(4))
844 call delete('TestCommand1Test')
845 call delete('TestCommand2Test')
846 delcom TestCommand1
847 delcom TestCommand2
848 close!
849endfunc
850
851" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
852func Test_complete_stop()
853 new
854 func Save_mode1()
855 let g:mode1 = mode(1)
856 return ''
857 endfunc
858 func Save_mode2()
859 let g:mode2 = mode(1)
860 return ''
861 endfunc
862 inoremap <F1> <C-R>=Save_mode1()<CR>
863 inoremap <F2> <C-R>=Save_mode2()<CR>
864 call setline(1, ['aaa bbb ccc '])
865 exe "normal A\<C-N>\<C-P>\<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('aaa bbb ccc ', getline(1))
869 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
870 call assert_equal('ic', g:mode1)
871 call assert_equal('i', g:mode2)
872 call assert_equal('aaa bbb ccc aaa', getline(1))
873 set completeopt+=noselect
874 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
875 call assert_equal('ic', g:mode1)
876 call assert_equal('i', g:mode2)
877 call assert_equal('aaa bbb ccc aaa bb', getline(1))
878 set completeopt&
879 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
880 call assert_equal('ic', g:mode1)
881 call assert_equal('i', g:mode2)
882 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
883 com! -buffer TestCommand1 echo 'TestCommand1'
884 com! -buffer TestCommand2 echo 'TestCommand2'
885 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
886 call assert_equal('ic', g:mode1)
887 call assert_equal('i', g:mode2)
888 call assert_equal('TestCommand2', getline(2))
889 delcom TestCommand1
890 delcom TestCommand2
891 unlet g:mode1
892 unlet g:mode2
893 iunmap <F1>
894 iunmap <F2>
895 delfunc Save_mode1
896 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200897 close!
898endfunc
899
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000900" Test for typing CTRL-R in insert completion mode to insert a register
901" content.
902func Test_complete_reginsert()
903 new
904 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
905
906 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
907 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
908 " inserted.
909 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
910 call assert_equal('a123', getline(5))
911 let @r = "\<C-P>\<C-P>"
912 exe "normal GCa\<C-P>\<C-R>r"
913 call assert_equal('a12', getline(5))
914 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
915 call assert_equal('a1234x', getline(5))
916 bw!
917endfunc
918
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200919func Test_issue_7021()
920 CheckMSWindows
921
922 let orig_shellslash = &shellslash
923 set noshellslash
924
925 set completeslash=slash
926 call assert_false(expand('~') =~ '/')
927
928 let &shellslash = orig_shellslash
929 set completeslash=
930endfunc
931
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000932" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
933func Test_complete_longest_match()
934 for e in ['latin1', 'utf-8']
935 exe 'set encoding=' .. e
936 new
937 set complete=.
938 set completeopt=menu,longest
939 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
940 exe "normal Gopfx\<C-P>"
941 call assert_equal('pfx_', getline(5))
942 bw!
943 endfor
944
945 " Test for completing additional words with longest match set
946 new
947 call setline(1, ['abc1', 'abd2'])
948 exe "normal Goab\<C-P>\<C-X>\<C-P>"
949 call assert_equal('ab', getline(3))
950 bw!
951 set complete& completeopt&
952endfunc
953
954" Test for removing the first displayed completion match and selecting the
955" match just before that.
956func Test_complete_erase_firstmatch()
957 new
958 call setline(1, ['a12', 'a34', 'a56'])
959 set complete=.
960 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
961 call assert_equal('a34', getline('$'))
962 set complete&
963 bw!
964endfunc
965
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000966" Test for completing words from unloaded buffers
967func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100968 call writefile(['abc'], "Xfile1", 'D')
969 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000970 edit Xfile1
971 edit Xfile2
972 new | close
973 enew
974 bunload Xfile1 Xfile2
975 set complete=u
976 " complete from an unloaded buffer
977 exe "normal! ia\<C-P>"
978 call assert_equal('abc', getline(1))
979 exe "normal! od\<C-P>"
980 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100981
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000982 set complete&
983 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000984endfunc
985
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000986" Test for completing whole lines from unloaded buffers
987func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100988 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000989 edit Xfile1
990 enew
991 set complete=u
992 exe "normal! ia\<C-X>\<C-L>\<C-P>"
993 call assert_equal('a line2', getline(1))
994 %d
995 " completing from an unlisted buffer should fail
996 bdel Xfile1
997 exe "normal! ia\<C-X>\<C-L>\<C-P>"
998 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100999
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001000 set complete&
1001 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001002endfunc
1003
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001004" Test for completing words from unlisted buffers
1005func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001006 call writefile(['abc'], "Xfile1", 'D')
1007 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001008 edit Xfile1
1009 edit Xfile2
1010 new | close
1011 bdel Xfile1 Xfile2
1012 set complete=U
1013 " complete from an unlisted buffer
1014 exe "normal! ia\<C-P>"
1015 call assert_equal('abc', getline(1))
1016 exe "normal! od\<C-P>"
1017 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001018
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001019 set complete&
1020 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001021endfunc
1022
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001023" Test for completing whole lines from unlisted buffers
1024func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001025 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001026 edit Xfile1
1027 enew
1028 set complete=U
1029 " completing from a unloaded buffer should fail
1030 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1031 call assert_equal('a', getline(1))
1032 %d
1033 bdel Xfile1
1034 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1035 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001036
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001037 set complete&
1038 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001039endfunc
1040
1041" Test for adding a multibyte character using CTRL-L in completion mode
1042func Test_complete_mbyte_char_add()
1043 new
1044 set complete=.
1045 call setline(1, 'abė')
1046 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1047 call assert_equal('abė', getline(2))
1048 " Test for a leader with multibyte character
1049 %d
1050 call setline(1, 'abėĕ')
1051 exe "normal! oabė\<C-P>"
1052 call assert_equal('abėĕ', getline(2))
1053 bw!
1054endfunc
1055
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001056" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1057" not to complete matches from the local buffer. Also test using multiple
1058" <C-X> to cancel the current completion mode.
1059func Test_complete_local_expansion()
1060 new
1061 set complete=t
1062 call setline(1, ['abc', 'def'])
1063 exe "normal! Go\<C-X>\<C-P>"
1064 call assert_equal("def", getline(3))
1065 exe "normal! Go\<C-P>"
1066 call assert_equal("", getline(4))
1067 exe "normal! Go\<C-X>\<C-N>"
1068 call assert_equal("abc", getline(5))
1069 exe "normal! Go\<C-N>"
1070 call assert_equal("", getline(6))
1071
1072 " use multiple <C-X> to cancel the previous completion mode
1073 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1074 call assert_equal("", getline(7))
1075 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1076 call assert_equal("", getline(8))
1077 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1078 call assert_equal("abc", getline(9))
1079
1080 " interrupt the current completion mode
1081 set completeopt=menu,noinsert
1082 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1083 call assert_equal("abc", getline(10))
1084
1085 " when only one <C-X> is used to interrupt, do normal expansion
1086 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1087 call assert_equal("", getline(11))
1088 set completeopt&
1089
1090 " using two <C-X> in non-completion mode and restarting the same mode
1091 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1092 call assert_equal("def", getline(12))
1093
1094 " test for adding a match from the original empty text
1095 %d
1096 call setline(1, 'abc def g')
1097 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1098 call assert_equal('def', getline(2))
1099 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1100 call assert_equal('abc', getline(2))
1101
1102 bw!
1103endfunc
1104
1105" Test for undoing changes after a insert-mode completion
1106func Test_complete_undo()
1107 new
1108 set complete=.
1109 " undo with 'ignorecase'
1110 call setline(1, ['ABOVE', 'BELOW'])
1111 set ignorecase
1112 exe "normal! Goab\<C-G>u\<C-P>"
1113 call assert_equal("ABOVE", getline(3))
1114 undo
1115 call assert_equal("ab", getline(3))
1116 set ignorecase&
1117 %d
1118 " undo with longest match
1119 set completeopt=menu,longest
1120 call setline(1, ['above', 'about'])
1121 exe "normal! Goa\<C-G>u\<C-P>"
1122 call assert_equal("abo", getline(3))
1123 undo
1124 call assert_equal("a", getline(3))
1125 set completeopt&
1126 %d
1127 " undo for line completion
1128 call setline(1, ['above that change', 'below that change'])
1129 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1130 call assert_equal("above that change", getline(3))
1131 undo
1132 call assert_equal("above", getline(3))
1133
1134 bw!
1135endfunc
1136
1137" Test for completing a very long word
1138func Test_complete_long_word()
1139 set complete&
1140 new
1141 call setline(1, repeat('x', 950) .. ' one two three')
1142 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1143 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1144 %d
1145 " should fail when more than 950 characters are in a word
1146 call setline(1, repeat('x', 951) .. ' one two three')
1147 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1148 call assert_equal(repeat('x', 951), getline(2))
1149
1150 " Test for adding a very long word to an existing completion
1151 %d
1152 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1153 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1154 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1155 bw!
1156endfunc
1157
1158" Test for some fields in the complete items used by complete()
1159func Test_complete_items()
1160 func CompleteItems(idx)
1161 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1162 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1163 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1164 \ [#{user_data: 'u9'}],
1165 \ [#{word: "", user_data: 'u10'}],
1166 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1167 call complete(col('.'), items[a:idx])
1168 return ''
1169 endfunc
1170 new
1171 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1172 call assert_equal('u2', v:completed_item.user_data)
1173 call assert_equal('one', getline(1))
1174 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1175 call assert_equal('u3', v:completed_item.user_data)
1176 call assert_equal('one', getline(2))
1177 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1178 call assert_equal('', getline(3))
1179 set completeopt=menu,noinsert
1180 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1181 call assert_equal('oNE', getline(4))
1182 call assert_equal('u8', v:completed_item.user_data)
1183 set completeopt&
1184 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1185 call assert_equal('', getline(5))
1186 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1187 call assert_equal('', getline(6))
1188 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1189 call assert_equal('', getline(7))
1190 call assert_equal('u11', v:completed_item.user_data)
1191 " pass invalid argument to complete()
1192 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1193 call assert_fails('exe cmd', 'E730:')
1194 bw!
1195 delfunc CompleteItems
1196endfunc
1197
1198" Test for the "refresh" item in the dict returned by an insert completion
1199" function
1200func Test_complete_item_refresh_always()
1201 let g:CallCount = 0
1202 func! Tcomplete(findstart, base)
1203 if a:findstart
1204 " locate the start of the word
1205 let line = getline('.')
1206 let start = col('.') - 1
1207 while start > 0 && line[start - 1] =~ '\a'
1208 let start -= 1
1209 endwhile
1210 return start
1211 else
1212 let g:CallCount += 1
1213 let res = ["update1", "update12", "update123"]
1214 return #{words: res, refresh: 'always'}
1215 endif
1216 endfunc
1217 new
1218 set completeopt=menu,longest
1219 set completefunc=Tcomplete
1220 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1221 call assert_equal('up', getline(1))
1222 call assert_equal(2, g:CallCount)
1223 set completeopt&
1224 set completefunc&
1225 bw!
1226 delfunc Tcomplete
1227endfunc
1228
1229" Test for completing from a thesaurus file without read permission
1230func Test_complete_unreadable_thesaurus_file()
1231 CheckUnix
1232 CheckNotRoot
1233
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001234 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001235 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001236 new
1237 set complete=sXfile
1238 exe "normal! ia\<C-P>"
1239 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001240
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001241 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001242 set complete&
1243endfunc
1244
Bram Moolenaarcc233582020-12-12 13:32:07 +01001245" Test to ensure 'Scanning...' messages are not recorded in messages history
1246func Test_z1_complete_no_history()
1247 new
1248 messages clear
1249 let currmess = execute('messages')
1250 setlocal dictionary=README.txt
1251 exe "normal owh\<C-X>\<C-K>"
1252 exe "normal owh\<C-N>"
1253 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001254 bwipe!
1255endfunc
1256
1257" A mapping is not used for the key after CTRL-X.
1258func Test_no_mapping_for_ctrl_x_key()
1259 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001260 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001261 setlocal dictionary=README.txt
1262 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1263 call assert_equal('example ', getline(1))
1264 call assert_false(exists('was_mapped'))
1265 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001266endfunc
1267
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001268" Test for different ways of setting the 'completefunc' option
1269func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001270 func CompleteFunc1(callnr, findstart, base)
1271 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1272 return a:findstart ? 0 : []
1273 endfunc
1274 func CompleteFunc2(findstart, base)
1275 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001276 return a:findstart ? 0 : []
1277 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001278
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001279 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001280 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001281 LET &completefunc = 'g:CompleteFunc2'
1282 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001283 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001284 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001285 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001286 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001287 bw!
1288
1289 #" Test for using a function()
1290 set completefunc=function('g:CompleteFunc1',\ [10])
1291 new
1292 call setline(1, 'one')
1293 LET g:CompleteFunc1Args = []
1294 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1295 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001296 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001297
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001298 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001299 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001300 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001301 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001302 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001303 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001304 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001305 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001306 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001307
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001308 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001309 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001310 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001311 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001312 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001313 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001314 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001315 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001316 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001317
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001318 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001319 set completefunc=funcref('g:CompleteFunc1',\ [13])
1320 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001321 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001322 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001323 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001324 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001325 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001326
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001327 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001328 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001329 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001330 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001331 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001332 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001333 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001334 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001335 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001336
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001337 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001338 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001339 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001340 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001341 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001342 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001343 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001344 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001345 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001346
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001347 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001348 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001349 LET optval = substitute(optval, ' ', '\\ ', 'g')
1350 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 call setline(1, 'five')
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([[16, 1, ''], [16, 0, 'five']], 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 lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001359 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001360 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001361 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001362 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001363 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001364 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001365 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001366
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001367 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001368 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001369 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001370 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001371 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001372 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001373 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001374 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001375
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001376 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001377 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001378 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001379 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001380 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001381 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001382 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001383 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001384 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001385
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001386 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001387 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001388 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001389 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001390 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001391 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001392 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001393 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001394 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001395
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001396 #" Test for using a lambda function with incorrect return value
1397 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1398 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001399 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001400 call setline(1, 'eight')
1401 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1402 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001403
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 #" Test for clearing the 'completefunc' option
1405 set completefunc=''
1406 set completefunc&
1407 call assert_fails("set completefunc=function('abc')", "E700:")
1408 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001409
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001411 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001412 call setline(1, 'five')
1413 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1414 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001415 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001416 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001417 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001418 bw!
1419 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001420 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001421
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001422 " Test for using a script-local function name
1423 func s:CompleteFunc3(findstart, base)
1424 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1425 return a:findstart ? 0 : []
1426 endfunc
1427 set completefunc=s:CompleteFunc3
1428 new
1429 call setline(1, 'script1')
1430 let g:CompleteFunc3Args = []
1431 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1432 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1433 bw!
1434
1435 let &completefunc = 's:CompleteFunc3'
1436 new
1437 call setline(1, 'script2')
1438 let g:CompleteFunc3Args = []
1439 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1440 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1441 bw!
1442 delfunc s:CompleteFunc3
1443
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001444 " In Vim9 script s: can be omitted
1445 let lines =<< trim END
1446 vim9script
1447 var CompleteFunc4Args = []
1448 def CompleteFunc4(findstart: bool, base: string): any
1449 add(CompleteFunc4Args, [findstart, base])
1450 return findstart ? 0 : []
1451 enddef
1452 set completefunc=CompleteFunc4
1453 new
1454 setline(1, 'script1')
1455 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1456 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1457 bw!
1458 END
1459 call v9.CheckScriptSuccess(lines)
1460
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001461 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001462 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001463 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1464
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001465 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001466 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001467 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001468 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001469 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001470 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001471
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001472 " set 'completefunc' to a partial with dict. This used to cause a crash.
1473 func SetCompleteFunc()
1474 let params = {'complete': function('g:DictCompleteFunc')}
1475 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001476 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001477 func g:DictCompleteFunc(_) dict
1478 endfunc
1479 call SetCompleteFunc()
1480 new
1481 call SetCompleteFunc()
1482 bw
1483 call test_garbagecollect_now()
1484 new
1485 set completefunc=
1486 wincmd w
1487 set completefunc=
1488 %bw!
1489 delfunc g:DictCompleteFunc
1490 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001491
1492 " Vim9 tests
1493 let lines =<< trim END
1494 vim9script
1495
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001496 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1497 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001498 return findstart ? 0 : []
1499 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001500
1501 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001502 set completefunc=function('Vim9CompleteFunc',\ [60])
1503 new | only
1504 setline(1, 'one')
1505 g:Vim9completeFuncArgs = []
1506 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1507 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1508 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001509
1510 # Test for using a global function name
1511 &completefunc = g:CompleteFunc2
1512 new | only
1513 setline(1, 'two')
1514 g:CompleteFunc2Args = []
1515 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1516 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1517 bw!
1518
1519 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001520 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001521 add(g:LocalCompleteFuncArgs, [findstart, base])
1522 return findstart ? 0 : []
1523 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001524 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001525 new | only
1526 setline(1, 'three')
1527 g:LocalCompleteFuncArgs = []
1528 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1529 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1530 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001531 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001532 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001533
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001534 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001535 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001536 delfunc CompleteFunc1
1537 delfunc CompleteFunc2
1538 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001539 %bw!
1540endfunc
1541
1542" Test for different ways of setting the 'omnifunc' option
1543func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001544 func OmniFunc1(callnr, findstart, base)
1545 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1546 return a:findstart ? 0 : []
1547 endfunc
1548 func OmniFunc2(findstart, base)
1549 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001550 return a:findstart ? 0 : []
1551 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001552
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001553 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001554 #" Test for using a function name
1555 LET &omnifunc = 'g:OmniFunc2'
1556 new
1557 call setline(1, 'zero')
1558 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001559 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001560 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1561 bw!
1562
1563 #" Test for using a function()
1564 set omnifunc=function('g:OmniFunc1',\ [10])
1565 new
1566 call setline(1, 'one')
1567 LET g:OmniFunc1Args = []
1568 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1569 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001570 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001571
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001572 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001573 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001574 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001575 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001576 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001577 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001578 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001579 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001580 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001581
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001582 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001583 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001584 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001585 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001586 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001587 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001588 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001589 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001590 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001591
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001592 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001593 set omnifunc=funcref('g:OmniFunc1',\ [13])
1594 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001595 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001596 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001598 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001599 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001600
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001602 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001603 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001604 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001606 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001607 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001608 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001609 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001610
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001611 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001612 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001613 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001614 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001615 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001616 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001617 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001618 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001619 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001620
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001621 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001622 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001623 LET optval = substitute(optval, ' ', '\\ ', 'g')
1624 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 call setline(1, 'five')
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([[16, 1, ''], [16, 0, 'five']], 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 lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001633 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001634 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001635 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001636 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001637 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001638 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001639 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001640
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001641 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001642 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001643 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001645 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001646 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001647 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001648 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001649
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001650 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001651 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001652 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001653 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001654 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001655 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001656 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001657 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001659
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001661 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001662 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001663 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001664 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001665 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001666 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001667 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001668 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001669
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001670 #" Test for using a lambda function with incorrect return value
1671 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1672 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001673 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001674 call setline(1, 'eight')
1675 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1676 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001677
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 #" Test for clearing the 'omnifunc' option
1679 set omnifunc=''
1680 set omnifunc&
1681 call assert_fails("set omnifunc=function('abc')", "E700:")
1682 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001683
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001685 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001686 call setline(1, 'nine')
1687 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1688 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001689 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001690 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001691 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001692 bw!
1693 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001694 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001695
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001696 " Test for using a script-local function name
1697 func s:OmniFunc3(findstart, base)
1698 call add(g:OmniFunc3Args, [a:findstart, a:base])
1699 return a:findstart ? 0 : []
1700 endfunc
1701 set omnifunc=s:OmniFunc3
1702 new
1703 call setline(1, 'script1')
1704 let g:OmniFunc3Args = []
1705 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1706 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1707 bw!
1708
1709 let &omnifunc = 's:OmniFunc3'
1710 new
1711 call setline(1, 'script2')
1712 let g:OmniFunc3Args = []
1713 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1714 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1715 bw!
1716 delfunc s:OmniFunc3
1717
1718 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001719 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001720 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1721
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001722 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001723 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001724 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001725 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001726 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001727 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001728
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001729 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1730 func SetOmniFunc()
1731 let params = {'omni': function('g:DictOmniFunc')}
1732 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001733 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001734 func g:DictOmniFunc(_) dict
1735 endfunc
1736 call SetOmniFunc()
1737 new
1738 call SetOmniFunc()
1739 bw
1740 call test_garbagecollect_now()
1741 new
1742 set omnifunc=
1743 wincmd w
1744 set omnifunc=
1745 %bw!
1746 delfunc g:DictOmniFunc
1747 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001748
1749 " Vim9 tests
1750 let lines =<< trim END
1751 vim9script
1752
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001753 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1754 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001755 return findstart ? 0 : []
1756 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001757
1758 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001759 set omnifunc=function('Vim9omniFunc',\ [60])
1760 new | only
1761 setline(1, 'one')
1762 g:Vim9omniFunc_Args = []
1763 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1764 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1765 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001766
1767 # Test for using a global function name
1768 &omnifunc = g:OmniFunc2
1769 new | only
1770 setline(1, 'two')
1771 g:OmniFunc2Args = []
1772 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1773 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1774 bw!
1775
1776 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001777 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001778 add(g:LocalOmniFuncArgs, [findstart, base])
1779 return findstart ? 0 : []
1780 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001781 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001782 new | only
1783 setline(1, 'three')
1784 g:LocalOmniFuncArgs = []
1785 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1786 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1787 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001788 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001789 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001790
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001791 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001792 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001793 delfunc OmniFunc1
1794 delfunc OmniFunc2
1795 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001796 %bw!
1797endfunc
1798
1799" Test for different ways of setting the 'thesaurusfunc' option
1800func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001801 func TsrFunc1(callnr, findstart, base)
1802 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001803 return a:findstart ? 0 : []
1804 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001805 func TsrFunc2(findstart, base)
1806 call add(g:TsrFunc2Args, [a:findstart, a:base])
1807 return a:findstart ? 0 : ['sunday']
1808 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001809
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001810 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001811 #" Test for using a function name
1812 LET &thesaurusfunc = 'g:TsrFunc2'
1813 new
1814 call setline(1, 'zero')
1815 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001816 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001817 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1818 bw!
1819
1820 #" Test for using a function()
1821 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1822 new
1823 call setline(1, 'one')
1824 LET g:TsrFunc1Args = []
1825 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1826 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001827 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001828
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001829 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001830 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001831 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001832 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001833 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001834 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001835 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001836 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001837 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001838
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001839 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001840 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001841 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001842 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001843 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001844 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001845 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001846 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001847 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001848
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001849 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001850 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1851 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001852 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001853 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001854 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001855 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001856 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001857
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001858 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001859 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001860 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001861 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001862 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001863 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001864 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001865 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001866 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001867
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001868 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001869 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001870 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001871 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001872 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001873 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001874 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001875 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001876 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001877
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001878 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001879 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001880 LET optval = substitute(optval, ' ', '\\ ', 'g')
1881 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001882 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001883 call setline(1, 'five')
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([[16, 1, ''], [16, 0, 'five']], 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 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001890 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001891 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001892 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001893 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001894 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001895 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001897
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001898 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001899 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001900 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001901 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001902 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001903 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001904 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001905 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001906
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001907 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001908 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001909 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001910 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001911 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001912 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001913 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001914 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001915 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001916
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001917 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001918 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001919 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001920 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001921 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001922 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001924 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001925 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001926
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001927 #" Test for using a lambda function with incorrect return value
1928 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1929 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001930 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001931 call setline(1, 'eight')
1932 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1933 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001934
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 #" Test for clearing the 'thesaurusfunc' option
1936 set thesaurusfunc=''
1937 set thesaurusfunc&
1938 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1939 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001940
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001942 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001943 call setline(1, 'ten')
1944 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1945 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001946 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001947 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001948 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001949 bw!
1950
1951 #" Use a buffer-local value and a global value
1952 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001953 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001954 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001955 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001956 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1957 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001958 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 new
1960 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001961 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001962 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1963 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001964 call assert_equal([], g:TsrFunc1Args)
1965 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001966 wincmd w
1967 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001968 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001969 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1970 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001971 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001972 :%bw!
1973 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001974 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001976 " Test for using a script-local function name
1977 func s:TsrFunc3(findstart, base)
1978 call add(g:TsrFunc3Args, [a:findstart, a:base])
1979 return a:findstart ? 0 : []
1980 endfunc
1981 set tsrfu=s:TsrFunc3
1982 new
1983 call setline(1, 'script1')
1984 let g:TsrFunc3Args = []
1985 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1986 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1987 bw!
1988
1989 let &tsrfu = 's:TsrFunc3'
1990 new
1991 call setline(1, 'script2')
1992 let g:TsrFunc3Args = []
1993 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1994 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1995 bw!
1996 delfunc s:TsrFunc3
1997
1998 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001999 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002000 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2001
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002002 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002003 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002004 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002005 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002006 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002007 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002008 bw!
2009
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002010 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2011 func SetTsrFunc()
2012 let params = {'thesaurus': function('g:DictTsrFunc')}
2013 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002014 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002015 func g:DictTsrFunc(_) dict
2016 endfunc
2017 call SetTsrFunc()
2018 new
2019 call SetTsrFunc()
2020 bw
2021 call test_garbagecollect_now()
2022 new
2023 set thesaurusfunc=
2024 wincmd w
2025 %bw!
2026 delfunc SetTsrFunc
2027
2028 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2029 " cause a crash.
2030 func SetLocalTsrFunc()
2031 let params = {'thesaurus': function('g:DictTsrFunc')}
2032 let &l:thesaurusfunc = params.thesaurus
2033 endfunc
2034 call SetLocalTsrFunc()
2035 call test_garbagecollect_now()
2036 call SetLocalTsrFunc()
2037 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002038 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002039 delfunc g:DictTsrFunc
2040 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002041
2042 " Vim9 tests
2043 let lines =<< trim END
2044 vim9script
2045
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002046 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2047 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002048 return findstart ? 0 : []
2049 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002050
2051 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002052 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2053 new | only
2054 setline(1, 'one')
2055 g:Vim9tsrFunc_Args = []
2056 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2057 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2058 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002059
2060 # Test for using a global function name
2061 &thesaurusfunc = g:TsrFunc2
2062 new | only
2063 setline(1, 'two')
2064 g:TsrFunc2Args = []
2065 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2066 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2067 bw!
2068
2069 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002070 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002071 add(g:LocalTsrFuncArgs, [findstart, base])
2072 return findstart ? 0 : []
2073 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002074 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002075 new | only
2076 setline(1, 'three')
2077 g:LocalTsrFuncArgs = []
2078 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2079 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2080 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002081 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002082 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002083
2084 " cleanup
2085 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002086 delfunc TsrFunc1
2087 delfunc TsrFunc2
2088 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002089 %bw!
2090endfunc
2091
Christian Brabandtac72c212022-04-07 21:00:53 +01002092func FooBarComplete(findstart, base)
2093 if a:findstart
2094 return col('.') - 1
2095 else
2096 return ["Foo", "Bar", "}"]
2097 endif
2098endfunc
2099
2100func Test_complete_smartindent()
2101 new
2102 setlocal smartindent completefunc=FooBarComplete
2103
2104 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2105 let result = getline(1,'$')
2106 call assert_equal(['', '{','}',''], result)
2107 bw!
2108 delfunction! FooBarComplete
2109endfunc
2110
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002111func Test_complete_overrun()
2112 " this was going past the end of the copied text
2113 new
2114 sil norm si”0s0 
2115 bwipe!
2116endfunc
2117
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002118func Test_infercase_very_long_line()
2119 " this was truncating the line when inferring case
2120 new
2121 let longLine = "blah "->repeat(300)
2122 let verylongLine = "blah "->repeat(400)
2123 call setline(1, verylongLine)
2124 call setline(2, longLine)
2125 set ic infercase
2126 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2127 call assert_equal(longLine, getline(3))
2128
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002129 " check that the too long text is NUL terminated
2130 %del
2131 norm o
2132 norm 1987ax
2133 exec "norm ox\<C-X>\<C-L>"
2134 call assert_equal(repeat('x', 1987), getline(3))
2135
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002136 bwipe!
2137 set noic noinfercase
2138endfunc
2139
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002140func Test_ins_complete_add()
2141 " this was reading past the end of allocated memory
2142 new
2143 norm o
2144 norm 7o€€
2145 sil! norm o
2146
2147 bwipe!
2148endfunc
2149
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002150func Test_ins_complete_end_of_line()
2151 " this was reading past the end of the line
2152 new
2153 norm 8o€ý 
2154 sil! norm o
2155
2156 bwipe!
2157endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002158
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002159func s:Tagfunc(t,f,o)
2160 bwipe!
2161 return []
2162endfunc
2163
2164" This was using freed memory, since 'complete' was in a wiped out buffer.
2165" Also using a window that was closed.
2166func Test_tagfunc_wipes_out_buffer()
2167 new
2168 set complete=.,t,w,b,u,i
2169 se tagfunc=s:Tagfunc
2170 sil norm i
2171
2172 bwipe!
2173endfunc
2174
2175
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002176" vim: shiftwidth=2 sts=2 expandtab