blob: 1b6eafe8f015120c53ac3de5b6366ea9b34cf119 [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
zeertzjq440d4cb2023-03-02 17:51:32 +0000415" Test that mouse scrolling/movement should not interrupt completion.
416func Test_mouse_scroll_move_during_completion()
417 new
418 com! -buffer TestCommand1 echo 'TestCommand1'
419 com! -buffer TestCommand2 echo 'TestCommand2'
420 call setline(1, ['', '', '', '', ''])
421 call cursor(5, 1)
422
423 " Without completion menu scrolling can move text.
424 set completeopt-=menu wrap
425 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
426 call assert_equal('TestCommand2', getline('.'))
427 call assert_notequal(1, winsaveview().topline)
428 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
429 call assert_equal('TestCommand2', getline('.'))
430 call assert_equal(1, winsaveview().topline)
431 set nowrap
432 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
433 call assert_equal('TestCommand2', getline('.'))
434 call assert_notequal(0, winsaveview().leftcol)
435 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
436 call assert_equal('TestCommand2', getline('.'))
437 call assert_equal(0, winsaveview().leftcol)
438 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
439 call assert_equal('TestCommand2', getline('.'))
440
441 " With completion menu scrolling cannot move text.
442 set completeopt+=menu wrap
443 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
444 call assert_equal('TestCommand2', getline('.'))
445 call assert_equal(1, winsaveview().topline)
446 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
447 call assert_equal('TestCommand2', getline('.'))
448 call assert_equal(1, winsaveview().topline)
449 set nowrap
450 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
451 call assert_equal('TestCommand2', getline('.'))
452 call assert_equal(0, winsaveview().leftcol)
453 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
454 call assert_equal('TestCommand2', getline('.'))
455 call assert_equal(0, winsaveview().leftcol)
456 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
457 call assert_equal('TestCommand2', getline('.'))
458
459 bwipe!
460 set completeopt& wrap&
461endfunc
462
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100463" Check that when using feedkeys() typeahead does not interrupt searching for
464" completions.
465func Test_compl_feedkeys()
466 new
467 set completeopt=menuone,noselect
468 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
469 call assert_equal("jump jump", getline(1))
470 bwipe!
471 set completeopt&
472endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200473
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200474" Test for insert path completion with completeslash option
475func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200476 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200477
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100478 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200479 let orig_shellslash = &shellslash
480 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200481 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200482
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200483 set noshellslash
484
485 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100486 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100487 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200488
489 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100490 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100491 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200492
493 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100494 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100495 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200496
497 set shellslash
498
499 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100500 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100501 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200502
503 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100504 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100505 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200506
507 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100508 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100509 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200510 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200511
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200512 set noshellslash
513 set completeslash=slash
514 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
515
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200516 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200517 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200518endfunc
519
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200520func Test_pum_stopped_by_timer()
521 CheckScreendump
522
523 let lines =<< trim END
524 call setline(1, ['hello', 'hullo', 'heeee', ''])
525 func StartCompl()
526 call timer_start(100, { -> execute('stopinsert') })
527 call feedkeys("Gah\<C-N>")
528 endfunc
529 END
530
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100531 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200532 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
533 call term_sendkeys(buf, ":call StartCompl()\<CR>")
534 call TermWait(buf, 200)
535 call term_sendkeys(buf, "k")
536 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
537
538 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200539endfunc
540
zeertzjqcd5dbad2022-05-04 17:51:50 +0100541func Test_complete_stopinsert_startinsert()
542 nnoremap <F2> <Cmd>startinsert<CR>
543 inoremap <F2> <Cmd>stopinsert<CR>
544 " This just checks if this causes an error
545 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
546 nunmap <F2>
547 iunmap <F2>
548endfunc
549
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100550func Test_pum_with_folds_two_tabs()
551 CheckScreendump
552
553 let lines =<< trim END
554 set fdm=marker
555 call setline(1, ['" x {{{1', '" a some text'])
556 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
557 norm! zm
558 tab sp
559 call feedkeys('2Gzv', 'xt')
560 call feedkeys("0fa", 'xt')
561 END
562
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100563 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100564 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200565 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100566 call term_sendkeys(buf, "a\<C-N>")
567 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
568
569 call term_sendkeys(buf, "\<Esc>")
570 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100571endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100572
573func Test_pum_with_preview_win()
574 CheckScreendump
575
576 let lines =<< trim END
577 funct Omni_test(findstart, base)
578 if a:findstart
579 return col(".") - 1
580 endif
581 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
582 endfunc
583 set omnifunc=Omni_test
584 set completeopt+=longest
585 END
586
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100587 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100588 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100589 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100590 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100591 call term_sendkeys(buf, "\<C-N>")
592 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
593
594 call term_sendkeys(buf, "\<Esc>")
595 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100596endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100597
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000598func Test_scrollbar_on_wide_char()
599 CheckScreendump
600
601 let lines =<< trim END
602 call setline(1, ['a', ' 啊啊啊',
603 \ ' 哦哦哦',
604 \ ' 呃呃呃'])
605 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
606 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100607 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000608 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
609 call term_sendkeys(buf, "A\<C-N>")
610 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
611
612 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000613endfunc
614
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100615" Test for inserting the tag search pattern in insert mode
616func Test_ins_compl_tag_sft()
617 call writefile([
618 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
619 \ "first\tXfoo\t/^int first() {}$/",
620 \ "second\tXfoo\t/^int second() {}$/",
621 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100622 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100623 set tags=Xtags
624 let code =<< trim [CODE]
625 int first() {}
626 int second() {}
627 int third() {}
628 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100629 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100630
631 enew
632 set showfulltag
633 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
634 call assert_equal('int second() {}', getline(1))
635 set noshowfulltag
636
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100637 set tags&
638 %bwipe!
639endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200640
641" Test for 'completefunc' deleting text
642func Test_completefunc_error()
643 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200644 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200645 func CompleteFunc(findstart, base)
646 if a:findstart == 1
647 normal dd
648 return col('.') - 1
649 endif
650 return ['a', 'b']
651 endfunc
652 set completefunc=CompleteFunc
653 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100654 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200655
656 " delete text when called for the second time
657 func CompleteFunc2(findstart, base)
658 if a:findstart == 1
659 return col('.') - 1
660 endif
661 normal dd
662 return ['a', 'b']
663 endfunc
664 set completefunc=CompleteFunc2
665 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100666 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200667
Bram Moolenaar97202d92021-01-28 18:34:35 +0100668 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100669 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100670 if a:findstart == 1
671 return col('.') - 1
672 endif
673 wincmd p
674 return ['a', 'b']
675 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100676 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100677 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100678 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100679 close!
680
681 set completefunc&
682 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100683 delfunc CompleteFunc2
684 delfunc CompleteFunc3
685 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100686endfunc
687
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200688" Test for returning non-string values from 'completefunc'
689func Test_completefunc_invalid_data()
690 new
691 func! CompleteFunc(findstart, base)
692 if a:findstart == 1
693 return col('.') - 1
694 endif
695 return [{}, '', 'moon']
696 endfunc
697 set completefunc=CompleteFunc
698 exe "normal i\<C-X>\<C-U>"
699 call assert_equal('moon', getline(1))
700 set completefunc&
701 close!
702endfunc
703
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200704" Test for errors in using complete() function
705func Test_complete_func_error()
706 call assert_fails('call complete(1, ["a"])', 'E785:')
707 func ListColors()
708 call complete(col('.'), "blue")
709 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100710 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200711 func ListMonths()
712 call complete(col('.'), test_null_list())
713 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100714 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200715 delfunc ListColors
716 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100717 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200718 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200719endfunc
720
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000721" Test for recursively starting completion mode using complete()
722func Test_recursive_complete_func()
723 func ListColors()
724 call complete(5, ["red", "blue"])
725 return ''
726 endfunc
727 new
728 call setline(1, ['a1', 'a2'])
729 set complete=.
730 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
731 call assert_equal('a2blue', getline(3))
732 delfunc ListColors
733 bw!
734endfunc
735
bfredl87af60c2022-09-24 11:17:51 +0100736" Test for using complete() with completeopt+=longest
737func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100738 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100739 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100740
741 " default: insert first match
742 set completeopt&
743 call setline(1, ['i'])
744 exe "normal Aa\<f3>\<esc>"
745 call assert_equal('iaax', getline(1))
746
747 " with longest: insert longest prefix
748 set completeopt+=longest
749 call setline(1, ['i'])
750 exe "normal Aa\<f3>\<esc>"
751 call assert_equal('iaa', getline(1))
752 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100753 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100754endfunc
755
756
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200757" Test for completing words following a completed word in a line
758func Test_complete_wrapscan()
759 " complete words from another buffer
760 new
761 call setline(1, ['one two', 'three four'])
762 new
763 setlocal complete=w
764 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
765 call assert_equal('two three four', getline(1))
766 close!
767 " complete words from the current buffer
768 setlocal complete=.
769 %d
770 call setline(1, ['one two', ''])
771 call cursor(2, 1)
772 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
773 call assert_equal('one two one two', getline(2))
774 close!
775endfunc
776
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200777" Test for completing special characters
778func Test_complete_special_chars()
779 new
780 call setline(1, 'int .*[-\^$ func float')
781 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
782 call assert_equal('int .*[-\^$ func float', getline(2))
783 close!
784endfunc
785
786" Test for completion when text is wrapped across lines.
787func Test_complete_across_line()
788 new
789 call setline(1, ['red green blue', 'one two three'])
790 setlocal textwidth=20
791 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
792 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
793 close!
794endfunc
795
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000796" Test for completing words with a '.' at the end of a word.
797func Test_complete_joinspaces()
798 new
799 call setline(1, ['one two.', 'three. four'])
800 set joinspaces
801 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
802 call assert_equal("one two. three. four", getline(3))
803 set joinspaces&
804 bw!
805endfunc
806
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200807" Test for using CTRL-L to add one character when completing matching
808func Test_complete_add_onechar()
809 new
810 call setline(1, ['wool', 'woodwork'])
811 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
812 call assert_equal('woof', getline(3))
813
814 " use 'ignorecase' and backspace to erase characters from the prefix string
815 " and then add letters using CTRL-L
816 %d
817 set ignorecase backspace=2
818 setlocal complete=.
819 call setline(1, ['workhorse', 'workload'])
820 normal Go
nwounkn2e3cd522023-10-17 11:05:38 +0200821 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200822 call assert_equal('workh', getline(3))
823 set ignorecase& backspace&
824 close!
825endfunc
826
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000827" Test for using CTRL-X CTRL-L to complete whole lines lines
828func Test_complete_wholeline()
829 new
830 " complete one-line
831 call setline(1, ['a1', 'a2'])
832 exe "normal ggoa\<C-X>\<C-L>"
833 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
834 " go to the next match (wrapping around the buffer)
835 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
836 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
837 " go to the next match
838 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
839 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
840 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
841 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
842 " repeat the test using CTRL-L
843 " go to the next match (wrapping around the buffer)
844 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
845 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
846 " go to the next match
847 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
848 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
849 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
850 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
851 %d
852 " use CTRL-X CTRL-L to add one more line
853 call setline(1, ['a1', 'b1'])
854 setlocal complete=.
855 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
856 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
857 bw!
858endfunc
859
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200860" Test insert completion with 'cindent' (adjust the indent)
861func Test_complete_with_cindent()
862 new
863 setlocal cindent
864 call setline(1, ['if (i == 1)', " j = 2;"])
865 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
866 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
867
868 %d
869 call setline(1, ['when while', '{', ''])
870 setlocal cinkeys+==while
871 exe "normal Giwh\<C-P> "
872 call assert_equal("\twhile ", getline('$'))
873 close!
874endfunc
875
876" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
877func Test_complete_cmdline()
878 new
879 exe "normal icaddb\<C-X>\<C-V>"
880 call assert_equal('caddbuffer', getline(1))
881 exe "normal ocall getqf\<C-X>\<C-V>"
882 call assert_equal('call getqflist(', getline(2))
883 exe "normal oabcxyz(\<C-X>\<C-V>"
884 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200885 com! -buffer TestCommand1 echo 'TestCommand1'
886 com! -buffer TestCommand2 echo 'TestCommand2'
887 write TestCommand1Test
888 write TestCommand2Test
889 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
890 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
891 call assert_equal('TestCommand2Test', getline(4))
892 call delete('TestCommand1Test')
893 call delete('TestCommand2Test')
894 delcom TestCommand1
895 delcom TestCommand2
896 close!
897endfunc
898
899" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
900func Test_complete_stop()
901 new
902 func Save_mode1()
903 let g:mode1 = mode(1)
904 return ''
905 endfunc
906 func Save_mode2()
907 let g:mode2 = mode(1)
908 return ''
909 endfunc
910 inoremap <F1> <C-R>=Save_mode1()<CR>
911 inoremap <F2> <C-R>=Save_mode2()<CR>
912 call setline(1, ['aaa bbb ccc '])
913 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
914 call assert_equal('ic', g:mode1)
915 call assert_equal('i', g:mode2)
916 call assert_equal('aaa bbb ccc ', getline(1))
917 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
918 call assert_equal('ic', g:mode1)
919 call assert_equal('i', g:mode2)
920 call assert_equal('aaa bbb ccc aaa', getline(1))
921 set completeopt+=noselect
922 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
923 call assert_equal('ic', g:mode1)
924 call assert_equal('i', g:mode2)
925 call assert_equal('aaa bbb ccc aaa bb', getline(1))
926 set completeopt&
927 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
928 call assert_equal('ic', g:mode1)
929 call assert_equal('i', g:mode2)
930 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
931 com! -buffer TestCommand1 echo 'TestCommand1'
932 com! -buffer TestCommand2 echo 'TestCommand2'
933 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
934 call assert_equal('ic', g:mode1)
935 call assert_equal('i', g:mode2)
936 call assert_equal('TestCommand2', getline(2))
937 delcom TestCommand1
938 delcom TestCommand2
939 unlet g:mode1
940 unlet g:mode2
941 iunmap <F1>
942 iunmap <F2>
943 delfunc Save_mode1
944 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200945 close!
946endfunc
947
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000948" Test for typing CTRL-R in insert completion mode to insert a register
949" content.
950func Test_complete_reginsert()
951 new
952 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
953
954 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
955 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
956 " inserted.
957 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
958 call assert_equal('a123', getline(5))
959 let @r = "\<C-P>\<C-P>"
960 exe "normal GCa\<C-P>\<C-R>r"
961 call assert_equal('a12', getline(5))
962 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
963 call assert_equal('a1234x', getline(5))
964 bw!
965endfunc
966
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200967func Test_issue_7021()
968 CheckMSWindows
969
970 let orig_shellslash = &shellslash
971 set noshellslash
972
973 set completeslash=slash
974 call assert_false(expand('~') =~ '/')
975
976 let &shellslash = orig_shellslash
977 set completeslash=
978endfunc
979
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000980" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
981func Test_complete_longest_match()
982 for e in ['latin1', 'utf-8']
983 exe 'set encoding=' .. e
984 new
985 set complete=.
986 set completeopt=menu,longest
987 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
988 exe "normal Gopfx\<C-P>"
989 call assert_equal('pfx_', getline(5))
990 bw!
991 endfor
992
993 " Test for completing additional words with longest match set
994 new
995 call setline(1, ['abc1', 'abd2'])
996 exe "normal Goab\<C-P>\<C-X>\<C-P>"
997 call assert_equal('ab', getline(3))
998 bw!
999 set complete& completeopt&
1000endfunc
1001
1002" Test for removing the first displayed completion match and selecting the
1003" match just before that.
1004func Test_complete_erase_firstmatch()
1005 new
1006 call setline(1, ['a12', 'a34', 'a56'])
1007 set complete=.
1008 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1009 call assert_equal('a34', getline('$'))
1010 set complete&
1011 bw!
1012endfunc
1013
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001014" Test for completing words from unloaded buffers
1015func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001016 call writefile(['abc'], "Xfile1", 'D')
1017 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001018 edit Xfile1
1019 edit Xfile2
1020 new | close
1021 enew
1022 bunload Xfile1 Xfile2
1023 set complete=u
1024 " complete from an unloaded buffer
1025 exe "normal! ia\<C-P>"
1026 call assert_equal('abc', getline(1))
1027 exe "normal! od\<C-P>"
1028 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001029
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001030 set complete&
1031 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001032endfunc
1033
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001034" Test for completing whole lines from unloaded buffers
1035func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001036 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001037 edit Xfile1
1038 enew
1039 set complete=u
1040 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1041 call assert_equal('a line2', getline(1))
1042 %d
1043 " completing from an unlisted buffer should fail
1044 bdel Xfile1
1045 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1046 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001047
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001048 set complete&
1049 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001050endfunc
1051
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001052" Test for completing words from unlisted buffers
1053func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001054 call writefile(['abc'], "Xfile1", 'D')
1055 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001056 edit Xfile1
1057 edit Xfile2
1058 new | close
1059 bdel Xfile1 Xfile2
1060 set complete=U
1061 " complete from an unlisted buffer
1062 exe "normal! ia\<C-P>"
1063 call assert_equal('abc', getline(1))
1064 exe "normal! od\<C-P>"
1065 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001066
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001067 set complete&
1068 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001069endfunc
1070
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001071" Test for completing whole lines from unlisted buffers
1072func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001073 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001074 edit Xfile1
1075 enew
1076 set complete=U
1077 " completing from a unloaded buffer should fail
1078 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1079 call assert_equal('a', getline(1))
1080 %d
1081 bdel Xfile1
1082 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1083 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001084
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001085 set complete&
1086 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001087endfunc
1088
1089" Test for adding a multibyte character using CTRL-L in completion mode
1090func Test_complete_mbyte_char_add()
1091 new
1092 set complete=.
1093 call setline(1, 'abÄ—')
1094 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1095 call assert_equal('abÄ—', getline(2))
1096 " Test for a leader with multibyte character
1097 %d
1098 call setline(1, 'abÄ—Ä•')
1099 exe "normal! oabÄ—\<C-P>"
1100 call assert_equal('abÄ—Ä•', getline(2))
1101 bw!
1102endfunc
1103
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001104" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1105" not to complete matches from the local buffer. Also test using multiple
1106" <C-X> to cancel the current completion mode.
1107func Test_complete_local_expansion()
1108 new
1109 set complete=t
1110 call setline(1, ['abc', 'def'])
1111 exe "normal! Go\<C-X>\<C-P>"
1112 call assert_equal("def", getline(3))
1113 exe "normal! Go\<C-P>"
1114 call assert_equal("", getline(4))
1115 exe "normal! Go\<C-X>\<C-N>"
1116 call assert_equal("abc", getline(5))
1117 exe "normal! Go\<C-N>"
1118 call assert_equal("", getline(6))
1119
1120 " use multiple <C-X> to cancel the previous completion mode
1121 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1122 call assert_equal("", getline(7))
1123 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1124 call assert_equal("", getline(8))
1125 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1126 call assert_equal("abc", getline(9))
1127
1128 " interrupt the current completion mode
1129 set completeopt=menu,noinsert
1130 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1131 call assert_equal("abc", getline(10))
1132
1133 " when only one <C-X> is used to interrupt, do normal expansion
1134 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1135 call assert_equal("", getline(11))
1136 set completeopt&
1137
1138 " using two <C-X> in non-completion mode and restarting the same mode
1139 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1140 call assert_equal("def", getline(12))
1141
1142 " test for adding a match from the original empty text
1143 %d
1144 call setline(1, 'abc def g')
1145 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1146 call assert_equal('def', getline(2))
1147 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1148 call assert_equal('abc', getline(2))
1149
1150 bw!
1151endfunc
1152
1153" Test for undoing changes after a insert-mode completion
1154func Test_complete_undo()
1155 new
1156 set complete=.
1157 " undo with 'ignorecase'
1158 call setline(1, ['ABOVE', 'BELOW'])
1159 set ignorecase
1160 exe "normal! Goab\<C-G>u\<C-P>"
1161 call assert_equal("ABOVE", getline(3))
1162 undo
1163 call assert_equal("ab", getline(3))
1164 set ignorecase&
1165 %d
1166 " undo with longest match
1167 set completeopt=menu,longest
1168 call setline(1, ['above', 'about'])
1169 exe "normal! Goa\<C-G>u\<C-P>"
1170 call assert_equal("abo", getline(3))
1171 undo
1172 call assert_equal("a", getline(3))
1173 set completeopt&
1174 %d
1175 " undo for line completion
1176 call setline(1, ['above that change', 'below that change'])
1177 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1178 call assert_equal("above that change", getline(3))
1179 undo
1180 call assert_equal("above", getline(3))
1181
1182 bw!
1183endfunc
1184
1185" Test for completing a very long word
1186func Test_complete_long_word()
1187 set complete&
1188 new
1189 call setline(1, repeat('x', 950) .. ' one two three')
1190 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1191 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1192 %d
1193 " should fail when more than 950 characters are in a word
1194 call setline(1, repeat('x', 951) .. ' one two three')
1195 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1196 call assert_equal(repeat('x', 951), getline(2))
1197
1198 " Test for adding a very long word to an existing completion
1199 %d
1200 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1201 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1202 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1203 bw!
1204endfunc
1205
1206" Test for some fields in the complete items used by complete()
1207func Test_complete_items()
1208 func CompleteItems(idx)
1209 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1210 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1211 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1212 \ [#{user_data: 'u9'}],
1213 \ [#{word: "", user_data: 'u10'}],
1214 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1215 call complete(col('.'), items[a:idx])
1216 return ''
1217 endfunc
1218 new
1219 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1220 call assert_equal('u2', v:completed_item.user_data)
1221 call assert_equal('one', getline(1))
1222 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1223 call assert_equal('u3', v:completed_item.user_data)
1224 call assert_equal('one', getline(2))
1225 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1226 call assert_equal('', getline(3))
1227 set completeopt=menu,noinsert
1228 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1229 call assert_equal('oNE', getline(4))
1230 call assert_equal('u8', v:completed_item.user_data)
1231 set completeopt&
1232 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1233 call assert_equal('', getline(5))
1234 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1235 call assert_equal('', getline(6))
1236 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1237 call assert_equal('', getline(7))
1238 call assert_equal('u11', v:completed_item.user_data)
1239 " pass invalid argument to complete()
1240 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1241 call assert_fails('exe cmd', 'E730:')
1242 bw!
1243 delfunc CompleteItems
1244endfunc
1245
1246" Test for the "refresh" item in the dict returned by an insert completion
1247" function
1248func Test_complete_item_refresh_always()
1249 let g:CallCount = 0
1250 func! Tcomplete(findstart, base)
1251 if a:findstart
1252 " locate the start of the word
1253 let line = getline('.')
1254 let start = col('.') - 1
1255 while start > 0 && line[start - 1] =~ '\a'
1256 let start -= 1
1257 endwhile
1258 return start
1259 else
1260 let g:CallCount += 1
1261 let res = ["update1", "update12", "update123"]
1262 return #{words: res, refresh: 'always'}
1263 endif
1264 endfunc
1265 new
1266 set completeopt=menu,longest
1267 set completefunc=Tcomplete
1268 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1269 call assert_equal('up', getline(1))
1270 call assert_equal(2, g:CallCount)
1271 set completeopt&
1272 set completefunc&
1273 bw!
1274 delfunc Tcomplete
1275endfunc
1276
1277" Test for completing from a thesaurus file without read permission
1278func Test_complete_unreadable_thesaurus_file()
1279 CheckUnix
1280 CheckNotRoot
1281
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001282 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001283 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001284 new
1285 set complete=sXfile
1286 exe "normal! ia\<C-P>"
1287 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001288
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001289 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001290 set complete&
1291endfunc
1292
Bram Moolenaarcc233582020-12-12 13:32:07 +01001293" Test to ensure 'Scanning...' messages are not recorded in messages history
1294func Test_z1_complete_no_history()
1295 new
1296 messages clear
1297 let currmess = execute('messages')
1298 setlocal dictionary=README.txt
1299 exe "normal owh\<C-X>\<C-K>"
1300 exe "normal owh\<C-N>"
1301 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001302 bwipe!
1303endfunc
1304
1305" A mapping is not used for the key after CTRL-X.
1306func Test_no_mapping_for_ctrl_x_key()
1307 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001308 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001309 setlocal dictionary=README.txt
1310 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1311 call assert_equal('example ', getline(1))
1312 call assert_false(exists('was_mapped'))
1313 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001314endfunc
1315
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001316" Test for different ways of setting the 'completefunc' option
1317func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001318 func CompleteFunc1(callnr, findstart, base)
1319 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1320 return a:findstart ? 0 : []
1321 endfunc
1322 func CompleteFunc2(findstart, base)
1323 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001324 return a:findstart ? 0 : []
1325 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001326
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001327 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001328 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001329 LET &completefunc = 'g:CompleteFunc2'
1330 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001331 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001332 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001333 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001334 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 bw!
1336
1337 #" Test for using a function()
1338 set completefunc=function('g:CompleteFunc1',\ [10])
1339 new
1340 call setline(1, 'one')
1341 LET g:CompleteFunc1Args = []
1342 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1343 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001344 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001345
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001346 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001347 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001349 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001350 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001351 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001352 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001353 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001354 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001355
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001356 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001357 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001358 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001359 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001360 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001361 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001362 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001363 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001364 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001365
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001366 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001367 set completefunc=funcref('g:CompleteFunc1',\ [13])
1368 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001369 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001370 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001371 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001372 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001374
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001375 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001376 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001378 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001380 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001382 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001384
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001385 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001386 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001387 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001388 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001389 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001390 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001391 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001392 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001393 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001394
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001396 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 LET optval = substitute(optval, ' ', '\\ ', 'g')
1398 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001399 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001400 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001401 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001402 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001403 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001404 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001405
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001406 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001407 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001408 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001409 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001410 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001411 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001412 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001414
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001416 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001417 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001418 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001419 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001420 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001421 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001422 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001423
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001424 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001425 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001426 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001427 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001428 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001429 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001430 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001431 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001433
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001434 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001435 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001436 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001437 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001438 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001439 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001440 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001441 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001442 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001443
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001444 #" Test for using a lambda function with incorrect return value
1445 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1446 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001447 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001448 call setline(1, 'eight')
1449 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1450 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001451
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001452 #" Test for clearing the 'completefunc' option
1453 set completefunc=''
1454 set completefunc&
1455 call assert_fails("set completefunc=function('abc')", "E700:")
1456 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001457
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001458 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001459 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001460 call setline(1, 'five')
1461 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1462 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001463 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001464 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001465 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001466 bw!
1467 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001468 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001469
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001470 " Test for using a script-local function name
1471 func s:CompleteFunc3(findstart, base)
1472 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1473 return a:findstart ? 0 : []
1474 endfunc
1475 set completefunc=s:CompleteFunc3
1476 new
1477 call setline(1, 'script1')
1478 let g:CompleteFunc3Args = []
1479 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1480 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1481 bw!
1482
1483 let &completefunc = 's:CompleteFunc3'
1484 new
1485 call setline(1, 'script2')
1486 let g:CompleteFunc3Args = []
1487 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1488 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1489 bw!
1490 delfunc s:CompleteFunc3
1491
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001492 " In Vim9 script s: can be omitted
1493 let lines =<< trim END
1494 vim9script
1495 var CompleteFunc4Args = []
1496 def CompleteFunc4(findstart: bool, base: string): any
1497 add(CompleteFunc4Args, [findstart, base])
1498 return findstart ? 0 : []
1499 enddef
1500 set completefunc=CompleteFunc4
1501 new
1502 setline(1, 'script1')
1503 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1504 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1505 bw!
1506 END
1507 call v9.CheckScriptSuccess(lines)
1508
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001509 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001510 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001511 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1512
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001513 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001514 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001515 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001516 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001517 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001518 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001519
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001520 " set 'completefunc' to a partial with dict. This used to cause a crash.
1521 func SetCompleteFunc()
1522 let params = {'complete': function('g:DictCompleteFunc')}
1523 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001524 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001525 func g:DictCompleteFunc(_) dict
1526 endfunc
1527 call SetCompleteFunc()
1528 new
1529 call SetCompleteFunc()
1530 bw
1531 call test_garbagecollect_now()
1532 new
1533 set completefunc=
1534 wincmd w
1535 set completefunc=
1536 %bw!
1537 delfunc g:DictCompleteFunc
1538 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001539
1540 " Vim9 tests
1541 let lines =<< trim END
1542 vim9script
1543
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001544 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1545 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001546 return findstart ? 0 : []
1547 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001548
1549 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001550 set completefunc=function('Vim9CompleteFunc',\ [60])
1551 new | only
1552 setline(1, 'one')
1553 g:Vim9completeFuncArgs = []
1554 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1555 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1556 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001557
1558 # Test for using a global function name
1559 &completefunc = g:CompleteFunc2
1560 new | only
1561 setline(1, 'two')
1562 g:CompleteFunc2Args = []
1563 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1564 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1565 bw!
1566
1567 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001568 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001569 add(g:LocalCompleteFuncArgs, [findstart, base])
1570 return findstart ? 0 : []
1571 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001572 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001573 new | only
1574 setline(1, 'three')
1575 g:LocalCompleteFuncArgs = []
1576 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1577 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1578 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001579 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001580 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001581
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001582 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001583 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001584 delfunc CompleteFunc1
1585 delfunc CompleteFunc2
1586 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001587 %bw!
1588endfunc
1589
1590" Test for different ways of setting the 'omnifunc' option
1591func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001592 func OmniFunc1(callnr, findstart, base)
1593 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1594 return a:findstart ? 0 : []
1595 endfunc
1596 func OmniFunc2(findstart, base)
1597 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001598 return a:findstart ? 0 : []
1599 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001600
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001602 #" Test for using a function name
1603 LET &omnifunc = 'g:OmniFunc2'
1604 new
1605 call setline(1, 'zero')
1606 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001607 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001608 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1609 bw!
1610
1611 #" Test for using a function()
1612 set omnifunc=function('g:OmniFunc1',\ [10])
1613 new
1614 call setline(1, 'one')
1615 LET g:OmniFunc1Args = []
1616 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1617 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001619
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001620 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001621 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001623 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001629
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001631 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001632 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001633 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001635 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001637 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001639
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001641 set omnifunc=funcref('g:OmniFunc1',\ [13])
1642 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001643 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001644 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001646 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001648
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001652 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001654 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001656 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001658
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001659 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001660 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001661 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001662 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001663 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001664 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001665 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001666 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001668
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001670 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 LET optval = substitute(optval, ' ', '\\ ', 'g')
1672 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001673 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001674 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001675 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001676 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001677 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001679
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001681 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001682 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001683 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001684 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001686 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001688
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001690 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001691 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001692 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001693 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001694 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001695 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001696 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001697
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001698 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001699 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001700 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001701 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001702 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001703 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001704 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001706 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001707
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001708 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001709 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001710 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001711 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001712 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001713 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001714 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001715 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001716 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001717
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001718 #" Test for using a lambda function with incorrect return value
1719 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1720 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001721 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001722 call setline(1, 'eight')
1723 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1724 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001725
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001726 #" Test for clearing the 'omnifunc' option
1727 set omnifunc=''
1728 set omnifunc&
1729 call assert_fails("set omnifunc=function('abc')", "E700:")
1730 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001731
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001732 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001733 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001734 call setline(1, 'nine')
1735 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1736 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001737 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001738 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001739 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001740 bw!
1741 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001742 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001743
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001744 " Test for using a script-local function name
1745 func s:OmniFunc3(findstart, base)
1746 call add(g:OmniFunc3Args, [a:findstart, a:base])
1747 return a:findstart ? 0 : []
1748 endfunc
1749 set omnifunc=s:OmniFunc3
1750 new
1751 call setline(1, 'script1')
1752 let g:OmniFunc3Args = []
1753 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1754 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1755 bw!
1756
1757 let &omnifunc = 's:OmniFunc3'
1758 new
1759 call setline(1, 'script2')
1760 let g:OmniFunc3Args = []
1761 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1762 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1763 bw!
1764 delfunc s:OmniFunc3
1765
1766 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001767 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001768 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1769
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001770 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001771 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001772 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001773 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001774 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001775 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001776
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001777 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1778 func SetOmniFunc()
1779 let params = {'omni': function('g:DictOmniFunc')}
1780 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001781 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001782 func g:DictOmniFunc(_) dict
1783 endfunc
1784 call SetOmniFunc()
1785 new
1786 call SetOmniFunc()
1787 bw
1788 call test_garbagecollect_now()
1789 new
1790 set omnifunc=
1791 wincmd w
1792 set omnifunc=
1793 %bw!
1794 delfunc g:DictOmniFunc
1795 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001796
1797 " Vim9 tests
1798 let lines =<< trim END
1799 vim9script
1800
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001801 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1802 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001803 return findstart ? 0 : []
1804 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001805
1806 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001807 set omnifunc=function('Vim9omniFunc',\ [60])
1808 new | only
1809 setline(1, 'one')
1810 g:Vim9omniFunc_Args = []
1811 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1812 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1813 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001814
1815 # Test for using a global function name
1816 &omnifunc = g:OmniFunc2
1817 new | only
1818 setline(1, 'two')
1819 g:OmniFunc2Args = []
1820 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1821 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1822 bw!
1823
1824 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001825 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001826 add(g:LocalOmniFuncArgs, [findstart, base])
1827 return findstart ? 0 : []
1828 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001829 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001830 new | only
1831 setline(1, 'three')
1832 g:LocalOmniFuncArgs = []
1833 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1834 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1835 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001836 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001837 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001838
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001839 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001840 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001841 delfunc OmniFunc1
1842 delfunc OmniFunc2
1843 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001844 %bw!
1845endfunc
1846
1847" Test for different ways of setting the 'thesaurusfunc' option
1848func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001849 func TsrFunc1(callnr, findstart, base)
1850 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001851 return a:findstart ? 0 : []
1852 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001853 func TsrFunc2(findstart, base)
1854 call add(g:TsrFunc2Args, [a:findstart, a:base])
1855 return a:findstart ? 0 : ['sunday']
1856 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001857
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001858 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001859 #" Test for using a function name
1860 LET &thesaurusfunc = 'g:TsrFunc2'
1861 new
1862 call setline(1, 'zero')
1863 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001864 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001865 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1866 bw!
1867
1868 #" Test for using a function()
1869 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1870 new
1871 call setline(1, 'one')
1872 LET g:TsrFunc1Args = []
1873 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1874 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001875 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001876
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001877 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001878 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001879 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001880 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001881 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001882 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001883 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001884 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001885 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001886
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001887 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001888 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001889 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001890 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001891 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001892 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001893 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001894 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001895 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001896
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001897 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001898 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1899 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001900 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001901 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001903 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001905
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001907 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001909 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001911 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001913 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001915
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001916 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001917 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001918 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001919 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001920 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001921 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001922 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001923 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001924 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001925
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001927 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 LET optval = substitute(optval, ' ', '\\ ', 'g')
1929 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001930 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001931 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001932 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001933 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001934 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001936
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001937 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001938 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001939 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001940 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001941 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001942 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001943 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001945
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001947 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001948 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001949 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001950 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001952 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001953 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001954
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001955 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001956 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001958 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001960 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001961 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001964
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001965 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001966 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001967 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001968 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001969 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001970 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001971 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001972 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001973 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001974
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975 #" Test for using a lambda function with incorrect return value
1976 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1977 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001978 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001979 call setline(1, 'eight')
1980 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1981 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001982
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001983 #" Test for clearing the 'thesaurusfunc' option
1984 set thesaurusfunc=''
1985 set thesaurusfunc&
1986 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1987 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001988
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001989 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001990 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001991 call setline(1, 'ten')
1992 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1993 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001994 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001995 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001996 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001997 bw!
1998
1999 #" Use a buffer-local value and a global value
2000 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002001 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002002 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002003 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002004 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2005 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002006 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002007 new
2008 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002009 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002010 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2011 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002012 call assert_equal([], g:TsrFunc1Args)
2013 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002014 wincmd w
2015 call setline(1, 'sun')
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")
2018 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002019 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002020 :%bw!
2021 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002022 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002023
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002024 " Test for using a script-local function name
2025 func s:TsrFunc3(findstart, base)
2026 call add(g:TsrFunc3Args, [a:findstart, a:base])
2027 return a:findstart ? 0 : []
2028 endfunc
2029 set tsrfu=s:TsrFunc3
2030 new
2031 call setline(1, 'script1')
2032 let g:TsrFunc3Args = []
2033 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2034 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2035 bw!
2036
2037 let &tsrfu = 's:TsrFunc3'
2038 new
2039 call setline(1, 'script2')
2040 let g:TsrFunc3Args = []
2041 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2042 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2043 bw!
2044 delfunc s:TsrFunc3
2045
2046 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002047 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002048 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2049
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002050 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002051 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002052 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002053 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002054 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002055 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002056 bw!
2057
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002058 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2059 func SetTsrFunc()
2060 let params = {'thesaurus': function('g:DictTsrFunc')}
2061 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002062 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002063 func g:DictTsrFunc(_) dict
2064 endfunc
2065 call SetTsrFunc()
2066 new
2067 call SetTsrFunc()
2068 bw
2069 call test_garbagecollect_now()
2070 new
2071 set thesaurusfunc=
2072 wincmd w
2073 %bw!
2074 delfunc SetTsrFunc
2075
2076 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2077 " cause a crash.
2078 func SetLocalTsrFunc()
2079 let params = {'thesaurus': function('g:DictTsrFunc')}
2080 let &l:thesaurusfunc = params.thesaurus
2081 endfunc
2082 call SetLocalTsrFunc()
2083 call test_garbagecollect_now()
2084 call SetLocalTsrFunc()
2085 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002086 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002087 delfunc g:DictTsrFunc
2088 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002089
2090 " Vim9 tests
2091 let lines =<< trim END
2092 vim9script
2093
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002094 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2095 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002096 return findstart ? 0 : []
2097 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002098
2099 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002100 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2101 new | only
2102 setline(1, 'one')
2103 g:Vim9tsrFunc_Args = []
2104 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2105 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2106 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002107
2108 # Test for using a global function name
2109 &thesaurusfunc = g:TsrFunc2
2110 new | only
2111 setline(1, 'two')
2112 g:TsrFunc2Args = []
2113 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2114 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2115 bw!
2116
2117 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002118 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002119 add(g:LocalTsrFuncArgs, [findstart, base])
2120 return findstart ? 0 : []
2121 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002122 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002123 new | only
2124 setline(1, 'three')
2125 g:LocalTsrFuncArgs = []
2126 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2127 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2128 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002129 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002130 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002131
2132 " cleanup
2133 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002134 delfunc TsrFunc1
2135 delfunc TsrFunc2
2136 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002137 %bw!
2138endfunc
2139
Christian Brabandtac72c212022-04-07 21:00:53 +01002140func FooBarComplete(findstart, base)
2141 if a:findstart
2142 return col('.') - 1
2143 else
2144 return ["Foo", "Bar", "}"]
2145 endif
2146endfunc
2147
2148func Test_complete_smartindent()
2149 new
2150 setlocal smartindent completefunc=FooBarComplete
2151
2152 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2153 let result = getline(1,'$')
2154 call assert_equal(['', '{','}',''], result)
2155 bw!
2156 delfunction! FooBarComplete
2157endfunc
2158
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002159func Test_complete_overrun()
2160 " this was going past the end of the copied text
2161 new
2162 sil norm si”0s0 
2163 bwipe!
2164endfunc
2165
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002166func Test_infercase_very_long_line()
2167 " this was truncating the line when inferring case
2168 new
2169 let longLine = "blah "->repeat(300)
2170 let verylongLine = "blah "->repeat(400)
2171 call setline(1, verylongLine)
2172 call setline(2, longLine)
2173 set ic infercase
2174 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2175 call assert_equal(longLine, getline(3))
2176
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002177 " check that the too long text is NUL terminated
2178 %del
2179 norm o
2180 norm 1987ax
2181 exec "norm ox\<C-X>\<C-L>"
2182 call assert_equal(repeat('x', 1987), getline(3))
2183
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002184 bwipe!
2185 set noic noinfercase
2186endfunc
2187
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002188func Test_ins_complete_add()
2189 " this was reading past the end of allocated memory
2190 new
2191 norm o
2192 norm 7o€€
2193 sil! norm o
2194
2195 bwipe!
2196endfunc
2197
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002198func Test_ins_complete_end_of_line()
2199 " this was reading past the end of the line
Bram Moolenaar94722c52023-01-28 19:19:03 +00002200 new
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002201 norm 8o€ý 
2202 sil! norm o
2203
2204 bwipe!
2205endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002206
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002207func s:Tagfunc(t,f,o)
2208 bwipe!
2209 return []
2210endfunc
2211
2212" This was using freed memory, since 'complete' was in a wiped out buffer.
2213" Also using a window that was closed.
2214func Test_tagfunc_wipes_out_buffer()
2215 new
2216 set complete=.,t,w,b,u,i
2217 se tagfunc=s:Tagfunc
2218 sil norm i
2219
2220 bwipe!
2221endfunc
2222
Bram Moolenaar6ac2e432023-03-31 19:32:29 +01002223func Test_ins_complete_popup_position()
2224 CheckScreendump
2225
2226 let lines =<< trim END
2227 vim9script
2228 set nowrap
2229 setline(1, ['one', 'two', 'this is line ', 'four'])
2230 prop_type_add('test', {highlight: 'Error'})
2231 prop_add(3, 0, {
2232 text_align: 'above',
2233 text: 'The quick brown fox jumps over the lazy dog',
2234 type: 'test'
2235 })
2236 END
2237 call writefile(lines, 'XinsPopup', 'D')
2238 let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
2239
2240 call term_sendkeys(buf, "3GA\<C-N>")
2241 call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
2242
2243 call StopVimInTerminal(buf)
2244endfunc
2245
LemonBoy69fb5af2023-10-11 21:55:56 +02002246func GetCompleteInfo()
2247 let g:compl_info = complete_info()
2248 return ''
2249endfunc
2250
nwounkn2e3cd522023-10-17 11:05:38 +02002251func Test_completion_restart()
2252 new
2253 set complete=. completeopt=menuone backspace=2
2254 call setline(1, 'workhorse workhorse')
2255 exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
2256 call assert_equal(1, len(g:compl_info['items']))
2257 call assert_equal('workhorse', g:compl_info['items'][0]['word'])
2258 set complete& completeopt& backspace&
2259 bwipe!
2260endfunc
2261
LemonBoy69fb5af2023-10-11 21:55:56 +02002262func Test_complete_info_index()
2263 new
2264 call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
2265 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2266
2267 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2268
2269 set completeopt=menu,preview
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002270 " Search forward
LemonBoy69fb5af2023-10-11 21:55:56 +02002271 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2272 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002273 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002274 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2275 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002276 call assert_equal(6 , len(g:compl_info['items']))
2277 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2278 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2279 call assert_equal(6 , len(g:compl_info['items']))
2280 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2281 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2282 call assert_equal(6 , len(g:compl_info['items']))
2283 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2284 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2285 call assert_equal(6 , len(g:compl_info['items']))
2286 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2287 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2288 call assert_equal(6 , len(g:compl_info['items']))
2289 " Search forward: unselected item
2290 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2291 call assert_equal(6 , len(g:compl_info['items']))
2292 call assert_equal(-1 , g:compl_info['selected'])
LemonBoy69fb5af2023-10-11 21:55:56 +02002293
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002294 " Search backward
LemonBoy69fb5af2023-10-11 21:55:56 +02002295 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2296 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002297 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002298 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2299 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002300 call assert_equal(6 , len(g:compl_info['items']))
2301 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2302 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2303 call assert_equal(6 , len(g:compl_info['items']))
2304 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2305 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2306 call assert_equal(6 , len(g:compl_info['items']))
2307 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2308 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2309 call assert_equal(6 , len(g:compl_info['items']))
2310 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2311 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2312 call assert_equal(6 , len(g:compl_info['items']))
2313 " search backwards: unselected item
2314 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2315 call assert_equal(6 , len(g:compl_info['items']))
2316 call assert_equal(-1 , g:compl_info['selected'])
2317
2318 " switch direction: forwards, then backwards
2319 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2320 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2321 call assert_equal(6 , len(g:compl_info['items']))
2322 " switch direction: forwards, then backwards, then forwards again
2323 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2324 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2325 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2326 call assert_equal(6 , len(g:compl_info['items']))
2327
2328 " switch direction: backwards, then forwards
2329 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2330 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2331 call assert_equal(6 , len(g:compl_info['items']))
2332 " switch direction: backwards, then forwards, then backwards again
2333 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2334 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2335 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002336
2337 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2338 set completeopt+=noselect
2339 " Search forward.
2340 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2341 call assert_equal(-1, g:compl_info['selected'])
2342
2343 " Search backward.
2344 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2345 call assert_equal(-1, g:compl_info['selected'])
2346
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002347 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2348 call assert_equal(0, g:compl_info['selected'])
2349 call assert_equal(6 , len(g:compl_info['items']))
2350 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2351 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 +02002352 call assert_equal("aaa", 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']))
Christian Brabandt57f9ce12023-11-04 09:58:14 +01002354 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2355 call assert_equal(-1, g:compl_info['selected'])
2356 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002357
LemonBoy69fb5af2023-10-11 21:55:56 +02002358 set completeopt&
2359 bwipe!
2360endfunc
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002361
Christian Brabandtfef66302024-01-29 21:46:58 +01002362func Test_complete_changed_complete_info()
2363 CheckRunVimInTerminal
2364 " this used to crash vim, see #13929
2365 let lines =<< trim END
2366 set completeopt=menuone
2367 autocmd CompleteChanged * call complete_info(['items'])
2368 call feedkeys("iii\<cr>\<c-p>")
2369 END
2370 call writefile(lines, 'Xsegfault', 'D')
2371 let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
2372 call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
2373 call StopVimInTerminal(buf)
2374endfunc
2375
2376" vim: shiftwidth=2 sts=2 expandtab nofoldenable