Mercurial > vim
comparison src/testdir/test_iminsert.vim @ 26441:65ab0b035dd8 v8.2.3751
patch 8.2.3751: cannot assign a lambda to an option that takes a function
Commit: https://github.com/vim/vim/commit/6409553b6e3b4de4e1d72b8ee5445595214581ff
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Mon Dec 6 11:03:55 2021 +0000
patch 8.2.3751: cannot assign a lambda to an option that takes a function
Problem: Cannot assign a lambda to an option that takes a function.
Solution: Automatically convert the lambda to a string. (Yegappan
Lakshmanan, closes #9286)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 06 Dec 2021 12:15:04 +0100 |
parents | 8f17f8f327f3 |
children | 65b4109a4297 |
comparison
equal
deleted
inserted
replaced
26440:f6f5f604c17c | 26441:65ab0b035dd8 |
---|---|
128 set imstatusfunc=function('IMstatusfunc1') | 128 set imstatusfunc=function('IMstatusfunc1') |
129 normal! i | 129 normal! i |
130 | 130 |
131 " Using a funcref variable to set 'completefunc' | 131 " Using a funcref variable to set 'completefunc' |
132 let Fn1 = function('IMactivatefunc1') | 132 let Fn1 = function('IMactivatefunc1') |
133 let &imactivatefunc = Fn1 | |
134 let Fn2 = function('IMstatusfunc1') | |
135 let &imstatusfunc = Fn2 | |
136 normal! i | |
137 | |
138 " Using a string(funcref variable) to set 'completefunc' | |
133 let &imactivatefunc = string(Fn1) | 139 let &imactivatefunc = string(Fn1) |
134 let Fn2 = function('IMstatusfunc1') | |
135 let &imstatusfunc = string(Fn2) | 140 let &imstatusfunc = string(Fn2) |
136 normal! i | 141 normal! i |
137 call assert_fails('let &imactivatefunc = Fn1', 'E729:') | |
138 call assert_fails('let &imstatusfunc = Fn2', 'E729:') | |
139 | 142 |
140 " Test for using a funcref() | 143 " Test for using a funcref() |
141 set imactivatefunc=funcref('IMactivatefunc1') | 144 set imactivatefunc=funcref('IMactivatefunc1') |
142 set imstatusfunc=funcref('IMstatusfunc1') | 145 set imstatusfunc=funcref('IMstatusfunc1') |
143 normal! i | 146 normal! i |
144 | 147 |
145 " Using a funcref variable to set 'imactivatefunc' | 148 " Using a funcref variable to set 'imactivatefunc' |
146 let Fn1 = funcref('IMactivatefunc1') | 149 let Fn1 = funcref('IMactivatefunc1') |
150 let &imactivatefunc = Fn1 | |
151 let Fn2 = funcref('IMstatusfunc1') | |
152 let &imstatusfunc = Fn2 | |
153 normal! i | |
154 | |
155 " Using a string(funcref variable) to set 'imactivatefunc' | |
147 let &imactivatefunc = string(Fn1) | 156 let &imactivatefunc = string(Fn1) |
148 let Fn2 = funcref('IMstatusfunc1') | |
149 let &imstatusfunc = string(Fn2) | 157 let &imstatusfunc = string(Fn2) |
150 normal! i | 158 normal! i |
151 call assert_fails('let &imactivatefunc = Fn1', 'E729:') | |
152 call assert_fails('let &imstatusfunc = Fn2', 'E729:') | |
153 | 159 |
154 " Test for using a lambda function | 160 " Test for using a lambda function |
155 set imactivatefunc={a\ ->\ IMactivatefunc1(a)} | 161 set imactivatefunc={a\ ->\ IMactivatefunc1(a)} |
156 set imstatusfunc={\ ->\ IMstatusfunc1()} | 162 set imstatusfunc={\ ->\ IMstatusfunc1()} |
157 normal! i | 163 normal! i |
158 | 164 |
159 " Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression | 165 " Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression |
166 let &imactivatefunc = {a -> IMactivatefunc1(a)} | |
167 let &imstatusfunc = { -> IMstatusfunc1()} | |
168 normal! i | |
169 | |
170 " Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression) | |
160 let &imactivatefunc = '{a -> IMactivatefunc1(a)}' | 171 let &imactivatefunc = '{a -> IMactivatefunc1(a)}' |
161 let &imstatusfunc = '{ -> IMstatusfunc1()}' | 172 let &imstatusfunc = '{ -> IMstatusfunc1()}' |
162 normal! i | 173 normal! i |
163 | 174 |
164 " Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda expression | 175 " Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda expression |
165 let Lambda1 = {a -> IMactivatefunc1(a)} | 176 let Lambda1 = {a -> IMactivatefunc1(a)} |
166 let Lambda2 = { -> IMstatusfunc1()} | 177 let Lambda2 = { -> IMstatusfunc1()} |
178 let &imactivatefunc = Lambda1 | |
179 let &imstatusfunc = Lambda2 | |
180 normal! i | |
181 | |
182 " Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda | |
183 " expression) | |
167 let &imactivatefunc = string(Lambda1) | 184 let &imactivatefunc = string(Lambda1) |
168 let &imstatusfunc = string(Lambda2) | 185 let &imstatusfunc = string(Lambda2) |
169 normal! i | 186 normal! i |
170 call assert_fails('let &imactivatefunc = Lambda1', 'E729:') | |
171 call assert_fails('let &imstatusfunc = Lambda2', 'E729:') | |
172 | 187 |
173 " Test for clearing the 'completefunc' option | 188 " Test for clearing the 'completefunc' option |
174 set imactivatefunc='' imstatusfunc='' | 189 set imactivatefunc='' imstatusfunc='' |
175 set imactivatefunc& imstatusfunc& | 190 set imactivatefunc& imstatusfunc& |
176 | 191 |
177 call assert_fails("set imactivatefunc=function('abc')", "E700:") | 192 call assert_fails("set imactivatefunc=function('abc')", "E700:") |
178 call assert_fails("set imstatusfunc=function('abc')", "E700:") | 193 call assert_fails("set imstatusfunc=function('abc')", "E700:") |
179 call assert_fails("set imactivatefunc=funcref('abc')", "E700:") | 194 call assert_fails("set imactivatefunc=funcref('abc')", "E700:") |
180 call assert_fails("set imstatusfunc=funcref('abc')", "E700:") | 195 call assert_fails("set imstatusfunc=funcref('abc')", "E700:") |
181 | 196 |
182 call assert_equal(7, g:IMactivatefunc_called) | 197 call assert_equal(11, g:IMactivatefunc_called) |
183 call assert_equal(14, g:IMstatusfunc_called) | 198 call assert_equal(22, g:IMstatusfunc_called) |
184 | 199 |
185 " Vim9 tests | 200 " Vim9 tests |
186 let lines =<< trim END | 201 let lines =<< trim END |
187 vim9script | 202 vim9script |
188 | 203 |
214 } | 229 } |
215 var Fn2: func = () => { | 230 var Fn2: func = () => { |
216 g:IMstatusfunc_called += 1 | 231 g:IMstatusfunc_called += 1 |
217 return 1 | 232 return 1 |
218 } | 233 } |
234 &imactivatefunc = Fn1 | |
235 &imstatusfunc = Fn2 | |
236 normal! i | |
237 | |
238 # Test for using a string(variable with a lambda expression) | |
219 &imactivatefunc = string(Fn1) | 239 &imactivatefunc = string(Fn1) |
220 &imstatusfunc = string(Fn2) | 240 &imstatusfunc = string(Fn2) |
221 normal! i | 241 normal! i |
222 | 242 |
223 assert_equal(3, g:IMactivatefunc_called) | 243 assert_equal(4, g:IMactivatefunc_called) |
224 assert_equal(6, g:IMstatusfunc_called) | 244 assert_equal(8, g:IMstatusfunc_called) |
225 | 245 |
226 set iminsert=0 | 246 set iminsert=0 |
227 set imactivatefunc= | 247 set imactivatefunc= |
228 set imstatusfunc= | 248 set imstatusfunc= |
229 END | 249 END |