Skip to content

Commit 05e59e3

Browse files
yegappanbrammool
authored andcommitted
patch 8.2.3712: cannot use Vim9 lambda for 'tagfunc'
Problem: Cannot use Vim9 lambda for 'tagfunc'. Solution: Make it work, add more tests. (Yegappan Lakshmanan, closes #9250)
1 parent 56a8ffd commit 05e59e3

File tree

5 files changed

+114
-18
lines changed

5 files changed

+114
-18
lines changed

runtime/doc/options.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,15 @@ or a function reference or a lambda function. Examples:
379379
set opfunc=MyOpFunc
380380
set opfunc=function('MyOpFunc')
381381
set opfunc=funcref('MyOpFunc')
382-
let &opfunc = "{t -> MyOpFunc(t)}"
382+
set opfunc={a\ ->\ MyOpFunc(a)}
383+
" set using a funcref variable
384+
let Fn = function('MyTagFunc')
385+
let &tagfunc = string(Fn)
386+
" set using a lambda expression
387+
let &tagfunc = "{t -> MyTagFunc(t)}"
388+
" set using a variable with lambda expression
389+
let L = {a, b, c -> MyTagFunc(a, b , c)}
390+
let &tagfunc = string(L)
383391
<
384392

385393
Setting the filetype

src/insexpand.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,13 +2257,12 @@ get_complete_funcname(int type)
22572257
}
22582258

22592259
/*
2260-
* Execute user defined complete function 'completefunc' or 'omnifunc', and
2261-
* get matches in "matches".
2260+
* Execute user defined complete function 'completefunc', 'omnifunc' or
2261+
* 'thesaurusfunc', and get matches in "matches".
2262+
* "type" is either CTRL_X_OMNI or CTRL_X_FUNCTION or CTRL_X_THESAURUS.
22622263
*/
22632264
static void
2264-
expand_by_function(
2265-
int type, // CTRL_X_OMNI or CTRL_X_FUNCTION
2266-
char_u *base)
2265+
expand_by_function(int type, char_u *base)
22672266
{
22682267
list_T *matchlist = NULL;
22692268
dict_T *matchdict = NULL;

src/option.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7187,7 +7187,7 @@ option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
71877187
return OK;
71887188
}
71897189

