blob: 466e358cf8cd54a3dfcb598122def8d8d37be7c4 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for insert completion
2
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01003source screendump.vim
Bram Moolenaar50f91d22019-08-02 19:52:15 +02004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaar15993ce2017-10-26 20:21:44 +02006
7" Test for insert expansion
8func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02009 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +010010 " The files in the current directory interferes with the files
11 " used by this test. So use a separate directory for the test.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010012 call mkdir('Xcpldir')
13 cd Xcpldir
Bram Moolenaarfb094e12017-11-05 20:59:28 +010014
Bram Moolenaar15993ce2017-10-26 20:21:44 +020015 set ff=unix
16 call writefile(["test11\t36Gepeto\t/Tag/",
17 \ "asd\ttest11file\t36G",
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010018 \ "Makefile\tto\trun"], 'Xtestfile', 'D')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020019 call writefile(['', 'start of testfile',
20 \ 'ru',
21 \ 'run1',
22 \ 'run2',
23 \ 'STARTTEST',
24 \ 'ENDTEST',
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010025 \ 'end of testfile'], 'Xtestdata', 'D')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020026 set ff&
27
28 enew!
29 edit Xtestdata
30 new
31 call append(0, ['#include "Xtestfile"', ''])
32 call cursor(2, 1)
33
34 set cot=
35 set cpt=.,w
36 " add-expands (word from next line) from other window
37 exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
38 call assert_equal('run1 run3', getline('.'))
39 " add-expands (current buffer first)
40 exe "normal o\<C-P>\<C-X>\<C-N>"
41 call assert_equal('run3 run3', getline('.'))
42 " Local expansion, ends in an empty line (unless it becomes a global
43 " expansion)
44 exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
45 call assert_equal('', getline('.'))
46 " starts Local and switches to global add-expansion
47 exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
48 call assert_equal('run1 run2', getline('.'))
49
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +000050 set cpt=.,\ ,w,i
Bram Moolenaar15993ce2017-10-26 20:21:44 +020051 " i-add-expands and switches to local
52 exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
53 call assert_equal("Makefile\tto\trun3", getline('.'))
Dominique Pelle923dce22021-11-21 11:36:04 +000054 " add-expands lines (it would end in an empty line if it didn't ignore
Bram Moolenaar15993ce2017-10-26 20:21:44 +020055 " itself)
56 exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
57 call assert_equal("Makefile\tto\trun3", getline('.'))
58 call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
59
60 set cpt=kXtestfile
61 " checks k-expansion, and file expansion (use Xtest11 instead of test11,
62 " because TEST11.OUT may match first on DOS)
63 write Xtest11.one
64 write Xtest11.two
65 exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
66 call assert_equal('Xtest11.two', getline('.'))
67
68 " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
69 " CTRL-F again to verify this doesn't cause trouble.
70 exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
71 call assert_equal('Xtest11.one', getline('.'))
72 normal ddk
73
Yegappan Lakshmanan37079142022-01-08 10:38:48 +000074 " Test for expanding a non-existing filename
75 exe "normal oa1b2X3Y4\<C-X>\<C-F>"
76 call assert_equal('a1b2X3Y4', getline('.'))
77 normal ddk
78
Bram Moolenaar15993ce2017-10-26 20:21:44 +020079 set cpt=w
80 " checks make_cyclic in other window
81 exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
82 call assert_equal('STARTTEST', getline('.'))
83
84 set cpt=u nohid
85 " checks unloaded buffer expansion
86 only
87 exe "normal oEN\<C-N>"
88 call assert_equal('ENDTEST', getline('.'))
89 " checks adding mode abortion
90 exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
91 call assert_equal('unless', getline('.'))
92
93 set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
94 " tag expansion, define add-expansion interrupted
95 exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
96 call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
97 " t-expansion
98 exe "normal oa\<C-N>\<Esc>"
99 call assert_equal('asd', getline('.'))
100
101 %bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200102 call delete('Xtest11.one')
103 call delete('Xtest11.two')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200104 set cpt& cot& def& tags& tagbsearch& hidden&
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100105 cd ..
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100106 call delete('Xcpldir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200107endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100108
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000109func Test_ins_complete_invalid_byte()
110 if has('unix') && executable('base64')
111 " this weird command was causing an illegal memory access
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100112 call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64', 'D')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000113 call system('base64 -d Xinvalid64 > Xinvalid')
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100114 call writefile(['qa!'], 'Xexit', 'D')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000115 call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000116 call delete('Xinvalid')
Bram Moolenaar4b28ba32021-12-27 19:28:37 +0000117 endif
118endfunc
119
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100120func Test_omni_dash()
121 func Omni(findstart, base)
122 if a:findstart
123 return 5
124 else
125 echom a:base
126 return ['-help', '-v']
127 endif
128 endfunc
129 set omnifunc=Omni
130 new
131 exe "normal Gofind -\<C-x>\<C-o>"
Bram Moolenaarcc233582020-12-12 13:32:07 +0100132 call assert_equal("find -help", getline('$'))
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100133
134 bwipe!
135 delfunc Omni
136 set omnifunc=
137endfunc
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100138
LemonBoy9bcb9ca2022-05-26 15:23:26 +0100139func Test_omni_throw()
140 let g:CallCount = 0
141 func Omni(findstart, base)
142 let g:CallCount += 1
143 if a:findstart
144 throw "he he he"
145 endif
146 endfunc
147 set omnifunc=Omni
148 new
149 try
150 exe "normal ifoo\<C-x>\<C-o>"
151 call assert_false(v:true, 'command should have failed')
152 catch
153 call assert_exception('he he he')
154 call assert_equal(1, g:CallCount)
155 endtry
156
157 bwipe!
158 delfunc Omni
159 unlet g:CallCount
160 set omnifunc=
161endfunc
162
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200163func Test_omni_autoload()
164 let save_rtp = &rtp
165 set rtp=Xruntime/some
166 let dir = 'Xruntime/some/autoload'
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100167 call mkdir(dir, 'pR')
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200168
169 let lines =<< trim END
170 vim9script
Bram Moolenaar6a058072022-01-30 18:56:35 +0000171 export def Func(findstart: bool, base: string): any
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200172 if findstart
173 return 1
174 else
175 return ['match']
176 endif
177 enddef
178 {
179 eval 1 + 2
180 }
181 END
182 call writefile(lines, dir .. '/omni.vim')
183
184 new
Bram Moolenaar6a058072022-01-30 18:56:35 +0000185 setlocal omnifunc=omni#Func
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200186 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt')
187
188 bwipe!
Bram Moolenaarff34bee2021-07-25 20:27:06 +0200189 set omnifunc=
190 let &rtp = save_rtp
191endfunc
192
Bram Moolenaarffa96842018-06-12 22:05:14 +0200193func Test_completefunc_args()
194 let s:args = []
195 func! CompleteFunc(findstart, base)
196 let s:args += [[a:findstart, empty(a:base)]]
197 endfunc
198 new
199
200 set completefunc=CompleteFunc
201 call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200202 call assert_equal([1, 1], s:args[0])
203 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200204 set completefunc=
205
206 let s:args = []
207 set omnifunc=CompleteFunc
208 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200209 call assert_equal([1, 1], s:args[0])
210 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200211 set omnifunc=
212
213 bwipe!
214 unlet s:args
215 delfunc CompleteFunc
216endfunc
217
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100218func s:CompleteDone_CompleteFuncNone( findstart, base )
219 if a:findstart
220 return 0
221 endif
222
223 return v:none
224endfunc
225
Bram Moolenaar1e115362019-01-09 23:01:02 +0100226func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100227 if a:findstart
228 return 0
229 endif
230
231 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100232 \ 'words': [
233 \ {
234 \ 'word': 'aword',
235 \ 'abbr': 'wrd',
236 \ 'menu': 'extra text',
237 \ 'info': 'words are cool',
238 \ 'kind': 'W',
zeertzjq4cd45f12022-12-15 13:48:30 +0000239 \ 'user_data': ['one', 'two']
Bram Moolenaar08928322020-01-04 14:32:48 +0100240 \ }
241 \ ]
242 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100243endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100244
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100245func s:CompleteDone_CheckCompletedItemNone()
246 let s:called_completedone = 1
247endfunc
248
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100249func s:CompleteDone_CheckCompletedItemDict(pre)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100250 call assert_equal( 'aword', v:completed_item[ 'word' ] )
251 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
252 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
253 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
254 call assert_equal( 'W', v:completed_item[ 'kind' ] )
zeertzjq4cd45f12022-12-15 13:48:30 +0000255 call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100256
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100257 if a:pre
258 call assert_equal('function', complete_info().mode)
259 endif
Bram Moolenaar17e04782020-01-17 18:58:59 +0100260
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100261 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100262endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100263
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100264func Test_CompleteDoneNone()
265 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
Bram Moolenaar9845f362019-04-08 18:59:54 +0200266 let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100267
268 set completefunc=<SID>CompleteDone_CompleteFuncNone
269 execute "normal a\<C-X>\<C-U>\<C-Y>"
270 set completefunc&
Bram Moolenaar9845f362019-04-08 18:59:54 +0200271 let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100272
273 call assert_true(s:called_completedone)
Bram Moolenaar9845f362019-04-08 18:59:54 +0200274 call assert_equal(oldline, newline)
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100275
276 let s:called_completedone = 0
277 au! CompleteDone
278endfunc
279
glepnir1c5a1202024-12-04 20:27:34 +0100280func Test_CompleteDone_vevent_keys()
281 func OnDone()
282 let g:complete_word = get(v:event, 'complete_word', v:null)
283 let g:complete_type = get(v:event, 'complete_type', v:null)
284 endfunction
285
286 autocmd CompleteDone * :call OnDone()
287
288 func CompleteFunc(findstart, base)
289 if a:findstart
290 return col(".")
291 endif
292 return [#{word: "foo"}, #{word: "bar"}]
293 endfunc
294 set omnifunc=CompleteFunc
295 set completefunc=CompleteFunc
296 set completeopt+=menuone
297
298 new
299 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'tx')
300 call assert_equal('', g:complete_word)
301 call assert_equal('omni', g:complete_type)
302
303 call feedkeys("S\<C-X>\<C-O>\<C-Y>\<Esc>", 'tx')
304 call assert_equal('foo', g:complete_word)
305 call assert_equal('omni', g:complete_type)
306
307 call feedkeys("S\<C-X>\<C-O>\<C-N>\<C-Y>\<Esc>0", 'tx')
308 call assert_equal('bar', g:complete_word)
309 call assert_equal('omni', g:complete_type)
310
311 call feedkeys("Shello vim visual v\<C-X>\<C-N>\<ESC>", 'tx')
312 call assert_equal('', g:complete_word)
313 call assert_equal('keyword', g:complete_type)
314
315 call feedkeys("Shello vim visual v\<C-X>\<C-N>\<C-Y>", 'tx')
316 call assert_equal('vim', g:complete_word)
317 call assert_equal('keyword', g:complete_type)
318
319 call feedkeys("Shello vim visual v\<C-X>\<C-N>\<C-Y>", 'tx')
320 call assert_equal('vim', g:complete_word)
321 call assert_equal('keyword', g:complete_type)
322
323 call feedkeys("Shello vim\<CR>completion test\<CR>\<C-X>\<C-l>\<C-Y>", 'tx')
324 call assert_equal('completion test', g:complete_word)
325 call assert_equal('whole_line', g:complete_type)
326
327 call feedkeys("S\<C-X>\<C-U>\<C-Y>", 'tx')
328 call assert_equal('foo', g:complete_word)
329 call assert_equal('function', g:complete_type)
330
331 inoremap <buffer> <f3> <cmd>call complete(1, ["red", "blue"])<cr>
332 call feedkeys("S\<f3>\<C-Y>", 'tx')
333 call assert_equal('red', g:complete_word)
334 call assert_equal('eval', g:complete_type)
335
336 call feedkeys("S\<C-X>\<C-V>\<C-Y>", 'tx')
337 call assert_equal('!', g:complete_word)
338 call assert_equal('cmdline', g:complete_type)
339
340 call writefile([''], 'foo_test', 'D')
341 call feedkeys("Sfoo\<C-X>\<C-F>\<C-Y>\<Esc>", 'tx')
342 call assert_equal('foo_test', g:complete_word)
343 call assert_equal('files', g:complete_type)
344
345 call writefile(['hello help'], 'test_case.txt', 'D')
346 set dictionary=test_case.txt
347 call feedkeys("ggdGSh\<C-X>\<C-K>\<C-Y>\<Esc>", 'tx')
348 call assert_equal('hello', g:complete_word)
349 call assert_equal('dictionary', g:complete_type)
350
351 set spell spelllang=en_us
352 call feedkeys("STheatre\<C-X>s\<C-Y>\<Esc>", 'tx')
353 call assert_equal('Theater', g:complete_word)
354 call assert_equal('spell', g:complete_type)
355
356 bwipe!
357 set completeopt& omnifunc& completefunc& spell& spelllang& dictionary&
358 autocmd! CompleteDone
359 delfunc OnDone
360 delfunc CompleteFunc
361 unlet g:complete_word
362 unlet g:complete_type
363endfunc
364
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100365func Test_CompleteDoneDict()
Bram Moolenaar3f169ce2020-01-26 22:43:31 +0100366 au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
367 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100368
369 set completefunc=<SID>CompleteDone_CompleteFuncDict
370 execute "normal a\<C-X>\<C-U>\<C-Y>"
371 set completefunc&
372
zeertzjq4cd45f12022-12-15 13:48:30 +0000373 call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100374 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100375
376 let s:called_completedone = 0
377 au! CompleteDone
378endfunc
379
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100380func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100381 if a:findstart
382 return 0
383 endif
384
385 return {
Bram Moolenaar08928322020-01-04 14:32:48 +0100386 \ 'words': [
387 \ {
388 \ 'word': 'aword',
389 \ 'abbr': 'wrd',
390 \ 'menu': 'extra text',
391 \ 'info': 'words are cool',
392 \ 'kind': 'W',
Bram Moolenaar08928322020-01-04 14:32:48 +0100393 \ }
394 \ ]
395 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100396endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100397
Bram Moolenaar1e115362019-01-09 23:01:02 +0100398func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100399 call assert_equal( 'aword', v:completed_item[ 'word' ] )
400 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
401 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
402 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
403 call assert_equal( 'W', v:completed_item[ 'kind' ] )
zeertzjq4cd45f12022-12-15 13:48:30 +0000404 call assert_equal( '', v:completed_item[ 'user_data' ] )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100405
406 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100407endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100408
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100409func Test_CompleteDoneDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100410 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
411
412 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
413 execute "normal a\<C-X>\<C-U>\<C-Y>"
414 set completefunc&
415
zeertzjq4cd45f12022-12-15 13:48:30 +0000416 call assert_equal('', v:completed_item[ 'user_data' ])
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100417 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100418
419 let s:called_completedone = 0
420 au! CompleteDone
421endfunc
422
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100423func s:CompleteDone_CompleteFuncList(findstart, base)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100424 if a:findstart
425 return 0
426 endif
427
428 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100429endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100430
Bram Moolenaar1e115362019-01-09 23:01:02 +0100431func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100432 call assert_equal( 'aword', v:completed_item[ 'word' ] )
433 call assert_equal( '', v:completed_item[ 'abbr' ] )
434 call assert_equal( '', v:completed_item[ 'menu' ] )
435 call assert_equal( '', v:completed_item[ 'info' ] )
436 call assert_equal( '', v:completed_item[ 'kind' ] )
437 call assert_equal( '', v:completed_item[ 'user_data' ] )
438
439 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100440endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100441
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100442func Test_CompleteDoneList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100443 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
444
445 set completefunc=<SID>CompleteDone_CompleteFuncList
446 execute "normal a\<C-X>\<C-U>\<C-Y>"
447 set completefunc&
448
Bram Moolenaarcee9bc22019-01-11 13:02:23 +0100449 call assert_equal('', v:completed_item[ 'user_data' ])
450 call assert_true(s:called_completedone)
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100451
452 let s:called_completedone = 0
453 au! CompleteDone
454endfunc
455
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200456func Test_CompleteDone_undo()
457 au CompleteDone * call append(0, "prepend1")
458 new
459 call setline(1, ["line1", "line2"])
460 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
461 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
462 \ getline(1, '$'))
463 undo
464 call assert_equal(["line1", "line2"], getline(1, '$'))
465 bwipe!
466 au! CompleteDone
467endfunc
468
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100469func Test_CompleteDone_modify()
470 let value = {
471 \ 'word': '',
472 \ 'abbr': '',
473 \ 'menu': '',
474 \ 'info': '',
475 \ 'kind': '',
476 \ 'user_data': '',
477 \ }
478 let v:completed_item = value
zeertzjq75020942022-07-31 11:37:20 +0100479 call assert_equal(value, v:completed_item)
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100480endfunc
481
Bram Moolenaarb806aa52020-09-12 22:52:57 +0200482func CompleteTest(findstart, query)
483 if a:findstart
484 return col('.')
485 endif
486 return ['matched']
487endfunc
488
489func Test_completefunc_info()
490 new
491 set completeopt=menuone
492 set completefunc=CompleteTest
493 call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
Bram Moolenaarf9d51352020-10-26 19:22:42 +0100494 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 +0200495 bwipe!
496 set completeopt&
497 set completefunc&
498endfunc
499
Girish Palya8950bf72024-03-20 20:07:29 +0100500func CompleteInfoUserDefinedFn(findstart, query)
501 " User defined function (i_CTRL-X_CTRL-U)
502 if a:findstart
503 return col('.')
504 endif
505 return [{'word': 'foo'}, {'word': 'bar'}, {'word': 'baz'}, {'word': 'qux'}]
506endfunc
507
508func CompleteInfoTestUserDefinedFn(mvmt, idx, noselect)
509 new
510 if a:noselect
511 set completeopt=menuone,popup,noinsert,noselect
512 else
513 set completeopt=menu,preview
514 endif
515 set completefunc=CompleteInfoUserDefinedFn
516 call feedkeys("i\<C-X>\<C-U>" . a:mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
517 let completed = a:idx != -1 ? ['foo', 'bar', 'baz', 'qux']->get(a:idx) : ''
518 call assert_equal(completed. "{'pum_visible': 1, 'mode': 'function', 'selected': " . a:idx . ", 'items': [" .
519 \ "{'word': 'foo', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
520 \ "{'word': 'bar', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
521 \ "{'word': 'baz', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
522 \ "{'word': 'qux', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" .
523 \ "]}", getline(1))
524 bwipe!
525 set completeopt&
526 set completefunc&
527endfunc
528
529func Test_complete_info_user_defined_fn()
530 " forward
531 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 1, v:true)
532 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 2, v:true)
533 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 2, v:false)
534 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 3, v:false)
535 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>", -1, v:false)
536 " backward
537 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 2, v:true)
538 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 1, v:true)
539 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>", -1, v:true)
540 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 3, v:false)
541 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 2, v:false)
542 " forward backward
543 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 1, v:true)
544 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 0, v:true)
545 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 2, v:false)
546 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 3, v:false)
547 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 1, v:false)
548 " backward forward
549 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 0, v:true)
550 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 2, v:true)
551 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 1, v:false)
552 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 3, v:false)
553 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-N>\<C-N>", 1, v:false)
554endfunc
555
zeertzjq440d4cb2023-03-02 17:51:32 +0000556" Test that mouse scrolling/movement should not interrupt completion.
557func Test_mouse_scroll_move_during_completion()
558 new
559 com! -buffer TestCommand1 echo 'TestCommand1'
560 com! -buffer TestCommand2 echo 'TestCommand2'
561 call setline(1, ['', '', '', '', ''])
562 call cursor(5, 1)
563
564 " Without completion menu scrolling can move text.
565 set completeopt-=menu wrap
566 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
567 call assert_equal('TestCommand2', getline('.'))
568 call assert_notequal(1, winsaveview().topline)
569 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
570 call assert_equal('TestCommand2', getline('.'))
571 call assert_equal(1, winsaveview().topline)
572 set nowrap
573 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
574 call assert_equal('TestCommand2', getline('.'))
575 call assert_notequal(0, winsaveview().leftcol)
576 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
577 call assert_equal('TestCommand2', getline('.'))
578 call assert_equal(0, winsaveview().leftcol)
579 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
580 call assert_equal('TestCommand2', getline('.'))
581
582 " With completion menu scrolling cannot move text.
583 set completeopt+=menu wrap
584 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
585 call assert_equal('TestCommand2', getline('.'))
586 call assert_equal(1, winsaveview().topline)
587 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
588 call assert_equal('TestCommand2', getline('.'))
589 call assert_equal(1, winsaveview().topline)
590 set nowrap
591 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
592 call assert_equal('TestCommand2', getline('.'))
593 call assert_equal(0, winsaveview().leftcol)
594 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
595 call assert_equal('TestCommand2', getline('.'))
596 call assert_equal(0, winsaveview().leftcol)
597 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
598 call assert_equal('TestCommand2', getline('.'))
599
600 bwipe!
601 set completeopt& wrap&
602endfunc
603
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100604" Check that when using feedkeys() typeahead does not interrupt searching for
605" completions.
606func Test_compl_feedkeys()
607 new
608 set completeopt=menuone,noselect
609 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
610 call assert_equal("jump jump", getline(1))
611 bwipe!
612 set completeopt&
613endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200614
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200615" Test for insert path completion with completeslash option
616func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200617 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200618
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100619 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200620 let orig_shellslash = &shellslash
621 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200622 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200623
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200624 set noshellslash
625
626 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100627 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100628 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200629
630 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100631 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100632 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200633
634 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100635 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100636 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200637
638 set shellslash
639
640 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100641 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100642 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200643
644 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100645 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100646 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200647
648 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100649 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100650 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200651 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200652
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200653 set noshellslash
654 set completeslash=slash
655 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
656
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200657 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200658 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200659endfunc
660
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200661func Test_pum_stopped_by_timer()
662 CheckScreendump
663
664 let lines =<< trim END
665 call setline(1, ['hello', 'hullo', 'heeee', ''])
666 func StartCompl()
667 call timer_start(100, { -> execute('stopinsert') })
668 call feedkeys("Gah\<C-N>")
669 endfunc
670 END
671
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100672 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200673 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
674 call term_sendkeys(buf, ":call StartCompl()\<CR>")
675 call TermWait(buf, 200)
676 call term_sendkeys(buf, "k")
677 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
678
679 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200680endfunc
681
zeertzjqcd5dbad2022-05-04 17:51:50 +0100682func Test_complete_stopinsert_startinsert()
683 nnoremap <F2> <Cmd>startinsert<CR>
684 inoremap <F2> <Cmd>stopinsert<CR>
685 " This just checks if this causes an error
686 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
687 nunmap <F2>
688 iunmap <F2>
689endfunc
690
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100691func Test_pum_with_folds_two_tabs()
692 CheckScreendump
693
694 let lines =<< trim END
695 set fdm=marker
696 call setline(1, ['" x {{{1', '" a some text'])
697 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
698 norm! zm
699 tab sp
700 call feedkeys('2Gzv', 'xt')
701 call feedkeys("0fa", 'xt')
702 END
703
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100704 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100705 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200706 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100707 call term_sendkeys(buf, "a\<C-N>")
708 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
709
710 call term_sendkeys(buf, "\<Esc>")
711 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100712endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100713
714func Test_pum_with_preview_win()
715 CheckScreendump
716
717 let lines =<< trim END
glepnir53387c52024-05-27 15:11:01 +0200718 func Omni_test(findstart, base)
719 if a:findstart
720 return col(".") - 1
721 endif
722 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
723 endfunc
724 set omnifunc=Omni_test
725 set completeopt+=longest
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100726 END
727
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100728 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100729 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100730 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100731 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100732 call term_sendkeys(buf, "\<C-N>")
733 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
734
735 call term_sendkeys(buf, "\<Esc>")
736 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100737endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100738
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000739func Test_scrollbar_on_wide_char()
740 CheckScreendump
741
742 let lines =<< trim END
743 call setline(1, ['a', ' 啊啊啊',
744 \ ' 哦哦哦',
745 \ ' 呃呃呃'])
746 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
747 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100748 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000749 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
750 call term_sendkeys(buf, "A\<C-N>")
751 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
752
753 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000754endfunc
755
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100756" Test for inserting the tag search pattern in insert mode
757func Test_ins_compl_tag_sft()
758 call writefile([
759 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
760 \ "first\tXfoo\t/^int first() {}$/",
761 \ "second\tXfoo\t/^int second() {}$/",
762 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100763 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100764 set tags=Xtags
765 let code =<< trim [CODE]
766 int first() {}
767 int second() {}
768 int third() {}
769 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100770 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100771
772 enew
773 set showfulltag
774 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
775 call assert_equal('int second() {}', getline(1))
776 set noshowfulltag
777
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100778 set tags&
779 %bwipe!
780endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200781
782" Test for 'completefunc' deleting text
783func Test_completefunc_error()
784 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200785 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200786 func CompleteFunc(findstart, base)
787 if a:findstart == 1
788 normal dd
789 return col('.') - 1
790 endif
791 return ['a', 'b']
792 endfunc
793 set completefunc=CompleteFunc
794 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100795 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200796
797 " delete text when called for the second time
798 func CompleteFunc2(findstart, base)
799 if a:findstart == 1
800 return col('.') - 1
801 endif
802 normal dd
803 return ['a', 'b']
804 endfunc
805 set completefunc=CompleteFunc2
806 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100807 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200808
Bram Moolenaar97202d92021-01-28 18:34:35 +0100809 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100810 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100811 if a:findstart == 1
812 return col('.') - 1
813 endif
814 wincmd p
815 return ['a', 'b']
816 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100817 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100818 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100819 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100820 close!
821
822 set completefunc&
823 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100824 delfunc CompleteFunc2
825 delfunc CompleteFunc3
826 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100827endfunc
828
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200829" Test for returning non-string values from 'completefunc'
830func Test_completefunc_invalid_data()
831 new
832 func! CompleteFunc(findstart, base)
833 if a:findstart == 1
834 return col('.') - 1
835 endif
836 return [{}, '', 'moon']
837 endfunc
838 set completefunc=CompleteFunc
839 exe "normal i\<C-X>\<C-U>"
840 call assert_equal('moon', getline(1))
841 set completefunc&
842 close!
843endfunc
844
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200845" Test for errors in using complete() function
846func Test_complete_func_error()
847 call assert_fails('call complete(1, ["a"])', 'E785:')
848 func ListColors()
849 call complete(col('.'), "blue")
850 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100851 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200852 func ListMonths()
853 call complete(col('.'), test_null_list())
854 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100855 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200856 delfunc ListColors
857 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100858 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200859 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200860endfunc
861
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000862" Test for recursively starting completion mode using complete()
863func Test_recursive_complete_func()
864 func ListColors()
865 call complete(5, ["red", "blue"])
866 return ''
867 endfunc
868 new
869 call setline(1, ['a1', 'a2'])
870 set complete=.
871 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
872 call assert_equal('a2blue', getline(3))
873 delfunc ListColors
874 bw!
875endfunc
876
bfredl87af60c2022-09-24 11:17:51 +0100877" Test for using complete() with completeopt+=longest
878func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100879 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100880 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100881
882 " default: insert first match
883 set completeopt&
884 call setline(1, ['i'])
885 exe "normal Aa\<f3>\<esc>"
886 call assert_equal('iaax', getline(1))
887
888 " with longest: insert longest prefix
889 set completeopt+=longest
890 call setline(1, ['i'])
891 exe "normal Aa\<f3>\<esc>"
892 call assert_equal('iaa', getline(1))
893 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100894 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100895endfunc
896
zeertzjq529b9ad2024-06-05 20:27:06 +0200897" Test for buffer-local value of 'completeopt'
898func Test_completeopt_buffer_local()
899 set completeopt=menu
900 new
901 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
902 call assert_equal('', &l:completeopt)
903 call assert_equal('menu', &completeopt)
904 call assert_equal('menu', &g:completeopt)
905
906 setlocal bufhidden=hide
907 enew
908 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
909 call assert_equal('', &l:completeopt)
910 call assert_equal('menu', &completeopt)
911 call assert_equal('menu', &g:completeopt)
912
913 setlocal completeopt+=fuzzy,noinsert
914 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
915 call assert_equal('menu,fuzzy,noinsert', &completeopt)
916 call assert_equal('menu', &g:completeopt)
917 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
918 call assert_equal('foobaz', getline('.'))
919
920 setlocal completeopt=
921 call assert_equal('', &l:completeopt)
922 call assert_equal('menu', &completeopt)
923 call assert_equal('menu', &g:completeopt)
924 call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
925 call assert_equal('foofoo', getline('.'))
926
927 setlocal completeopt+=longest
928 call assert_equal('menu,longest', &l:completeopt)
929 call assert_equal('menu,longest', &completeopt)
930 call assert_equal('menu', &g:completeopt)
931 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
932 call assert_equal('foo', getline('.'))
933
934 setlocal bufhidden=hide
935 buffer #
936 call assert_equal('', &l:completeopt)
937 call assert_equal('menu', &completeopt)
938 call assert_equal('menu', &g:completeopt)
939 call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
940 call assert_equal('foofoo', getline('.'))
941
942 setlocal completeopt+=fuzzy,noinsert
943 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
944 call assert_equal('menu,fuzzy,noinsert', &completeopt)
945 call assert_equal('menu', &g:completeopt)
946 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
947 call assert_equal('foobaz', getline('.'))
948
949 buffer #
950 call assert_equal('menu,longest', &l:completeopt)
951 call assert_equal('menu,longest', &completeopt)
952 call assert_equal('menu', &g:completeopt)
953 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
954 call assert_equal('foo', getline('.'))
955
956 setlocal bufhidden=wipe
957 buffer! #
958 bwipe!
959 call assert_equal('', &l:completeopt)
960 call assert_equal('menu', &completeopt)
961 call assert_equal('menu', &g:completeopt)
962
zeertzjq46dcd842024-11-03 09:10:50 +0100963 new | only
964 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
965 set completeopt&
966 setlocal completeopt=menu,fuzzy,noinsert
967 setglobal completeopt=menu,longest
968 call assert_equal('menu,fuzzy,noinsert', &completeopt)
969 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
970 call assert_equal('menu,longest', &g:completeopt)
971 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
972 call assert_equal('foobaz', getline('.'))
973 setlocal bufhidden=wipe
974 new | only!
975 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
976 call assert_equal('menu,longest', &completeopt)
977 call assert_equal('menu,longest', &g:completeopt)
978 call assert_equal('', &l:completeopt)
979 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
980 call assert_equal('foo', getline('.'))
981 bwipe!
982
983 new | only
984 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
985 set completeopt&
986 setlocal completeopt=menu,fuzzy,noinsert
987 set completeopt=menu,longest
988 call assert_equal('menu,longest', &completeopt)
989 call assert_equal('menu,longest', &g:completeopt)
990 call assert_equal('', &l:completeopt)
991 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
992 call assert_equal('foo', getline('.'))
993 setlocal bufhidden=wipe
994 new | only!
995 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
996 call assert_equal('menu,longest', &completeopt)
997 call assert_equal('menu,longest', &g:completeopt)
998 call assert_equal('', &l:completeopt)
999 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
1000 call assert_equal('foo', getline('.'))
1001 bwipe!
1002
zeertzjq529b9ad2024-06-05 20:27:06 +02001003 set completeopt&
1004endfunc
bfredl87af60c2022-09-24 11:17:51 +01001005
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001006" Test for completing words following a completed word in a line
1007func Test_complete_wrapscan()
1008 " complete words from another buffer
1009 new
1010 call setline(1, ['one two', 'three four'])
1011 new
1012 setlocal complete=w
1013 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
1014 call assert_equal('two three four', getline(1))
1015 close!
1016 " complete words from the current buffer
1017 setlocal complete=.
1018 %d
1019 call setline(1, ['one two', ''])
1020 call cursor(2, 1)
1021 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
1022 call assert_equal('one two one two', getline(2))
1023 close!
1024endfunc
1025
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001026" Test for completing special characters
1027func Test_complete_special_chars()
1028 new
1029 call setline(1, 'int .*[-\^$ func float')
1030 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
1031 call assert_equal('int .*[-\^$ func float', getline(2))
1032 close!
1033endfunc
1034
1035" Test for completion when text is wrapped across lines.
1036func Test_complete_across_line()
1037 new
1038 call setline(1, ['red green blue', 'one two three'])
1039 setlocal textwidth=20
1040 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1041 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
1042 close!
1043endfunc
1044
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00001045" Test for completing words with a '.' at the end of a word.
1046func Test_complete_joinspaces()
1047 new
1048 call setline(1, ['one two.', 'three. four'])
1049 set joinspaces
1050 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1051 call assert_equal("one two. three. four", getline(3))
1052 set joinspaces&
1053 bw!
1054endfunc
1055
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001056" Test for using CTRL-L to add one character when completing matching
1057func Test_complete_add_onechar()
1058 new
1059 call setline(1, ['wool', 'woodwork'])
1060 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
1061 call assert_equal('woof', getline(3))
1062
1063 " use 'ignorecase' and backspace to erase characters from the prefix string
1064 " and then add letters using CTRL-L
1065 %d
1066 set ignorecase backspace=2
1067 setlocal complete=.
1068 call setline(1, ['workhorse', 'workload'])
1069 normal Go
nwounkn2e3cd522023-10-17 11:05:38 +02001070 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001071 call assert_equal('workh', getline(3))
1072 set ignorecase& backspace&
1073 close!
1074endfunc
1075
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00001076" Test for using CTRL-X CTRL-L to complete whole lines lines
1077func Test_complete_wholeline()
1078 new
1079 " complete one-line
1080 call setline(1, ['a1', 'a2'])
1081 exe "normal ggoa\<C-X>\<C-L>"
1082 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1083 " go to the next match (wrapping around the buffer)
1084 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
1085 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
1086 " go to the next match
1087 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
1088 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
1089 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
1090 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1091 " repeat the test using CTRL-L
1092 " go to the next match (wrapping around the buffer)
1093 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
1094 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
1095 " go to the next match
1096 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
1097 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
1098 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
1099 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1100 %d
1101 " use CTRL-X CTRL-L to add one more line
1102 call setline(1, ['a1', 'b1'])
1103 setlocal complete=.
1104 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
1105 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
1106 bw!
1107endfunc
1108
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001109" Test insert completion with 'cindent' (adjust the indent)
1110func Test_complete_with_cindent()
1111 new
1112 setlocal cindent
1113 call setline(1, ['if (i == 1)', " j = 2;"])
1114 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
1115 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
1116
1117 %d
1118 call setline(1, ['when while', '{', ''])
1119 setlocal cinkeys+==while
1120 exe "normal Giwh\<C-P> "
1121 call assert_equal("\twhile ", getline('$'))
1122 close!
1123endfunc
1124
1125" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
1126func Test_complete_cmdline()
1127 new
1128 exe "normal icaddb\<C-X>\<C-V>"
1129 call assert_equal('caddbuffer', getline(1))
1130 exe "normal ocall getqf\<C-X>\<C-V>"
1131 call assert_equal('call getqflist(', getline(2))
1132 exe "normal oabcxyz(\<C-X>\<C-V>"
1133 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +02001134 com! -buffer TestCommand1 echo 'TestCommand1'
1135 com! -buffer TestCommand2 echo 'TestCommand2'
1136 write TestCommand1Test
1137 write TestCommand2Test
1138 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
1139 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
1140 call assert_equal('TestCommand2Test', getline(4))
1141 call delete('TestCommand1Test')
1142 call delete('TestCommand2Test')
1143 delcom TestCommand1
1144 delcom TestCommand2
1145 close!
1146endfunc
1147
1148" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
1149func Test_complete_stop()
1150 new
1151 func Save_mode1()
1152 let g:mode1 = mode(1)
1153 return ''
1154 endfunc
1155 func Save_mode2()
1156 let g:mode2 = mode(1)
1157 return ''
1158 endfunc
1159 inoremap <F1> <C-R>=Save_mode1()<CR>
1160 inoremap <F2> <C-R>=Save_mode2()<CR>
1161 call setline(1, ['aaa bbb ccc '])
1162 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1163 call assert_equal('ic', g:mode1)
1164 call assert_equal('i', g:mode2)
1165 call assert_equal('aaa bbb ccc ', getline(1))
1166 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1167 call assert_equal('ic', g:mode1)
1168 call assert_equal('i', g:mode2)
1169 call assert_equal('aaa bbb ccc aaa', getline(1))
1170 set completeopt+=noselect
1171 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1172 call assert_equal('ic', g:mode1)
1173 call assert_equal('i', g:mode2)
1174 call assert_equal('aaa bbb ccc aaa bb', getline(1))
1175 set completeopt&
1176 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1177 call assert_equal('ic', g:mode1)
1178 call assert_equal('i', g:mode2)
1179 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
1180 com! -buffer TestCommand1 echo 'TestCommand1'
1181 com! -buffer TestCommand2 echo 'TestCommand2'
1182 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1183 call assert_equal('ic', g:mode1)
1184 call assert_equal('i', g:mode2)
1185 call assert_equal('TestCommand2', getline(2))
1186 delcom TestCommand1
1187 delcom TestCommand2
1188 unlet g:mode1
1189 unlet g:mode2
1190 iunmap <F1>
1191 iunmap <F2>
1192 delfunc Save_mode1
1193 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001194 close!
1195endfunc
1196
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00001197" Test for typing CTRL-R in insert completion mode to insert a register
1198" content.
1199func Test_complete_reginsert()
1200 new
1201 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
1202
1203 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
1204 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
1205 " inserted.
1206 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
1207 call assert_equal('a123', getline(5))
1208 let @r = "\<C-P>\<C-P>"
1209 exe "normal GCa\<C-P>\<C-R>r"
1210 call assert_equal('a12', getline(5))
1211 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
1212 call assert_equal('a1234x', getline(5))
1213 bw!
1214endfunc
1215
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02001216func Test_issue_7021()
1217 CheckMSWindows
1218
1219 let orig_shellslash = &shellslash
1220 set noshellslash
1221
1222 set completeslash=slash
1223 call assert_false(expand('~') =~ '/')
1224
1225 let &shellslash = orig_shellslash
1226 set completeslash=
1227endfunc
1228
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001229" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
1230func Test_complete_longest_match()
1231 for e in ['latin1', 'utf-8']
1232 exe 'set encoding=' .. e
1233 new
1234 set complete=.
1235 set completeopt=menu,longest
1236 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
1237 exe "normal Gopfx\<C-P>"
1238 call assert_equal('pfx_', getline(5))
1239 bw!
1240 endfor
1241
1242 " Test for completing additional words with longest match set
1243 new
1244 call setline(1, ['abc1', 'abd2'])
1245 exe "normal Goab\<C-P>\<C-X>\<C-P>"
1246 call assert_equal('ab', getline(3))
1247 bw!
1248 set complete& completeopt&
1249endfunc
1250
1251" Test for removing the first displayed completion match and selecting the
1252" match just before that.
1253func Test_complete_erase_firstmatch()
1254 new
1255 call setline(1, ['a12', 'a34', 'a56'])
1256 set complete=.
1257 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1258 call assert_equal('a34', getline('$'))
1259 set complete&
1260 bw!
1261endfunc
1262
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001263" Test for completing words from unloaded buffers
1264func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001265 call writefile(['abc'], "Xfile1", 'D')
1266 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001267 edit Xfile1
1268 edit Xfile2
1269 new | close
1270 enew
1271 bunload Xfile1 Xfile2
1272 set complete=u
1273 " complete from an unloaded buffer
1274 exe "normal! ia\<C-P>"
1275 call assert_equal('abc', getline(1))
1276 exe "normal! od\<C-P>"
1277 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001278
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001279 set complete&
1280 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001281endfunc
1282
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001283" Test for completing whole lines from unloaded buffers
1284func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001285 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001286 edit Xfile1
1287 enew
1288 set complete=u
1289 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1290 call assert_equal('a line2', getline(1))
1291 %d
1292 " completing from an unlisted buffer should fail
1293 bdel Xfile1
1294 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1295 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001296
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001297 set complete&
1298 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001299endfunc
1300
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001301" Test for completing words from unlisted buffers
1302func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001303 call writefile(['abc'], "Xfile1", 'D')
1304 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001305 edit Xfile1
1306 edit Xfile2
1307 new | close
1308 bdel Xfile1 Xfile2
1309 set complete=U
1310 " complete from an unlisted buffer
1311 exe "normal! ia\<C-P>"
1312 call assert_equal('abc', getline(1))
1313 exe "normal! od\<C-P>"
1314 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001315
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001316 set complete&
1317 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001318endfunc
1319
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001320" Test for completing whole lines from unlisted buffers
1321func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001322 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001323 edit Xfile1
1324 enew
1325 set complete=U
zeertzjqc029c132024-03-28 11:37:26 +01001326 " completing from an unloaded buffer should fail
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001327 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1328 call assert_equal('a', getline(1))
1329 %d
1330 bdel Xfile1
1331 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1332 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001333
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001334 set complete&
1335 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001336endfunc
1337
1338" Test for adding a multibyte character using CTRL-L in completion mode
1339func Test_complete_mbyte_char_add()
1340 new
1341 set complete=.
1342 call setline(1, 'abė')
1343 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1344 call assert_equal('abė', getline(2))
1345 " Test for a leader with multibyte character
1346 %d
1347 call setline(1, 'abėĕ')
1348 exe "normal! oabė\<C-P>"
1349 call assert_equal('abėĕ', getline(2))
1350 bw!
1351endfunc
1352
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001353" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1354" not to complete matches from the local buffer. Also test using multiple
1355" <C-X> to cancel the current completion mode.
1356func Test_complete_local_expansion()
1357 new
1358 set complete=t
1359 call setline(1, ['abc', 'def'])
1360 exe "normal! Go\<C-X>\<C-P>"
1361 call assert_equal("def", getline(3))
1362 exe "normal! Go\<C-P>"
1363 call assert_equal("", getline(4))
1364 exe "normal! Go\<C-X>\<C-N>"
1365 call assert_equal("abc", getline(5))
1366 exe "normal! Go\<C-N>"
1367 call assert_equal("", getline(6))
1368
1369 " use multiple <C-X> to cancel the previous completion mode
1370 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1371 call assert_equal("", getline(7))
1372 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1373 call assert_equal("", getline(8))
1374 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1375 call assert_equal("abc", getline(9))
1376
1377 " interrupt the current completion mode
1378 set completeopt=menu,noinsert
1379 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1380 call assert_equal("abc", getline(10))
1381
1382 " when only one <C-X> is used to interrupt, do normal expansion
1383 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1384 call assert_equal("", getline(11))
1385 set completeopt&
1386
1387 " using two <C-X> in non-completion mode and restarting the same mode
1388 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1389 call assert_equal("def", getline(12))
1390
1391 " test for adding a match from the original empty text
1392 %d
1393 call setline(1, 'abc def g')
1394 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1395 call assert_equal('def', getline(2))
1396 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1397 call assert_equal('abc', getline(2))
1398
1399 bw!
1400endfunc
1401
1402" Test for undoing changes after a insert-mode completion
1403func Test_complete_undo()
1404 new
1405 set complete=.
1406 " undo with 'ignorecase'
1407 call setline(1, ['ABOVE', 'BELOW'])
1408 set ignorecase
1409 exe "normal! Goab\<C-G>u\<C-P>"
1410 call assert_equal("ABOVE", getline(3))
1411 undo
1412 call assert_equal("ab", getline(3))
1413 set ignorecase&
1414 %d
1415 " undo with longest match
1416 set completeopt=menu,longest
1417 call setline(1, ['above', 'about'])
1418 exe "normal! Goa\<C-G>u\<C-P>"
1419 call assert_equal("abo", getline(3))
1420 undo
1421 call assert_equal("a", getline(3))
1422 set completeopt&
1423 %d
1424 " undo for line completion
1425 call setline(1, ['above that change', 'below that change'])
1426 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1427 call assert_equal("above that change", getline(3))
1428 undo
1429 call assert_equal("above", getline(3))
1430
1431 bw!
1432endfunc
1433
1434" Test for completing a very long word
1435func Test_complete_long_word()
1436 set complete&
1437 new
1438 call setline(1, repeat('x', 950) .. ' one two three')
1439 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1440 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1441 %d
1442 " should fail when more than 950 characters are in a word
1443 call setline(1, repeat('x', 951) .. ' one two three')
1444 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1445 call assert_equal(repeat('x', 951), getline(2))
1446
1447 " Test for adding a very long word to an existing completion
1448 %d
1449 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1450 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1451 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1452 bw!
1453endfunc
1454
1455" Test for some fields in the complete items used by complete()
1456func Test_complete_items()
1457 func CompleteItems(idx)
1458 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1459 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1460 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1461 \ [#{user_data: 'u9'}],
1462 \ [#{word: "", user_data: 'u10'}],
1463 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1464 call complete(col('.'), items[a:idx])
1465 return ''
1466 endfunc
1467 new
1468 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1469 call assert_equal('u2', v:completed_item.user_data)
1470 call assert_equal('one', getline(1))
1471 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1472 call assert_equal('u3', v:completed_item.user_data)
1473 call assert_equal('one', getline(2))
1474 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1475 call assert_equal('', getline(3))
1476 set completeopt=menu,noinsert
1477 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1478 call assert_equal('oNE', getline(4))
1479 call assert_equal('u8', v:completed_item.user_data)
1480 set completeopt&
1481 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1482 call assert_equal('', getline(5))
1483 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1484 call assert_equal('', getline(6))
1485 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1486 call assert_equal('', getline(7))
1487 call assert_equal('u11', v:completed_item.user_data)
1488 " pass invalid argument to complete()
1489 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1490 call assert_fails('exe cmd', 'E730:')
1491 bw!
1492 delfunc CompleteItems
1493endfunc
1494
1495" Test for the "refresh" item in the dict returned by an insert completion
1496" function
1497func Test_complete_item_refresh_always()
1498 let g:CallCount = 0
1499 func! Tcomplete(findstart, base)
1500 if a:findstart
1501 " locate the start of the word
1502 let line = getline('.')
1503 let start = col('.') - 1
1504 while start > 0 && line[start - 1] =~ '\a'
1505 let start -= 1
1506 endwhile
1507 return start
1508 else
1509 let g:CallCount += 1
1510 let res = ["update1", "update12", "update123"]
1511 return #{words: res, refresh: 'always'}
1512 endif
1513 endfunc
1514 new
1515 set completeopt=menu,longest
1516 set completefunc=Tcomplete
1517 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1518 call assert_equal('up', getline(1))
Luca Saccarola959ef612024-12-01 16:25:53 +01001519 call assert_equal(6, g:CallCount)
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001520 set completeopt&
1521 set completefunc&
1522 bw!
1523 delfunc Tcomplete
1524endfunc
1525
1526" Test for completing from a thesaurus file without read permission
1527func Test_complete_unreadable_thesaurus_file()
1528 CheckUnix
1529 CheckNotRoot
1530
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001531 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001532 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001533 new
1534 set complete=sXfile
1535 exe "normal! ia\<C-P>"
1536 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001537
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001538 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001539 set complete&
1540endfunc
1541
Bram Moolenaarcc233582020-12-12 13:32:07 +01001542" Test to ensure 'Scanning...' messages are not recorded in messages history
1543func Test_z1_complete_no_history()
1544 new
1545 messages clear
1546 let currmess = execute('messages')
1547 setlocal dictionary=README.txt
1548 exe "normal owh\<C-X>\<C-K>"
1549 exe "normal owh\<C-N>"
1550 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001551 bwipe!
1552endfunc
1553
1554" A mapping is not used for the key after CTRL-X.
1555func Test_no_mapping_for_ctrl_x_key()
1556 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001557 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001558 setlocal dictionary=README.txt
1559 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1560 call assert_equal('example ', getline(1))
1561 call assert_false(exists('was_mapped'))
1562 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001563endfunc
1564
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001565" Test for different ways of setting the 'completefunc' option
1566func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001567 func CompleteFunc1(callnr, findstart, base)
1568 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1569 return a:findstart ? 0 : []
1570 endfunc
1571 func CompleteFunc2(findstart, base)
1572 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001573 return a:findstart ? 0 : []
1574 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001575
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001576 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001577 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001578 LET &completefunc = 'g:CompleteFunc2'
1579 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001580 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001581 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001582 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001583 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001584 bw!
1585
1586 #" Test for using a function()
1587 set completefunc=function('g:CompleteFunc1',\ [10])
1588 new
1589 call setline(1, 'one')
1590 LET g:CompleteFunc1Args = []
1591 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1592 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001593 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001594
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001595 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001596 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001597 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001598 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001599 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001600 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001601 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001602 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001603 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001604
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001605 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001606 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001607 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001608 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001609 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001610 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001611 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001612 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001613 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001614
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001615 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001616 set completefunc=funcref('g:CompleteFunc1',\ [13])
1617 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001618 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001619 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001620 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001621 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001623
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001624 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001625 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001626 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001629 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001630 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001631 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001632 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001633
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001634 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001635 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001636 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001637 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001638 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001639 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001640 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001641 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001642 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001643
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001644 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001645 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001646 LET optval = substitute(optval, ' ', '\\ ', 'g')
1647 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001650 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001652 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001654
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001656 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001657 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001658 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001659 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001660 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001661 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001662 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001663
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001664 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001665 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001666 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001667 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001668 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001669 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001670 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001671 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001672
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001673 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001674 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001675 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001676 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001677 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001678 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001679 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001680 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001681 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001682
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001683 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001684 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001685 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001686 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001687 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001688 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001689 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001690 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001691 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001692
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001693 #" Test for using a lambda function with incorrect return value
1694 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1695 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001696 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001697 call setline(1, 'eight')
1698 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1699 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001700
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001701 #" Test for clearing the 'completefunc' option
1702 set completefunc=''
1703 set completefunc&
1704 call assert_fails("set completefunc=function('abc')", "E700:")
1705 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001706
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001707 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001708 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001709 call setline(1, 'five')
1710 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1711 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001712 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001713 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001714 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001715 bw!
1716 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001717 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001718
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001719 " Test for using a script-local function name
1720 func s:CompleteFunc3(findstart, base)
1721 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1722 return a:findstart ? 0 : []
1723 endfunc
1724 set completefunc=s:CompleteFunc3
1725 new
1726 call setline(1, 'script1')
1727 let g:CompleteFunc3Args = []
1728 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1729 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1730 bw!
1731
1732 let &completefunc = 's:CompleteFunc3'
1733 new
1734 call setline(1, 'script2')
1735 let g:CompleteFunc3Args = []
1736 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1737 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1738 bw!
1739 delfunc s:CompleteFunc3
1740
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001741 " In Vim9 script s: can be omitted
1742 let lines =<< trim END
1743 vim9script
1744 var CompleteFunc4Args = []
1745 def CompleteFunc4(findstart: bool, base: string): any
1746 add(CompleteFunc4Args, [findstart, base])
1747 return findstart ? 0 : []
1748 enddef
1749 set completefunc=CompleteFunc4
1750 new
1751 setline(1, 'script1')
1752 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1753 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1754 bw!
1755 END
1756 call v9.CheckScriptSuccess(lines)
1757
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001758 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001759 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001760 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1761
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001762 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001763 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001764 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001765 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001766 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001767 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001768
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001769 " set 'completefunc' to a partial with dict. This used to cause a crash.
1770 func SetCompleteFunc()
1771 let params = {'complete': function('g:DictCompleteFunc')}
1772 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001773 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001774 func g:DictCompleteFunc(_) dict
1775 endfunc
1776 call SetCompleteFunc()
1777 new
1778 call SetCompleteFunc()
1779 bw
1780 call test_garbagecollect_now()
1781 new
1782 set completefunc=
1783 wincmd w
1784 set completefunc=
1785 %bw!
1786 delfunc g:DictCompleteFunc
1787 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001788
1789 " Vim9 tests
1790 let lines =<< trim END
1791 vim9script
1792
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001793 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1794 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001795 return findstart ? 0 : []
1796 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001797
1798 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001799 set completefunc=function('Vim9CompleteFunc',\ [60])
1800 new | only
1801 setline(1, 'one')
1802 g:Vim9completeFuncArgs = []
1803 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1804 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1805 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001806
1807 # Test for using a global function name
1808 &completefunc = g:CompleteFunc2
1809 new | only
1810 setline(1, 'two')
1811 g:CompleteFunc2Args = []
1812 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1813 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1814 bw!
1815
1816 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001817 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001818 add(g:LocalCompleteFuncArgs, [findstart, base])
1819 return findstart ? 0 : []
1820 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001821 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001822 new | only
1823 setline(1, 'three')
1824 g:LocalCompleteFuncArgs = []
1825 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1826 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1827 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001828 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001829 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001830
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001831 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001832 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001833 delfunc CompleteFunc1
1834 delfunc CompleteFunc2
1835 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001836 %bw!
1837endfunc
1838
1839" Test for different ways of setting the 'omnifunc' option
1840func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001841 func OmniFunc1(callnr, findstart, base)
1842 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1843 return a:findstart ? 0 : []
1844 endfunc
1845 func OmniFunc2(findstart, base)
1846 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001847 return a:findstart ? 0 : []
1848 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001849
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001850 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001851 #" Test for using a function name
1852 LET &omnifunc = 'g:OmniFunc2'
1853 new
1854 call setline(1, 'zero')
1855 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001856 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001857 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1858 bw!
1859
1860 #" Test for using a function()
1861 set omnifunc=function('g:OmniFunc1',\ [10])
1862 new
1863 call setline(1, 'one')
1864 LET g:OmniFunc1Args = []
1865 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1866 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001867 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001868
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001869 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001870 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001871 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001872 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001873 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001874 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001875 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001876 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001877 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001878
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001879 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001880 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001881 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001882 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001883 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001884 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001885 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001886 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001887 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001888
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001889 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001890 set omnifunc=funcref('g:OmniFunc1',\ [13])
1891 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001892 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001893 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001894 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001895 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001897
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001898 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001899 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001900 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001901 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001903 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001904 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001905 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001906 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001907
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001908 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001909 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001910 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001911 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001912 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001913 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001914 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001915 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001916 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001917
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001918 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001919 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001920 LET optval = substitute(optval, ' ', '\\ ', 'g')
1921 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001922 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001924 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001925 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001926 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001927 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001928
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001929 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001930 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001931 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001932 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001933 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001934 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001935 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001936 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001937
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001938 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001939 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001940 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001941 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001942 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001943 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001944 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001945 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001946
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001947 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001948 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001949 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001950 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001951 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001952 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001953 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001954 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001955 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001956
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001957 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001958 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001959 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001960 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001961 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001962 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001963 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001964 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001965 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001966
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001967 #" Test for using a lambda function with incorrect return value
1968 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1969 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001970 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001971 call setline(1, 'eight')
1972 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1973 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001974
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975 #" Test for clearing the 'omnifunc' option
1976 set omnifunc=''
1977 set omnifunc&
1978 call assert_fails("set omnifunc=function('abc')", "E700:")
1979 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001980
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001981 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001982 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001983 call setline(1, 'nine')
1984 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
1985 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001986 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001987 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001988 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001989 bw!
1990 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001991 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001992
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001993 " Test for using a script-local function name
1994 func s:OmniFunc3(findstart, base)
1995 call add(g:OmniFunc3Args, [a:findstart, a:base])
1996 return a:findstart ? 0 : []
1997 endfunc
1998 set omnifunc=s:OmniFunc3
1999 new
2000 call setline(1, 'script1')
2001 let g:OmniFunc3Args = []
2002 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2003 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
2004 bw!
2005
2006 let &omnifunc = 's:OmniFunc3'
2007 new
2008 call setline(1, 'script2')
2009 let g:OmniFunc3Args = []
2010 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2011 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
2012 bw!
2013 delfunc s:OmniFunc3
2014
2015 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002016 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002017 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2018
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002019 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002020 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002021 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002022 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002023 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002024 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002025
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002026 " set 'omnifunc' to a partial with dict. This used to cause a crash.
2027 func SetOmniFunc()
2028 let params = {'omni': function('g:DictOmniFunc')}
2029 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002030 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002031 func g:DictOmniFunc(_) dict
2032 endfunc
2033 call SetOmniFunc()
2034 new
2035 call SetOmniFunc()
2036 bw
2037 call test_garbagecollect_now()
2038 new
2039 set omnifunc=
2040 wincmd w
2041 set omnifunc=
2042 %bw!
2043 delfunc g:DictOmniFunc
2044 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002045
2046 " Vim9 tests
2047 let lines =<< trim END
2048 vim9script
2049
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002050 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
2051 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002052 return findstart ? 0 : []
2053 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002054
2055 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002056 set omnifunc=function('Vim9omniFunc',\ [60])
2057 new | only
2058 setline(1, 'one')
2059 g:Vim9omniFunc_Args = []
2060 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2061 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
2062 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002063
2064 # Test for using a global function name
2065 &omnifunc = g:OmniFunc2
2066 new | only
2067 setline(1, 'two')
2068 g:OmniFunc2Args = []
2069 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2070 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
2071 bw!
2072
2073 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002074 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002075 add(g:LocalOmniFuncArgs, [findstart, base])
2076 return findstart ? 0 : []
2077 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002078 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002079 new | only
2080 setline(1, 'three')
2081 g:LocalOmniFuncArgs = []
2082 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2083 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
2084 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002085 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002086 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00002087
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002088 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002089 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002090 delfunc OmniFunc1
2091 delfunc OmniFunc2
2092 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002093 %bw!
2094endfunc
2095
2096" Test for different ways of setting the 'thesaurusfunc' option
2097func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002098 func TsrFunc1(callnr, findstart, base)
2099 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002100 return a:findstart ? 0 : []
2101 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002102 func TsrFunc2(findstart, base)
2103 call add(g:TsrFunc2Args, [a:findstart, a:base])
2104 return a:findstart ? 0 : ['sunday']
2105 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002106
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002107 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002108 #" Test for using a function name
2109 LET &thesaurusfunc = 'g:TsrFunc2'
2110 new
2111 call setline(1, 'zero')
2112 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002113 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002114 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
2115 bw!
2116
2117 #" Test for using a function()
2118 set thesaurusfunc=function('g:TsrFunc1',\ [10])
2119 new
2120 call setline(1, 'one')
2121 LET g:TsrFunc1Args = []
2122 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2123 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002124 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002125
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002126 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002127 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002128 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002129 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002130 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002131 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002132 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002133 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002134 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002135
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002136 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002137 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002138 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002139 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002140 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002141 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002142 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002143 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002144 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002145
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002146 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002147 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
2148 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002149 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002150 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002151 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002152 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002153 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002154
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002155 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002156 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002157 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002158 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002159 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002160 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002161 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002162 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002163 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002164
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002165 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002166 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002167 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002168 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002169 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002170 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002171 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002172 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002173 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002174
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002175 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00002176 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002177 LET optval = substitute(optval, ' ', '\\ ', 'g')
2178 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002179 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002180 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002181 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002182 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002183 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002184 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002185
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002186 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00002187 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002188 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002189 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002190 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002191 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002192 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002193 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002194
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002195 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002196 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002197 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002198 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002199 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002200 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002201 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002202 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002203
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002204 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00002205 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002206 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002207 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002208 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002209 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002210 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002211 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002212 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002213
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002214 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002215 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002216 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002217 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002218 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002219 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002220 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002221 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002222 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002223
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002224 #" Test for using a lambda function with incorrect return value
2225 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2226 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002227 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002228 call setline(1, 'eight')
2229 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2230 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002231
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002232 #" Test for clearing the 'thesaurusfunc' option
2233 set thesaurusfunc=''
2234 set thesaurusfunc&
2235 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
2236 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002237
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002238 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002239 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002240 call setline(1, 'ten')
2241 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
2242 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002243 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002244 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002245 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002246 bw!
2247
2248 #" Use a buffer-local value and a global value
2249 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002250 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002251 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002252 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002253 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2254 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002255 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002256 new
2257 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002258 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002259 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2260 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002261 call assert_equal([], g:TsrFunc1Args)
2262 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002263 wincmd w
2264 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002265 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002266 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2267 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002268 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002269 :%bw!
2270 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002271 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002272
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002273 " Test for using a script-local function name
2274 func s:TsrFunc3(findstart, base)
2275 call add(g:TsrFunc3Args, [a:findstart, a:base])
2276 return a:findstart ? 0 : []
2277 endfunc
zeertzjq6eda2692024-11-03 09:23:33 +01002278
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002279 set tsrfu=s:TsrFunc3
2280 new
2281 call setline(1, 'script1')
2282 let g:TsrFunc3Args = []
2283 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2284 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2285 bw!
2286
2287 let &tsrfu = 's:TsrFunc3'
2288 new
2289 call setline(1, 'script2')
2290 let g:TsrFunc3Args = []
2291 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2292 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2293 bw!
zeertzjq6eda2692024-11-03 09:23:33 +01002294
2295 new | only
2296 set thesaurusfunc=
2297 setlocal thesaurusfunc=NoSuchFunc
2298 setglobal thesaurusfunc=s:TsrFunc3
2299 call assert_equal('NoSuchFunc', &thesaurusfunc)
2300 call assert_equal('NoSuchFunc', &l:thesaurusfunc)
2301 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2302 new | only
2303 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2304 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2305 call assert_equal('', &l:thesaurusfunc)
2306 call setline(1, 'script1')
2307 let g:TsrFunc3Args = []
2308 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2309 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2310 bw!
2311
2312 new | only
2313 set thesaurusfunc=
2314 setlocal thesaurusfunc=NoSuchFunc
2315 set thesaurusfunc=s:TsrFunc3
2316 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2317 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2318 call assert_equal('', &l:thesaurusfunc)
2319 call setline(1, 'script1')
2320 let g:TsrFunc3Args = []
2321 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2322 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2323 setlocal bufhidden=wipe
2324 new | only!
2325 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2326 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2327 call assert_equal('', &l:thesaurusfunc)
2328 call setline(1, 'script1')
2329 let g:TsrFunc3Args = []
2330 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2331 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2332 bw!
2333
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002334 delfunc s:TsrFunc3
2335
2336 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002337 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002338 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2339
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002340 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002341 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002342 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002343 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002344 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002345 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002346 bw!
2347
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002348 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2349 func SetTsrFunc()
2350 let params = {'thesaurus': function('g:DictTsrFunc')}
2351 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002352 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002353 func g:DictTsrFunc(_) dict
2354 endfunc
2355 call SetTsrFunc()
2356 new
2357 call SetTsrFunc()
2358 bw
2359 call test_garbagecollect_now()
2360 new
2361 set thesaurusfunc=
2362 wincmd w
2363 %bw!
2364 delfunc SetTsrFunc
2365
2366 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2367 " cause a crash.
2368 func SetLocalTsrFunc()
2369 let params = {'thesaurus': function('g:DictTsrFunc')}
2370 let &l:thesaurusfunc = params.thesaurus
2371 endfunc
2372 call SetLocalTsrFunc()
2373 call test_garbagecollect_now()
2374 call SetLocalTsrFunc()
2375 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002376 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002377 delfunc g:DictTsrFunc
2378 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002379
2380 " Vim9 tests
2381 let lines =<< trim END
2382 vim9script
2383
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002384 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2385 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002386 return findstart ? 0 : []
2387 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002388
2389 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002390 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2391 new | only
2392 setline(1, 'one')
2393 g:Vim9tsrFunc_Args = []
2394 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2395 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2396 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002397
2398 # Test for using a global function name
2399 &thesaurusfunc = g:TsrFunc2
2400 new | only
2401 setline(1, 'two')
2402 g:TsrFunc2Args = []
2403 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2404 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2405 bw!
2406
2407 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002408 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002409 add(g:LocalTsrFuncArgs, [findstart, base])
2410 return findstart ? 0 : []
2411 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002412 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002413 new | only
2414 setline(1, 'three')
2415 g:LocalTsrFuncArgs = []
2416 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2417 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2418 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002419 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002420 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002421
2422 " cleanup
2423 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002424 delfunc TsrFunc1
2425 delfunc TsrFunc2
2426 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002427 %bw!
2428endfunc
2429
Christian Brabandtac72c212022-04-07 21:00:53 +01002430func FooBarComplete(findstart, base)
2431 if a:findstart
2432 return col('.') - 1
2433 else
2434 return ["Foo", "Bar", "}"]
2435 endif
2436endfunc
2437
2438func Test_complete_smartindent()
2439 new
2440 setlocal smartindent completefunc=FooBarComplete
2441
2442 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2443 let result = getline(1,'$')
2444 call assert_equal(['', '{','}',''], result)
2445 bw!
2446 delfunction! FooBarComplete
2447endfunc
2448
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002449func Test_complete_overrun()
2450 " this was going past the end of the copied text
2451 new
2452 sil norm si”0s0 
2453 bwipe!
2454endfunc
2455
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002456func Test_infercase_very_long_line()
2457 " this was truncating the line when inferring case
2458 new
2459 let longLine = "blah "->repeat(300)
2460 let verylongLine = "blah "->repeat(400)
2461 call setline(1, verylongLine)
2462 call setline(2, longLine)
2463 set ic infercase
2464 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2465 call assert_equal(longLine, getline(3))
2466
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002467 " check that the too long text is NUL terminated
2468 %del
2469 norm o
2470 norm 1987ax
2471 exec "norm ox\<C-X>\<C-L>"
2472 call assert_equal(repeat('x', 1987), getline(3))
2473
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002474 bwipe!
2475 set noic noinfercase
2476endfunc
2477
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002478func Test_ins_complete_add()
2479 " this was reading past the end of allocated memory
2480 new
2481 norm o
2482 norm 7o€€
2483 sil! norm o
2484
2485 bwipe!
2486endfunc
2487
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002488func Test_ins_complete_end_of_line()
2489 " this was reading past the end of the line
Bram Moolenaar94722c52023-01-28 19:19:03 +00002490 new
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002491 norm 8o€ý 
2492 sil! norm o
2493
2494 bwipe!
2495endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002496
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002497func s:Tagfunc(t,f,o)
2498 bwipe!
2499 return []
2500endfunc
2501
2502" This was using freed memory, since 'complete' was in a wiped out buffer.
2503" Also using a window that was closed.
2504func Test_tagfunc_wipes_out_buffer()
2505 new
2506 set complete=.,t,w,b,u,i
2507 se tagfunc=s:Tagfunc
2508 sil norm i
2509
2510 bwipe!
2511endfunc
2512
Bram Moolenaar6ac2e432023-03-31 19:32:29 +01002513func Test_ins_complete_popup_position()
2514 CheckScreendump
2515
2516 let lines =<< trim END
2517 vim9script
2518 set nowrap
2519 setline(1, ['one', 'two', 'this is line ', 'four'])
2520 prop_type_add('test', {highlight: 'Error'})
2521 prop_add(3, 0, {
2522 text_align: 'above',
2523 text: 'The quick brown fox jumps over the lazy dog',
2524 type: 'test'
2525 })
2526 END
2527 call writefile(lines, 'XinsPopup', 'D')
2528 let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
2529
2530 call term_sendkeys(buf, "3GA\<C-N>")
2531 call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
2532
2533 call StopVimInTerminal(buf)
2534endfunc
2535
LemonBoy69fb5af2023-10-11 21:55:56 +02002536func GetCompleteInfo()
2537 let g:compl_info = complete_info()
2538 return ''
2539endfunc
2540
nwounkn2e3cd522023-10-17 11:05:38 +02002541func Test_completion_restart()
2542 new
2543 set complete=. completeopt=menuone backspace=2
2544 call setline(1, 'workhorse workhorse')
2545 exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
2546 call assert_equal(1, len(g:compl_info['items']))
2547 call assert_equal('workhorse', g:compl_info['items'][0]['word'])
2548 set complete& completeopt& backspace&
2549 bwipe!
2550endfunc
2551
LemonBoy69fb5af2023-10-11 21:55:56 +02002552func Test_complete_info_index()
2553 new
2554 call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
2555 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2556
2557 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2558
2559 set completeopt=menu,preview
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002560 " Search forward
LemonBoy69fb5af2023-10-11 21:55:56 +02002561 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2562 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002563 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002564 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2565 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002566 call assert_equal(6 , len(g:compl_info['items']))
2567 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2568 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2569 call assert_equal(6 , len(g:compl_info['items']))
2570 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2571 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2572 call assert_equal(6 , len(g:compl_info['items']))
2573 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2574 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2575 call assert_equal(6 , len(g:compl_info['items']))
2576 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2577 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2578 call assert_equal(6 , len(g:compl_info['items']))
2579 " Search forward: unselected item
2580 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2581 call assert_equal(6 , len(g:compl_info['items']))
2582 call assert_equal(-1 , g:compl_info['selected'])
LemonBoy69fb5af2023-10-11 21:55:56 +02002583
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002584 " Search backward
LemonBoy69fb5af2023-10-11 21:55:56 +02002585 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2586 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002587 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002588 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2589 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002590 call assert_equal(6 , len(g:compl_info['items']))
2591 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2592 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2593 call assert_equal(6 , len(g:compl_info['items']))
2594 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2595 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2596 call assert_equal(6 , len(g:compl_info['items']))
2597 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2598 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2599 call assert_equal(6 , len(g:compl_info['items']))
2600 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2601 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2602 call assert_equal(6 , len(g:compl_info['items']))
2603 " search backwards: unselected item
2604 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2605 call assert_equal(6 , len(g:compl_info['items']))
2606 call assert_equal(-1 , g:compl_info['selected'])
2607
2608 " switch direction: forwards, then backwards
2609 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2610 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2611 call assert_equal(6 , len(g:compl_info['items']))
2612 " switch direction: forwards, then backwards, then forwards again
2613 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2614 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2615 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2616 call assert_equal(6 , len(g:compl_info['items']))
2617
2618 " switch direction: backwards, then forwards
2619 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2620 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2621 call assert_equal(6 , len(g:compl_info['items']))
2622 " switch direction: backwards, then forwards, then backwards again
2623 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2624 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2625 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002626
2627 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2628 set completeopt+=noselect
2629 " Search forward.
2630 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2631 call assert_equal(-1, g:compl_info['selected'])
2632
2633 " Search backward.
2634 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2635 call assert_equal(-1, g:compl_info['selected'])
2636
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002637 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
Girish Palya8950bf72024-03-20 20:07:29 +01002638 call assert_equal(5, g:compl_info['selected'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002639 call assert_equal(6 , len(g:compl_info['items']))
2640 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2641 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
Christian Brabandt87ca5e82023-10-27 19:47:29 +02002642 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002643 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandt57f9ce12023-11-04 09:58:14 +01002644 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2645 call assert_equal(-1, g:compl_info['selected'])
2646 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002647
LemonBoy69fb5af2023-10-11 21:55:56 +02002648 set completeopt&
2649 bwipe!
2650endfunc
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002651
Christian Brabandtfef66302024-01-29 21:46:58 +01002652func Test_complete_changed_complete_info()
2653 CheckRunVimInTerminal
2654 " this used to crash vim, see #13929
2655 let lines =<< trim END
2656 set completeopt=menuone
2657 autocmd CompleteChanged * call complete_info(['items'])
2658 call feedkeys("iii\<cr>\<c-p>")
2659 END
2660 call writefile(lines, 'Xsegfault', 'D')
2661 let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
2662 call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
2663 call StopVimInTerminal(buf)
2664endfunc
2665
zeertzjq0a419e02024-04-02 19:01:14 +02002666func Test_completefunc_first_call_complete_add()
2667 new
2668
2669 func Complete(findstart, base) abort
2670 if a:findstart
2671 let col = col('.')
2672 call complete_add('#')
2673 return col - 1
2674 else
2675 return []
2676 endif
2677 endfunc
2678
2679 set completeopt=longest completefunc=Complete
2680 " This used to cause heap-buffer-overflow
2681 call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:')
2682
2683 delfunc Complete
2684 set completeopt& completefunc&
2685 bwipe!
2686endfunc
2687
glepnira218cc62024-06-03 19:32:39 +02002688func Test_complete_fuzzy_match()
2689 func OnPumChange()
2690 let g:item = get(v:event, 'completed_item', {})
2691 let g:word = get(g:item, 'word', v:null)
2692 endfunction
2693
2694 augroup AAAAA_Group
2695 au!
2696 autocmd CompleteChanged * :call OnPumChange()
2697 augroup END
2698
2699 func Omni_test(findstart, base)
2700 if a:findstart
2701 return col(".")
2702 endif
glepnirf400a0c2025-01-23 19:55:14 +01002703 return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}, #{word: "你好吗"}, #{word: "我好"}]
glepnira218cc62024-06-03 19:32:39 +02002704 endfunc
zeertzjq551d8c32024-06-05 19:53:32 +02002705
glepnira218cc62024-06-03 19:32:39 +02002706 new
2707 set omnifunc=Omni_test
2708 set completeopt+=noinsert,fuzzy
2709 call feedkeys("Gi\<C-x>\<C-o>", 'tx')
2710 call assert_equal('foo', g:word)
2711 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2712 call assert_equal('fooBaz', g:word)
2713 call feedkeys("S\<C-x>\<C-o>fa", 'tx')
2714 call assert_equal('foobar', g:word)
2715 " select next
2716 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2717 call assert_equal('foobar', g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002718 " can cyclically select next
glepnira218cc62024-06-03 19:32:39 +02002719 call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx')
2720 call assert_equal(v:null, g:word)
2721 " select prev
2722 call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx')
2723 call assert_equal(v:null, g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002724 " can cyclically select prev
glepnira218cc62024-06-03 19:32:39 +02002725 call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx')
2726 call assert_equal('fooBaz', g:word)
2727
glepnirf94c9c42024-06-14 21:11:56 +02002728 func Comp()
2729 call complete(col('.'), ["fooBaz", "foobar", "foobala"])
2730 return ''
2731 endfunc
2732 call feedkeys("i\<C-R>=Comp()\<CR>", 'tx')
2733 call assert_equal('fooBaz', g:word)
2734
glepnira218cc62024-06-03 19:32:39 +02002735 " respect noselect
2736 set completeopt+=noselect
2737 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2738 call assert_equal(v:null, g:word)
2739 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2740 call assert_equal('fooBaz', g:word)
2741
zeertzjqacc87462024-06-16 08:44:05 +02002742 " avoid breaking default completion behavior
glepniraced8c22024-06-15 15:13:05 +02002743 set completeopt=fuzzy,menu
2744 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002745 " Use "!" flag of feedkeys() so that ex_normal_busy is not set and
2746 " ins_compl_check_keys() is not skipped.
2747 " Add a "0" after the <Esc> to avoid waiting for an escape sequence.
2748 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002749 call assert_equal('hello help hero hello', getline('.'))
2750 set completeopt+=noinsert
2751 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002752 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002753 call assert_equal('hello help hero h', getline('.'))
2754
glepnir8159fb12024-07-17 20:32:54 +02002755 set completeopt-=noinsert
2756 call setline(1, ['xyz yxz x'])
2757 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2758 call assert_equal('xyz yxz xyz', getline('.'))
2759 " can fuzzy get yxz when use Ctrl-N twice
2760 call setline(1, ['xyz yxz x'])
2761 call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2762 call assert_equal('xyz yxz yxz', getline('.'))
2763
2764 call setline(1, ['你好 你'])
2765 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2766 call assert_equal('你好 你好', getline('.'))
2767 call setline(1, ['你的 我的 的'])
2768 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2769 call assert_equal('你的 我的 你的', getline('.'))
2770 " can fuzzy get multiple-byte word when use Ctrl-N twice
2771 call setline(1, ['你的 我的 的'])
2772 call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2773 call assert_equal('你的 我的 我的', getline('.'))
2774
2775 " respect wrapscan
2776 set nowrapscan
2777 call setline(1, ["xyz", "yxz", ""])
2778 call cursor(3, 1)
2779 call feedkeys("Sy\<C-X>\<C-N>\<Esc>0", 'tx!')
2780 call assert_equal('y', getline('.'))
2781 set wrapscan
2782 call feedkeys("Sy\<C-X>\<C-N>\<Esc>0", 'tx!')
2783 call assert_equal('xyz', getline('.'))
2784
2785 " fuzzy on file
2786 call writefile([''], 'fobar', 'D')
2787 call writefile([''], 'foobar', 'D')
2788 call setline(1, ['fob'])
2789 call cursor(1, 1)
2790 call feedkeys("A\<C-X>\<C-f>\<Esc>0", 'tx!')
2791 call assert_equal('fobar', getline('.'))
2792 call feedkeys("Sfob\<C-X>\<C-f>\<C-N>\<Esc>0", 'tx!')
2793 call assert_equal('foobar', getline('.'))
glepnir0be03e12024-07-19 16:45:05 +02002794 call feedkeys("S../\<C-X>\<C-f>\<Esc>0", 'tx!')
2795 call assert_match('../*', getline('.'))
2796 call feedkeys("S../td\<C-X>\<C-f>\<Esc>0", 'tx!')
2797 call assert_match('../testdir', getline('.'))
glepnir8159fb12024-07-17 20:32:54 +02002798
2799 " can get completion from other buffer
2800 set completeopt=fuzzy,menu,menuone
2801 vnew
2802 call setline(1, ["completeness,", "compatibility", "Composite", "Omnipotent"])
2803 wincmd p
2804 call feedkeys("Somp\<C-N>\<Esc>0", 'tx!')
2805 call assert_equal('completeness', getline('.'))
2806 call feedkeys("Somp\<C-N>\<C-N>\<Esc>0", 'tx!')
2807 call assert_equal('compatibility', getline('.'))
2808 call feedkeys("Somp\<C-P>\<Esc>0", 'tx!')
2809 call assert_equal('Omnipotent', getline('.'))
2810 call feedkeys("Somp\<C-P>\<C-P>\<Esc>0", 'tx!')
2811 call assert_equal('Composite', getline('.'))
glepnir0be03e12024-07-19 16:45:05 +02002812 call feedkeys("S omp\<C-N>\<Esc>0", 'tx!')
2813 call assert_equal(' completeness', getline('.'))
glepnir8159fb12024-07-17 20:32:54 +02002814
2815 " fuzzy on whole line completion
2816 call setline(1, ["world is on fire", "no one can save me but you", 'user can execute', ''])
2817 call cursor(4, 1)
2818 call feedkeys("Swio\<C-X>\<C-L>\<Esc>0", 'tx!')
2819 call assert_equal('world is on fire', getline('.'))
2820 call feedkeys("Su\<C-X>\<C-L>\<C-P>\<Esc>0", 'tx!')
2821 call assert_equal('no one can save me but you', getline('.'))
2822
glepnir7cfe6932024-09-15 20:06:28 +02002823 " issue #15412
2824 call setline(1, ['alpha bravio charlie'])
2825 call feedkeys("Salpha\<C-X>\<C-N>\<Esc>0", 'tx!')
2826 call assert_equal('alpha bravio', getline('.'))
2827 call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!')
2828 call assert_equal('alpha', getline('.'))
2829 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2830 call assert_equal('alpha bravio', getline('.'))
2831 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2832 call assert_equal('alpha bravio charlie', getline('.'))
2833
2834 set complete-=i
2835 call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!')
2836 call assert_equal('alpha', getline('.'))
2837 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2838 call assert_equal('alpha bravio', getline('.'))
2839 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2840 call assert_equal('alpha bravio charlie', getline('.'))
2841
2842 call setline(1, ['alpha bravio charlie', 'alpha another'])
2843 call feedkeys("Salpha\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2844 call assert_equal('alpha another', getline('.'))
2845 call setline(1, ['你好 我好', '你好 他好'])
2846 call feedkeys("S你好\<C-X>\<C-N>\<Esc>0", 'tx!')
2847 call assert_equal('你好 我好', getline('.'))
2848 call feedkeys("S你好\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2849 call assert_equal('你好 他好', getline('.'))
2850
glepnir753794b2024-08-20 19:58:44 +02002851 " issue #15526
2852 set completeopt=fuzzy,menuone,menu,noselect
2853 call setline(1, ['Text', 'ToText', ''])
glepnir7cfe6932024-09-15 20:06:28 +02002854 call cursor(3, 1)
glepnir753794b2024-08-20 19:58:44 +02002855 call feedkeys("STe\<C-X>\<C-N>x\<CR>\<Esc>0", 'tx!')
2856 call assert_equal('Tex', getline('.'))
2857
glepnirf400a0c2025-01-23 19:55:14 +01002858 " test case for nosort option
2859 set cot=menuone,menu,noinsert,fuzzy,nosort
zeertzjqd65aa1b2025-01-25 15:29:03 +01002860 " "fooBaz" should have a higher score when the leader is "fb".
2861 " With "nosort", "foobar" should still be shown first in the popup menu.
glepnirf400a0c2025-01-23 19:55:14 +01002862 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2863 call assert_equal('foobar', g:word)
2864 call feedkeys("S\<C-x>\<C-o>好", 'tx')
2865 call assert_equal("你好吗", g:word)
2866
2867 set cot+=noselect
2868 call feedkeys("S\<C-x>\<C-o>好", 'tx')
2869 call assert_equal(v:null, g:word)
2870 call feedkeys("S\<C-x>\<C-o>好\<C-N>", 'tx')
2871 call assert_equal('你好吗', g:word)
2872
zeertzjqd65aa1b2025-01-25 15:29:03 +01002873 " "nosort" shouldn't enable fuzzy filtering when "fuzzy" isn't present.
2874 set cot=menuone,noinsert,nosort
2875 call feedkeys("S\<C-x>\<C-o>fooB\<C-Y>", 'tx')
2876 call assert_equal('fooBaz', getline('.'))
2877
glepnira218cc62024-06-03 19:32:39 +02002878 " clean up
2879 set omnifunc=
2880 bw!
glepnir8159fb12024-07-17 20:32:54 +02002881 bw!
glepnira218cc62024-06-03 19:32:39 +02002882 set complete& completeopt&
2883 autocmd! AAAAA_Group
2884 augroup! AAAAA_Group
2885 delfunc OnPumChange
2886 delfunc Omni_test
glepnirf94c9c42024-06-14 21:11:56 +02002887 delfunc Comp
zeertzjq551d8c32024-06-05 19:53:32 +02002888 unlet g:item
2889 unlet g:word
glepnira218cc62024-06-03 19:32:39 +02002890endfunc
2891
glepnirb9de1a02024-08-02 19:14:38 +02002892func Test_complete_fuzzy_with_completeslash()
2893 CheckMSWindows
2894
2895 call writefile([''], 'fobar', 'D')
2896 let orig_shellslash = &shellslash
2897 set cpt&
2898 new
2899 set completeopt+=fuzzy
2900 set noshellslash
2901
2902 " Test with completeslash unset
2903 set completeslash=
2904 call setline(1, ['.\fob'])
2905 call feedkeys("A\<C-X>\<C-F>\<Esc>0", 'tx!')
2906 call assert_equal('.\fobar', getline('.'))
2907
2908 " Test with completeslash=backslash
2909 set completeslash=backslash
2910 call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!')
2911 call assert_equal('.\fobar', getline('.'))
2912
2913 " Test with completeslash=slash
2914 set completeslash=slash
2915 call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!')
2916 call assert_equal('./fobar', getline('.'))
2917
2918 " Reset and clean up
2919 let &shellslash = orig_shellslash
2920 set completeslash=
2921 %bw!
2922endfunc
2923
zeertzjq8e567472024-06-14 20:04:42 +02002924" Check that tie breaking is stable for completeopt+=fuzzy (which should
2925" behave the same on different platforms).
2926func Test_complete_fuzzy_match_tie()
2927 new
2928 set completeopt+=fuzzy,noselect
2929 call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', ''])
2930
2931 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx')
2932 call assert_equal('aaabbccc', getline('.'))
2933 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx')
2934 call assert_equal('aaabbCCC', getline('.'))
2935 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
2936 call assert_equal('aaabbcccc', getline('.'))
2937 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
2938 call assert_equal('aaabbCCCC', getline('.'))
2939
2940 bwipe!
2941 set completeopt&
2942endfunc
2943
Christian Brabandt13032a42024-07-28 21:16:48 +02002944func Test_complete_backwards_default()
2945 new
2946 call append(1, ['foobar', 'foobaz'])
2947 new
2948 call feedkeys("i\<c-p>", 'tx')
2949 call assert_equal('foobaz', getline('.'))
2950 bw!
2951 bw!
2952endfunc
2953
glepnird4088ed2024-12-31 10:55:22 +01002954func Test_complete_info_matches()
2955 let g:what = ['matches']
2956 func ShownInfo()
2957 let g:compl_info = complete_info(g:what)
2958 return ''
2959 endfunc
2960 set completeopt+=noinsert
2961
2962 new
2963 call setline(1, ['aaa', 'aab', 'aba', 'abb'])
2964 inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
2965
2966 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
2967 call assert_equal([
2968 \ {'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2969 \ {'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2970 \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2971 \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2972 \], g:compl_info['matches'])
2973
2974 call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
2975 call assert_equal([
2976 \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2977 \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2978 \], g:compl_info['matches'])
2979
2980 " items and matches both in what
2981 let g:what = ['items', 'matches']
2982 call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
2983 call assert_equal([
2984 \ {'word': 'aaa', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
2985 \ {'word': 'aab', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
2986 \ {'word': 'aba', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
2987 \ {'word': 'abb', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
2988 \], g:compl_info['items'])
2989 call assert_false(has_key(g:compl_info, 'matches'))
2990
2991 bw!
2992 bw!
2993 unlet g:what
2994 delfunc ShownInfo
2995 set cot&
2996endfunc
2997
glepnir037b0282025-01-16 14:37:44 +01002998func Test_complete_info_completed()
2999 func ShownInfo()
3000 let g:compl_info = complete_info(['completed'])
3001 return ''
3002 endfunc
3003 set completeopt+=noinsert
3004
3005 new
3006 call setline(1, ['aaa', 'aab', 'aba', 'abb'])
3007 inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
3008
3009 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
3010 call assert_equal({'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
3011
3012 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
3013 call assert_equal({'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
3014
3015 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
3016 call assert_equal({'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
3017
3018 set completeopt+=noselect
3019 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
3020 call assert_equal({}, g:compl_info)
3021
3022 bw!
3023 bw!
3024 delfunc ShownInfo
3025 set cot&
3026endfunc
3027
Christian Brabandtfef66302024-01-29 21:46:58 +01003028" vim: shiftwidth=2 sts=2 expandtab nofoldenable