vim-patch:8.2.3788: lambda for option that is a function may be freed

Problem:    Lambda for option that is a function may be garbage collected.
Solution:   Set a reference in the funcref. (Yegappan Lakshmanan,
            closes vim/vim#9330)

6ae8fae869

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq
2022-11-07 13:37:22 +08:00
parent 8f9ae52784
commit c00d241981
10 changed files with 977 additions and 763 deletions

View File

@@ -30,6 +30,7 @@
#include "nvim/ex_session.h" #include "nvim/ex_session.h"
#include "nvim/getchar.h" #include "nvim/getchar.h"
#include "nvim/highlight_group.h" #include "nvim/highlight_group.h"
#include "nvim/insexpand.h"
#include "nvim/locale.h" #include "nvim/locale.h"
#include "nvim/lua/executor.h" #include "nvim/lua/executor.h"
#include "nvim/mark.h" #include "nvim/mark.h"
@@ -49,6 +50,7 @@
#include "nvim/search.h" #include "nvim/search.h"
#include "nvim/sign.h" #include "nvim/sign.h"
#include "nvim/syntax.h" #include "nvim/syntax.h"
#include "nvim/tag.h"
#include "nvim/ui.h" #include "nvim/ui.h"
#include "nvim/ui_compositor.h" #include "nvim/ui_compositor.h"
#include "nvim/undo.h" #include "nvim/undo.h"
@@ -4168,10 +4170,23 @@ bool garbage_collect(bool testing)
ABORTING(set_ref_dict)(buf->additional_data, copyID); ABORTING(set_ref_dict)(buf->additional_data, copyID);
// buffer callback functions // buffer callback functions
set_ref_in_callback(&buf->b_prompt_callback, copyID, NULL, NULL); ABORTING(set_ref_in_callback)(&buf->b_prompt_callback, copyID, NULL, NULL);
set_ref_in_callback(&buf->b_prompt_interrupt, copyID, NULL, NULL); ABORTING(set_ref_in_callback)(&buf->b_prompt_interrupt, copyID, NULL, NULL);
ABORTING(set_ref_in_callback)(&buf->b_cfu_cb, copyID, NULL, NULL);
ABORTING(set_ref_in_callback)(&buf->b_ofu_cb, copyID, NULL, NULL);
ABORTING(set_ref_in_callback)(&buf->b_tsrfu_cb, copyID, NULL, NULL);
ABORTING(set_ref_in_callback)(&buf->b_tfu_cb, copyID, NULL, NULL);
} }
// 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
ABORTING(set_ref_in_insexpand_funcs)(copyID);
// 'operatorfunc' callback
ABORTING(set_ref_in_opfunc)(copyID);
// 'tagfunc' callback
ABORTING(set_ref_in_tagfunc)(copyID);
FOR_ALL_TAB_WINDOWS(tp, wp) { FOR_ALL_TAB_WINDOWS(tp, wp) {
// window-local variables // window-local variables
ABORTING(set_ref_in_item)(&wp->w_winvar.di_tv, copyID, NULL, NULL); ABORTING(set_ref_in_item)(&wp->w_winvar.di_tv, copyID, NULL, NULL);
@@ -5910,7 +5925,7 @@ bool callback_call(Callback *const callback, const int argcount_in, typval_T *co
return call_func(name, -1, rettv, argcount_in, argvars_in, &funcexe); return call_func(name, -1, rettv, argcount_in, argvars_in, &funcexe);
} }
static bool set_ref_in_callback(Callback *callback, int copyID, ht_stack_T **ht_stack, bool set_ref_in_callback(Callback *callback, int copyID, ht_stack_T **ht_stack,
list_stack_T **list_stack) list_stack_T **list_stack)
{ {
typval_T tv; typval_T tv;

View File

@@ -2299,6 +2299,17 @@ int set_thesaurusfunc_option(void)
return retval; return retval;
} }
/// Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
/// "copyID" so that they are not garbage collected.
bool set_ref_in_insexpand_funcs(int copyID)
{
bool abort = set_ref_in_callback(&cfu_cb, copyID, NULL, NULL);
abort = abort || set_ref_in_callback(&ofu_cb, copyID, NULL, NULL);
abort = abort || set_ref_in_callback(&tsrfu_cb, copyID, NULL, NULL);
return abort;
}
/// Get the user-defined completion function name for completion "type" /// Get the user-defined completion function name for completion "type"
static char_u *get_complete_funcname(int type) static char_u *get_complete_funcname(int type)
{ {

View File

@@ -5606,6 +5606,13 @@ void free_operatorfunc_option(void)
} }
#endif #endif
/// Mark the global 'operatorfunc' callback with "copyID" so that it is not
/// garbage collected.
bool set_ref_in_opfunc(int copyID)
{
return set_ref_in_callback(&opfunc_cb, copyID, NULL, NULL);
}
/// Handle the "g@" operator: call 'operatorfunc'. /// Handle the "g@" operator: call 'operatorfunc'.
static void op_function(const oparg_T *oap) static void op_function(const oparg_T *oap)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL

View File

@@ -6686,7 +6686,8 @@ int set_errorlist(win_T *wp, list_T *list, int action, char *title, dict_T *what
return retval; return retval;
} }
/// Mark the context as in use for all the lists in a quickfix stack. /// Mark the quickfix context and callback function as in use for all the lists
/// in a quickfix stack.
static bool mark_quickfix_ctx(qf_info_T *qi, int copyID) static bool mark_quickfix_ctx(qf_info_T *qi, int copyID)
{ {
bool abort = false; bool abort = false;
@@ -6695,8 +6696,11 @@ static bool mark_quickfix_ctx(qf_info_T *qi, int copyID)
typval_T *ctx = qi->qf_lists[i].qf_ctx; typval_T *ctx = qi->qf_lists[i].qf_ctx;
if (ctx != NULL && ctx->v_type != VAR_NUMBER if (ctx != NULL && ctx->v_type != VAR_NUMBER
&& ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT) { && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT) {
abort = set_ref_in_item(ctx, copyID, NULL, NULL); abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
} }
Callback *cb = &qi->qf_lists[i].qf_qftf_cb;
abort = abort || set_ref_in_callback(cb, copyID, NULL, NULL);
} }
return abort; return abort;
@@ -6711,6 +6715,11 @@ bool set_ref_in_quickfix(int copyID)
return abort; return abort;
} }
abort = set_ref_in_callback(&qftf_cb, copyID, NULL, NULL);
if (abort) {
return abort;
}
FOR_ALL_TAB_WINDOWS(tp, win) { FOR_ALL_TAB_WINDOWS(tp, win) {
if (win->w_llist != NULL) { if (win->w_llist != NULL) {
abort = mark_quickfix_ctx(win->w_llist, copyID); abort = mark_quickfix_ctx(win->w_llist, copyID);

View File

@@ -149,6 +149,13 @@ void free_tagfunc_option(void)
} }
#endif #endif
/// Mark the global 'tagfunc' callback with "copyID" so that it is not garbage
/// collected.
bool set_ref_in_tagfunc(int copyID)
{
return set_ref_in_callback(&tfu_cb, copyID, NULL, NULL);
}
/// Copy the global 'tagfunc' callback function to the buffer-local 'tagfunc' /// Copy the global 'tagfunc' callback function to the buffer-local 'tagfunc'
/// callback for 'buf'. /// callback for 'buf'.
void set_buflocal_tfu_callback(buf_T *buf) void set_buflocal_tfu_callback(buf_T *buf)

View File

@@ -1,5 +1,8 @@
" Test for insert completion
source screendump.vim source screendump.vim
source check.vim source check.vim
source vim9.vim
" Test for insert expansion " Test for insert expansion
func Test_ins_complete() func Test_ins_complete()
@@ -1292,136 +1295,129 @@ func Test_completefunc_callback()
return a:findstart ? 0 : [] return a:findstart ? 0 : []
endfunc endfunc
" Test for using a function() let lines =<< trim END
set completefunc=function('MycompleteFunc1',[10]) #" Test for using a function()
set completefunc=function('g:MycompleteFunc1',\ [10])
new | only new | only
call setline(1, 'one') call setline(1, 'one')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args) call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args)
bw! bw!
" Using a funcref variable to set 'completefunc' #" Using a funcref variable to set 'completefunc'
let Fn = function('MycompleteFunc1', [11]) VAR Fn = function('g:MycompleteFunc1', [11])
let &completefunc = Fn LET &completefunc = Fn
new | only new | only
call setline(1, 'two') call setline(1, 'two')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args) call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args)
bw! bw!
" Using string(funcref_variable) to set 'completefunc' #" Using string(funcref_variable) to set 'completefunc'
let Fn = function('MycompleteFunc1', [12]) LET Fn = function('g:MycompleteFunc1', [12])
let &completefunc = string(Fn) LET &completefunc = string(Fn)
new | only new | only
call setline(1, 'two') call setline(1, 'two')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args) call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args)
bw! bw!
" Test for using a funcref() #" Test for using a funcref()
set completefunc=funcref('MycompleteFunc1',\ [13]) set completefunc=funcref('g:MycompleteFunc1',\ [13])
new | only new | only
call setline(1, 'three') call setline(1, 'three')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args) call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args)
bw! bw!
" Using a funcref variable to set 'completefunc' #" Using a funcref variable to set 'completefunc'
let Fn = funcref('MycompleteFunc1', [14]) LET Fn = funcref('g:MycompleteFunc1', [14])
let &completefunc = Fn LET &completefunc = Fn
new | only new | only
call setline(1, 'four') call setline(1, 'four')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args) call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args)
bw! bw!
" Using a string(funcref_variable) to set 'completefunc' #" Using a string(funcref_variable) to set 'completefunc'
let Fn = funcref('MycompleteFunc1', [15]) LET Fn = funcref('g:MycompleteFunc1', [15])
let &completefunc = string(Fn) LET &completefunc = string(Fn)
new | only new | only
call setline(1, 'four') call setline(1, 'four')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args) call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args)
bw! bw!
" Test for using a lambda function #" Test for using a lambda function with set
set completefunc={a,\ b\ ->\ MycompleteFunc1(16,\ a,\ b)} VAR optval = "LSTART a, b LMIDDLE MycompleteFunc1(16, a, b) LEND"
LET optval = substitute(optval, ' ', '\\ ', 'g')
exe "set completefunc=" .. optval
new | only new | only
call setline(1, 'five') call setline(1, 'five')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args) call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args)
bw! bw!
" Set 'completefunc' to a lambda expression #" Set 'completefunc' to a lambda expression
let &completefunc = {a, b -> MycompleteFunc1(17, a, b)} LET &completefunc = LSTART a, b LMIDDLE MycompleteFunc1(17, a, b) LEND
new | only new | only
call setline(1, 'six') call setline(1, 'six')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args) call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args)
bw! bw!
" Set 'completefunc' to string(lambda_expression) #" Set 'completefunc' to string(lambda_expression)
let &completefunc = '{a, b -> MycompleteFunc1(18, a, b)}' LET &completefunc = 'LSTART a, b LMIDDLE MycompleteFunc1(18, a, b) LEND'
new | only new | only
call setline(1, 'six') call setline(1, 'six')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args) call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args)
bw! bw!
" Set 'completefunc' to a variable with a lambda expression #" Set 'completefunc' to a variable with a lambda expression
let Lambda = {a, b -> MycompleteFunc1(19, a, b)} VAR Lambda = LSTART a, b LMIDDLE MycompleteFunc1(19, a, b) LEND
let &completefunc = Lambda LET &completefunc = Lambda
new | only new | only
call setline(1, 'seven') call setline(1, 'seven')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args) call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args)
bw! bw!
" Set 'completefunc' to a string(variable with a lambda expression) #" Set 'completefunc' to a string(variable with a lambda expression)
let Lambda = {a, b -> MycompleteFunc1(20, a, b)} LET Lambda = LSTART a, b LMIDDLE MycompleteFunc1(20, a, b) LEND
let &completefunc = string(Lambda) LET &completefunc = string(Lambda)
new | only new | only
call setline(1, 'seven') call setline(1, 'seven')
let g:MycompleteFunc1_args = [] LET g:MycompleteFunc1_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args) call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args)
bw! bw!
" Test for using a lambda function with incorrect return value #" Test for using a lambda function with incorrect return value
let Lambda = {s -> strlen(s)} LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
let &completefunc = Lambda LET &completefunc = Lambda
new | only new | only
call setline(1, 'eight') call setline(1, 'eight')
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
bw! bw!
" Test for clearing the 'completefunc' option #" Test for clearing the 'completefunc' option
set completefunc='' set completefunc=''
set completefunc& set completefunc&
call assert_fails("set completefunc=function('abc')", "E700:") call assert_fails("set completefunc=function('abc')", "E700:")
call assert_fails("set completefunc=funcref('abc')", "E700:") call assert_fails("set completefunc=funcref('abc')", "E700:")
let &completefunc = {a -> 'abc'}
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
" Using Vim9 lambda expression in legacy context should fail #" set 'completefunc' to a non-existing function
" set completefunc=(a,\ b)\ =>\ g:MycompleteFunc1(21,\ a,\ b)
new | only
let g:MycompleteFunc1_args = []
" call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
call assert_equal([], g:MycompleteFunc1_args)
" set 'completefunc' to a non-existing function
func MycompleteFunc2(findstart, base) func MycompleteFunc2(findstart, base)
call add(g:MycompleteFunc2_args, [a:findstart, a:base]) call add(g:MycompleteFunc2_args, [a:findstart, a:base])
return a:findstart ? 0 : [] return a:findstart ? 0 : []
@@ -1429,17 +1425,49 @@ func Test_completefunc_callback()
set completefunc=MycompleteFunc2 set completefunc=MycompleteFunc2
call setline(1, 'five') call setline(1, 'five')
call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
call assert_fails("let &completefunc = function('NonExistingFunc')", 'E700:') call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
let g:MycompleteFunc2_args = [] LET g:MycompleteFunc2_args = []
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args) call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args)
bw! bw!
END
call CheckLegacyAndVim9Success(lines)
let &completefunc = {a -> 'abc'}
call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
" Using Vim9 lambda expression in legacy context should fail
" set completefunc=(a,\ b)\ =>\ MycompleteFunc1(21,\ a,\ b)
new | only
let g:MycompleteFunc1_args = []
" call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
call assert_equal([], g:MycompleteFunc1_args)
" set 'completefunc' to a partial with dict. This used to cause a crash.
func SetCompleteFunc()
let params = {'complete': function('g:DictCompleteFunc')}
let &completefunc = params.complete
endfunc
func g:DictCompleteFunc(_) dict
endfunc
call SetCompleteFunc()
new
call SetCompleteFunc()
bw
call test_garbagecollect_now()
new
set completefunc=
wincmd w
set completefunc=
%bw!
delfunc g:DictCompleteFunc
delfunc SetCompleteFunc
" Vim9 tests " Vim9 tests
let lines =<< trim END let lines =<< trim END
vim9script vim9script
# Test for using function() # Test for using a def function with completefunc
def Vim9CompleteFunc(val: number, findstart: number, base: string): any def Vim9CompleteFunc(val: number, findstart: number, base: string): any
add(g:Vim9completeFuncArgs, [val, findstart, base]) add(g:Vim9completeFuncArgs, [val, findstart, base])
return findstart ? 0 : [] return findstart ? 0 : []
@@ -1451,44 +1479,6 @@ func Test_completefunc_callback()
feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
bw! bw!
# Test for using a lambda
&completefunc = (a, b) => Vim9CompleteFunc(61, a, b)
new | only
setline(1, 'two')
g:Vim9completeFuncArgs = []
feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
assert_equal([[61, 1, ''], [61, 0, 'two']], g:Vim9completeFuncArgs)
bw!
# Test for using a string(lambda)
&completefunc = '(a, b) => Vim9CompleteFunc(62, a, b)'
new | only
setline(1, 'two')
g:Vim9completeFuncArgs = []
feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
assert_equal([[62, 1, ''], [62, 0, 'two']], g:Vim9completeFuncArgs)
bw!
# Test for using a variable with a lambda expression
var Fn: func = (a, b) => Vim9CompleteFunc(63, a, b)
&completefunc = Fn
new | only
setline(1, 'three')
g:Vim9completeFuncArgs = []
feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
assert_equal([[63, 1, ''], [63, 0, 'three']], g:Vim9completeFuncArgs)
bw!
# Test for using a string(variable with a lambda expression)
Fn = (a, b) => Vim9CompleteFunc(64, a, b)
&completefunc = string(Fn)
new | only
setline(1, 'three')
g:Vim9completeFuncArgs = []
feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
assert_equal([[64, 1, ''], [64, 0, 'three']], g:Vim9completeFuncArgs)
bw!
END END
" call CheckScriptSuccess(lines) " call CheckScriptSuccess(lines)
@@ -1506,136 +1496,129 @@ func Test_omnifunc_callback()
return a:findstart ? 0 : [] return a:findstart ? 0 : []
endfunc endfunc
" Test for using a function() let lines =<< trim END
set omnifunc=function('MyomniFunc1',[10]) #" Test for using a function()
set omnifunc=function('g:MyomniFunc1',\ [10])
new | only new | only
call setline(1, 'one') call setline(1, 'one')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MyomniFunc1_args) call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MyomniFunc1_args)
bw! bw!
" Using a funcref variable to set 'omnifunc' #" Using a funcref variable to set 'omnifunc'
let Fn = function('MyomniFunc1', [11]) VAR Fn = function('g:MyomniFunc1', [11])
let &omnifunc = Fn LET &omnifunc = Fn
new | only new | only
call setline(1, 'two') call setline(1, 'two')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MyomniFunc1_args) call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MyomniFunc1_args)
bw! bw!
" Using a string(funcref_variable) to set 'omnifunc' #" Using a string(funcref_variable) to set 'omnifunc'
let Fn = function('MyomniFunc1', [12]) LET Fn = function('g:MyomniFunc1', [12])
let &omnifunc = string(Fn) LET &omnifunc = string(Fn)
new | only new | only
call setline(1, 'two') call setline(1, 'two')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MyomniFunc1_args) call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MyomniFunc1_args)
bw! bw!
" Test for using a funcref() #" Test for using a funcref()
set omnifunc=funcref('MyomniFunc1',\ [13]) set omnifunc=funcref('g:MyomniFunc1',\ [13])
new | only new | only
call setline(1, 'three') call setline(1, 'three')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MyomniFunc1_args) call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MyomniFunc1_args)
bw! bw!
" Using a funcref variable to set 'omnifunc' #" Use let to set 'omnifunc' to a funcref
let Fn = funcref('MyomniFunc1', [14]) LET Fn = funcref('g:MyomniFunc1', [14])
let &omnifunc = Fn LET &omnifunc = Fn
new | only new | only
call setline(1, 'four') call setline(1, 'four')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MyomniFunc1_args) call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MyomniFunc1_args)
bw! bw!
" Using a string(funcref_variable) to set 'omnifunc' #" Using a string(funcref) to set 'omnifunc'
let Fn = funcref('MyomniFunc1', [15]) LET Fn = funcref("g:MyomniFunc1", [15])
let &omnifunc = string(Fn) LET &omnifunc = string(Fn)
new | only new | only
call setline(1, 'four') call setline(1, 'four')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MyomniFunc1_args) call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MyomniFunc1_args)
bw! bw!
" Test for using a lambda function #" Test for using a lambda function with set
set omnifunc={a,\ b\ ->\ MyomniFunc1(16,\ a,\ b)} VAR optval = "LSTART a, b LMIDDLE MyomniFunc1(16, a, b) LEND"
LET optval = substitute(optval, ' ', '\\ ', 'g')
exe "set omnifunc=" .. optval
new | only new | only
call setline(1, 'five') call setline(1, 'five')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MyomniFunc1_args) call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MyomniFunc1_args)
bw! bw!
" Set 'omnifunc' to a lambda expression #" Set 'omnifunc' to a lambda expression
let &omnifunc = {a, b -> MyomniFunc1(17, a, b)} LET &omnifunc = LSTART a, b LMIDDLE MyomniFunc1(17, a, b) LEND
new | only new | only
call setline(1, 'six') call setline(1, 'six')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MyomniFunc1_args) call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MyomniFunc1_args)
bw! bw!
" Set 'omnifunc' to a string(lambda_expression) #" Set 'omnifunc' to a string(lambda_expression)
let &omnifunc = '{a, b -> MyomniFunc1(18, a, b)}' LET &omnifunc = 'LSTART a, b LMIDDLE MyomniFunc1(18, a, b) LEND'
new | only new | only
call setline(1, 'six') call setline(1, 'six')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MyomniFunc1_args) call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MyomniFunc1_args)
bw! bw!
" Set 'omnifunc' to a variable with a lambda expression #" Set 'omnifunc' to a variable with a lambda expression
let Lambda = {a, b -> MyomniFunc1(19, a, b)} VAR Lambda = LSTART a, b LMIDDLE MyomniFunc1(19, a, b) LEND
let &omnifunc = Lambda LET &omnifunc = Lambda
new | only new | only
call setline(1, 'seven') call setline(1, 'seven')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MyomniFunc1_args) call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MyomniFunc1_args)
bw! bw!
" Set 'omnifunc' to a string(variable with a lambda expression) #" Set 'omnifunc' to a string(variable with a lambda expression)
let Lambda = {a, b -> MyomniFunc1(20, a, b)} LET Lambda = LSTART a, b LMIDDLE MyomniFunc1(20, a, b) LEND
let &omnifunc = string(Lambda) LET &omnifunc = string(Lambda)
new | only new | only
call setline(1, 'seven') call setline(1, 'seven')
let g:MyomniFunc1_args = [] LET g:MyomniFunc1_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MyomniFunc1_args) call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MyomniFunc1_args)
bw! bw!
" Test for using a lambda function with incorrect return value #" Test for using a lambda function with incorrect return value
let Lambda = {s -> strlen(s)} LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
let &omnifunc = Lambda LET &omnifunc = Lambda
new | only new | only
call setline(1, 'eight') call setline(1, 'eight')
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
bw! bw!
" Test for clearing the 'omnifunc' option #" Test for clearing the 'omnifunc' option
set omnifunc='' set omnifunc=''
set omnifunc& set omnifunc&
call assert_fails("set omnifunc=function('abc')", "E700:") call assert_fails("set omnifunc=function('abc')", "E700:")
call assert_fails("set omnifunc=funcref('abc')", "E700:") call assert_fails("set omnifunc=funcref('abc')", "E700:")
let &omnifunc = {a -> 'abc'}
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
" Using Vim9 lambda expression in legacy context should fail #" set 'omnifunc' to a non-existing function
" set omnifunc=(a,\ b)\ =>\ g:MyomniFunc1(21,\ a,\ b)
new | only
let g:MyomniFunc1_args = []
" call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
call assert_equal([], g:MyomniFunc1_args)
" set 'omnifunc' to a non-existing function
func MyomniFunc2(findstart, base) func MyomniFunc2(findstart, base)
call add(g:MyomniFunc2_args, [a:findstart, a:base]) call add(g:MyomniFunc2_args, [a:findstart, a:base])
return a:findstart ? 0 : [] return a:findstart ? 0 : []
@@ -1643,17 +1626,49 @@ func Test_omnifunc_callback()
set omnifunc=MyomniFunc2 set omnifunc=MyomniFunc2
call setline(1, 'nine') call setline(1, 'nine')
call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
call assert_fails("let &omnifunc = function('NonExistingFunc')", 'E700:') call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
let g:MyomniFunc2_args = [] LET g:MyomniFunc2_args = []
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
call assert_equal([[1, ''], [0, 'nine']], g:MyomniFunc2_args) call assert_equal([[1, ''], [0, 'nine']], g:MyomniFunc2_args)
bw! bw!
END
call CheckLegacyAndVim9Success(lines)
let &omnifunc = {a -> 'abc'}
call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
" Using Vim9 lambda expression in legacy context should fail
" set omnifunc=(a,\ b)\ =>\ MyomniFunc1(21,\ a,\ b)
new | only
let g:MyomniFunc1_args = []
" call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
call assert_equal([], g:MyomniFunc1_args)
" set 'omnifunc' to a partial with dict. This used to cause a crash.
func SetOmniFunc()
let params = {'omni': function('g:DictOmniFunc')}
let &omnifunc = params.omni
endfunc
func g:DictOmniFunc(_) dict
endfunc
call SetOmniFunc()
new
call SetOmniFunc()
bw
call test_garbagecollect_now()
new
set omnifunc=
wincmd w
set omnifunc=
%bw!
delfunc g:DictOmniFunc
delfunc SetOmniFunc
" Vim9 tests " Vim9 tests
let lines =<< trim END let lines =<< trim END
vim9script vim9script
# Test for using function() # Test for using a def function with omnifunc
def Vim9omniFunc(val: number, findstart: number, base: string): any def Vim9omniFunc(val: number, findstart: number, base: string): any
add(g:Vim9omniFunc_Args, [val, findstart, base]) add(g:Vim9omniFunc_Args, [val, findstart, base])
return findstart ? 0 : [] return findstart ? 0 : []
@@ -1665,44 +1680,6 @@ func Test_omnifunc_callback()
feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
bw! bw!
# Test for using a lambda
&omnifunc = (a, b) => Vim9omniFunc(61, a, b)
new | only
setline(1, 'two')
g:Vim9omniFunc_Args = []
feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
assert_equal([[61, 1, ''], [61, 0, 'two']], g:Vim9omniFunc_Args)
bw!
# Test for using a string(lambda)
&omnifunc = '(a, b) => Vim9omniFunc(62, a, b)'
new | only
setline(1, 'two')
g:Vim9omniFunc_Args = []
feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
assert_equal([[62, 1, ''], [62, 0, 'two']], g:Vim9omniFunc_Args)
bw!
# Test for using a variable with a lambda expression
var Fn: func = (a, b) => Vim9omniFunc(63, a, b)
&omnifunc = Fn
new | only
setline(1, 'three')
g:Vim9omniFunc_Args = []
feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
assert_equal([[63, 1, ''], [63, 0, 'three']], g:Vim9omniFunc_Args)
bw!
# Test for using a string(variable with a lambda expression)
Fn = (a, b) => Vim9omniFunc(64, a, b)
&omnifunc = string(Fn)
new | only
setline(1, 'three')
g:Vim9omniFunc_Args = []
feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
assert_equal([[64, 1, ''], [64, 0, 'three']], g:Vim9omniFunc_Args)
bw!
END END
" call CheckScriptSuccess(lines) " call CheckScriptSuccess(lines)
@@ -1720,160 +1697,129 @@ func Test_thesaurusfunc_callback()
return a:findstart ? 0 : [] return a:findstart ? 0 : []
endfunc endfunc
" Test for using a function() let lines =<< trim END
set thesaurusfunc=function('MytsrFunc1',[10]) #" Test for using a function()
set thesaurusfunc=function('g:MytsrFunc1',\ [10])
new | only new | only
call setline(1, 'one') call setline(1, 'one')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MytsrFunc1_args) call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MytsrFunc1_args)
bw! bw!
" Using a funcref variable to set 'thesaurusfunc' #" Using a funcref variable to set 'thesaurusfunc'
let Fn = function('MytsrFunc1', [11]) VAR Fn = function('g:MytsrFunc1', [11])
let &thesaurusfunc = Fn LET &thesaurusfunc = Fn
new | only new | only
call setline(1, 'two') call setline(1, 'two')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MytsrFunc1_args) call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MytsrFunc1_args)
bw! bw!
" Using a string(funcref_variable) to set 'thesaurusfunc' #" Using a string(funcref_variable) to set 'thesaurusfunc'
let Fn = function('MytsrFunc1', [12]) LET Fn = function('g:MytsrFunc1', [12])
let &thesaurusfunc = string(Fn) LET &thesaurusfunc = string(Fn)
new | only new | only
call setline(1, 'two') call setline(1, 'two')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MytsrFunc1_args) call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MytsrFunc1_args)
bw! bw!
" Test for using a funcref() #" Test for using a funcref()
set thesaurusfunc=funcref('MytsrFunc1',[13]) set thesaurusfunc=funcref('g:MytsrFunc1',\ [13])
new | only new | only
call setline(1, 'three') call setline(1, 'three')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MytsrFunc1_args) call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MytsrFunc1_args)
bw! bw!
" Using a funcref variable to set 'thesaurusfunc' #" Using a funcref variable to set 'thesaurusfunc'
let Fn = funcref('MytsrFunc1', [14]) LET Fn = funcref('g:MytsrFunc1', [14])
let &thesaurusfunc = Fn LET &thesaurusfunc = Fn
new | only new | only
call setline(1, 'four') call setline(1, 'four')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MytsrFunc1_args) call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MytsrFunc1_args)
bw! bw!
" Using a string(funcref_variable) to set 'thesaurusfunc' #" Using a string(funcref_variable) to set 'thesaurusfunc'
let Fn = funcref('MytsrFunc1', [15]) LET Fn = funcref('g:MytsrFunc1', [15])
let &thesaurusfunc = string(Fn) LET &thesaurusfunc = string(Fn)
new | only new | only
call setline(1, 'four') call setline(1, 'four')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MytsrFunc1_args) call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MytsrFunc1_args)
bw! bw!
" Test for using a lambda function #" Test for using a lambda function
set thesaurusfunc={a,\ b\ ->\ MytsrFunc1(16,\ a,\ b)} VAR optval = "LSTART a, b LMIDDLE MytsrFunc1(16, a, b) LEND"
LET optval = substitute(optval, ' ', '\\ ', 'g')
exe "set thesaurusfunc=" .. optval
new | only new | only
call setline(1, 'five') call setline(1, 'five')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MytsrFunc1_args) call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MytsrFunc1_args)
bw! bw!
" Set 'thesaurusfunc' to a lambda expression #" Test for using a lambda function with set
let &thesaurusfunc = {a, b -> MytsrFunc1(17, a, b)} LET &thesaurusfunc = LSTART a, b LMIDDLE MytsrFunc1(17, a, b) LEND
new | only new | only
call setline(1, 'six') call setline(1, 'six')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MytsrFunc1_args) call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MytsrFunc1_args)
bw! bw!
" Set 'thesaurusfunc' to a string(lambda expression) #" Set 'thesaurusfunc' to a string(lambda expression)
let &thesaurusfunc = '{a, b -> MytsrFunc1(18, a, b)}' LET &thesaurusfunc = 'LSTART a, b LMIDDLE MytsrFunc1(18, a, b) LEND'
new | only new | only
call setline(1, 'six') call setline(1, 'six')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MytsrFunc1_args) call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MytsrFunc1_args)
bw! bw!
" Set 'thesaurusfunc' to a variable with a lambda expression #" Set 'thesaurusfunc' to a variable with a lambda expression
let Lambda = {a, b -> MytsrFunc1(19, a, b)} VAR Lambda = LSTART a, b LMIDDLE MytsrFunc1(19, a, b) LEND
let &thesaurusfunc = Lambda LET &thesaurusfunc = Lambda
new | only new | only
call setline(1, 'seven') call setline(1, 'seven')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MytsrFunc1_args) call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MytsrFunc1_args)
bw! bw!
" Set 'thesaurusfunc' to a string(variable with a lambda expression) #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
let Lambda = {a, b -> MytsrFunc1(20, a, b)} LET Lambda = LSTART a, b LMIDDLE MytsrFunc1(20, a, b) LEND
let &thesaurusfunc = string(Lambda) LET &thesaurusfunc = string(Lambda)
new | only new | only
call setline(1, 'seven') call setline(1, 'seven')
let g:MytsrFunc1_args = [] LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MytsrFunc1_args) call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MytsrFunc1_args)
bw! bw!
" Test for using a lambda function with incorrect return value #" Test for using a lambda function with incorrect return value
let Lambda = {s -> strlen(s)} LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
let &thesaurusfunc = Lambda LET &thesaurusfunc = Lambda
new | only new | only
call setline(1, 'eight') call setline(1, 'eight')
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
bw! bw!
" Test for clearing the 'thesaurusfunc' option #" Test for clearing the 'thesaurusfunc' option
set thesaurusfunc='' set thesaurusfunc=''
set thesaurusfunc& set thesaurusfunc&
call assert_fails("set thesaurusfunc=function('abc')", "E700:") call assert_fails("set thesaurusfunc=function('abc')", "E700:")
call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
let &thesaurusfunc = {a -> 'abc'}
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
" Using Vim9 lambda expression in legacy context should fail #" set 'thesaurusfunc' to a non-existing function
" set thesaurusfunc=(a,\ b)\ =>\ g:MytsrFunc1(21,\ a,\ b)
new | only
let g:MytsrFunc1_args = []
" call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
call assert_equal([], g:MytsrFunc1_args)
bw!
" Use a buffer-local value and a global value
set thesaurusfunc&
setlocal thesaurusfunc=function('MytsrFunc1',[22])
call setline(1, 'sun')
let g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
call assert_equal('sun', getline(1))
call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:MytsrFunc1_args)
new
call setline(1, 'sun')
let g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
call assert_equal('sun', getline(1))
call assert_equal([], g:MytsrFunc1_args)
set thesaurusfunc=function('MytsrFunc1',[23])
wincmd w
call setline(1, 'sun')
let g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
call assert_equal('sun', getline(1))
call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:MytsrFunc1_args)
%bw!
" set 'thesaurusfunc' to a non-existing function
func MytsrFunc2(findstart, base) func MytsrFunc2(findstart, base)
call add(g:MytsrFunc2_args, [a:findstart, a:base]) call add(g:MytsrFunc2_args, [a:findstart, a:base])
return a:findstart ? 0 : ['sunday'] return a:findstart ? 0 : ['sunday']
@@ -1881,17 +1827,85 @@ func Test_thesaurusfunc_callback()
set thesaurusfunc=MytsrFunc2 set thesaurusfunc=MytsrFunc2
call setline(1, 'ten') call setline(1, 'ten')
call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
call assert_fails("let &thesaurusfunc = function('NonExistingFunc')", 'E700:') call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
let g:MytsrFunc2_args = [] LET g:MytsrFunc2_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
call assert_equal([[1, ''], [0, 'ten']], g:MytsrFunc2_args) call assert_equal([[1, ''], [0, 'ten']], g:MytsrFunc2_args)
bw! bw!
#" Use a buffer-local value and a global value
set thesaurusfunc&
setlocal thesaurusfunc=function('g:MytsrFunc1',\ [22])
call setline(1, 'sun')
LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
call assert_equal('sun', getline(1))
call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:MytsrFunc1_args)
new
call setline(1, 'sun')
LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
call assert_equal('sun', getline(1))
call assert_equal([], g:MytsrFunc1_args)
set thesaurusfunc=function('g:MytsrFunc1',\ [23])
wincmd w
call setline(1, 'sun')
LET g:MytsrFunc1_args = []
call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
call assert_equal('sun', getline(1))
call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:MytsrFunc1_args)
:%bw!
END
call CheckLegacyAndVim9Success(lines)
let &thesaurusfunc = {a -> 'abc'}
call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
" Using Vim9 lambda expression in legacy context should fail
" set thesaurusfunc=(a,\ b)\ =>\ MytsrFunc1(21,\ a,\ b)
new | only
let g:MytsrFunc1_args = []
" call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
call assert_equal([], g:MytsrFunc1_args)
bw!
" set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
func SetTsrFunc()
let params = {'thesaurus': function('g:DictTsrFunc')}
let &thesaurusfunc = params.thesaurus
endfunc
func g:DictTsrFunc(_) dict
endfunc
call SetTsrFunc()
new
call SetTsrFunc()
bw
call test_garbagecollect_now()
new
set thesaurusfunc=
wincmd w
%bw!
delfunc SetTsrFunc
" set buffer-local 'thesaurusfunc' to a partial with dict. This used to
" cause a crash.
func SetLocalTsrFunc()
let params = {'thesaurus': function('g:DictTsrFunc')}
let &l:thesaurusfunc = params.thesaurus
endfunc
call SetLocalTsrFunc()
call test_garbagecollect_now()
call SetLocalTsrFunc()
set thesaurusfunc=
bw!
delfunc g:DictTsrFunc
delfunc SetLocalTsrFunc
" Vim9 tests " Vim9 tests
let lines =<< trim END let lines =<< trim END
vim9script vim9script
# Test for using function() # Test for using a def function with thesaurusfunc
def Vim9tsrFunc(val: number, findstart: number, base: string): any def Vim9tsrFunc(val: number, findstart: number, base: string): any
add(g:Vim9tsrFunc_Args, [val, findstart, base]) add(g:Vim9tsrFunc_Args, [val, findstart, base])
return findstart ? 0 : [] return findstart ? 0 : []
@@ -1903,44 +1917,6 @@ func Test_thesaurusfunc_callback()
feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args) assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
bw! bw!
# Test for using a lambda
&thesaurusfunc = (a, b) => Vim9tsrFunc(61, a, b)
new | only
setline(1, 'two')
g:Vim9tsrFunc_Args = []
feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
assert_equal([[61, 1, ''], [61, 0, 'two']], g:Vim9tsrFunc_Args)
bw!
# Test for using a string(lambda)
&thesaurusfunc = '(a, b) => Vim9tsrFunc(62, a, b)'
new | only
setline(1, 'two')
g:Vim9tsrFunc_Args = []
feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
assert_equal([[62, 1, ''], [62, 0, 'two']], g:Vim9tsrFunc_Args)
bw!
# Test for using a variable with a lambda expression
var Fn: func = (a, b) => Vim9tsrFunc(63, a, b)
&thesaurusfunc = Fn
new | only
setline(1, 'three')
g:Vim9tsrFunc_Args = []
feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
assert_equal([[63, 1, ''], [63, 0, 'three']], g:Vim9tsrFunc_Args)
bw!
# Test for using a string(variable with a lambda expression)
Fn = (a, b) => Vim9tsrFunc(64, a, b)
&thesaurusfunc = string(Fn)
new | only
setline(1, 'three')
g:Vim9tsrFunc_Args = []
feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
assert_equal([[64, 1, ''], [64, 0, 'three']], g:Vim9tsrFunc_Args)
bw!
END END
" call CheckScriptSuccess(lines) " call CheckScriptSuccess(lines)

