blob: 582824f1c734c5915df771c1dc5d3b9494e146fd [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',
239 \ 'user_data': 'test'
240 \ }
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' ] )
255 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
256
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
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100288 call assert_equal('test', v:completed_item[ 'user_data' ])
289 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',
308 \ 'user_data': ['one', 'two'],
309 \ }
310 \ ]
311 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100312endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100313
Bram Moolenaar1e115362019-01-09 23:01:02 +0100314func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100315 call assert_equal( 'aword', v:completed_item[ 'word' ] )
316 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
317 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
318 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
319 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100320 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100321
322 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100323endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100324
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100325func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100326 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
327
328 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
329 execute "normal a\<C-X>\<C-U>\<C-Y>"
330 set completefunc&
331
Bram Moolenaar08928322020-01-04 14:32:48 +0100332 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100333 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100334
335 let s:called_completedone = 0
336 au! CompleteDone
337endfunc
338
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100339func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100340 if a:findstart
341 return 0
342 endif
343
344 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100345endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100346
Bram Moolenaar1e115362019-01-09 23:01:02 +0100347func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100348 call assert_equal( 'aword', v:completed_item[ 'word' ] )
349 call assert_equal( '', v:completed_item[ 'abbr' ] )
350 call assert_equal( '', v:completed_item[ 'menu' ] )
351 call assert_equal( '', v:completed_item[ 'info' ] )
352 call assert_equal( '', v:completed_item[ 'kind' ] )
353 call assert_equal( '', v:completed_item[ 'user_data' ] )
354
355 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100356endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100357
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100358func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100359 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
360
361 set completefunc=<SID>CompleteDone_CompleteFuncList
362 execute "normal a\<C-X>\<C-U>\<C-Y>"
363 set completefunc&
364
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100365 call assert_equal('', v:completed_item[ 'user_data' ])
366 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100367
368 let s:called_completedone = 0
369 au! CompleteDone
370endfunc
371
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200372func Test_CompleteDone_undo()
373 au CompleteDone * call append(0, "prepend1")
374 new
375 call setline(1, ["line1", "line2"])
376 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
377 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
378 \ getline(1, '$'))
379 undo
380 call assert_equal(["line1", "line2"], getline(1, '$'))
381 bwipe!
382 au! CompleteDone
383endfunc
384
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100385func Test_CompleteDone_modify()
386 let value = {
387 \ 'word': '',
388 \ 'abbr': '',
389 \ 'menu': '',
390 \ 'info': '',
391 \ 'kind': '',
392 \ 'user_data': '',
393 \ }
394 let v:completed_item = value
zeertzjq75020942022-07-31 11:37:20 +0100395 call assert_equal(value, v:completed_item)
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100396endfunc
397
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200398func CompleteTest(findstart, query)
399 if a:findstart
400 return col('.')
401 endif
402 return ['matched']
403endfunc
404
405func Test_completefunc_info()
406 new
407 set completeopt=menuone
408 set completefunc=CompleteTest
409 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100410 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 +0200411 bwipe!
412 set completeopt&
413 set completefunc&
414endfunc
415
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100416" Check that when using feedkeys() typeahead does not interrupt searching for
417" completions.
418func Test_compl_feedkeys()
419 new
420 set completeopt=menuone,noselect
421 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
422 call assert_equal("jump jump", getline(1))
423 bwipe!
424 set completeopt&
425endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200426
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200427" Test for insert path completion with completeslash option
428func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200429 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200430
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100431 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200432 let orig_shellslash = &shellslash
433 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200434 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200435
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200436 set noshellslash
437
438 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100439 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100440 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200441
442 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100443 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100444 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200445
446 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100447 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100448 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200449
450 set shellslash
451
452 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100453 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100454 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200455
456 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100457 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100458 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200459
460 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100461 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100462 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200463 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200464
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200465 set noshellslash
466 set completeslash=slash
467 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
468
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200469 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200470 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200471endfunc
472
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200473func Test_pum_stopped_by_timer()
474 CheckScreendump
475
476 let lines =<< trim END
477 call setline(1, ['hello', 'hullo', 'heeee', ''])
478 func StartCompl()
479 call timer_start(100, { -> execute('stopinsert') })
480 call feedkeys("Gah\<C-N>")
481 endfunc
482 END
483
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100484 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200485 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
486 call term_sendkeys(buf, ":call StartCompl()\<CR>")
487 call TermWait(buf, 200)
488 call term_sendkeys(buf, "k")
489 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
490
491 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200492endfunc
493
zeertzjqcd5dbad2022-05-04 17:51:50 +0100494func Test_complete_stopinsert_startinsert()
495 nnoremap <F2> <Cmd>startinsert<CR>
496 inoremap <F2> <Cmd>stopinsert<CR>
497 " This just checks if this causes an error
498 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
499 nunmap <F2>
500 iunmap <F2>
501endfunc
502
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100503func Test_pum_with_folds_two_tabs()
504 CheckScreendump
505
506 let lines =<< trim END
507 set fdm=marker
508 call setline(1, ['" x {{{1', '" a some text'])
509 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
510 norm! zm
511 tab sp
512 call feedkeys('2Gzv', 'xt')
513 call feedkeys("0fa", 'xt')
514 END
515
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100516 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100517 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200518 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100519 call term_sendkeys(buf, "a\<C-N>")
520 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
521
522 call term_sendkeys(buf, "\<Esc>")
523 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100524endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100525
526func Test_pum_with_preview_win()
527 CheckScreendump
528
529 let lines =<< trim END
530 funct Omni_test(findstart, base)
531 if a:findstart
532 return col(".") - 1
533 endif
534 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
535 endfunc
536 set omnifunc=Omni_test
537 set completeopt+=longest
538 END
539
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100540 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100541 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100542 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100543 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100544 call term_sendkeys(buf, "\<C-N>")
545 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
546
547 call term_sendkeys(buf, "\<Esc>")
548 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100549endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100550
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000551func Test_scrollbar_on_wide_char()
552 CheckScreendump
553
554 let lines =<< trim END
555 call setline(1, ['a', ' 啊啊啊',
556 \ ' 哦哦哦',
557 \ ' 呃呃呃'])
558 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
559 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100560 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000561 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
562 call term_sendkeys(buf, "A\<C-N>")
563 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
564
565 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000566endfunc
567
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100568" Test for inserting the tag search pattern in insert mode
569func Test_ins_compl_tag_sft()
570 call writefile([
571 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
572 \ "first\tXfoo\t/^int first() {}$/",
573 \ "second\tXfoo\t/^int second() {}$/",
574 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100575 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100576 set tags=Xtags
577 let code =<< trim [CODE]
578 int first() {}
579 int second() {}
580 int third() {}
581 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100582 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100583
584 enew
585 set showfulltag
586 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
587 call assert_equal('int second() {}', getline(1))
588 set noshowfulltag
589
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100590 set tags&
591 %bwipe!
592endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200593
594" Test for 'completefunc' deleting text
595func Test_completefunc_error()
596 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200597 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200598 func CompleteFunc(findstart, base)
599 if a:findstart == 1
600 normal dd
601 return col('.') - 1
602 endif
603 return ['a', 'b']
604 endfunc
605 set completefunc=CompleteFunc
606 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100607 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200608
609 " delete text when called for the second time
610 func CompleteFunc2(findstart, base)
611 if a:findstart == 1
612 return col('.') - 1
613 endif
614 normal dd
615 return ['a', 'b']
616 endfunc
617 set completefunc=CompleteFunc2
618 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100619 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200620
Bram Moolenaar97202d92021-01-28 18:34:35 +0100621 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100622 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100623 if a:findstart == 1
624 return col('.') - 1
625 endif
626 wincmd p
627 return ['a', 'b']
628 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100629 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100630 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100631 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100632 close!
633
634 set completefunc&
635 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100636 delfunc CompleteFunc2
637 delfunc CompleteFunc3
638 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100639endfunc
640
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200641" Test for returning non-string values from 'completefunc'
642func Test_completefunc_invalid_data()
643 new
644 func! CompleteFunc(findstart, base)
645 if a:findstart == 1
646 return col('.') - 1
647 endif
648 return [{}, '', 'moon']
649 endfunc
650 set completefunc=CompleteFunc
651 exe "normal i\<C-X>\<C-U>"
652 call assert_equal('moon', getline(1))
653 set completefunc&
654 close!
655endfunc
656
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200657" Test for errors in using complete() function
658func Test_complete_func_error()
659 call assert_fails('call complete(1, ["a"])', 'E785:')
660 func ListColors()
661 call complete(col('.'), "blue")
662 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100663 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200664 func ListMonths()
665 call complete(col('.'), test_null_list())
666 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100667 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200668 delfunc ListColors
669 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100670 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200671 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200672endfunc
673
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000674" Test for recursively starting completion mode using complete()
675func Test_recursive_complete_func()
676 func ListColors()
677 call complete(5, ["red", "blue"])
678 return ''
679 endfunc
680 new
681 call setline(1, ['a1', 'a2'])
682 set complete=.
683 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
684 call assert_equal('a2blue', getline(3))
685 delfunc ListColors
686 bw!
687endfunc
688
bfredl87af60c2022-09-24 11:17:51 +0100689" Test for using complete() with completeopt+=longest
690func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100691 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100692 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100693
694 " default: insert first match
695 set completeopt&
696 call setline(1, ['i'])
697 exe "normal Aa\<f3>\<esc>"
698 call assert_equal('iaax', getline(1))
699
700 " with longest: insert longest prefix
701 set completeopt+=longest
702 call setline(1, ['i'])
703 exe "normal Aa\<f3>\<esc>"
704 call assert_equal('iaa', getline(1))
705 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100706 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100707endfunc
708
709
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200710" Test for completing words following a completed word in a line
711func Test_complete_wrapscan()
712 " complete words from another buffer
713 new
714 call setline(1, ['one two', 'three four'])
715 new
716 setlocal complete=w
717 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
718 call assert_equal('two three four', getline(1))
719 close!
720 " complete words from the current buffer
721 setlocal complete=.
722 %d
723 call setline(1, ['one two', ''])
724 call cursor(2, 1)
725 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
726 call assert_equal('one two one two', getline(2))
727 close!
728endfunc
729
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200730" Test for completing special characters
731func Test_complete_special_chars()
732 new
733 call setline(1, 'int .*[-\^$ func float')
734 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
735 call assert_equal('int .*[-\^$ func float', getline(2))
736 close!
737endfunc
738
739" Test for completion when text is wrapped across lines.
740func Test_complete_across_line()
741 new
742 call setline(1, ['red green blue', 'one two three'])
743 setlocal textwidth=20
744 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
745 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
746 close!
747endfunc
748
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000749" Test for completing words with a '.' at the end of a word.
750func Test_complete_joinspaces()
751 new
752 call setline(1, ['one two.', 'three. four'])
753 set joinspaces
754 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
755 call assert_equal("one two. three. four", getline(3))
756 set joinspaces&
757 bw!
758endfunc
759
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200760" Test for using CTRL-L to add one character when completing matching
761func Test_complete_add_onechar()
762 new
763 call setline(1, ['wool', 'woodwork'])
764 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
765 call assert_equal('woof', getline(3))
766
767 " use 'ignorecase' and backspace to erase characters from the prefix string
768 " and then add letters using CTRL-L
769 %d
770 set ignorecase backspace=2
771 setlocal complete=.
772 call setline(1, ['workhorse', 'workload'])
773 normal Go
774 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
775 call assert_equal('workh', getline(3))
776 set ignorecase& backspace&
777 close!
778endfunc
779
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000780" Test for using CTRL-X CTRL-L to complete whole lines lines
781func Test_complete_wholeline()
782 new
783 " complete one-line
784 call setline(1, ['a1', 'a2'])
785 exe "normal ggoa\<C-X>\<C-L>"
786 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
787 " go to the next match (wrapping around the buffer)
788 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
789 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
790 " go to the next match
791 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
792 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
793 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
794 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
795 " repeat the test using CTRL-L
796 " go to the next match (wrapping around the buffer)
797 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
798 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
799 " go to the next match
800 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
801 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
802 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
803 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
804 %d
805 " use CTRL-X CTRL-L to add one more line
806 call setline(1, ['a1', 'b1'])
807 setlocal complete=.
808 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
809 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
810 bw!
811endfunc
812
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200813" Test insert completion with 'cindent' (adjust the indent)
814func Test_complete_with_cindent()
815 new
816 setlocal cindent
817 call setline(1, ['if (i == 1)', " j = 2;"])
818 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
819 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
820
821 %d
822 call setline(1, ['when while', '{', ''])
823 setlocal cinkeys+==while
824 exe "normal Giwh\<C-P> "
825 call assert_equal("\twhile ", getline('$'))
826 close!
827endfunc
828
829" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
830func Test_complete_cmdline()
831 new
832 exe "normal icaddb\<C-X>\<C-V>"
833 call assert_equal('caddbuffer', getline(1))
834 exe "normal ocall getqf\<C-X>\<C-V>"
835 call assert_equal('call getqflist(', getline(2))
836 exe "normal oabcxyz(\<C-X>\<C-V>"
837 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200838 com! -buffer TestCommand1 echo 'TestCommand1'
839 com! -buffer TestCommand2 echo 'TestCommand2'
840 write TestCommand1Test
841 write TestCommand2Test
842 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
843 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
844 call assert_equal('TestCommand2Test', getline(4))
845 call delete('TestCommand1Test')
846 call delete('TestCommand2Test')
847 delcom TestCommand1
848 delcom TestCommand2
849 close!
850endfunc
851
852" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
853func Test_complete_stop()
854 new
855 func Save_mode1()
856 let g:mode1 = mode(1)
857 return ''
858 endfunc
859 func Save_mode2()
860 let g:mode2 = mode(1)
861 return ''
862 endfunc
863 inoremap <F1> <C-R>=Save_mode1()<CR>
864 inoremap <F2> <C-R>=Save_mode2()<CR>
865 call setline(1, ['aaa bbb ccc '])
866 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
867 call assert_equal('ic', g:mode1)
868 call assert_equal('i', g:mode2)
869 call assert_equal('aaa bbb ccc ', getline(1))
870 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
871 call assert_equal('ic', g:mode1)
872 call assert_equal('i', g:mode2)
873 call assert_equal('aaa bbb ccc aaa', getline(1))
874 set completeopt+=noselect
875 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
876 call assert_equal('ic', g:mode1)
877 call assert_equal('i', g:mode2)
878 call assert_equal('aaa bbb ccc aaa bb', getline(1))
879 set completeopt&
880 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
881 call assert_equal('ic', g:mode1)
882 call assert_equal('i', g:mode2)
883 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
884 com! -buffer TestCommand1 echo 'TestCommand1'
885 com! -buffer TestCommand2 echo 'TestCommand2'
886 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
887 call assert_equal('ic', g:mode1)
888 call assert_equal('i', g:mode2)
889 call assert_equal('TestCommand2', getline(2))
890 delcom TestCommand1
891 delcom TestCommand2
892 unlet g:mode1
893 unlet g:mode2
894 iunmap <F1>
895 iunmap <F2>
896 delfunc Save_mode1
897 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200898 close!
899endfunc
900
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000901" Test for typing CTRL-R in insert completion mode to insert a register
902" content.
903func Test_complete_reginsert()
904 new
905 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
906
907 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
908 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
909 " inserted.
910 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
911 call assert_equal('a123', getline(5))
912 let @r = "\<C-P>\<C-P>"
913 exe "normal GCa\<C-P>\<C-R>r"
914 call assert_equal('a12', getline(5))
915 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
916 call assert_equal('a1234x', getline(5))
917 bw!
918endfunc
919
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200920func Test_issue_7021()
921 CheckMSWindows
922
923 let orig_shellslash = &shellslash
924 set noshellslash
925
926 set completeslash=slash
927 call assert_false(expand('~') =~ '/')
928
929 let &shellslash = orig_shellslash
930 set completeslash=
931endfunc
932
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000933" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
934func Test_complete_longest_match()
935 for e in ['latin1', 'utf-8']
936 exe 'set encoding=' .. e
937 new
938 set complete=.
939 set completeopt=menu,longest
940 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
941 exe "normal Gopfx\<C-P>"
942 call assert_equal('pfx_', getline(5))
943 bw!
944 endfor
945
946 " Test for completing additional words with longest match set
947 new
948 call setline(1, ['abc1', 'abd2'])
949 exe "normal Goab\<C-P>\<C-X>\<C-P>"
950 call assert_equal('ab', getline(3))
951 bw!
952 set complete& completeopt&
953endfunc
954
955" Test for removing the first displayed completion match and selecting the
956" match just before that.
957func Test_complete_erase_firstmatch()
958 new
959 call setline(1, ['a12', 'a34', 'a56'])
960 set complete=.
961 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
962 call assert_equal('a34', getline('$'))
963 set complete&
964 bw!
965endfunc
966
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000967" Test for completing words from unloaded buffers
968func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100969 call writefile(['abc'], "Xfile1", 'D')
970 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000971 edit Xfile1
972 edit Xfile2
973 new | close
974 enew
975 bunload Xfile1 Xfile2
976 set complete=u
977 " complete from an unloaded buffer
978 exe "normal! ia\<C-P>"
979 call assert_equal('abc', getline(1))
980 exe "normal! od\<C-P>"
981 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100982
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000983 set complete&
984 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +0000985endfunc
986
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000987" Test for completing whole lines from unloaded buffers
988func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100989 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000990 edit Xfile1
991 enew
992 set complete=u
993 exe "normal! ia\<C-X>\<C-L>\<C-P>"
994 call assert_equal('a line2', getline(1))
995 %d
996 " completing from an unlisted buffer should fail
997 bdel Xfile1
998 exe "normal! ia\<C-X>\<C-L>\<C-P>"
999 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001000
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001001 set complete&
1002 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001003endfunc
1004
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001005" Test for completing words from unlisted buffers
1006func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001007 call writefile(['abc'], "Xfile1", 'D')
1008 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001009 edit Xfile1
1010 edit Xfile2
1011 new | close
1012 bdel Xfile1 Xfile2
1013 set complete=U
1014 " complete from an unlisted buffer
1015 exe "normal! ia\<C-P>"
1016 call assert_equal('abc', getline(1))
1017 exe "normal! od\<C-P>"
1018 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001019
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001020 set complete&
1021 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001022endfunc
1023
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001024" Test for completing whole lines from unlisted buffers
1025func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001026 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001027 edit Xfile1
1028 enew
1029 set complete=U
1030 " completing from a unloaded buffer should fail
1031 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1032 call assert_equal('a', getline(1))
1033 %d
1034 bdel Xfile1
1035 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1036 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001037
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001038 set complete&
1039 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001040endfunc
1041
1042" Test for adding a multibyte character using CTRL-L in completion mode
1043func Test_complete_mbyte_char_add()
1044 new
1045 set complete=.
1046 call setline(1, 'abė')
1047 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1048 call assert_equal('abė', getline(2))
1049 " Test for a leader with multibyte character
1050 %d
1051 call setline(1, 'abėĕ')
1052 exe "normal! oabė\<C-P>"
1053 call assert_equal('abėĕ', getline(2))
1054 bw!
1055endfunc
1056
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001057" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1058" not to complete matches from the local buffer. Also test using multiple
1059" <C-X> to cancel the current completion mode.
1060func Test_complete_local_expansion()
1061 new
1062 set complete=t
1063 call setline(1, ['abc', 'def'])
1064 exe "normal! Go\<C-X>\<C-P>"
1065 call assert_equal("def", getline(3))
1066 exe "normal! Go\<C-P>"
1067 call assert_equal("", getline(4))
1068 exe "normal! Go\<C-X>\<C-N>"
1069 call assert_equal("abc", getline(5))
1070 exe "normal! Go\<C-N>"
1071 call assert_equal("", getline(6))
1072
1073 " use multiple <C-X> to cancel the previous completion mode
1074 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1075 call assert_equal("", getline(7))
1076 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1077 call assert_equal("", getline(8))
1078 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1079 call assert_equal("abc", getline(9))
1080
1081 " interrupt the current completion mode
1082 set completeopt=menu,noinsert
1083 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1084 call assert_equal("abc", getline(10))
1085
1086 " when only one <C-X> is used to interrupt, do normal expansion
1087 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1088 call assert_equal("", getline(11))
1089 set completeopt&
1090
1091 " using two <C-X> in non-completion mode and restarting the same mode
1092 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1093 call assert_equal("def", getline(12))
1094
1095 " test for adding a match from the original empty text
1096 %d
1097 call setline(1, 'abc def g')
1098 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1099 call assert_equal('def', getline(2))
1100 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1101 call assert_equal('abc', getline(2))
1102
1103 bw!
1104endfunc
1105
1106" Test for undoing changes after a insert-mode completion
1107func Test_complete_undo()
1108 new
1109 set complete=.
1110 " undo with 'ignorecase'
1111 call setline(1, ['ABOVE', 'BELOW'])
1112 set ignorecase
1113 exe "normal! Goab\<C-G>u\<C-P>"
1114 call assert_equal("ABOVE", getline(3))
1115 undo
1116 call assert_equal("ab", getline(3))
1117 set ignorecase&
1118 %d
1119 " undo with longest match
1120 set completeopt=menu,longest
1121 call setline(1, ['above', 'about'])
1122 exe "normal! Goa\<C-G>u\<C-P>"
1123 call assert_equal("abo", getline(3))
1124 undo
1125 call assert_equal("a", getline(3))
1126 set completeopt&
1127 %d
1128 " undo for line completion
1129 call setline(1, ['above that change', 'below that change'])
1130 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1131 call assert_equal("above that change", getline(3))
1132 undo
1133 call assert_equal("above", getline(3))
1134
1135 bw!
1136endfunc
1137
1138" Test for completing a very long word
1139func Test_complete_long_word()
1140 set complete&
1141 new
1142 call setline(1, repeat('x', 950) .. ' one two three')
1143 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1144 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1145 %d
1146 " should fail when more than 950 characters are in a word
1147 call setline(1, repeat('x', 951) .. ' one two three')
1148 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1149 call assert_equal(repeat('x', 951), getline(2))
1150
1151 " Test for adding a very long word to an existing completion
1152 %d
1153 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1154 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1155 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1156 bw!
1157endfunc
1158
1159" Test for some fields in the complete items used by complete()
1160func Test_complete_items()
1161 func CompleteItems(idx)
1162 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1163 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1164 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1165 \ [#{user_data: 'u9'}],
1166 \ [#{word: "", user_data: 'u10'}],
1167 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1168 call complete(col('.'), items[a:idx])
1169 return ''
1170 endfunc
1171 new
1172 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1173 call assert_equal('u2', v:completed_item.user_data)
1174 call assert_equal('one', getline(1))
1175 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1176 call assert_equal('u3', v:completed_item.user_data)
1177 call assert_equal('one', getline(2))
1178 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1179 call assert_equal('', getline(3))
1180 set completeopt=menu,noinsert
1181 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1182 call assert_equal('oNE', getline(4))
1183 call assert_equal('u8', v:completed_item.user_data)
1184 set completeopt&
1185 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1186 call assert_equal('', getline(5))
1187 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1188 call assert_equal('', getline(6))
1189 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1190 call assert_equal('', getline(7))
1191 call assert_equal('u11', v:completed_item.user_data)
1192 " pass invalid argument to complete()
1193 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1194 call assert_fails('exe cmd', 'E730:')
1195 bw!
1196 delfunc CompleteItems
1197endfunc
1198
1199" Test for the "refresh" item in the dict returned by an insert completion
1200" function
1201func Test_complete_item_refresh_always()
1202 let g:CallCount = 0
1203 func! Tcomplete(findstart, base)
1204 if a:findstart
1205 " locate the start of the word
1206 let line = getline('.')
1207 let start = col('.') - 1
1208 while start > 0 && line[start - 1] =~ '\a'
1209 let start -= 1
1210 endwhile
1211 return start
1212 else
1213 let g:CallCount += 1
1214 let res = ["update1", "update12", "update123"]
1215 return #{words: res, refresh: 'always'}
1216 endif
1217 endfunc
1218 new
1219 set completeopt=menu,longest
1220 set completefunc=Tcomplete
1221 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1222 call assert_equal('up', getline(1))
1223 call assert_equal(2, g:CallCount)
1224 set completeopt&
1225 set completefunc&
1226 bw!
1227 delfunc Tcomplete
1228endfunc
1229
1230" Test for completing from a thesaurus file without read permission
1231func Test_complete_unreadable_thesaurus_file()
1232 CheckUnix
1233 CheckNotRoot
1234
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001235 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001236 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001237 new
1238 set complete=sXfile
1239 exe "normal! ia\<C-P>"
1240 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001241
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001242 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001243 set complete&
1244endfunc
1245
Bram Moolenaarcc233582020-12-12 13:32:07 +01001246" Test to ensure 'Scanning...' messages are not recorded in messages history
1247func Test_z1_complete_no_history()
1248 new
1249 messages clear
1250 let currmess = execute('messages')
1251 setlocal dictionary=README.txt
1252 exe "normal owh\<C-X>\<C-K>"
1253 exe "normal owh\<C-N>"
1254 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001255 bwipe!
1256endfunc
1257
1258" A mapping is not used for the key after CTRL-X.
1259func Test_no_mapping_for_ctrl_x_key()
1260 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001261 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001262 setlocal dictionary=README.txt
1263 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1264 call assert_equal('example ', getline(1))
1265 call assert_false(exists('was_mapped'))
1266 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001267endfunc
1268
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001269" Test for different ways of setting the 'completefunc' option
1270func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001271 func CompleteFunc1(callnr, findstart, base)
1272 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1273 return a:findstart ? 0 : []
1274 endfunc
1275 func CompleteFunc2(findstart, base)
1276 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001277 return a:findstart ? 0 : []
1278 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001279
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001280 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001281 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001282 LET &completefunc = 'g:CompleteFunc2'
1283 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001284 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001285 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001286 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001287 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001288 bw!
1289
1290 #" Test for using a function()
1291 set completefunc=function('g:CompleteFunc1',\ [10])
1292 new
1293 call setline(1, 'one')
1294 LET g:CompleteFunc1Args = []
1295 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1296 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001297 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001298
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001299 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001300 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001301 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001302 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001303 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001304 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001305 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001306 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001307 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001308
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001309 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001310 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001311 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001312 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001313 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001314 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001315 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001316 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001317 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001318
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001319 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001320 set completefunc=funcref('g:CompleteFunc1',\ [13])
1321 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001322 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001323 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001324 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001325 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001326 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001327
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001328 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001329 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001330 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001331 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001332 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001333 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001334 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001335 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001336 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001337
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001338 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001339 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001340 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001341 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001342 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001343 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001344 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001345 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001346 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001347
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001348 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001349 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001350 LET optval = substitute(optval, ' ', '\\ ', 'g')
1351 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001352 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001353 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001354 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001355 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001356 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001357 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001358
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001359 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001360 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001361 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001362 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001363 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001364 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001365 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001366 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001367
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001368 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001369 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001370 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001371 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001372 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001373 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001374 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001375 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001376
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001377 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001378 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001379 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001380 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001381 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001382 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001383 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001384 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001385 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001386
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001387 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001388 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001389 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001390 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001391 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001392 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001393 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001394 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001395 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001396
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 #" Test for using a lambda function with incorrect return value
1398 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1399 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001400 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001401 call setline(1, 'eight')
1402 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1403 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001404
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001405 #" Test for clearing the 'completefunc' option
1406 set completefunc=''
1407 set completefunc&
1408 call assert_fails("set completefunc=function('abc')", "E700:")
1409 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001410
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001411 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001412 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 call setline(1, 'five')
1414 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1415 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001416 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001417 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001418 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001419 bw!
1420 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001421 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001422
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001423 " Test for using a script-local function name
1424 func s:CompleteFunc3(findstart, base)
1425 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1426 return a:findstart ? 0 : []
1427 endfunc
1428 set completefunc=s:CompleteFunc3
1429 new
1430 call setline(1, 'script1')
1431 let g:CompleteFunc3Args = []
1432 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1433 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1434 bw!
1435
1436 let &completefunc = 's:CompleteFunc3'
1437 new
1438 call setline(1, 'script2')
1439 let g:CompleteFunc3Args = []
1440 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1441 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1442 bw!
1443 delfunc s:CompleteFunc3
1444
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001445 " In Vim9 script s: can be omitted
1446 let lines =<< trim END
1447 vim9script
1448 var CompleteFunc4Args = []
1449 def CompleteFunc4(findstart: bool, base: string): any
1450 add(CompleteFunc4Args, [findstart, base])
1451 return findstart ? 0 : []
1452 enddef
1453 set completefunc=CompleteFunc4
1454 new
1455 setline(1, 'script1')
1456 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1457 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1458 bw!
1459 END
1460 call v9.CheckScriptSuccess(lines)
1461
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001462 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001463 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001464 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1465
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001466 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001467 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001468 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001469 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001470 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001471 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001472
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001473 " set 'completefunc' to a partial with dict. This used to cause a crash.
1474 func SetCompleteFunc()
1475 let params = {'complete': function('g:DictCompleteFunc')}
1476 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001477 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001478 func g:DictCompleteFunc(_) dict
1479 endfunc
1480 call SetCompleteFunc()
1481 new
1482 call SetCompleteFunc()
1483 bw
1484 call test_garbagecollect_now()
1485 new
1486 set completefunc=
1487 wincmd w
1488 set completefunc=
1489 %bw!
1490 delfunc g:DictCompleteFunc
1491 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001492
1493 " Vim9 tests
1494 let lines =<< trim END
1495 vim9script
1496
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001497 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1498 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001499 return findstart ? 0 : []
1500 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001501
1502 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001503 set completefunc=function('Vim9CompleteFunc',\ [60])
1504 new | only
1505 setline(1, 'one')
1506 g:Vim9completeFuncArgs = []
1507 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1508 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1509 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001510
1511 # Test for using a global function name
1512 &completefunc = g:CompleteFunc2
1513 new | only
1514 setline(1, 'two')
1515 g:CompleteFunc2Args = []
1516 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1517 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1518 bw!
1519
1520 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001521 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001522 add(g:LocalCompleteFuncArgs, [findstart, base])
1523 return findstart ? 0 : []
1524 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001525 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001526 new | only
1527 setline(1, 'three')
1528 g:LocalCompleteFuncArgs = []
1529 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1530 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1531 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001532 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001533 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001534
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001535 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001536 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001537 delfunc CompleteFunc1
1538 delfunc CompleteFunc2
1539 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001540 %bw!
1541endfunc
1542
1543" Test for different ways of setting the 'omnifunc' option
1544func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001545 func OmniFunc1(callnr, findstart, base)
1546 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1547 return a:findstart ? 0 : []
1548 endfunc
1549 func OmniFunc2(findstart, base)
1550 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001551 return a:findstart ? 0 : []
1552 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001553
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001554 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001555 #" Test for using a function name
1556 LET &omnifunc = 'g:OmniFunc2'
1557 new
1558 call setline(1, 'zero')
1559 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001560 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001561 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1562 bw!
1563
1564 #" Test for using a function()
1565 set omnifunc=function('g:OmniFunc1',\ [10])
1566 new
1567 call setline(1, 'one')
1568 LET g:OmniFunc1Args = []
1569 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1570 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001571 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001572
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001573 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001574 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001575 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001576 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001577 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001578 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001579 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001580 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001581 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001582
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001583 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001584 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001585 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001586 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001587 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001588 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001589 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001590 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001591 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001592
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001594 set omnifunc=funcref('g:OmniFunc1',\ [13])
1595 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001596 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001597 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001598 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001599 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001600 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001601
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001602 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001603 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001604 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001605 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001606 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001607 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001608 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001609 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001610 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001611
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001612 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001613 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001614 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001615 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001616 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001617 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001619 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001620 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001621
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001623 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 LET optval = substitute(optval, ' ', '\\ ', 'g')
1625 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001626 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001627 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001628 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001629 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001631 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001632
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001633 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001634 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001635 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001637 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001639 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001641
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001643 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001644 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001646 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001650
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001652 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001654 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001656 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001658 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001659 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001660
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001661 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001662 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001663 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001664 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001665 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001666 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001670
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 #" Test for using a lambda function with incorrect return value
1672 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1673 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001674 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 call setline(1, 'eight')
1676 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1677 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001678
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001679 #" Test for clearing the 'omnifunc' option
1680 set omnifunc=''
1681 set omnifunc&
1682 call assert_fails("set omnifunc=function('abc')", "E700:")
1683 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001684
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001686 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 call setline(1, 'nine')
1688 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1689 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001690 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001691 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001692 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 bw!
1694 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001695 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001696
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001697 " Test for using a script-local function name
1698 func s:OmniFunc3(findstart, base)
1699 call add(g:OmniFunc3Args, [a:findstart, a:base])
1700 return a:findstart ? 0 : []
1701 endfunc
1702 set omnifunc=s:OmniFunc3
1703 new
1704 call setline(1, 'script1')
1705 let g:OmniFunc3Args = []
1706 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1707 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1708 bw!
1709
1710 let &omnifunc = 's:OmniFunc3'
1711 new
1712 call setline(1, 'script2')
1713 let g:OmniFunc3Args = []
1714 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1715 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1716 bw!
1717 delfunc s:OmniFunc3
1718
1719 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001720 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001721 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1722
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001723 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001724 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001725 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001726 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001727 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001728 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001729
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001730 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1731 func SetOmniFunc()
1732 let params = {'omni': function('g:DictOmniFunc')}
1733 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001734 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001735 func g:DictOmniFunc(_) dict
1736 endfunc
1737 call SetOmniFunc()
1738 new
1739 call SetOmniFunc()
1740 bw
1741 call test_garbagecollect_now()
1742 new
1743 set omnifunc=
1744 wincmd w
1745 set omnifunc=
1746 %bw!
1747 delfunc g:DictOmniFunc
1748 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001749
1750 " Vim9 tests
1751 let lines =<< trim END
1752 vim9script
1753
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001754 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1755 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001756 return findstart ? 0 : []
1757 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001758
1759 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001760 set omnifunc=function('Vim9omniFunc',\ [60])
1761 new | only
1762 setline(1, 'one')
1763 g:Vim9omniFunc_Args = []
1764 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1765 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1766 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001767
1768 # Test for using a global function name
1769 &omnifunc = g:OmniFunc2
1770 new | only
1771 setline(1, 'two')
1772 g:OmniFunc2Args = []
1773 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1774 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1775 bw!
1776
1777 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001778 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001779 add(g:LocalOmniFuncArgs, [findstart, base])
1780 return findstart ? 0 : []
1781 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001782 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001783 new | only
1784 setline(1, 'three')
1785 g:LocalOmniFuncArgs = []
1786 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1787 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1788 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001789 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001790 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001791
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001792 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001793 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001794 delfunc OmniFunc1
1795 delfunc OmniFunc2
1796 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001797 %bw!
1798endfunc
1799
1800" Test for different ways of setting the 'thesaurusfunc' option
1801func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001802 func TsrFunc1(callnr, findstart, base)
1803 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001804 return a:findstart ? 0 : []
1805 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001806 func TsrFunc2(findstart, base)
1807 call add(g:TsrFunc2Args, [a:findstart, a:base])
1808 return a:findstart ? 0 : ['sunday']
1809 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001810
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001811 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001812 #" Test for using a function name
1813 LET &thesaurusfunc = 'g:TsrFunc2'
1814 new
1815 call setline(1, 'zero')
1816 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001817 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001818 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1819 bw!
1820
1821 #" Test for using a function()
1822 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1823 new
1824 call setline(1, 'one')
1825 LET g:TsrFunc1Args = []
1826 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1827 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001828 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001829
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001830 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001831 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001832 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001833 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001834 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001835 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001836 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001837 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001838 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001839
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001840 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001841 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001842 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001843 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001844 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001845 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001846 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001847 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001848 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001849
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001850 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001851 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1852 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001853 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001854 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001855 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001856 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001857 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001858
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001859 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001860 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001861 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001862 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001863 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001864 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001865 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001866 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001868
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001869 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001870 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001871 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001872 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001873 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001874 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001875 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001876 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001877 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001878
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001879 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001880 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001881 LET optval = substitute(optval, ' ', '\\ ', 'g')
1882 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001883 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001884 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001885 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001886 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001887 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001888 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001889
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001890 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001891 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001892 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001893 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001894 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001895 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001896 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001897 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001898
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001899 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001900 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001901 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001903 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001905 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001907
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001909 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001911 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001913 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001915 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001916 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001917
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001918 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001919 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001920 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001921 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001922 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001923 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001924 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001925 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001926 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001927
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 #" Test for using a lambda function with incorrect return value
1929 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1930 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001931 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001932 call setline(1, 'eight')
1933 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1934 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001935
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001936 #" Test for clearing the 'thesaurusfunc' option
1937 set thesaurusfunc=''
1938 set thesaurusfunc&
1939 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
1940 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001941
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001942 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001943 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 call setline(1, 'ten')
1945 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
1946 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001947 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001948 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001949 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001950 bw!
1951
1952 #" Use a buffer-local value and a global value
1953 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001954 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001955 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001956 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1958 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001959 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001960 new
1961 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1964 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001965 call assert_equal([], g:TsrFunc1Args)
1966 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001967 wincmd w
1968 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001969 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001970 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
1971 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001972 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001973 :%bw!
1974 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001975 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001976
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001977 " Test for using a script-local function name
1978 func s:TsrFunc3(findstart, base)
1979 call add(g:TsrFunc3Args, [a:findstart, a:base])
1980 return a:findstart ? 0 : []
1981 endfunc
1982 set tsrfu=s:TsrFunc3
1983 new
1984 call setline(1, 'script1')
1985 let g:TsrFunc3Args = []
1986 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1987 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
1988 bw!
1989
1990 let &tsrfu = 's:TsrFunc3'
1991 new
1992 call setline(1, 'script2')
1993 let g:TsrFunc3Args = []
1994 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1995 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
1996 bw!
1997 delfunc s:TsrFunc3
1998
1999 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002000 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002001 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2002
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002003 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002004 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002005 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002006 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002007 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002008 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002009 bw!
2010
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002011 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2012 func SetTsrFunc()
2013 let params = {'thesaurus': function('g:DictTsrFunc')}
2014 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002015 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002016 func g:DictTsrFunc(_) dict
2017 endfunc
2018 call SetTsrFunc()
2019 new
2020 call SetTsrFunc()
2021 bw
2022 call test_garbagecollect_now()
2023 new
2024 set thesaurusfunc=
2025 wincmd w
2026 %bw!
2027 delfunc SetTsrFunc
2028
2029 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2030 " cause a crash.
2031 func SetLocalTsrFunc()
2032 let params = {'thesaurus': function('g:DictTsrFunc')}
2033 let &l:thesaurusfunc = params.thesaurus
2034 endfunc
2035 call SetLocalTsrFunc()
2036 call test_garbagecollect_now()
2037 call SetLocalTsrFunc()
2038 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002039 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002040 delfunc g:DictTsrFunc
2041 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002042
2043 " Vim9 tests
2044 let lines =<< trim END
2045 vim9script
2046
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002047 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2048 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002049 return findstart ? 0 : []
2050 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002051
2052 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002053 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2054 new | only
2055 setline(1, 'one')
2056 g:Vim9tsrFunc_Args = []
2057 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2058 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2059 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002060
2061 # Test for using a global function name
2062 &thesaurusfunc = g:TsrFunc2
2063 new | only
2064 setline(1, 'two')
2065 g:TsrFunc2Args = []
2066 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2067 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2068 bw!
2069
2070 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002071 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002072 add(g:LocalTsrFuncArgs, [findstart, base])
2073 return findstart ? 0 : []
2074 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002075 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002076 new | only
2077 setline(1, 'three')
2078 g:LocalTsrFuncArgs = []
2079 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2080 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2081 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002082 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002083 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002084
2085 " cleanup
2086 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002087 delfunc TsrFunc1
2088 delfunc TsrFunc2
2089 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002090 %bw!
2091endfunc
2092
Christian Brabandtac72c212022-04-07 21:00:53 +01002093func FooBarComplete(findstart, base)
2094 if a:findstart
2095 return col('.') - 1
2096 else
2097 return ["Foo", "Bar", "}"]
2098 endif
2099endfunc
2100
2101func Test_complete_smartindent()
2102 new
2103 setlocal smartindent completefunc=FooBarComplete
2104
2105 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2106 let result = getline(1,'$')
2107 call assert_equal(['', '{','}',''], result)
2108 bw!
2109 delfunction! FooBarComplete
2110endfunc
2111
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002112func Test_complete_overrun()
2113 " this was going past the end of the copied text
2114 new
2115 sil norm si”0s0 
2116 bwipe!
2117endfunc
2118
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002119func Test_infercase_very_long_line()
2120 " this was truncating the line when inferring case
2121 new
2122 let longLine = "blah "->repeat(300)
2123 let verylongLine = "blah "->repeat(400)
2124 call setline(1, verylongLine)
2125 call setline(2, longLine)
2126 set ic infercase
2127 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2128 call assert_equal(longLine, getline(3))
2129
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002130 " check that the too long text is NUL terminated
2131 %del
2132 norm o
2133 norm 1987ax
2134 exec "norm ox\<C-X>\<C-L>"
2135 call assert_equal(repeat('x', 1987), getline(3))
2136
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002137 bwipe!
2138 set noic noinfercase
2139endfunc
2140
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002141func Test_ins_complete_add()
2142 " this was reading past the end of allocated memory
2143 new
2144 norm o
2145 norm 7o€€
2146 sil! norm o
2147
2148 bwipe!
2149endfunc
2150
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002151func Test_ins_complete_end_of_line()
2152 " this was reading past the end of the line
2153 new
2154 norm 8o€ý 
2155 sil! norm o
2156
2157 bwipe!
2158endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002159
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002160func s:Tagfunc(t,f,o)
2161 bwipe!
2162 return []
2163endfunc
2164
2165" This was using freed memory, since 'complete' was in a wiped out buffer.
2166" Also using a window that was closed.
2167func Test_tagfunc_wipes_out_buffer()
2168 new
2169 set complete=.,t,w,b,u,i
2170 se tagfunc=s:Tagfunc
2171 sil norm i
2172
2173 bwipe!
2174endfunc
2175
2176
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002177" vim: shiftwidth=2 sts=2 expandtab