7190-
if (*optval == '{'
7190+
if (*optval == '{' || (in_vim9script() && *optval == '(')
71917191
|| (STRNCMP(optval, "function(", 9) == 0)
71927192
|| (STRNCMP(optval, "funcref(", 8) == 0))
71937193
// Lambda expression or a funcref

src/testdir/test_tagfunc.vim

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
" Test 'tagfunc'
22

3+
source vim9.vim
4+
35
func TagFunc(pat, flag, info)
46
let g:tagfunc_args = [a:pat, a:flag, a:info]
57
let tags = []
@@ -124,32 +126,73 @@ func Test_tagfunc_callback()
124126
let g:MytagFunc1_args = [a:pat, a:flags, a:info]
125127
return v:null
126128
endfunc
127-
let g:MytagFunc1_args = []
128129
set tagfunc=function('MytagFunc1')
129-
call assert_fails('tag abc', 'E433:')
130-
call assert_equal(['abc', '', {}], g:MytagFunc1_args)
130+
new | only
131+
let g:MytagFunc1_args = []
132+
call assert_fails('tag a11', 'E433:')
133+
call assert_equal(['a11', '', {}], g:MytagFunc1_args)
134+
135+
" Using a funcref variable to set 'tagfunc'
136+
let Fn = function('MytagFunc1')
137+
let &tagfunc = string(Fn)
138+
new | only
139+
let g:MytagFunc1_args = []
140+
call assert_fails('tag a12', 'E433:')
141+
call assert_equal(['a12', '', {}], g:MytagFunc1_args)
142+
call assert_fails('let &tagfunc = Fn', 'E729:')
131143

132144
" Test for using a funcref()
133-
new
134145
func MytagFunc2(pat, flags, info)
135146
let g:MytagFunc2_args = [a:pat, a:flags, a:info]
136147
return v:null
137148
endfunc
138-
let g:MytagFunc2_args = []
139149
set tagfunc=funcref('MytagFunc2')
140-
call assert_fails('tag def', 'E433:')
141-
call assert_equal(['def', '', {}], g:MytagFunc2_args)
150+
new | only
151+
let g:MytagFunc2_args = []
152+
call assert_fails('tag a13', 'E433:')
153+
call assert_equal(['a13', '', {}], g:MytagFunc2_args)
154+
155+
" Using a funcref variable to set 'tagfunc'
156+
let Fn = funcref('MytagFunc2')
157+
let &tagfunc = string(Fn)
158+
new | only
159+
let g:MytagFunc2_args = []
160+
call assert_fails('tag a14', 'E433:')
161+
call assert_equal(['a14', '', {}], g:MytagFunc2_args)
162+
call assert_fails('let &tagfunc = Fn', 'E729:')
142163

143164
" Test for using a lambda function
144-
new
145165
func MytagFunc3(pat, flags, info)
146166
let g:MytagFunc3_args = [a:pat, a:flags, a:info]
147167
return v:null
148168
endfunc
169+
set tagfunc={a,\ b,\ c\ ->\ MytagFunc3(a,\ b,\ c)}
170+
new | only
171+
let g:MytagFunc3_args = []
172+
call assert_fails('tag a15', 'E433:')
173+
call assert_equal(['a15', '', {}], g:MytagFunc3_args)
174+
175+
" Set 'tagfunc' to a lambda expression
176+
let &tagfunc = '{a, b, c -> MytagFunc3(a, b, c)}'
177+
new | only
149178
let g:MytagFunc3_args = []
150-
let &tagfunc= '{a, b, c -> MytagFunc3(a, b, c)}'
151-
call assert_fails('tag ghi', 'E433:')
152-
call assert_equal(['ghi', '', {}], g:MytagFunc3_args)
179+
call assert_fails('tag a16', 'E433:')
180+
call assert_equal(['a16', '', {}], g:MytagFunc3_args)
181+
182+
" Set 'tagfunc' to a variable with a lambda expression
183+
let Lambda = {a, b, c -> MytagFunc3(a, b, c)}
184+
let &tagfunc = string(Lambda)
185+
new | only
186+
let g:MytagFunc3_args = []
187+
call assert_fails("tag a17", "E433:")
188+
call assert_equal(['a17', '', {}], g:MytagFunc3_args)
189+
call assert_fails('let &tagfunc = Lambda', 'E729:')
190+
191+
" Test for using a lambda function with incorrect return value
192+
let Lambda = {s -> strlen(s)}
193+
let &tagfunc = string(Lambda)
194+
new | only
195+
call assert_fails("tag a17", "E987:")
153196

154197
" Test for clearing the 'tagfunc' option
155198
set tagfunc=''
@@ -160,10 +203,54 @@ func Test_tagfunc_callback()
160203
let &tagfunc = "{a -> 'abc'}"
161204
call assert_fails("echo taglist('a')", "E987:")
162205

206+
" Vim9 tests
207+
let lines =<< trim END
208+
vim9script
209+
210+
# Test for using function()
211+
def MytagFunc1(pat: string, flags: string, info: dict<any>): any
212+
g:MytagFunc1_args = [pat, flags, info]
213+
return null
214+
enddef
215+
set tagfunc=function('MytagFunc1')
216+
new | only
217+
g:MytagFunc1_args = []
218+
assert_fails('tag a10', 'E433:')
219+
assert_equal(['a10', '', {}], g:MytagFunc1_args)
220+
221+
# Test for using a lambda
222+
def MytagFunc2(pat: string, flags: string, info: dict<any>): any
223+
g:MytagFunc2_args = [pat, flags, info]
224+
return null
225+
enddef
226+
&tagfunc = '(a, b, c) => MytagFunc2(a, b, c)'
227+
new | only
228+
g:MytagFunc2_args = []
229+
assert_fails('tag a20', 'E433:')
230+
assert_equal(['a20', '', {}], g:MytagFunc2_args)
231+
232+
# Test for using a variable with a lambda expression
233+
var Fn: func = (a, b, c) => MytagFunc2(a, b, c)
234+
&tagfunc = string(Fn)
235+
new | only
236+
g:MytagFunc2_args = []
237+
assert_fails('tag a30', 'E433:')
238+
assert_equal(['a30', '', {}], g:MytagFunc2_args)
239+
END
240+
call CheckScriptSuccess(lines)
241+
242+
" Using Vim9 lambda expression in legacy context should fail
243+
set tagfunc=(a,\ b,\ c)\ =>\ g:MytagFunc2(a,\ b,\ c)
244+
new | only
245+
let g:MytagFunc3_args = []
246+
call assert_fails("tag a17", "E117:")
247+
call assert_equal([], g:MytagFunc3_args)
248+
163249
" cleanup
164250
delfunc MytagFunc1
165251
delfunc MytagFunc2
166252
delfunc MytagFunc3
253+
set tagfunc&
167254
%bw!
168255
endfunc
169256

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,8 @@ static char *(features[]) =
753753

754754
static int included_patches[] =
755755
{ /* Add new patch number below this line */
756+
/**/
757+
3712,
756758
/**/
757759
3711,
758760
/**/

0 commit comments

Comments
 (0)