View File

@@ -3,6 +3,7 @@
source shared.vim source shared.vim
source check.vim source check.vim
source view_util.vim source view_util.vim
source vim9.vim
source screendump.vim source screendump.vim
func Setup_NewWindow() func Setup_NewWindow()
@@ -463,130 +464,147 @@ func Test_opfunc_callback()
let g:OpFuncArgs = [a:val, a:type] let g:OpFuncArgs = [a:val, a:type]
endfunc endfunc
" Test for using a function() let lines =<< trim END
set opfunc=function('MyopFunc',\ [11]) #" Test for using a function()
let g:OpFuncArgs = [] set opfunc=function('g:MyopFunc',\ [10])
LET g:OpFuncArgs = []
normal! g@l
call assert_equal([10, 'char'], g:OpFuncArgs)
#" Using a funcref variable to set 'operatorfunc'
VAR Fn = function('g:MyopFunc', [11])
LET &opfunc = Fn
LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([11, 'char'], g:OpFuncArgs) call assert_equal([11, 'char'], g:OpFuncArgs)
" Using a funcref variable to set 'operatorfunc' #" Using a string(funcref_variable) to set 'operatorfunc'
let Fn = function('MyopFunc', [12]) LET Fn = function('g:MyopFunc', [12])
let &opfunc = Fn LET &operatorfunc = string(Fn)
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([12, 'char'], g:OpFuncArgs) call assert_equal([12, 'char'], g:OpFuncArgs)
" Using a string(funcref_variable) to set 'operatorfunc' #" Test for using a funcref()
let Fn = function('MyopFunc', [13]) set operatorfunc=funcref('g:MyopFunc',\ [13])
let &operatorfunc = string(Fn) LET g:OpFuncArgs = []
let g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([13, 'char'], g:OpFuncArgs) call assert_equal([13, 'char'], g:OpFuncArgs)
" Test for using a funcref() #" Using a funcref variable to set 'operatorfunc'
set operatorfunc=funcref('MyopFunc',\ [14]) LET Fn = funcref('g:MyopFunc', [14])
let g:OpFuncArgs = [] LET &opfunc = Fn
LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([14, 'char'], g:OpFuncArgs) call assert_equal([14, 'char'], g:OpFuncArgs)
" Using a funcref variable to set 'operatorfunc' #" Using a string(funcref_variable) to set 'operatorfunc'
let Fn = funcref('MyopFunc', [15]) LET Fn = funcref('g:MyopFunc', [15])
let &opfunc = Fn LET &opfunc = string(Fn)
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([15, 'char'], g:OpFuncArgs) call assert_equal([15, 'char'], g:OpFuncArgs)
" Using a string(funcref_variable) to set 'operatorfunc' #" Test for using a lambda function using set
let Fn = funcref('MyopFunc', [16]) VAR optval = "LSTART a LMIDDLE MyopFunc(16, a) LEND"
let &opfunc = string(Fn) LET optval = substitute(optval, ' ', '\\ ', 'g')
let g:OpFuncArgs = [] exe "set opfunc=" .. optval
LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([16, 'char'], g:OpFuncArgs) call assert_equal([16, 'char'], g:OpFuncArgs)
" Test for using a lambda function using set #" Test for using a lambda function using LET
set opfunc={a\ ->\ MyopFunc(17,\ a)} LET &opfunc = LSTART a LMIDDLE MyopFunc(17, a) LEND
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([17, 'char'], g:OpFuncArgs) call assert_equal([17, 'char'], g:OpFuncArgs)
" Test for using a lambda function using let #" Set 'operatorfunc' to a string(lambda expression)
let &opfunc = {a -> MyopFunc(18, a)} LET &opfunc = 'LSTART a LMIDDLE MyopFunc(18, a) LEND'
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([18, 'char'], g:OpFuncArgs) call assert_equal([18, 'char'], g:OpFuncArgs)
" Set 'operatorfunc' to a string(lambda expression) #" Set 'operatorfunc' to a variable with a lambda expression
let &opfunc = '{a -> MyopFunc(19, a)}' VAR Lambda = LSTART a LMIDDLE MyopFunc(19, a) LEND
let g:OpFuncArgs = [] LET &opfunc = Lambda
LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([19, 'char'], g:OpFuncArgs) call assert_equal([19, 'char'], g:OpFuncArgs)
" Set 'operatorfunc' to a variable with a lambda expression #" Set 'operatorfunc' to a string(variable with a lambda expression)
let Lambda = {a -> MyopFunc(20, a)} LET Lambda = LSTART a LMIDDLE MyopFunc(20, a) LEND
let &opfunc = Lambda LET &opfunc = string(Lambda)
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
normal! g@l normal! g@l
call assert_equal([20, 'char'], g:OpFuncArgs) call assert_equal([20, 'char'], g:OpFuncArgs)
" Set 'operatorfunc' to a string(variable with a lambda expression) #" Try to use 'operatorfunc' after the function is deleted
let Lambda = {a -> MyopFunc(21, a)} func g:TmpOpFunc(type)
let &opfunc = string(Lambda) LET g:OpFuncArgs = [21, a:type]
let g:OpFuncArgs = []
normal! g@l
call assert_equal([21, 'char'], g:OpFuncArgs)
" Try to use 'operatorfunc' after the function is deleted
func TmpOpFunc(type)
let g:OpFuncArgs = [22, a:type]
endfunc endfunc
let &opfunc = function('TmpOpFunc') LET &opfunc = function('g:TmpOpFunc')
delfunc TmpOpFunc delfunc g:TmpOpFunc
call test_garbagecollect_now() call test_garbagecollect_now()
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
call assert_fails('normal! g@l', 'E117:') call assert_fails('normal! g@l', 'E117:')
call assert_equal([], g:OpFuncArgs) call assert_equal([], g:OpFuncArgs)
" Try to use a function with two arguments for 'operatorfunc' #" Try to use a function with two arguments for 'operatorfunc'
func! MyopFunc2(x, y) func MyopFunc2(x, y)
let g:OpFuncArgs = [a:x, a:y] LET g:OpFuncArgs = [a:x, a:y]
endfunc endfunc
set opfunc=MyopFunc2 set opfunc=MyopFunc2
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
call assert_fails('normal! g@l', 'E119:') call assert_fails('normal! g@l', 'E119:')
call assert_equal([], g:OpFuncArgs) call assert_equal([], g:OpFuncArgs)
" Try to use a lambda function with two arguments for 'operatorfunc' #" Try to use a lambda function with two arguments for 'operatorfunc'
let &opfunc = {a, b -> MyopFunc(23, b)} LET &opfunc = LSTART a, b LMIDDLE MyopFunc(22, b) LEND
let g:OpFuncArgs = [] LET g:OpFuncArgs = []
call assert_fails('normal! g@l', 'E119:') call assert_fails('normal! g@l', 'E119:')
call assert_equal([], g:OpFuncArgs) call assert_equal([], g:OpFuncArgs)
" Test for clearing the 'operatorfunc' option #" Test for clearing the 'operatorfunc' option
set opfunc='' set opfunc=''
set opfunc& set opfunc&
call assert_fails("set opfunc=function('abc')", "E700:") call assert_fails("set opfunc=function('abc')", "E700:")
call assert_fails("set opfunc=funcref('abc')", "E700:") call assert_fails("set opfunc=funcref('abc')", "E700:")
#" set 'operatorfunc' to a non-existing function
LET &opfunc = function('g:MyopFunc', [23])
call assert_fails("set opfunc=function('NonExistingFunc')", 'E700:')
call assert_fails("LET &opfunc = function('NonExistingFunc')", 'E700:')
LET g:OpFuncArgs = []
normal! g@l
call assert_equal([23, 'char'], g:OpFuncArgs)
END
call CheckTransLegacySuccess(lines)
" Using Vim9 lambda expression in legacy context should fail " Using Vim9 lambda expression in legacy context should fail
" set opfunc=(a)\ =>\ MyopFunc(24,\ a) " set opfunc=(a)\ =>\ MyopFunc(24,\ a)
let g:OpFuncArgs = [] let g:OpFuncArgs = []
" call assert_fails('normal! g@l', 'E117:') " call assert_fails('normal! g@l', 'E117:')
call assert_equal([], g:OpFuncArgs) call assert_equal([], g:OpFuncArgs)
" set 'operatorfunc' to a non-existing function " set 'operatorfunc' to a partial with dict. This used to cause a crash.
let &opfunc = function('MyopFunc', [25]) func SetOpFunc()
call assert_fails("set opfunc=function('NonExistingFunc')", 'E700:') let operator = {'execute': function('OperatorExecute')}
call assert_fails("let &opfunc = function('NonExistingFunc')", 'E700:') let &opfunc = operator.execute
let g:OpFuncArgs = [] endfunc
normal! g@l func OperatorExecute(_) dict
call assert_equal([25, 'char'], g:OpFuncArgs) endfunc
call SetOpFunc()
call test_garbagecollect_now()
set operatorfunc=
delfunc SetOpFunc
delfunc OperatorExecute
" Vim9 tests " Vim9 tests
let lines =<< trim END let lines =<< trim END
vim9script vim9script
# Test for using function() # Test for using a def function with opfunc
def g:Vim9opFunc(val: number, type: string): void def g:Vim9opFunc(val: number, type: string): void
g:OpFuncArgs = [val, type] g:OpFuncArgs = [val, type]
enddef enddef
@@ -594,33 +612,6 @@ func Test_opfunc_callback()
g:OpFuncArgs = [] g:OpFuncArgs = []
normal! g@l normal! g@l
assert_equal([60, 'char'], g:OpFuncArgs) assert_equal([60, 'char'], g:OpFuncArgs)
# Test for using a lambda
&opfunc = (a) => Vim9opFunc(61, a)
g:OpFuncArgs = []
normal! g@l
assert_equal([61, 'char'], g:OpFuncArgs)
# Test for using a string(lambda)
&opfunc = '(a) => Vim9opFunc(62, a)'
g:OpFuncArgs = []
normal! g@l
assert_equal([62, 'char'], g:OpFuncArgs)
# Test for using a variable with a lambda expression
var Fn: func = (a) => Vim9opFunc(63, a)
&opfunc = Fn
g:OpFuncArgs = []
normal! g@l
assert_equal([63, 'char'], g:OpFuncArgs)
# Test for using a string(variable with a lambda expression)
Fn = (a) => Vim9opFunc(64, a)
&opfunc = string(Fn)
g:OpFuncArgs = []
normal! g@l
assert_equal([64, 'char'], g:OpFuncArgs)
bw!
END END
" call CheckScriptSuccess(lines) " call CheckScriptSuccess(lines)

