blob: 52306e8e91f216b1abf132509f4752a34ef794f9 [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
Girish Palya8950bf72024-03-20 20:07:29 +0100415func CompleteInfoUserDefinedFn(findstart, query)
416 " User defined function (i_CTRL-X_CTRL-U)
417 if a:findstart
418 return col('.')
419 endif
420 return [{'word': 'foo'}, {'word': 'bar'}, {'word': 'baz'}, {'word': 'qux'}]
421endfunc
422
423func CompleteInfoTestUserDefinedFn(mvmt, idx, noselect)
424 new
425 if a:noselect
426 set completeopt=menuone,popup,noinsert,noselect
427 else
428 set completeopt=menu,preview
429 endif
430 set completefunc=CompleteInfoUserDefinedFn
431 call feedkeys("i\<C-X>\<C-U>" . a:mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
432 let completed = a:idx != -1 ? ['foo', 'bar', 'baz', 'qux']->get(a:idx) : ''
433 call assert_equal(completed. "{'pum_visible': 1, 'mode': 'function', 'selected': " . a:idx . ", 'items': [" .
434 \ "{'word': 'foo', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
435 \ "{'word': 'bar', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
436 \ "{'word': 'baz', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
437 \ "{'word': 'qux', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" .
438 \ "]}", getline(1))
439 bwipe!
440 set completeopt&
441 set completefunc&
442endfunc
443
444func Test_complete_info_user_defined_fn()
445 " forward
446 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 1, v:true)
447 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 2, v:true)
448 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 2, v:false)
449 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 3, v:false)
450 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>", -1, v:false)
451 " backward
452 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 2, v:true)
453 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 1, v:true)
454 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>", -1, v:true)
455 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 3, v:false)
456 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 2, v:false)
457 " forward backward
458 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 1, v:true)
459 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 0, v:true)
460 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 2, v:false)
461 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 3, v:false)
462 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 1, v:false)
463 " backward forward
464 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 0, v:true)
465 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 2, v:true)
466 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 1, v:false)
467 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 3, v:false)
468 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-N>\<C-N>", 1, v:false)
469endfunc
470
zeertzjq440d4cb2023-03-02 17:51:32 +0000471" Test that mouse scrolling/movement should not interrupt completion.
472func Test_mouse_scroll_move_during_completion()
473 new
474 com! -buffer TestCommand1 echo 'TestCommand1'
475 com! -buffer TestCommand2 echo 'TestCommand2'
476 call setline(1, ['', '', '', '', ''])
477 call cursor(5, 1)
478
479 " Without completion menu scrolling can move text.
480 set completeopt-=menu wrap
481 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
482 call assert_equal('TestCommand2', getline('.'))
483 call assert_notequal(1, winsaveview().topline)
484 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
485 call assert_equal('TestCommand2', getline('.'))
486 call assert_equal(1, winsaveview().topline)
487 set nowrap
488 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
489 call assert_equal('TestCommand2', getline('.'))
490 call assert_notequal(0, winsaveview().leftcol)
491 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
492 call assert_equal('TestCommand2', getline('.'))
493 call assert_equal(0, winsaveview().leftcol)
494 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
495 call assert_equal('TestCommand2', getline('.'))
496
497 " With completion menu scrolling cannot move text.
498 set completeopt+=menu wrap
499 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
500 call assert_equal('TestCommand2', getline('.'))
501 call assert_equal(1, winsaveview().topline)
502 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
503 call assert_equal('TestCommand2', getline('.'))
504 call assert_equal(1, winsaveview().topline)
505 set nowrap
506 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
507 call assert_equal('TestCommand2', getline('.'))
508 call assert_equal(0, winsaveview().leftcol)
509 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
510 call assert_equal('TestCommand2', getline('.'))
511 call assert_equal(0, winsaveview().leftcol)
512 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
513 call assert_equal('TestCommand2', getline('.'))
514
515 bwipe!
516 set completeopt& wrap&
517endfunc
518
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100519" Check that when using feedkeys() typeahead does not interrupt searching for
520" completions.
521func Test_compl_feedkeys()
522 new
523 set completeopt=menuone,noselect
524 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
525 call assert_equal("jump jump", getline(1))
526 bwipe!
527 set completeopt&
528endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200529
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200530" Test for insert path completion with completeslash option
531func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200532 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200533
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100534 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200535 let orig_shellslash = &shellslash
536 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200537 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200538
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200539 set noshellslash
540
541 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100542 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100543 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200544
545 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100546 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100547 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200548
549 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100550 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100551 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200552
553 set shellslash
554
555 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100556 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100557 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200558
559 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100560 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100561 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200562
563 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100564 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100565 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200566 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200567
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200568 set noshellslash
569 set completeslash=slash
570 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
571
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200572 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200573 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200574endfunc
575
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200576func Test_pum_stopped_by_timer()
577 CheckScreendump
578
579 let lines =<< trim END
580 call setline(1, ['hello', 'hullo', 'heeee', ''])
581 func StartCompl()
582 call timer_start(100, { -> execute('stopinsert') })
583 call feedkeys("Gah\<C-N>")
584 endfunc
585 END
586
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100587 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200588 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
589 call term_sendkeys(buf, ":call StartCompl()\<CR>")
590 call TermWait(buf, 200)
591 call term_sendkeys(buf, "k")
592 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
593
594 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200595endfunc
596
zeertzjqcd5dbad2022-05-04 17:51:50 +0100597func Test_complete_stopinsert_startinsert()
598 nnoremap <F2> <Cmd>startinsert<CR>
599 inoremap <F2> <Cmd>stopinsert<CR>
600 " This just checks if this causes an error
601 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
602 nunmap <F2>
603 iunmap <F2>
604endfunc
605
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100606func Test_pum_with_folds_two_tabs()
607 CheckScreendump
608
609 let lines =<< trim END
610 set fdm=marker
611 call setline(1, ['" x {{{1', '" a some text'])
612 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
613 norm! zm
614 tab sp
615 call feedkeys('2Gzv', 'xt')
616 call feedkeys("0fa", 'xt')
617 END
618
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100619 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100620 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200621 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100622 call term_sendkeys(buf, "a\<C-N>")
623 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
624
625 call term_sendkeys(buf, "\<Esc>")
626 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100627endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100628
629func Test_pum_with_preview_win()
630 CheckScreendump
631
632 let lines =<< trim END
633 funct Omni_test(findstart, base)
634 if a:findstart
635 return col(".") - 1
636 endif
637 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
638 endfunc
639 set omnifunc=Omni_test
640 set completeopt+=longest
641 END
642
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100643 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100644 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100645 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100646 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100647 call term_sendkeys(buf, "\<C-N>")
648 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
649
650 call term_sendkeys(buf, "\<Esc>")
651 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100652endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100653
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000654func Test_scrollbar_on_wide_char()
655 CheckScreendump
656
657 let lines =<< trim END
658 call setline(1, ['a', ' 啊啊啊',
659 \ ' 哦哦哦',
660 \ ' 呃呃呃'])
661 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
662 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100663 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000664 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
665 call term_sendkeys(buf, "A\<C-N>")
666 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
667
668 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000669endfunc
670
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100671" Test for inserting the tag search pattern in insert mode
672func Test_ins_compl_tag_sft()
673 call writefile([
674 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
675 \ "first\tXfoo\t/^int first() {}$/",
676 \ "second\tXfoo\t/^int second() {}$/",
677 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100678 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100679 set tags=Xtags
680 let code =<< trim [CODE]
681 int first() {}
682 int second() {}
683 int third() {}
684 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100685 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100686
687 enew
688 set showfulltag
689 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
690 call assert_equal('int second() {}', getline(1))
691 set noshowfulltag
692
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100693 set tags&
694 %bwipe!
695endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200696
697" Test for 'completefunc' deleting text
698func Test_completefunc_error()
699 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200700 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200701 func CompleteFunc(findstart, base)
702 if a:findstart == 1
703 normal dd
704 return col('.') - 1
705 endif
706 return ['a', 'b']
707 endfunc
708 set completefunc=CompleteFunc
709 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100710 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200711
712 " delete text when called for the second time
713 func CompleteFunc2(findstart, base)
714 if a:findstart == 1
715 return col('.') - 1
716 endif
717 normal dd
718 return ['a', 'b']
719 endfunc
720 set completefunc=CompleteFunc2
721 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100722 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200723
Bram Moolenaar97202d92021-01-28 18:34:35 +0100724 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100725 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100726 if a:findstart == 1
727 return col('.') - 1
728 endif
729 wincmd p
730 return ['a', 'b']
731 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100732 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100733 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100734 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100735 close!
736
737 set completefunc&
738 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100739 delfunc CompleteFunc2
740 delfunc CompleteFunc3
741 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100742endfunc
743
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200744" Test for returning non-string values from 'completefunc'
745func Test_completefunc_invalid_data()
746 new
747 func! CompleteFunc(findstart, base)
748 if a:findstart == 1
749 return col('.') - 1
750 endif
751 return [{}, '', 'moon']
752 endfunc
753 set completefunc=CompleteFunc
754 exe "normal i\<C-X>\<C-U>"
755 call assert_equal('moon', getline(1))
756 set completefunc&
757 close!
758endfunc
759
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200760" Test for errors in using complete() function
761func Test_complete_func_error()
762 call assert_fails('call complete(1, ["a"])', 'E785:')
763 func ListColors()
764 call complete(col('.'), "blue")
765 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100766 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200767 func ListMonths()
768 call complete(col('.'), test_null_list())
769 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100770 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200771 delfunc ListColors
772 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100773 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200774 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200775endfunc
776
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000777" Test for recursively starting completion mode using complete()
778func Test_recursive_complete_func()
779 func ListColors()
780 call complete(5, ["red", "blue"])
781 return ''
782 endfunc
783 new
784 call setline(1, ['a1', 'a2'])
785 set complete=.
786 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
787 call assert_equal('a2blue', getline(3))
788 delfunc ListColors
789 bw!
790endfunc
791
bfredl87af60c2022-09-24 11:17:51 +0100792" Test for using complete() with completeopt+=longest
793func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100794 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100795 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100796
797 " default: insert first match
798 set completeopt&
799 call setline(1, ['i'])
800 exe "normal Aa\<f3>\<esc>"
801 call assert_equal('iaax', getline(1))
802
803 " with longest: insert longest prefix
804 set completeopt+=longest
805 call setline(1, ['i'])
806 exe "normal Aa\<f3>\<esc>"
807 call assert_equal('iaa', getline(1))
808 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100809 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100810endfunc
811
812
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200813" Test for completing words following a completed word in a line
814func Test_complete_wrapscan()
815 " complete words from another buffer
816 new
817 call setline(1, ['one two', 'three four'])
818 new
819 setlocal complete=w
820 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
821 call assert_equal('two three four', getline(1))
822 close!
823 " complete words from the current buffer
824 setlocal complete=.
825 %d
826 call setline(1, ['one two', ''])
827 call cursor(2, 1)
828 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
829 call assert_equal('one two one two', getline(2))
830 close!
831endfunc
832
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200833" Test for completing special characters
834func Test_complete_special_chars()
835 new
836 call setline(1, 'int .*[-\^$ func float')
837 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
838 call assert_equal('int .*[-\^$ func float', getline(2))
839 close!
840endfunc
841
842" Test for completion when text is wrapped across lines.
843func Test_complete_across_line()
844 new
845 call setline(1, ['red green blue', 'one two three'])
846 setlocal textwidth=20
847 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
848 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
849 close!
850endfunc
851
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000852" Test for completing words with a '.' at the end of a word.
853func Test_complete_joinspaces()
854 new
855 call setline(1, ['one two.', 'three. four'])
856 set joinspaces
857 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
858 call assert_equal("one two. three. four", getline(3))
859 set joinspaces&
860 bw!
861endfunc
862
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200863" Test for using CTRL-L to add one character when completing matching
864func Test_complete_add_onechar()
865 new
866 call setline(1, ['wool', 'woodwork'])
867 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
868 call assert_equal('woof', getline(3))
869
870 " use 'ignorecase' and backspace to erase characters from the prefix string
871 " and then add letters using CTRL-L
872 %d
873 set ignorecase backspace=2
874 setlocal complete=.
875 call setline(1, ['workhorse', 'workload'])
876 normal Go
nwounkn2e3cd522023-10-17 11:05:38 +0200877 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200878 call assert_equal('workh', getline(3))
879 set ignorecase& backspace&
880 close!
881endfunc
882
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000883" Test for using CTRL-X CTRL-L to complete whole lines lines
884func Test_complete_wholeline()
885 new
886 " complete one-line
887 call setline(1, ['a1', 'a2'])
888 exe "normal ggoa\<C-X>\<C-L>"
889 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
890 " go to the next match (wrapping around the buffer)
891 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
892 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
893 " go to the next match
894 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
895 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
896 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
897 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
898 " repeat the test using CTRL-L
899 " go to the next match (wrapping around the buffer)
900 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
901 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
902 " go to the next match
903 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
904 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
905 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
906 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
907 %d
908 " use CTRL-X CTRL-L to add one more line
909 call setline(1, ['a1', 'b1'])
910 setlocal complete=.
911 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
912 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
913 bw!
914endfunc
915
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200916" Test insert completion with 'cindent' (adjust the indent)
917func Test_complete_with_cindent()
918 new
919 setlocal cindent
920 call setline(1, ['if (i == 1)', " j = 2;"])
921 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
922 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
923
924 %d
925 call setline(1, ['when while', '{', ''])
926 setlocal cinkeys+==while
927 exe "normal Giwh\<C-P> "
928 call assert_equal("\twhile ", getline('$'))
929 close!
930endfunc
931
932" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
933func Test_complete_cmdline()
934 new
935 exe "normal icaddb\<C-X>\<C-V>"
936 call assert_equal('caddbuffer', getline(1))
937 exe "normal ocall getqf\<C-X>\<C-V>"
938 call assert_equal('call getqflist(', getline(2))
939 exe "normal oabcxyz(\<C-X>\<C-V>"
940 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200941 com! -buffer TestCommand1 echo 'TestCommand1'
942 com! -buffer TestCommand2 echo 'TestCommand2'
943 write TestCommand1Test
944 write TestCommand2Test
945 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
946 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
947 call assert_equal('TestCommand2Test', getline(4))
948 call delete('TestCommand1Test')
949 call delete('TestCommand2Test')
950 delcom TestCommand1
951 delcom TestCommand2
952 close!
953endfunc
954
955" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
956func Test_complete_stop()
957 new
958 func Save_mode1()
959 let g:mode1 = mode(1)
960 return ''
961 endfunc
962 func Save_mode2()
963 let g:mode2 = mode(1)
964 return ''
965 endfunc
966 inoremap <F1> <C-R>=Save_mode1()<CR>
967 inoremap <F2> <C-R>=Save_mode2()<CR>
968 call setline(1, ['aaa bbb ccc '])
969 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
970 call assert_equal('ic', g:mode1)
971 call assert_equal('i', g:mode2)
972 call assert_equal('aaa bbb ccc ', getline(1))
973 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
974 call assert_equal('ic', g:mode1)
975 call assert_equal('i', g:mode2)
976 call assert_equal('aaa bbb ccc aaa', getline(1))
977 set completeopt+=noselect
978 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
979 call assert_equal('ic', g:mode1)
980 call assert_equal('i', g:mode2)
981 call assert_equal('aaa bbb ccc aaa bb', getline(1))
982 set completeopt&
983 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
984 call assert_equal('ic', g:mode1)
985 call assert_equal('i', g:mode2)
986 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
987 com! -buffer TestCommand1 echo 'TestCommand1'
988 com! -buffer TestCommand2 echo 'TestCommand2'
989 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
990 call assert_equal('ic', g:mode1)
991 call assert_equal('i', g:mode2)
992 call assert_equal('TestCommand2', getline(2))
993 delcom TestCommand1
994 delcom TestCommand2
995 unlet g:mode1
996 unlet g:mode2
997 iunmap <F1>
998 iunmap <F2>
999 delfunc Save_mode1
1000 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001001 close!
1002endfunc
1003
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00001004" Test for typing CTRL-R in insert completion mode to insert a register
1005" content.
1006func Test_complete_reginsert()
1007 new
1008 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
1009
1010 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
1011 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
1012 " inserted.
1013 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
1014 call assert_equal('a123', getline(5))
1015 let @r = "\<C-P>\<C-P>"
1016 exe "normal GCa\<C-P>\<C-R>r"
1017 call assert_equal('a12', getline(5))
1018 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
1019 call assert_equal('a1234x', getline(5))
1020 bw!
1021endfunc
1022
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02001023func Test_issue_7021()
1024 CheckMSWindows
1025
1026 let orig_shellslash = &shellslash
1027 set noshellslash
1028
1029 set completeslash=slash
1030 call assert_false(expand('~') =~ '/')
1031
1032 let &shellslash = orig_shellslash
1033 set completeslash=
1034endfunc
1035
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001036" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
1037func Test_complete_longest_match()
1038 for e in ['latin1', 'utf-8']
1039 exe 'set encoding=' .. e
1040 new
1041 set complete=.
1042 set completeopt=menu,longest
1043 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
1044 exe "normal Gopfx\<C-P>"
1045 call assert_equal('pfx_', getline(5))
1046 bw!
1047 endfor
1048
1049 " Test for completing additional words with longest match set
1050 new
1051 call setline(1, ['abc1', 'abd2'])
1052 exe "normal Goab\<C-P>\<C-X>\<C-P>"
1053 call assert_equal('ab', getline(3))
1054 bw!
1055 set complete& completeopt&
1056endfunc
1057
1058" Test for removing the first displayed completion match and selecting the
1059" match just before that.
1060func Test_complete_erase_firstmatch()
1061 new
1062 call setline(1, ['a12', 'a34', 'a56'])
1063 set complete=.
1064 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1065 call assert_equal('a34', getline('$'))
1066 set complete&
1067 bw!
1068endfunc
1069
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001070" Test for completing words from unloaded buffers
1071func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001072 call writefile(['abc'], "Xfile1", 'D')
1073 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001074 edit Xfile1
1075 edit Xfile2
1076 new | close
1077 enew
1078 bunload Xfile1 Xfile2
1079 set complete=u
1080 " complete from an unloaded buffer
1081 exe "normal! ia\<C-P>"
1082 call assert_equal('abc', getline(1))
1083 exe "normal! od\<C-P>"
1084 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001085
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001086 set complete&
1087 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001088endfunc
1089
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001090" Test for completing whole lines from unloaded buffers
1091func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001092 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001093 edit Xfile1
1094 enew
1095 set complete=u
1096 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1097 call assert_equal('a line2', getline(1))
1098 %d
1099 " completing from an unlisted buffer should fail
1100 bdel Xfile1
1101 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1102 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001103
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001104 set complete&
1105 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001106endfunc
1107
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001108" Test for completing words from unlisted buffers
1109func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001110 call writefile(['abc'], "Xfile1", 'D')
1111 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001112 edit Xfile1
1113 edit Xfile2
1114 new | close
1115 bdel Xfile1 Xfile2
1116 set complete=U
1117 " complete from an unlisted buffer
1118 exe "normal! ia\<C-P>"
1119 call assert_equal('abc', getline(1))
1120 exe "normal! od\<C-P>"
1121 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001122
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001123 set complete&
1124 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001125endfunc
1126
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001127" Test for completing whole lines from unlisted buffers
1128func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001129 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001130 edit Xfile1
1131 enew
1132 set complete=U
1133 " completing from a unloaded buffer should fail
1134 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1135 call assert_equal('a', getline(1))
1136 %d
1137 bdel Xfile1
1138 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1139 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001140
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001141 set complete&
1142 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001143endfunc
1144
1145" Test for adding a multibyte character using CTRL-L in completion mode
1146func Test_complete_mbyte_char_add()
1147 new
1148 set complete=.
1149 call setline(1, 'abÄ—')
1150 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1151 call assert_equal('abÄ—', getline(2))
1152 " Test for a leader with multibyte character
1153 %d
1154 call setline(1, 'abÄ—Ä•')
1155 exe "normal! oabÄ—\<C-P>"
1156 call assert_equal('abÄ—Ä•', getline(2))
1157 bw!
1158endfunc
1159
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001160" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1161" not to complete matches from the local buffer. Also test using multiple
1162" <C-X> to cancel the current completion mode.
1163func Test_complete_local_expansion()
1164 new
1165 set complete=t
1166 call setline(1, ['abc', 'def'])
1167 exe "normal! Go\<C-X>\<C-P>"
1168 call assert_equal("def", getline(3))
1169 exe "normal! Go\<C-P>"
1170 call assert_equal("", getline(4))
1171 exe "normal! Go\<C-X>\<C-N>"
1172 call assert_equal("abc", getline(5))
1173 exe "normal! Go\<C-N>"
1174 call assert_equal("", getline(6))
1175
1176 " use multiple <C-X> to cancel the previous completion mode
1177 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1178 call assert_equal("", getline(7))
1179 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1180 call assert_equal("", getline(8))
1181 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1182 call assert_equal("abc", getline(9))
1183
1184 " interrupt the current completion mode
1185 set completeopt=menu,noinsert
1186 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1187 call assert_equal("abc", getline(10))
1188
1189 " when only one <C-X> is used to interrupt, do normal expansion
1190 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1191 call assert_equal("", getline(11))
1192 set completeopt&
1193
1194 " using two <C-X> in non-completion mode and restarting the same mode
1195 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1196 call assert_equal("def", getline(12))
1197
1198 " test for adding a match from the original empty text
1199 %d
1200 call setline(1, 'abc def g')
1201 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1202 call assert_equal('def', getline(2))
1203 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1204 call assert_equal('abc', getline(2))
1205
1206 bw!
1207endfunc
1208
1209" Test for undoing changes after a insert-mode completion
1210func Test_complete_undo()
1211 new
1212 set complete=.
1213 " undo with 'ignorecase'
1214 call setline(1, ['ABOVE', 'BELOW'])
1215 set ignorecase
1216 exe "normal! Goab\<C-G>u\<C-P>"
1217 call assert_equal("ABOVE", getline(3))
1218 undo
1219 call assert_equal("ab", getline(3))
1220 set ignorecase&
1221 %d
1222 " undo with longest match
1223 set completeopt=menu,longest
1224 call setline(1, ['above', 'about'])
1225 exe "normal! Goa\<C-G>u\<C-P>"
1226 call assert_equal("abo", getline(3))
1227 undo
1228 call assert_equal("a", getline(3))
1229 set completeopt&
1230 %d
1231 " undo for line completion
1232 call setline(1, ['above that change', 'below that change'])
1233 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1234 call assert_equal("above that change", getline(3))
1235 undo
1236 call assert_equal("above", getline(3))
1237
1238 bw!
1239endfunc
1240
1241" Test for completing a very long word
1242func Test_complete_long_word()
1243 set complete&
1244 new
1245 call setline(1, repeat('x', 950) .. ' one two three')
1246 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1247 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1248 %d
1249 " should fail when more than 950 characters are in a word
1250 call setline(1, repeat('x', 951) .. ' one two three')
1251 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1252 call assert_equal(repeat('x', 951), getline(2))
1253
1254 " Test for adding a very long word to an existing completion
1255 %d
1256 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1257 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1258 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1259 bw!
1260endfunc
1261
1262" Test for some fields in the complete items used by complete()
1263func Test_complete_items()
1264 func CompleteItems(idx)
1265 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1266 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1267 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1268 \ [#{user_data: 'u9'}],
1269 \ [#{word: "", user_data: 'u10'}],
1270 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1271 call complete(col('.'), items[a:idx])
1272 return ''
1273 endfunc
1274 new
1275 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1276 call assert_equal('u2', v:completed_item.user_data)
1277 call assert_equal('one', getline(1))
1278 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1279 call assert_equal('u3', v:completed_item.user_data)
1280 call assert_equal('one', getline(2))
1281 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1282 call assert_equal('', getline(3))
1283 set completeopt=menu,noinsert
1284 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1285 call assert_equal('oNE', getline(4))
1286 call assert_equal('u8', v:completed_item.user_data)
1287 set completeopt&
1288 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1289 call assert_equal('', getline(5))
1290 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1291 call assert_equal('', getline(6))
1292 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1293 call assert_equal('', getline(7))
1294 call assert_equal('u11', v:completed_item.user_data)
1295 " pass invalid argument to complete()
1296 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1297 call assert_fails('exe cmd', 'E730:')
1298 bw!
1299 delfunc CompleteItems
1300endfunc
1301
1302" Test for the "refresh" item in the dict returned by an insert completion
1303" function
1304func Test_complete_item_refresh_always()
1305 let g:CallCount = 0
1306 func! Tcomplete(findstart, base)
1307 if a:findstart
1308 " locate the start of the word
1309 let line = getline('.')
1310 let start = col('.') - 1
1311 while start > 0 && line[start - 1] =~ '\a'
1312 let start -= 1
1313 endwhile
1314 return start
1315 else
1316 let g:CallCount += 1
1317 let res = ["update1", "update12", "update123"]
1318 return #{words: res, refresh: 'always'}
1319 endif
1320 endfunc
1321 new
1322 set completeopt=menu,longest
1323 set completefunc=Tcomplete
1324 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1325 call assert_equal('up', getline(1))
1326 call assert_equal(2, g:CallCount)
1327 set completeopt&
1328 set completefunc&
1329 bw!
1330 delfunc Tcomplete
1331endfunc
1332
1333" Test for completing from a thesaurus file without read permission
1334func Test_complete_unreadable_thesaurus_file()
1335 CheckUnix
1336 CheckNotRoot
1337
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001338 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001339 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001340 new
1341 set complete=sXfile
1342 exe "normal! ia\<C-P>"
1343 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001344
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001345 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001346 set complete&
1347endfunc
1348
Bram Moolenaarcc233582020-12-12 13:32:07 +01001349" Test to ensure 'Scanning...' messages are not recorded in messages history
1350func Test_z1_complete_no_history()
1351 new
1352 messages clear
1353 let currmess = execute('messages')
1354 setlocal dictionary=README.txt
1355 exe "normal owh\<C-X>\<C-K>"
1356 exe "normal owh\<C-N>"
1357 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001358 bwipe!
1359endfunc
1360
1361" A mapping is not used for the key after CTRL-X.
1362func Test_no_mapping_for_ctrl_x_key()
1363 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001364 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001365 setlocal dictionary=README.txt
1366 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1367 call assert_equal('example ', getline(1))
1368 call assert_false(exists('was_mapped'))
1369 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001370endfunc
1371
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001372" Test for different ways of setting the 'completefunc' option
1373func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001374 func CompleteFunc1(callnr, findstart, base)
1375 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1376 return a:findstart ? 0 : []
1377 endfunc
1378 func CompleteFunc2(findstart, base)
1379 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001380 return a:findstart ? 0 : []
1381 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001382
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001384 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001385 LET &completefunc = 'g:CompleteFunc2'
1386 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001387 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001388 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001389 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001390 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001391 bw!
1392
1393 #" Test for using a function()
1394 set completefunc=function('g:CompleteFunc1',\ [10])
1395 new
1396 call setline(1, 'one')
1397 LET g:CompleteFunc1Args = []
1398 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1399 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001400 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001401
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001402 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001403 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001405 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001407 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001408 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001409 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001410 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001411
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001412 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001413 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001414 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001415 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001416 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001417 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001418 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001419 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001420 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001421
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001422 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001423 set completefunc=funcref('g:CompleteFunc1',\ [13])
1424 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001425 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001426 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001427 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001428 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001429 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001430
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001431 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001432 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001433 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001434 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001435 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001436 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001437 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001438 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001439 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001440
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001441 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001442 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001443 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001444 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001445 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001446 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001447 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001448 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001449 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001450
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001451 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001452 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001453 LET optval = substitute(optval, ' ', '\\ ', 'g')
1454 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001455 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001456 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001457 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001458 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001459 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001460 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001461
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001462 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001463 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001464 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001465 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001466 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001467 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001468 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001469 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001470
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001471 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001472 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001473 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001474 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001475 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001476 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001477 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001478 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001479
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001480 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001481 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001482 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001483 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001484 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001486 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001487 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001488 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001489
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001490 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001491 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001492 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001493 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001494 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001495 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001496 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001497 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001498 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001499
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001500 #" Test for using a lambda function with incorrect return value
1501 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1502 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001503 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001504 call setline(1, 'eight')
1505 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1506 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001507
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001508 #" Test for clearing the 'completefunc' option
1509 set completefunc=''
1510 set completefunc&
1511 call assert_fails("set completefunc=function('abc')", "E700:")
1512 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001513
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001514 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001515 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001516 call setline(1, 'five')
1517 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1518 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001519 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001520 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001521 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001522 bw!
1523 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001524 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001525
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001526 " Test for using a script-local function name
1527 func s:CompleteFunc3(findstart, base)
1528 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1529 return a:findstart ? 0 : []
1530 endfunc
1531 set completefunc=s:CompleteFunc3
1532 new
1533 call setline(1, 'script1')
1534 let g:CompleteFunc3Args = []
1535 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1536 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1537 bw!
1538
1539 let &completefunc = 's:CompleteFunc3'
1540 new
1541 call setline(1, 'script2')
1542 let g:CompleteFunc3Args = []
1543 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1544 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1545 bw!
1546 delfunc s:CompleteFunc3
1547
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001548 " In Vim9 script s: can be omitted
1549 let lines =<< trim END
1550 vim9script
1551 var CompleteFunc4Args = []
1552 def CompleteFunc4(findstart: bool, base: string): any
1553 add(CompleteFunc4Args, [findstart, base])
1554 return findstart ? 0 : []
1555 enddef
1556 set completefunc=CompleteFunc4
1557 new
1558 setline(1, 'script1')
1559 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1560 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1561 bw!
1562 END
1563 call v9.CheckScriptSuccess(lines)
1564
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001565 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001566 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001567 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1568
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001569 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001570 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001571 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001572 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001573 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001574 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001575
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001576 " set 'completefunc' to a partial with dict. This used to cause a crash.
1577 func SetCompleteFunc()
1578 let params = {'complete': function('g:DictCompleteFunc')}
1579 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001580 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001581 func g:DictCompleteFunc(_) dict
1582 endfunc
1583 call SetCompleteFunc()
1584 new
1585 call SetCompleteFunc()
1586 bw
1587 call test_garbagecollect_now()
1588 new
1589 set completefunc=
1590 wincmd w
1591 set completefunc=
1592 %bw!
1593 delfunc g:DictCompleteFunc
1594 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001595
1596 " Vim9 tests
1597 let lines =<< trim END
1598 vim9script
1599
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001600 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1601 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001602 return findstart ? 0 : []
1603 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001604
1605 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001606 set completefunc=function('Vim9CompleteFunc',\ [60])
1607 new | only
1608 setline(1, 'one')
1609 g:Vim9completeFuncArgs = []
1610 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1611 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1612 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001613
1614 # Test for using a global function name
1615 &completefunc = g:CompleteFunc2
1616 new | only
1617 setline(1, 'two')
1618 g:CompleteFunc2Args = []
1619 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1620 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1621 bw!
1622
1623 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001624 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001625 add(g:LocalCompleteFuncArgs, [findstart, base])
1626 return findstart ? 0 : []
1627 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001628 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001629 new | only
1630 setline(1, 'three')
1631 g:LocalCompleteFuncArgs = []
1632 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1633 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1634 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001635 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001636 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001637
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001638 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001639 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001640 delfunc CompleteFunc1
1641 delfunc CompleteFunc2
1642 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001643 %bw!
1644endfunc
1645
1646" Test for different ways of setting the 'omnifunc' option
1647func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 func OmniFunc1(callnr, findstart, base)
1649 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1650 return a:findstart ? 0 : []
1651 endfunc
1652 func OmniFunc2(findstart, base)
1653 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001654 return a:findstart ? 0 : []
1655 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001656
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001658 #" Test for using a function name
1659 LET &omnifunc = 'g:OmniFunc2'
1660 new
1661 call setline(1, 'zero')
1662 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001663 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001664 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1665 bw!
1666
1667 #" Test for using a function()
1668 set omnifunc=function('g:OmniFunc1',\ [10])
1669 new
1670 call setline(1, 'one')
1671 LET g:OmniFunc1Args = []
1672 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1673 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001674 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001675
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001676 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001677 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001679 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001681 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001683 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001685
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001686 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001687 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001688 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001689 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001690 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001691 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001692 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001693 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001694 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001695
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001696 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001697 set omnifunc=funcref('g:OmniFunc1',\ [13])
1698 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001699 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001700 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001701 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001702 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001703 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001704
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001705 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001706 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001707 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001708 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001709 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001710 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001711 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001712 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001713 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001714
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001715 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001716 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001717 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001718 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001719 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001720 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001721 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001722 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001723 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001724
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001725 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001726 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001727 LET optval = substitute(optval, ' ', '\\ ', 'g')
1728 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001729 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001730 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001731 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001732 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001733 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001734 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001735
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001736 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001737 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001738 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001739 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001740 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001741 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001742 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001743 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001744
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001745 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001746 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001747 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001748 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001749 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001750 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001751 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001752 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001753
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001754 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001755 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001756 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001757 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001758 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001759 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001760 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001761 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001762 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001763
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001764 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001765 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001766 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001767 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001768 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001769 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001770 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001771 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001772 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001773
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001774 #" Test for using a lambda function with incorrect return value
1775 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1776 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001777 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001778 call setline(1, 'eight')
1779 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1780 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001781
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001782 #" Test for clearing the 'omnifunc' option
1783 set omnifunc=''
1784 set omnifunc&
1785 call assert_fails("set omnifunc=function('abc')", "E700:")
1786 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001787
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001788 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001789 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001790 call setline(1, 'nine')
1791 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1792 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001793 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001794 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001795 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001796 bw!
1797 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001798 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001799
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001800 " Test for using a script-local function name
1801 func s:OmniFunc3(findstart, base)
1802 call add(g:OmniFunc3Args, [a:findstart, a:base])
1803 return a:findstart ? 0 : []
1804 endfunc
1805 set omnifunc=s:OmniFunc3
1806 new
1807 call setline(1, 'script1')
1808 let g:OmniFunc3Args = []
1809 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1810 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1811 bw!
1812
1813 let &omnifunc = 's:OmniFunc3'
1814 new
1815 call setline(1, 'script2')
1816 let g:OmniFunc3Args = []
1817 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1818 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1819 bw!
1820 delfunc s:OmniFunc3
1821
1822 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001823 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001824 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1825
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001826 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001827 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001828 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001829 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001830 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001831 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001832
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001833 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1834 func SetOmniFunc()
1835 let params = {'omni': function('g:DictOmniFunc')}
1836 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001837 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001838 func g:DictOmniFunc(_) dict
1839 endfunc
1840 call SetOmniFunc()
1841 new
1842 call SetOmniFunc()
1843 bw
1844 call test_garbagecollect_now()
1845 new
1846 set omnifunc=
1847 wincmd w
1848 set omnifunc=
1849 %bw!
1850 delfunc g:DictOmniFunc
1851 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001852
1853 " Vim9 tests
1854 let lines =<< trim END
1855 vim9script
1856
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001857 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1858 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001859 return findstart ? 0 : []
1860 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001861
1862 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001863 set omnifunc=function('Vim9omniFunc',\ [60])
1864 new | only
1865 setline(1, 'one')
1866 g:Vim9omniFunc_Args = []
1867 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1868 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1869 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001870
1871 # Test for using a global function name
1872 &omnifunc = g:OmniFunc2
1873 new | only
1874 setline(1, 'two')
1875 g:OmniFunc2Args = []
1876 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1877 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1878 bw!
1879
1880 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001881 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001882 add(g:LocalOmniFuncArgs, [findstart, base])
1883 return findstart ? 0 : []
1884 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001885 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001886 new | only
1887 setline(1, 'three')
1888 g:LocalOmniFuncArgs = []
1889 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1890 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1891 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001892 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001893 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001894
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001895 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001896 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001897 delfunc OmniFunc1
1898 delfunc OmniFunc2
1899 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001900 %bw!
1901endfunc
1902
1903" Test for different ways of setting the 'thesaurusfunc' option
1904func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001905 func TsrFunc1(callnr, findstart, base)
1906 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001907 return a:findstart ? 0 : []
1908 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001909 func TsrFunc2(findstart, base)
1910 call add(g:TsrFunc2Args, [a:findstart, a:base])
1911 return a:findstart ? 0 : ['sunday']
1912 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001913
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001915 #" Test for using a function name
1916 LET &thesaurusfunc = 'g:TsrFunc2'
1917 new
1918 call setline(1, 'zero')
1919 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001920 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001921 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1922 bw!
1923
1924 #" Test for using a function()
1925 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1926 new
1927 call setline(1, 'one')
1928 LET g:TsrFunc1Args = []
1929 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1930 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001931 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001932
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001933 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001934 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001936 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001937 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001938 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001939 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001940 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001942
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001943 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001944 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001945 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001946 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001947 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001948 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001949 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001950 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001952
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001953 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001954 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1955 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001956 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001957 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001958 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001959 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001960 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001961
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001962 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001963 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001964 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001965 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001966 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001967 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001968 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001969 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001970 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001971
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001972 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001973 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001974 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001975 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001976 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001977 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001978 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001979 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001980 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001981
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001982 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001983 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001984 LET optval = substitute(optval, ' ', '\\ ', 'g')
1985 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001986 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001987 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001988 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001989 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001990 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001991 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001992
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001993 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001994 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001995 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001996 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001997 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001998 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001999 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002000 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002001
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002002 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002003 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002004 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002005 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002006 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002007 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002008 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002009 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002010
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002011 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00002012 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002013 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002014 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002015 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002016 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002017 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002018 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002019 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002020
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002021 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002022 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002023 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002024 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002025 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002026 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002027 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002028 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002029 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002030
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002031 #" Test for using a lambda function with incorrect return value
2032 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2033 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002034 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002035 call setline(1, 'eight')
2036 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2037 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002038
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002039 #" Test for clearing the 'thesaurusfunc' option
2040 set thesaurusfunc=''
2041 set thesaurusfunc&
2042 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
2043 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002044
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002045 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002046 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002047 call setline(1, 'ten')
2048 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
2049 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002050 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002051 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002052 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002053 bw!
2054
2055 #" Use a buffer-local value and a global value
2056 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002057 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002058 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002059 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002060 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2061 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002062 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002063 new
2064 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002065 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002066 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2067 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002068 call assert_equal([], g:TsrFunc1Args)
2069 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002070 wincmd w
2071 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002072 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002073 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2074 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002075 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002076 :%bw!
2077 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002078 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002079
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002080 " Test for using a script-local function name
2081 func s:TsrFunc3(findstart, base)
2082 call add(g:TsrFunc3Args, [a:findstart, a:base])
2083 return a:findstart ? 0 : []
2084 endfunc
2085 set tsrfu=s:TsrFunc3
2086 new
2087 call setline(1, 'script1')
2088 let g:TsrFunc3Args = []
2089 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2090 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2091 bw!
2092
2093 let &tsrfu = 's:TsrFunc3'
2094 new
2095 call setline(1, 'script2')
2096 let g:TsrFunc3Args = []
2097 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2098 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2099 bw!
2100 delfunc s:TsrFunc3
2101
2102 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002103 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002104 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2105
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002106 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002107 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002108 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002109 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002110 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002111 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002112 bw!
2113
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002114 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2115 func SetTsrFunc()
2116 let params = {'thesaurus': function('g:DictTsrFunc')}
2117 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002118 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002119 func g:DictTsrFunc(_) dict
2120 endfunc
2121 call SetTsrFunc()
2122 new
2123 call SetTsrFunc()
2124 bw
2125 call test_garbagecollect_now()
2126 new
2127 set thesaurusfunc=
2128 wincmd w
2129 %bw!
2130 delfunc SetTsrFunc
2131
2132 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2133 " cause a crash.
2134 func SetLocalTsrFunc()
2135 let params = {'thesaurus': function('g:DictTsrFunc')}
2136 let &l:thesaurusfunc = params.thesaurus
2137 endfunc
2138 call SetLocalTsrFunc()
2139 call test_garbagecollect_now()
2140 call SetLocalTsrFunc()
2141 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002142 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002143 delfunc g:DictTsrFunc
2144 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002145
2146 " Vim9 tests
2147 let lines =<< trim END
2148 vim9script
2149
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002150 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2151 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002152 return findstart ? 0 : []
2153 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002154
2155 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002156 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2157 new | only
2158 setline(1, 'one')
2159 g:Vim9tsrFunc_Args = []
2160 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2161 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2162 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002163
2164 # Test for using a global function name
2165 &thesaurusfunc = g:TsrFunc2
2166 new | only
2167 setline(1, 'two')
2168 g:TsrFunc2Args = []
2169 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2170 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2171 bw!
2172
2173 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002174 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002175 add(g:LocalTsrFuncArgs, [findstart, base])
2176 return findstart ? 0 : []
2177 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002178 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002179 new | only
2180 setline(1, 'three')
2181 g:LocalTsrFuncArgs = []
2182 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2183 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2184 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002185 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002186 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002187
2188 " cleanup
2189 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002190 delfunc TsrFunc1
2191 delfunc TsrFunc2
2192 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002193 %bw!
2194endfunc
2195
Christian Brabandtac72c212022-04-07 21:00:53 +01002196func FooBarComplete(findstart, base)
2197 if a:findstart
2198 return col('.') - 1
2199 else
2200 return ["Foo", "Bar", "}"]
2201 endif
2202endfunc
2203
2204func Test_complete_smartindent()
2205 new
2206 setlocal smartindent completefunc=FooBarComplete
2207
2208 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2209 let result = getline(1,'$')
2210 call assert_equal(['', '{','}',''], result)
2211 bw!
2212 delfunction! FooBarComplete
2213endfunc
2214
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002215func Test_complete_overrun()
2216 " this was going past the end of the copied text
2217 new
2218 sil norm si”0s0 
2219 bwipe!
2220endfunc
2221
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002222func Test_infercase_very_long_line()
2223 " this was truncating the line when inferring case
2224 new
2225 let longLine = "blah "->repeat(300)
2226 let verylongLine = "blah "->repeat(400)
2227 call setline(1, verylongLine)
2228 call setline(2, longLine)
2229 set ic infercase
2230 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2231 call assert_equal(longLine, getline(3))
2232
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002233 " check that the too long text is NUL terminated
2234 %del
2235 norm o
2236 norm 1987ax
2237 exec "norm ox\<C-X>\<C-L>"
2238 call assert_equal(repeat('x', 1987), getline(3))
2239
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002240 bwipe!
2241 set noic noinfercase
2242endfunc
2243
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002244func Test_ins_complete_add()
2245 " this was reading past the end of allocated memory
2246 new
2247 norm o
2248 norm 7o€€
2249 sil! norm o
2250
2251 bwipe!
2252endfunc
2253
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002254func Test_ins_complete_end_of_line()
2255 " this was reading past the end of the line
Bram Moolenaar94722c52023-01-28 19:19:03 +00002256 new
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002257 norm 8o€ý 
2258 sil! norm o
2259
2260 bwipe!
2261endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002262
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002263func s:Tagfunc(t,f,o)
2264 bwipe!
2265 return []
2266endfunc
2267
2268" This was using freed memory, since 'complete' was in a wiped out buffer.
2269" Also using a window that was closed.
2270func Test_tagfunc_wipes_out_buffer()
2271 new
2272 set complete=.,t,w,b,u,i
2273 se tagfunc=s:Tagfunc
2274 sil norm i
2275
2276 bwipe!
2277endfunc
2278
Bram Moolenaar6ac2e432023-03-31 19:32:29 +01002279func Test_ins_complete_popup_position()
2280 CheckScreendump
2281
2282 let lines =<< trim END
2283 vim9script
2284 set nowrap
2285 setline(1, ['one', 'two', 'this is line ', 'four'])
2286 prop_type_add('test', {highlight: 'Error'})
2287 prop_add(3, 0, {
2288 text_align: 'above',
2289 text: 'The quick brown fox jumps over the lazy dog',
2290 type: 'test'
2291 })
2292 END
2293 call writefile(lines, 'XinsPopup', 'D')
2294 let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
2295
2296 call term_sendkeys(buf, "3GA\<C-N>")
2297 call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
2298
2299 call StopVimInTerminal(buf)
2300endfunc
2301
LemonBoy69fb5af2023-10-11 21:55:56 +02002302func GetCompleteInfo()
2303 let g:compl_info = complete_info()
2304 return ''
2305endfunc
2306
nwounkn2e3cd522023-10-17 11:05:38 +02002307func Test_completion_restart()
2308 new
2309 set complete=. completeopt=menuone backspace=2
2310 call setline(1, 'workhorse workhorse')
2311 exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
2312 call assert_equal(1, len(g:compl_info['items']))
2313 call assert_equal('workhorse', g:compl_info['items'][0]['word'])
2314 set complete& completeopt& backspace&
2315 bwipe!
2316endfunc
2317
LemonBoy69fb5af2023-10-11 21:55:56 +02002318func Test_complete_info_index()
2319 new
2320 call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
2321 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2322
2323 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2324
2325 set completeopt=menu,preview
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002326 " Search forward
LemonBoy69fb5af2023-10-11 21:55:56 +02002327 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2328 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002329 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002330 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2331 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002332 call assert_equal(6 , len(g:compl_info['items']))
2333 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2334 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2335 call assert_equal(6 , len(g:compl_info['items']))
2336 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2337 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2338 call assert_equal(6 , len(g:compl_info['items']))
2339 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2340 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2341 call assert_equal(6 , len(g:compl_info['items']))
2342 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2343 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2344 call assert_equal(6 , len(g:compl_info['items']))
2345 " Search forward: unselected item
2346 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2347 call assert_equal(6 , len(g:compl_info['items']))
2348 call assert_equal(-1 , g:compl_info['selected'])
LemonBoy69fb5af2023-10-11 21:55:56 +02002349
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002350 " Search backward
LemonBoy69fb5af2023-10-11 21:55:56 +02002351 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2352 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002353 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002354 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2355 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002356 call assert_equal(6 , len(g:compl_info['items']))
2357 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2358 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2359 call assert_equal(6 , len(g:compl_info['items']))
2360 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2361 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2362 call assert_equal(6 , len(g:compl_info['items']))
2363 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2364 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2365 call assert_equal(6 , len(g:compl_info['items']))
2366 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2367 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2368 call assert_equal(6 , len(g:compl_info['items']))
2369 " search backwards: unselected item
2370 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2371 call assert_equal(6 , len(g:compl_info['items']))
2372 call assert_equal(-1 , g:compl_info['selected'])
2373
2374 " switch direction: forwards, then backwards
2375 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2376 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2377 call assert_equal(6 , len(g:compl_info['items']))
2378 " switch direction: forwards, then backwards, then forwards again
2379 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2380 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2381 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2382 call assert_equal(6 , len(g:compl_info['items']))
2383
2384 " switch direction: backwards, then forwards
2385 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2386 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2387 call assert_equal(6 , len(g:compl_info['items']))
2388 " switch direction: backwards, then forwards, then backwards again
2389 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2390 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2391 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002392
2393 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2394 set completeopt+=noselect
2395 " Search forward.
2396 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2397 call assert_equal(-1, g:compl_info['selected'])
2398
2399 " Search backward.
2400 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2401 call assert_equal(-1, g:compl_info['selected'])
2402
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002403 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
Girish Palya8950bf72024-03-20 20:07:29 +01002404 call assert_equal(5, g:compl_info['selected'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002405 call assert_equal(6 , len(g:compl_info['items']))
2406 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2407 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
Christian Brabandt87ca5e82023-10-27 19:47:29 +02002408 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002409 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandt57f9ce12023-11-04 09:58:14 +01002410 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2411 call assert_equal(-1, g:compl_info['selected'])
2412 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002413
LemonBoy69fb5af2023-10-11 21:55:56 +02002414 set completeopt&
2415 bwipe!
2416endfunc
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002417
Christian Brabandtfef66302024-01-29 21:46:58 +01002418func Test_complete_changed_complete_info()
2419 CheckRunVimInTerminal
2420 " this used to crash vim, see #13929
2421 let lines =<< trim END
2422 set completeopt=menuone
2423 autocmd CompleteChanged * call complete_info(['items'])
2424 call feedkeys("iii\<cr>\<c-p>")
2425 END
2426 call writefile(lines, 'Xsegfault', 'D')
2427 let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
2428 call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
2429 call StopVimInTerminal(buf)
2430endfunc
2431
2432" vim: shiftwidth=2 sts=2 expandtab nofoldenable