comparison src/testdir/test_tagfunc.vim @ 26618:b531c26f728b v8.2.3838

patch 8.2.3838: cannot use script-local function for setting *func options Commit: https://github.com/vim/vim/commit/db1a410b610b2c1941311acc57dcc4afec20720e Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Dec 17 16:21:20 2021 +0000 patch 8.2.3838: cannot use script-local function for setting *func options Problem: Cannot use script-local function for setting *func options. Solution: Use the script context. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/9362)
author Bram Moolenaar <Bram@vim.org>
date Fri, 17 Dec 2021 17:30:04 +0100
parents 6fed1f1bc282
children 4c16acb2525f
comparison
equal deleted inserted replaced
26617:86ca0d6a6a34 26618:b531c26f728b
265 call assert_equal([], g:TagFunc2Args) 265 call assert_equal([], g:TagFunc2Args)
266 bw! 266 bw!
267 END 267 END
268 call CheckLegacyAndVim9Success(lines) 268 call CheckLegacyAndVim9Success(lines)
269 269
270 " Test for using a script-local function name
271 func s:TagFunc3(pat, flags, info)
272 let g:TagFunc3Args = [a:pat, a:flags, a:info]
273 return v:null
274 endfunc
275 set tagfunc=s:TagFunc3
276 new
277 let g:TagFunc3Args = []
278 call assert_fails('tag a21', 'E433:')
279 call assert_equal(['a21', '', {}], g:TagFunc3Args)
280 bw!
281 let &tagfunc = 's:TagFunc3'
282 new
283 let g:TagFunc3Args = []
284 call assert_fails('tag a22', 'E433:')
285 call assert_equal(['a22', '', {}], g:TagFunc3Args)
286 bw!
287 delfunc s:TagFunc3
288
289 " invalid return value
270 let &tagfunc = "{a -> 'abc'}" 290 let &tagfunc = "{a -> 'abc'}"
271 call assert_fails("echo taglist('a')", "E987:") 291 call assert_fails("echo taglist('a')", "E987:")
272 292
273 " Using Vim9 lambda expression in legacy context should fail 293 " Using Vim9 lambda expression in legacy context should fail
274 set tagfunc=(a,\ b,\ c)\ =>\ g:TagFunc1(21,\ a,\ b,\ c) 294 set tagfunc=(a,\ b,\ c)\ =>\ g:TagFunc1(21,\ a,\ b,\ c)
275 new | only 295 new
276 let g:TagFunc1Args = [] 296 let g:TagFunc1Args = []
277 call assert_fails("tag a17", "E117:") 297 call assert_fails("tag a17", "E117:")
278 call assert_equal([], g:TagFunc1Args) 298 call assert_equal([], g:TagFunc1Args)
299 bw!
279 300
280 " Test for using a script local function 301 " Test for using a script local function
281 set tagfunc=<SID>ScriptLocalTagFunc 302 set tagfunc=<SID>ScriptLocalTagFunc
282 new | only 303 new
283 let g:ScriptLocalFuncArgs = [] 304 let g:ScriptLocalFuncArgs = []
284 call assert_fails('tag a15', 'E433:') 305 call assert_fails('tag a15', 'E433:')
285 call assert_equal(['a15', '', {}], g:ScriptLocalFuncArgs) 306 call assert_equal(['a15', '', {}], g:ScriptLocalFuncArgs)
307 bw!
286 308
287 " Test for using a script local funcref variable 309 " Test for using a script local funcref variable
288 let Fn = function("s:ScriptLocalTagFunc") 310 let Fn = function("s:ScriptLocalTagFunc")
289 let &tagfunc= Fn 311 let &tagfunc= Fn
290 new | only 312 new
291 let g:ScriptLocalFuncArgs = [] 313 let g:ScriptLocalFuncArgs = []
292 call assert_fails('tag a16', 'E433:') 314 call assert_fails('tag a16', 'E433:')
293 call assert_equal(['a16', '', {}], g:ScriptLocalFuncArgs) 315 call assert_equal(['a16', '', {}], g:ScriptLocalFuncArgs)
316 bw!
294 317
295 " Test for using a string(script local funcref variable) 318 " Test for using a string(script local funcref variable)
296 let Fn = function("s:ScriptLocalTagFunc") 319 let Fn = function("s:ScriptLocalTagFunc")
297 let &tagfunc= string(Fn) 320 let &tagfunc= string(Fn)
298 new | only 321 new
299 let g:ScriptLocalFuncArgs = [] 322 let g:ScriptLocalFuncArgs = []
300 call assert_fails('tag a16', 'E433:') 323 call assert_fails('tag a16', 'E433:')
301 call assert_equal(['a16', '', {}], g:ScriptLocalFuncArgs) 324 call assert_equal(['a16', '', {}], g:ScriptLocalFuncArgs)
325 bw!
302 326
303 " set 'tagfunc' to a partial with dict. This used to cause a crash. 327 " set 'tagfunc' to a partial with dict. This used to cause a crash.
304 func SetTagFunc() 328 func SetTagFunc()
305 let params = {'tagfn': function('g:DictTagFunc')} 329 let params = {'tagfn': function('g:DictTagFunc')}
306 let &tagfunc = params.tagfn 330 let &tagfunc = params.tagfn
322 346
323 " Vim9 tests 347 " Vim9 tests
324 let lines =<< trim END 348 let lines =<< trim END
325 vim9script 349 vim9script
326 350
327 # Test for using function() 351 def Vim9tagFunc(callnr: number, pat: string, flags: string, info: dict<any>): any
328 def Vim9tagFunc(val: number, pat: string, flags: string, info: dict<any>): any 352 g:Vim9tagFuncArgs = [callnr, pat, flags, info]
329 g:Vim9tagFuncArgs = [val, pat, flags, info]
330 return null 353 return null
331 enddef 354 enddef
355
356 # Test for using a def function with completefunc
332 set tagfunc=function('Vim9tagFunc',\ [60]) 357 set tagfunc=function('Vim9tagFunc',\ [60])
333 new | only 358 new
334 g:Vim9tagFuncArgs = [] 359 g:Vim9tagFuncArgs = []
335 assert_fails('tag a10', 'E433:') 360 assert_fails('tag a10', 'E433:')
336 assert_equal([60, 'a10', '', {}], g:Vim9tagFuncArgs) 361 assert_equal([60, 'a10', '', {}], g:Vim9tagFuncArgs)
362
363 # Test for using a global function name
364 &tagfunc = g:TagFunc2
365 new
366 g:TagFunc2Args = []
367 assert_fails('tag a11', 'E433:')
368 assert_equal(['a11', '', {}], g:TagFunc2Args)
369 bw!
370
371 # Test for using a script-local function name
372 def s:LocalTagFunc(pat: string, flags: string, info: dict<any> ): any
373 g:LocalTagFuncArgs = [pat, flags, info]
374 return null
375 enddef
376 &tagfunc = s:LocalTagFunc
377 new
378 g:LocalTagFuncArgs = []
379 assert_fails('tag a12', 'E433:')
380 assert_equal(['a12', '', {}], g:LocalTagFuncArgs)
381 bw!
337 END 382 END
338 call CheckScriptSuccess(lines) 383 call CheckScriptSuccess(lines)
339 384
340 " cleanup 385 " cleanup
341 delfunc TagFunc1 386 delfunc TagFunc1
342 delfunc TagFunc2 387 delfunc TagFunc2
343 set tagfunc& 388 set tagfunc&
344 %bw! 389 %bw!
345 endfunc 390 endfunc
346 391
347 func Test_set_tagfunc_on_cmdline()
348 CheckScreendump
349
350 let buf = RunVimInTerminal(' +"set tagfunc=s:Func"', #{rows: 6, wait_for_ruler: 0})
351 call VerifyScreenDump(buf, 'Test_set_tagfunc_on_cmdline', {})
352 call StopVimInTerminal(buf)
353 endfunc
354
355
356 " vim: shiftwidth=2 sts=2 expandtab 392 " vim: shiftwidth=2 sts=2 expandtab