blob: b302a3ee745e7b03f30b49fed59fd59b6820df3d [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 Palya12b1eb52025-02-24 21:39:42 +0100500func ScrollInfoWindowUserDefinedFn(findstart, query)
501 " User defined function (i_CTRL-X_CTRL-U)
502 if a:findstart
503 return col('.')
504 endif
505 let infostr = range(20)->mapnew({_, v -> string(v)})->join("\n")
506 return [{'word': 'foo', 'info': infostr}, {'word': 'bar'}]
507endfunc
508
509func ScrollInfoWindowPageDown()
510 call win_execute(popup_findinfo(), "normal! \<PageDown>")
511 return ''
512endfunc
513
514func ScrollInfoWindowPageUp()
515 call win_execute(popup_findinfo(), "normal! \<PageUp>")
516 return ''
517endfunc
518
519func ScrollInfoWindowTest(mvmt, count, fline)
520 new
521 set completeopt=menuone,popup,noinsert,noselect
522 set completepopup=height:5
523 set completefunc=ScrollInfoWindowUserDefinedFn
524 let keyseq = "i\<C-X>\<C-U>\<C-N>"
525 for _ in range(a:count)
526 let keyseq .= (a:mvmt == "pageup" ? "\<C-R>\<C-R>=ScrollInfoWindowPageUp()\<CR>" :
527 \ "\<C-R>\<C-R>=ScrollInfoWindowPageDown()\<CR>")
528 endfor
529 let keyseq .= "\<C-R>\<C-R>=string(popup_getpos(popup_findinfo()))\<CR>\<ESC>"
530 call feedkeys(keyseq, "tx")
531 call assert_match('''firstline'': ' . a:fline, getline(1))
532 bwipe!
533 set completeopt&
534 set completepopup&
535 set completefunc&
536endfunc
537
538func Test_scroll_info_window()
539 call ScrollInfoWindowTest("", 0, 1)
540 call ScrollInfoWindowTest("pagedown", 1, 4)
541 call ScrollInfoWindowTest("pagedown", 2, 7)
542 call ScrollInfoWindowTest("pagedown", 3, 11)
543 call ScrollInfoWindowTest("pageup", 3, 1)
544endfunc
545
Girish Palya8950bf72024-03-20 20:07:29 +0100546func CompleteInfoUserDefinedFn(findstart, query)
547 " User defined function (i_CTRL-X_CTRL-U)
548 if a:findstart
549 return col('.')
550 endif
551 return [{'word': 'foo'}, {'word': 'bar'}, {'word': 'baz'}, {'word': 'qux'}]
552endfunc
553
554func CompleteInfoTestUserDefinedFn(mvmt, idx, noselect)
555 new
556 if a:noselect
557 set completeopt=menuone,popup,noinsert,noselect
558 else
559 set completeopt=menu,preview
560 endif
561 set completefunc=CompleteInfoUserDefinedFn
562 call feedkeys("i\<C-X>\<C-U>" . a:mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
563 let completed = a:idx != -1 ? ['foo', 'bar', 'baz', 'qux']->get(a:idx) : ''
564 call assert_equal(completed. "{'pum_visible': 1, 'mode': 'function', 'selected': " . a:idx . ", 'items': [" .
565 \ "{'word': 'foo', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
566 \ "{'word': 'bar', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
567 \ "{'word': 'baz', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
568 \ "{'word': 'qux', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" .
569 \ "]}", getline(1))
570 bwipe!
571 set completeopt&
572 set completefunc&
573endfunc
574
575func Test_complete_info_user_defined_fn()
576 " forward
577 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 1, v:true)
578 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 2, v:true)
579 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 2, v:false)
580 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 3, v:false)
581 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>", -1, v:false)
582 " backward
583 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 2, v:true)
584 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 1, v:true)
585 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>", -1, v:true)
586 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 3, v:false)
587 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 2, v:false)
588 " forward backward
589 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 1, v:true)
590 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 0, v:true)
591 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 2, v:false)
592 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 3, v:false)
593 call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 1, v:false)
594 " backward forward
595 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 0, v:true)
596 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 2, v:true)
597 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 1, v:false)
598 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 3, v:false)
599 call CompleteInfoTestUserDefinedFn("\<C-P>\<C-N>\<C-N>", 1, v:false)
600endfunc
601
zeertzjq440d4cb2023-03-02 17:51:32 +0000602" Test that mouse scrolling/movement should not interrupt completion.
603func Test_mouse_scroll_move_during_completion()
604 new
605 com! -buffer TestCommand1 echo 'TestCommand1'
606 com! -buffer TestCommand2 echo 'TestCommand2'
607 call setline(1, ['', '', '', '', ''])
608 call cursor(5, 1)
609
610 " Without completion menu scrolling can move text.
611 set completeopt-=menu wrap
612 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
613 call assert_equal('TestCommand2', getline('.'))
614 call assert_notequal(1, winsaveview().topline)
615 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
616 call assert_equal('TestCommand2', getline('.'))
617 call assert_equal(1, winsaveview().topline)
618 set nowrap
619 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
620 call assert_equal('TestCommand2', getline('.'))
621 call assert_notequal(0, winsaveview().leftcol)
622 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
623 call assert_equal('TestCommand2', getline('.'))
624 call assert_equal(0, winsaveview().leftcol)
625 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
626 call assert_equal('TestCommand2', getline('.'))
627
628 " With completion menu scrolling cannot move text.
629 set completeopt+=menu wrap
630 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
631 call assert_equal('TestCommand2', getline('.'))
632 call assert_equal(1, winsaveview().topline)
633 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
634 call assert_equal('TestCommand2', getline('.'))
635 call assert_equal(1, winsaveview().topline)
636 set nowrap
637 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
638 call assert_equal('TestCommand2', getline('.'))
639 call assert_equal(0, winsaveview().leftcol)
640 call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
641 call assert_equal('TestCommand2', getline('.'))
642 call assert_equal(0, winsaveview().leftcol)
643 call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
644 call assert_equal('TestCommand2', getline('.'))
645
646 bwipe!
647 set completeopt& wrap&
648endfunc
649
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100650" Check that when using feedkeys() typeahead does not interrupt searching for
651" completions.
652func Test_compl_feedkeys()
653 new
654 set completeopt=menuone,noselect
655 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
656 call assert_equal("jump jump", getline(1))
657 bwipe!
658 set completeopt&
659endfunc
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200660
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200661" Test for insert path completion with completeslash option
662func Test_ins_completeslash()
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200663 CheckMSWindows
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200664
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100665 call mkdir('Xcpldir', 'R')
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200666 let orig_shellslash = &shellslash
667 set cpt&
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200668 new
Bram Moolenaar8f187fc2020-09-26 18:47:11 +0200669
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200670 set noshellslash
671
672 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100673 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100674 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200675
676 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100677 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100678 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200679
680 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100681 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100682 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200683
684 set shellslash
685
686 set completeslash=
Bram Moolenaar816736b2022-08-29 23:01:45 +0100687 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100688 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200689
690 set completeslash=backslash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100691 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100692 call assert_equal('Xcpldir\', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200693
694 set completeslash=slash
Bram Moolenaar816736b2022-08-29 23:01:45 +0100695 exe "normal oXcp\<C-X>\<C-F>"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100696 call assert_equal('Xcpldir/', getline('.'))
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200697 %bw!
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200698
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200699 set noshellslash
700 set completeslash=slash
701 call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
702
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200703 let &shellslash = orig_shellslash
Bram Moolenaar50f91d22019-08-02 19:52:15 +0200704 set completeslash=
Bram Moolenaarac3150d2019-07-28 16:36:39 +0200705endfunc
706
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200707func Test_pum_stopped_by_timer()
708 CheckScreendump
709
710 let lines =<< trim END
711 call setline(1, ['hello', 'hullo', 'heeee', ''])
712 func StartCompl()
713 call timer_start(100, { -> execute('stopinsert') })
714 call feedkeys("Gah\<C-N>")
715 endfunc
716 END
717
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100718 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200719 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
720 call term_sendkeys(buf, ":call StartCompl()\<CR>")
721 call TermWait(buf, 200)
722 call term_sendkeys(buf, "k")
723 call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
724
725 call StopVimInTerminal(buf)
Bram Moolenaard0e1b712020-09-27 20:13:03 +0200726endfunc
727
zeertzjqcd5dbad2022-05-04 17:51:50 +0100728func Test_complete_stopinsert_startinsert()
729 nnoremap <F2> <Cmd>startinsert<CR>
730 inoremap <F2> <Cmd>stopinsert<CR>
731 " This just checks if this causes an error
732 call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
733 nunmap <F2>
734 iunmap <F2>
735endfunc
736
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100737func Test_pum_with_folds_two_tabs()
738 CheckScreendump
739
740 let lines =<< trim END
741 set fdm=marker
742 call setline(1, ['" x {{{1', '" a some text'])
743 call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
744 norm! zm
745 tab sp
746 call feedkeys('2Gzv', 'xt')
747 call feedkeys("0fa", 'xt')
748 END
749
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100750 call writefile(lines, 'Xpumscript', 'D')
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100751 let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200752 call TermWait(buf, 50)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100753 call term_sendkeys(buf, "a\<C-N>")
754 call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
755
756 call term_sendkeys(buf, "\<Esc>")
757 call StopVimInTerminal(buf)
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100758endfunc
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100759
760func Test_pum_with_preview_win()
761 CheckScreendump
762
763 let lines =<< trim END
glepnir53387c52024-05-27 15:11:01 +0200764 func Omni_test(findstart, base)
765 if a:findstart
766 return col(".") - 1
767 endif
768 return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
769 endfunc
770 set omnifunc=Omni_test
771 set completeopt+=longest
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100772 END
773
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100774 call writefile(lines, 'Xpreviewscript', 'D')
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100775 let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100776 call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
Bram Moolenaar0ff01832022-09-24 19:20:30 +0100777 call TermWait(buf, 200)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100778 call term_sendkeys(buf, "\<C-N>")
779 call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
780
781 call term_sendkeys(buf, "\<Esc>")
782 call StopVimInTerminal(buf)
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100783endfunc
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100784
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000785func Test_scrollbar_on_wide_char()
786 CheckScreendump
787
788 let lines =<< trim END
789 call setline(1, ['a', ' 啊啊啊',
790 \ ' 哦哦哦',
791 \ ' 呃呃呃'])
792 call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
793 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100794 call writefile(lines, 'Xwidescript', 'D')
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000795 let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
796 call term_sendkeys(buf, "A\<C-N>")
797 call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
798
799 call StopVimInTerminal(buf)
Bram Moolenaar35d8c202022-03-03 11:46:00 +0000800endfunc
801
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100802" Test for inserting the tag search pattern in insert mode
803func Test_ins_compl_tag_sft()
804 call writefile([
805 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
806 \ "first\tXfoo\t/^int first() {}$/",
807 \ "second\tXfoo\t/^int second() {}$/",
808 \ "third\tXfoo\t/^int third() {}$/"],
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100809 \ 'Xtags', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100810 set tags=Xtags
811 let code =<< trim [CODE]
812 int first() {}
813 int second() {}
814 int third() {}
815 [CODE]
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100816 call writefile(code, 'Xfoo', 'D')
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100817
818 enew
819 set showfulltag
820 exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
821 call assert_equal('int second() {}', getline(1))
822 set noshowfulltag
823
Bram Moolenaar830c1af2020-01-05 20:35:44 +0100824 set tags&
825 %bwipe!
826endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200827
828" Test for 'completefunc' deleting text
829func Test_completefunc_error()
830 new
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200831 " delete text when called for the first time
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200832 func CompleteFunc(findstart, base)
833 if a:findstart == 1
834 normal dd
835 return col('.') - 1
836 endif
837 return ['a', 'b']
838 endfunc
839 set completefunc=CompleteFunc
840 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100841 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200842
843 " delete text when called for the second time
844 func CompleteFunc2(findstart, base)
845 if a:findstart == 1
846 return col('.') - 1
847 endif
848 normal dd
849 return ['a', 'b']
850 endfunc
851 set completefunc=CompleteFunc2
852 call setline(1, ['', 'abcd', ''])
zeertzjqcfe45652022-05-27 17:26:55 +0100853 call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200854
Bram Moolenaar97202d92021-01-28 18:34:35 +0100855 " Jump to a different window from the complete function
Bram Moolenaar28976e22021-01-29 21:07:07 +0100856 func CompleteFunc3(findstart, base)
Bram Moolenaar97202d92021-01-28 18:34:35 +0100857 if a:findstart == 1
858 return col('.') - 1
859 endif
860 wincmd p
861 return ['a', 'b']
862 endfunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100863 set completefunc=CompleteFunc3
Bram Moolenaar97202d92021-01-28 18:34:35 +0100864 new
Bram Moolenaar28976e22021-01-29 21:07:07 +0100865 call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
Bram Moolenaar97202d92021-01-28 18:34:35 +0100866 close!
867
868 set completefunc&
869 delfunc CompleteFunc
Bram Moolenaar28976e22021-01-29 21:07:07 +0100870 delfunc CompleteFunc2
871 delfunc CompleteFunc3
872 close!
Bram Moolenaar97202d92021-01-28 18:34:35 +0100873endfunc
874
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200875" Test for returning non-string values from 'completefunc'
876func Test_completefunc_invalid_data()
877 new
878 func! CompleteFunc(findstart, base)
879 if a:findstart == 1
880 return col('.') - 1
881 endif
882 return [{}, '', 'moon']
883 endfunc
884 set completefunc=CompleteFunc
885 exe "normal i\<C-X>\<C-U>"
886 call assert_equal('moon', getline(1))
887 set completefunc&
888 close!
889endfunc
890
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200891" Test for errors in using complete() function
892func Test_complete_func_error()
893 call assert_fails('call complete(1, ["a"])', 'E785:')
894 func ListColors()
895 call complete(col('.'), "blue")
896 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100897 call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200898 func ListMonths()
899 call complete(col('.'), test_null_list())
900 endfunc
Bram Moolenaard83392a2022-09-01 12:22:46 +0100901 call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200902 delfunc ListColors
903 delfunc ListMonths
Bram Moolenaard83392a2022-09-01 12:22:46 +0100904 call assert_fails('call complete_info({})', 'E1211:')
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200905 call assert_equal([], complete_info(['items']).items)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200906endfunc
907
Yegappan Lakshmanane9825862022-01-03 11:03:48 +0000908" Test for recursively starting completion mode using complete()
909func Test_recursive_complete_func()
910 func ListColors()
911 call complete(5, ["red", "blue"])
912 return ''
913 endfunc
914 new
915 call setline(1, ['a1', 'a2'])
916 set complete=.
917 exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
918 call assert_equal('a2blue', getline(3))
919 delfunc ListColors
920 bw!
921endfunc
922
bfredl87af60c2022-09-24 11:17:51 +0100923" Test for using complete() with completeopt+=longest
924func Test_complete_with_longest()
bfredl87af60c2022-09-24 11:17:51 +0100925 new
zeertzjq75f4baf2022-09-24 14:08:23 +0100926 inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
bfredl87af60c2022-09-24 11:17:51 +0100927
928 " default: insert first match
929 set completeopt&
930 call setline(1, ['i'])
931 exe "normal Aa\<f3>\<esc>"
932 call assert_equal('iaax', getline(1))
933
934 " with longest: insert longest prefix
935 set completeopt+=longest
936 call setline(1, ['i'])
937 exe "normal Aa\<f3>\<esc>"
938 call assert_equal('iaa', getline(1))
939 set completeopt&
zeertzjq75f4baf2022-09-24 14:08:23 +0100940 bwipe!
bfredl87af60c2022-09-24 11:17:51 +0100941endfunc
942
zeertzjq529b9ad2024-06-05 20:27:06 +0200943" Test for buffer-local value of 'completeopt'
944func Test_completeopt_buffer_local()
945 set completeopt=menu
946 new
947 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
948 call assert_equal('', &l:completeopt)
949 call assert_equal('menu', &completeopt)
950 call assert_equal('menu', &g:completeopt)
951
952 setlocal bufhidden=hide
953 enew
954 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
955 call assert_equal('', &l:completeopt)
956 call assert_equal('menu', &completeopt)
957 call assert_equal('menu', &g:completeopt)
958
959 setlocal completeopt+=fuzzy,noinsert
960 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
961 call assert_equal('menu,fuzzy,noinsert', &completeopt)
962 call assert_equal('menu', &g:completeopt)
963 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
964 call assert_equal('foobaz', getline('.'))
965
966 setlocal completeopt=
967 call assert_equal('', &l:completeopt)
968 call assert_equal('menu', &completeopt)
969 call assert_equal('menu', &g:completeopt)
970 call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
971 call assert_equal('foofoo', getline('.'))
972
973 setlocal completeopt+=longest
974 call assert_equal('menu,longest', &l:completeopt)
975 call assert_equal('menu,longest', &completeopt)
976 call assert_equal('menu', &g:completeopt)
977 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
978 call assert_equal('foo', getline('.'))
979
980 setlocal bufhidden=hide
981 buffer #
982 call assert_equal('', &l:completeopt)
983 call assert_equal('menu', &completeopt)
984 call assert_equal('menu', &g:completeopt)
985 call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
986 call assert_equal('foofoo', getline('.'))
987
988 setlocal completeopt+=fuzzy,noinsert
989 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
990 call assert_equal('menu,fuzzy,noinsert', &completeopt)
991 call assert_equal('menu', &g:completeopt)
992 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
993 call assert_equal('foobaz', getline('.'))
994
995 buffer #
996 call assert_equal('menu,longest', &l:completeopt)
997 call assert_equal('menu,longest', &completeopt)
998 call assert_equal('menu', &g:completeopt)
999 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
1000 call assert_equal('foo', getline('.'))
1001
1002 setlocal bufhidden=wipe
1003 buffer! #
1004 bwipe!
1005 call assert_equal('', &l:completeopt)
1006 call assert_equal('menu', &completeopt)
1007 call assert_equal('menu', &g:completeopt)
1008
zeertzjq46dcd842024-11-03 09:10:50 +01001009 new | only
1010 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
1011 set completeopt&
1012 setlocal completeopt=menu,fuzzy,noinsert
1013 setglobal completeopt=menu,longest
1014 call assert_equal('menu,fuzzy,noinsert', &completeopt)
1015 call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
1016 call assert_equal('menu,longest', &g:completeopt)
1017 call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
1018 call assert_equal('foobaz', getline('.'))
1019 setlocal bufhidden=wipe
1020 new | only!
1021 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
1022 call assert_equal('menu,longest', &completeopt)
1023 call assert_equal('menu,longest', &g:completeopt)
1024 call assert_equal('', &l:completeopt)
1025 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
1026 call assert_equal('foo', getline('.'))
1027 bwipe!
1028
1029 new | only
1030 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
1031 set completeopt&
1032 setlocal completeopt=menu,fuzzy,noinsert
1033 set completeopt=menu,longest
1034 call assert_equal('menu,longest', &completeopt)
1035 call assert_equal('menu,longest', &g:completeopt)
1036 call assert_equal('', &l:completeopt)
1037 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
1038 call assert_equal('foo', getline('.'))
1039 setlocal bufhidden=wipe
1040 new | only!
1041 call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
1042 call assert_equal('menu,longest', &completeopt)
1043 call assert_equal('menu,longest', &g:completeopt)
1044 call assert_equal('', &l:completeopt)
1045 call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
1046 call assert_equal('foo', getline('.'))
1047 bwipe!
1048
zeertzjq529b9ad2024-06-05 20:27:06 +02001049 set completeopt&
1050endfunc
bfredl87af60c2022-09-24 11:17:51 +01001051
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001052" Test for completing words following a completed word in a line
1053func Test_complete_wrapscan()
1054 " complete words from another buffer
1055 new
1056 call setline(1, ['one two', 'three four'])
1057 new
1058 setlocal complete=w
1059 call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
1060 call assert_equal('two three four', getline(1))
1061 close!
1062 " complete words from the current buffer
1063 setlocal complete=.
1064 %d
1065 call setline(1, ['one two', ''])
1066 call cursor(2, 1)
1067 call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
1068 call assert_equal('one two one two', getline(2))
1069 close!
1070endfunc
1071
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001072" Test for completing special characters
1073func Test_complete_special_chars()
1074 new
1075 call setline(1, 'int .*[-\^$ func float')
1076 call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
1077 call assert_equal('int .*[-\^$ func float', getline(2))
1078 close!
1079endfunc
1080
1081" Test for completion when text is wrapped across lines.
1082func Test_complete_across_line()
1083 new
1084 call setline(1, ['red green blue', 'one two three'])
1085 setlocal textwidth=20
1086 exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1087 call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
1088 close!
1089endfunc
1090
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00001091" Test for completing words with a '.' at the end of a word.
1092func Test_complete_joinspaces()
1093 new
1094 call setline(1, ['one two.', 'three. four'])
1095 set joinspaces
1096 exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1097 call assert_equal("one two. three. four", getline(3))
1098 set joinspaces&
1099 bw!
1100endfunc
1101
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001102" Test for using CTRL-L to add one character when completing matching
1103func Test_complete_add_onechar()
1104 new
1105 call setline(1, ['wool', 'woodwork'])
1106 call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
1107 call assert_equal('woof', getline(3))
1108
1109 " use 'ignorecase' and backspace to erase characters from the prefix string
1110 " and then add letters using CTRL-L
1111 %d
1112 set ignorecase backspace=2
1113 setlocal complete=.
1114 call setline(1, ['workhorse', 'workload'])
1115 normal Go
nwounkn2e3cd522023-10-17 11:05:38 +02001116 exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001117 call assert_equal('workh', getline(3))
1118 set ignorecase& backspace&
1119 close!
1120endfunc
1121
Yegappan Lakshmananedc6f102021-12-29 17:38:46 +00001122" Test for using CTRL-X CTRL-L to complete whole lines lines
1123func Test_complete_wholeline()
1124 new
1125 " complete one-line
1126 call setline(1, ['a1', 'a2'])
1127 exe "normal ggoa\<C-X>\<C-L>"
1128 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1129 " go to the next match (wrapping around the buffer)
1130 exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
1131 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
1132 " go to the next match
1133 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
1134 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
1135 exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
1136 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1137 " repeat the test using CTRL-L
1138 " go to the next match (wrapping around the buffer)
1139 exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
1140 call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
1141 " go to the next match
1142 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
1143 call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
1144 exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
1145 call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
1146 %d
1147 " use CTRL-X CTRL-L to add one more line
1148 call setline(1, ['a1', 'b1'])
1149 setlocal complete=.
1150 exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
1151 call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
1152 bw!
1153endfunc
1154
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001155" Test insert completion with 'cindent' (adjust the indent)
1156func Test_complete_with_cindent()
1157 new
1158 setlocal cindent
1159 call setline(1, ['if (i == 1)', " j = 2;"])
1160 exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
1161 call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
1162
1163 %d
1164 call setline(1, ['when while', '{', ''])
1165 setlocal cinkeys+==while
1166 exe "normal Giwh\<C-P> "
1167 call assert_equal("\twhile ", getline('$'))
1168 close!
1169endfunc
1170
1171" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
1172func Test_complete_cmdline()
1173 new
1174 exe "normal icaddb\<C-X>\<C-V>"
1175 call assert_equal('caddbuffer', getline(1))
1176 exe "normal ocall getqf\<C-X>\<C-V>"
1177 call assert_equal('call getqflist(', getline(2))
1178 exe "normal oabcxyz(\<C-X>\<C-V>"
1179 call assert_equal('abcxyz(', getline(3))
zeertzjqdca29d92021-08-31 19:12:51 +02001180 com! -buffer TestCommand1 echo 'TestCommand1'
1181 com! -buffer TestCommand2 echo 'TestCommand2'
1182 write TestCommand1Test
1183 write TestCommand2Test
1184 " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
1185 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
1186 call assert_equal('TestCommand2Test', getline(4))
1187 call delete('TestCommand1Test')
1188 call delete('TestCommand2Test')
1189 delcom TestCommand1
1190 delcom TestCommand2
1191 close!
1192endfunc
1193
1194" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
1195func Test_complete_stop()
1196 new
1197 func Save_mode1()
1198 let g:mode1 = mode(1)
1199 return ''
1200 endfunc
1201 func Save_mode2()
1202 let g:mode2 = mode(1)
1203 return ''
1204 endfunc
1205 inoremap <F1> <C-R>=Save_mode1()<CR>
1206 inoremap <F2> <C-R>=Save_mode2()<CR>
1207 call setline(1, ['aaa bbb ccc '])
1208 exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1209 call assert_equal('ic', g:mode1)
1210 call assert_equal('i', g:mode2)
1211 call assert_equal('aaa bbb ccc ', getline(1))
1212 exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1213 call assert_equal('ic', g:mode1)
1214 call assert_equal('i', g:mode2)
1215 call assert_equal('aaa bbb ccc aaa', getline(1))
1216 set completeopt+=noselect
1217 exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1218 call assert_equal('ic', g:mode1)
1219 call assert_equal('i', g:mode2)
1220 call assert_equal('aaa bbb ccc aaa bb', getline(1))
1221 set completeopt&
1222 exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1223 call assert_equal('ic', g:mode1)
1224 call assert_equal('i', g:mode2)
1225 call assert_equal('aaa bbb ccc aaa bb d', getline(1))
1226 com! -buffer TestCommand1 echo 'TestCommand1'
1227 com! -buffer TestCommand2 echo 'TestCommand2'
1228 exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
1229 call assert_equal('ic', g:mode1)
1230 call assert_equal('i', g:mode2)
1231 call assert_equal('TestCommand2', getline(2))
1232 delcom TestCommand1
1233 delcom TestCommand2
1234 unlet g:mode1
1235 unlet g:mode2
1236 iunmap <F1>
1237 iunmap <F2>
1238 delfunc Save_mode1
1239 delfunc Save_mode2
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +02001240 close!
1241endfunc
1242
Yegappan Lakshmanan5d2e0072021-12-30 11:40:53 +00001243" Test for typing CTRL-R in insert completion mode to insert a register
1244" content.
1245func Test_complete_reginsert()
1246 new
1247 call setline(1, ['a1', 'a12', 'a123', 'a1234'])
1248
1249 " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
1250 " processed. Otherwise, CTRL-X mode should be stopped and the key should be
1251 " inserted.
1252 exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
1253 call assert_equal('a123', getline(5))
1254 let @r = "\<C-P>\<C-P>"
1255 exe "normal GCa\<C-P>\<C-R>r"
1256 call assert_equal('a12', getline(5))
1257 exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
1258 call assert_equal('a1234x', getline(5))
1259 bw!
1260endfunc
1261
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02001262func Test_issue_7021()
1263 CheckMSWindows
1264
1265 let orig_shellslash = &shellslash
1266 set noshellslash
1267
1268 set completeslash=slash
1269 call assert_false(expand('~') =~ '/')
1270
1271 let &shellslash = orig_shellslash
1272 set completeslash=
1273endfunc
1274
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001275" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
1276func Test_complete_longest_match()
1277 for e in ['latin1', 'utf-8']
1278 exe 'set encoding=' .. e
1279 new
1280 set complete=.
1281 set completeopt=menu,longest
1282 call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
1283 exe "normal Gopfx\<C-P>"
1284 call assert_equal('pfx_', getline(5))
1285 bw!
1286 endfor
1287
1288 " Test for completing additional words with longest match set
1289 new
1290 call setline(1, ['abc1', 'abd2'])
1291 exe "normal Goab\<C-P>\<C-X>\<C-P>"
1292 call assert_equal('ab', getline(3))
1293 bw!
1294 set complete& completeopt&
1295endfunc
1296
1297" Test for removing the first displayed completion match and selecting the
1298" match just before that.
1299func Test_complete_erase_firstmatch()
1300 new
1301 call setline(1, ['a12', 'a34', 'a56'])
1302 set complete=.
1303 exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
1304 call assert_equal('a34', getline('$'))
1305 set complete&
1306 bw!
1307endfunc
1308
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001309" Test for completing words from unloaded buffers
1310func Test_complete_from_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001311 call writefile(['abc'], "Xfile1", 'D')
1312 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001313 edit Xfile1
1314 edit Xfile2
1315 new | close
1316 enew
1317 bunload Xfile1 Xfile2
1318 set complete=u
1319 " complete from an unloaded buffer
1320 exe "normal! ia\<C-P>"
1321 call assert_equal('abc', getline(1))
1322 exe "normal! od\<C-P>"
1323 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001324
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001325 set complete&
1326 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001327endfunc
1328
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001329" Test for completing whole lines from unloaded buffers
1330func Test_complete_wholeline_unloadedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001331 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001332 edit Xfile1
1333 enew
1334 set complete=u
1335 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1336 call assert_equal('a line2', getline(1))
1337 %d
1338 " completing from an unlisted buffer should fail
1339 bdel Xfile1
1340 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1341 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001342
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001343 set complete&
1344 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001345endfunc
1346
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001347" Test for completing words from unlisted buffers
1348func Test_complete_from_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001349 call writefile(['abc'], "Xfile1", 'D')
1350 call writefile(['def'], "Xfile2", 'D')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001351 edit Xfile1
1352 edit Xfile2
1353 new | close
1354 bdel Xfile1 Xfile2
1355 set complete=U
1356 " complete from an unlisted buffer
1357 exe "normal! ia\<C-P>"
1358 call assert_equal('abc', getline(1))
1359 exe "normal! od\<C-P>"
1360 call assert_equal('def', getline(2))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001361
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001362 set complete&
1363 %bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001364endfunc
1365
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001366" Test for completing whole lines from unlisted buffers
1367func Test_complete_wholeline_unlistedbuf()
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001368 call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D')
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001369 edit Xfile1
1370 enew
1371 set complete=U
zeertzjqc029c132024-03-28 11:37:26 +01001372 " completing from an unloaded buffer should fail
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001373 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1374 call assert_equal('a', getline(1))
1375 %d
1376 bdel Xfile1
1377 exe "normal! ia\<C-X>\<C-L>\<C-P>"
1378 call assert_equal('a line2', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001379
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001380 set complete&
1381 %bw!
Yegappan Lakshmanane9825862022-01-03 11:03:48 +00001382endfunc
1383
1384" Test for adding a multibyte character using CTRL-L in completion mode
1385func Test_complete_mbyte_char_add()
1386 new
1387 set complete=.
1388 call setline(1, 'abė')
1389 exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
1390 call assert_equal('abė', getline(2))
1391 " Test for a leader with multibyte character
1392 %d
1393 call setline(1, 'abėĕ')
1394 exe "normal! oabė\<C-P>"
1395 call assert_equal('abėĕ', getline(2))
1396 bw!
1397endfunc
1398
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001399" Test for using <C-X><C-P> for local expansion even if 'complete' is set to
1400" not to complete matches from the local buffer. Also test using multiple
1401" <C-X> to cancel the current completion mode.
1402func Test_complete_local_expansion()
1403 new
1404 set complete=t
1405 call setline(1, ['abc', 'def'])
1406 exe "normal! Go\<C-X>\<C-P>"
1407 call assert_equal("def", getline(3))
1408 exe "normal! Go\<C-P>"
1409 call assert_equal("", getline(4))
1410 exe "normal! Go\<C-X>\<C-N>"
1411 call assert_equal("abc", getline(5))
1412 exe "normal! Go\<C-N>"
1413 call assert_equal("", getline(6))
1414
1415 " use multiple <C-X> to cancel the previous completion mode
1416 exe "normal! Go\<C-P>\<C-X>\<C-P>"
1417 call assert_equal("", getline(7))
1418 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
1419 call assert_equal("", getline(8))
1420 exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
1421 call assert_equal("abc", getline(9))
1422
1423 " interrupt the current completion mode
1424 set completeopt=menu,noinsert
1425 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
1426 call assert_equal("abc", getline(10))
1427
1428 " when only one <C-X> is used to interrupt, do normal expansion
1429 exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
1430 call assert_equal("", getline(11))
1431 set completeopt&
1432
1433 " using two <C-X> in non-completion mode and restarting the same mode
1434 exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
1435 call assert_equal("def", getline(12))
1436
1437 " test for adding a match from the original empty text
1438 %d
1439 call setline(1, 'abc def g')
1440 exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
1441 call assert_equal('def', getline(2))
1442 exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
1443 call assert_equal('abc', getline(2))
1444
1445 bw!
1446endfunc
1447
1448" Test for undoing changes after a insert-mode completion
1449func Test_complete_undo()
1450 new
1451 set complete=.
1452 " undo with 'ignorecase'
1453 call setline(1, ['ABOVE', 'BELOW'])
1454 set ignorecase
1455 exe "normal! Goab\<C-G>u\<C-P>"
1456 call assert_equal("ABOVE", getline(3))
1457 undo
1458 call assert_equal("ab", getline(3))
1459 set ignorecase&
1460 %d
1461 " undo with longest match
1462 set completeopt=menu,longest
1463 call setline(1, ['above', 'about'])
1464 exe "normal! Goa\<C-G>u\<C-P>"
1465 call assert_equal("abo", getline(3))
1466 undo
1467 call assert_equal("a", getline(3))
1468 set completeopt&
1469 %d
1470 " undo for line completion
1471 call setline(1, ['above that change', 'below that change'])
1472 exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
1473 call assert_equal("above that change", getline(3))
1474 undo
1475 call assert_equal("above", getline(3))
1476
1477 bw!
1478endfunc
1479
1480" Test for completing a very long word
1481func Test_complete_long_word()
1482 set complete&
1483 new
1484 call setline(1, repeat('x', 950) .. ' one two three')
1485 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1486 call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
1487 %d
1488 " should fail when more than 950 characters are in a word
1489 call setline(1, repeat('x', 951) .. ' one two three')
1490 exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
1491 call assert_equal(repeat('x', 951), getline(2))
1492
1493 " Test for adding a very long word to an existing completion
1494 %d
1495 call setline(1, ['abc', repeat('x', 1016) .. '012345'])
1496 exe "normal! Goab\<C-P>\<C-X>\<C-P>"
1497 call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
1498 bw!
1499endfunc
1500
1501" Test for some fields in the complete items used by complete()
1502func Test_complete_items()
1503 func CompleteItems(idx)
1504 let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
1505 \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
1506 \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
1507 \ [#{user_data: 'u9'}],
1508 \ [#{word: "", user_data: 'u10'}],
1509 \ [#{word: "", empty: 1, user_data: 'u11'}]]
1510 call complete(col('.'), items[a:idx])
1511 return ''
1512 endfunc
1513 new
1514 exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
1515 call assert_equal('u2', v:completed_item.user_data)
1516 call assert_equal('one', getline(1))
1517 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
1518 call assert_equal('u3', v:completed_item.user_data)
1519 call assert_equal('one', getline(2))
1520 exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
1521 call assert_equal('', getline(3))
1522 set completeopt=menu,noinsert
1523 exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
1524 call assert_equal('oNE', getline(4))
1525 call assert_equal('u8', v:completed_item.user_data)
1526 set completeopt&
1527 exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
1528 call assert_equal('', getline(5))
1529 exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
1530 call assert_equal('', getline(6))
1531 exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
1532 call assert_equal('', getline(7))
1533 call assert_equal('u11', v:completed_item.user_data)
1534 " pass invalid argument to complete()
1535 let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
1536 call assert_fails('exe cmd', 'E730:')
1537 bw!
1538 delfunc CompleteItems
1539endfunc
1540
1541" Test for the "refresh" item in the dict returned by an insert completion
1542" function
1543func Test_complete_item_refresh_always()
1544 let g:CallCount = 0
1545 func! Tcomplete(findstart, base)
1546 if a:findstart
1547 " locate the start of the word
1548 let line = getline('.')
1549 let start = col('.') - 1
1550 while start > 0 && line[start - 1] =~ '\a'
1551 let start -= 1
1552 endwhile
1553 return start
1554 else
1555 let g:CallCount += 1
1556 let res = ["update1", "update12", "update123"]
1557 return #{words: res, refresh: 'always'}
1558 endif
1559 endfunc
1560 new
1561 set completeopt=menu,longest
1562 set completefunc=Tcomplete
1563 exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
1564 call assert_equal('up', getline(1))
Luca Saccarola959ef612024-12-01 16:25:53 +01001565 call assert_equal(6, g:CallCount)
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001566 set completeopt&
1567 set completefunc&
1568 bw!
1569 delfunc Tcomplete
1570endfunc
1571
1572" Test for completing from a thesaurus file without read permission
1573func Test_complete_unreadable_thesaurus_file()
1574 CheckUnix
1575 CheckNotRoot
1576
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001577 call writefile(['about', 'above'], 'Xunrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001578 call setfperm('Xunrfile', '---r--r--')
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001579 new
1580 set complete=sXfile
1581 exe "normal! ia\<C-P>"
1582 call assert_equal('a', getline(1))
Bram Moolenaar7dd5a782022-09-29 21:01:57 +01001583
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001584 bw!
Yegappan Lakshmanan37079142022-01-08 10:38:48 +00001585 set complete&
1586endfunc
1587
Bram Moolenaarcc233582020-12-12 13:32:07 +01001588" Test to ensure 'Scanning...' messages are not recorded in messages history
1589func Test_z1_complete_no_history()
1590 new
1591 messages clear
1592 let currmess = execute('messages')
1593 setlocal dictionary=README.txt
1594 exe "normal owh\<C-X>\<C-K>"
1595 exe "normal owh\<C-N>"
1596 call assert_equal(currmess, execute('messages'))
Bram Moolenaard979d642022-03-04 14:51:06 +00001597 bwipe!
1598endfunc
1599
1600" A mapping is not used for the key after CTRL-X.
1601func Test_no_mapping_for_ctrl_x_key()
1602 new
zeertzjq75f4baf2022-09-24 14:08:23 +01001603 inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
Bram Moolenaard979d642022-03-04 14:51:06 +00001604 setlocal dictionary=README.txt
1605 call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
1606 call assert_equal('example ', getline(1))
1607 call assert_false(exists('was_mapped'))
1608 bwipe!
Bram Moolenaarcc233582020-12-12 13:32:07 +01001609endfunc
1610
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001611" Test for different ways of setting the 'completefunc' option
1612func Test_completefunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001613 func CompleteFunc1(callnr, findstart, base)
1614 call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
1615 return a:findstart ? 0 : []
1616 endfunc
1617 func CompleteFunc2(findstart, base)
1618 call add(g:CompleteFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001619 return a:findstart ? 0 : []
1620 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001621
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001622 let lines =<< trim END
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001623 #" Test for using a global function name
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001624 LET &completefunc = 'g:CompleteFunc2'
1625 new
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001626 call setline(1, 'global')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001627 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001628 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001629 call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001630 bw!
1631
1632 #" Test for using a function()
1633 set completefunc=function('g:CompleteFunc1',\ [10])
1634 new
1635 call setline(1, 'one')
1636 LET g:CompleteFunc1Args = []
1637 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1638 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001639 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001640
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001641 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001642 VAR Fn = function('g:CompleteFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001643 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001644 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001645 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001646 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001647 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001648 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001649 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001650
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001651 #" Using string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001652 LET Fn = function('g:CompleteFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001653 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001654 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001655 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001656 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001657 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001658 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001659 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001660
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001661 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001662 set completefunc=funcref('g:CompleteFunc1',\ [13])
1663 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001664 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001665 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001666 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001667 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001668 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001669
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001670 #" Using a funcref variable to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001671 LET Fn = funcref('g:CompleteFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001672 LET &completefunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001673 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001674 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001675 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001676 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001677 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001678 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001679
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001680 #" Using a string(funcref_variable) to set 'completefunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001681 LET Fn = funcref('g:CompleteFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001682 LET &completefunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001683 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001684 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001685 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001686 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001687 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001688 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001689
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001690 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001691 VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001692 LET optval = substitute(optval, ' ', '\\ ', 'g')
1693 exe "set completefunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001694 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001695 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001696 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001697 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001698 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001699 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001700
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001701 #" Set 'completefunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001702 LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001703 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001704 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001705 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001706 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001707 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001708 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001709
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001710 #" Set 'completefunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001711 LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001712 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001713 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001714 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001715 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001716 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001717 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001718
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001719 #" Set 'completefunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001720 VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001721 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001722 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001723 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001724 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001725 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001726 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001727 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001728
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001729 #" Set 'completefunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001730 LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001731 LET &completefunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001732 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001733 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001734 LET g:CompleteFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001735 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001736 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001737 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001738
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001739 #" Test for using a lambda function with incorrect return value
1740 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
1741 LET &completefunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001742 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001743 call setline(1, 'eight')
1744 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1745 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001746
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001747 #" Test for clearing the 'completefunc' option
1748 set completefunc=''
1749 set completefunc&
1750 call assert_fails("set completefunc=function('abc')", "E700:")
1751 call assert_fails("set completefunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001752
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001753 #" set 'completefunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001754 set completefunc=g:CompleteFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001755 call setline(1, 'five')
1756 call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
1757 call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001758 LET g:CompleteFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001759 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001760 call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001761 bw!
1762 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001763 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001764
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001765 " Test for using a script-local function name
1766 func s:CompleteFunc3(findstart, base)
1767 call add(g:CompleteFunc3Args, [a:findstart, a:base])
1768 return a:findstart ? 0 : []
1769 endfunc
1770 set completefunc=s:CompleteFunc3
1771 new
1772 call setline(1, 'script1')
1773 let g:CompleteFunc3Args = []
1774 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1775 call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
1776 bw!
1777
1778 let &completefunc = 's:CompleteFunc3'
1779 new
1780 call setline(1, 'script2')
1781 let g:CompleteFunc3Args = []
1782 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1783 call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
1784 bw!
1785 delfunc s:CompleteFunc3
1786
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00001787 " In Vim9 script s: can be omitted
1788 let lines =<< trim END
1789 vim9script
1790 var CompleteFunc4Args = []
1791 def CompleteFunc4(findstart: bool, base: string): any
1792 add(CompleteFunc4Args, [findstart, base])
1793 return findstart ? 0 : []
1794 enddef
1795 set completefunc=CompleteFunc4
1796 new
1797 setline(1, 'script1')
1798 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1799 assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
1800 bw!
1801 END
1802 call v9.CheckScriptSuccess(lines)
1803
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001804 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001805 let &completefunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001806 call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1807
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001808 " Using Vim9 lambda expression in legacy context should fail
Bram Moolenaar62aec932022-01-29 21:45:34 +00001809 set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001810 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001811 let g:CompleteFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001812 call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001813 call assert_equal([], g:CompleteFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001814
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001815 " set 'completefunc' to a partial with dict. This used to cause a crash.
1816 func SetCompleteFunc()
1817 let params = {'complete': function('g:DictCompleteFunc')}
1818 let &completefunc = params.complete
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001819 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001820 func g:DictCompleteFunc(_) dict
1821 endfunc
1822 call SetCompleteFunc()
1823 new
1824 call SetCompleteFunc()
1825 bw
1826 call test_garbagecollect_now()
1827 new
1828 set completefunc=
1829 wincmd w
1830 set completefunc=
1831 %bw!
1832 delfunc g:DictCompleteFunc
1833 delfunc SetCompleteFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001834
1835 " Vim9 tests
1836 let lines =<< trim END
1837 vim9script
1838
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001839 def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
1840 add(g:Vim9completeFuncArgs, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001841 return findstart ? 0 : []
1842 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001843
1844 # Test for using a def function with completefunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001845 set completefunc=function('Vim9CompleteFunc',\ [60])
1846 new | only
1847 setline(1, 'one')
1848 g:Vim9completeFuncArgs = []
1849 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1850 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
1851 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001852
1853 # Test for using a global function name
1854 &completefunc = g:CompleteFunc2
1855 new | only
1856 setline(1, 'two')
1857 g:CompleteFunc2Args = []
1858 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1859 assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
1860 bw!
1861
1862 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001863 def LocalCompleteFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001864 add(g:LocalCompleteFuncArgs, [findstart, base])
1865 return findstart ? 0 : []
1866 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00001867 &completefunc = LocalCompleteFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00001868 new | only
1869 setline(1, 'three')
1870 g:LocalCompleteFuncArgs = []
1871 feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
1872 assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
1873 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001874 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001875 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00001876
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001877 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001878 set completefunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001879 delfunc CompleteFunc1
1880 delfunc CompleteFunc2
1881 unlet g:CompleteFunc1Args g:CompleteFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001882 %bw!
1883endfunc
1884
1885" Test for different ways of setting the 'omnifunc' option
1886func Test_omnifunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001887 func OmniFunc1(callnr, findstart, base)
1888 call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
1889 return a:findstart ? 0 : []
1890 endfunc
1891 func OmniFunc2(findstart, base)
1892 call add(g:OmniFunc2Args, [a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001893 return a:findstart ? 0 : []
1894 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00001895
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001896 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001897 #" Test for using a function name
1898 LET &omnifunc = 'g:OmniFunc2'
1899 new
1900 call setline(1, 'zero')
1901 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001902 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001903 call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
1904 bw!
1905
1906 #" Test for using a function()
1907 set omnifunc=function('g:OmniFunc1',\ [10])
1908 new
1909 call setline(1, 'one')
1910 LET g:OmniFunc1Args = []
1911 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
1912 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001913 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001914
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001915 #" Using a funcref variable to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001916 VAR Fn = function('g:OmniFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001917 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001918 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001919 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001920 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001921 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001922 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001923 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001924
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001925 #" Using a string(funcref_variable) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001926 LET Fn = function('g:OmniFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001927 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001928 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001929 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001930 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001931 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001932 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001933 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001934
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001935 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001936 set omnifunc=funcref('g:OmniFunc1',\ [13])
1937 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001938 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001939 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001940 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001941 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001942 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001943
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001944 #" Use let to set 'omnifunc' to a funcref
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001945 LET Fn = funcref('g:OmniFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001946 LET &omnifunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001947 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001948 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001949 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001950 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001951 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001952 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001953
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001954 #" Using a string(funcref) to set 'omnifunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001955 LET Fn = funcref("g:OmniFunc1", [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001956 LET &omnifunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001957 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001958 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001959 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001960 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001961 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001962 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001963
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001964 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00001965 VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001966 LET optval = substitute(optval, ' ', '\\ ', 'g')
1967 exe "set omnifunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001968 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001969 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001970 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001971 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001972 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001973 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001974
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001975 #" Set 'omnifunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001976 LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001977 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001978 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001979 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001980 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001981 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001982 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001983
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001984 #" Set 'omnifunc' to a string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00001985 LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001986 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001987 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001988 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001989 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001990 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001991 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00001992
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001993 #" Set 'omnifunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00001994 VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001995 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001996 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001997 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00001998 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00001999 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002000 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002001 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002002
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002003 #" Set 'omnifunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002004 LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002005 LET &omnifunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002006 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002007 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002008 LET g:OmniFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002009 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002010 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002011 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002012
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002013 #" Test for using a lambda function with incorrect return value
2014 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2015 LET &omnifunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002016 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002017 call setline(1, 'eight')
2018 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2019 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002020
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002021 #" Test for clearing the 'omnifunc' option
2022 set omnifunc=''
2023 set omnifunc&
2024 call assert_fails("set omnifunc=function('abc')", "E700:")
2025 call assert_fails("set omnifunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002026
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002027 #" set 'omnifunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002028 set omnifunc=g:OmniFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002029 call setline(1, 'nine')
2030 call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
2031 call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002032 LET g:OmniFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002033 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002034 call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002035 bw!
2036 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002037 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002038
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002039 " Test for using a script-local function name
2040 func s:OmniFunc3(findstart, base)
2041 call add(g:OmniFunc3Args, [a:findstart, a:base])
2042 return a:findstart ? 0 : []
2043 endfunc
2044 set omnifunc=s:OmniFunc3
2045 new
2046 call setline(1, 'script1')
2047 let g:OmniFunc3Args = []
2048 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2049 call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
2050 bw!
2051
2052 let &omnifunc = 's:OmniFunc3'
2053 new
2054 call setline(1, 'script2')
2055 let g:OmniFunc3Args = []
2056 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2057 call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
2058 bw!
2059 delfunc s:OmniFunc3
2060
2061 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002062 let &omnifunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002063 call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2064
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002065 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002066 set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002067 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002068 let g:OmniFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002069 call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002070 call assert_equal([], g:OmniFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002071
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002072 " set 'omnifunc' to a partial with dict. This used to cause a crash.
2073 func SetOmniFunc()
2074 let params = {'omni': function('g:DictOmniFunc')}
2075 let &omnifunc = params.omni
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002076 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002077 func g:DictOmniFunc(_) dict
2078 endfunc
2079 call SetOmniFunc()
2080 new
2081 call SetOmniFunc()
2082 bw
2083 call test_garbagecollect_now()
2084 new
2085 set omnifunc=
2086 wincmd w
2087 set omnifunc=
2088 %bw!
2089 delfunc g:DictOmniFunc
2090 delfunc SetOmniFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002091
2092 " Vim9 tests
2093 let lines =<< trim END
2094 vim9script
2095
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002096 def Vim9omniFunc(callnr: number, findstart: number, base: string): any
2097 add(g:Vim9omniFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002098 return findstart ? 0 : []
2099 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002100
2101 # Test for using a def function with omnifunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002102 set omnifunc=function('Vim9omniFunc',\ [60])
2103 new | only
2104 setline(1, 'one')
2105 g:Vim9omniFunc_Args = []
2106 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2107 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
2108 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002109
2110 # Test for using a global function name
2111 &omnifunc = g:OmniFunc2
2112 new | only
2113 setline(1, 'two')
2114 g:OmniFunc2Args = []
2115 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2116 assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
2117 bw!
2118
2119 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002120 def LocalOmniFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002121 add(g:LocalOmniFuncArgs, [findstart, base])
2122 return findstart ? 0 : []
2123 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002124 &omnifunc = LocalOmniFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002125 new | only
2126 setline(1, 'three')
2127 g:LocalOmniFuncArgs = []
2128 feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
2129 assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
2130 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002131 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002132 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00002133
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002134 " cleanup
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002135 set omnifunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002136 delfunc OmniFunc1
2137 delfunc OmniFunc2
2138 unlet g:OmniFunc1Args g:OmniFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002139 %bw!
2140endfunc
2141
2142" Test for different ways of setting the 'thesaurusfunc' option
2143func Test_thesaurusfunc_callback()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002144 func TsrFunc1(callnr, findstart, base)
2145 call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002146 return a:findstart ? 0 : []
2147 endfunc
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002148 func TsrFunc2(findstart, base)
2149 call add(g:TsrFunc2Args, [a:findstart, a:base])
2150 return a:findstart ? 0 : ['sunday']
2151 endfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002152
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002153 let lines =<< trim END
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002154 #" Test for using a function name
2155 LET &thesaurusfunc = 'g:TsrFunc2'
2156 new
2157 call setline(1, 'zero')
2158 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002159 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002160 call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
2161 bw!
2162
2163 #" Test for using a function()
2164 set thesaurusfunc=function('g:TsrFunc1',\ [10])
2165 new
2166 call setline(1, 'one')
2167 LET g:TsrFunc1Args = []
2168 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2169 call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002170 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002171
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002172 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002173 VAR Fn = function('g:TsrFunc1', [11])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002174 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002175 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002176 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002177 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002178 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002179 call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002180 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002181
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002182 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002183 LET Fn = function('g:TsrFunc1', [12])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002184 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002185 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002186 call setline(1, 'two')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002187 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002188 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002189 call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002190 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002191
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002192 #" Test for using a funcref()
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002193 set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
2194 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002195 call setline(1, 'three')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002196 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002197 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002198 call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002199 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002200
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002201 #" Using a funcref variable to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002202 LET Fn = funcref('g:TsrFunc1', [14])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002203 LET &thesaurusfunc = Fn
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002204 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002205 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002206 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002207 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002208 call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002209 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002210
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002211 #" Using a string(funcref_variable) to set 'thesaurusfunc'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002212 LET Fn = funcref('g:TsrFunc1', [15])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002213 LET &thesaurusfunc = string(Fn)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002214 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002215 call setline(1, 'four')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002216 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002217 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002218 call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002219 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002220
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002221 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +00002222 VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002223 LET optval = substitute(optval, ' ', '\\ ', 'g')
2224 exe "set thesaurusfunc=" .. optval
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002225 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002226 call setline(1, 'five')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002227 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002228 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002229 call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002230 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002231
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002232 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00002233 LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002234 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002235 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002236 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002237 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002238 call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002239 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002240
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002241 #" Set 'thesaurusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002242 LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND'
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002243 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002244 call setline(1, 'six')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002245 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002246 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002247 call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002248 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002249
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002250 #" Set 'thesaurusfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00002251 VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002252 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002253 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002254 call setline(1, 'seven')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002255 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002256 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002257 call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002258 bw!
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002259
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002260 #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00002261 LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002262 LET &thesaurusfunc = string(Lambda)
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002263 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002264 call setline(1, 'seven')
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')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002267 call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002268 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002269
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002270 #" Test for using a lambda function with incorrect return value
2271 LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
2272 LET &thesaurusfunc = Lambda
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002273 new
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002274 call setline(1, 'eight')
2275 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2276 bw!
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002277
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002278 #" Test for clearing the 'thesaurusfunc' option
2279 set thesaurusfunc=''
2280 set thesaurusfunc&
2281 call assert_fails("set thesaurusfunc=function('abc')", "E700:")
2282 call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002283
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002284 #" set 'thesaurusfunc' to a non-existing function
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002285 set thesaurusfunc=g:TsrFunc2
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002286 call setline(1, 'ten')
2287 call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
2288 call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002289 LET g:TsrFunc2Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002290 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002291 call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002292 bw!
2293
2294 #" Use a buffer-local value and a global value
2295 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002296 setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002297 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002298 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002299 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2300 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002301 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002302 new
2303 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002304 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002305 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2306 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002307 call assert_equal([], g:TsrFunc1Args)
2308 set thesaurusfunc=function('g:TsrFunc1',\ [23])
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002309 wincmd w
2310 call setline(1, 'sun')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002311 LET g:TsrFunc1Args = []
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002312 call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
2313 call assert_equal('sun', getline(1))
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002314 call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002315 :%bw!
2316 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002317 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002318
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002319 " Test for using a script-local function name
2320 func s:TsrFunc3(findstart, base)
2321 call add(g:TsrFunc3Args, [a:findstart, a:base])
2322 return a:findstart ? 0 : []
2323 endfunc
zeertzjq6eda2692024-11-03 09:23:33 +01002324
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002325 set tsrfu=s:TsrFunc3
2326 new
2327 call setline(1, 'script1')
2328 let g:TsrFunc3Args = []
2329 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2330 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2331 bw!
2332
2333 let &tsrfu = 's:TsrFunc3'
2334 new
2335 call setline(1, 'script2')
2336 let g:TsrFunc3Args = []
2337 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2338 call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
2339 bw!
zeertzjq6eda2692024-11-03 09:23:33 +01002340
2341 new | only
2342 set thesaurusfunc=
2343 setlocal thesaurusfunc=NoSuchFunc
2344 setglobal thesaurusfunc=s:TsrFunc3
2345 call assert_equal('NoSuchFunc', &thesaurusfunc)
2346 call assert_equal('NoSuchFunc', &l:thesaurusfunc)
2347 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2348 new | only
2349 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2350 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2351 call assert_equal('', &l:thesaurusfunc)
2352 call setline(1, 'script1')
2353 let g:TsrFunc3Args = []
2354 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2355 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2356 bw!
2357
2358 new | only
2359 set thesaurusfunc=
2360 setlocal thesaurusfunc=NoSuchFunc
2361 set thesaurusfunc=s:TsrFunc3
2362 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2363 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2364 call assert_equal('', &l:thesaurusfunc)
2365 call setline(1, 'script1')
2366 let g:TsrFunc3Args = []
2367 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2368 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2369 setlocal bufhidden=wipe
2370 new | only!
2371 call assert_equal('s:TsrFunc3', &thesaurusfunc)
2372 call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
2373 call assert_equal('', &l:thesaurusfunc)
2374 call setline(1, 'script1')
2375 let g:TsrFunc3Args = []
2376 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2377 call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
2378 bw!
2379
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002380 delfunc s:TsrFunc3
2381
2382 " invalid return value
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002383 let &thesaurusfunc = {a -> 'abc'}
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002384 call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2385
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002386 " Using Vim9 lambda expression in legacy context should fail
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002387 set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002388 new | only
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002389 let g:TsrFunc1Args = []
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002390 call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002391 call assert_equal([], g:TsrFunc1Args)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002392 bw!
2393
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002394 " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
2395 func SetTsrFunc()
2396 let params = {'thesaurus': function('g:DictTsrFunc')}
2397 let &thesaurusfunc = params.thesaurus
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002398 endfunc
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002399 func g:DictTsrFunc(_) dict
2400 endfunc
2401 call SetTsrFunc()
2402 new
2403 call SetTsrFunc()
2404 bw
2405 call test_garbagecollect_now()
2406 new
2407 set thesaurusfunc=
2408 wincmd w
2409 %bw!
2410 delfunc SetTsrFunc
2411
2412 " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
2413 " cause a crash.
2414 func SetLocalTsrFunc()
2415 let params = {'thesaurus': function('g:DictTsrFunc')}
2416 let &l:thesaurusfunc = params.thesaurus
2417 endfunc
2418 call SetLocalTsrFunc()
2419 call test_garbagecollect_now()
2420 call SetLocalTsrFunc()
2421 set thesaurusfunc=
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002422 bw!
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00002423 delfunc g:DictTsrFunc
2424 delfunc SetLocalTsrFunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002425
2426 " Vim9 tests
2427 let lines =<< trim END
2428 vim9script
2429
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002430 def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
2431 add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002432 return findstart ? 0 : []
2433 enddef
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002434
2435 # Test for using a def function with thesaurusfunc
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002436 set thesaurusfunc=function('Vim9tsrFunc',\ [60])
2437 new | only
2438 setline(1, 'one')
2439 g:Vim9tsrFunc_Args = []
2440 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2441 assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
2442 bw!
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002443
2444 # Test for using a global function name
2445 &thesaurusfunc = g:TsrFunc2
2446 new | only
2447 setline(1, 'two')
2448 g:TsrFunc2Args = []
2449 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2450 assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
2451 bw!
2452
2453 # Test for using a script-local function name
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002454 def LocalTsrFunc(findstart: number, base: string): any
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002455 add(g:LocalTsrFuncArgs, [findstart, base])
2456 return findstart ? 0 : []
2457 enddef
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002458 &thesaurusfunc = LocalTsrFunc
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00002459 new | only
2460 setline(1, 'three')
2461 g:LocalTsrFuncArgs = []
2462 feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
2463 assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
2464 bw!
Yegappan Lakshmanan2172bff2021-12-08 10:46:21 +00002465 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002466 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002467
2468 " cleanup
2469 set thesaurusfunc&
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00002470 delfunc TsrFunc1
2471 delfunc TsrFunc2
2472 unlet g:TsrFunc1Args g:TsrFunc2Args
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002473 %bw!
2474endfunc
2475
Christian Brabandtac72c212022-04-07 21:00:53 +01002476func FooBarComplete(findstart, base)
2477 if a:findstart
2478 return col('.') - 1
2479 else
2480 return ["Foo", "Bar", "}"]
2481 endif
2482endfunc
2483
2484func Test_complete_smartindent()
2485 new
2486 setlocal smartindent completefunc=FooBarComplete
2487
2488 exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
2489 let result = getline(1,'$')
2490 call assert_equal(['', '{','}',''], result)
2491 bw!
2492 delfunction! FooBarComplete
2493endfunc
2494
Bram Moolenaarf12129f2022-07-01 19:58:30 +01002495func Test_complete_overrun()
2496 " this was going past the end of the copied text
2497 new
2498 sil norm si”0s0 
2499 bwipe!
2500endfunc
2501
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002502func Test_infercase_very_long_line()
2503 " this was truncating the line when inferring case
2504 new
2505 let longLine = "blah "->repeat(300)
2506 let verylongLine = "blah "->repeat(400)
2507 call setline(1, verylongLine)
2508 call setline(2, longLine)
2509 set ic infercase
2510 exe "normal 2Go\<C-X>\<C-L>\<Esc>"
2511 call assert_equal(longLine, getline(3))
2512
Bram Moolenaarb9e71732022-07-23 06:53:08 +01002513 " check that the too long text is NUL terminated
2514 %del
2515 norm o
2516 norm 1987ax
2517 exec "norm ox\<C-X>\<C-L>"
2518 call assert_equal(repeat('x', 1987), getline(3))
2519
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002520 bwipe!
2521 set noic noinfercase
2522endfunc
2523
Bram Moolenaarbaefde12022-07-07 19:59:49 +01002524func Test_ins_complete_add()
2525 " this was reading past the end of allocated memory
2526 new
2527 norm o
2528 norm 7o€€
2529 sil! norm o
2530
2531 bwipe!
2532endfunc
2533
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002534func Test_ins_complete_end_of_line()
2535 " this was reading past the end of the line
Bram Moolenaar94722c52023-01-28 19:19:03 +00002536 new
Bram Moolenaara6f9e302022-07-28 21:51:37 +01002537 norm 8o€ý 
2538 sil! norm o
2539
2540 bwipe!
2541endfunc
Bram Moolenaarcaea6642022-07-07 19:42:04 +01002542
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002543func s:Tagfunc(t,f,o)
2544 bwipe!
2545 return []
2546endfunc
2547
2548" This was using freed memory, since 'complete' was in a wiped out buffer.
2549" Also using a window that was closed.
2550func Test_tagfunc_wipes_out_buffer()
2551 new
2552 set complete=.,t,w,b,u,i
2553 se tagfunc=s:Tagfunc
2554 sil norm i
2555
2556 bwipe!
2557endfunc
2558
Bram Moolenaar6ac2e432023-03-31 19:32:29 +01002559func Test_ins_complete_popup_position()
2560 CheckScreendump
2561
2562 let lines =<< trim END
2563 vim9script
2564 set nowrap
2565 setline(1, ['one', 'two', 'this is line ', 'four'])
2566 prop_type_add('test', {highlight: 'Error'})
2567 prop_add(3, 0, {
2568 text_align: 'above',
2569 text: 'The quick brown fox jumps over the lazy dog',
2570 type: 'test'
2571 })
2572 END
2573 call writefile(lines, 'XinsPopup', 'D')
2574 let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
2575
2576 call term_sendkeys(buf, "3GA\<C-N>")
2577 call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
2578
2579 call StopVimInTerminal(buf)
2580endfunc
2581
LemonBoy69fb5af2023-10-11 21:55:56 +02002582func GetCompleteInfo()
2583 let g:compl_info = complete_info()
2584 return ''
2585endfunc
2586
nwounkn2e3cd522023-10-17 11:05:38 +02002587func Test_completion_restart()
2588 new
2589 set complete=. completeopt=menuone backspace=2
2590 call setline(1, 'workhorse workhorse')
2591 exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
2592 call assert_equal(1, len(g:compl_info['items']))
2593 call assert_equal('workhorse', g:compl_info['items'][0]['word'])
2594 set complete& completeopt& backspace&
2595 bwipe!
2596endfunc
2597
LemonBoy69fb5af2023-10-11 21:55:56 +02002598func Test_complete_info_index()
2599 new
2600 call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
2601 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2602
2603 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2604
2605 set completeopt=menu,preview
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002606 " Search forward
LemonBoy69fb5af2023-10-11 21:55:56 +02002607 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2608 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002609 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002610 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2611 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002612 call assert_equal(6 , len(g:compl_info['items']))
2613 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2614 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2615 call assert_equal(6 , len(g:compl_info['items']))
2616 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2617 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2618 call assert_equal(6 , len(g:compl_info['items']))
2619 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2620 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2621 call assert_equal(6 , len(g:compl_info['items']))
2622 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2623 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2624 call assert_equal(6 , len(g:compl_info['items']))
2625 " Search forward: unselected item
2626 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2627 call assert_equal(6 , len(g:compl_info['items']))
2628 call assert_equal(-1 , g:compl_info['selected'])
LemonBoy69fb5af2023-10-11 21:55:56 +02002629
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002630 " Search backward
LemonBoy69fb5af2023-10-11 21:55:56 +02002631 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2632 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002633 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002634 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2635 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002636 call assert_equal(6 , len(g:compl_info['items']))
2637 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2638 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2639 call assert_equal(6 , len(g:compl_info['items']))
2640 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2641 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2642 call assert_equal(6 , len(g:compl_info['items']))
2643 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2644 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2645 call assert_equal(6 , len(g:compl_info['items']))
2646 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2647 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2648 call assert_equal(6 , len(g:compl_info['items']))
2649 " search backwards: unselected item
2650 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2651 call assert_equal(6 , len(g:compl_info['items']))
2652 call assert_equal(-1 , g:compl_info['selected'])
2653
2654 " switch direction: forwards, then backwards
2655 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2656 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2657 call assert_equal(6 , len(g:compl_info['items']))
2658 " switch direction: forwards, then backwards, then forwards again
2659 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2660 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2661 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2662 call assert_equal(6 , len(g:compl_info['items']))
2663
2664 " switch direction: backwards, then forwards
2665 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2666 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2667 call assert_equal(6 , len(g:compl_info['items']))
2668 " switch direction: backwards, then forwards, then backwards again
2669 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2670 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2671 call assert_equal(6 , len(g:compl_info['items']))
LemonBoy69fb5af2023-10-11 21:55:56 +02002672
2673 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2674 set completeopt+=noselect
2675 " Search forward.
2676 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2677 call assert_equal(-1, g:compl_info['selected'])
2678
2679 " Search backward.
2680 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2681 call assert_equal(-1, g:compl_info['selected'])
2682
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002683 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
Girish Palya8950bf72024-03-20 20:07:29 +01002684 call assert_equal(5, g:compl_info['selected'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002685 call assert_equal(6 , len(g:compl_info['items']))
2686 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2687 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 +02002688 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002689 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandt57f9ce12023-11-04 09:58:14 +01002690 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2691 call assert_equal(-1, g:compl_info['selected'])
2692 call assert_equal(6 , len(g:compl_info['items']))
Christian Brabandtdaef8c72023-10-27 19:16:26 +02002693
LemonBoy69fb5af2023-10-11 21:55:56 +02002694 set completeopt&
2695 bwipe!
2696endfunc
Bram Moolenaar0ff01832022-09-24 19:20:30 +01002697
Christian Brabandtfef66302024-01-29 21:46:58 +01002698func Test_complete_changed_complete_info()
2699 CheckRunVimInTerminal
2700 " this used to crash vim, see #13929
2701 let lines =<< trim END
2702 set completeopt=menuone
2703 autocmd CompleteChanged * call complete_info(['items'])
2704 call feedkeys("iii\<cr>\<c-p>")
2705 END
2706 call writefile(lines, 'Xsegfault', 'D')
2707 let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
2708 call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
2709 call StopVimInTerminal(buf)
2710endfunc
2711
zeertzjq0a419e02024-04-02 19:01:14 +02002712func Test_completefunc_first_call_complete_add()
2713 new
2714
2715 func Complete(findstart, base) abort
2716 if a:findstart
2717 let col = col('.')
2718 call complete_add('#')
2719 return col - 1
2720 else
2721 return []
2722 endif
2723 endfunc
2724
2725 set completeopt=longest completefunc=Complete
2726 " This used to cause heap-buffer-overflow
2727 call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:')
2728
2729 delfunc Complete
2730 set completeopt& completefunc&
2731 bwipe!
2732endfunc
2733
glepnira218cc62024-06-03 19:32:39 +02002734func Test_complete_fuzzy_match()
2735 func OnPumChange()
2736 let g:item = get(v:event, 'completed_item', {})
2737 let g:word = get(g:item, 'word', v:null)
glepnirc0b7ca42025-02-13 20:27:44 +01002738 let g:abbr = get(g:item, 'abbr', v:null)
2739 let g:selected = get(complete_info(['selected']), 'selected')
glepnira218cc62024-06-03 19:32:39 +02002740 endfunction
2741
2742 augroup AAAAA_Group
2743 au!
2744 autocmd CompleteChanged * :call OnPumChange()
2745 augroup END
2746
2747 func Omni_test(findstart, base)
2748 if a:findstart
2749 return col(".")
2750 endif
glepnirf400a0c2025-01-23 19:55:14 +01002751 return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}, #{word: "你好吗"}, #{word: "我好"}]
glepnira218cc62024-06-03 19:32:39 +02002752 endfunc
zeertzjq551d8c32024-06-05 19:53:32 +02002753
glepnira218cc62024-06-03 19:32:39 +02002754 new
2755 set omnifunc=Omni_test
2756 set completeopt+=noinsert,fuzzy
2757 call feedkeys("Gi\<C-x>\<C-o>", 'tx')
2758 call assert_equal('foo', g:word)
2759 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2760 call assert_equal('fooBaz', g:word)
2761 call feedkeys("S\<C-x>\<C-o>fa", 'tx')
2762 call assert_equal('foobar', g:word)
2763 " select next
2764 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2765 call assert_equal('foobar', g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002766 " can cyclically select next
glepnira218cc62024-06-03 19:32:39 +02002767 call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx')
2768 call assert_equal(v:null, g:word)
2769 " select prev
2770 call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx')
2771 call assert_equal(v:null, g:word)
zeertzjq551d8c32024-06-05 19:53:32 +02002772 " can cyclically select prev
glepnira218cc62024-06-03 19:32:39 +02002773 call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx')
2774 call assert_equal('fooBaz', g:word)
2775
glepnirf94c9c42024-06-14 21:11:56 +02002776 func Comp()
2777 call complete(col('.'), ["fooBaz", "foobar", "foobala"])
2778 return ''
2779 endfunc
2780 call feedkeys("i\<C-R>=Comp()\<CR>", 'tx')
2781 call assert_equal('fooBaz', g:word)
2782
glepnira218cc62024-06-03 19:32:39 +02002783 " respect noselect
2784 set completeopt+=noselect
2785 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2786 call assert_equal(v:null, g:word)
2787 call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
2788 call assert_equal('fooBaz', g:word)
2789
zeertzjqacc87462024-06-16 08:44:05 +02002790 " avoid breaking default completion behavior
glepniraced8c22024-06-15 15:13:05 +02002791 set completeopt=fuzzy,menu
2792 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002793 " Use "!" flag of feedkeys() so that ex_normal_busy is not set and
2794 " ins_compl_check_keys() is not skipped.
2795 " Add a "0" after the <Esc> to avoid waiting for an escape sequence.
2796 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002797 call assert_equal('hello help hero hello', getline('.'))
2798 set completeopt+=noinsert
2799 call setline(1, ['hello help hero h'])
zeertzjqacc87462024-06-16 08:44:05 +02002800 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
glepniraced8c22024-06-15 15:13:05 +02002801 call assert_equal('hello help hero h', getline('.'))
2802
glepnir8159fb12024-07-17 20:32:54 +02002803 set completeopt-=noinsert
2804 call setline(1, ['xyz yxz x'])
2805 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2806 call assert_equal('xyz yxz xyz', getline('.'))
2807 " can fuzzy get yxz when use Ctrl-N twice
2808 call setline(1, ['xyz yxz x'])
2809 call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2810 call assert_equal('xyz yxz yxz', getline('.'))
2811
2812 call setline(1, ['你好 你'])
2813 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2814 call assert_equal('你好 你好', getline('.'))
2815 call setline(1, ['你的 我的 的'])
2816 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2817 call assert_equal('你的 我的 你的', getline('.'))
2818 " can fuzzy get multiple-byte word when use Ctrl-N twice
2819 call setline(1, ['你的 我的 的'])
2820 call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2821 call assert_equal('你的 我的 我的', getline('.'))
2822
2823 " respect wrapscan
2824 set nowrapscan
2825 call setline(1, ["xyz", "yxz", ""])
2826 call cursor(3, 1)
2827 call feedkeys("Sy\<C-X>\<C-N>\<Esc>0", 'tx!')
2828 call assert_equal('y', getline('.'))
2829 set wrapscan
2830 call feedkeys("Sy\<C-X>\<C-N>\<Esc>0", 'tx!')
2831 call assert_equal('xyz', getline('.'))
2832
2833 " fuzzy on file
2834 call writefile([''], 'fobar', 'D')
2835 call writefile([''], 'foobar', 'D')
2836 call setline(1, ['fob'])
2837 call cursor(1, 1)
2838 call feedkeys("A\<C-X>\<C-f>\<Esc>0", 'tx!')
2839 call assert_equal('fobar', getline('.'))
2840 call feedkeys("Sfob\<C-X>\<C-f>\<C-N>\<Esc>0", 'tx!')
2841 call assert_equal('foobar', getline('.'))
glepnir0be03e12024-07-19 16:45:05 +02002842 call feedkeys("S../\<C-X>\<C-f>\<Esc>0", 'tx!')
2843 call assert_match('../*', getline('.'))
2844 call feedkeys("S../td\<C-X>\<C-f>\<Esc>0", 'tx!')
2845 call assert_match('../testdir', getline('.'))
glepnir8159fb12024-07-17 20:32:54 +02002846
2847 " can get completion from other buffer
2848 set completeopt=fuzzy,menu,menuone
2849 vnew
2850 call setline(1, ["completeness,", "compatibility", "Composite", "Omnipotent"])
2851 wincmd p
2852 call feedkeys("Somp\<C-N>\<Esc>0", 'tx!')
2853 call assert_equal('completeness', getline('.'))
2854 call feedkeys("Somp\<C-N>\<C-N>\<Esc>0", 'tx!')
2855 call assert_equal('compatibility', getline('.'))
2856 call feedkeys("Somp\<C-P>\<Esc>0", 'tx!')
2857 call assert_equal('Omnipotent', getline('.'))
2858 call feedkeys("Somp\<C-P>\<C-P>\<Esc>0", 'tx!')
2859 call assert_equal('Composite', getline('.'))
glepnir0be03e12024-07-19 16:45:05 +02002860 call feedkeys("S omp\<C-N>\<Esc>0", 'tx!')
2861 call assert_equal(' completeness', getline('.'))
glepnir8159fb12024-07-17 20:32:54 +02002862
2863 " fuzzy on whole line completion
2864 call setline(1, ["world is on fire", "no one can save me but you", 'user can execute', ''])
2865 call cursor(4, 1)
2866 call feedkeys("Swio\<C-X>\<C-L>\<Esc>0", 'tx!')
2867 call assert_equal('world is on fire', getline('.'))
2868 call feedkeys("Su\<C-X>\<C-L>\<C-P>\<Esc>0", 'tx!')
2869 call assert_equal('no one can save me but you', getline('.'))
2870
glepnir7cfe6932024-09-15 20:06:28 +02002871 " issue #15412
2872 call setline(1, ['alpha bravio charlie'])
2873 call feedkeys("Salpha\<C-X>\<C-N>\<Esc>0", 'tx!')
2874 call assert_equal('alpha bravio', getline('.'))
2875 call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!')
2876 call assert_equal('alpha', getline('.'))
2877 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2878 call assert_equal('alpha bravio', getline('.'))
2879 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2880 call assert_equal('alpha bravio charlie', getline('.'))
2881
2882 set complete-=i
2883 call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!')
2884 call assert_equal('alpha', getline('.'))
2885 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2886 call assert_equal('alpha bravio', getline('.'))
2887 call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
2888 call assert_equal('alpha bravio charlie', getline('.'))
2889
2890 call setline(1, ['alpha bravio charlie', 'alpha another'])
2891 call feedkeys("Salpha\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2892 call assert_equal('alpha another', getline('.'))
2893 call setline(1, ['你好 我好', '你好 他好'])
2894 call feedkeys("S你好\<C-X>\<C-N>\<Esc>0", 'tx!')
2895 call assert_equal('你好 我好', getline('.'))
2896 call feedkeys("S你好\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!')
2897 call assert_equal('你好 他好', getline('.'))
2898
glepnir753794b2024-08-20 19:58:44 +02002899 " issue #15526
2900 set completeopt=fuzzy,menuone,menu,noselect
2901 call setline(1, ['Text', 'ToText', ''])
glepnir7cfe6932024-09-15 20:06:28 +02002902 call cursor(3, 1)
glepnir753794b2024-08-20 19:58:44 +02002903 call feedkeys("STe\<C-X>\<C-N>x\<CR>\<Esc>0", 'tx!')
glepnir07f0dbe2025-02-18 20:27:30 +01002904 call assert_equal('Tex', getline(line('.') - 1))
glepnir753794b2024-08-20 19:58:44 +02002905
glepnirf400a0c2025-01-23 19:55:14 +01002906 " test case for nosort option
2907 set cot=menuone,menu,noinsert,fuzzy,nosort
zeertzjqd65aa1b2025-01-25 15:29:03 +01002908 " "fooBaz" should have a higher score when the leader is "fb".
2909 " With "nosort", "foobar" should still be shown first in the popup menu.
glepnirf400a0c2025-01-23 19:55:14 +01002910 call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2911 call assert_equal('foobar', g:word)
2912 call feedkeys("S\<C-x>\<C-o>好", 'tx')
2913 call assert_equal("你好吗", g:word)
2914
2915 set cot+=noselect
2916 call feedkeys("S\<C-x>\<C-o>好", 'tx')
2917 call assert_equal(v:null, g:word)
2918 call feedkeys("S\<C-x>\<C-o>好\<C-N>", 'tx')
2919 call assert_equal('你好吗', g:word)
2920
zeertzjqd65aa1b2025-01-25 15:29:03 +01002921 " "nosort" shouldn't enable fuzzy filtering when "fuzzy" isn't present.
2922 set cot=menuone,noinsert,nosort
2923 call feedkeys("S\<C-x>\<C-o>fooB\<C-Y>", 'tx')
2924 call assert_equal('fooBaz', getline('.'))
2925
glepnirc0b7ca42025-02-13 20:27:44 +01002926 set cot=menuone,fuzzy,nosort
2927 func CompAnother()
2928 call complete(col('.'), [#{word: "do" }, #{word: "echo"}, #{word: "for (${1:expr1}, ${2:expr2}, ${3:expr3}) {\n\t$0\n}", abbr: "for" }, #{word: "foo"}])
2929 return ''
2930 endfunc
2931 call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>", 'tx')
2932 call assert_equal("for", g:abbr)
2933 call assert_equal(2, g:selected)
2934
2935 set cot+=noinsert
2936 call feedkeys("i\<C-R>=CompAnother()\<CR>f", 'tx')
2937 call assert_equal("for", g:abbr)
2938 call assert_equal(2, g:selected)
2939
glepnir3af0a8d2025-02-20 22:06:16 +01002940 set cot=menu,menuone,noselect,fuzzy
2941 call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>\<C-N>\<C-N>", 'tx')
2942 call assert_equal("foo", g:word)
2943 call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-P>", 'tx')
2944 call assert_equal("foo", g:word)
2945 call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-P>\<C-P>", 'tx')
2946 call assert_equal("for", g:abbr)
2947
glepnira218cc62024-06-03 19:32:39 +02002948 " clean up
2949 set omnifunc=
2950 bw!
glepnir8159fb12024-07-17 20:32:54 +02002951 bw!
glepnira218cc62024-06-03 19:32:39 +02002952 set complete& completeopt&
2953 autocmd! AAAAA_Group
2954 augroup! AAAAA_Group
2955 delfunc OnPumChange
2956 delfunc Omni_test
glepnirf94c9c42024-06-14 21:11:56 +02002957 delfunc Comp
glepnirc0b7ca42025-02-13 20:27:44 +01002958 delfunc CompAnother
zeertzjq551d8c32024-06-05 19:53:32 +02002959 unlet g:item
2960 unlet g:word
glepnirc0b7ca42025-02-13 20:27:44 +01002961 unlet g:selected
2962 unlet g:abbr
glepnira218cc62024-06-03 19:32:39 +02002963endfunc
2964
glepnirb9de1a02024-08-02 19:14:38 +02002965func Test_complete_fuzzy_with_completeslash()
2966 CheckMSWindows
2967
2968 call writefile([''], 'fobar', 'D')
2969 let orig_shellslash = &shellslash
2970 set cpt&
2971 new
2972 set completeopt+=fuzzy
2973 set noshellslash
2974
2975 " Test with completeslash unset
2976 set completeslash=
2977 call setline(1, ['.\fob'])
2978 call feedkeys("A\<C-X>\<C-F>\<Esc>0", 'tx!')
2979 call assert_equal('.\fobar', getline('.'))
2980
2981 " Test with completeslash=backslash
2982 set completeslash=backslash
2983 call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!')
2984 call assert_equal('.\fobar', getline('.'))
2985
2986 " Test with completeslash=slash
2987 set completeslash=slash
2988 call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!')
2989 call assert_equal('./fobar', getline('.'))
2990
2991 " Reset and clean up
2992 let &shellslash = orig_shellslash
2993 set completeslash=
2994 %bw!
2995endfunc
2996
zeertzjq8e567472024-06-14 20:04:42 +02002997" Check that tie breaking is stable for completeopt+=fuzzy (which should
2998" behave the same on different platforms).
2999func Test_complete_fuzzy_match_tie()
3000 new
3001 set completeopt+=fuzzy,noselect
3002 call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', ''])
3003
3004 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx')
3005 call assert_equal('aaabbccc', getline('.'))
3006 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx')
3007 call assert_equal('aaabbCCC', getline('.'))
3008 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
3009 call assert_equal('aaabbcccc', getline('.'))
3010 call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
3011 call assert_equal('aaabbCCCC', getline('.'))
3012
3013 bwipe!
3014 set completeopt&
3015endfunc
3016
Christian Brabandt13032a42024-07-28 21:16:48 +02003017func Test_complete_backwards_default()
3018 new
3019 call append(1, ['foobar', 'foobaz'])
3020 new
3021 call feedkeys("i\<c-p>", 'tx')
3022 call assert_equal('foobaz', getline('.'))
3023 bw!
3024 bw!
3025endfunc
3026
glepnird4088ed2024-12-31 10:55:22 +01003027func Test_complete_info_matches()
3028 let g:what = ['matches']
3029 func ShownInfo()
3030 let g:compl_info = complete_info(g:what)
3031 return ''
3032 endfunc
3033 set completeopt+=noinsert
3034
3035 new
3036 call setline(1, ['aaa', 'aab', 'aba', 'abb'])
3037 inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
3038
3039 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
3040 call assert_equal([
3041 \ {'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
3042 \ {'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
3043 \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
3044 \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
3045 \], g:compl_info['matches'])
3046
3047 call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
3048 call assert_equal([
3049 \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
3050 \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
3051 \], g:compl_info['matches'])
3052
3053 " items and matches both in what
3054 let g:what = ['items', 'matches']
3055 call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
3056 call assert_equal([
3057 \ {'word': 'aaa', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
3058 \ {'word': 'aab', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
3059 \ {'word': 'aba', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
3060 \ {'word': 'abb', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
3061 \], g:compl_info['items'])
3062 call assert_false(has_key(g:compl_info, 'matches'))
3063
3064 bw!
glepnird4088ed2024-12-31 10:55:22 +01003065 unlet g:what
3066 delfunc ShownInfo
3067 set cot&
3068endfunc
3069
glepnir037b0282025-01-16 14:37:44 +01003070func Test_complete_info_completed()
3071 func ShownInfo()
3072 let g:compl_info = complete_info(['completed'])
3073 return ''
3074 endfunc
3075 set completeopt+=noinsert
3076
3077 new
3078 call setline(1, ['aaa', 'aab', 'aba', 'abb'])
3079 inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
3080
3081 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
3082 call assert_equal({'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
3083
3084 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
3085 call assert_equal({'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
3086
3087 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
3088 call assert_equal({'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
3089
3090 set completeopt+=noselect
3091 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
3092 call assert_equal({}, g:compl_info)
3093
3094 bw!
glepnir037b0282025-01-16 14:37:44 +01003095 delfunc ShownInfo
3096 set cot&
3097endfunc
3098
glepniredd4ac32025-01-29 18:53:51 +01003099function Test_completeopt_preinsert()
3100 func Omni_test(findstart, base)
3101 if a:findstart
3102 return col(".")
3103 endif
3104 return [#{word: "fobar"}, #{word: "foobar"}, #{word: "你的"}, #{word: "你好世界"}]
3105 endfunc
3106 set omnifunc=Omni_test
3107 set completeopt=menu,menuone,preinsert
3108
3109 new
3110 call feedkeys("S\<C-X>\<C-O>f", 'tx')
3111 call assert_equal("fobar", getline('.'))
3112 call feedkeys("\<C-E>\<ESC>", 'tx')
3113
3114 call feedkeys("S\<C-X>\<C-O>foo", 'tx')
3115 call assert_equal("foobar", getline('.'))
3116 call feedkeys("\<C-E>\<ESC>", 'tx')
3117
3118 call feedkeys("S\<C-X>\<C-O>foo\<BS>\<BS>\<BS>", 'tx')
3119 call assert_equal("", getline('.'))
3120 call feedkeys("\<C-E>\<ESC>", 'tx')
3121
3122 " delete a character and input new leader
3123 call feedkeys("S\<C-X>\<C-O>foo\<BS>b", 'tx')
3124 call assert_equal("fobar", getline('.'))
3125 call feedkeys("\<C-E>\<ESC>", 'tx')
3126
3127 " delete preinsert when prepare completion
3128 call feedkeys("S\<C-X>\<C-O>f\<Space>", 'tx')
3129 call assert_equal("f ", getline('.'))
3130 call feedkeys("\<C-E>\<ESC>", 'tx')
3131
3132 call feedkeys("S\<C-X>\<C-O>你", 'tx')
3133 call assert_equal("你的", getline('.'))
3134 call feedkeys("\<C-E>\<ESC>", 'tx')
3135
3136 call feedkeys("S\<C-X>\<C-O>你好", 'tx')
3137 call assert_equal("你好世界", getline('.'))
3138 call feedkeys("\<C-E>\<ESC>", 'tx')
3139
3140 call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f", 'tx')
3141 call assert_equal("hello fobar wo", getline('.'))
3142 call feedkeys("\<C-E>\<ESC>", 'tx')
3143
3144 call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f\<BS>", 'tx')
3145 call assert_equal("hello wo", getline('.'))
3146 call feedkeys("\<C-E>\<ESC>", 'tx')
3147
3148 call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo", 'tx')
3149 call assert_equal("hello foobar wo", getline('.'))
3150 call feedkeys("\<C-E>\<ESC>", 'tx')
3151
3152 call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo\<BS>b", 'tx')
3153 call assert_equal("hello fobar wo", getline('.'))
3154 call feedkeys("\<C-E>\<ESC>", 'tx')
3155
zeertzjq8297e2c2025-01-30 11:04:47 +01003156 " confirm
glepniredd4ac32025-01-29 18:53:51 +01003157 call feedkeys("S\<C-X>\<C-O>f\<C-Y>", 'tx')
3158 call assert_equal("fobar", getline('.'))
3159 call assert_equal(5, col('.'))
3160
3161 " cancel
3162 call feedkeys("S\<C-X>\<C-O>fo\<C-E>", 'tx')
3163 call assert_equal("fo", getline('.'))
3164 call assert_equal(2, col('.'))
3165
3166 call feedkeys("S hello hero\<CR>h\<C-X>\<C-N>", 'tx')
3167 call assert_equal("hello", getline('.'))
3168 call assert_equal(1, col('.'))
3169
3170 call feedkeys("Sh\<C-X>\<C-N>\<C-Y>", 'tx')
3171 call assert_equal("hello", getline('.'))
3172 call assert_equal(5, col('.'))
3173
3174 " delete preinsert part
3175 call feedkeys("S\<C-X>\<C-O>fo ", 'tx')
3176 call assert_equal("fo ", getline('.'))
3177 call assert_equal(3, col('.'))
3178
glepnir001c26c2025-02-02 09:36:22 +01003179 call feedkeys("She\<C-X>\<C-N>\<C-U>", 'tx')
3180 call assert_equal("", getline('.'))
3181 call assert_equal(1, col('.'))
3182
3183 call feedkeys("She\<C-X>\<C-N>\<C-W>", 'tx')
3184 call assert_equal("", getline('.'))
3185 call assert_equal(1, col('.'))
3186
glepniredd4ac32025-01-29 18:53:51 +01003187 " whole line
3188 call feedkeys("Shello hero\<CR>\<C-X>\<C-L>", 'tx')
3189 call assert_equal("hello hero", getline('.'))
3190 call assert_equal(1, col('.'))
3191
3192 call feedkeys("Shello hero\<CR>he\<C-X>\<C-L>", 'tx')
3193 call assert_equal("hello hero", getline('.'))
3194 call assert_equal(2, col('.'))
3195
glepnirbfb4eea2025-01-31 15:28:29 +01003196 call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>er", 'tx')
3197 call assert_equal("hero", getline('.'))
3198 call assert_equal(3, col('.'))
3199
glepniredd4ac32025-01-29 18:53:51 +01003200 " can not work with fuzzy
3201 set cot+=fuzzy
3202 call feedkeys("S\<C-X>\<C-O>", 'tx')
3203 call assert_equal("fobar", getline('.'))
3204 call assert_equal(5, col('.'))
3205
3206 " test for fuzzy and noinsert
3207 set cot+=noinsert
3208 call feedkeys("S\<C-X>\<C-O>fb", 'tx')
3209 call assert_equal("fb", getline('.'))
3210 call assert_equal(2, col('.'))
3211
3212 call feedkeys("S\<C-X>\<C-O>你", 'tx')
3213 call assert_equal("", getline('.'))
3214 call assert_equal(1, col('.'))
3215
3216 call feedkeys("S\<C-X>\<C-O>fb\<C-Y>", 'tx')
3217 call assert_equal("fobar", getline('.'))
3218 call assert_equal(5, col('.'))
3219
glepnir52fd8672025-02-20 22:13:24 +01003220 set cot=preinsert
3221 call feedkeys("Sfoo1 foo2\<CR>f\<C-X>\<C-N>bar", 'tx')
3222 call assert_equal("fbar", getline('.'))
3223 call assert_equal(4, col('.'))
3224
glepniredd4ac32025-01-29 18:53:51 +01003225 bw!
glepniredd4ac32025-01-29 18:53:51 +01003226 set cot&
3227 set omnifunc&
3228 delfunc Omni_test
glepniredd4ac32025-01-29 18:53:51 +01003229endfunc
3230
zeertzjq060e6552025-02-21 20:06:26 +01003231" Check that mark positions are correct after triggering multiline completion.
3232func Test_complete_multiline_marks()
3233 func Omni_test(findstart, base)
3234 if a:findstart
3235 return col(".")
3236 endif
3237 return [
3238 \ #{word: "func ()\n\t\nend"},
3239 \ #{word: "foobar"},
3240 \ #{word: "你好\n\t\n我好"}
3241 \ ]
3242 endfunc
3243 set omnifunc=Omni_test
3244
3245 new
3246 let lines = mapnew(range(10), 'string(v:val)')
3247 call setline(1, lines)
3248 call setpos("'a", [0, 3, 1, 0])
3249
3250 call feedkeys("A \<C-X>\<C-O>\<C-E>\<BS>", 'tx')
3251 call assert_equal(lines, getline(1, '$'))
3252 call assert_equal([0, 3, 1, 0], getpos("'a"))
3253
3254 call feedkeys("A \<C-X>\<C-O>\<C-N>\<C-E>\<BS>", 'tx')
3255 call assert_equal(lines, getline(1, '$'))
3256 call assert_equal([0, 3, 1, 0], getpos("'a"))
3257
3258 call feedkeys("A \<C-X>\<C-O>\<C-N>\<C-N>\<C-E>\<BS>", 'tx')
3259 call assert_equal(lines, getline(1, '$'))
3260 call assert_equal([0, 3, 1, 0], getpos("'a"))
3261
3262 call feedkeys("A \<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-E>\<BS>", 'tx')
3263 call assert_equal(lines, getline(1, '$'))
3264 call assert_equal([0, 3, 1, 0], getpos("'a"))
3265
3266 call feedkeys("A \<C-X>\<C-O>\<C-Y>", 'tx')
3267 call assert_equal(['0 func ()', "\t", 'end'] + lines[1:], getline(1, '$'))
3268 call assert_equal([0, 5, 1, 0], getpos("'a"))
3269
3270 bw!
3271 set omnifunc&
3272 delfunc Omni_test
3273endfunc
3274
Christian Brabandtfef66302024-01-29 21:46:58 +01003275" vim: shiftwidth=2 sts=2 expandtab nofoldenable