View File

@@ -1,6 +1,7 @@
" Test for the quickfix feature. " Test for the quickfix feature.
source check.vim source check.vim
source vim9.vim
CheckFeature quickfix CheckFeature quickfix
source screendump.vim source screendump.vim
@@ -5690,6 +5691,131 @@ func Test_qftextfunc()
call Xtest_qftextfunc('l') call Xtest_qftextfunc('l')
endfunc endfunc
func Test_qftextfunc_callback()
let lines =<< trim END
set efm=%f:%l:%c:%m
#" Test for using a function()
set qftf=function('g:Tqfexpr')
cexpr "F1:1:1:L1"
copen
call assert_equal('F1-L1C1-L1', getline(1))
cclose
#" Using a funcref variable to set 'quickfixtextfunc'
VAR Fn = function('g:Tqfexpr')
LET &qftf = Fn
cexpr "F2:2:2:L2"
copen
call assert_equal('F2-L2C2-L2', getline(1))
cclose
#" Using string(funcref_variable) to set 'quickfixtextfunc'
LET Fn = function('g:Tqfexpr')
LET &qftf = string(Fn)
cexpr "F3:3:3:L3"
copen
call assert_equal('F3-L3C3-L3', getline(1))
cclose
#" Test for using a funcref()
set qftf=funcref('g:Tqfexpr')
cexpr "F4:4:4:L4"
copen
call assert_equal('F4-L4C4-L4', getline(1))
cclose
#" Using a funcref variable to set 'quickfixtextfunc'
LET Fn = funcref('g:Tqfexpr')
LET &qftf = Fn
cexpr "F5:5:5:L5"
copen
call assert_equal('F5-L5C5-L5', getline(1))
cclose
#" Using a string(funcref_variable) to set 'quickfixtextfunc'
LET Fn = funcref('g:Tqfexpr')
LET &qftf = string(Fn)
cexpr "F5:5:5:L5"
copen
call assert_equal('F5-L5C5-L5', getline(1))
cclose
#" Test for using a lambda function with set
VAR optval = "LSTART a LMIDDLE Tqfexpr(a) LEND"
LET optval = substitute(optval, ' ', '\\ ', 'g')
exe "set qftf=" .. optval
cexpr "F6:6:6:L6"
copen
call assert_equal('F6-L6C6-L6', getline(1))
cclose
#" Set 'quickfixtextfunc' to a lambda expression
LET &qftf = LSTART a LMIDDLE Tqfexpr(a) LEND
cexpr "F7:7:7:L7"
copen
call assert_equal('F7-L7C7-L7', getline(1))
cclose
#" Set 'quickfixtextfunc' to string(lambda_expression)
LET &qftf = "LSTART a LMIDDLE Tqfexpr(a) LEND"
cexpr "F8:8:8:L8"
copen
call assert_equal('F8-L8C8-L8', getline(1))
cclose
#" Set 'quickfixtextfunc' to a variable with a lambda expression
VAR Lambda = LSTART a LMIDDLE Tqfexpr(a) LEND
LET &qftf = Lambda
cexpr "F9:9:9:L9"
copen
call assert_equal('F9-L9C9-L9', getline(1))
cclose
#" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
LET Lambda = LSTART a LMIDDLE Tqfexpr(a) LEND
LET &qftf = string(Lambda)
cexpr "F9:9:9:L9"
copen
call assert_equal('F9-L9C9-L9', getline(1))
cclose
END
call CheckLegacyAndVim9Success(lines)
" set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
func SetQftfFunc()
let params = {'qftf': function('g:DictQftfFunc')}
let &quickfixtextfunc = params.qftf
endfunc
func g:DictQftfFunc(_) dict
endfunc
call SetQftfFunc()
new
call SetQftfFunc()
bw
call test_garbagecollect_now()
new
set qftf=
wincmd w
set qftf=
:%bw!
" set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
" to cause a crash.
let &qftf = ''
func SetLocalQftfFunc()
let params = {'qftf': function('g:DictQftfFunc')}
call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
endfunc
call SetLocalQftfFunc()
call test_garbagecollect_now()
call setqflist([], 'a', {'quickfixtextfunc' : ''})
delfunc g:DictQftfFunc
delfunc SetQftfFunc
delfunc SetLocalQftfFunc
set efm&
endfunc
" Test for updating a location list for some other window and check that " Test for updating a location list for some other window and check that
" 'qftextfunc' uses the correct location list. " 'qftextfunc' uses the correct location list.
func Test_qftextfunc_other_loclist() func Test_qftextfunc_other_loclist()

