blob: fd4c7cded527df6e78a12bea504d483c36e3efa0 [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
zeertzjq46dcd842024-11-03 09:10:50 +0100878 new | only
879 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
880 set completeopt&
881 setlocal completeopt=menu,fuzzy,noinsert
882 setglobal completeopt=menu,longest
883 call assert_equal('menu,fuzzy,noinsert', &completeopt)
884 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
885 call assert_equal('menu,longest', &g:completeopt)
886 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
887 call assert_equal('foobaz', getline('.'))
888 setlocal bufhidden=wipe
889 new | only!
890 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
891 call assert_equal('menu,longest', &completeopt)
892 call assert_equal('menu,longest', &g:completeopt)
893 call assert_equal('', &l:completeopt)
894 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
895 call assert_equal('foo', getline('.'))
896 bwipe!
897
898 new | only
899 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
900 set completeopt&
901 setlocal completeopt=menu,fuzzy,noinsert
902 set completeopt=menu,longest
903 call assert_equal('menu,longest', &completeopt)
904 call assert_equal('menu,longest', &g:completeopt)
905 call assert_equal('', &l:completeopt)
906 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
907 call assert_equal('foo', getline('.'))
908 setlocal bufhidden=wipe
909 new | only!
910 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
911 call assert_equal('menu,longest', &completeopt)
912 call assert_equal('menu,longest', &g:completeopt)
913 call assert_equal('', &l:completeopt)
914 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
915 call assert_equal('foo', getline('.'))
916 bwipe!
917
zeertzjq529b9ad2024-06-05 20:27:06 +0200918 set completeopt&
919endfunc
bfredl87af60c2022-09-24 11:17:51 +0100920
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200921" Test for completing words following a completed word in a line
922func Test_complete_wrapscan()
923 " complete words from another buffer
924 new
925 call setline(1, ['one two', 'three four'])
926 new
927 setlocal complete=w
928 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
929 call assert_equal('two three four', getline(1))
930 close!
931 " complete words from the current buffer
932 setlocal complete=.
933 %d
934 call setline(1, ['one two', ''])
935 call cursor(2, 1)
936 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
937 call assert_equal('one two one two', getline(2))
938 close!
939endfunc
940
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200941" Test for completing special characters
942func Test_complete_special_chars()
943 new
944 call setline(1, 'int .*[-\^$ func float')
945 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
946 call assert_equal('int .*[-\^$ func float', getline(2))
947 close!
948endfunc
949
950" Test for completion when text is wrapped across lines.
951func Test_complete_across_line()
952 new
953 call setline(1, ['red green blue', 'one two three'])
954 setlocal textwidth=20
955 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
956 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
957 close!
958endfunc
959
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000960" Test for completing words with a '.' at the end of a word.
961func Test_complete_joinspaces()
962 new
963 call setline(1, ['one two.', 'three. four'])
964 set joinspaces
965 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
966 call assert_equal("one two. three. four", getline(3))
967 set joinspaces&
968 bw!
969endfunc
970
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200971" Test for using CTRL-L to add one character when completing matching
972func Test_complete_add_onechar()
973 new
974 call setline(1, ['wool', 'woodwork'])
975 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
976 call assert_equal('woof', getline(3))
977
978 " use 'ignorecase' and backspace to erase characters from the prefix string
979 " and then add letters using CTRL-L
980 %d
981 set ignorecase backspace=2
982 setlocal complete=.
983 call setline(1, ['workhorse', 'workload'])
984 normal Go
nwounkn2e3cd522023-10-17 11:05:38 +0200985 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200986 call assert_equal('workh', getline(3))
987 set ignorecase& backspace&
988 close!
989endfunc
990
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000991" Test for using CTRL-X CTRL-L to complete whole lines lines
992func Test_complete_wholeline()
993 new
994 " complete one-line
995 call setline(1, ['a1', 'a2'])
996 exe "normal ggoa\<C-X>\<C-L>"
997 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
998 " go to the next match (wrapping around the buffer)
999 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
1000 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
1001 " go to the next match
1002 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
1003 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
1004 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
1005 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1006 " repeat the test using CTRL-L
1007 " go to the next match (wrapping around the buffer)
1008 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
1009 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
1010 " go to the next match
1011 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
1012 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
1013 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
1014 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1015 %d
1016 " use CTRL-X CTRL-L to add one more line
1017 call setline(1, ['a1', 'b1'])
1018 setlocal complete=.
1019 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
1020 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
1021 bw!
1022endfunc
1023
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001024" Test insert completion with 'cindent' (adjust the indent)
1025func Test_complete_with_cindent()
1026 new
1027 setlocal cindent
1028 call setline(1, ['if (i == 1)', " j = 2;"])
1029 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
1030 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
1031
1032 %d
1033 call setline(1, ['when while', '{', ''])
1034 setlocal cinkeys+==while
1035 exe "normal Giwh\<C-P> "
1036 call assert_equal("\twhile ", getline('$'))
1037 close!
1038endfunc
1039
1040" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
1041func Test_complete_cmdline()
1042 new
1043 exe "normal icaddb\<C-X>\<C-V>"
1044 call assert_equal('caddbuffer', getline(1))
1045 exe "normal ocall getqf\<C-X>\<C-V>"
1046 call assert_equal('call getqflist(', getline(2))
1047 exe "normal oabcxyz(\<C-X>\<C-V>"
1048 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +02001049 com! -buffer TestCommand1 echo 'TestCommand1'
1050 com! -buffer TestCommand2 echo 'TestCommand2'
1051 write TestCommand1Test
1052 write TestCommand2Test
1053 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
1054 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
1055 call assert_equal('TestCommand2Test', getline(4))
1056 call delete('TestCommand1Test')
1057 call delete('TestCommand2Test')
1058 delcom TestCommand1
1059 delcom TestCommand2
1060 close!
1061endfunc
1062
1063" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
1064func Test_complete_stop()
1065 new
1066 func Save_mode1()
1067 let g:mode1 = mode(1)
1068 return ''
1069 endfunc
1070 func Save_mode2()
1071 let g:mode2 = mode(1)
1072 return ''
1073 endfunc
1074 inoremap <F1> <C-R>=Save_mode1()<CR>
1075 inoremap <F2> <C-R>=Save_mode2()<CR>
1076 call setline(1, ['aaa bbb ccc '])
1077 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1078 call assert_equal('ic', g:mode1)
1079 call assert_equal('i', g:mode2)
1080 call assert_equal('aaa bbb ccc ', getline(1))
1081 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1082 call assert_equal('ic', g:mode1)
1083 call assert_equal('i', g:mode2)
1084 call assert_equal('aaa bbb ccc aaa', getline(1))
1085 set completeopt+=noselect
1086 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1087 call assert_equal('ic', g:mode1)
1088 call assert_equal('i', g:mode2)
1089 call assert_equal('aaa bbb ccc aaa bb', getline(1))
1090 set completeopt&
1091 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1092 call assert_equal('ic', g:mode1)
1093 call assert_equal('i', g:mode2)
1094 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
1095 com! -buffer TestCommand1 echo 'TestCommand1'
1096 com! -buffer TestCommand2 echo 'TestCommand2'
1097 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1098 call assert_equal('ic', g:mode1)
1099 call assert_equal('i', g:mode2)
1100 call assert_equal('TestCommand2', getline(2))
1101 delcom TestCommand1
1102 delcom TestCommand2
1103 unlet g:mode1
1104 unlet g:mode2
1105 iunmap <F1>
1106 iunmap <F2>
1107 delfunc Save_mode1
1108 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001109 close!
1110endfunc
1111
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00001112" Test for typing CTRL-R in insert completion mode to insert a register
1113" content.
1114func Test_complete_reginsert()
1115 new
1116 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
1117
1118 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
1119 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
1120 " inserted.
1121 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
1122 call assert_equal('a123', getline(5))
1123 let @r = "\<C-P>\<C-P>"
1124 exe "normal GCa\<C-P>\<C-R>r"
1125 call assert_equal('a12', getline(5))
1126 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
1127 call assert_equal('a1234x', getline(5))
1128 bw!
1129endfunc
1130
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02001131func Test_issue_7021()
1132 CheckMSWindows
1133
1134 let orig_shellslash = &shellslash
1135 set noshellslash
1136
1137 set completeslash=slash
1138 call assert_false(expand('~') =~ '/')
1139
1140 let &shellslash = orig_shellslash
1141 set completeslash=
1142endfunc
1143
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001144" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
1145func Test_complete_longest_match()
1146 for e in ['latin1', 'utf-8']
1147 exe 'set encoding=' .. e
1148 new
1149 set complete=.
1150 set completeopt=menu,longest
1151 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
1152 exe "normal Gopfx\<C-P>"
1153 call assert_equal('pfx_', getline(5))
1154 bw!
1155 endfor
1156
1157 " Test for completing additional words with longest match set
1158 new
1159 call setline(1, ['abc1', 'abd2'])
1160 exe "normal Goab\<C-P>\<C-X>\<C-P>"
1161 call assert_equal('ab', getline(3))
1162 bw!
1163 set complete& completeopt&
1164endfunc
1165
1166" Test for removing the first displayed completion match and selecting the
1167" match just before that.
1168func Test_complete_erase_firstmatch()
1169 new
1170 call setline(1, ['a12', 'a34', 'a56'])
1171 set complete=.
1172 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1173 call assert_equal('a34', getline('$'))
1174 set complete&
1175 bw!
1176endfunc
1177
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001178" Test for completing words from unloaded buffers
1179func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001180 call writefile(['abc'], "Xfile1", 'D')
1181 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001182 edit Xfile1
1183 edit Xfile2
1184 new | close
1185 enew
1186 bunload Xfile1 Xfile2
1187 set complete=u
1188 " complete from an unloaded buffer
1189 exe "normal! ia\<C-P>"
1190 call assert_equal('abc', getline(1))
1191 exe "normal! od\<C-P>"
1192 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001193
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001194 set complete&
1195 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001196endfunc
1197
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001198" Test for completing whole lines from unloaded buffers
1199func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001200 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001201 edit Xfile1
1202 enew
1203 set complete=u
1204 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1205 call assert_equal('a line2', getline(1))
1206 %d
1207 " completing from an unlisted buffer should fail
1208 bdel Xfile1
1209 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1210 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001211
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001212 set complete&
1213 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001214endfunc
1215
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001216" Test for completing words from unlisted buffers
1217func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001218 call writefile(['abc'], "Xfile1", 'D')
1219 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001220 edit Xfile1
1221 edit Xfile2
1222 new | close
1223 bdel Xfile1 Xfile2
1224 set complete=U
1225 " complete from an unlisted buffer
1226 exe "normal! ia\<C-P>"
1227 call assert_equal('abc', getline(1))
1228 exe "normal! od\<C-P>"
1229 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001230
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001231 set complete&
1232 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001233endfunc
1234
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001235" Test for completing whole lines from unlisted buffers
1236func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001237 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001238 edit Xfile1
1239 enew
1240 set complete=U
zeertzjqc029c132024-03-28 11:37:26 +01001241 " completing from an unloaded buffer should fail
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001242 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1243 call assert_equal('a', getline(1))
1244 %d
1245 bdel Xfile1
1246 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1247 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001248
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001249 set complete&
1250 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001251endfunc
1252
1253" Test for adding a multibyte character using CTRL-L in completion mode
1254func Test_complete_mbyte_char_add()
1255 new
1256 set complete=.
1257 call setline(1, 'abė')
1258 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1259 call assert_equal('abė', getline(2))
1260 " Test for a leader with multibyte character
1261 %d
1262 call setline(1, 'abėĕ')
1263 exe "normal! oabė\<C-P>"
1264 call assert_equal('abėĕ', getline(2))
1265 bw!
1266endfunc
1267
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001268" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1269" not to complete matches from the local buffer. Also test using multiple
1270" <C-X> to cancel the current completion mode.
1271func Test_complete_local_expansion()
1272 new
1273 set complete=t
1274 call setline(1, ['abc', 'def'])
1275 exe "normal! Go\<C-X>\<C-P>"
1276 call assert_equal("def", getline(3))
1277 exe "normal! Go\<C-P>"
1278 call assert_equal("", getline(4))
1279 exe "normal! Go\<C-X>\<C-N>"
1280 call assert_equal("abc", getline(5))
1281 exe "normal! Go\<C-N>"
1282 call assert_equal("", getline(6))
1283
1284 " use multiple <C-X> to cancel the previous completion mode
1285 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1286 call assert_equal("", getline(7))
1287 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1288 call assert_equal("", getline(8))
1289 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1290 call assert_equal("abc", getline(9))
1291
1292 " interrupt the current completion mode
1293 set completeopt=menu,noinsert
1294 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1295 call assert_equal("abc", getline(10))
1296
1297 " when only one <C-X> is used to interrupt, do normal expansion
1298 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1299 call assert_equal("", getline(11))
1300 set completeopt&
1301
1302 " using two <C-X> in non-completion mode and restarting the same mode
1303 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1304 call assert_equal("def", getline(12))
1305
1306 " test for adding a match from the original empty text
1307 %d
1308 call setline(1, 'abc def g')
1309 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1310 call assert_equal('def', getline(2))
1311 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1312 call assert_equal('abc', getline(2))
1313
1314 bw!
1315endfunc
1316
1317" Test for undoing changes after a insert-mode completion
1318func Test_complete_undo()
1319 new
1320 set complete=.
1321 " undo with 'ignorecase'
1322 call setline(1, ['ABOVE', 'BELOW'])
1323 set ignorecase
1324 exe "normal! Goab\<C-G>u\<C-P>"
1325 call assert_equal("ABOVE", getline(3))
1326 undo
1327 call assert_equal("ab", getline(3))
1328 set ignorecase&
1329 %d
1330 " undo with longest match
1331 set completeopt=menu,longest
1332 call setline(1, ['above', 'about'])
1333 exe "normal! Goa\<C-G>u\<C-P>"
1334 call assert_equal("abo", getline(3))
1335 undo
1336 call assert_equal("a", getline(3))
1337 set completeopt&
1338 %d
1339 " undo for line completion
1340 call setline(1, ['above that change', 'below that change'])
1341 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1342 call assert_equal("above that change", getline(3))
1343 undo
1344 call assert_equal("above", getline(3))
1345
1346 bw!
1347endfunc
1348
1349" Test for completing a very long word
1350func Test_complete_long_word()
1351 set complete&
1352 new
1353 call setline(1, repeat('x', 950) .. ' one two three')
1354 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1355 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1356 %d
1357 " should fail when more than 950 characters are in a word
1358 call setline(1, repeat('x', 951) .. ' one two three')
1359 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1360 call assert_equal(repeat('x', 951), getline(2))
1361
1362 " Test for adding a very long word to an existing completion
1363 %d
1364 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1365 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1366 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1367 bw!
1368endfunc
1369
1370" Test for some fields in the complete items used by complete()
1371func Test_complete_items()
1372 func CompleteItems(idx)
1373 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1374 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1375 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1376 \ [#{user_data: 'u9'}],
1377 \ [#{word: "", user_data: 'u10'}],
1378 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1379 call complete(col('.'), items[a:idx])
1380 return ''
1381 endfunc
1382 new
1383 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1384 call assert_equal('u2', v:completed_item.user_data)
1385 call assert_equal('one', getline(1))
1386 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1387 call assert_equal('u3', v:completed_item.user_data)
1388 call assert_equal('one', getline(2))
1389 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1390 call assert_equal('', getline(3))
1391 set completeopt=menu,noinsert
1392 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1393 call assert_equal('oNE', getline(4))
1394 call assert_equal('u8', v:completed_item.user_data)
1395 set completeopt&
1396 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1397 call assert_equal('', getline(5))
1398 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1399 call assert_equal('', getline(6))
1400 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1401 call assert_equal('', getline(7))
1402 call assert_equal('u11', v:completed_item.user_data)
1403 " pass invalid argument to complete()
1404 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1405 call assert_fails('exe cmd', 'E730:')
1406 bw!
1407 delfunc CompleteItems
1408endfunc
1409
1410" Test for the "refresh" item in the dict returned by an insert completion
1411" function
1412func Test_complete_item_refresh_always()
1413 let g:CallCount = 0
1414 func! Tcomplete(findstart, base)
1415 if a:findstart
1416 " locate the start of the word
1417 let line = getline('.')
1418 let start = col('.') - 1
1419 while start > 0 && line[start - 1] =~ '\a'
1420 let start -= 1
1421 endwhile
1422 return start
1423 else
1424 let g:CallCount += 1
1425 let res = ["update1", "update12", "update123"]
1426 return #{words: res, refresh: 'always'}
1427 endif
1428 endfunc
1429 new
1430 set completeopt=menu,longest
1431 set completefunc=Tcomplete
1432 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1433 call assert_equal('up', getline(1))
1434 call assert_equal(2, g:CallCount)
1435 set completeopt&
1436 set completefunc&
1437 bw!
1438 delfunc Tcomplete
1439endfunc
1440
1441" Test for completing from a thesaurus file without read permission
1442func Test_complete_unreadable_thesaurus_file()
1443 CheckUnix
1444 CheckNotRoot
1445
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001446 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001447 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001448 new
1449 set complete=sXfile
1450 exe "normal! ia\<C-P>"
1451 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001452
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001453 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001454 set complete&
1455endfunc
1456
Bram Moolenaarcc233582020-12-12 13:32:07 +01001457" Test to ensure 'Scanning...' messages are not recorded in messages history
1458func Test_z1_complete_no_history()
1459 new
1460 messages clear
1461 let currmess = execute('messages')
1462 setlocal dictionary=README.txt
1463 exe "normal owh\<C-X>\<C-K>"
1464 exe "normal owh\<C-N>"
1465 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001466 bwipe!
1467endfunc
1468
1469" A mapping is not used for the key after CTRL-X.
1470func Test_no_mapping_for_ctrl_x_key()
1471 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001472 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001473 setlocal dictionary=README.txt
1474 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1475 call assert_equal('example ', getline(1))
1476 call assert_false(exists('was_mapped'))
1477 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001478endfunc
1479
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001480" Test for different ways of setting the 'completefunc' option
1481func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001482 func CompleteFunc1(callnr, findstart, base)
1483 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1484 return a:findstart ? 0 : []
1485 endfunc
1486 func CompleteFunc2(findstart, base)
1487 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001488 return a:findstart ? 0 : []
1489 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001490
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001491 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001492 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001493 LET &completefunc = 'g:CompleteFunc2'
1494 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001495 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001496 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001497 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001498 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001499 bw!
1500
1501 #" Test for using a function()
1502 set completefunc=function('g:CompleteFunc1',\ [10])
1503 new
1504 call setline(1, 'one')
1505 LET g:CompleteFunc1Args = []
1506 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1507 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001508 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001509
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001510 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001511 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001512 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001513 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001514 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001515 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001516 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001517 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001518 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001519
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001520 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001521 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001522 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001523 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001524 call setline(1, 'two')
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([[12, 1, ''], [12, 0, 'two']], 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 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001531 set completefunc=funcref('g:CompleteFunc1',\ [13])
1532 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001533 call setline(1, 'three')
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([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001537 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001538
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001539 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001540 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001541 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001542 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001543 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001544 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001545 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001546 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001547 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001548
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001549 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001550 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001551 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001552 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001553 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001554 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001555 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001556 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001557 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001558
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001559 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001560 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001561 LET optval = substitute(optval, ' ', '\\ ', 'g')
1562 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001563 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001564 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001565 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001566 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001567 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001568 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001569
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001570 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001571 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001572 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001573 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001574 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001575 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001576 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001577 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001578
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001579 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001580 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001581 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001582 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001583 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001584 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001585 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001586 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001587
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001588 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001589 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001590 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001591 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001592 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001593 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001594 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001595 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001596 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001597
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001598 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001599 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001600 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001601 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001603 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001604 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001605 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001606 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001607
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001608 #" Test for using a lambda function with incorrect return value
1609 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1610 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001611 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001612 call setline(1, 'eight')
1613 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1614 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001615
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001616 #" Test for clearing the 'completefunc' option
1617 set completefunc=''
1618 set completefunc&
1619 call assert_fails("set completefunc=function('abc')", "E700:")
1620 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001621
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001623 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 call setline(1, 'five')
1625 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1626 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001629 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 bw!
1631 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001632 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001633
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001634 " Test for using a script-local function name
1635 func s:CompleteFunc3(findstart, base)
1636 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1637 return a:findstart ? 0 : []
1638 endfunc
1639 set completefunc=s:CompleteFunc3
1640 new
1641 call setline(1, 'script1')
1642 let g:CompleteFunc3Args = []
1643 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1644 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1645 bw!
1646
1647 let &completefunc = 's:CompleteFunc3'
1648 new
1649 call setline(1, 'script2')
1650 let g:CompleteFunc3Args = []
1651 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1652 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1653 bw!
1654 delfunc s:CompleteFunc3
1655
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001656 " In Vim9 script s: can be omitted
1657 let lines =<< trim END
1658 vim9script
1659 var CompleteFunc4Args = []
1660 def CompleteFunc4(findstart: bool, base: string): any
1661 add(CompleteFunc4Args, [findstart, base])
1662 return findstart ? 0 : []
1663 enddef
1664 set completefunc=CompleteFunc4
1665 new
1666 setline(1, 'script1')
1667 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1668 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1669 bw!
1670 END
1671 call v9.CheckScriptSuccess(lines)
1672
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001673 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001674 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001675 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1676
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001677 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001678 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001679 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001680 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001681 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001682 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001683
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 " set 'completefunc' to a partial with dict. This used to cause a crash.
1685 func SetCompleteFunc()
1686 let params = {'complete': function('g:DictCompleteFunc')}
1687 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001688 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 func g:DictCompleteFunc(_) dict
1690 endfunc
1691 call SetCompleteFunc()
1692 new
1693 call SetCompleteFunc()
1694 bw
1695 call test_garbagecollect_now()
1696 new
1697 set completefunc=
1698 wincmd w
1699 set completefunc=
1700 %bw!
1701 delfunc g:DictCompleteFunc
1702 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001703
1704 " Vim9 tests
1705 let lines =<< trim END
1706 vim9script
1707
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001708 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1709 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001710 return findstart ? 0 : []
1711 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001712
1713 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001714 set completefunc=function('Vim9CompleteFunc',\ [60])
1715 new | only
1716 setline(1, 'one')
1717 g:Vim9completeFuncArgs = []
1718 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1719 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1720 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001721
1722 # Test for using a global function name
1723 &completefunc = g:CompleteFunc2
1724 new | only
1725 setline(1, 'two')
1726 g:CompleteFunc2Args = []
1727 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1728 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1729 bw!
1730
1731 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001732 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001733 add(g:LocalCompleteFuncArgs, [findstart, base])
1734 return findstart ? 0 : []
1735 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001736 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001737 new | only
1738 setline(1, 'three')
1739 g:LocalCompleteFuncArgs = []
1740 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1741 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1742 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001743 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001744 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001745
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001746 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001747 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001748 delfunc CompleteFunc1
1749 delfunc CompleteFunc2
1750 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001751 %bw!
1752endfunc
1753
1754" Test for different ways of setting the 'omnifunc' option
1755func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001756 func OmniFunc1(callnr, findstart, base)
1757 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1758 return a:findstart ? 0 : []
1759 endfunc
1760 func OmniFunc2(findstart, base)
1761 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001762 return a:findstart ? 0 : []
1763 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001764
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001765 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001766 #" Test for using a function name
1767 LET &omnifunc = 'g:OmniFunc2'
1768 new
1769 call setline(1, 'zero')
1770 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001771 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001772 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1773 bw!
1774
1775 #" Test for using a function()
1776 set omnifunc=function('g:OmniFunc1',\ [10])
1777 new
1778 call setline(1, 'one')
1779 LET g:OmniFunc1Args = []
1780 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1781 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001782 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001783
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001784 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001785 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001786 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001787 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001788 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001789 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001790 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001791 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001792 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001793
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001794 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001795 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001796 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001797 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001798 call setline(1, 'two')
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([[12, 1, ''], [12, 0, 'two']], 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 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001805 set omnifunc=funcref('g:OmniFunc1',\ [13])
1806 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001807 call setline(1, 'three')
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([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001811 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001812
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001813 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001814 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001815 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001816 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001817 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001818 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001819 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001820 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001821 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001822
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001823 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001824 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001825 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001826 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001827 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001828 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001829 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001830 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001831 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001832
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001833 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001834 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001835 LET optval = substitute(optval, ' ', '\\ ', 'g')
1836 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001837 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001838 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001839 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001840 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001841 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001842 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001843
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001844 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001845 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001846 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001847 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001848 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001849 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001850 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001851 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001852
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001853 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001854 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001855 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001856 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001857 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001858 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001859 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001860 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001861
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001862 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001863 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001864 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001865 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001866 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001867 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001868 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001869 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001870 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001871
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001872 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001873 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001874 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001875 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001876 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001877 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001878 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001879 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001880 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001881
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001882 #" Test for using a lambda function with incorrect return value
1883 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1884 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001885 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001886 call setline(1, 'eight')
1887 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1888 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001889
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001890 #" Test for clearing the 'omnifunc' option
1891 set omnifunc=''
1892 set omnifunc&
1893 call assert_fails("set omnifunc=function('abc')", "E700:")
1894 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001895
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001897 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001898 call setline(1, 'nine')
1899 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1900 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001901 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001903 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 bw!
1905 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001906 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001907
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001908 " Test for using a script-local function name
1909 func s:OmniFunc3(findstart, base)
1910 call add(g:OmniFunc3Args, [a:findstart, a:base])
1911 return a:findstart ? 0 : []
1912 endfunc
1913 set omnifunc=s:OmniFunc3
1914 new
1915 call setline(1, 'script1')
1916 let g:OmniFunc3Args = []
1917 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1918 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1919 bw!
1920
1921 let &omnifunc = 's:OmniFunc3'
1922 new
1923 call setline(1, 'script2')
1924 let g:OmniFunc3Args = []
1925 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1926 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1927 bw!
1928 delfunc s:OmniFunc3
1929
1930 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001931 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001932 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1933
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001934 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001935 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001936 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001937 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001938 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001939 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001940
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1942 func SetOmniFunc()
1943 let params = {'omni': function('g:DictOmniFunc')}
1944 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001945 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 func g:DictOmniFunc(_) dict
1947 endfunc
1948 call SetOmniFunc()
1949 new
1950 call SetOmniFunc()
1951 bw
1952 call test_garbagecollect_now()
1953 new
1954 set omnifunc=
1955 wincmd w
1956 set omnifunc=
1957 %bw!
1958 delfunc g:DictOmniFunc
1959 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001960
1961 " Vim9 tests
1962 let lines =<< trim END
1963 vim9script
1964
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001965 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1966 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001967 return findstart ? 0 : []
1968 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001969
1970 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001971 set omnifunc=function('Vim9omniFunc',\ [60])
1972 new | only
1973 setline(1, 'one')
1974 g:Vim9omniFunc_Args = []
1975 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1976 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1977 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001978
1979 # Test for using a global function name
1980 &omnifunc = g:OmniFunc2
1981 new | only
1982 setline(1, 'two')
1983 g:OmniFunc2Args = []
1984 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1985 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1986 bw!
1987
1988 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001989 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001990 add(g:LocalOmniFuncArgs, [findstart, base])
1991 return findstart ? 0 : []
1992 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001993 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001994 new | only
1995 setline(1, 'three')
1996 g:LocalOmniFuncArgs = []
1997 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1998 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1999 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002000 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002001 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00002002
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002003 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002004 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002005 delfunc OmniFunc1
2006 delfunc OmniFunc2
2007 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002008 %bw!
2009endfunc
2010
2011" Test for different ways of setting the 'thesaurusfunc' option
2012func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002013 func TsrFunc1(callnr, findstart, base)
2014 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002015 return a:findstart ? 0 : []
2016 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002017 func TsrFunc2(findstart, base)
2018 call add(g:TsrFunc2Args, [a:findstart, a:base])
2019 return a:findstart ? 0 : ['sunday']
2020 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002021
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002022 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002023 #" Test for using a function name
2024 LET &thesaurusfunc = 'g:TsrFunc2'
2025 new
2026 call setline(1, 'zero')
2027 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002028 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002029 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
2030 bw!
2031
2032 #" Test for using a function()
2033 set thesaurusfunc=function('g:TsrFunc1',\ [10])
2034 new
2035 call setline(1, 'one')
2036 LET g:TsrFunc1Args = []
2037 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2038 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002039 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002040
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002041 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002042 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002043 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002044 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002045 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002046 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002047 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002048 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002049 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002050
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002051 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002052 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002053 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002054 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002055 call setline(1, 'two')
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([[12, 1, ''], [12, 0, 'two']], 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 funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002062 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
2063 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002064 call setline(1, 'three')
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([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002068 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002069
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002070 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002071 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002072 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002073 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002074 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002075 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002076 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002077 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002078 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002079
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002080 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002081 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002082 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002083 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002084 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002085 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002086 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002087 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002088 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002089
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002090 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00002091 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002092 LET optval = substitute(optval, ' ', '\\ ', 'g')
2093 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002094 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002095 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002096 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002097 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002098 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002099 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002100
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002101 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00002102 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002103 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002104 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002105 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002106 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002107 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002108 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002109
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002110 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002111 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002112 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002113 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002114 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002115 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002116 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002117 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002118
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002119 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00002120 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002121 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002122 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002123 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002124 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002125 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002126 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002127 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002128
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002129 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002130 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002131 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002132 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002133 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002134 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002135 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002136 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002137 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002138
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002139 #" Test for using a lambda function with incorrect return value
2140 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2141 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002142 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002143 call setline(1, 'eight')
2144 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2145 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002146
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002147 #" Test for clearing the 'thesaurusfunc' option
2148 set thesaurusfunc=''
2149 set thesaurusfunc&
2150 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
2151 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002152
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002153 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002154 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002155 call setline(1, 'ten')
2156 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
2157 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002158 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002159 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002160 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002161 bw!
2162
2163 #" Use a buffer-local value and a global value
2164 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002165 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002166 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002167 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002168 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2169 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002170 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002171 new
2172 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002173 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002174 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2175 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002176 call assert_equal([], g:TsrFunc1Args)
2177 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002178 wincmd w
2179 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002180 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002181 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2182 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002183 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002184 :%bw!
2185 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002186 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002187
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002188 " Test for using a script-local function name
2189 func s:TsrFunc3(findstart, base)
2190 call add(g:TsrFunc3Args, [a:findstart, a:base])
2191 return a:findstart ? 0 : []
2192 endfunc
zeertzjq6eda2692024-11-03 09:23:33 +01002193
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002194 set tsrfu=s:TsrFunc3
2195 new
2196 call setline(1, 'script1')
2197 let g:TsrFunc3Args = []
2198 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2199 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2200 bw!
2201
2202 let &tsrfu = 's:TsrFunc3'
2203 new
2204 call setline(1, 'script2')
2205 let g:TsrFunc3Args = []
2206 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2207 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2208 bw!
zeertzjq6eda2692024-11-03 09:23:33 +01002209
2210 new | only
2211 set thesaurusfunc=
2212 setlocal thesaurusfunc=NoSuchFunc
2213 setglobal thesaurusfunc=s:TsrFunc3
2214 call assert_equal('NoSuchFunc', &thesaurusfunc)
2215 call assert_equal('NoSuchFunc', &l:thesaurusfunc)
2216 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2217 new | only
2218 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2219 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2220 call assert_equal('', &l:thesaurusfunc)
2221 call setline(1, 'script1')
2222 let g:TsrFunc3Args = []
2223 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2224 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2225 bw!
2226
2227 new | only
2228 set thesaurusfunc=
2229 setlocal thesaurusfunc=NoSuchFunc
2230 set thesaurusfunc=s:TsrFunc3
2231 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2232 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2233 call assert_equal('', &l:thesaurusfunc)
2234 call setline(1, 'script1')
2235 let g:TsrFunc3Args = []
2236 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2237 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2238 setlocal bufhidden=wipe
2239 new | only!
2240 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2241 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2242 call assert_equal('', &l:thesaurusfunc)
2243 call setline(1, 'script1')
2244 let g:TsrFunc3Args = []
2245 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2246 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2247 bw!
2248
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002249 delfunc s:TsrFunc3
2250
2251 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002252 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002253 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2254
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002255 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002256 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002257 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002258 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002259 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002260 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002261 bw!
2262
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002263 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2264 func SetTsrFunc()
2265 let params = {'thesaurus': function('g:DictTsrFunc')}
2266 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002267 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002268 func g:DictTsrFunc(_) dict
2269 endfunc
2270 call SetTsrFunc()
2271 new
2272 call SetTsrFunc()
2273 bw
2274 call test_garbagecollect_now()
2275 new
2276 set thesaurusfunc=
2277 wincmd w
2278 %bw!
2279 delfunc SetTsrFunc
2280
2281 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2282 " cause a crash.
2283 func SetLocalTsrFunc()
2284 let params = {'thesaurus': function('g:DictTsrFunc')}
2285 let &l:thesaurusfunc = params.thesaurus
2286 endfunc
2287 call SetLocalTsrFunc()
2288 call test_garbagecollect_now()
2289 call SetLocalTsrFunc()
2290 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002291 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002292 delfunc g:DictTsrFunc
2293 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002294
2295 " Vim9 tests
2296 let lines =<< trim END
2297 vim9script
2298
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002299 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2300 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002301 return findstart ? 0 : []
2302 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002303
2304 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002305 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2306 new | only
2307 setline(1, 'one')
2308 g:Vim9tsrFunc_Args = []
2309 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2310 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2311 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002312
2313 # Test for using a global function name
2314 &thesaurusfunc = g:TsrFunc2
2315 new | only
2316 setline(1, 'two')
2317 g:TsrFunc2Args = []
2318 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2319 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2320 bw!
2321
2322 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002323 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002324 add(g:LocalTsrFuncArgs, [findstart, base])
2325 return findstart ? 0 : []
2326 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002327 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002328 new | only
2329 setline(1, 'three')
2330 g:LocalTsrFuncArgs = []
2331 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2332 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2333 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002334 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002335 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002336
2337 " cleanup
2338 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002339 delfunc TsrFunc1
2340 delfunc TsrFunc2
2341 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002342 %bw!
2343endfunc
2344
Christian Brabandtac72c212022-04-07 21:00:53 +01002345func FooBarComplete(findstart, base)
2346 if a:findstart
2347 return col('.') - 1
2348 else
2349 return ["Foo", "Bar", "}"]
2350 endif
2351endfunc
2352
2353func Test_complete_smartindent()
2354 new
2355 setlocal smartindent completefunc=FooBarComplete
2356
2357 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2358 let result = getline(1,'$')
2359 call assert_equal(['', '{','}',''], result)
2360 bw!
2361 delfunction! FooBarComplete
2362endfunc
2363
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002364func Test_complete_overrun()
2365 " this was going past the end of the copied text
2366 new
2367 sil norm si”0s0 
2368 bwipe!
2369endfunc
2370
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002371func Test_infercase_very_long_line()
2372 " this was truncating the line when inferring case
2373 new
2374 let longLine = "blah "->repeat(300)
2375 let verylongLine = "blah "->repeat(400)
2376 call setline(1, verylongLine)
2377 call setline(2, longLine)
2378 set ic infercase
2379 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2380 call assert_equal(longLine, getline(3))
2381
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002382 " check that the too long text is NUL terminated
2383 %del
2384 norm o
2385 norm 1987ax
2386 exec "norm ox\<C-X>\<C-L>"
2387 call assert_equal(repeat('x', 1987), getline(3))
2388
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002389 bwipe!
2390 set noic noinfercase
2391endfunc
2392
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002393func Test_ins_complete_add()
2394 " this was reading past the end of allocated memory
2395 new
2396 norm o
2397 norm 7o€€
2398 sil! norm o
2399
2400 bwipe!
2401endfunc
2402
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002403func Test_ins_complete_end_of_line()
2404 " this was reading past the end of the line
Bram Moolenaar94722c52023-01-28 19:19:03 +00002405 new
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002406 norm 8o€ý 
2407 sil! norm o
2408
2409 bwipe!
2410endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002411
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002412func s:Tagfunc(t,f,o)
2413 bwipe!
2414 return []
2415endfunc
2416
2417" This was using freed memory, since 'complete' was in a wiped out buffer.
2418" Also using a window that was closed.
2419func Test_tagfunc_wipes_out_buffer()
2420 new
2421 set complete=.,t,w,b,u,i
2422 se tagfunc=s:Tagfunc
2423 sil norm i
2424
2425 bwipe!
2426endfunc
2427
Bram Moolenaar6ac2e432023-03-31 19:32:29 +01002428func Test_ins_complete_popup_position()
2429 CheckScreendump
2430
2431 let lines =<< trim END
2432 vim9script
2433 set nowrap
2434 setline(1, ['one', 'two', 'this is line ', 'four'])
2435 prop_type_add('test', {highlight: 'Error'})
2436 prop_add(3, 0, {
2437 text_align: 'above',
2438 text: 'The quick brown fox jumps over the lazy dog',
2439 type: 'test'
2440 })
2441 END
2442 call writefile(lines, 'XinsPopup', 'D')
2443 let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
2444
2445 call term_sendkeys(buf, "3GA\<C-N>")
2446 call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
2447
2448 call StopVimInTerminal(buf)
2449endfunc
2450
LemonBoy69fb5af2023-10-11 21:55:56 +02002451func GetCompleteInfo()
2452 let g:compl_info = complete_info()
2453 return ''
2454endfunc
2455
nwounkn2e3cd522023-10-17 11:05:38 +02002456func Test_completion_restart()
2457 new
2458 set complete=. completeopt=menuone backspace=2
2459 call setline(1, 'workhorse workhorse')
2460 exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
2461 call assert_equal(1, len(g:compl_info['items']))
2462 call assert_equal('workhorse', g:compl_info['items'][0]['word'])
2463 set complete& completeopt& backspace&
2464 bwipe!
2465endfunc
2466
LemonBoy69fb5af2023-10-11 21:55:56 +02002467func Test_complete_info_index()
2468 new
2469 call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
2470 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2471
2472 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2473
2474 set completeopt=menu,preview
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002475 " Search forward
LemonBoy69fb5af2023-10-11 21:55:56 +02002476 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2477 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002478 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002479 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2480 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002481 call assert_equal(6 , len(g:compl_info['items']))
2482 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2483 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2484 call assert_equal(6 , len(g:compl_info['items']))
2485 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2486 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2487 call assert_equal(6 , len(g:compl_info['items']))
2488 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2489 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2490 call assert_equal(6 , len(g:compl_info['items']))
2491 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2492 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2493 call assert_equal(6 , len(g:compl_info['items']))
2494 " Search forward: unselected item
2495 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2496 call assert_equal(6 , len(g:compl_info['items']))
2497 call assert_equal(-1 , g:compl_info['selected'])
LemonBoy69fb5af2023-10-11 21:55:56 +02002498
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002499 " Search backward
LemonBoy69fb5af2023-10-11 21:55:56 +02002500 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2501 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002502 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002503 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2504 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002505 call assert_equal(6 , len(g:compl_info['items']))
2506 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2507 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2508 call assert_equal(6 , len(g:compl_info['items']))
2509 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2510 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2511 call assert_equal(6 , len(g:compl_info['items']))
2512 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2513 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2514 call assert_equal(6 , len(g:compl_info['items']))
2515 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2516 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2517 call assert_equal(6 , len(g:compl_info['items']))
2518 " search backwards: unselected item
2519 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2520 call assert_equal(6 , len(g:compl_info['items']))
2521 call assert_equal(-1 , g:compl_info['selected'])
2522
2523 " switch direction: forwards, then backwards
2524 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2525 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2526 call assert_equal(6 , len(g:compl_info['items']))
2527 " switch direction: forwards, then backwards, then forwards again
2528 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2529 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2530 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2531 call assert_equal(6 , len(g:compl_info['items']))
2532
2533 " switch direction: backwards, then forwards
2534 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2535 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2536 call assert_equal(6 , len(g:compl_info['items']))
2537 " switch direction: backwards, then forwards, then backwards again
2538 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2539 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2540 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002541
2542 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2543 set completeopt+=noselect
2544 " Search forward.
2545 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2546 call assert_equal(-1, g:compl_info['selected'])
2547
2548 " Search backward.
2549 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2550 call assert_equal(-1, g:compl_info['selected'])
2551
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002552 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
Girish Palya8950bf72024-03-20 20:07:29 +01002553 call assert_equal(5, g:compl_info['selected'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002554 call assert_equal(6 , len(g:compl_info['items']))
2555 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2556 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 +02002557 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002558 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandt57f9ce12023-11-04 09:58:14 +01002559 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2560 call assert_equal(-1, g:compl_info['selected'])
2561 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002562
LemonBoy69fb5af2023-10-11 21:55:56 +02002563 set completeopt&
2564 bwipe!
2565endfunc
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002566
Christian Brabandtfef66302024-01-29 21:46:58 +01002567func Test_complete_changed_complete_info()
2568 CheckRunVimInTerminal
2569 " this used to crash vim, see #13929
2570 let lines =<< trim END
2571 set completeopt=menuone
2572 autocmd CompleteChanged * call complete_info(['items'])
2573 call feedkeys("iii\<cr>\<c-p>")
2574 END
2575 call writefile(lines, 'Xsegfault', 'D')
2576 let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
2577 call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
2578 call StopVimInTerminal(buf)
2579endfunc
2580
zeertzjq0a419e02024-04-02 19:01:14 +02002581func Test_completefunc_first_call_complete_add()
2582 new
2583
2584 func Complete(findstart, base) abort
2585 if a:findstart
2586 let col = col('.')
2587 call complete_add('#')
2588 return col - 1
2589 else
2590 return []
2591 endif
2592 endfunc
2593
2594 set completeopt=longest completefunc=Complete
2595 " This used to cause heap-buffer-overflow
2596 call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:')
2597
2598 delfunc Complete
2599 set completeopt& completefunc&
2600 bwipe!
2601endfunc
2602
glepnira218cc62024-06-03 19:32:39 +02002603func Test_complete_fuzzy_match()
2604 func OnPumChange()
2605 let g:item = get(v:event, 'completed_item', {})
2606 let g:word = get(g:item, 'word', v:null)
2607 endfunction
2608
2609 augroup AAAAA_Group
2610 au!
2611 autocmd CompleteChanged * :call OnPumChange()
2612 augroup END
2613
2614 func Omni_test(findstart, base)
2615 if a:findstart
2616 return col(".")
2617 endif
2618 return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}]
2619 endfunc
zeertzjq551d8c32024-06-05 19:53:32 +02002620
glepnira218cc62024-06-03 19:32:39 +02002621 new
2622 set omnifunc=Omni_test
2623 set completeopt+=noinsert,fuzzy
2624 call feedkeys("Gi\<C-x>\<C-o>", 'tx')
2625 call assert_equal('foo', g:word)
2626 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2627 call assert_equal('fooBaz', g:word)
2628 call feedkeys("S\<C-x>\<C-o>fa", 'tx')
2629 call assert_equal('foobar', g:word)
2630 " select next
2631 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2632 call assert_equal('foobar', g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002633 " can cyclically select next
glepnira218cc62024-06-03 19:32:39 +02002634 call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx')
2635 call assert_equal(v:null, g:word)
2636 " select prev
2637 call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx')
2638 call assert_equal(v:null, g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002639 " can cyclically select prev
glepnira218cc62024-06-03 19:32:39 +02002640 call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx')
2641 call assert_equal('fooBaz', g:word)
2642
glepnirf94c9c42024-06-14 21:11:56 +02002643 func Comp()
2644 call complete(col('.'), ["fooBaz", "foobar", "foobala"])
2645 return ''
2646 endfunc
2647 call feedkeys("i\<C-R>=Comp()\<CR>", 'tx')
2648 call assert_equal('fooBaz', g:word)
2649
glepnira218cc62024-06-03 19:32:39 +02002650 " respect noselect
2651 set completeopt+=noselect
2652 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2653 call assert_equal(v:null, g:word)
2654 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2655 call assert_equal('fooBaz', g:word)
2656
zeertzjqacc87462024-06-16 08:44:05 +02002657 " avoid breaking default completion behavior
glepniraced8c22024-06-15 15:13:05 +02002658 set completeopt=fuzzy,menu
2659 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002660 " Use "!" flag of feedkeys() so that ex_normal_busy is not set and
2661 " ins_compl_check_keys() is not skipped.
2662 " Add a "0" after the <Esc> to avoid waiting for an escape sequence.
2663 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002664 call assert_equal('hello help hero hello', getline('.'))
2665 set completeopt+=noinsert
2666 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002667 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002668 call assert_equal('hello help hero h', getline('.'))
2669
glepnir8159fb12024-07-17 20:32:54 +02002670 set completeopt-=noinsert
2671 call setline(1, ['xyz yxz x'])
2672 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2673 call assert_equal('xyz yxz xyz', getline('.'))
2674 " can fuzzy get yxz when use Ctrl-N twice
2675 call setline(1, ['xyz yxz x'])
2676 call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2677 call assert_equal('xyz yxz yxz', getline('.'))
2678
2679 call setline(1, ['你好 你'])
2680 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2681 call assert_equal('你好 你好', getline('.'))
2682 call setline(1, ['你的 我的 的'])
2683 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2684 call assert_equal('你的 我的 你的', getline('.'))
2685 " can fuzzy get multiple-byte word when use Ctrl-N twice
2686 call setline(1, ['你的 我的 的'])
2687 call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2688 call assert_equal('你的 我的 我的', getline('.'))
2689
2690 " respect wrapscan
2691 set nowrapscan
2692 call setline(1, ["xyz", "yxz", ""])
2693 call cursor(3, 1)
2694 call feedkeys("Sy\<C-X>\<C-N>\<Esc>0", 'tx!')
2695 call assert_equal('y', getline('.'))
2696 set wrapscan
2697 call feedkeys("Sy\<C-X>\<C-N>\<Esc>0", 'tx!')
2698 call assert_equal('xyz', getline('.'))
2699
2700 " fuzzy on file
2701 call writefile([''], 'fobar', 'D')
2702 call writefile([''], 'foobar', 'D')
2703 call setline(1, ['fob'])
2704 call cursor(1, 1)
2705 call feedkeys("A\<C-X>\<C-f>\<Esc>0", 'tx!')
2706 call assert_equal('fobar', getline('.'))
2707 call feedkeys("Sfob\<C-X>\<C-f>\<C-N>\<Esc>0", 'tx!')
2708 call assert_equal('foobar', getline('.'))
glepnir0be03e12024-07-19 16:45:05 +02002709 call feedkeys("S../\<C-X>\<C-f>\<Esc>0", 'tx!')
2710 call assert_match('../*', getline('.'))
2711 call feedkeys("S../td\<C-X>\<C-f>\<Esc>0", 'tx!')
2712 call assert_match('../testdir', getline('.'))
glepnir8159fb12024-07-17 20:32:54 +02002713
2714 " can get completion from other buffer
2715 set completeopt=fuzzy,menu,menuone
2716 vnew
2717 call setline(1, ["completeness,", "compatibility", "Composite", "Omnipotent"])
2718 wincmd p
2719 call feedkeys("Somp\<C-N>\<Esc>0", 'tx!')
2720 call assert_equal('completeness', getline('.'))
2721 call feedkeys("Somp\<C-N>\<C-N>\<Esc>0", 'tx!')
2722 call assert_equal('compatibility', getline('.'))
2723 call feedkeys("Somp\<C-P>\<Esc>0", 'tx!')
2724 call assert_equal('Omnipotent', getline('.'))
2725 call feedkeys("Somp\<C-P>\<C-P>\<Esc>0", 'tx!')
2726 call assert_equal('Composite', getline('.'))
glepnir0be03e12024-07-19 16:45:05 +02002727 call feedkeys("S omp\<C-N>\<Esc>0", 'tx!')
2728 call assert_equal(' completeness', getline('.'))
glepnir8159fb12024-07-17 20:32:54 +02002729
2730 " fuzzy on whole line completion
2731 call setline(1, ["world is on fire", "no one can save me but you", 'user can execute', ''])
2732 call cursor(4, 1)
2733 call feedkeys("Swio\<C-X>\<C-L>\<Esc>0", 'tx!')
2734 call assert_equal('world is on fire', getline('.'))
2735 call feedkeys("Su\<C-X>\<C-L>\<C-P>\<Esc>0", 'tx!')
2736 call assert_equal('no one can save me but you', getline('.'))
2737
glepnir7cfe6932024-09-15 20:06:28 +02002738 " issue #15412
2739 call setline(1, ['alpha bravio charlie'])
2740 call feedkeys("Salpha\<C-X>\<C-N>\<Esc>0", 'tx!')
2741 call assert_equal('alpha bravio', getline('.'))
2742 call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!')
2743 call assert_equal('alpha', getline('.'))
2744 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2745 call assert_equal('alpha bravio', getline('.'))
2746 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2747 call assert_equal('alpha bravio charlie', getline('.'))
2748
2749 set complete-=i
2750 call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!')
2751 call assert_equal('alpha', getline('.'))
2752 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2753 call assert_equal('alpha bravio', getline('.'))
2754 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2755 call assert_equal('alpha bravio charlie', getline('.'))
2756
2757 call setline(1, ['alpha bravio charlie', 'alpha another'])
2758 call feedkeys("Salpha\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2759 call assert_equal('alpha another', getline('.'))
2760 call setline(1, ['你好 我好', '你好 他好'])
2761 call feedkeys("S你好\<C-X>\<C-N>\<Esc>0", 'tx!')
2762 call assert_equal('你好 我好', getline('.'))
2763 call feedkeys("S你好\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2764 call assert_equal('你好 他好', getline('.'))
2765
glepnir753794b2024-08-20 19:58:44 +02002766 " issue #15526
2767 set completeopt=fuzzy,menuone,menu,noselect
2768 call setline(1, ['Text', 'ToText', ''])
glepnir7cfe6932024-09-15 20:06:28 +02002769 call cursor(3, 1)
glepnir753794b2024-08-20 19:58:44 +02002770 call feedkeys("STe\<C-X>\<C-N>x\<CR>\<Esc>0", 'tx!')
2771 call assert_equal('Tex', getline('.'))
2772
glepnira218cc62024-06-03 19:32:39 +02002773 " clean up
2774 set omnifunc=
2775 bw!
glepnir8159fb12024-07-17 20:32:54 +02002776 bw!
glepnira218cc62024-06-03 19:32:39 +02002777 set complete& completeopt&
2778 autocmd! AAAAA_Group
2779 augroup! AAAAA_Group
2780 delfunc OnPumChange
2781 delfunc Omni_test
glepnirf94c9c42024-06-14 21:11:56 +02002782 delfunc Comp
zeertzjq551d8c32024-06-05 19:53:32 +02002783 unlet g:item
2784 unlet g:word
glepnira218cc62024-06-03 19:32:39 +02002785endfunc
2786
glepnirb9de1a02024-08-02 19:14:38 +02002787func Test_complete_fuzzy_with_completeslash()
2788 CheckMSWindows
2789
2790 call writefile([''], 'fobar', 'D')
2791 let orig_shellslash = &shellslash
2792 set cpt&
2793 new
2794 set completeopt+=fuzzy
2795 set noshellslash
2796
2797 " Test with completeslash unset
2798 set completeslash=
2799 call setline(1, ['.\fob'])
2800 call feedkeys("A\<C-X>\<C-F>\<Esc>0", 'tx!')
2801 call assert_equal('.\fobar', getline('.'))
2802
2803 " Test with completeslash=backslash
2804 set completeslash=backslash
2805 call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!')
2806 call assert_equal('.\fobar', getline('.'))
2807
2808 " Test with completeslash=slash
2809 set completeslash=slash
2810 call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!')
2811 call assert_equal('./fobar', getline('.'))
2812
2813 " Reset and clean up
2814 let &shellslash = orig_shellslash
2815 set completeslash=
2816 %bw!
2817endfunc
2818
zeertzjq8e567472024-06-14 20:04:42 +02002819" Check that tie breaking is stable for completeopt+=fuzzy (which should
2820" behave the same on different platforms).
2821func Test_complete_fuzzy_match_tie()
2822 new
2823 set completeopt+=fuzzy,noselect
2824 call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', ''])
2825
2826 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx')
2827 call assert_equal('aaabbccc', getline('.'))
2828 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx')
2829 call assert_equal('aaabbCCC', getline('.'))
2830 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
2831 call assert_equal('aaabbcccc', getline('.'))
2832 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
2833 call assert_equal('aaabbCCCC', getline('.'))
2834
2835 bwipe!
2836 set completeopt&
2837endfunc
2838
Christian Brabandt13032a42024-07-28 21:16:48 +02002839func Test_complete_backwards_default()
2840 new
2841 call append(1, ['foobar', 'foobaz'])
2842 new
2843 call feedkeys("i\<c-p>", 'tx')
2844 call assert_equal('foobaz', getline('.'))
2845 bw!
2846 bw!
2847endfunc
2848
Christian Brabandtfef66302024-01-29 21:46:58 +01002849" vim: shiftwidth=2 sts=2 expandtab nofoldenable