diff options
author | Yegappan Lakshmanan <yegappan@yahoo.com> | 2021-12-12 16:26:44 +0000 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2021-12-12 16:26:44 +0000 |
commit | 6ae8fae8696623b527c7fb22567f6a3705b2f0dd (patch) | |
tree | 5013ad6590516571ae06f992906c4270d7f03b45 /src/testdir/test_ins_complete.vim | |
parent | 6e371ecb27227ff8fedd8561d0f3880a17576848 (diff) |
patch 8.2.3788: lambda for option that is a function may be freedv8.2.3788
Problem: Lambda for option that is a function may be garbage collected.
Solution: Set a reference in the funcref. (Yegappan Lakshmanan,
closes #9330)
Diffstat (limited to 'src/testdir/test_ins_complete.vim')
-rw-r--r-- | src/testdir/test_ins_complete.vim | 957 |
1 files changed, 465 insertions, 492 deletions
diff --git a/src/testdir/test_ins_complete.vim b/src/testdir/test_ins_complete.vim index 6a99ecd3eb..1303a13545 100644 --- a/src/testdir/test_ins_complete.vim +++ b/src/testdir/test_ins_complete.vim @@ -875,154 +875,179 @@ func Test_completefunc_callback() return a:findstart ? 0 : [] endfunc - " Test for using a function() - set completefunc=function('MycompleteFunc1',[10]) - new | only - call setline(1, 'one') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args) - bw! + let lines =<< trim END + #" Test for using a function() + set completefunc=function('g:MycompleteFunc1',\ [10]) + new | only + call setline(1, 'one') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args) + bw! - " Using a funcref variable to set 'completefunc' - let Fn = function('MycompleteFunc1', [11]) - let &completefunc = Fn - new | only - call setline(1, 'two') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args) - bw! + #" Using a funcref variable to set 'completefunc' + VAR Fn = function('g:MycompleteFunc1', [11]) + LET &completefunc = Fn + new | only + call setline(1, 'two') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args) + bw! - " Using string(funcref_variable) to set 'completefunc' - let Fn = function('MycompleteFunc1', [12]) - let &completefunc = string(Fn) - new | only - call setline(1, 'two') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args) - bw! + #" Using string(funcref_variable) to set 'completefunc' + LET Fn = function('g:MycompleteFunc1', [12]) + LET &completefunc = string(Fn) + new | only + call setline(1, 'two') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args) + bw! - " Test for using a funcref() - set completefunc=funcref('MycompleteFunc1',\ [13]) - new | only - call setline(1, 'three') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args) - bw! + #" Test for using a funcref() + set completefunc=funcref('g:MycompleteFunc1',\ [13]) + new | only + call setline(1, 'three') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args) + bw! - " Using a funcref variable to set 'completefunc' - let Fn = funcref('MycompleteFunc1', [14]) - let &completefunc = Fn - new | only - call setline(1, 'four') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args) - bw! + #" Using a funcref variable to set 'completefunc' + LET Fn = funcref('g:MycompleteFunc1', [14]) + LET &completefunc = Fn + new | only + call setline(1, 'four') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args) + bw! - " Using a string(funcref_variable) to set 'completefunc' - let Fn = funcref('MycompleteFunc1', [15]) - let &completefunc = string(Fn) - new | only - call setline(1, 'four') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args) - bw! + #" Using a string(funcref_variable) to set 'completefunc' + LET Fn = funcref('g:MycompleteFunc1', [15]) + LET &completefunc = string(Fn) + new | only + call setline(1, 'four') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args) + bw! - " Test for using a lambda function - set completefunc={a,\ b\ ->\ MycompleteFunc1(16,\ a,\ b)} - new | only - call setline(1, 'five') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args) - bw! + #" Test for using a lambda function with set + VAR optval = "LSTART a, b LMIDDLE MycompleteFunc1(16, a, b) LEND" + LET optval = substitute(optval, ' ', '\\ ', 'g') + exe "set completefunc=" .. optval + new | only + call setline(1, 'five') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args) + bw! - " Set 'completefunc' to a lambda expression - let &completefunc = {a, b -> MycompleteFunc1(17, a, b)} - new | only - call setline(1, 'six') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args) - bw! + #" Set 'completefunc' to a lambda expression + LET &completefunc = LSTART a, b LMIDDLE MycompleteFunc1(17, a, b) LEND + new | only + call setline(1, 'six') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args) + bw! - " Set 'completefunc' to string(lambda_expression) - let &completefunc = '{a, b -> MycompleteFunc1(18, a, b)}' - new | only - call setline(1, 'six') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args) - bw! + #" Set 'completefunc' to string(lambda_expression) + LET &completefunc = 'LSTART a, b LMIDDLE MycompleteFunc1(18, a, b) LEND' + new | only + call setline(1, 'six') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args) + bw! - " Set 'completefunc' to a variable with a lambda expression - let Lambda = {a, b -> MycompleteFunc1(19, a, b)} - let &completefunc = Lambda - new | only - call setline(1, 'seven') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args) - bw! + #" Set 'completefunc' to a variable with a lambda expression + VAR Lambda = LSTART a, b LMIDDLE MycompleteFunc1(19, a, b) LEND + LET &completefunc = Lambda + new | only + call setline(1, 'seven') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args) + bw! - " Set 'completefunc' to a string(variable with a lambda expression) - let Lambda = {a, b -> MycompleteFunc1(20, a, b)} - let &completefunc = string(Lambda) - new | only - call setline(1, 'seven') - let g:MycompleteFunc1_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args) - bw! + #" Set 'completefunc' to a string(variable with a lambda expression) + LET Lambda = LSTART a, b LMIDDLE MycompleteFunc1(20, a, b) LEND + LET &completefunc = string(Lambda) + new | only + call setline(1, 'seven') + LET g:MycompleteFunc1_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args) + bw! - " Test for using a lambda function with incorrect return value - let Lambda = {s -> strlen(s)} - let &completefunc = Lambda - new | only - call setline(1, 'eight') - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - bw! + #" Test for using a lambda function with incorrect return value + LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND + LET &completefunc = Lambda + new | only + call setline(1, 'eight') + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + bw! - " Test for clearing the 'completefunc' option - set completefunc='' - set completefunc& + #" Test for clearing the 'completefunc' option + set completefunc='' + set completefunc& + call assert_fails("set completefunc=function('abc')", "E700:") + call assert_fails("set completefunc=funcref('abc')", "E700:") + + #" set 'completefunc' to a non-existing function + func MycompleteFunc2(findstart, base) + call add(g:MycompleteFunc2_args, [a:findstart, a:base]) + return a:findstart ? 0 : [] + endfunc + set completefunc=MycompleteFunc2 + call setline(1, 'five') + call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') + call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:') + LET g:MycompleteFunc2_args = [] + call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') + call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args) + bw! + END + call CheckLegacyAndVim9Success(lines) - call assert_fails("set completefunc=function('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=(a,\ b)\ =>\ g:MycompleteFunc1(21,\ a,\ b) + 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 non-existing function - func MycompleteFunc2(findstart, base) - call add(g:MycompleteFunc2_args, [a:findstart, a:base]) - return a:findstart ? 0 : [] + " 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 - set completefunc=MycompleteFunc2 - call setline(1, 'five') - call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') - call assert_fails("let &completefunc = function('NonExistingFunc')", 'E700:') - let g:MycompleteFunc2_args = [] - call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') - call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args) - bw! + 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 let lines =<< trim END vim9script - # Test for using function() + # Test for using a def function with completefunc def Vim9CompleteFunc(val: number, findstart: number, base: string): any add(g:Vim9completeFuncArgs, [val, findstart, base]) return findstart ? 0 : [] @@ -1034,44 +1059,6 @@ func Test_completefunc_callback() feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) 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 call CheckScriptSuccess(lines) @@ -1089,154 +1076,179 @@ func Test_omnifunc_callback() return a:findstart ? 0 : [] endfunc - " Test for using a function() - set omnifunc=function('MyomniFunc1',[10]) - new | only - call setline(1, 'one') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MyomniFunc1_args) - bw! + let lines =<< trim END + #" Test for using a function() + set omnifunc=function('g:MyomniFunc1',\ [10]) + new | only + call setline(1, 'one') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MyomniFunc1_args) + bw! - " Using a funcref variable to set 'omnifunc' - let Fn = function('MyomniFunc1', [11]) - let &omnifunc = Fn - new | only - call setline(1, 'two') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MyomniFunc1_args) - bw! + #" Using a funcref variable to set 'omnifunc' + VAR Fn = function('g:MyomniFunc1', [11]) + LET &omnifunc = Fn + new | only + call setline(1, 'two') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MyomniFunc1_args) + bw! - " Using a string(funcref_variable) to set 'omnifunc' - let Fn = function('MyomniFunc1', [12]) - let &omnifunc = string(Fn) - new | only - call setline(1, 'two') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MyomniFunc1_args) - bw! + #" Using a string(funcref_variable) to set 'omnifunc' + LET Fn = function('g:MyomniFunc1', [12]) + LET &omnifunc = string(Fn) + new | only + call setline(1, 'two') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MyomniFunc1_args) + bw! - " Test for using a funcref() - set omnifunc=funcref('MyomniFunc1',\ [13]) - new | only - call setline(1, 'three') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MyomniFunc1_args) - bw! + #" Test for using a funcref() + set omnifunc=funcref('g:MyomniFunc1',\ [13]) + new | only + call setline(1, 'three') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MyomniFunc1_args) + bw! - " Using a funcref variable to set 'omnifunc' - let Fn = funcref('MyomniFunc1', [14]) - let &omnifunc = Fn - new | only - call setline(1, 'four') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MyomniFunc1_args) - bw! + #" Use let to set 'omnifunc' to a funcref + LET Fn = funcref('g:MyomniFunc1', [14]) + LET &omnifunc = Fn + new | only + call setline(1, 'four') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MyomniFunc1_args) + bw! - " Using a string(funcref_variable) to set 'omnifunc' - let Fn = funcref('MyomniFunc1', [15]) - let &omnifunc = string(Fn) - new | only - call setline(1, 'four') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MyomniFunc1_args) - bw! + #" Using a string(funcref) to set 'omnifunc' + LET Fn = funcref("g:MyomniFunc1", [15]) + LET &omnifunc = string(Fn) + new | only + call setline(1, 'four') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MyomniFunc1_args) + bw! - " Test for using a lambda function - set omnifunc={a,\ b\ ->\ MyomniFunc1(16,\ a,\ b)} - new | only - call setline(1, 'five') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MyomniFunc1_args) - bw! + #" Test for using a lambda function with set + VAR optval = "LSTART a, b LMIDDLE MyomniFunc1(16, a, b) LEND" + LET optval = substitute(optval, ' ', '\\ ', 'g') + exe "set omnifunc=" .. optval + new | only + call setline(1, 'five') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MyomniFunc1_args) + bw! - " Set 'omnifunc' to a lambda expression - let &omnifunc = {a, b -> MyomniFunc1(17, a, b)} - new | only - call setline(1, 'six') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MyomniFunc1_args) - bw! + #" Set 'omnifunc' to a lambda expression + LET &omnifunc = LSTART a, b LMIDDLE MyomniFunc1(17, a, b) LEND + new | only + call setline(1, 'six') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MyomniFunc1_args) + bw! - " Set 'omnifunc' to a string(lambda_expression) - let &omnifunc = '{a, b -> MyomniFunc1(18, a, b)}' - new | only - call setline(1, 'six') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MyomniFunc1_args) - bw! + #" Set 'omnifunc' to a string(lambda_expression) + LET &omnifunc = 'LSTART a, b LMIDDLE MyomniFunc1(18, a, b) LEND' + new | only + call setline(1, 'six') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MyomniFunc1_args) + bw! - " Set 'omnifunc' to a variable with a lambda expression - let Lambda = {a, b -> MyomniFunc1(19, a, b)} - let &omnifunc = Lambda - new | only - call setline(1, 'seven') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MyomniFunc1_args) - bw! + #" Set 'omnifunc' to a variable with a lambda expression + VAR Lambda = LSTART a, b LMIDDLE MyomniFunc1(19, a, b) LEND + LET &omnifunc = Lambda + new | only + call setline(1, 'seven') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MyomniFunc1_args) + bw! - " Set 'omnifunc' to a string(variable with a lambda expression) - let Lambda = {a, b -> MyomniFunc1(20, a, b)} - let &omnifunc = string(Lambda) - new | only - call setline(1, 'seven') - let g:MyomniFunc1_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MyomniFunc1_args) - bw! + #" Set 'omnifunc' to a string(variable with a lambda expression) + LET Lambda = LSTART a, b LMIDDLE MyomniFunc1(20, a, b) LEND + LET &omnifunc = string(Lambda) + new | only + call setline(1, 'seven') + LET g:MyomniFunc1_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MyomniFunc1_args) + bw! - " Test for using a lambda function with incorrect return value - let Lambda = {s -> strlen(s)} - let &omnifunc = Lambda - new | only - call setline(1, 'eight') - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - bw! + #" Test for using a lambda function with incorrect return value + LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND + LET &omnifunc = Lambda + new | only + call setline(1, 'eight') + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + bw! - " Test for clearing the 'omnifunc' option - set omnifunc='' - set omnifunc& + #" Test for clearing the 'omnifunc' option + set omnifunc='' + set omnifunc& + call assert_fails("set omnifunc=function('abc')", "E700:") + call assert_fails("set omnifunc=funcref('abc')", "E700:") + + #" set 'omnifunc' to a non-existing function + func MyomniFunc2(findstart, base) + call add(g:MyomniFunc2_args, [a:findstart, a:base]) + return a:findstart ? 0 : [] + endfunc + set omnifunc=MyomniFunc2 + call setline(1, 'nine') + call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') + call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:') + LET g:MyomniFunc2_args = [] + call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') + call assert_equal([[1, ''], [0, 'nine']], g:MyomniFunc2_args) + bw! + END + call CheckLegacyAndVim9Success(lines) - call assert_fails("set omnifunc=function('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=(a,\ b)\ =>\ g:MyomniFunc1(21,\ a,\ b) + 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 non-existing function - func MyomniFunc2(findstart, base) - call add(g:MyomniFunc2_args, [a:findstart, a:base]) - return a:findstart ? 0 : [] + " 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 - set omnifunc=MyomniFunc2 - call setline(1, 'nine') - call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') - call assert_fails("let &omnifunc = function('NonExistingFunc')", 'E700:') - let g:MyomniFunc2_args = [] - call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') - call assert_equal([[1, ''], [0, 'nine']], g:MyomniFunc2_args) - bw! + 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 let lines =<< trim END vim9script - # Test for using function() + # Test for using a def function with omnifunc def Vim9omniFunc(val: number, findstart: number, base: string): any add(g:Vim9omniFunc_Args, [val, findstart, base]) return findstart ? 0 : [] @@ -1248,44 +1260,6 @@ func Test_omnifunc_callback() feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) 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 call CheckScriptSuccess(lines) @@ -1303,178 +1277,215 @@ func Test_thesaurusfunc_callback() return a:findstart ? 0 : [] endfunc - " Test for using a function() - set thesaurusfunc=function('MytsrFunc1',[10]) - new | only - call setline(1, 'one') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MytsrFunc1_args) - bw! + let lines =<< trim END + #" Test for using a function() + set thesaurusfunc=function('g:MytsrFunc1',\ [10]) + new | only + call setline(1, 'one') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MytsrFunc1_args) + bw! - " Using a funcref variable to set 'thesaurusfunc' - let Fn = function('MytsrFunc1', [11]) - let &thesaurusfunc = Fn - new | only - call setline(1, 'two') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MytsrFunc1_args) - bw! + #" Using a funcref variable to set 'thesaurusfunc' + VAR Fn = function('g:MytsrFunc1', [11]) + LET &thesaurusfunc = Fn + new | only + call setline(1, 'two') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MytsrFunc1_args) + bw! - " Using a string(funcref_variable) to set 'thesaurusfunc' - let Fn = function('MytsrFunc1', [12]) - let &thesaurusfunc = string(Fn) - new | only - call setline(1, 'two') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MytsrFunc1_args) - bw! + #" Using a string(funcref_variable) to set 'thesaurusfunc' + LET Fn = function('g:MytsrFunc1', [12]) + LET &thesaurusfunc = string(Fn) + new | only + call setline(1, 'two') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MytsrFunc1_args) + bw! - " Test for using a funcref() - set thesaurusfunc=funcref('MytsrFunc1',[13]) - new | only - call setline(1, 'three') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MytsrFunc1_args) - bw! + #" Test for using a funcref() + set thesaurusfunc=funcref('g:MytsrFunc1',\ [13]) + new | only + call setline(1, 'three') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MytsrFunc1_args) + bw! - " Using a funcref variable to set 'thesaurusfunc' - let Fn = funcref('MytsrFunc1', [14]) - let &thesaurusfunc = Fn - new | only - call setline(1, 'four') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MytsrFunc1_args) - bw! + #" Using a funcref variable to set 'thesaurusfunc' + LET Fn = funcref('g:MytsrFunc1', [14]) + LET &thesaurusfunc = Fn + new | only + call setline(1, 'four') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MytsrFunc1_args) + bw! - " Using a string(funcref_variable) to set 'thesaurusfunc' - let Fn = funcref('MytsrFunc1', [15]) - let &thesaurusfunc = string(Fn) - new | only - call setline(1, 'four') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MytsrFunc1_args) - bw! + #" Using a string(funcref_variable) to set 'thesaurusfunc' + LET Fn = funcref('g:MytsrFunc1', [15]) + LET &thesaurusfunc = string(Fn) + new | only + call setline(1, 'four') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MytsrFunc1_args) + bw! - " Test for using a lambda function - set thesaurusfunc={a,\ b\ ->\ MytsrFunc1(16,\ a,\ b)} - new | only - call setline(1, 'five') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MytsrFunc1_args) - bw! + #" Test for using a lambda function + VAR optval = "LSTART a, b LMIDDLE MytsrFunc1(16, a, b) LEND" + LET optval = substitute(optval, ' ', '\\ ', 'g') + exe "set thesaurusfunc=" .. optval + new | only + call setline(1, 'five') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MytsrFunc1_args) + bw! - " Set 'thesaurusfunc' to a lambda expression - let &thesaurusfunc = {a, b -> MytsrFunc1(17, a, b)} - new | only - call setline(1, 'six') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MytsrFunc1_args) - bw! + #" Test for using a lambda function with set + LET &thesaurusfunc = LSTART a, b LMIDDLE MytsrFunc1(17, a, b) LEND + new | only + call setline(1, 'six') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MytsrFunc1_args) + bw! - " Set 'thesaurusfunc' to a string(lambda expression) - let &thesaurusfunc = '{a, b -> MytsrFunc1(18, a, b)}' - new | only - call setline(1, 'six') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MytsrFunc1_args) - bw! + #" Set 'thesaurusfunc' to a string(lambda expression) + LET &thesaurusfunc = 'LSTART a, b LMIDDLE MytsrFunc1(18, a, b) LEND' + new | only + call setline(1, 'six') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MytsrFunc1_args) + bw! - " Set 'thesaurusfunc' to a variable with a lambda expression - let Lambda = {a, b -> MytsrFunc1(19, a, b)} - let &thesaurusfunc = Lambda - new | only - call setline(1, 'seven') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MytsrFunc1_args) - bw! + #" Set 'thesaurusfunc' to a variable with a lambda expression + VAR Lambda = LSTART a, b LMIDDLE MytsrFunc1(19, a, b) LEND + LET &thesaurusfunc = Lambda + new | only + call setline(1, 'seven') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MytsrFunc1_args) + bw! - " Set 'thesaurusfunc' to a string(variable with a lambda expression) - let Lambda = {a, b -> MytsrFunc1(20, a, b)} - let &thesaurusfunc = string(Lambda) - new | only - call setline(1, 'seven') - let g:MytsrFunc1_args = [] - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MytsrFunc1_args) - bw! + #" Set 'thesaurusfunc' to a string(variable with a lambda expression) + LET Lambda = LSTART a, b LMIDDLE MytsrFunc1(20, a, b) LEND + LET &thesaurusfunc = string(Lambda) + new | only + call setline(1, 'seven') + LET g:MytsrFunc1_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MytsrFunc1_args) + bw! - " Test for using a lambda function with incorrect return value - let Lambda = {s -> strlen(s)} - let &thesaurusfunc = Lambda - new | only - call setline(1, 'eight') - call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') - bw! + #" Test for using a lambda function with incorrect return value + LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND + LET &thesaurusfunc = Lambda + new | only + call setline(1, 'eight') + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + bw! - " Test for clearing the 'thesaurusfunc' option - set thesaurusfunc='' - set thesaurusfunc& + #" Test for clearing the 'thesaurusfunc' option + set thesaurusfunc='' + set thesaurusfunc& + call assert_fails("set thesaurusfunc=function('abc')", "E700:") + call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") + + #" set 'thesaurusfunc' to a non-existing function + func MytsrFunc2(findstart, base) + call add(g:MytsrFunc2_args, [a:findstart, a:base]) + return a:findstart ? 0 : ['sunday'] + endfunc + set thesaurusfunc=MytsrFunc2 + call setline(1, 'ten') + call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') + call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:') + LET g:MytsrFunc2_args = [] + call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') + call assert_equal([[1, ''], [0, 'ten']], g:MytsrFunc2_args) + 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) - call assert_fails("set thesaurusfunc=function('abc')", "E700:") - call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") let &thesaurusfunc = {a -> 'abc'} call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |