blob: 5ae89089d06b3fd0bb8eb0fa95958d76cd491e3d [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.
12 call mkdir('Xdir')
13 cd Xdir
14
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
18 \ "Makefile\tto\trun"], 'Xtestfile')
19 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
25 \ 'end of testfile'], 'Xtestdata')
26 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!
102 call delete('Xtestfile')
103 call delete('Xtest11.one')
104 call delete('Xtest11.two')
105 call delete('Xtestdata')
106 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100107 cd ..
108 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200109endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100110
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000111func Test_ins_complete_invalid_byte()
112 if has('unix') && executable('base64')
113 " this weird command was causing an illegal memory access
114 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
115 call system('base64 -d Xinvalid64 > Xinvalid')
116 call writefile(['qa!'], 'Xexit')
117 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
118 call delete('Xinvalid64')
119 call delete('Xinvalid')
120 call delete('Xexit')
121 endif
122endfunc
123
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100124func Test_omni_dash()
125 func Omni(findstart, base)
126 if a:findstart
127 return 5
128 else
129 echom a:base
130 return ['-help', '-v']
131 endif
132 endfunc
133 set omnifunc=Omni
134 new
135 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100136 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100137
138 bwipe!
139 delfunc Omni
140 set omnifunc=
141endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100142
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100143func Test_omni_throw()
144 let g:CallCount = 0
145 func Omni(findstart, base)
146 let g:CallCount += 1
147 if a:findstart
148 throw "he he he"
149 endif
150 endfunc
151 set omnifunc=Omni
152 new
153 try
154 exe "normal ifoo\<C-x>\<C-o>"
155 call assert_false(v:true, 'command should have failed')
156 catch
157 call assert_exception('he he he')
158 call assert_equal(1, g:CallCount)
159 endtry
160
161 bwipe!
162 delfunc Omni
163 unlet g:CallCount
164 set omnifunc=
165endfunc
166
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200167func Test_omni_autoload()
168 let save_rtp = &rtp
169 set rtp=Xruntime/some
170 let dir = 'Xruntime/some/autoload'
171 call mkdir(dir, 'p')
172
173 let lines =<< trim END
174 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000175 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200176 if findstart
177 return 1
178 else
179 return ['match']
180 endif
181 enddef
182 {
183 eval 1 + 2
184 }
185 END
186 call writefile(lines, dir .. '/omni.vim')
187
188 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000189 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200190 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
191
192 bwipe!
193 call delete('Xruntime', 'rf')
194 set omnifunc=
195 let &rtp = save_rtp
196endfunc
197
Bram Moolenaarffa96842018-06-12 22:05:14 +0200198func Test_completefunc_args()
199 let s:args = []
200 func! CompleteFunc(findstart, base)
201 let s:args += [[a:findstart, empty(a:base)]]
202 endfunc
203 new
204
205 set completefunc=CompleteFunc
206 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200207 call assert_equal([1, 1], s:args[0])
208 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200209 set completefunc=
210
211 let s:args = []
212 set omnifunc=CompleteFunc
213 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200214 call assert_equal([1, 1], s:args[0])
215 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200216 set omnifunc=
217
218 bwipe!
219 unlet s:args
220 delfunc CompleteFunc
221endfunc
222
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100223func s:CompleteDone_CompleteFuncNone( findstart, base )
224 if a:findstart
225 return 0
226 endif
227
228 return v:none
229endfunc
230
Bram Moolenaar1e115362019-01-09 23:01:02 +0100231func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100232 if a:findstart
233 return 0
234 endif
235
236 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100237 \ 'words': [
238 \ {
239 \ 'word': 'aword',
240 \ 'abbr': 'wrd',
241 \ 'menu': 'extra text',
242 \ 'info': 'words are cool',
243 \ 'kind': 'W',
244 \ 'user_data': 'test'
245 \ }
246 \ ]
247 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100248endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100249
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100250func s:CompleteDone_CheckCompletedItemNone()
251 let s:called_completedone = 1
252endfunc
253
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100254func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100255 call assert_equal( 'aword', v:completed_item[ 'word' ] )
256 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
257 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
258 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
259 call assert_equal( 'W', v:completed_item[ 'kind' ] )
260 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
261
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100262 if a:pre
263 call assert_equal('function', complete_info().mode)
264 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100265
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100266 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100267endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100268
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100269func Test_CompleteDoneNone()
270 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 set completefunc=<SID>CompleteDone_CompleteFuncNone
274 execute "normal a\<C-X>\<C-U>\<C-Y>"
275 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200276 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100277
278 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200279 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100280
281 let s:called_completedone = 0
282 au! CompleteDone
283endfunc
284
285func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100286 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
287 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100288
289 set completefunc=<SID>CompleteDone_CompleteFuncDict
290 execute "normal a\<C-X>\<C-U>\<C-Y>"
291 set completefunc&
292
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100293 call assert_equal('test', v:completed_item[ 'user_data' ])
294 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100295
296 let s:called_completedone = 0
297 au! CompleteDone
298endfunc
299
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100300func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100301 if a:findstart
302 return 0
303 endif
304
305 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100306 \ 'words': [
307 \ {
308 \ 'word': 'aword',
309 \ 'abbr': 'wrd',
310 \ 'menu': 'extra text',
311 \ 'info': 'words are cool',
312 \ 'kind': 'W',
313 \ 'user_data': ['one', 'two'],
314 \ }
315 \ ]
316 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100317endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100318
Bram Moolenaar1e115362019-01-09 23:01:02 +0100319func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100320 call assert_equal( 'aword', v:completed_item[ 'word' ] )
321 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
322 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
323 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
324 call assert_equal( 'W', v:completed_item[ 'kind' ] )
Bram Moolenaar08928322020-01-04 14:32:48 +0100325 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100326
327 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100328endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100329
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100330func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100331 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
332
333 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
334 execute "normal a\<C-X>\<C-U>\<C-Y>"
335 set completefunc&
336
Bram Moolenaar08928322020-01-04 14:32:48 +0100337 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100338 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100339
340 let s:called_completedone = 0
341 au! CompleteDone
342endfunc
343
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100344func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100345 if a:findstart
346 return 0
347 endif
348
349 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100350endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100351
Bram Moolenaar1e115362019-01-09 23:01:02 +0100352func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100353 call assert_equal( 'aword', v:completed_item[ 'word' ] )
354 call assert_equal( '', v:completed_item[ 'abbr' ] )
355 call assert_equal( '', v:completed_item[ 'menu' ] )
356 call assert_equal( '', v:completed_item[ 'info' ] )
357 call assert_equal( '', v:completed_item[ 'kind' ] )
358 call assert_equal( '', v:completed_item[ 'user_data' ] )
359
360 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100361endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100362
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100363func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100364 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
365
366 set completefunc=<SID>CompleteDone_CompleteFuncList
367 execute "normal a\<C-X>\<C-U>\<C-Y>"
368 set completefunc&
369
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100370 call assert_equal('', v:completed_item[ 'user_data' ])
371 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100372
373 let s:called_completedone = 0
374 au! CompleteDone
375endfunc
376
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200377func Test_CompleteDone_undo()
378 au CompleteDone * call append(0, "prepend1")
379 new
380 call setline(1, ["line1", "line2"])
381 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
382 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
383 \ getline(1, '$'))
384 undo
385 call assert_equal(["line1", "line2"], getline(1, '$'))
386 bwipe!
387 au! CompleteDone
388endfunc
389
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200390func CompleteTest(findstart, query)
391 if a:findstart
392 return col('.')
393 endif
394 return ['matched']
395endfunc
396
397func Test_completefunc_info()
398 new
399 set completeopt=menuone
400 set completefunc=CompleteTest
401 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100402 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 +0200403 bwipe!
404 set completeopt&
405 set completefunc&
406endfunc
407
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100408" Check that when using feedkeys() typeahead does not interrupt searching for
409" completions.
410func Test_compl_feedkeys()
411 new
412 set completeopt=menuone,noselect
413 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
414 call assert_equal("jump jump", getline(1))
415 bwipe!
416 set completeopt&
417endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200418
mityua1198122021-11-20 19:13:39 +0000419func s:ComplInCmdwin_GlobalCompletion(a, l, p)
420 return 'global'
421endfunc
422
423func s:ComplInCmdwin_LocalCompletion(a, l, p)
424 return 'local'
425endfunc
426
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200427func Test_compl_in_cmdwin()
Bram Moolenaar21829c52021-01-26 22:42:21 +0100428 CheckFeature cmdwin
429
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200430 set wildmenu wildchar=<Tab>
431 com! -nargs=1 -complete=command GetInput let input = <q-args>
432 com! -buffer TestCommand echo 'TestCommand'
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100433 let w:test_winvar = 'winvar'
434 let b:test_bufvar = 'bufvar'
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200435
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100436 " User-defined commands
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200437 let input = ''
438 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
439 call assert_equal('TestCommand', input)
440
441 let input = ''
442 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
443 call assert_equal('T', input)
444
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100445
446 com! -nargs=1 -complete=var GetInput let input = <q-args>
447 " Window-local variables
448 let input = ''
449 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
450 call assert_equal('w:test_winvar', input)
451
452 let input = ''
453 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
454 call assert_equal('w:test_', input)
455
456 " Buffer-local variables
457 let input = ''
458 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
459 call assert_equal('b:test_bufvar', input)
460
461 let input = ''
462 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
463 call assert_equal('b:test_', input)
464
mityua1198122021-11-20 19:13:39 +0000465
466 " Argument completion of buffer-local command
467 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
468 return ['global']
469 endfunc
470
471 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
472 return ['local']
473 endfunc
474
475 func s:ComplInCmdwin_CheckCompletion(arg)
476 call assert_equal('local', a:arg)
477 endfunc
478
479 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
480 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
481 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
482 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
483 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
484
485 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
486 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
487 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
488 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
489
490 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
491
492 func! s:ComplInCmdwin_CheckCompletion(arg)
493 call assert_equal('global', a:arg)
494 endfunc
495 new
496 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
497 quit
498
499 delfunc s:ComplInCmdwin_GlobalCompletion
500 delfunc s:ComplInCmdwin_LocalCompletion
501 delfunc s:ComplInCmdwin_GlobalCompletionList
502 delfunc s:ComplInCmdwin_LocalCompletionList
503 delfunc s:ComplInCmdwin_CheckCompletion
504
505 delcom -buffer TestCommand
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200506 delcom TestCommand
507 delcom GetInput
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +0100508 unlet w:test_winvar
509 unlet b:test_bufvar
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200510 set wildmenu& wildchar&
511endfunc
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200512
513" Test for insert path completion with completeslash option
514func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200515 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200516
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200517 call mkdir('Xdir')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200518 let orig_shellslash = &shellslash
519 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200520 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200521
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200522 set noshellslash
523
524 set completeslash=
525 exe "normal oXd\<C-X>\<C-F>"
526 call assert_equal('Xdir\', getline('.'))
527
528 set completeslash=backslash
529 exe "normal oXd\<C-X>\<C-F>"
530 call assert_equal('Xdir\', getline('.'))
531
532 set completeslash=slash
533 exe "normal oXd\<C-X>\<C-F>"
534 call assert_equal('Xdir/', getline('.'))
535
536 set shellslash
537
538 set completeslash=
539 exe "normal oXd\<C-X>\<C-F>"
540 call assert_equal('Xdir/', getline('.'))
541
542 set completeslash=backslash
543 exe "normal oXd\<C-X>\<C-F>"
544 call assert_equal('Xdir\', getline('.'))
545
546 set completeslash=slash
547 exe "normal oXd\<C-X>\<C-F>"
548 call assert_equal('Xdir/', getline('.'))
549 %bw!
550 call delete('Xdir', 'rf')
551
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200552 set noshellslash
553 set completeslash=slash
554 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
555
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200556 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200557 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200558endfunc
559
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200560func Test_pum_stopped_by_timer()
561 CheckScreendump
562
563 let lines =<< trim END
564 call setline(1, ['hello', 'hullo', 'heeee', ''])
565 func StartCompl()
566 call timer_start(100, { -> execute('stopinsert') })
567 call feedkeys("Gah\<C-N>")
568 endfunc
569 END
570
571 call writefile(lines, 'Xpumscript')
572 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
573 call term_sendkeys(buf, ":call StartCompl()\<CR>")
574 call TermWait(buf, 200)
575 call term_sendkeys(buf, "k")
576 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
577
578 call StopVimInTerminal(buf)
579 call delete('Xpumscript')
580endfunc
581
zeertzjqcd5dbad2022-05-04 17:51:50 +0100582func Test_complete_stopinsert_startinsert()
583 nnoremap <F2> <Cmd>startinsert<CR>
584 inoremap <F2> <Cmd>stopinsert<CR>
585 " This just checks if this causes an error
586 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
587 nunmap <F2>
588 iunmap <F2>
589endfunc
590
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100591func Test_pum_with_folds_two_tabs()
592 CheckScreendump
593
594 let lines =<< trim END
595 set fdm=marker
596 call setline(1, ['" x {{{1', '" a some text'])
597 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
598 norm! zm
599 tab sp
600 call feedkeys('2Gzv', 'xt')
601 call feedkeys("0fa", 'xt')
602 END
603
604 call writefile(lines, 'Xpumscript')
605 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200606 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100607 call term_sendkeys(buf, "a\<C-N>")
608 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
609
610 call term_sendkeys(buf, "\<Esc>")
611 call StopVimInTerminal(buf)
612 call delete('Xpumscript')
613endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100614
615func Test_pum_with_preview_win()
616 CheckScreendump
617
618 let lines =<< trim END
619 funct Omni_test(findstart, base)
620 if a:findstart
621 return col(".") - 1
622 endif
623 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
624 endfunc
625 set omnifunc=Omni_test
626 set completeopt+=longest
627 END
628
629 call writefile(lines, 'Xpreviewscript')
630 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200631 call TermWait(buf, 50)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100632 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200633 call TermWait(buf, 100)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100634 call term_sendkeys(buf, "\<C-N>")
635 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
636
637 call term_sendkeys(buf, "\<Esc>")
638 call StopVimInTerminal(buf)
639 call delete('Xpreviewscript')
640endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100641
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000642func Test_scrollbar_on_wide_char()
643 CheckScreendump
644
645 let lines =<< trim END
646 call setline(1, ['a', ' 啊啊啊',
647 \ ' 哦哦哦',
648 \ ' 呃呃呃'])
649 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
650 END
651 call writefile(lines, 'Xwidescript')
652 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
653 call term_sendkeys(buf, "A\<C-N>")
654 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
655
656 call StopVimInTerminal(buf)
657 call delete('Xwidescript')
658endfunc
659
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100660" Test for inserting the tag search pattern in insert mode
661func Test_ins_compl_tag_sft()
662 call writefile([
663 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
664 \ "first\tXfoo\t/^int first() {}$/",
665 \ "second\tXfoo\t/^int second() {}$/",
666 \ "third\tXfoo\t/^int third() {}$/"],
667 \ 'Xtags')
668 set tags=Xtags
669 let code =<< trim [CODE]
670 int first() {}
671 int second() {}
672 int third() {}
673 [CODE]
674 call writefile(code, 'Xfoo')
675
676 enew
677 set showfulltag
678 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
679 call assert_equal('int second() {}', getline(1))
680 set noshowfulltag
681
682 call delete('Xtags')
683 call delete('Xfoo')
684 set tags&
685 %bwipe!
686endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200687
688" Test for 'completefunc' deleting text
689func Test_completefunc_error()
690 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200691 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200692 func CompleteFunc(findstart, base)
693 if a:findstart == 1
694 normal dd
695 return col('.') - 1
696 endif
697 return ['a', 'b']
698 endfunc
699 set completefunc=CompleteFunc
700 call setline(1, ['', 'abcd', ''])
Bram Moolenaar3eb6bd92021-01-29 21:47:24 +0100701 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200702
703 " delete text when called for the second time
704 func CompleteFunc2(findstart, base)
705 if a:findstart == 1
706 return col('.') - 1
707 endif
708 normal dd
709 return ['a', 'b']
710 endfunc
711 set completefunc=CompleteFunc2
712 call setline(1, ['', 'abcd', ''])
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200713 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200714
Bram Moolenaar97202d92021-01-28 18:34:35 +0100715 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100716 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100717 if a:findstart == 1
718 return col('.') - 1
719 endif
720 wincmd p
721 return ['a', 'b']
722 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100723 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100724 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100725 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100726 close!
727
728 set completefunc&
729 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100730 delfunc CompleteFunc2
731 delfunc CompleteFunc3
732 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100733endfunc
734
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200735" Test for returning non-string values from 'completefunc'
736func Test_completefunc_invalid_data()
737 new
738 func! CompleteFunc(findstart, base)
739 if a:findstart == 1
740 return col('.') - 1
741 endif
742 return [{}, '', 'moon']
743 endfunc
744 set completefunc=CompleteFunc
745 exe "normal i\<C-X>\<C-U>"
746 call assert_equal('moon', getline(1))
747 set completefunc&
748 close!
749endfunc
750
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200751" Test for errors in using complete() function
752func Test_complete_func_error()
753 call assert_fails('call complete(1, ["a"])', 'E785:')
754 func ListColors()
755 call complete(col('.'), "blue")
756 endfunc
757 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
758 func ListMonths()
759 call complete(col('.'), test_null_list())
760 endfunc
761 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
762 delfunc ListColors
763 delfunc ListMonths
764 call assert_fails('call complete_info({})', 'E714:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200765 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200766endfunc
767
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000768" Test for recursively starting completion mode using complete()
769func Test_recursive_complete_func()
770 func ListColors()
771 call complete(5, ["red", "blue"])
772 return ''
773 endfunc
774 new
775 call setline(1, ['a1', 'a2'])
776 set complete=.
777 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
778 call assert_equal('a2blue', getline(3))
779 delfunc ListColors
780 bw!
781endfunc
782
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200783" Test for completing words following a completed word in a line
784func Test_complete_wrapscan()
785 " complete words from another buffer
786 new
787 call setline(1, ['one two', 'three four'])
788 new
789 setlocal complete=w
790 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
791 call assert_equal('two three four', getline(1))
792 close!
793 " complete words from the current buffer
794 setlocal complete=.
795 %d
796 call setline(1, ['one two', ''])
797 call cursor(2, 1)
798 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
799 call assert_equal('one two one two', getline(2))
800 close!
801endfunc
802
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200803" Test for completing special characters
804func Test_complete_special_chars()
805 new
806 call setline(1, 'int .*[-\^$ func float')
807 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
808 call assert_equal('int .*[-\^$ func float', getline(2))
809 close!
810endfunc
811
812" Test for completion when text is wrapped across lines.
813func Test_complete_across_line()
814 new
815 call setline(1, ['red green blue', 'one two three'])
816 setlocal textwidth=20
817 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
818 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
819 close!
820endfunc
821
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000822" Test for completing words with a '.' at the end of a word.
823func Test_complete_joinspaces()
824 new
825 call setline(1, ['one two.', 'three. four'])
826 set joinspaces
827 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
828 call assert_equal("one two. three. four", getline(3))
829 set joinspaces&
830 bw!
831endfunc
832
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200833" Test for using CTRL-L to add one character when completing matching
834func Test_complete_add_onechar()
835 new
836 call setline(1, ['wool', 'woodwork'])
837 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
838 call assert_equal('woof', getline(3))
839
840 " use 'ignorecase' and backspace to erase characters from the prefix string
841 " and then add letters using CTRL-L
842 %d
843 set ignorecase backspace=2
844 setlocal complete=.
845 call setline(1, ['workhorse', 'workload'])
846 normal Go
847 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>"
848 call assert_equal('workh', getline(3))
849 set ignorecase& backspace&
850 close!
851endfunc
852
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +0000853" Test for using CTRL-X CTRL-L to complete whole lines lines
854func Test_complete_wholeline()
855 new
856 " complete one-line
857 call setline(1, ['a1', 'a2'])
858 exe "normal ggoa\<C-X>\<C-L>"
859 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
860 " go to the next match (wrapping around the buffer)
861 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
862 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
863 " go to the next match
864 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
865 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
866 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
867 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
868 " repeat the test using CTRL-L
869 " go to the next match (wrapping around the buffer)
870 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
871 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
872 " go to the next match
873 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
874 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
875 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
876 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
877 %d
878 " use CTRL-X CTRL-L to add one more line
879 call setline(1, ['a1', 'b1'])
880 setlocal complete=.
881 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
882 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
883 bw!
884endfunc
885
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200886" Test insert completion with 'cindent' (adjust the indent)
887func Test_complete_with_cindent()
888 new
889 setlocal cindent
890 call setline(1, ['if (i == 1)', " j = 2;"])
891 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
892 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
893
894 %d
895 call setline(1, ['when while', '{', ''])
896 setlocal cinkeys+==while
897 exe "normal Giwh\<C-P> "
898 call assert_equal("\twhile ", getline('$'))
899 close!
900endfunc
901
902" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
903func Test_complete_cmdline()
904 new
905 exe "normal icaddb\<C-X>\<C-V>"
906 call assert_equal('caddbuffer', getline(1))
907 exe "normal ocall getqf\<C-X>\<C-V>"
908 call assert_equal('call getqflist(', getline(2))
909 exe "normal oabcxyz(\<C-X>\<C-V>"
910 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +0200911 com! -buffer TestCommand1 echo 'TestCommand1'
912 com! -buffer TestCommand2 echo 'TestCommand2'
913 write TestCommand1Test
914 write TestCommand2Test
915 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
916 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
917 call assert_equal('TestCommand2Test', getline(4))
918 call delete('TestCommand1Test')
919 call delete('TestCommand2Test')
920 delcom TestCommand1
921 delcom TestCommand2
922 close!
923endfunc
924
925" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
926func Test_complete_stop()
927 new
928 func Save_mode1()
929 let g:mode1 = mode(1)
930 return ''
931 endfunc
932 func Save_mode2()
933 let g:mode2 = mode(1)
934 return ''
935 endfunc
936 inoremap <F1> <C-R>=Save_mode1()<CR>
937 inoremap <F2> <C-R>=Save_mode2()<CR>
938 call setline(1, ['aaa bbb ccc '])
939 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
940 call assert_equal('ic', g:mode1)
941 call assert_equal('i', g:mode2)
942 call assert_equal('aaa bbb ccc ', getline(1))
943 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
944 call assert_equal('ic', g:mode1)
945 call assert_equal('i', g:mode2)
946 call assert_equal('aaa bbb ccc aaa', getline(1))
947 set completeopt+=noselect
948 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
949 call assert_equal('ic', g:mode1)
950 call assert_equal('i', g:mode2)
951 call assert_equal('aaa bbb ccc aaa bb', getline(1))
952 set completeopt&
953 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
954 call assert_equal('ic', g:mode1)
955 call assert_equal('i', g:mode2)
956 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
957 com! -buffer TestCommand1 echo 'TestCommand1'
958 com! -buffer TestCommand2 echo 'TestCommand2'
959 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
960 call assert_equal('ic', g:mode1)
961 call assert_equal('i', g:mode2)
962 call assert_equal('TestCommand2', getline(2))
963 delcom TestCommand1
964 delcom TestCommand2
965 unlet g:mode1
966 unlet g:mode2
967 iunmap <F1>
968 iunmap <F2>
969 delfunc Save_mode1
970 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200971 close!
972endfunc
973
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +0000974" Test for typing CTRL-R in insert completion mode to insert a register
975" content.
976func Test_complete_reginsert()
977 new
978 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
979
980 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
981 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
982 " inserted.
983 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
984 call assert_equal('a123', getline(5))
985 let @r = "\<C-P>\<C-P>"
986 exe "normal GCa\<C-P>\<C-R>r"
987 call assert_equal('a12', getline(5))
988 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
989 call assert_equal('a1234x', getline(5))
990 bw!
991endfunc
992
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200993func Test_issue_7021()
994 CheckMSWindows
995
996 let orig_shellslash = &shellslash
997 set noshellslash
998
999 set completeslash=slash
1000 call assert_false(expand('~') =~ '/')
1001
1002 let &shellslash = orig_shellslash
1003 set completeslash=
1004endfunc
1005
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001006" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
1007func Test_complete_longest_match()
1008 for e in ['latin1', 'utf-8']
1009 exe 'set encoding=' .. e
1010 new
1011 set complete=.
1012 set completeopt=menu,longest
1013 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
1014 exe "normal Gopfx\<C-P>"
1015 call assert_equal('pfx_', getline(5))
1016 bw!
1017 endfor
1018
1019 " Test for completing additional words with longest match set
1020 new
1021 call setline(1, ['abc1', 'abd2'])
1022 exe "normal Goab\<C-P>\<C-X>\<C-P>"
1023 call assert_equal('ab', getline(3))
1024 bw!
1025 set complete& completeopt&
1026endfunc
1027
1028" Test for removing the first displayed completion match and selecting the
1029" match just before that.
1030func Test_complete_erase_firstmatch()
1031 new
1032 call setline(1, ['a12', 'a34', 'a56'])
1033 set complete=.
1034 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1035 call assert_equal('a34', getline('$'))
1036 set complete&
1037 bw!
1038endfunc
1039
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001040" Test for completing words from unloaded buffers
1041func Test_complete_from_unloadedbuf()
1042 call writefile(['abc'], "Xfile1")
1043 call writefile(['def'], "Xfile2")
1044 edit Xfile1
1045 edit Xfile2
1046 new | close
1047 enew
1048 bunload Xfile1 Xfile2
1049 set complete=u
1050 " complete from an unloaded buffer
1051 exe "normal! ia\<C-P>"
1052 call assert_equal('abc', getline(1))
1053 exe "normal! od\<C-P>"
1054 call assert_equal('def', getline(2))
1055 set complete&
1056 %bw!
1057 call delete("Xfile1")
1058 call delete("Xfile2")
1059endfunc
1060
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001061" Test for completing whole lines from unloaded buffers
1062func Test_complete_wholeline_unloadedbuf()
1063 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1064 edit Xfile1
1065 enew
1066 set complete=u
1067 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1068 call assert_equal('a line2', getline(1))
1069 %d
1070 " completing from an unlisted buffer should fail
1071 bdel Xfile1
1072 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1073 call assert_equal('a', getline(1))
1074 set complete&
1075 %bw!
1076 call delete("Xfile1")
1077endfunc
1078
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001079" Test for completing words from unlisted buffers
1080func Test_complete_from_unlistedbuf()
1081 call writefile(['abc'], "Xfile1")
1082 call writefile(['def'], "Xfile2")
1083 edit Xfile1
1084 edit Xfile2
1085 new | close
1086 bdel Xfile1 Xfile2
1087 set complete=U
1088 " complete from an unlisted buffer
1089 exe "normal! ia\<C-P>"
1090 call assert_equal('abc', getline(1))
1091 exe "normal! od\<C-P>"
1092 call assert_equal('def', getline(2))
1093 set complete&
1094 %bw!
1095 call delete("Xfile1")
1096 call delete("Xfile2")
1097endfunc
1098
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001099" Test for completing whole lines from unlisted buffers
1100func Test_complete_wholeline_unlistedbuf()
1101 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
1102 edit Xfile1
1103 enew
1104 set complete=U
1105 " completing from a unloaded buffer should fail
1106 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1107 call assert_equal('a', getline(1))
1108 %d
1109 bdel Xfile1
1110 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1111 call assert_equal('a line2', getline(1))
1112 set complete&
1113 %bw!
1114 call delete("Xfile1")
1115endfunc
1116
1117" Test for adding a multibyte character using CTRL-L in completion mode
1118func Test_complete_mbyte_char_add()
1119 new
1120 set complete=.
1121 call setline(1, 'abÄ—')
1122 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1123 call assert_equal('abÄ—', getline(2))
1124 " Test for a leader with multibyte character
1125 %d
1126 call setline(1, 'abÄ—Ä•')
1127 exe "normal! oabÄ—\<C-P>"
1128 call assert_equal('abÄ—Ä•', getline(2))
1129 bw!
1130endfunc
1131
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001132" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1133" not to complete matches from the local buffer. Also test using multiple
1134" <C-X> to cancel the current completion mode.
1135func Test_complete_local_expansion()
1136 new
1137 set complete=t
1138 call setline(1, ['abc', 'def'])
1139 exe "normal! Go\<C-X>\<C-P>"
1140 call assert_equal("def", getline(3))
1141 exe "normal! Go\<C-P>"
1142 call assert_equal("", getline(4))
1143 exe "normal! Go\<C-X>\<C-N>"
1144 call assert_equal("abc", getline(5))
1145 exe "normal! Go\<C-N>"
1146 call assert_equal("", getline(6))
1147
1148 " use multiple <C-X> to cancel the previous completion mode
1149 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1150 call assert_equal("", getline(7))
1151 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1152 call assert_equal("", getline(8))
1153 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1154 call assert_equal("abc", getline(9))
1155
1156 " interrupt the current completion mode
1157 set completeopt=menu,noinsert
1158 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1159 call assert_equal("abc", getline(10))
1160
1161 " when only one <C-X> is used to interrupt, do normal expansion
1162 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1163 call assert_equal("", getline(11))
1164 set completeopt&
1165
1166 " using two <C-X> in non-completion mode and restarting the same mode
1167 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1168 call assert_equal("def", getline(12))
1169
1170 " test for adding a match from the original empty text
1171 %d
1172 call setline(1, 'abc def g')
1173 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1174 call assert_equal('def', getline(2))
1175 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1176 call assert_equal('abc', getline(2))
1177
1178 bw!
1179endfunc
1180
1181" Test for undoing changes after a insert-mode completion
1182func Test_complete_undo()
1183 new
1184 set complete=.
1185 " undo with 'ignorecase'
1186 call setline(1, ['ABOVE', 'BELOW'])
1187 set ignorecase
1188 exe "normal! Goab\<C-G>u\<C-P>"
1189 call assert_equal("ABOVE", getline(3))
1190 undo
1191 call assert_equal("ab", getline(3))
1192 set ignorecase&
1193 %d
1194 " undo with longest match
1195 set completeopt=menu,longest
1196 call setline(1, ['above', 'about'])
1197 exe "normal! Goa\<C-G>u\<C-P>"
1198 call assert_equal("abo", getline(3))
1199 undo
1200 call assert_equal("a", getline(3))
1201 set completeopt&
1202 %d
1203 " undo for line completion
1204 call setline(1, ['above that change', 'below that change'])
1205 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1206 call assert_equal("above that change", getline(3))
1207 undo
1208 call assert_equal("above", getline(3))
1209
1210 bw!
1211endfunc
1212
1213" Test for completing a very long word
1214func Test_complete_long_word()
1215 set complete&
1216 new
1217 call setline(1, repeat('x', 950) .. ' one two three')
1218 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1219 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1220 %d
1221 " should fail when more than 950 characters are in a word
1222 call setline(1, repeat('x', 951) .. ' one two three')
1223 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1224 call assert_equal(repeat('x', 951), getline(2))
1225
1226 " Test for adding a very long word to an existing completion
1227 %d
1228 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1229 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1230 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1231 bw!
1232endfunc
1233
1234" Test for some fields in the complete items used by complete()
1235func Test_complete_items()
1236 func CompleteItems(idx)
1237 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1238 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1239 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1240 \ [#{user_data: 'u9'}],
1241 \ [#{word: "", user_data: 'u10'}],
1242 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1243 call complete(col('.'), items[a:idx])
1244 return ''
1245 endfunc
1246 new
1247 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1248 call assert_equal('u2', v:completed_item.user_data)
1249 call assert_equal('one', getline(1))
1250 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1251 call assert_equal('u3', v:completed_item.user_data)
1252 call assert_equal('one', getline(2))
1253 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1254 call assert_equal('', getline(3))
1255 set completeopt=menu,noinsert
1256 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1257 call assert_equal('oNE', getline(4))
1258 call assert_equal('u8', v:completed_item.user_data)
1259 set completeopt&
1260 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1261 call assert_equal('', getline(5))
1262 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1263 call assert_equal('', getline(6))
1264 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1265 call assert_equal('', getline(7))
1266 call assert_equal('u11', v:completed_item.user_data)
1267 " pass invalid argument to complete()
1268 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1269 call assert_fails('exe cmd', 'E730:')
1270 bw!
1271 delfunc CompleteItems
1272endfunc
1273
1274" Test for the "refresh" item in the dict returned by an insert completion
1275" function
1276func Test_complete_item_refresh_always()
1277 let g:CallCount = 0
1278 func! Tcomplete(findstart, base)
1279 if a:findstart
1280 " locate the start of the word
1281 let line = getline('.')
1282 let start = col('.') - 1
1283 while start > 0 && line[start - 1] =~ '\a'
1284 let start -= 1
1285 endwhile
1286 return start
1287 else
1288 let g:CallCount += 1
1289 let res = ["update1", "update12", "update123"]
1290 return #{words: res, refresh: 'always'}
1291 endif
1292 endfunc
1293 new
1294 set completeopt=menu,longest
1295 set completefunc=Tcomplete
1296 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1297 call assert_equal('up', getline(1))
1298 call assert_equal(2, g:CallCount)
1299 set completeopt&
1300 set completefunc&
1301 bw!
1302 delfunc Tcomplete
1303endfunc
1304
1305" Test for completing from a thesaurus file without read permission
1306func Test_complete_unreadable_thesaurus_file()
1307 CheckUnix
1308 CheckNotRoot
1309
1310 call writefile(['about', 'above'], 'Xfile')
1311 call setfperm('Xfile', '---r--r--')
1312 new
1313 set complete=sXfile
1314 exe "normal! ia\<C-P>"
1315 call assert_equal('a', getline(1))
1316 bw!
1317 call delete('Xfile')
1318 set complete&
1319endfunc
1320
Bram Moolenaarcc233582020-12-12 13:32:07 +01001321" Test to ensure 'Scanning...' messages are not recorded in messages history
1322func Test_z1_complete_no_history()
1323 new
1324 messages clear
1325 let currmess = execute('messages')
1326 setlocal dictionary=README.txt
1327 exe "normal owh\<C-X>\<C-K>"
1328 exe "normal owh\<C-N>"
1329 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001330 bwipe!
1331endfunc
1332
1333" A mapping is not used for the key after CTRL-X.
1334func Test_no_mapping_for_ctrl_x_key()
1335 new
1336 inoremap <C-K> <Cmd>let was_mapped = 'yes'<CR>
1337 setlocal dictionary=README.txt
1338 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1339 call assert_equal('example ', getline(1))
1340 call assert_false(exists('was_mapped'))
1341 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001342endfunc
1343
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001344" Test for different ways of setting the 'completefunc' option
1345func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001346 func CompleteFunc1(callnr, findstart, base)
1347 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1348 return a:findstart ? 0 : []
1349 endfunc
1350 func CompleteFunc2(findstart, base)
1351 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001352 return a:findstart ? 0 : []
1353 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001354
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001355 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001356 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001357 LET &completefunc = 'g:CompleteFunc2'
1358 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001359 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001360 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001361 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001362 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001363 bw!
1364
1365 #" Test for using a function()
1366 set completefunc=function('g:CompleteFunc1',\ [10])
1367 new
1368 call setline(1, 'one')
1369 LET g:CompleteFunc1Args = []
1370 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1371 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001372 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001373
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001374 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001375 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001376 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001377 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001378 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001379 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001380 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001381 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001382 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001383
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001384 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001385 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001386 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001387 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001388 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001389 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001390 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001391 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001392 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001393
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001394 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001395 set completefunc=funcref('g:CompleteFunc1',\ [13])
1396 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001397 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001398 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001399 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001400 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001401 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001402
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001403 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001404 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001405 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001406 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001407 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001408 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001409 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001410 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001411 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001412
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001413 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001414 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001415 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001416 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001417 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001418 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001419 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001420 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001421 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001422
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001423 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001424 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001425 LET optval = substitute(optval, ' ', '\\ ', 'g')
1426 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001427 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001428 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001429 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001430 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001431 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001432 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001433
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001434 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001435 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001436 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001437 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001438 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001439 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001440 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001441 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001442
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001443 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001444 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001445 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001446 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001447 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001448 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001449 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001450 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001451
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001452 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001453 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001454 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001455 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001456 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001457 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001458 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001459 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001460 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001461
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001462 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001463 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001464 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001465 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001466 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001467 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001468 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001469 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001470 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001471
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001472 #" Test for using a lambda function with incorrect return value
1473 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1474 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001475 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001476 call setline(1, 'eight')
1477 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1478 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001479
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001480 #" Test for clearing the 'completefunc' option
1481 set completefunc=''
1482 set completefunc&
1483 call assert_fails("set completefunc=function('abc')", "E700:")
1484 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001485
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001486 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001487 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001488 call setline(1, 'five')
1489 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1490 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001491 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001492 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001493 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001494 bw!
1495 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001496 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001497
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001498 " Test for using a script-local function name
1499 func s:CompleteFunc3(findstart, base)
1500 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1501 return a:findstart ? 0 : []
1502 endfunc
1503 set completefunc=s:CompleteFunc3
1504 new
1505 call setline(1, 'script1')
1506 let g:CompleteFunc3Args = []
1507 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1508 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1509 bw!
1510
1511 let &completefunc = 's:CompleteFunc3'
1512 new
1513 call setline(1, 'script2')
1514 let g:CompleteFunc3Args = []
1515 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1516 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1517 bw!
1518 delfunc s:CompleteFunc3
1519
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001520 " In Vim9 script s: can be omitted
1521 let lines =<< trim END
1522 vim9script
1523 var CompleteFunc4Args = []
1524 def CompleteFunc4(findstart: bool, base: string): any
1525 add(CompleteFunc4Args, [findstart, base])
1526 return findstart ? 0 : []
1527 enddef
1528 set completefunc=CompleteFunc4
1529 new
1530 setline(1, 'script1')
1531 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1532 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1533 bw!
1534 END
1535 call v9.CheckScriptSuccess(lines)
1536
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001537 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001538 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001539 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1540
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001541 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001542 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001543 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001544 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001545 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001546 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001547
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001548 " set 'completefunc' to a partial with dict. This used to cause a crash.
1549 func SetCompleteFunc()
1550 let params = {'complete': function('g:DictCompleteFunc')}
1551 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001552 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001553 func g:DictCompleteFunc(_) dict
1554 endfunc
1555 call SetCompleteFunc()
1556 new
1557 call SetCompleteFunc()
1558 bw
1559 call test_garbagecollect_now()
1560 new
1561 set completefunc=
1562 wincmd w
1563 set completefunc=
1564 %bw!
1565 delfunc g:DictCompleteFunc
1566 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001567
1568 " Vim9 tests
1569 let lines =<< trim END
1570 vim9script
1571
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001572 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1573 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001574 return findstart ? 0 : []
1575 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001576
1577 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001578 set completefunc=function('Vim9CompleteFunc',\ [60])
1579 new | only
1580 setline(1, 'one')
1581 g:Vim9completeFuncArgs = []
1582 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1583 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1584 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001585
1586 # Test for using a global function name
1587 &completefunc = g:CompleteFunc2
1588 new | only
1589 setline(1, 'two')
1590 g:CompleteFunc2Args = []
1591 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1592 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1593 bw!
1594
1595 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001596 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001597 add(g:LocalCompleteFuncArgs, [findstart, base])
1598 return findstart ? 0 : []
1599 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001600 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001601 new | only
1602 setline(1, 'three')
1603 g:LocalCompleteFuncArgs = []
1604 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1605 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1606 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001607 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001608 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001609
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001610 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001611 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001612 delfunc CompleteFunc1
1613 delfunc CompleteFunc2
1614 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001615 %bw!
1616endfunc
1617
1618" Test for different ways of setting the 'omnifunc' option
1619func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001620 func OmniFunc1(callnr, findstart, base)
1621 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1622 return a:findstart ? 0 : []
1623 endfunc
1624 func OmniFunc2(findstart, base)
1625 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001626 return a:findstart ? 0 : []
1627 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001628
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001629 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 #" Test for using a function name
1631 LET &omnifunc = 'g:OmniFunc2'
1632 new
1633 call setline(1, 'zero')
1634 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001635 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001636 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1637 bw!
1638
1639 #" Test for using a function()
1640 set omnifunc=function('g:OmniFunc1',\ [10])
1641 new
1642 call setline(1, 'one')
1643 LET g:OmniFunc1Args = []
1644 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1645 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001646 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001647
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001648 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001649 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001650 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001651 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001652 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001653 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001654 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001655 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001656 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001657
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001659 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001661 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001662 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001663 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001664 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001665 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001666 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001667
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001668 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001669 set omnifunc=funcref('g:OmniFunc1',\ [13])
1670 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001672 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001674 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001676
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001677 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001678 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001679 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001680 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001681 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001682 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001683 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001684 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001686
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001688 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001690 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001691 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001692 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001694 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001695 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001696
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001697 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001698 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001699 LET optval = substitute(optval, ' ', '\\ ', 'g')
1700 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001701 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001702 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001703 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001704 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001706 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001707
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001708 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001709 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001710 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001711 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001712 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001713 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001714 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001715 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001716
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001717 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001718 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001719 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001720 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001721 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001722 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001723 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001724 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001725
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001726 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001727 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001728 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001729 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001730 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001731 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001732 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001733 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001734 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001735
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001736 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001737 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001738 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001739 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001740 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001741 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001742 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001743 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001744 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001745
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001746 #" Test for using a lambda function with incorrect return value
1747 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1748 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001749 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001750 call setline(1, 'eight')
1751 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1752 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001753
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001754 #" Test for clearing the 'omnifunc' option
1755 set omnifunc=''
1756 set omnifunc&
1757 call assert_fails("set omnifunc=function('abc')", "E700:")
1758 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001759
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001760 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001761 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001762 call setline(1, 'nine')
1763 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1764 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001765 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001766 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001767 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001768 bw!
1769 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001770 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001771
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001772 " Test for using a script-local function name
1773 func s:OmniFunc3(findstart, base)
1774 call add(g:OmniFunc3Args, [a:findstart, a:base])
1775 return a:findstart ? 0 : []
1776 endfunc
1777 set omnifunc=s:OmniFunc3
1778 new
1779 call setline(1, 'script1')
1780 let g:OmniFunc3Args = []
1781 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1782 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
1783 bw!
1784
1785 let &omnifunc = 's:OmniFunc3'
1786 new
1787 call setline(1, 'script2')
1788 let g:OmniFunc3Args = []
1789 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1790 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
1791 bw!
1792 delfunc s:OmniFunc3
1793
1794 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001795 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001796 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1797
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001798 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001799 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001800 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001801 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001802 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001803 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001804
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001805 " set 'omnifunc' to a partial with dict. This used to cause a crash.
1806 func SetOmniFunc()
1807 let params = {'omni': function('g:DictOmniFunc')}
1808 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001809 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001810 func g:DictOmniFunc(_) dict
1811 endfunc
1812 call SetOmniFunc()
1813 new
1814 call SetOmniFunc()
1815 bw
1816 call test_garbagecollect_now()
1817 new
1818 set omnifunc=
1819 wincmd w
1820 set omnifunc=
1821 %bw!
1822 delfunc g:DictOmniFunc
1823 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001824
1825 " Vim9 tests
1826 let lines =<< trim END
1827 vim9script
1828
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001829 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
1830 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001831 return findstart ? 0 : []
1832 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001833
1834 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001835 set omnifunc=function('Vim9omniFunc',\ [60])
1836 new | only
1837 setline(1, 'one')
1838 g:Vim9omniFunc_Args = []
1839 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1840 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
1841 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001842
1843 # Test for using a global function name
1844 &omnifunc = g:OmniFunc2
1845 new | only
1846 setline(1, 'two')
1847 g:OmniFunc2Args = []
1848 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1849 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
1850 bw!
1851
1852 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001853 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001854 add(g:LocalOmniFuncArgs, [findstart, base])
1855 return findstart ? 0 : []
1856 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001857 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001858 new | only
1859 setline(1, 'three')
1860 g:LocalOmniFuncArgs = []
1861 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1862 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
1863 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001864 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001865 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001866
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001867 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001868 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001869 delfunc OmniFunc1
1870 delfunc OmniFunc2
1871 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001872 %bw!
1873endfunc
1874
1875" Test for different ways of setting the 'thesaurusfunc' option
1876func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001877 func TsrFunc1(callnr, findstart, base)
1878 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001879 return a:findstart ? 0 : []
1880 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001881 func TsrFunc2(findstart, base)
1882 call add(g:TsrFunc2Args, [a:findstart, a:base])
1883 return a:findstart ? 0 : ['sunday']
1884 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001885
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001886 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001887 #" Test for using a function name
1888 LET &thesaurusfunc = 'g:TsrFunc2'
1889 new
1890 call setline(1, 'zero')
1891 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001892 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001893 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
1894 bw!
1895
1896 #" Test for using a function()
1897 set thesaurusfunc=function('g:TsrFunc1',\ [10])
1898 new
1899 call setline(1, 'one')
1900 LET g:TsrFunc1Args = []
1901 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
1902 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001903 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001904
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001905 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001906 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001907 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001908 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001909 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001910 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001911 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001912 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001913 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001914
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001915 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001916 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001917 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001918 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001919 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001920 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001921 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001922 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001924
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001925 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001926 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
1927 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001928 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001929 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001930 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001931 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001932 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001933
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001934 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001935 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001936 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001937 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001938 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001939 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001940 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001941 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001942 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001943
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001945 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001947 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001948 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001949 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001950 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001951 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001952 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001953
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001954 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00001955 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001956 LET optval = substitute(optval, ' ', '\\ ', 'g')
1957 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001958 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001960 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001961 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001964
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001965 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001966 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001967 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001968 call setline(1, 'six')
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')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001971 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001972 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001973
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001974 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001975 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001976 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001977 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001978 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001979 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001980 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001981 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001982
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001983 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001984 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001985 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001986 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001987 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001988 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001989 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001990 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001991 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001992
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001993 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001994 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001995 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001996 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001997 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001998 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001999 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002000 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002001 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002002
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002003 #" Test for using a lambda function with incorrect return value
2004 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2005 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002006 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002007 call setline(1, 'eight')
2008 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2009 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002010
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002011 #" Test for clearing the 'thesaurusfunc' option
2012 set thesaurusfunc=''
2013 set thesaurusfunc&
2014 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
2015 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002016
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002017 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002018 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002019 call setline(1, 'ten')
2020 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
2021 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002022 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002023 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002024 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002025 bw!
2026
2027 #" Use a buffer-local value and a global value
2028 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002029 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002030 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002031 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002032 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2033 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002034 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002035 new
2036 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002037 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002038 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2039 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002040 call assert_equal([], g:TsrFunc1Args)
2041 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002042 wincmd w
2043 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002044 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002045 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2046 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002047 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002048 :%bw!
2049 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002050 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002051
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002052 " Test for using a script-local function name
2053 func s:TsrFunc3(findstart, base)
2054 call add(g:TsrFunc3Args, [a:findstart, a:base])
2055 return a:findstart ? 0 : []
2056 endfunc
2057 set tsrfu=s:TsrFunc3
2058 new
2059 call setline(1, 'script1')
2060 let g:TsrFunc3Args = []
2061 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2062 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2063 bw!
2064
2065 let &tsrfu = 's:TsrFunc3'
2066 new
2067 call setline(1, 'script2')
2068 let g:TsrFunc3Args = []
2069 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2070 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2071 bw!
2072 delfunc s:TsrFunc3
2073
2074 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002075 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002076 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2077
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002078 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002079 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002080 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002081 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002082 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002083 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002084 bw!
2085
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002086 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2087 func SetTsrFunc()
2088 let params = {'thesaurus': function('g:DictTsrFunc')}
2089 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002090 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002091 func g:DictTsrFunc(_) dict
2092 endfunc
2093 call SetTsrFunc()
2094 new
2095 call SetTsrFunc()
2096 bw
2097 call test_garbagecollect_now()
2098 new
2099 set thesaurusfunc=
2100 wincmd w
2101 %bw!
2102 delfunc SetTsrFunc
2103
2104 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2105 " cause a crash.
2106 func SetLocalTsrFunc()
2107 let params = {'thesaurus': function('g:DictTsrFunc')}
2108 let &l:thesaurusfunc = params.thesaurus
2109 endfunc
2110 call SetLocalTsrFunc()
2111 call test_garbagecollect_now()
2112 call SetLocalTsrFunc()
2113 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002114 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002115 delfunc g:DictTsrFunc
2116 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002117
2118 " Vim9 tests
2119 let lines =<< trim END
2120 vim9script
2121
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002122 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2123 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002124 return findstart ? 0 : []
2125 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002126
2127 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002128 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2129 new | only
2130 setline(1, 'one')
2131 g:Vim9tsrFunc_Args = []
2132 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2133 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2134 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002135
2136 # Test for using a global function name
2137 &thesaurusfunc = g:TsrFunc2
2138 new | only
2139 setline(1, 'two')
2140 g:TsrFunc2Args = []
2141 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2142 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2143 bw!
2144
2145 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002146 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002147 add(g:LocalTsrFuncArgs, [findstart, base])
2148 return findstart ? 0 : []
2149 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002150 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002151 new | only
2152 setline(1, 'three')
2153 g:LocalTsrFuncArgs = []
2154 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2155 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2156 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002157 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002158 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002159
2160 " cleanup
2161 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002162 delfunc TsrFunc1
2163 delfunc TsrFunc2
2164 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002165 %bw!
2166endfunc
2167
Christian Brabandtac72c212022-04-07 21:00:53 +01002168func FooBarComplete(findstart, base)
2169 if a:findstart
2170 return col('.') - 1
2171 else
2172 return ["Foo", "Bar", "}"]
2173 endif
2174endfunc
2175
2176func Test_complete_smartindent()
2177 new
2178 setlocal smartindent completefunc=FooBarComplete
2179
2180 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2181 let result = getline(1,'$')
2182 call assert_equal(['', '{','}',''], result)
2183 bw!
2184 delfunction! FooBarComplete
2185endfunc
2186
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002187" vim: shiftwidth=2 sts=2 expandtab