To: vim_dev@googlegroups.com Subject: Patch 8.2.3712 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3712 Problem: Cannot use Vim9 lambda for 'tagfunc'. Solution: Make it work, add more tests. (Yegappan Lakshmanan, closes #9250) Files: runtime/doc/options.txt, src/insexpand.c, src/option.c, src/testdir/test_tagfunc.vim *** ../vim-8.2.3711/runtime/doc/options.txt 2021-11-29 20:39:06.682101619 +0000 --- runtime/doc/options.txt 2021-12-01 10:22:21.312649704 +0000 *************** *** 372,378 **** set opfunc=MyOpFunc set opfunc=function('MyOpFunc') set opfunc=funcref('MyOpFunc') ! let &opfunc = "{t -> MyOpFunc(t)}" < Setting the filetype --- 379,393 ---- set opfunc=MyOpFunc set opfunc=function('MyOpFunc') set opfunc=funcref('MyOpFunc') ! set opfunc={a\ ->\ MyOpFunc(a)} ! " set using a funcref variable ! let Fn = function('MyTagFunc') ! let &tagfunc = string(Fn) ! " set using a lambda expression ! let &tagfunc = "{t -> MyTagFunc(t)}" ! " set using a variable with lambda expression ! let L = {a, b, c -> MyTagFunc(a, b , c)} ! let &tagfunc = string(L) < Setting the filetype *** ../vim-8.2.3711/src/insexpand.c 2021-11-17 15:51:46.421992164 +0000 --- src/insexpand.c 2021-12-01 10:28:37.680470993 +0000 *************** *** 2257,2269 **** } /* ! * Execute user defined complete function 'completefunc' or 'omnifunc', and ! * get matches in "matches". */ static void ! expand_by_function( ! int type, // CTRL_X_OMNI or CTRL_X_FUNCTION ! char_u *base) { list_T *matchlist = NULL; dict_T *matchdict = NULL; --- 2257,2268 ---- } /* ! * Execute user defined complete function 'completefunc', 'omnifunc' or ! * 'thesaurusfunc', and get matches in "matches". ! * "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS. */ static void ! expand_by_function(int type, char_u *base) { list_T *matchlist = NULL; dict_T *matchdict = NULL; *** ../vim-8.2.3711/src/option.c 2021-11-29 20:39:06.678101624 +0000 --- src/option.c 2021-12-01 10:22:21.316649704 +0000 *************** *** 7187,7193 **** return OK; } ! if (*optval == '{' || (STRNCMP(optval, "function(", 9) == 0) || (STRNCMP(optval, "funcref(", 8) == 0)) // Lambda expression or a funcref --- 7187,7193 ---- return OK; } ! if (*optval == '{' || (in_vim9script() && *optval == '(') || (STRNCMP(optval, "function(", 9) == 0) || (STRNCMP(optval, "funcref(", 8) == 0)) // Lambda expression or a funcref *** ../vim-8.2.3711/src/testdir/test_tagfunc.vim 2021-11-24 16:32:50.720422469 +0000 --- src/testdir/test_tagfunc.vim 2021-12-01 10:22:21.316649704 +0000 *************** *** 1,5 **** --- 1,7 ---- " Test 'tagfunc' + source vim9.vim + func TagFunc(pat, flag, info) let g:tagfunc_args = [a:pat, a:flag, a:info] let tags = [] *************** *** 124,155 **** let g:MytagFunc1_args = [a:pat, a:flags, a:info] return v:null endfunc - let g:MytagFunc1_args = [] set tagfunc=function('MytagFunc1') ! call assert_fails('tag abc', 'E433:') ! call assert_equal(['abc', '', {}], g:MytagFunc1_args) " Test for using a funcref() - new func MytagFunc2(pat, flags, info) let g:MytagFunc2_args = [a:pat, a:flags, a:info] return v:null endfunc - let g:MytagFunc2_args = [] set tagfunc=funcref('MytagFunc2') ! call assert_fails('tag def', 'E433:') ! call assert_equal(['def', '', {}], g:MytagFunc2_args) " Test for using a lambda function - new func MytagFunc3(pat, flags, info) let g:MytagFunc3_args = [a:pat, a:flags, a:info] return v:null endfunc let g:MytagFunc3_args = [] ! let &tagfunc= '{a, b, c -> MytagFunc3(a, b, c)}' ! call assert_fails('tag ghi', 'E433:') ! call assert_equal(['ghi', '', {}], g:MytagFunc3_args) " Test for clearing the 'tagfunc' option set tagfunc='' --- 126,198 ---- let g:MytagFunc1_args = [a:pat, a:flags, a:info] return v:null endfunc set tagfunc=function('MytagFunc1') ! new | only ! let g:MytagFunc1_args = [] ! call assert_fails('tag a11', 'E433:') ! call assert_equal(['a11', '', {}], g:MytagFunc1_args) ! ! " Using a funcref variable to set 'tagfunc' ! let Fn = function('MytagFunc1') ! let &tagfunc = string(Fn) ! new | only ! let g:MytagFunc1_args = [] ! call assert_fails('tag a12', 'E433:') ! call assert_equal(['a12', '', {}], g:MytagFunc1_args) ! call assert_fails('let &tagfunc = Fn', 'E729:') " Test for using a funcref() func MytagFunc2(pat, flags, info) let g:MytagFunc2_args = [a:pat, a:flags, a:info] return v:null endfunc set tagfunc=funcref('MytagFunc2') ! new | only ! let g:MytagFunc2_args = [] ! call assert_fails('tag a13', 'E433:') ! call assert_equal(['a13', '', {}], g:MytagFunc2_args) ! ! " Using a funcref variable to set 'tagfunc' ! let Fn = funcref('MytagFunc2') ! let &tagfunc = string(Fn) ! new | only ! let g:MytagFunc2_args = [] ! call assert_fails('tag a14', 'E433:') ! call assert_equal(['a14', '', {}], g:MytagFunc2_args) ! call assert_fails('let &tagfunc = Fn', 'E729:') " Test for using a lambda function func MytagFunc3(pat, flags, info) let g:MytagFunc3_args = [a:pat, a:flags, a:info] return v:null endfunc + set tagfunc={a,\ b,\ c\ ->\ MytagFunc3(a,\ b,\ c)} + new | only + let g:MytagFunc3_args = [] + call assert_fails('tag a15', 'E433:') + call assert_equal(['a15', '', {}], g:MytagFunc3_args) + + " Set 'tagfunc' to a lambda expression + let &tagfunc = '{a, b, c -> MytagFunc3(a, b, c)}' + new | only + let g:MytagFunc3_args = [] + call assert_fails('tag a16', 'E433:') + call assert_equal(['a16', '', {}], g:MytagFunc3_args) + + " Set 'tagfunc' to a variable with a lambda expression + let Lambda = {a, b, c -> MytagFunc3(a, b, c)} + let &tagfunc = string(Lambda) + new | only let g:MytagFunc3_args = [] ! call assert_fails("tag a17", "E433:") ! call assert_equal(['a17', '', {}], g:MytagFunc3_args) ! call assert_fails('let &tagfunc = Lambda', 'E729:') ! ! " Test for using a lambda function with incorrect return value ! let Lambda = {s -> strlen(s)} ! let &tagfunc = string(Lambda) ! new | only ! call assert_fails("tag a17", "E987:") " Test for clearing the 'tagfunc' option set tagfunc='' *************** *** 160,169 **** --- 203,256 ---- let &tagfunc = "{a -> 'abc'}" call assert_fails("echo taglist('a')", "E987:") + " Vim9 tests + let lines =<< trim END + vim9script + + # Test for using function() + def MytagFunc1(pat: string, flags: string, info: dict): any + g:MytagFunc1_args = [pat, flags, info] + return null + enddef + set tagfunc=function('MytagFunc1') + new | only + g:MytagFunc1_args = [] + assert_fails('tag a10', 'E433:') + assert_equal(['a10', '', {}], g:MytagFunc1_args) + + # Test for using a lambda + def MytagFunc2(pat: string, flags: string, info: dict): any + g:MytagFunc2_args = [pat, flags, info] + return null + enddef + &tagfunc = '(a, b, c) => MytagFunc2(a, b, c)' + new | only + g:MytagFunc2_args = [] + assert_fails('tag a20', 'E433:') + assert_equal(['a20', '', {}], g:MytagFunc2_args) + + # Test for using a variable with a lambda expression + var Fn: func = (a, b, c) => MytagFunc2(a, b, c) + &tagfunc = string(Fn) + new | only + g:MytagFunc2_args = [] + assert_fails('tag a30', 'E433:') + assert_equal(['a30', '', {}], g:MytagFunc2_args) + END + call CheckScriptSuccess(lines) + + " Using Vim9 lambda expression in legacy context should fail + set tagfunc=(a,\ b,\ c)\ =>\ g:MytagFunc2(a,\ b,\ c) + new | only + let g:MytagFunc3_args = [] + call assert_fails("tag a17", "E117:") + call assert_equal([], g:MytagFunc3_args) + " cleanup delfunc MytagFunc1 delfunc MytagFunc2 delfunc MytagFunc3 + set tagfunc& %bw! endfunc *** ../vim-8.2.3711/src/version.c 2021-12-01 10:09:12.408191471 +0000 --- src/version.c 2021-12-01 10:24:04.228613206 +0000 *************** *** 755,756 **** --- 755,758 ---- { /* Add new patch number below this line */ + /**/ + 3712, /**/ -- OLD WOMAN: King of the WHO? ARTHUR: The Britons. OLD WOMAN: Who are the Britons? "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///