View File

@@ -1,5 +1,7 @@
" Test 'tagfunc' " Test 'tagfunc'
source vim9.vim
func TagFunc(pat, flag, info) func TagFunc(pat, flag, info)
let g:tagfunc_args = [a:pat, a:flag, a:info] let g:tagfunc_args = [a:pat, a:flag, a:info]
let tags = [] let tags = []
@@ -130,56 +132,122 @@ func Test_tagfunc_callback()
return v:null return v:null
endfunc endfunc
" Test for using a function() let lines =<< trim END
set tagfunc=function('MytagFunc1',[10]) #" Test for using a function()
set tagfunc=function('g:MytagFunc1',\ [10])
new | only new | only
let g:MytagFunc1_args = [] LET g:MytagFunc1_args = []
call assert_fails('tag a11', 'E433:') call assert_fails('tag a11', 'E433:')
call assert_equal([10, 'a11', '', {}], g:MytagFunc1_args) call assert_equal([10, 'a11', '', {}], g:MytagFunc1_args)
" Using a funcref variable to set 'tagfunc' #" Using a funcref variable to set 'tagfunc'
let Fn = function('MytagFunc1', [11]) VAR Fn = function('g:MytagFunc1', [11])
let &tagfunc = Fn LET &tagfunc = Fn
new | only new | only
let g:MytagFunc1_args = [] LET g:MytagFunc1_args = []
call assert_fails('tag a12', 'E433:') call assert_fails('tag a12', 'E433:')
call assert_equal([11, 'a12', '', {}], g:MytagFunc1_args) call assert_equal([11, 'a12', '', {}], g:MytagFunc1_args)
" Using a string(funcref_variable) to set 'tagfunc' #" Using a string(funcref_variable) to set 'tagfunc'
let Fn = function('MytagFunc1', [12]) LET Fn = function('g:MytagFunc1', [12])
let &tagfunc = string(Fn) LET &tagfunc = string(Fn)
new | only new | only
let g:MytagFunc1_args = [] LET g:MytagFunc1_args = []
call assert_fails('tag a12', 'E433:') call assert_fails('tag a12', 'E433:')
call assert_equal([12, 'a12', '', {}], g:MytagFunc1_args) call assert_equal([12, 'a12', '', {}], g:MytagFunc1_args)
" Test for using a funcref() #" Test for using a funcref()
func MytagFunc2(pat, flags, info) set tagfunc=funcref('g:MytagFunc1',\ [13])
let g:MytagFunc2_args = [a:pat, a:flags, a:info]
return v:null
endfunc
set tagfunc=funcref('MytagFunc1',[13])
new | only new | only
let g:MytagFunc1_args = [] LET g:MytagFunc1_args = []
call assert_fails('tag a13', 'E433:') call assert_fails('tag a13', 'E433:')
call assert_equal([13, 'a13', '', {}], g:MytagFunc1_args) call assert_equal([13, 'a13', '', {}], g:MytagFunc1_args)
" Using a funcref variable to set 'tagfunc' #" Using a funcref variable to set 'tagfunc'
let Fn = funcref('MytagFunc1', [14]) LET Fn = funcref('g:MytagFunc1', [14])
let &tagfunc = Fn LET &tagfunc = Fn
new | only new | only
let g:MytagFunc1_args = [] LET g:MytagFunc1_args = []
call assert_fails('tag a14', 'E433:') call assert_fails('tag a14', 'E433:')
call assert_equal([14, 'a14', '', {}], g:MytagFunc1_args) call assert_equal([14, 'a14', '', {}], g:MytagFunc1_args)
" Using a string(funcref_variable) to set 'tagfunc' #" Using a string(funcref_variable) to set 'tagfunc'
let Fn = funcref('MytagFunc1', [15]) LET Fn = funcref('g:MytagFunc1', [15])
let &tagfunc = string(Fn) LET &tagfunc = string(Fn)
new | only new | only
let g:MytagFunc1_args = [] LET g:MytagFunc1_args = []
call assert_fails('tag a14', 'E433:') call assert_fails('tag a14', 'E433:')
call assert_equal([15, 'a14', '', {}], g:MytagFunc1_args) call assert_equal([15, 'a14', '', {}], g:MytagFunc1_args)
#" Test for using a lambda function
VAR optval = "LSTART a, b, c LMIDDLE MytagFunc1(16, a, b, c) LEND"
LET optval = substitute(optval, ' ', '\\ ', 'g')
exe "set tagfunc=" .. optval
new | only
LET g:MytagFunc1_args = []
call assert_fails('tag a17', 'E433:')
call assert_equal([16, 'a17', '', {}], g:MytagFunc1_args)
#" Set 'tagfunc' to a lambda expression
LET &tagfunc = LSTART a, b, c LMIDDLE MytagFunc1(17, a, b, c) LEND
new | only
LET g:MytagFunc1_args = []
call assert_fails('tag a18', 'E433:')
call assert_equal([17, 'a18', '', {}], g:MytagFunc1_args)
#" Set 'tagfunc' to a string(lambda expression)
LET &tagfunc = 'LSTART a, b, c LMIDDLE MytagFunc1(18, a, b, c) LEND'
new | only
LET g:MytagFunc1_args = []
call assert_fails('tag a18', 'E433:')
call assert_equal([18, 'a18', '', {}], g:MytagFunc1_args)
#" Set 'tagfunc' to a variable with a lambda expression
VAR Lambda = LSTART a, b, c LMIDDLE MytagFunc1(19, a, b, c) LEND
LET &tagfunc = Lambda
new | only
LET g:MytagFunc1_args = []
call assert_fails("tag a19", "E433:")
call assert_equal([19, 'a19', '', {}], g:MytagFunc1_args)
#" Set 'tagfunc' to a string(variable with a lambda expression)
LET Lambda = LSTART a, b, c LMIDDLE MytagFunc1(20, a, b, c) LEND
LET &tagfunc = string(Lambda)
new | only
LET g:MytagFunc1_args = []
call assert_fails("tag a19", "E433:")
call assert_equal([20, 'a19', '', {}], g:MytagFunc1_args)
#" Test for using a lambda function with incorrect return value
LET Lambda = LSTART a, b, c LMIDDLE strlen(a) LEND
LET &tagfunc = string(Lambda)
new | only
call assert_fails("tag a20", "E987:")
#" Test for clearing the 'tagfunc' option
set tagfunc=''
set tagfunc&
call assert_fails("set tagfunc=function('abc')", "E700:")
call assert_fails("set tagfunc=funcref('abc')", "E700:")
#" set 'tagfunc' to a non-existing function
LET &tagfunc = function('g:MytagFunc1', [21])
call assert_fails("set tagfunc=function('NonExistingFunc')", 'E700:')
call assert_fails("LET &tagfunc = function('NonExistingFunc')", 'E700:')
call assert_fails("tag axb123", 'E426:')
END
call CheckLegacyAndVim9Success(lines)
let &tagfunc = "{a -> 'abc'}"
call assert_fails("echo taglist('a')", "E987:")
" Using Vim9 lambda expression in legacy context should fail
" set tagfunc=(a,\ b,\ c)\ =>\ g:MytagFunc1(21,\ a,\ b,\ c)
new | only
let g:MytagFunc1_args = []
" call assert_fails("tag a17", "E117:")
call assert_equal([], g:MytagFunc1_args)
" Test for using a script local function " Test for using a script local function
set tagfunc=<SID>ScriptLocalTagFunc set tagfunc=<SID>ScriptLocalTagFunc
new | only new | only
@@ -203,70 +271,25 @@ func Test_tagfunc_callback()
call assert_fails('tag a16', 'E433:') call assert_fails('tag a16', 'E433:')
call assert_equal(['a16', '', {}], g:ScriptLocalFuncArgs) call assert_equal(['a16', '', {}], g:ScriptLocalFuncArgs)
" Test for using a lambda function " set 'tagfunc' to a partial with dict. This used to cause a crash.
set tagfunc={a,\ b,\ c\ ->\ MytagFunc1(16,\ a,\ b,\ c)} func SetTagFunc()
new | only let params = {'tagfn': function('g:DictTagFunc')}
let g:MytagFunc1_args = [] let &tagfunc = params.tagfn
call assert_fails('tag a17', 'E433:') endfunc
call assert_equal([16, 'a17', '', {}], g:MytagFunc1_args) func g:DictTagFunc(_) dict
endfunc
" Set 'tagfunc' to a lambda expression call SetTagFunc()
let &tagfunc = {a, b, c -> MytagFunc1(17, a, b, c)} new
new | only call SetTagFunc()
let g:MytagFunc1_args = [] bw
call assert_fails('tag a18', 'E433:') call test_garbagecollect_now()
call assert_equal([17, 'a18', '', {}], g:MytagFunc1_args) new
set tagfunc=
" Set 'tagfunc' to a string(lambda expression) wincmd w
let &tagfunc = '{a, b, c -> MytagFunc1(18, a, b, c)}' set tagfunc=
new | only :%bw!
let g:MytagFunc1_args = [] delfunc g:DictTagFunc
call assert_fails('tag a18', 'E433:') delfunc SetTagFunc
call assert_equal([18, 'a18', '', {}], g:MytagFunc1_args)
" Set 'tagfunc' to a variable with a lambda expression
let Lambda = {a, b, c -> MytagFunc1(19, a, b, c)}
let &tagfunc = Lambda
new | only
let g:MytagFunc1_args = []
call assert_fails("tag a19", "E433:")
call assert_equal([19, 'a19', '', {}], g:MytagFunc1_args)
" Set 'tagfunc' to a string(variable with a lambda expression)
let Lambda = {a, b, c -> MytagFunc1(20, a, b, c)}
let &tagfunc = string(Lambda)
new | only
let g:MytagFunc1_args = []
call assert_fails("tag a19", "E433:")
call assert_equal([20, 'a19', '', {}], g:MytagFunc1_args)
" 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 a20", "E987:")
" Test for clearing the 'tagfunc' option
set tagfunc=''
set tagfunc&
call assert_fails("set tagfunc=function('abc')", "E700:")
call assert_fails("set tagfunc=funcref('abc')", "E700:")
let &tagfunc = "{a -> 'abc'}"
call assert_fails("echo taglist('a')", "E987:")
" Using Vim9 lambda expression in legacy context should fail
" set tagfunc=(a,\ b,\ c)\ =>\ g:MytagFunc1(21,\ a,\ b,\ c)
new | only
let g:MytagFunc1_args = []
" call assert_fails("tag a17", "E117:")
call assert_equal([], g:MytagFunc1_args)
" set 'tagfunc' to a non-existing function
call assert_fails("set tagfunc=function('NonExistingFunc')", 'E700:')
call assert_fails("let &tagfunc = function('NonExistingFunc')", 'E700:')
call assert_fails("tag axb123", 'E426:')
bw!
" Vim9 tests " Vim9 tests
let lines =<< trim END let lines =<< trim END
@@ -282,42 +305,11 @@ func Test_tagfunc_callback()
g:Vim9tagFuncArgs = [] g:Vim9tagFuncArgs = []
assert_fails('tag a10', 'E433:') assert_fails('tag a10', 'E433:')
assert_equal([60, 'a10', '', {}], g:Vim9tagFuncArgs) assert_equal([60, 'a10', '', {}], g:Vim9tagFuncArgs)
# Test for using a lambda
&tagfunc = (a, b, c) => MytagFunc1(61, a, b, c)
new | only
g:MytagFunc1_args = []
assert_fails('tag a20', 'E433:')
assert_equal([61, 'a20', '', {}], g:MytagFunc1_args)
# Test for using a string(lambda)
&tagfunc = '(a, b, c) => MytagFunc1(62, a, b, c)'
new | only
g:MytagFunc1_args = []
assert_fails('tag a20', 'E433:')
assert_equal([62, 'a20', '', {}], g:MytagFunc1_args)
# Test for using a variable with a lambda expression
var Fn: func = (a, b, c) => MytagFunc1(63, a, b, c)
&tagfunc = Fn
new | only
g:MytagFunc1_args = []
assert_fails('tag a30', 'E433:')
assert_equal([63, 'a30', '', {}], g:MytagFunc1_args)
# Test for using a variable with a lambda expression
Fn = (a, b, c) => MytagFunc1(64, a, b, c)
&tagfunc = string(Fn)
new | only
g:MytagFunc1_args = []
assert_fails('tag a30', 'E433:')
assert_equal([64, 'a30', '', {}], g:MytagFunc1_args)
END END
" call CheckScriptSuccess(lines) " call CheckScriptSuccess(lines)
" cleanup " cleanup
delfunc MytagFunc1 delfunc MytagFunc1
delfunc MytagFunc2
set tagfunc& set tagfunc&
%bw! %bw!
endfunc endfunc

