blob: 48589ce18887c5e856c4bb5cea332ea8dd67fe40 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010012 call mkdir('Xcpldir')
13 cd Xcpldir
Bram Moolenaarfb094e12017-11-05 20:59:28 +010014
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010018 \ "Makefile\tto\trun"], 'Xtestfile', 'D')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020019 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010025 \ 'end of testfile'], 'Xtestdata', 'D')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020026 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200102 call delete('Xtest11.one')
103 call delete('Xtest11.two')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200104 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100105 cd ..
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100106 call delete('Xcpldir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200107endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100108
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000109func Test_ins_complete_invalid_byte()
110 if has('unix') && executable('base64')
111 " this weird command was causing an illegal memory access
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100112 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64', 'D')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000113 call system('base64 -d Xinvalid64 > Xinvalid')
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100114 call writefile(['qa!'], 'Xexit', 'D')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000115 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000116 call delete('Xinvalid')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000117 endif
118endfunc
119
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100120func Test_omni_dash()
121 func Omni(findstart, base)
122 if a:findstart
123 return 5
124 else
125 echom a:base
126 return ['-help', '-v']
127 endif
128 endfunc
129 set omnifunc=Omni
130 new
131 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100132 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100133
134 bwipe!
135 delfunc Omni
136 set omnifunc=
137endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100138
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100139func Test_omni_throw()
140 let g:CallCount = 0
141 func Omni(findstart, base)
142 let g:CallCount += 1
143 if a:findstart
144 throw "he he he"
145 endif
146 endfunc
147 set omnifunc=Omni
148 new
149 try
150 exe "normal ifoo\<C-x>\<C-o>"
151 call assert_false(v:true, 'command should have failed')
152 catch
153 call assert_exception('he he he')
154 call assert_equal(1, g:CallCount)
155 endtry
156
157 bwipe!
158 delfunc Omni
159 unlet g:CallCount
160 set omnifunc=
161endfunc
162
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200163func Test_omni_autoload()
164 let save_rtp = &rtp
165 set rtp=Xruntime/some
166 let dir = 'Xruntime/some/autoload'
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100167 call mkdir(dir, 'pR')
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200168
169 let lines =<< trim END
170 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000171 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200172 if findstart
173 return 1
174 else
175 return ['match']
176 endif
177 enddef
178 {
179 eval 1 + 2
180 }
181 END
182 call writefile(lines, dir .. '/omni.vim')
183
184 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000185 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200186 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
187
188 bwipe!
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200189 set omnifunc=
190 let &rtp = save_rtp
191endfunc
192
Bram Moolenaarffa96842018-06-12 22:05:14 +0200193func Test_completefunc_args()
194 let s:args = []
195 func! CompleteFunc(findstart, base)
196 let s:args += [[a:findstart, empty(a:base)]]
197 endfunc
198 new
199
200 set completefunc=CompleteFunc
201 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200202 call assert_equal([1, 1], s:args[0])
203 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200204 set completefunc=
205
206 let s:args = []
207 set omnifunc=CompleteFunc
208 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200209 call assert_equal([1, 1], s:args[0])
210 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200211 set omnifunc=
212
213 bwipe!
214 unlet s:args
215 delfunc CompleteFunc
216endfunc
217
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100218func s:CompleteDone_CompleteFuncNone( findstart, base )
219 if a:findstart
220 return 0
221 endif
222
223 return v:none
224endfunc
225
Bram Moolenaar1e115362019-01-09 23:01:02 +0100226func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100227 if a:findstart
228 return 0
229 endif
230
231 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100232 \ 'words': [
233 \ {
234 \ 'word': 'aword',
235 \ 'abbr': 'wrd',
236 \ 'menu': 'extra text',
237 \ 'info': 'words are cool',
238 \ 'kind': 'W',
zeertzjq4cd45f12022-12-15 13:48:30 +0000239 \ 'user_data': ['one', 'two']
Bram Moolenaar08928322020-01-04 14:32:48 +0100240 \ }
241 \ ]
242 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100243endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100244
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100245func s:CompleteDone_CheckCompletedItemNone()
246 let s:called_completedone = 1
247endfunc
248
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100249func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100250 call assert_equal( 'aword', v:completed_item[ 'word' ] )
251 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
252 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
253 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
254 call assert_equal( 'W', v:completed_item[ 'kind' ] )
zeertzjq4cd45f12022-12-15 13:48:30 +0000255 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100256
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100257 if a:pre
258 call assert_equal('function', complete_info().mode)
259 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100260
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100261 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100262endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100263
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100264func Test_CompleteDoneNone()
265 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200266 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100267
268 set completefunc=<SID>CompleteDone_CompleteFuncNone
269 execute "normal a\<C-X>\<C-U>\<C-Y>"
270 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200274 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100275
276 let s:called_completedone = 0
277 au! CompleteDone
278endfunc
279
280func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100281 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
282 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100283
284 set completefunc=<SID>CompleteDone_CompleteFuncDict
285 execute "normal a\<C-X>\<C-U>\<C-Y>"
286 set completefunc&
287
zeertzjq4cd45f12022-12-15 13:48:30 +0000288 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100289 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100290
291 let s:called_completedone = 0
292 au! CompleteDone
293endfunc
294
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100295func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100296 if a:findstart
297 return 0
298 endif
299
300 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100301 \ 'words': [
302 \ {
303 \ 'word': 'aword',
304 \ 'abbr': 'wrd',
305 \ 'menu': 'extra text',
306 \ 'info': 'words are cool',
307 \ 'kind': 'W',
Bram Moolenaar08928322020-01-04 14:32:48 +0100308 \ }
309 \ ]
310 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100311endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100312
Bram Moolenaar1e115362019-01-09 23:01:02 +0100313func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100314 call assert_equal( 'aword', v:completed_item[ 'word' ] )
315 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
316 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
317 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
318 call assert_equal( 'W', v:completed_item[ 'kind' ] )
zeertzjq4cd45f12022-12-15 13:48:30 +0000319 call assert_equal( '', v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320
321 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100322endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100323
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100324func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100325 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
326
327 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
328 execute "normal a\<C-X>\<C-U>\<C-Y>"
329 set completefunc&
330
zeertzjq4cd45f12022-12-15 13:48:30 +0000331 call assert_equal('', v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100332 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100333
334 let s:called_completedone = 0
335 au! CompleteDone
336endfunc
337
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100338func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100339 if a:findstart
340 return 0
341 endif
342
343 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100344endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100345
Bram Moolenaar1e115362019-01-09 23:01:02 +0100346func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100347 call assert_equal( 'aword', v:completed_item[ 'word' ] )
348 call assert_equal( '', v:completed_item[ 'abbr' ] )
349 call assert_equal( '', v:completed_item[ 'menu' ] )
350 call assert_equal( '', v:completed_item[ 'info' ] )
351 call assert_equal( '', v:completed_item[ 'kind' ] )
352 call assert_equal( '', v:completed_item[ 'user_data' ] )
353
354 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100355endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100356
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100357func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100358 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
359
360 set completefunc=<SID>CompleteDone_CompleteFuncList
361 execute "normal a\<C-X>\<C-U>\<C-Y>"
362 set completefunc&
363
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100364 call assert_equal('', v:completed_item[ 'user_data' ])
365 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100366
367 let s:called_completedone = 0
368 au! CompleteDone
369endfunc
370
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200371func Test_CompleteDone_undo()
372 au CompleteDone * call append(0, "prepend1")
373 new
374 call setline(1, ["line1", "line2"])
375 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
376 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
377 \ getline(1, '$'))
378 undo
379 call assert_equal(["line1", "line2"], getline(1, '$'))
380 bwipe!
381 au! CompleteDone
382endfunc
383
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100384func Test_CompleteDone_modify()
385 let value = {
386 \ 'word': '',
387 \ 'abbr': '',
388 \ 'menu': '',
389 \ 'info': '',
390 \ 'kind': '',
391 \ 'user_data': '',
392 \ }
393 let v:completed_item = value
zeertzjq75020942022-07-31 11:37:20 +0100394 call assert_equal(value, v:completed_item)
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100395endfunc
396
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200397func CompleteTest(findstart, query)
398 if a:findstart
399 return col('.')
400 endif
401 return ['matched']
402endfunc
403
404func Test_completefunc_info()
405 new
406 set completeopt=menuone
407 set completefunc=CompleteTest
408 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100409 call assert_equal("matched{'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}", getline(1))
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200410 bwipe!
411 set completeopt&
412 set completefunc&
413endfunc
414
Girish Palya8950bf72024-03-20 20:07:29 +0100415func CompleteInfoUserDefinedFn(findstart, query)
416 " User defined function (i_CTRL-X_CTRL-U)
417 if a:findstart
418 return col('.')
419 endif
420 return [{'word': 'foo'}, {'word': 'bar'}, {'word': 'baz'}, {'word': 'qux'}]
421endfunc
422
423func CompleteInfoTestUserDefinedFn(mvmt, idx, noselect)
424 new
425 if a:noselect
426 set completeopt=menuone,popup,noinsert,noselect
427 else
428 set completeopt=menu,preview
429 endif
430 set completefunc=CompleteInfoUserDefinedFn
431 call feedkeys("i\<C-X>\<C-U>" . a:mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
432 let completed = a:idx != -1 ? ['foo', 'bar', 'baz', 'qux']->get(a:idx) : ''
433 call assert_equal(completed. "{'pum_visible': 1, 'mode': 'function', 'selected': " . a:idx . ", 'items': [" .
434 \ "{'word': 'foo', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
435 \ "{'word': 'bar', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
436 \ "{'word': 'baz', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
437 \ "{'word': 'qux', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" .
438 \ "]}", getline(1))
439 bwipe!
440 set completeopt&
441 set completefunc&
442endfunc
443
444func Test_complete_info_user_defined_fn()
445 " forward
446 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 1, v:true)
447 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 2, v:true)
448 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 2, v:false)
449 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 3, v:false)
450 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>", -1, v:false)
451 " backward
452 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 2, v:true)
453 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 1, v:true)
454 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>", -1, v:true)
455 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 3, v:false)
456 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 2, v:false)
457 " forward backward
458 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 1, v:true)
459 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 0, v:true)
460 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 2, v:false)
461 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 3, v:false)
462 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 1, v:false)
463 " backward forward
464 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 0, v:true)
465 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 2, v:true)
466 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 1, v:false)
467 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 3, v:false)
468 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-N>\<C-N>", 1, v:false)
469endfunc
470
zeertzjq440d4cb2023-03-02 17:51:32 +0000471" Test that mouse scrolling/movement should not interrupt completion.
472func Test_mouse_scroll_move_during_completion()
473 new
474 com! -buffer TestCommand1 echo 'TestCommand1'
475 com! -buffer TestCommand2 echo 'TestCommand2'
476 call setline(1, ['', '', '', '', ''])
477 call cursor(5, 1)
478
479 " Without completion menu scrolling can move text.
480 set completeopt-=menu wrap
481 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
482 call assert_equal('TestCommand2', getline('.'))
483 call assert_notequal(1, winsaveview().topline)
484 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
485 call assert_equal('TestCommand2', getline('.'))
486 call assert_equal(1, winsaveview().topline)
487 set nowrap
488 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
489 call assert_equal('TestCommand2', getline('.'))
490 call assert_notequal(0, winsaveview().leftcol)
491 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
492 call assert_equal('TestCommand2', getline('.'))
493 call assert_equal(0, winsaveview().leftcol)
494 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
495 call assert_equal('TestCommand2', getline('.'))
496
497 " With completion menu scrolling cannot move text.
498 set completeopt+=menu wrap
499 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
500 call assert_equal('TestCommand2', getline('.'))
501 call assert_equal(1, winsaveview().topline)
502 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
503 call assert_equal('TestCommand2', getline('.'))
504 call assert_equal(1, winsaveview().topline)
505 set nowrap
506 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
507 call assert_equal('TestCommand2', getline('.'))
508 call assert_equal(0, winsaveview().leftcol)
509 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
510 call assert_equal('TestCommand2', getline('.'))
511 call assert_equal(0, winsaveview().leftcol)
512 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
513 call assert_equal('TestCommand2', getline('.'))
514
515 bwipe!
516 set completeopt& wrap&
517endfunc
518
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100519" Check that when using feedkeys() typeahead does not interrupt searching for
520" completions.
521func Test_compl_feedkeys()
522 new
523 set completeopt=menuone,noselect
524 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
525 call assert_equal("jump jump", getline(1))
526 bwipe!
527 set completeopt&
528endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200529
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200530" Test for insert path completion with completeslash option
531func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200532 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200533
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100534 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200535 let orig_shellslash = &shellslash
536 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200537 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200538
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200539 set noshellslash
540
541 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100542 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100543 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200544
545 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100546 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100547 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200548
549 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100550 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100551 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200552
553 set shellslash
554
555 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100556 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100557 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200558
559 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100560 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100561 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200562
563 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100564 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100565 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200566 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200567
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200568 set noshellslash
569 set completeslash=slash
570 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
571
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200572 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200573 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200574endfunc
575
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200576func Test_pum_stopped_by_timer()
577 CheckScreendump
578
579 let lines =<< trim END
580 call setline(1, ['hello', 'hullo', 'heeee', ''])
581 func StartCompl()
582 call timer_start(100, { -> execute('stopinsert') })
583 call feedkeys("Gah\<C-N>")
584 endfunc
585 END
586
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100587 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200588 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
589 call term_sendkeys(buf, ":call StartCompl()\<CR>")
590 call TermWait(buf, 200)
591 call term_sendkeys(buf, "k")
592 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
593
594 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200595endfunc
596
zeertzjqcd5dbad2022-05-04 17:51:50 +0100597func Test_complete_stopinsert_startinsert()
598 nnoremap <F2> <Cmd>startinsert<CR>
599 inoremap <F2> <Cmd>stopinsert<CR>
600 " This just checks if this causes an error
601 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
602 nunmap <F2>
603 iunmap <F2>
604endfunc
605
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100606func Test_pum_with_folds_two_tabs()
607 CheckScreendump
608
609 let lines =<< trim END
610 set fdm=marker
611 call setline(1, ['" x {{{1', '" a some text'])
612 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
613 norm! zm
614 tab sp
615 call feedkeys('2Gzv', 'xt')
616 call feedkeys("0fa", 'xt')
617 END
618
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100619 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100620 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200621 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100622 call term_sendkeys(buf, "a\<C-N>")
623 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
624
625 call term_sendkeys(buf, "\<Esc>")
626 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100627endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100628
629func Test_pum_with_preview_win()
630 CheckScreendump
631
632 let lines =<< trim END
glepnir53387c52024-05-27 15:11:01 +0200633 func Omni_test(findstart, base)
634 if a:findstart
635 return col(".") - 1
636 endif
637 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
638 endfunc
639 set omnifunc=Omni_test
640 set completeopt+=longest
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100641 END
642
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100643 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100644 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100645 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100646 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100647 call term_sendkeys(buf, "\<C-N>")
648 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
649
650 call term_sendkeys(buf, "\<Esc>")
651 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100652endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100653
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000654func Test_scrollbar_on_wide_char()
655 CheckScreendump
656
657 let lines =<< trim END
658 call setline(1, ['a', ' 啊啊啊',
659 \ ' 哦哦哦',
660 \ ' 呃呃呃'])
661 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
662 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100663 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000664 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
665 call term_sendkeys(buf, "A\<C-N>")
666 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
667
668 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000669endfunc
670
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100671" Test for inserting the tag search pattern in insert mode
672func Test_ins_compl_tag_sft()
673 call writefile([
674 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
675 \ "first\tXfoo\t/^int first() {}$/",
676 \ "second\tXfoo\t/^int second() {}$/",
677 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100678 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100679 set tags=Xtags
680 let code =<< trim [CODE]
681 int first() {}
682 int second() {}
683 int third() {}
684 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100685 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100686
687 enew
688 set showfulltag
689 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
690 call assert_equal('int second() {}', getline(1))
691 set noshowfulltag
692
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100693 set tags&
694 %bwipe!
695endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200696
697" Test for 'completefunc' deleting text
698func Test_completefunc_error()
699 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200700 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200701 func CompleteFunc(findstart, base)
702 if a:findstart == 1
703 normal dd
704 return col('.') - 1
705 endif
706 return ['a', 'b']
707 endfunc
708 set completefunc=CompleteFunc
709 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100710 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200711
712 " delete text when called for the second time
713 func CompleteFunc2(findstart, base)
714 if a:findstart == 1
715 return col('.') - 1
716 endif
717 normal dd
718 return ['a', 'b']
719 endfunc
720 set completefunc=CompleteFunc2
721 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100722 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200723
Bram Moolenaar97202d92021-01-28 18:34:35 +0100724 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100725 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100726 if a:findstart == 1
727 return col('.') - 1
728 endif
729 wincmd p
730 return ['a', 'b']
731 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100732 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100733 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100734 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100735 close!
736
737 set completefunc&
738 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100739 delfunc CompleteFunc2
740 delfunc CompleteFunc3
741 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100742endfunc
743
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200744" Test for returning non-string values from 'completefunc'
745func Test_completefunc_invalid_data()
746 new
747 func! CompleteFunc(findstart, base)
748 if a:findstart == 1
749 return col('.') - 1
750 endif
751 return [{}, '', 'moon']
752 endfunc
753 set completefunc=CompleteFunc
754 exe "normal i\<C-X>\<C-U>"
755 call assert_equal('moon', getline(1))
756 set completefunc&
757 close!
758endfunc
759
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200760" Test for errors in using complete() function
761func Test_complete_func_error()
762 call assert_fails('call complete(1, ["a"])', 'E785:')
763 func ListColors()
764 call complete(col('.'), "blue")
765 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100766 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200767 func ListMonths()
768 call complete(col('.'), test_null_list())
769 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100770 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200771 delfunc ListColors
772 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100773 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200774 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200775endfunc
776
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000777" Test for recursively starting completion mode using complete()
778func Test_recursive_complete_func()
779 func ListColors()
780 call complete(5, ["red", "blue"])
781 return ''
782 endfunc
783 new
784 call setline(1, ['a1', 'a2'])
785 set complete=.
786 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
787 call assert_equal('a2blue', getline(3))
788 delfunc ListColors
789 bw!
790endfunc
791
bfredl87af60c2022-09-24 11:17:51 +0100792" Test for using complete() with completeopt+=longest
793func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100794 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100795 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100796
797 " default: insert first match
798 set completeopt&
799 call setline(1, ['i'])
800 exe "normal Aa\<f3>\<esc>"
801 call assert_equal('iaax', getline(1))
802
803 " with longest: insert longest prefix
804 set completeopt+=longest
805 call setline(1, ['i'])
806 exe "normal Aa\<f3>\<esc>"
807 call assert_equal('iaa', getline(1))
808 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100809 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100810endfunc
811
zeertzjq529b9ad2024-06-05 20:27:06 +0200812" Test for buffer-local value of 'completeopt'
813func Test_completeopt_buffer_local()
814 set completeopt=menu
815 new
816 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
817 call assert_equal('', &l:completeopt)
818 call assert_equal('menu', &completeopt)
819 call assert_equal('menu', &g:completeopt)
820
821 setlocal bufhidden=hide
822 enew
823 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
824 call assert_equal('', &l:completeopt)
825 call assert_equal('menu', &completeopt)
826 call assert_equal('menu', &g:completeopt)
827
828 setlocal completeopt+=fuzzy,noinsert
829 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
830 call assert_equal('menu,fuzzy,noinsert', &completeopt)
831 call assert_equal('menu', &g:completeopt)
832 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
833 call assert_equal('foobaz', getline('.'))
834
835 setlocal completeopt=
836 call assert_equal('', &l:completeopt)
837 call assert_equal('menu', &completeopt)
838 call assert_equal('menu', &g:completeopt)
839 call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
840 call assert_equal('foofoo', getline('.'))
841
842 setlocal completeopt+=longest
843 call assert_equal('menu,longest', &l:completeopt)
844 call assert_equal('menu,longest', &completeopt)
845 call assert_equal('menu', &g:completeopt)
846 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
847 call assert_equal('foo', getline('.'))
848
849 setlocal bufhidden=hide
850 buffer #
851 call assert_equal('', &l:completeopt)
852 call assert_equal('menu', &completeopt)
853 call assert_equal('menu', &g:completeopt)
854 call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
855 call assert_equal('foofoo', getline('.'))
856
857 setlocal completeopt+=fuzzy,noinsert
858 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
859 call assert_equal('menu,fuzzy,noinsert', &completeopt)
860 call assert_equal('menu', &g:completeopt)
861 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
862 call assert_equal('foobaz', getline('.'))
863
864 buffer #
865 call assert_equal('menu,longest', &l:completeopt)
866 call assert_equal('menu,longest', &completeopt)
867 call assert_equal('menu', &g:completeopt)
868 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
869 call assert_equal('foo', getline('.'))
870
871 setlocal bufhidden=wipe
872 buffer! #
873 bwipe!
874 call assert_equal('', &l:completeopt)
875 call assert_equal('menu', &completeopt)
876 call assert_equal('menu', &g:completeopt)
877
878 set completeopt&
879endfunc
bfredl87af60c2022-09-24 11:17:51 +0100880
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200881" Test for completing words following a completed word in a line
882func Test_complete_wrapscan()
883 " complete words from another buffer
884 new
885 call setline(1, ['one two', 'three four'])
886 new
887 setlocal complete=w
888 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
889 call assert_equal('two three four', getline(1))
890 close!
891 " complete words from the current buffer
892 setlocal complete=.
893 %d
894 call setline(1, ['one two', ''])
895 call cursor(2, 1)
896 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
897 call assert_equal('one two one two', getline(2))
898 close!
899endfunc
900
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200901" Test for completing special characters
902func Test_complete_special_chars()
903 new
904 call setline(1, 'int .*[-\^$ func float')
905 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
906 call assert_equal('int .*[-\^$ func float', getline(2))
907 close!
908endfunc
909
910" Test for completion when text is wrapped across lines.
911func Test_complete_across_line()
912 new
913 call setline(1, ['red green blue', 'one two three'])
914 setlocal textwidth=20
915 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
916 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
917 close!
918endfunc
919
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000920" Test for completing words with a '.' at the end of a word.
921func Test_complete_joinspaces()
922 new
923 call setline(1, ['one two.', 'three. four'])
924 set joinspaces
925 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
926 call assert_equal("one two. three. four", getline(3))
927 set joinspaces&
928 bw!
929endfunc
930
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200931" Test for using CTRL-L to add one character when completing matching
932func Test_complete_add_onechar()
933 new
934 call setline(1, ['wool', 'woodwork'])
935 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
936 call assert_equal('woof', getline(3))
937
938 " use 'ignorecase' and backspace to erase characters from the prefix string
939 " and then add letters using CTRL-L
940 %d
941 set ignorecase backspace=2
942 setlocal complete=.
943 call setline(1, ['workhorse', 'workload'])
944 normal Go
nwounkn2e3cd522023-10-17 11:05:38 +0200945 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200946 call assert_equal('workh', getline(3))
947 set ignorecase& backspace&
948 close!
949endfunc
950
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000951" Test for using CTRL-X CTRL-L to complete whole lines lines
952func Test_complete_wholeline()
953 new
954 " complete one-line
955 call setline(1, ['a1', 'a2'])
956 exe "normal ggoa\<C-X>\<C-L>"
957 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
958 " go to the next match (wrapping around the buffer)
959 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
960 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
961 " go to the next match
962 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
963 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
964 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
965 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
966 " repeat the test using CTRL-L
967 " go to the next match (wrapping around the buffer)
968 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
969 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
970 " go to the next match
971 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
972 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
973 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
974 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
975 %d
976 " use CTRL-X CTRL-L to add one more line
977 call setline(1, ['a1', 'b1'])
978 setlocal complete=.
979 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
980 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
981 bw!
982endfunc
983
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200984" Test insert completion with 'cindent' (adjust the indent)
985func Test_complete_with_cindent()
986 new
987 setlocal cindent
988 call setline(1, ['if (i == 1)', " j = 2;"])
989 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
990 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
991
992 %d
993 call setline(1, ['when while', '{', ''])
994 setlocal cinkeys+==while
995 exe "normal Giwh\<C-P> "
996 call assert_equal("\twhile ", getline('$'))
997 close!
998endfunc
999
1000" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
1001func Test_complete_cmdline()
1002 new
1003 exe "normal icaddb\<C-X>\<C-V>"
1004 call assert_equal('caddbuffer', getline(1))
1005 exe "normal ocall getqf\<C-X>\<C-V>"
1006 call assert_equal('call getqflist(', getline(2))
1007 exe "normal oabcxyz(\<C-X>\<C-V>"
1008 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +02001009 com! -buffer TestCommand1 echo 'TestCommand1'
1010 com! -buffer TestCommand2 echo 'TestCommand2'
1011 write TestCommand1Test
1012 write TestCommand2Test
1013 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
1014 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
1015 call assert_equal('TestCommand2Test', getline(4))
1016 call delete('TestCommand1Test')
1017 call delete('TestCommand2Test')
1018 delcom TestCommand1
1019 delcom TestCommand2
1020 close!
1021endfunc
1022
1023" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
1024func Test_complete_stop()
1025 new
1026 func Save_mode1()
1027 let g:mode1 = mode(1)
1028 return ''
1029 endfunc
1030 func Save_mode2()
1031 let g:mode2 = mode(1)
1032 return ''
1033 endfunc
1034 inoremap <F1> <C-R>=Save_mode1()<CR>
1035 inoremap <F2> <C-R>=Save_mode2()<CR>
1036 call setline(1, ['aaa bbb ccc '])
1037 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1038 call assert_equal('ic', g:mode1)
1039 call assert_equal('i', g:mode2)
1040 call assert_equal('aaa bbb ccc ', getline(1))
1041 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1042 call assert_equal('ic', g:mode1)
1043 call assert_equal('i', g:mode2)
1044 call assert_equal('aaa bbb ccc aaa', getline(1))
1045 set completeopt+=noselect
1046 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1047 call assert_equal('ic', g:mode1)
1048 call assert_equal('i', g:mode2)
1049 call assert_equal('aaa bbb ccc aaa bb', getline(1))
1050 set completeopt&
1051 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1052 call assert_equal('ic', g:mode1)
1053 call assert_equal('i', g:mode2)
1054 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
1055 com! -buffer TestCommand1 echo 'TestCommand1'
1056 com! -buffer TestCommand2 echo 'TestCommand2'
1057 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1058 call assert_equal('ic', g:mode1)
1059 call assert_equal('i', g:mode2)
1060 call assert_equal('TestCommand2', getline(2))
1061 delcom TestCommand1
1062 delcom TestCommand2
1063 unlet g:mode1
1064 unlet g:mode2
1065 iunmap <F1>
1066 iunmap <F2>
1067 delfunc Save_mode1
1068 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001069 close!
1070endfunc
1071
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00001072" Test for typing CTRL-R in insert completion mode to insert a register
1073" content.
1074func Test_complete_reginsert()
1075 new
1076 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
1077
1078 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
1079 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
1080 " inserted.
1081 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
1082 call assert_equal('a123', getline(5))
1083 let @r = "\<C-P>\<C-P>"
1084 exe "normal GCa\<C-P>\<C-R>r"
1085 call assert_equal('a12', getline(5))
1086 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
1087 call assert_equal('a1234x', getline(5))
1088 bw!
1089endfunc
1090
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02001091func Test_issue_7021()
1092 CheckMSWindows
1093
1094 let orig_shellslash = &shellslash
1095 set noshellslash
1096
1097 set completeslash=slash
1098 call assert_false(expand('~') =~ '/')
1099
1100 let &shellslash = orig_shellslash
1101 set completeslash=
1102endfunc
1103
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001104" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
1105func Test_complete_longest_match()
1106 for e in ['latin1', 'utf-8']
1107 exe 'set encoding=' .. e
1108 new
1109 set complete=.
1110 set completeopt=menu,longest
1111 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
1112 exe "normal Gopfx\<C-P>"
1113 call assert_equal('pfx_', getline(5))
1114 bw!
1115 endfor
1116
1117 " Test for completing additional words with longest match set
1118 new
1119 call setline(1, ['abc1', 'abd2'])
1120 exe "normal Goab\<C-P>\<C-X>\<C-P>"
1121 call assert_equal('ab', getline(3))
1122 bw!
1123 set complete& completeopt&
1124endfunc
1125
1126" Test for removing the first displayed completion match and selecting the
1127" match just before that.
1128func Test_complete_erase_firstmatch()
1129 new
1130 call setline(1, ['a12', 'a34', 'a56'])
1131 set complete=.
1132 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1133 call assert_equal('a34', getline('$'))
1134 set complete&
1135 bw!
1136endfunc
1137
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001138" Test for completing words from unloaded buffers
1139func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001140 call writefile(['abc'], "Xfile1", 'D')
1141 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001142 edit Xfile1
1143 edit Xfile2
1144 new | close
1145 enew
1146 bunload Xfile1 Xfile2
1147 set complete=u
1148 " complete from an unloaded buffer
1149 exe "normal! ia\<C-P>"
1150 call assert_equal('abc', getline(1))
1151 exe "normal! od\<C-P>"
1152 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001153
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001154 set complete&
1155 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001156endfunc
1157
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001158" Test for completing whole lines from unloaded buffers
1159func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001160 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001161 edit Xfile1
1162 enew
1163 set complete=u
1164 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1165 call assert_equal('a line2', getline(1))
1166 %d
1167 " completing from an unlisted buffer should fail
1168 bdel Xfile1
1169 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1170 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001171
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001172 set complete&
1173 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001174endfunc
1175
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001176" Test for completing words from unlisted buffers
1177func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001178 call writefile(['abc'], "Xfile1", 'D')
1179 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001180 edit Xfile1
1181 edit Xfile2
1182 new | close
1183 bdel Xfile1 Xfile2
1184 set complete=U
1185 " complete from an unlisted buffer
1186 exe "normal! ia\<C-P>"
1187 call assert_equal('abc', getline(1))
1188 exe "normal! od\<C-P>"
1189 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001190
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001191 set complete&
1192 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001193endfunc
1194
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001195" Test for completing whole lines from unlisted buffers
1196func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001197 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001198 edit Xfile1
1199 enew
1200 set complete=U
zeertzjqc029c132024-03-28 11:37:26 +01001201 " completing from an unloaded buffer should fail
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001202 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1203 call assert_equal('a', getline(1))
1204 %d
1205 bdel Xfile1
1206 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1207 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001208
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001209 set complete&
1210 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001211endfunc
1212
1213" Test for adding a multibyte character using CTRL-L in completion mode
1214func Test_complete_mbyte_char_add()
1215 new
1216 set complete=.
1217 call setline(1, 'abÄ—')
1218 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1219 call assert_equal('abÄ—', getline(2))
1220 " Test for a leader with multibyte character
1221 %d
1222 call setline(1, 'abÄ—Ä•')
1223 exe "normal! oabÄ—\<C-P>"
1224 call assert_equal('abÄ—Ä•', getline(2))
1225 bw!
1226endfunc
1227
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001228" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1229" not to complete matches from the local buffer. Also test using multiple
1230" <C-X> to cancel the current completion mode.
1231func Test_complete_local_expansion()
1232 new
1233 set complete=t
1234 call setline(1, ['abc', 'def'])
1235 exe "normal! Go\<C-X>\<C-P>"
1236 call assert_equal("def", getline(3))
1237 exe "normal! Go\<C-P>"
1238 call assert_equal("", getline(4))
1239 exe "normal! Go\<C-X>\<C-N>"
1240 call assert_equal("abc", getline(5))
1241 exe "normal! Go\<C-N>"
1242 call assert_equal("", getline(6))
1243
1244 " use multiple <C-X> to cancel the previous completion mode
1245 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1246 call assert_equal("", getline(7))
1247 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1248 call assert_equal("", getline(8))
1249 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1250 call assert_equal("abc", getline(9))
1251
1252 " interrupt the current completion mode
1253 set completeopt=menu,noinsert
1254 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1255 call assert_equal("abc", getline(10))
1256
1257 " when only one <C-X> is used to interrupt, do normal expansion
1258 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1259 call assert_equal("", getline(11))
1260 set completeopt&
1261
1262 " using two <C-X> in non-completion mode and restarting the same mode
1263 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1264 call assert_equal("def", getline(12))
1265
1266 " test for adding a match from the original empty text
1267 %d
1268 call setline(1, 'abc def g')
1269 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1270 call assert_equal('def', getline(2))
1271 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1272 call assert_equal('abc', getline(2))
1273
1274 bw!
1275endfunc
1276
1277" Test for undoing changes after a insert-mode completion
1278func Test_complete_undo()
1279 new
1280 set complete=.
1281 " undo with 'ignorecase'
1282 call setline(1, ['ABOVE', 'BELOW'])
1283 set ignorecase
1284 exe "normal! Goab\<C-G>u\<C-P>"
1285 call assert_equal("ABOVE", getline(3))
1286 undo
1287 call assert_equal("ab", getline(3))
1288 set ignorecase&
1289 %d
1290 " undo with longest match
1291 set completeopt=menu,longest
1292 call setline(1, ['above', 'about'])
1293 exe "normal! Goa\<C-G>u\<C-P>"
1294 call assert_equal("abo", getline(3))
1295 undo
1296 call assert_equal("a", getline(3))
1297 set completeopt&
1298 %d
1299 " undo for line completion
1300 call setline(1, ['above that change', 'below that change'])
1301 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1302 call assert_equal("above that change", getline(3))
1303 undo
1304 call assert_equal("above", getline(3))
1305
1306 bw!
1307endfunc
1308
1309" Test for completing a very long word
1310func Test_complete_long_word()
1311 set complete&
1312 new
1313 call setline(1, repeat('x', 950) .. ' one two three')
1314 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1315 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1316 %d
1317 " should fail when more than 950 characters are in a word
1318 call setline(1, repeat('x', 951) .. ' one two three')
1319 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1320 call assert_equal(repeat('x', 951), getline(2))
1321
1322 " Test for adding a very long word to an existing completion
1323 %d
1324 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1325 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1326 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1327 bw!
1328endfunc
1329
1330" Test for some fields in the complete items used by complete()
1331func Test_complete_items()
1332 func CompleteItems(idx)
1333 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1334 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1335 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1336 \ [#{user_data: 'u9'}],
1337 \ [#{word: "", user_data: 'u10'}],
1338 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1339 call complete(col('.'), items[a:idx])
1340 return ''
1341 endfunc
1342 new
1343 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1344 call assert_equal('u2', v:completed_item.user_data)
1345 call assert_equal('one', getline(1))
1346 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1347 call assert_equal('u3', v:completed_item.user_data)
1348 call assert_equal('one', getline(2))
1349 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1350 call assert_equal('', getline(3))
1351 set completeopt=menu,noinsert
1352 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1353 call assert_equal('oNE', getline(4))
1354 call assert_equal('u8', v:completed_item.user_data)
1355 set completeopt&
1356 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1357 call assert_equal('', getline(5))
1358 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1359 call assert_equal('', getline(6))
1360 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1361 call assert_equal('', getline(7))
1362 call assert_equal('u11', v:completed_item.user_data)
1363 " pass invalid argument to complete()
1364 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1365 call assert_fails('exe cmd', 'E730:')
1366 bw!
1367 delfunc CompleteItems
1368endfunc
1369
1370" Test for the "refresh" item in the dict returned by an insert completion
1371" function
1372func Test_complete_item_refresh_always()
1373 let g:CallCount = 0
1374 func! Tcomplete(findstart, base)
1375 if a:findstart
1376 " locate the start of the word
1377 let line = getline('.')
1378 let start = col('.') - 1
1379 while start > 0 && line[start - 1] =~ '\a'
1380 let start -= 1
1381 endwhile
1382 return start
1383 else
1384 let g:CallCount += 1
1385 let res = ["update1", "update12", "update123"]
1386 return #{words: res, refresh: 'always'}
1387 endif
1388 endfunc
1389 new
1390 set completeopt=menu,longest
1391 set completefunc=Tcomplete
1392 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1393 call assert_equal('up', getline(1))
1394 call assert_equal(2, g:CallCount)
1395 set completeopt&
1396 set completefunc&
1397 bw!
1398 delfunc Tcomplete
1399endfunc
1400
1401" Test for completing from a thesaurus file without read permission
1402func Test_complete_unreadable_thesaurus_file()
1403 CheckUnix
1404 CheckNotRoot
1405
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001406 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001407 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001408 new
1409 set complete=sXfile
1410 exe "normal! ia\<C-P>"
1411 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001412
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001413 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001414 set complete&
1415endfunc
1416
Bram Moolenaarcc233582020-12-12 13:32:07 +01001417" Test to ensure 'Scanning...' messages are not recorded in messages history
1418func Test_z1_complete_no_history()
1419 new
1420 messages clear
1421 let currmess = execute('messages')
1422 setlocal dictionary=README.txt
1423 exe "normal owh\<C-X>\<C-K>"
1424 exe "normal owh\<C-N>"
1425 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001426 bwipe!
1427endfunc
1428
1429" A mapping is not used for the key after CTRL-X.
1430func Test_no_mapping_for_ctrl_x_key()
1431 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001432 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001433 setlocal dictionary=README.txt
1434 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1435 call assert_equal('example ', getline(1))
1436 call assert_false(exists('was_mapped'))
1437 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001438endfunc
1439
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001440" Test for different ways of setting the 'completefunc' option
1441func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001442 func CompleteFunc1(callnr, findstart, base)
1443 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1444 return a:findstart ? 0 : []
1445 endfunc
1446 func CompleteFunc2(findstart, base)
1447 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001448 return a:findstart ? 0 : []
1449 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001450
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001451 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001452 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001453 LET &completefunc = 'g:CompleteFunc2'
1454 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001455 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001456 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001457 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001458 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001459 bw!
1460
1461 #" Test for using a function()
1462 set completefunc=function('g:CompleteFunc1',\ [10])
1463 new
1464 call setline(1, 'one')
1465 LET g:CompleteFunc1Args = []
1466 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1467 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001468 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001469
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001470 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001471 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001472 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001473 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001474 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001475 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001476 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001477 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001478 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001479
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001480 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001481 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001482 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001483 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001484 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001485 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001486 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001487 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001488 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001489
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001490 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001491 set completefunc=funcref('g:CompleteFunc1',\ [13])
1492 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001493 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001494 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001495 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001496 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001497 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001498
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001499 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001500 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001501 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001502 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001503 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001504 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001505 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001506 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001507 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001508
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001509 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001510 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001511 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001512 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001513 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001514 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001515 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001516 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001517 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001518
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001519 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001520 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001521 LET optval = substitute(optval, ' ', '\\ ', 'g')
1522 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001523 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001524 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001525 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001526 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001527 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001528 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001529
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001530 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001531 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001532 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001533 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001534 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001535 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001536 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001537 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001538
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001539 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001540 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001541 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001542 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001543 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001544 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001545 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001546 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001547
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001548 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001549 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001550 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001551 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001552 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001553 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001554 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001555 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001556 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001557
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001558 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001559 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001560 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001561 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001562 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001563 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001564 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001565 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001566 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001567
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001568 #" Test for using a lambda function with incorrect return value
1569 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1570 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001571 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001572 call setline(1, 'eight')
1573 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1574 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001575
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001576 #" Test for clearing the 'completefunc' option
1577 set completefunc=''
1578 set completefunc&
1579 call assert_fails("set completefunc=function('abc')", "E700:")
1580 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001581
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001582 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001583 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001584 call setline(1, 'five')
1585 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1586 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001587 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001588 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001589 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001590 bw!
1591 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001592 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001594 " Test for using a script-local function name
1595 func s:CompleteFunc3(findstart, base)
1596 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1597 return a:findstart ? 0 : []
1598 endfunc
1599 set completefunc=s:CompleteFunc3
1600 new
1601 call setline(1, 'script1')
1602 let g:CompleteFunc3Args = []
1603 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1604 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1605 bw!
1606
1607 let &completefunc = 's:CompleteFunc3'
1608 new
1609 call setline(1, 'script2')
1610 let g:CompleteFunc3Args = []
1611 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1612 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1613 bw!
1614 delfunc s:CompleteFunc3
1615
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001616 " In Vim9 script s: can be omitted
1617 let lines =<< trim END
1618 vim9script
1619 var CompleteFunc4Args = []
1620 def CompleteFunc4(findstart: bool, base: string): any
1621 add(CompleteFunc4Args, [findstart, base])
1622 return findstart ? 0 : []
1623 enddef
1624 set completefunc=CompleteFunc4
1625 new
1626 setline(1, 'script1')
1627 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1628 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1629 bw!
1630 END
1631 call v9.CheckScriptSuccess(lines)
1632
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001633 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001634 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001635 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1636
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001637 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001638 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001639 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001640 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001641 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001642 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001643
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 " set 'completefunc' to a partial with dict. This used to cause a crash.
1645 func SetCompleteFunc()
1646 let params = {'complete': function('g:DictCompleteFunc')}
1647 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001648 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 func g:DictCompleteFunc(_) dict
1650 endfunc
1651 call SetCompleteFunc()
1652 new
1653 call SetCompleteFunc()
1654 bw
1655 call test_garbagecollect_now()
1656 new
1657 set completefunc=
1658 wincmd w
1659 set completefunc=
1660 %bw!
1661 delfunc g:DictCompleteFunc
1662 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001663
1664 " Vim9 tests
1665 let lines =<< trim END
1666 vim9script
1667
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001668 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1669 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001670 return findstart ? 0 : []
1671 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001672
1673 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001674 set completefunc=function('Vim9CompleteFunc',\ [60])
1675 new | only
1676 setline(1, 'one')
1677 g:Vim9completeFuncArgs = []
1678 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1679 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1680 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001681
1682 # Test for using a global function name
1683 &completefunc = g:CompleteFunc2
1684 new | only
1685 setline(1, 'two')
1686 g:CompleteFunc2Args = []
1687 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1688 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1689 bw!
1690
1691 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001692 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001693 add(g:LocalCompleteFuncArgs, [findstart, base])
1694 return findstart ? 0 : []
1695 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001696 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001697 new | only
1698 setline(1, 'three')
1699 g:LocalCompleteFuncArgs = []
1700 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1701 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1702 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001703 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001704 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001705
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001706 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001707 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001708 delfunc CompleteFunc1
1709 delfunc CompleteFunc2
1710 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001711 %bw!
1712endfunc
1713
1714" Test for different ways of setting the 'omnifunc' option
1715func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001716 func OmniFunc1(callnr, findstart, base)
1717 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1718 return a:findstart ? 0 : []
1719 endfunc
1720 func OmniFunc2(findstart, base)
1721 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001722 return a:findstart ? 0 : []
1723 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001724
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001725 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001726 #" Test for using a function name
1727 LET &omnifunc = 'g:OmniFunc2'
1728 new
1729 call setline(1, 'zero')
1730 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001731 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001732 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1733 bw!
1734
1735 #" Test for using a function()
1736 set omnifunc=function('g:OmniFunc1',\ [10])
1737 new
1738 call setline(1, 'one')
1739 LET g:OmniFunc1Args = []
1740 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1741 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001742 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001743
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001744 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001745 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001746 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001747 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001748 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001749 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001750 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001751 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001752 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001753
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001754 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001755 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001756 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001757 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001758 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001759 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001760 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001761 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001762 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001763
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001764 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001765 set omnifunc=funcref('g:OmniFunc1',\ [13])
1766 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001767 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001768 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001769 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001770 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001771 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001772
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001773 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001774 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001775 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001776 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001777 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001778 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001779 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001780 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001781 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001782
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001783 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001784 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001785 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001786 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001787 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001788 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001789 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001790 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001791 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001792
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001793 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001794 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001795 LET optval = substitute(optval, ' ', '\\ ', 'g')
1796 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001797 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001798 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001799 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001800 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001801 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001802 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001803
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001804 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001805 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001806 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001807 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001808 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001809 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001810 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001811 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001812
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001813 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001814 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001815 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001816 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001817 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001818 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001819 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001820 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001821
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001822 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001823 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001824 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001825 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001826 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001827 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001828 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001829 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001830 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001831
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001832 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001833 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001834 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001835 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001836 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001837 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001838 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001839 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001840 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001841
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001842 #" Test for using a lambda function with incorrect return value
1843 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1844 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001845 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001846 call setline(1, 'eight')
1847 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1848 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001849
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001850 #" Test for clearing the 'omnifunc' option
1851 set omnifunc=''
1852 set omnifunc&
1853 call assert_fails("set omnifunc=function('abc')", "E700:")
1854 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001855
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001856 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001857 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001858 call setline(1, 'nine')
1859 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1860 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001861 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001862 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001863 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001864 bw!
1865 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001866 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001868 " Test for using a script-local function name
1869 func s:OmniFunc3(findstart, base)
1870 call add(g:OmniFunc3Args, [a:findstart, a:base])
1871 return a:findstart ? 0 : []
1872 endfunc
1873 set omnifunc=s:OmniFunc3
1874 new
1875 call setline(1, 'script1')
1876 let g:OmniFunc3Args = []
1877 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1878 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1879 bw!
1880
1881 let &omnifunc = 's:OmniFunc3'
1882 new
1883 call setline(1, 'script2')
1884 let g:OmniFunc3Args = []
1885 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1886 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1887 bw!
1888 delfunc s:OmniFunc3
1889
1890 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001891 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001892 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1893
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001894 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001895 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001896 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001897 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001898 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001899 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001900
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001901 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1902 func SetOmniFunc()
1903 let params = {'omni': function('g:DictOmniFunc')}
1904 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001905 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 func g:DictOmniFunc(_) dict
1907 endfunc
1908 call SetOmniFunc()
1909 new
1910 call SetOmniFunc()
1911 bw
1912 call test_garbagecollect_now()
1913 new
1914 set omnifunc=
1915 wincmd w
1916 set omnifunc=
1917 %bw!
1918 delfunc g:DictOmniFunc
1919 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001920
1921 " Vim9 tests
1922 let lines =<< trim END
1923 vim9script
1924
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001925 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1926 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001927 return findstart ? 0 : []
1928 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001929
1930 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001931 set omnifunc=function('Vim9omniFunc',\ [60])
1932 new | only
1933 setline(1, 'one')
1934 g:Vim9omniFunc_Args = []
1935 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1936 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1937 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001938
1939 # Test for using a global function name
1940 &omnifunc = g:OmniFunc2
1941 new | only
1942 setline(1, 'two')
1943 g:OmniFunc2Args = []
1944 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1945 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1946 bw!
1947
1948 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001949 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001950 add(g:LocalOmniFuncArgs, [findstart, base])
1951 return findstart ? 0 : []
1952 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001953 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001954 new | only
1955 setline(1, 'three')
1956 g:LocalOmniFuncArgs = []
1957 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1958 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1959 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001960 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001961 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001962
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001963 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001964 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001965 delfunc OmniFunc1
1966 delfunc OmniFunc2
1967 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001968 %bw!
1969endfunc
1970
1971" Test for different ways of setting the 'thesaurusfunc' option
1972func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001973 func TsrFunc1(callnr, findstart, base)
1974 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001975 return a:findstart ? 0 : []
1976 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001977 func TsrFunc2(findstart, base)
1978 call add(g:TsrFunc2Args, [a:findstart, a:base])
1979 return a:findstart ? 0 : ['sunday']
1980 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001981
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001982 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001983 #" Test for using a function name
1984 LET &thesaurusfunc = 'g:TsrFunc2'
1985 new
1986 call setline(1, 'zero')
1987 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001988 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001989 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1990 bw!
1991
1992 #" Test for using a function()
1993 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1994 new
1995 call setline(1, 'one')
1996 LET g:TsrFunc1Args = []
1997 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1998 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001999 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002000
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002001 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002002 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002003 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002004 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002005 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002006 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002007 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002008 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002009 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002010
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002011 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002012 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002013 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002014 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002015 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002016 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002017 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002018 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002019 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002020
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002021 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002022 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
2023 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002024 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002025 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002026 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002027 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002028 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002029
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002030 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002031 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002032 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002033 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002034 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002035 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002036 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002037 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002038 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002039
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002040 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002041 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002042 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002043 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002044 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002045 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002046 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002047 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002048 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002049
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002050 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00002051 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002052 LET optval = substitute(optval, ' ', '\\ ', 'g')
2053 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002054 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002055 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002056 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002057 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002058 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002059 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002060
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002061 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00002062 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002063 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002064 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002065 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002066 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002067 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002068 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002069
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002070 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002071 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002072 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002073 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002074 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002075 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002076 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002077 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002078
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002079 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00002080 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002081 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002082 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002083 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002084 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002085 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002086 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002087 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002088
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002089 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002090 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002091 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002092 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002093 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002094 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002095 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002096 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002097 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002098
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002099 #" Test for using a lambda function with incorrect return value
2100 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2101 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002102 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002103 call setline(1, 'eight')
2104 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2105 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002106
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002107 #" Test for clearing the 'thesaurusfunc' option
2108 set thesaurusfunc=''
2109 set thesaurusfunc&
2110 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
2111 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002112
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002113 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002114 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002115 call setline(1, 'ten')
2116 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
2117 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002118 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002119 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002120 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002121 bw!
2122
2123 #" Use a buffer-local value and a global value
2124 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002125 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002126 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002127 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002128 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2129 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002130 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002131 new
2132 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002133 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002134 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2135 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002136 call assert_equal([], g:TsrFunc1Args)
2137 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002138 wincmd w
2139 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002140 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002141 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2142 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002143 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002144 :%bw!
2145 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002146 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002147
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002148 " Test for using a script-local function name
2149 func s:TsrFunc3(findstart, base)
2150 call add(g:TsrFunc3Args, [a:findstart, a:base])
2151 return a:findstart ? 0 : []
2152 endfunc
2153 set tsrfu=s:TsrFunc3
2154 new
2155 call setline(1, 'script1')
2156 let g:TsrFunc3Args = []
2157 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2158 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2159 bw!
2160
2161 let &tsrfu = 's:TsrFunc3'
2162 new
2163 call setline(1, 'script2')
2164 let g:TsrFunc3Args = []
2165 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2166 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2167 bw!
2168 delfunc s:TsrFunc3
2169
2170 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002171 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002172 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2173
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002174 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002175 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002176 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002177 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002178 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002179 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002180 bw!
2181
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002182 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2183 func SetTsrFunc()
2184 let params = {'thesaurus': function('g:DictTsrFunc')}
2185 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002186 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002187 func g:DictTsrFunc(_) dict
2188 endfunc
2189 call SetTsrFunc()
2190 new
2191 call SetTsrFunc()
2192 bw
2193 call test_garbagecollect_now()
2194 new
2195 set thesaurusfunc=
2196 wincmd w
2197 %bw!
2198 delfunc SetTsrFunc
2199
2200 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2201 " cause a crash.
2202 func SetLocalTsrFunc()
2203 let params = {'thesaurus': function('g:DictTsrFunc')}
2204 let &l:thesaurusfunc = params.thesaurus
2205 endfunc
2206 call SetLocalTsrFunc()
2207 call test_garbagecollect_now()
2208 call SetLocalTsrFunc()
2209 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002210 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002211 delfunc g:DictTsrFunc
2212 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002213
2214 " Vim9 tests
2215 let lines =<< trim END
2216 vim9script
2217
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002218 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2219 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002220 return findstart ? 0 : []
2221 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002222
2223 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002224 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2225 new | only
2226 setline(1, 'one')
2227 g:Vim9tsrFunc_Args = []
2228 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2229 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2230 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002231
2232 # Test for using a global function name
2233 &thesaurusfunc = g:TsrFunc2
2234 new | only
2235 setline(1, 'two')
2236 g:TsrFunc2Args = []
2237 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2238 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2239 bw!
2240
2241 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002242 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002243 add(g:LocalTsrFuncArgs, [findstart, base])
2244 return findstart ? 0 : []
2245 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002246 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002247 new | only
2248 setline(1, 'three')
2249 g:LocalTsrFuncArgs = []
2250 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2251 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2252 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002253 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002254 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002255
2256 " cleanup
2257 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002258 delfunc TsrFunc1
2259 delfunc TsrFunc2
2260 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002261 %bw!
2262endfunc
2263
Christian Brabandtac72c212022-04-07 21:00:53 +01002264func FooBarComplete(findstart, base)
2265 if a:findstart
2266 return col('.') - 1
2267 else
2268 return ["Foo", "Bar", "}"]
2269 endif
2270endfunc
2271
2272func Test_complete_smartindent()
2273 new
2274 setlocal smartindent completefunc=FooBarComplete
2275
2276 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2277 let result = getline(1,'$')
2278 call assert_equal(['', '{','}',''], result)
2279 bw!
2280 delfunction! FooBarComplete
2281endfunc
2282
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002283func Test_complete_overrun()
2284 " this was going past the end of the copied text
2285 new
2286 sil norm si”0s0 
2287 bwipe!
2288endfunc
2289
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002290func Test_infercase_very_long_line()
2291 " this was truncating the line when inferring case
2292 new
2293 let longLine = "blah "->repeat(300)
2294 let verylongLine = "blah "->repeat(400)
2295 call setline(1, verylongLine)
2296 call setline(2, longLine)
2297 set ic infercase
2298 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2299 call assert_equal(longLine, getline(3))
2300
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002301 " check that the too long text is NUL terminated
2302 %del
2303 norm o
2304 norm 1987ax
2305 exec "norm ox\<C-X>\<C-L>"
2306 call assert_equal(repeat('x', 1987), getline(3))
2307
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002308 bwipe!
2309 set noic noinfercase
2310endfunc
2311
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002312func Test_ins_complete_add()
2313 " this was reading past the end of allocated memory
2314 new
2315 norm o
2316 norm 7o€€
2317 sil! norm o
2318
2319 bwipe!
2320endfunc
2321
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002322func Test_ins_complete_end_of_line()
2323 " this was reading past the end of the line
Bram Moolenaar94722c52023-01-28 19:19:03 +00002324 new
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002325 norm 8o€ý 
2326 sil! norm o
2327
2328 bwipe!
2329endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002330
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002331func s:Tagfunc(t,f,o)
2332 bwipe!
2333 return []
2334endfunc
2335
2336" This was using freed memory, since 'complete' was in a wiped out buffer.
2337" Also using a window that was closed.
2338func Test_tagfunc_wipes_out_buffer()
2339 new
2340 set complete=.,t,w,b,u,i
2341 se tagfunc=s:Tagfunc
2342 sil norm i
2343
2344 bwipe!
2345endfunc
2346
Bram Moolenaar6ac2e432023-03-31 19:32:29 +01002347func Test_ins_complete_popup_position()
2348 CheckScreendump
2349
2350 let lines =<< trim END
2351 vim9script
2352 set nowrap
2353 setline(1, ['one', 'two', 'this is line ', 'four'])
2354 prop_type_add('test', {highlight: 'Error'})
2355 prop_add(3, 0, {
2356 text_align: 'above',
2357 text: 'The quick brown fox jumps over the lazy dog',
2358 type: 'test'
2359 })
2360 END
2361 call writefile(lines, 'XinsPopup', 'D')
2362 let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
2363
2364 call term_sendkeys(buf, "3GA\<C-N>")
2365 call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
2366
2367 call StopVimInTerminal(buf)
2368endfunc
2369
LemonBoy69fb5af2023-10-11 21:55:56 +02002370func GetCompleteInfo()
2371 let g:compl_info = complete_info()
2372 return ''
2373endfunc
2374
nwounkn2e3cd522023-10-17 11:05:38 +02002375func Test_completion_restart()
2376 new
2377 set complete=. completeopt=menuone backspace=2
2378 call setline(1, 'workhorse workhorse')
2379 exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
2380 call assert_equal(1, len(g:compl_info['items']))
2381 call assert_equal('workhorse', g:compl_info['items'][0]['word'])
2382 set complete& completeopt& backspace&
2383 bwipe!
2384endfunc
2385
LemonBoy69fb5af2023-10-11 21:55:56 +02002386func Test_complete_info_index()
2387 new
2388 call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
2389 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2390
2391 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2392
2393 set completeopt=menu,preview
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002394 " Search forward
LemonBoy69fb5af2023-10-11 21:55:56 +02002395 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2396 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002397 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002398 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2399 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002400 call assert_equal(6 , len(g:compl_info['items']))
2401 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2402 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2403 call assert_equal(6 , len(g:compl_info['items']))
2404 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2405 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2406 call assert_equal(6 , len(g:compl_info['items']))
2407 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2408 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2409 call assert_equal(6 , len(g:compl_info['items']))
2410 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2411 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2412 call assert_equal(6 , len(g:compl_info['items']))
2413 " Search forward: unselected item
2414 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2415 call assert_equal(6 , len(g:compl_info['items']))
2416 call assert_equal(-1 , g:compl_info['selected'])
LemonBoy69fb5af2023-10-11 21:55:56 +02002417
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002418 " Search backward
LemonBoy69fb5af2023-10-11 21:55:56 +02002419 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2420 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002421 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002422 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2423 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002424 call assert_equal(6 , len(g:compl_info['items']))
2425 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2426 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2427 call assert_equal(6 , len(g:compl_info['items']))
2428 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2429 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2430 call assert_equal(6 , len(g:compl_info['items']))
2431 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2432 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2433 call assert_equal(6 , len(g:compl_info['items']))
2434 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2435 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2436 call assert_equal(6 , len(g:compl_info['items']))
2437 " search backwards: unselected item
2438 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2439 call assert_equal(6 , len(g:compl_info['items']))
2440 call assert_equal(-1 , g:compl_info['selected'])
2441
2442 " switch direction: forwards, then backwards
2443 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2444 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2445 call assert_equal(6 , len(g:compl_info['items']))
2446 " switch direction: forwards, then backwards, then forwards again
2447 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2448 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2449 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2450 call assert_equal(6 , len(g:compl_info['items']))
2451
2452 " switch direction: backwards, then forwards
2453 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2454 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2455 call assert_equal(6 , len(g:compl_info['items']))
2456 " switch direction: backwards, then forwards, then backwards again
2457 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2458 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2459 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002460
2461 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2462 set completeopt+=noselect
2463 " Search forward.
2464 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2465 call assert_equal(-1, g:compl_info['selected'])
2466
2467 " Search backward.
2468 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2469 call assert_equal(-1, g:compl_info['selected'])
2470
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002471 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
Girish Palya8950bf72024-03-20 20:07:29 +01002472 call assert_equal(5, g:compl_info['selected'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002473 call assert_equal(6 , len(g:compl_info['items']))
2474 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2475 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 +02002476 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002477 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandt57f9ce12023-11-04 09:58:14 +01002478 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2479 call assert_equal(-1, g:compl_info['selected'])
2480 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002481
LemonBoy69fb5af2023-10-11 21:55:56 +02002482 set completeopt&
2483 bwipe!
2484endfunc
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002485
Christian Brabandtfef66302024-01-29 21:46:58 +01002486func Test_complete_changed_complete_info()
2487 CheckRunVimInTerminal
2488 " this used to crash vim, see #13929
2489 let lines =<< trim END
2490 set completeopt=menuone
2491 autocmd CompleteChanged * call complete_info(['items'])
2492 call feedkeys("iii\<cr>\<c-p>")
2493 END
2494 call writefile(lines, 'Xsegfault', 'D')
2495 let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
2496 call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
2497 call StopVimInTerminal(buf)
2498endfunc
2499
zeertzjq0a419e02024-04-02 19:01:14 +02002500func Test_completefunc_first_call_complete_add()
2501 new
2502
2503 func Complete(findstart, base) abort
2504 if a:findstart
2505 let col = col('.')
2506 call complete_add('#')
2507 return col - 1
2508 else
2509 return []
2510 endif
2511 endfunc
2512
2513 set completeopt=longest completefunc=Complete
2514 " This used to cause heap-buffer-overflow
2515 call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:')
2516
2517 delfunc Complete
2518 set completeopt& completefunc&
2519 bwipe!
2520endfunc
2521
glepnira218cc62024-06-03 19:32:39 +02002522func Test_complete_fuzzy_match()
2523 func OnPumChange()
2524 let g:item = get(v:event, 'completed_item', {})
2525 let g:word = get(g:item, 'word', v:null)
2526 endfunction
2527
2528 augroup AAAAA_Group
2529 au!
2530 autocmd CompleteChanged * :call OnPumChange()
2531 augroup END
2532
2533 func Omni_test(findstart, base)
2534 if a:findstart
2535 return col(".")
2536 endif
2537 return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}]
2538 endfunc
zeertzjq551d8c32024-06-05 19:53:32 +02002539
glepnira218cc62024-06-03 19:32:39 +02002540 new
2541 set omnifunc=Omni_test
2542 set completeopt+=noinsert,fuzzy
2543 call feedkeys("Gi\<C-x>\<C-o>", 'tx')
2544 call assert_equal('foo', g:word)
2545 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2546 call assert_equal('fooBaz', g:word)
2547 call feedkeys("S\<C-x>\<C-o>fa", 'tx')
2548 call assert_equal('foobar', g:word)
2549 " select next
2550 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2551 call assert_equal('foobar', g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002552 " can cyclically select next
glepnira218cc62024-06-03 19:32:39 +02002553 call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx')
2554 call assert_equal(v:null, g:word)
2555 " select prev
2556 call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx')
2557 call assert_equal(v:null, g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002558 " can cyclically select prev
glepnira218cc62024-06-03 19:32:39 +02002559 call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx')
2560 call assert_equal('fooBaz', g:word)
2561
glepnirf94c9c42024-06-14 21:11:56 +02002562 func Comp()
2563 call complete(col('.'), ["fooBaz", "foobar", "foobala"])
2564 return ''
2565 endfunc
2566 call feedkeys("i\<C-R>=Comp()\<CR>", 'tx')
2567 call assert_equal('fooBaz', g:word)
2568
glepnira218cc62024-06-03 19:32:39 +02002569 " respect noselect
2570 set completeopt+=noselect
2571 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2572 call assert_equal(v:null, g:word)
2573 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2574 call assert_equal('fooBaz', g:word)
2575
zeertzjqacc87462024-06-16 08:44:05 +02002576 " avoid breaking default completion behavior
glepniraced8c22024-06-15 15:13:05 +02002577 set completeopt=fuzzy,menu
2578 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002579 " Use "!" flag of feedkeys() so that ex_normal_busy is not set and
2580 " ins_compl_check_keys() is not skipped.
2581 " Add a "0" after the <Esc> to avoid waiting for an escape sequence.
2582 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002583 call assert_equal('hello help hero hello', getline('.'))
2584 set completeopt+=noinsert
2585 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002586 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002587 call assert_equal('hello help hero h', getline('.'))
2588
glepnira218cc62024-06-03 19:32:39 +02002589 " clean up
2590 set omnifunc=
2591 bw!
2592 set complete& completeopt&
2593 autocmd! AAAAA_Group
2594 augroup! AAAAA_Group
2595 delfunc OnPumChange
2596 delfunc Omni_test
glepnirf94c9c42024-06-14 21:11:56 +02002597 delfunc Comp
zeertzjq551d8c32024-06-05 19:53:32 +02002598 unlet g:item
2599 unlet g:word
glepnira218cc62024-06-03 19:32:39 +02002600endfunc
2601
zeertzjq8e567472024-06-14 20:04:42 +02002602" Check that tie breaking is stable for completeopt+=fuzzy (which should
2603" behave the same on different platforms).
2604func Test_complete_fuzzy_match_tie()
2605 new
2606 set completeopt+=fuzzy,noselect
2607 call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', ''])
2608
2609 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx')
2610 call assert_equal('aaabbccc', getline('.'))
2611 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx')
2612 call assert_equal('aaabbCCC', getline('.'))
2613 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
2614 call assert_equal('aaabbcccc', getline('.'))
2615 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
2616 call assert_equal('aaabbCCCC', getline('.'))
2617
2618 bwipe!
2619 set completeopt&
2620endfunc
2621
Christian Brabandtfef66302024-01-29 21:46:58 +01002622" vim: shiftwidth=2 sts=2 expandtab nofoldenable