80
src/nvim/testdir/vim9.vim Normal file
View File

@@ -0,0 +1,80 @@
" Use a different file name for each run.
let s:sequence = 1
" Check that "lines" inside a legacy function has no error.
func CheckLegacySuccess(lines)
let cwd = getcwd()
let fname = 'XlegacySuccess' .. s:sequence
let s:sequence += 1
call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
try
exe 'so ' .. fname
call Func()
finally
delfunc! Func
call chdir(cwd)
call delete(fname)
endtry
endfunc
" Check that "lines" inside a legacy function results in the expected error
func CheckLegacyFailure(lines, error)
let cwd = getcwd()
let fname = 'XlegacyFails' .. s:sequence
let s:sequence += 1
call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
try
call assert_fails('so ' .. fname, a:error)
finally
delfunc! Func
call chdir(cwd)
call delete(fname)
endtry
endfunc
" Execute "lines" in a legacy function, translated as in
" CheckLegacyAndVim9Success()
func CheckTransLegacySuccess(lines)
let legacylines = a:lines->deepcopy()->map({_, v ->
\ v->substitute('\<VAR\>', 'let', 'g')
\ ->substitute('\<LET\>', 'let', 'g')
\ ->substitute('\<LSTART\>', '{', 'g')
\ ->substitute('\<LMIDDLE\>', '->', 'g')
\ ->substitute('\<LEND\>', '}', 'g')
\ ->substitute('\<TRUE\>', '1', 'g')
\ ->substitute('\<FALSE\>', '0', 'g')
\ ->substitute('#"', ' "', 'g')
\ })
call CheckLegacySuccess(legacylines)
endfunc
" Execute "lines" in a legacy function
" Use 'VAR' for a declaration.
" Use 'LET' for an assignment
" Use ' #"' for a comment
" Use LSTART arg LMIDDLE expr LEND for lambda
" Use 'TRUE' for 1
" Use 'FALSE' for 0
func CheckLegacyAndVim9Success(lines)
call CheckTransLegacySuccess(a:lines)
endfunc
" Execute "lines" in a legacy function
" Use 'VAR' for a declaration.
" Use 'LET' for an assignment
" Use ' #"' for a comment
func CheckLegacyAndVim9Failure(lines, error)
if type(a:error) == type('string')
let legacyError = error
else
let legacyError = error[0]
endif
let legacylines = a:lines->deepcopy()->map({_, v ->
\ v->substitute('\<VAR\>', 'let', 'g')
\ ->substitute('\<LET\>', 'let', 'g')
\ ->substitute('#"', ' "', 'g')
\ })
call CheckLegacyFailure(legacylines, legacyError)
endfunc