Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1 | " Test for insert completion |
| 2 | |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 3 | source screendump.vim |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 4 | source check.vim |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 5 | source vim9.vim |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 6 | |
| 7 | " Test for insert expansion |
| 8 | func Test_ins_complete() |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 9 | edit test_ins_complete.vim |
Bram Moolenaar | fb094e1 | 2017-11-05 20:59:28 +0100 | [diff] [blame] | 10 | " The files in the current directory interferes with the files |
| 11 | " used by this test. So use a separate directory for the test. |
| 12 | call mkdir('Xdir') |
| 13 | cd Xdir |
| 14 | |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 15 | set ff=unix |
| 16 | call writefile(["test11\t36Gepeto\t/Tag/", |
| 17 | \ "asd\ttest11file\t36G", |
| 18 | \ "Makefile\tto\trun"], 'Xtestfile') |
| 19 | call writefile(['', 'start of testfile', |
| 20 | \ 'ru', |
| 21 | \ 'run1', |
| 22 | \ 'run2', |
| 23 | \ 'STARTTEST', |
| 24 | \ 'ENDTEST', |
| 25 | \ 'end of testfile'], 'Xtestdata') |
| 26 | set ff& |
| 27 | |
| 28 | enew! |
| 29 | edit Xtestdata |
| 30 | new |
| 31 | call append(0, ['#include "Xtestfile"', '']) |
| 32 | call cursor(2, 1) |
| 33 | |
| 34 | set cot= |
| 35 | set cpt=.,w |
| 36 | " add-expands (word from next line) from other window |
| 37 | exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>" |
| 38 | call assert_equal('run1 run3', getline('.')) |
| 39 | " add-expands (current buffer first) |
| 40 | exe "normal o\<C-P>\<C-X>\<C-N>" |
| 41 | call assert_equal('run3 run3', getline('.')) |
| 42 | " Local expansion, ends in an empty line (unless it becomes a global |
| 43 | " expansion) |
| 44 | exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>" |
| 45 | call assert_equal('', getline('.')) |
| 46 | " starts Local and switches to global add-expansion |
| 47 | exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>" |
| 48 | call assert_equal('run1 run2', getline('.')) |
| 49 | |
| 50 | set cpt=.,w,i |
| 51 | " i-add-expands and switches to local |
| 52 | exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>" |
| 53 | call assert_equal("Makefile\tto\trun3", getline('.')) |
Dominique Pelle | 923dce2 | 2021-11-21 11:36:04 +0000 | [diff] [blame] | 54 | " add-expands lines (it would end in an empty line if it didn't ignore |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 55 | " itself) |
| 56 | exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>" |
| 57 | call assert_equal("Makefile\tto\trun3", getline('.')) |
| 58 | call assert_equal("Makefile\tto\trun3", getline(line('.') - 1)) |
| 59 | |
| 60 | set cpt=kXtestfile |
| 61 | " checks k-expansion, and file expansion (use Xtest11 instead of test11, |
| 62 | " because TEST11.OUT may match first on DOS) |
| 63 | write Xtest11.one |
| 64 | write Xtest11.two |
| 65 | exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>" |
| 66 | call assert_equal('Xtest11.two', getline('.')) |
| 67 | |
| 68 | " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X |
| 69 | " CTRL-F again to verify this doesn't cause trouble. |
| 70 | exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>" |
| 71 | call assert_equal('Xtest11.one', getline('.')) |
| 72 | normal ddk |
| 73 | |
| 74 | set cpt=w |
| 75 | " checks make_cyclic in other window |
| 76 | exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>" |
| 77 | call assert_equal('STARTTEST', getline('.')) |
| 78 | |
| 79 | set cpt=u nohid |
| 80 | " checks unloaded buffer expansion |
| 81 | only |
| 82 | exe "normal oEN\<C-N>" |
| 83 | call assert_equal('ENDTEST', getline('.')) |
| 84 | " checks adding mode abortion |
| 85 | exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>" |
| 86 | call assert_equal('unless', getline('.')) |
| 87 | |
| 88 | set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch |
| 89 | " tag expansion, define add-expansion interrupted |
| 90 | exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>" |
| 91 | call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.')) |
| 92 | " t-expansion |
| 93 | exe "normal oa\<C-N>\<Esc>" |
| 94 | call assert_equal('asd', getline('.')) |
| 95 | |
| 96 | %bw! |
| 97 | call delete('Xtestfile') |
| 98 | call delete('Xtest11.one') |
| 99 | call delete('Xtest11.two') |
| 100 | call delete('Xtestdata') |
| 101 | set cpt& cot& def& tags& tagbsearch& hidden& |
Bram Moolenaar | fb094e1 | 2017-11-05 20:59:28 +0100 | [diff] [blame] | 102 | cd .. |
| 103 | call delete('Xdir', 'rf') |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 104 | endfunc |
Bram Moolenaar | ffd99f7 | 2017-11-02 15:44:14 +0100 | [diff] [blame] | 105 | |
| 106 | func Test_omni_dash() |
| 107 | func Omni(findstart, base) |
| 108 | if a:findstart |
| 109 | return 5 |
| 110 | else |
| 111 | echom a:base |
| 112 | return ['-help', '-v'] |
| 113 | endif |
| 114 | endfunc |
| 115 | set omnifunc=Omni |
| 116 | new |
| 117 | exe "normal Gofind -\<C-x>\<C-o>" |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 118 | call assert_equal("find -help", getline('$')) |
Bram Moolenaar | ffd99f7 | 2017-11-02 15:44:14 +0100 | [diff] [blame] | 119 | |
| 120 | bwipe! |
| 121 | delfunc Omni |
| 122 | set omnifunc= |
| 123 | endfunc |
Bram Moolenaar | 02ae9b4 | 2018-02-09 15:06:02 +0100 | [diff] [blame] | 124 | |
Bram Moolenaar | ff34bee | 2021-07-25 20:27:06 +0200 | [diff] [blame] | 125 | func Test_omni_autoload() |
| 126 | let save_rtp = &rtp |
| 127 | set rtp=Xruntime/some |
| 128 | let dir = 'Xruntime/some/autoload' |
| 129 | call mkdir(dir, 'p') |
| 130 | |
| 131 | let lines =<< trim END |
| 132 | vim9script |
| 133 | def omni#func(findstart: bool, base: string): any |
| 134 | if findstart |
| 135 | return 1 |
| 136 | else |
| 137 | return ['match'] |
| 138 | endif |
| 139 | enddef |
| 140 | { |
| 141 | eval 1 + 2 |
| 142 | } |
| 143 | END |
| 144 | call writefile(lines, dir .. '/omni.vim') |
| 145 | |
| 146 | new |
| 147 | setlocal omnifunc=omni#func |
| 148 | call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt') |
| 149 | |
| 150 | bwipe! |
| 151 | call delete('Xruntime', 'rf') |
| 152 | set omnifunc= |
| 153 | let &rtp = save_rtp |
| 154 | endfunc |
| 155 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 156 | func Test_completefunc_args() |
| 157 | let s:args = [] |
| 158 | func! CompleteFunc(findstart, base) |
| 159 | let s:args += [[a:findstart, empty(a:base)]] |
| 160 | endfunc |
| 161 | new |
| 162 | |
| 163 | set completefunc=CompleteFunc |
| 164 | call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x') |
Bram Moolenaar | 52d3aae | 2018-06-13 21:27:24 +0200 | [diff] [blame] | 165 | call assert_equal([1, 1], s:args[0]) |
| 166 | call assert_equal(0, s:args[1][0]) |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 167 | set completefunc= |
| 168 | |
| 169 | let s:args = [] |
| 170 | set omnifunc=CompleteFunc |
| 171 | call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x') |
Bram Moolenaar | 52d3aae | 2018-06-13 21:27:24 +0200 | [diff] [blame] | 172 | call assert_equal([1, 1], s:args[0]) |
| 173 | call assert_equal(0, s:args[1][0]) |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 174 | set omnifunc= |
| 175 | |
| 176 | bwipe! |
| 177 | unlet s:args |
| 178 | delfunc CompleteFunc |
| 179 | endfunc |
| 180 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 181 | func s:CompleteDone_CompleteFuncNone( findstart, base ) |
| 182 | if a:findstart |
| 183 | return 0 |
| 184 | endif |
| 185 | |
| 186 | return v:none |
| 187 | endfunc |
| 188 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 189 | func s:CompleteDone_CompleteFuncDict( findstart, base ) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 190 | if a:findstart |
| 191 | return 0 |
| 192 | endif |
| 193 | |
| 194 | return { |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 195 | \ 'words': [ |
| 196 | \ { |
| 197 | \ 'word': 'aword', |
| 198 | \ 'abbr': 'wrd', |
| 199 | \ 'menu': 'extra text', |
| 200 | \ 'info': 'words are cool', |
| 201 | \ 'kind': 'W', |
| 202 | \ 'user_data': 'test' |
| 203 | \ } |
| 204 | \ ] |
| 205 | \ } |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 206 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 207 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 208 | func s:CompleteDone_CheckCompletedItemNone() |
| 209 | let s:called_completedone = 1 |
| 210 | endfunc |
| 211 | |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 212 | func s:CompleteDone_CheckCompletedItemDict(pre) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 213 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 214 | call assert_equal( 'wrd', v:completed_item[ 'abbr' ] ) |
| 215 | call assert_equal( 'extra text', v:completed_item[ 'menu' ] ) |
| 216 | call assert_equal( 'words are cool', v:completed_item[ 'info' ] ) |
| 217 | call assert_equal( 'W', v:completed_item[ 'kind' ] ) |
| 218 | call assert_equal( 'test', v:completed_item[ 'user_data' ] ) |
| 219 | |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 220 | if a:pre |
| 221 | call assert_equal('function', complete_info().mode) |
| 222 | endif |
Bram Moolenaar | 17e0478 | 2020-01-17 18:58:59 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 224 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 225 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 226 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 227 | func Test_CompleteDoneNone() |
| 228 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone() |
Bram Moolenaar | 9845f36 | 2019-04-08 18:59:54 +0200 | [diff] [blame] | 229 | let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '') |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 230 | |
| 231 | set completefunc=<SID>CompleteDone_CompleteFuncNone |
| 232 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 233 | set completefunc& |
Bram Moolenaar | 9845f36 | 2019-04-08 18:59:54 +0200 | [diff] [blame] | 234 | let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '') |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 235 | |
| 236 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9845f36 | 2019-04-08 18:59:54 +0200 | [diff] [blame] | 237 | call assert_equal(oldline, newline) |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 238 | |
| 239 | let s:called_completedone = 0 |
| 240 | au! CompleteDone |
| 241 | endfunc |
| 242 | |
| 243 | func Test_CompleteDoneDict() |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 244 | au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1) |
| 245 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 246 | |
| 247 | set completefunc=<SID>CompleteDone_CompleteFuncDict |
| 248 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 249 | set completefunc& |
| 250 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 251 | call assert_equal('test', v:completed_item[ 'user_data' ]) |
| 252 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 253 | |
| 254 | let s:called_completedone = 0 |
| 255 | au! CompleteDone |
| 256 | endfunc |
| 257 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 258 | func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 259 | if a:findstart |
| 260 | return 0 |
| 261 | endif |
| 262 | |
| 263 | return { |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 264 | \ 'words': [ |
| 265 | \ { |
| 266 | \ 'word': 'aword', |
| 267 | \ 'abbr': 'wrd', |
| 268 | \ 'menu': 'extra text', |
| 269 | \ 'info': 'words are cool', |
| 270 | \ 'kind': 'W', |
| 271 | \ 'user_data': ['one', 'two'], |
| 272 | \ } |
| 273 | \ ] |
| 274 | \ } |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 275 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 276 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 277 | func s:CompleteDone_CheckCompletedItemDictNoUserData() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 278 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 279 | call assert_equal( 'wrd', v:completed_item[ 'abbr' ] ) |
| 280 | call assert_equal( 'extra text', v:completed_item[ 'menu' ] ) |
| 281 | call assert_equal( 'words are cool', v:completed_item[ 'info' ] ) |
| 282 | call assert_equal( 'W', v:completed_item[ 'kind' ] ) |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 283 | call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] ) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 284 | |
| 285 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 286 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 287 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 288 | func Test_CompleteDoneDictNoUserData() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 289 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData() |
| 290 | |
| 291 | set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData |
| 292 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 293 | set completefunc& |
| 294 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 295 | call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ]) |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 296 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 297 | |
| 298 | let s:called_completedone = 0 |
| 299 | au! CompleteDone |
| 300 | endfunc |
| 301 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 302 | func s:CompleteDone_CompleteFuncList(findstart, base) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 303 | if a:findstart |
| 304 | return 0 |
| 305 | endif |
| 306 | |
| 307 | return [ 'aword' ] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 308 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 309 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 310 | func s:CompleteDone_CheckCompletedItemList() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 311 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 312 | call assert_equal( '', v:completed_item[ 'abbr' ] ) |
| 313 | call assert_equal( '', v:completed_item[ 'menu' ] ) |
| 314 | call assert_equal( '', v:completed_item[ 'info' ] ) |
| 315 | call assert_equal( '', v:completed_item[ 'kind' ] ) |
| 316 | call assert_equal( '', v:completed_item[ 'user_data' ] ) |
| 317 | |
| 318 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 319 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 320 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 321 | func Test_CompleteDoneList() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 322 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList() |
| 323 | |
| 324 | set completefunc=<SID>CompleteDone_CompleteFuncList |
| 325 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 326 | set completefunc& |
| 327 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 328 | call assert_equal('', v:completed_item[ 'user_data' ]) |
| 329 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 330 | |
| 331 | let s:called_completedone = 0 |
| 332 | au! CompleteDone |
| 333 | endfunc |
| 334 | |
Bram Moolenaar | af559d2 | 2018-08-08 22:55:41 +0200 | [diff] [blame] | 335 | func Test_CompleteDone_undo() |
| 336 | au CompleteDone * call append(0, "prepend1") |
| 337 | new |
| 338 | call setline(1, ["line1", "line2"]) |
| 339 | call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx") |
| 340 | call assert_equal(["prepend1", "line1", "line2", "line1", ""], |
| 341 | \ getline(1, '$')) |
| 342 | undo |
| 343 | call assert_equal(["line1", "line2"], getline(1, '$')) |
| 344 | bwipe! |
| 345 | au! CompleteDone |
| 346 | endfunc |
| 347 | |
Bram Moolenaar | b806aa5 | 2020-09-12 22:52:57 +0200 | [diff] [blame] | 348 | func CompleteTest(findstart, query) |
| 349 | if a:findstart |
| 350 | return col('.') |
| 351 | endif |
| 352 | return ['matched'] |
| 353 | endfunc |
| 354 | |
| 355 | func Test_completefunc_info() |
| 356 | new |
| 357 | set completeopt=menuone |
| 358 | set completefunc=CompleteTest |
| 359 | call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx") |
Bram Moolenaar | f9d5135 | 2020-10-26 19:22:42 +0100 | [diff] [blame] | 360 | call assert_equal("matched{'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}", getline(1)) |
Bram Moolenaar | b806aa5 | 2020-09-12 22:52:57 +0200 | [diff] [blame] | 361 | bwipe! |
| 362 | set completeopt& |
| 363 | set completefunc& |
| 364 | endfunc |
| 365 | |
Bram Moolenaar | 02ae9b4 | 2018-02-09 15:06:02 +0100 | [diff] [blame] | 366 | " Check that when using feedkeys() typeahead does not interrupt searching for |
| 367 | " completions. |
| 368 | func Test_compl_feedkeys() |
| 369 | new |
| 370 | set completeopt=menuone,noselect |
| 371 | call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx") |
| 372 | call assert_equal("jump jump", getline(1)) |
| 373 | bwipe! |
| 374 | set completeopt& |
| 375 | endfunc |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 376 | |
mityu | a119812 | 2021-11-20 19:13:39 +0000 | [diff] [blame] | 377 | func s:ComplInCmdwin_GlobalCompletion(a, l, p) |
| 378 | return 'global' |
| 379 | endfunc |
| 380 | |
| 381 | func s:ComplInCmdwin_LocalCompletion(a, l, p) |
| 382 | return 'local' |
| 383 | endfunc |
| 384 | |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 385 | func Test_compl_in_cmdwin() |
Bram Moolenaar | 21829c5 | 2021-01-26 22:42:21 +0100 | [diff] [blame] | 386 | CheckFeature cmdwin |
| 387 | |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 388 | set wildmenu wildchar=<Tab> |
| 389 | com! -nargs=1 -complete=command GetInput let input = <q-args> |
| 390 | com! -buffer TestCommand echo 'TestCommand' |
Bram Moolenaar | 4ff2f2f | 2020-10-25 13:22:42 +0100 | [diff] [blame] | 391 | let w:test_winvar = 'winvar' |
| 392 | let b:test_bufvar = 'bufvar' |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 393 | |
Bram Moolenaar | 4ff2f2f | 2020-10-25 13:22:42 +0100 | [diff] [blame] | 394 | " User-defined commands |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 395 | let input = '' |
| 396 | call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!') |
| 397 | call assert_equal('TestCommand', input) |
| 398 | |
| 399 | let input = '' |
| 400 | call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!') |
| 401 | call assert_equal('T', input) |
| 402 | |
Bram Moolenaar | 4ff2f2f | 2020-10-25 13:22:42 +0100 | [diff] [blame] | 403 | |
| 404 | com! -nargs=1 -complete=var GetInput let input = <q-args> |
| 405 | " Window-local variables |
| 406 | let input = '' |
| 407 | call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!') |
| 408 | call assert_equal('w:test_winvar', input) |
| 409 | |
| 410 | let input = '' |
| 411 | call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!') |
| 412 | call assert_equal('w:test_', input) |
| 413 | |
| 414 | " Buffer-local variables |
| 415 | let input = '' |
| 416 | call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!') |
| 417 | call assert_equal('b:test_bufvar', input) |
| 418 | |
| 419 | let input = '' |
| 420 | call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!') |
| 421 | call assert_equal('b:test_', input) |
| 422 | |
mityu | a119812 | 2021-11-20 19:13:39 +0000 | [diff] [blame] | 423 | |
| 424 | " Argument completion of buffer-local command |
| 425 | func s:ComplInCmdwin_GlobalCompletionList(a, l, p) |
| 426 | return ['global'] |
| 427 | endfunc |
| 428 | |
| 429 | func s:ComplInCmdwin_LocalCompletionList(a, l, p) |
| 430 | return ['local'] |
| 431 | endfunc |
| 432 | |
| 433 | func s:ComplInCmdwin_CheckCompletion(arg) |
| 434 | call assert_equal('local', a:arg) |
| 435 | endfunc |
| 436 | |
| 437 | com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion |
| 438 | \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) |
| 439 | com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion |
| 440 | \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) |
| 441 | call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!') |
| 442 | |
| 443 | com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList |
| 444 | \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) |
| 445 | com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList |
| 446 | \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) |
| 447 | |
| 448 | call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!') |
| 449 | |
| 450 | func! s:ComplInCmdwin_CheckCompletion(arg) |
| 451 | call assert_equal('global', a:arg) |
| 452 | endfunc |
| 453 | new |
| 454 | call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!') |
| 455 | quit |
| 456 | |
| 457 | delfunc s:ComplInCmdwin_GlobalCompletion |
| 458 | delfunc s:ComplInCmdwin_LocalCompletion |
| 459 | delfunc s:ComplInCmdwin_GlobalCompletionList |
| 460 | delfunc s:ComplInCmdwin_LocalCompletionList |
| 461 | delfunc s:ComplInCmdwin_CheckCompletion |
| 462 | |
| 463 | delcom -buffer TestCommand |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 464 | delcom TestCommand |
| 465 | delcom GetInput |
Bram Moolenaar | 4ff2f2f | 2020-10-25 13:22:42 +0100 | [diff] [blame] | 466 | unlet w:test_winvar |
| 467 | unlet b:test_bufvar |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 468 | set wildmenu& wildchar& |
| 469 | endfunc |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 470 | |
| 471 | " Test for insert path completion with completeslash option |
| 472 | func Test_ins_completeslash() |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 473 | CheckMSWindows |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 474 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 475 | call mkdir('Xdir') |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 476 | let orig_shellslash = &shellslash |
| 477 | set cpt& |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 478 | new |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 479 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 480 | set noshellslash |
| 481 | |
| 482 | set completeslash= |
| 483 | exe "normal oXd\<C-X>\<C-F>" |
| 484 | call assert_equal('Xdir\', getline('.')) |
| 485 | |
| 486 | set completeslash=backslash |
| 487 | exe "normal oXd\<C-X>\<C-F>" |
| 488 | call assert_equal('Xdir\', getline('.')) |
| 489 | |
| 490 | set completeslash=slash |
| 491 | exe "normal oXd\<C-X>\<C-F>" |
| 492 | call assert_equal('Xdir/', getline('.')) |
| 493 | |
| 494 | set shellslash |
| 495 | |
| 496 | set completeslash= |
| 497 | exe "normal oXd\<C-X>\<C-F>" |
| 498 | call assert_equal('Xdir/', getline('.')) |
| 499 | |
| 500 | set completeslash=backslash |
| 501 | exe "normal oXd\<C-X>\<C-F>" |
| 502 | call assert_equal('Xdir\', getline('.')) |
| 503 | |
| 504 | set completeslash=slash |
| 505 | exe "normal oXd\<C-X>\<C-F>" |
| 506 | call assert_equal('Xdir/', getline('.')) |
| 507 | %bw! |
| 508 | call delete('Xdir', 'rf') |
| 509 | |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 510 | set noshellslash |
| 511 | set completeslash=slash |
| 512 | call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1) |
| 513 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 514 | let &shellslash = orig_shellslash |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 515 | set completeslash= |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 516 | endfunc |
| 517 | |
Bram Moolenaar | d0e1b71 | 2020-09-27 20:13:03 +0200 | [diff] [blame] | 518 | func Test_pum_stopped_by_timer() |
| 519 | CheckScreendump |
| 520 | |
| 521 | let lines =<< trim END |
| 522 | call setline(1, ['hello', 'hullo', 'heeee', '']) |
| 523 | func StartCompl() |
| 524 | call timer_start(100, { -> execute('stopinsert') }) |
| 525 | call feedkeys("Gah\<C-N>") |
| 526 | endfunc |
| 527 | END |
| 528 | |
| 529 | call writefile(lines, 'Xpumscript') |
| 530 | let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12}) |
| 531 | call term_sendkeys(buf, ":call StartCompl()\<CR>") |
| 532 | call TermWait(buf, 200) |
| 533 | call term_sendkeys(buf, "k") |
| 534 | call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {}) |
| 535 | |
| 536 | call StopVimInTerminal(buf) |
| 537 | call delete('Xpumscript') |
| 538 | endfunc |
| 539 | |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 540 | func Test_pum_with_folds_two_tabs() |
| 541 | CheckScreendump |
| 542 | |
| 543 | let lines =<< trim END |
| 544 | set fdm=marker |
| 545 | call setline(1, ['" x {{{1', '" a some text']) |
| 546 | call setline(3, range(&lines)->map({_, val -> '" a' .. val})) |
| 547 | norm! zm |
| 548 | tab sp |
| 549 | call feedkeys('2Gzv', 'xt') |
| 550 | call feedkeys("0fa", 'xt') |
| 551 | END |
| 552 | |
| 553 | call writefile(lines, 'Xpumscript') |
| 554 | let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 555 | call TermWait(buf, 50) |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 556 | call term_sendkeys(buf, "a\<C-N>") |
| 557 | call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {}) |
| 558 | |
| 559 | call term_sendkeys(buf, "\<Esc>") |
| 560 | call StopVimInTerminal(buf) |
| 561 | call delete('Xpumscript') |
| 562 | endfunc |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 563 | |
| 564 | func Test_pum_with_preview_win() |
| 565 | CheckScreendump |
| 566 | |
| 567 | let lines =<< trim END |
| 568 | funct Omni_test(findstart, base) |
| 569 | if a:findstart |
| 570 | return col(".") - 1 |
| 571 | endif |
| 572 | return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}] |
| 573 | endfunc |
| 574 | set omnifunc=Omni_test |
| 575 | set completeopt+=longest |
| 576 | END |
| 577 | |
| 578 | call writefile(lines, 'Xpreviewscript') |
| 579 | let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 580 | call TermWait(buf, 50) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 581 | call term_sendkeys(buf, "Gi\<C-X>\<C-O>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 582 | call TermWait(buf, 100) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 583 | call term_sendkeys(buf, "\<C-N>") |
| 584 | call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {}) |
| 585 | |
| 586 | call term_sendkeys(buf, "\<Esc>") |
| 587 | call StopVimInTerminal(buf) |
| 588 | call delete('Xpreviewscript') |
| 589 | endfunc |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 590 | |
| 591 | " Test for inserting the tag search pattern in insert mode |
| 592 | func Test_ins_compl_tag_sft() |
| 593 | call writefile([ |
| 594 | \ "!_TAG_FILE_ENCODING\tutf-8\t//", |
| 595 | \ "first\tXfoo\t/^int first() {}$/", |
| 596 | \ "second\tXfoo\t/^int second() {}$/", |
| 597 | \ "third\tXfoo\t/^int third() {}$/"], |
| 598 | \ 'Xtags') |
| 599 | set tags=Xtags |
| 600 | let code =<< trim [CODE] |
| 601 | int first() {} |
| 602 | int second() {} |
| 603 | int third() {} |
| 604 | [CODE] |
| 605 | call writefile(code, 'Xfoo') |
| 606 | |
| 607 | enew |
| 608 | set showfulltag |
| 609 | exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>" |
| 610 | call assert_equal('int second() {}', getline(1)) |
| 611 | set noshowfulltag |
| 612 | |
| 613 | call delete('Xtags') |
| 614 | call delete('Xfoo') |
| 615 | set tags& |
| 616 | %bwipe! |
| 617 | endfunc |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 618 | |
| 619 | " Test for 'completefunc' deleting text |
| 620 | func Test_completefunc_error() |
| 621 | new |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 622 | " delete text when called for the first time |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 623 | func CompleteFunc(findstart, base) |
| 624 | if a:findstart == 1 |
| 625 | normal dd |
| 626 | return col('.') - 1 |
| 627 | endif |
| 628 | return ['a', 'b'] |
| 629 | endfunc |
| 630 | set completefunc=CompleteFunc |
| 631 | call setline(1, ['', 'abcd', '']) |
Bram Moolenaar | 3eb6bd9 | 2021-01-29 21:47:24 +0100 | [diff] [blame] | 632 | call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 633 | |
| 634 | " delete text when called for the second time |
| 635 | func CompleteFunc2(findstart, base) |
| 636 | if a:findstart == 1 |
| 637 | return col('.') - 1 |
| 638 | endif |
| 639 | normal dd |
| 640 | return ['a', 'b'] |
| 641 | endfunc |
| 642 | set completefunc=CompleteFunc2 |
| 643 | call setline(1, ['', 'abcd', '']) |
Bram Moolenaar | 6adb9ea | 2020-04-30 22:31:18 +0200 | [diff] [blame] | 644 | call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E578:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 645 | |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 646 | " Jump to a different window from the complete function |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 647 | func CompleteFunc3(findstart, base) |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 648 | if a:findstart == 1 |
| 649 | return col('.') - 1 |
| 650 | endif |
| 651 | wincmd p |
| 652 | return ['a', 'b'] |
| 653 | endfunc |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 654 | set completefunc=CompleteFunc3 |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 655 | new |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 656 | call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 657 | close! |
| 658 | |
| 659 | set completefunc& |
| 660 | delfunc CompleteFunc |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 661 | delfunc CompleteFunc2 |
| 662 | delfunc CompleteFunc3 |
| 663 | close! |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 664 | endfunc |
| 665 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 666 | " Test for returning non-string values from 'completefunc' |
| 667 | func Test_completefunc_invalid_data() |
| 668 | new |
| 669 | func! CompleteFunc(findstart, base) |
| 670 | if a:findstart == 1 |
| 671 | return col('.') - 1 |
| 672 | endif |
| 673 | return [{}, '', 'moon'] |
| 674 | endfunc |
| 675 | set completefunc=CompleteFunc |
| 676 | exe "normal i\<C-X>\<C-U>" |
| 677 | call assert_equal('moon', getline(1)) |
| 678 | set completefunc& |
| 679 | close! |
| 680 | endfunc |
| 681 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 682 | " Test for errors in using complete() function |
| 683 | func Test_complete_func_error() |
| 684 | call assert_fails('call complete(1, ["a"])', 'E785:') |
| 685 | func ListColors() |
| 686 | call complete(col('.'), "blue") |
| 687 | endfunc |
| 688 | call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:') |
| 689 | func ListMonths() |
| 690 | call complete(col('.'), test_null_list()) |
| 691 | endfunc |
| 692 | call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:') |
| 693 | delfunc ListColors |
| 694 | delfunc ListMonths |
| 695 | call assert_fails('call complete_info({})', 'E714:') |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 696 | call assert_equal([], complete_info(['items']).items) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 697 | endfunc |
| 698 | |
Bram Moolenaar | 224a5f1 | 2020-04-28 20:29:07 +0200 | [diff] [blame] | 699 | " Test for completing words following a completed word in a line |
| 700 | func Test_complete_wrapscan() |
| 701 | " complete words from another buffer |
| 702 | new |
| 703 | call setline(1, ['one two', 'three four']) |
| 704 | new |
| 705 | setlocal complete=w |
| 706 | call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 707 | call assert_equal('two three four', getline(1)) |
| 708 | close! |
| 709 | " complete words from the current buffer |
| 710 | setlocal complete=. |
| 711 | %d |
| 712 | call setline(1, ['one two', '']) |
| 713 | call cursor(2, 1) |
| 714 | call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 715 | call assert_equal('one two one two', getline(2)) |
| 716 | close! |
| 717 | endfunc |
| 718 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 719 | " Test for completing special characters |
| 720 | func Test_complete_special_chars() |
| 721 | new |
| 722 | call setline(1, 'int .*[-\^$ func float') |
| 723 | call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt') |
| 724 | call assert_equal('int .*[-\^$ func float', getline(2)) |
| 725 | close! |
| 726 | endfunc |
| 727 | |
| 728 | " Test for completion when text is wrapped across lines. |
| 729 | func Test_complete_across_line() |
| 730 | new |
| 731 | call setline(1, ['red green blue', 'one two three']) |
| 732 | setlocal textwidth=20 |
| 733 | exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 734 | call assert_equal(['one two three red', 'green blue one'], getline(2, '$')) |
| 735 | close! |
| 736 | endfunc |
| 737 | |
| 738 | " Test for using CTRL-L to add one character when completing matching |
| 739 | func Test_complete_add_onechar() |
| 740 | new |
| 741 | call setline(1, ['wool', 'woodwork']) |
| 742 | call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt') |
| 743 | call assert_equal('woof', getline(3)) |
| 744 | |
| 745 | " use 'ignorecase' and backspace to erase characters from the prefix string |
| 746 | " and then add letters using CTRL-L |
| 747 | %d |
| 748 | set ignorecase backspace=2 |
| 749 | setlocal complete=. |
| 750 | call setline(1, ['workhorse', 'workload']) |
| 751 | normal Go |
| 752 | exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>" |
| 753 | call assert_equal('workh', getline(3)) |
| 754 | set ignorecase& backspace& |
| 755 | close! |
| 756 | endfunc |
| 757 | |
| 758 | " Test insert completion with 'cindent' (adjust the indent) |
| 759 | func Test_complete_with_cindent() |
| 760 | new |
| 761 | setlocal cindent |
| 762 | call setline(1, ['if (i == 1)', " j = 2;"]) |
| 763 | exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}" |
| 764 | call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$')) |
| 765 | |
| 766 | %d |
| 767 | call setline(1, ['when while', '{', '']) |
| 768 | setlocal cinkeys+==while |
| 769 | exe "normal Giwh\<C-P> " |
| 770 | call assert_equal("\twhile ", getline('$')) |
| 771 | close! |
| 772 | endfunc |
| 773 | |
| 774 | " Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions |
| 775 | func Test_complete_cmdline() |
| 776 | new |
| 777 | exe "normal icaddb\<C-X>\<C-V>" |
| 778 | call assert_equal('caddbuffer', getline(1)) |
| 779 | exe "normal ocall getqf\<C-X>\<C-V>" |
| 780 | call assert_equal('call getqflist(', getline(2)) |
| 781 | exe "normal oabcxyz(\<C-X>\<C-V>" |
| 782 | call assert_equal('abcxyz(', getline(3)) |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 783 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 784 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 785 | write TestCommand1Test |
| 786 | write TestCommand2Test |
| 787 | " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode |
| 788 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>" |
| 789 | call assert_equal('TestCommand2Test', getline(4)) |
| 790 | call delete('TestCommand1Test') |
| 791 | call delete('TestCommand2Test') |
| 792 | delcom TestCommand1 |
| 793 | delcom TestCommand2 |
| 794 | close! |
| 795 | endfunc |
| 796 | |
| 797 | " Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match |
| 798 | func Test_complete_stop() |
| 799 | new |
| 800 | func Save_mode1() |
| 801 | let g:mode1 = mode(1) |
| 802 | return '' |
| 803 | endfunc |
| 804 | func Save_mode2() |
| 805 | let g:mode2 = mode(1) |
| 806 | return '' |
| 807 | endfunc |
| 808 | inoremap <F1> <C-R>=Save_mode1()<CR> |
| 809 | inoremap <F2> <C-R>=Save_mode2()<CR> |
| 810 | call setline(1, ['aaa bbb ccc ']) |
| 811 | exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 812 | call assert_equal('ic', g:mode1) |
| 813 | call assert_equal('i', g:mode2) |
| 814 | call assert_equal('aaa bbb ccc ', getline(1)) |
| 815 | exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 816 | call assert_equal('ic', g:mode1) |
| 817 | call assert_equal('i', g:mode2) |
| 818 | call assert_equal('aaa bbb ccc aaa', getline(1)) |
| 819 | set completeopt+=noselect |
| 820 | exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 821 | call assert_equal('ic', g:mode1) |
| 822 | call assert_equal('i', g:mode2) |
| 823 | call assert_equal('aaa bbb ccc aaa bb', getline(1)) |
| 824 | set completeopt& |
| 825 | exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 826 | call assert_equal('ic', g:mode1) |
| 827 | call assert_equal('i', g:mode2) |
| 828 | call assert_equal('aaa bbb ccc aaa bb d', getline(1)) |
| 829 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 830 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 831 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 832 | call assert_equal('ic', g:mode1) |
| 833 | call assert_equal('i', g:mode2) |
| 834 | call assert_equal('TestCommand2', getline(2)) |
| 835 | delcom TestCommand1 |
| 836 | delcom TestCommand2 |
| 837 | unlet g:mode1 |
| 838 | unlet g:mode2 |
| 839 | iunmap <F1> |
| 840 | iunmap <F2> |
| 841 | delfunc Save_mode1 |
| 842 | delfunc Save_mode2 |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 843 | close! |
| 844 | endfunc |
| 845 | |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 846 | func Test_issue_7021() |
| 847 | CheckMSWindows |
| 848 | |
| 849 | let orig_shellslash = &shellslash |
| 850 | set noshellslash |
| 851 | |
| 852 | set completeslash=slash |
| 853 | call assert_false(expand('~') =~ '/') |
| 854 | |
| 855 | let &shellslash = orig_shellslash |
| 856 | set completeslash= |
| 857 | endfunc |
| 858 | |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 859 | " Test to ensure 'Scanning...' messages are not recorded in messages history |
| 860 | func Test_z1_complete_no_history() |
| 861 | new |
| 862 | messages clear |
| 863 | let currmess = execute('messages') |
| 864 | setlocal dictionary=README.txt |
| 865 | exe "normal owh\<C-X>\<C-K>" |
| 866 | exe "normal owh\<C-N>" |
| 867 | call assert_equal(currmess, execute('messages')) |
| 868 | close! |
| 869 | endfunc |
| 870 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 871 | " Test for different ways of setting the 'completefunc' option |
| 872 | func Test_completefunc_callback() |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 873 | func MycompleteFunc1(val, findstart, base) |
| 874 | call add(g:MycompleteFunc1_args, [a:val, a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 875 | return a:findstart ? 0 : [] |
| 876 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 877 | |
| 878 | " Test for using a function() |
| 879 | set completefunc=function('MycompleteFunc1',[10]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 880 | new | only |
| 881 | call setline(1, 'one') |
| 882 | let g:MycompleteFunc1_args = [] |
| 883 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 884 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 885 | bw! |
| 886 | |
| 887 | " Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 888 | let Fn = function('MycompleteFunc1', [11]) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 889 | let &completefunc = Fn |
| 890 | new | only |
| 891 | call setline(1, 'two') |
| 892 | let g:MycompleteFunc1_args = [] |
| 893 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 894 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 895 | bw! |
| 896 | |
| 897 | " Using string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 898 | let Fn = function('MycompleteFunc1', [12]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 899 | let &completefunc = string(Fn) |
| 900 | new | only |
| 901 | call setline(1, 'two') |
| 902 | let g:MycompleteFunc1_args = [] |
| 903 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 904 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 905 | bw! |
| 906 | |
| 907 | " Test for using a funcref() |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 908 | set completefunc=funcref('MycompleteFunc1',\ [13]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 909 | new | only |
| 910 | call setline(1, 'three') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 911 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 912 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 913 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 914 | bw! |
| 915 | |
| 916 | " Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 917 | let Fn = funcref('MycompleteFunc1', [14]) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 918 | let &completefunc = Fn |
| 919 | new | only |
| 920 | call setline(1, 'four') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 921 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 922 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 923 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 924 | bw! |
| 925 | |
| 926 | " Using a string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 927 | let Fn = funcref('MycompleteFunc1', [15]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 928 | let &completefunc = string(Fn) |
| 929 | new | only |
| 930 | call setline(1, 'four') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 931 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 932 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 933 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 934 | bw! |
| 935 | |
| 936 | " Test for using a lambda function |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 937 | set completefunc={a,\ b\ ->\ MycompleteFunc1(16,\ a,\ b)} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 938 | new | only |
| 939 | call setline(1, 'five') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 940 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 941 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 942 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 943 | bw! |
| 944 | |
| 945 | " Set 'completefunc' to a lambda expression |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 946 | let &completefunc = {a, b -> MycompleteFunc1(17, a, b)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 947 | new | only |
| 948 | call setline(1, 'six') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 949 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 950 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 951 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 952 | bw! |
| 953 | |
| 954 | " Set 'completefunc' to string(lambda_expression) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 955 | let &completefunc = '{a, b -> MycompleteFunc1(18, a, b)}' |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 956 | new | only |
| 957 | call setline(1, 'six') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 958 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 959 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 960 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 961 | bw! |
| 962 | |
| 963 | " Set 'completefunc' to a variable with a lambda expression |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 964 | let Lambda = {a, b -> MycompleteFunc1(19, a, b)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 965 | let &completefunc = Lambda |
| 966 | new | only |
| 967 | call setline(1, 'seven') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 968 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 969 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 970 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 971 | bw! |
| 972 | |
| 973 | " Set 'completefunc' to a string(variable with a lambda expression) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 974 | let Lambda = {a, b -> MycompleteFunc1(20, a, b)} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 975 | let &completefunc = string(Lambda) |
| 976 | new | only |
| 977 | call setline(1, 'seven') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 978 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 979 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 980 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 981 | bw! |
| 982 | |
| 983 | " Test for using a lambda function with incorrect return value |
| 984 | let Lambda = {s -> strlen(s)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 985 | let &completefunc = Lambda |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 986 | new | only |
| 987 | call setline(1, 'eight') |
| 988 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 989 | bw! |
| 990 | |
| 991 | " Test for clearing the 'completefunc' option |
| 992 | set completefunc='' |
| 993 | set completefunc& |
| 994 | |
| 995 | call assert_fails("set completefunc=function('abc')", "E700:") |
| 996 | call assert_fails("set completefunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 997 | let &completefunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 998 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 999 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1000 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1001 | set completefunc=(a,\ b)\ =>\ g:MycompleteFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1002 | new | only |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1003 | let g:MycompleteFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1004 | call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1005 | call assert_equal([], g:MycompleteFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1006 | |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1007 | " set 'completefunc' to a non-existing function |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1008 | func MycompleteFunc2(findstart, base) |
| 1009 | call add(g:MycompleteFunc2_args, [a:findstart, a:base]) |
| 1010 | return a:findstart ? 0 : [] |
| 1011 | endfunc |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1012 | set completefunc=MycompleteFunc2 |
| 1013 | call setline(1, 'five') |
| 1014 | call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') |
| 1015 | call assert_fails("let &completefunc = function('NonExistingFunc')", 'E700:') |
| 1016 | let g:MycompleteFunc2_args = [] |
| 1017 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1018 | call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1019 | bw! |
| 1020 | |
| 1021 | " Vim9 tests |
| 1022 | let lines =<< trim END |
| 1023 | vim9script |
| 1024 | |
| 1025 | # Test for using function() |
| 1026 | def Vim9CompleteFunc(val: number, findstart: number, base: string): any |
| 1027 | add(g:Vim9completeFuncArgs, [val, findstart, base]) |
| 1028 | return findstart ? 0 : [] |
| 1029 | enddef |
| 1030 | set completefunc=function('Vim9CompleteFunc',\ [60]) |
| 1031 | new | only |
| 1032 | setline(1, 'one') |
| 1033 | g:Vim9completeFuncArgs = [] |
| 1034 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1035 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) |
| 1036 | bw! |
| 1037 | |
| 1038 | # Test for using a lambda |
| 1039 | &completefunc = (a, b) => Vim9CompleteFunc(61, a, b) |
| 1040 | new | only |
| 1041 | setline(1, 'two') |
| 1042 | g:Vim9completeFuncArgs = [] |
| 1043 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1044 | assert_equal([[61, 1, ''], [61, 0, 'two']], g:Vim9completeFuncArgs) |
| 1045 | bw! |
| 1046 | |
| 1047 | # Test for using a string(lambda) |
| 1048 | &completefunc = '(a, b) => Vim9CompleteFunc(62, a, b)' |
| 1049 | new | only |
| 1050 | setline(1, 'two') |
| 1051 | g:Vim9completeFuncArgs = [] |
| 1052 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1053 | assert_equal([[62, 1, ''], [62, 0, 'two']], g:Vim9completeFuncArgs) |
| 1054 | bw! |
| 1055 | |
| 1056 | # Test for using a variable with a lambda expression |
| 1057 | var Fn: func = (a, b) => Vim9CompleteFunc(63, a, b) |
| 1058 | &completefunc = Fn |
| 1059 | new | only |
| 1060 | setline(1, 'three') |
| 1061 | g:Vim9completeFuncArgs = [] |
| 1062 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1063 | assert_equal([[63, 1, ''], [63, 0, 'three']], g:Vim9completeFuncArgs) |
| 1064 | bw! |
| 1065 | |
| 1066 | # Test for using a string(variable with a lambda expression) |
| 1067 | Fn = (a, b) => Vim9CompleteFunc(64, a, b) |
| 1068 | &completefunc = string(Fn) |
| 1069 | new | only |
| 1070 | setline(1, 'three') |
| 1071 | g:Vim9completeFuncArgs = [] |
| 1072 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1073 | assert_equal([[64, 1, ''], [64, 0, 'three']], g:Vim9completeFuncArgs) |
| 1074 | bw! |
| 1075 | END |
| 1076 | call CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1077 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1078 | " cleanup |
| 1079 | delfunc MycompleteFunc1 |
| 1080 | delfunc MycompleteFunc2 |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1081 | set completefunc& |
| 1082 | %bw! |
| 1083 | endfunc |
| 1084 | |
| 1085 | " Test for different ways of setting the 'omnifunc' option |
| 1086 | func Test_omnifunc_callback() |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1087 | func MyomniFunc1(val, findstart, base) |
| 1088 | call add(g:MyomniFunc1_args, [a:val, a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1089 | return a:findstart ? 0 : [] |
| 1090 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1091 | |
| 1092 | " Test for using a function() |
| 1093 | set omnifunc=function('MyomniFunc1',[10]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1094 | new | only |
| 1095 | call setline(1, 'one') |
| 1096 | let g:MyomniFunc1_args = [] |
| 1097 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1098 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1099 | bw! |
| 1100 | |
| 1101 | " Using a funcref variable to set 'omnifunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1102 | let Fn = function('MyomniFunc1', [11]) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1103 | let &omnifunc = Fn |
| 1104 | new | only |
| 1105 | call setline(1, 'two') |
| 1106 | let g:MyomniFunc1_args = [] |
| 1107 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1108 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1109 | bw! |
| 1110 | |
| 1111 | " Using a string(funcref_variable) to set 'omnifunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1112 | let Fn = function('MyomniFunc1', [12]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1113 | let &omnifunc = string(Fn) |
| 1114 | new | only |
| 1115 | call setline(1, 'two') |
| 1116 | let g:MyomniFunc1_args = [] |
| 1117 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1118 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1119 | bw! |
| 1120 | |
| 1121 | " Test for using a funcref() |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1122 | set omnifunc=funcref('MyomniFunc1',\ [13]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1123 | new | only |
| 1124 | call setline(1, 'three') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1125 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1126 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1127 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1128 | bw! |
| 1129 | |
| 1130 | " Using a funcref variable to set 'omnifunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1131 | let Fn = funcref('MyomniFunc1', [14]) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1132 | let &omnifunc = Fn |
| 1133 | new | only |
| 1134 | call setline(1, 'four') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1135 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1136 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1137 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1138 | bw! |
| 1139 | |
| 1140 | " Using a string(funcref_variable) to set 'omnifunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1141 | let Fn = funcref('MyomniFunc1', [15]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1142 | let &omnifunc = string(Fn) |
| 1143 | new | only |
| 1144 | call setline(1, 'four') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1145 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1146 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1147 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1148 | bw! |
| 1149 | |
| 1150 | " Test for using a lambda function |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1151 | set omnifunc={a,\ b\ ->\ MyomniFunc1(16,\ a,\ b)} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1152 | new | only |
| 1153 | call setline(1, 'five') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1154 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1155 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1156 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1157 | bw! |
| 1158 | |
| 1159 | " Set 'omnifunc' to a lambda expression |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1160 | let &omnifunc = {a, b -> MyomniFunc1(17, a, b)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1161 | new | only |
| 1162 | call setline(1, 'six') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1163 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1164 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1165 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1166 | bw! |
| 1167 | |
| 1168 | " Set 'omnifunc' to a string(lambda_expression) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1169 | let &omnifunc = '{a, b -> MyomniFunc1(18, a, b)}' |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1170 | new | only |
| 1171 | call setline(1, 'six') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1172 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1173 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1174 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1175 | bw! |
| 1176 | |
| 1177 | " Set 'omnifunc' to a variable with a lambda expression |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1178 | let Lambda = {a, b -> MyomniFunc1(19, a, b)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1179 | let &omnifunc = Lambda |
| 1180 | new | only |
| 1181 | call setline(1, 'seven') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1182 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1183 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1184 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1185 | bw! |
| 1186 | |
| 1187 | " Set 'omnifunc' to a string(variable with a lambda expression) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1188 | let Lambda = {a, b -> MyomniFunc1(20, a, b)} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1189 | let &omnifunc = string(Lambda) |
| 1190 | new | only |
| 1191 | call setline(1, 'seven') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1192 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1193 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1194 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1195 | bw! |
| 1196 | |
| 1197 | " Test for using a lambda function with incorrect return value |
| 1198 | let Lambda = {s -> strlen(s)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1199 | let &omnifunc = Lambda |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1200 | new | only |
| 1201 | call setline(1, 'eight') |
| 1202 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1203 | bw! |
| 1204 | |
| 1205 | " Test for clearing the 'omnifunc' option |
| 1206 | set omnifunc='' |
| 1207 | set omnifunc& |
| 1208 | |
| 1209 | call assert_fails("set omnifunc=function('abc')", "E700:") |
| 1210 | call assert_fails("set omnifunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1211 | let &omnifunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1212 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1213 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1214 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1215 | set omnifunc=(a,\ b)\ =>\ g:MyomniFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1216 | new | only |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1217 | let g:MyomniFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1218 | call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1219 | call assert_equal([], g:MyomniFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1220 | |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1221 | " set 'omnifunc' to a non-existing function |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1222 | func MyomniFunc2(findstart, base) |
| 1223 | call add(g:MyomniFunc2_args, [a:findstart, a:base]) |
| 1224 | return a:findstart ? 0 : [] |
| 1225 | endfunc |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1226 | set omnifunc=MyomniFunc2 |
| 1227 | call setline(1, 'nine') |
| 1228 | call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') |
| 1229 | call assert_fails("let &omnifunc = function('NonExistingFunc')", 'E700:') |
| 1230 | let g:MyomniFunc2_args = [] |
| 1231 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1232 | call assert_equal([[1, ''], [0, 'nine']], g:MyomniFunc2_args) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1233 | bw! |
| 1234 | |
| 1235 | " Vim9 tests |
| 1236 | let lines =<< trim END |
| 1237 | vim9script |
| 1238 | |
| 1239 | # Test for using function() |
| 1240 | def Vim9omniFunc(val: number, findstart: number, base: string): any |
| 1241 | add(g:Vim9omniFunc_Args, [val, findstart, base]) |
| 1242 | return findstart ? 0 : [] |
| 1243 | enddef |
| 1244 | set omnifunc=function('Vim9omniFunc',\ [60]) |
| 1245 | new | only |
| 1246 | setline(1, 'one') |
| 1247 | g:Vim9omniFunc_Args = [] |
| 1248 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1249 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) |
| 1250 | bw! |
| 1251 | |
| 1252 | # Test for using a lambda |
| 1253 | &omnifunc = (a, b) => Vim9omniFunc(61, a, b) |
| 1254 | new | only |
| 1255 | setline(1, 'two') |
| 1256 | g:Vim9omniFunc_Args = [] |
| 1257 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1258 | assert_equal([[61, 1, ''], [61, 0, 'two']], g:Vim9omniFunc_Args) |
| 1259 | bw! |
| 1260 | |
| 1261 | # Test for using a string(lambda) |
| 1262 | &omnifunc = '(a, b) => Vim9omniFunc(62, a, b)' |
| 1263 | new | only |
| 1264 | setline(1, 'two') |
| 1265 | g:Vim9omniFunc_Args = [] |
| 1266 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1267 | assert_equal([[62, 1, ''], [62, 0, 'two']], g:Vim9omniFunc_Args) |
| 1268 | bw! |
| 1269 | |
| 1270 | # Test for using a variable with a lambda expression |
| 1271 | var Fn: func = (a, b) => Vim9omniFunc(63, a, b) |
| 1272 | &omnifunc = Fn |
| 1273 | new | only |
| 1274 | setline(1, 'three') |
| 1275 | g:Vim9omniFunc_Args = [] |
| 1276 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1277 | assert_equal([[63, 1, ''], [63, 0, 'three']], g:Vim9omniFunc_Args) |
| 1278 | bw! |
| 1279 | |
| 1280 | # Test for using a string(variable with a lambda expression) |
| 1281 | Fn = (a, b) => Vim9omniFunc(64, a, b) |
| 1282 | &omnifunc = string(Fn) |
| 1283 | new | only |
| 1284 | setline(1, 'three') |
| 1285 | g:Vim9omniFunc_Args = [] |
| 1286 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1287 | assert_equal([[64, 1, ''], [64, 0, 'three']], g:Vim9omniFunc_Args) |
| 1288 | bw! |
| 1289 | END |
| 1290 | call CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1291 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1292 | " cleanup |
| 1293 | delfunc MyomniFunc1 |
| 1294 | delfunc MyomniFunc2 |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1295 | set omnifunc& |
| 1296 | %bw! |
| 1297 | endfunc |
| 1298 | |
| 1299 | " Test for different ways of setting the 'thesaurusfunc' option |
| 1300 | func Test_thesaurusfunc_callback() |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1301 | func MytsrFunc1(val, findstart, base) |
| 1302 | call add(g:MytsrFunc1_args, [a:val, a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1303 | return a:findstart ? 0 : [] |
| 1304 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1305 | |
| 1306 | " Test for using a function() |
| 1307 | set thesaurusfunc=function('MytsrFunc1',[10]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1308 | new | only |
| 1309 | call setline(1, 'one') |
| 1310 | let g:MytsrFunc1_args = [] |
| 1311 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1312 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1313 | bw! |
| 1314 | |
| 1315 | " Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1316 | let Fn = function('MytsrFunc1', [11]) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1317 | let &thesaurusfunc = Fn |
| 1318 | new | only |
| 1319 | call setline(1, 'two') |
| 1320 | let g:MytsrFunc1_args = [] |
| 1321 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1322 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1323 | bw! |
| 1324 | |
| 1325 | " Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1326 | let Fn = function('MytsrFunc1', [12]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1327 | let &thesaurusfunc = string(Fn) |
| 1328 | new | only |
| 1329 | call setline(1, 'two') |
| 1330 | let g:MytsrFunc1_args = [] |
| 1331 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1332 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1333 | bw! |
| 1334 | |
| 1335 | " Test for using a funcref() |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1336 | set thesaurusfunc=funcref('MytsrFunc1',[13]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1337 | new | only |
| 1338 | call setline(1, 'three') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1339 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1340 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1341 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1342 | bw! |
| 1343 | |
| 1344 | " Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1345 | let Fn = funcref('MytsrFunc1', [14]) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1346 | let &thesaurusfunc = Fn |
| 1347 | new | only |
| 1348 | call setline(1, 'four') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1349 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1350 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1351 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1352 | bw! |
| 1353 | |
| 1354 | " Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1355 | let Fn = funcref('MytsrFunc1', [15]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1356 | let &thesaurusfunc = string(Fn) |
| 1357 | new | only |
| 1358 | call setline(1, 'four') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1359 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1360 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1361 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1362 | bw! |
| 1363 | |
| 1364 | " Test for using a lambda function |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1365 | set thesaurusfunc={a,\ b\ ->\ MytsrFunc1(16,\ a,\ b)} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1366 | new | only |
| 1367 | call setline(1, 'five') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1368 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1369 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1370 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1371 | bw! |
| 1372 | |
| 1373 | " Set 'thesaurusfunc' to a lambda expression |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1374 | let &thesaurusfunc = {a, b -> MytsrFunc1(17, a, b)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1375 | new | only |
| 1376 | call setline(1, 'six') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1377 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1378 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1379 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1380 | bw! |
| 1381 | |
| 1382 | " Set 'thesaurusfunc' to a string(lambda expression) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1383 | let &thesaurusfunc = '{a, b -> MytsrFunc1(18, a, b)}' |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1384 | new | only |
| 1385 | call setline(1, 'six') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1386 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1387 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1388 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1389 | bw! |
| 1390 | |
| 1391 | " Set 'thesaurusfunc' to a variable with a lambda expression |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1392 | let Lambda = {a, b -> MytsrFunc1(19, a, b)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1393 | let &thesaurusfunc = Lambda |
| 1394 | new | only |
| 1395 | call setline(1, 'seven') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1396 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1397 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1398 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1399 | bw! |
| 1400 | |
| 1401 | " Set 'thesaurusfunc' to a string(variable with a lambda expression) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1402 | let Lambda = {a, b -> MytsrFunc1(20, a, b)} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1403 | let &thesaurusfunc = string(Lambda) |
| 1404 | new | only |
| 1405 | call setline(1, 'seven') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1406 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1407 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1408 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1409 | bw! |
| 1410 | |
| 1411 | " Test for using a lambda function with incorrect return value |
| 1412 | let Lambda = {s -> strlen(s)} |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1413 | let &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1414 | new | only |
| 1415 | call setline(1, 'eight') |
| 1416 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1417 | bw! |
| 1418 | |
| 1419 | " Test for clearing the 'thesaurusfunc' option |
| 1420 | set thesaurusfunc='' |
| 1421 | set thesaurusfunc& |
| 1422 | |
| 1423 | call assert_fails("set thesaurusfunc=function('abc')", "E700:") |
| 1424 | call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1425 | let &thesaurusfunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1426 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1427 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1428 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1429 | set thesaurusfunc=(a,\ b)\ =>\ g:MytsrFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1430 | new | only |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1431 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1432 | call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1433 | call assert_equal([], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1434 | bw! |
| 1435 | |
| 1436 | " Use a buffer-local value and a global value |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1437 | set thesaurusfunc& |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1438 | setlocal thesaurusfunc=function('MytsrFunc1',[22]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1439 | call setline(1, 'sun') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1440 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1441 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 1442 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1443 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:MytsrFunc1_args) |
| 1444 | new |
| 1445 | call setline(1, 'sun') |
| 1446 | let g:MytsrFunc1_args = [] |
| 1447 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 1448 | call assert_equal('sun', getline(1)) |
| 1449 | call assert_equal([], g:MytsrFunc1_args) |
| 1450 | set thesaurusfunc=function('MytsrFunc1',[23]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1451 | wincmd w |
| 1452 | call setline(1, 'sun') |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1453 | let g:MytsrFunc1_args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1454 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1455 | call assert_equal('sun', getline(1)) |
| 1456 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:MytsrFunc1_args) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1457 | %bw! |
| 1458 | |
| 1459 | " set 'thesaurusfunc' to a non-existing function |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1460 | func MytsrFunc2(findstart, base) |
| 1461 | call add(g:MytsrFunc2_args, [a:findstart, a:base]) |
| 1462 | return a:findstart ? 0 : ['sunday'] |
| 1463 | endfunc |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1464 | set thesaurusfunc=MytsrFunc2 |
| 1465 | call setline(1, 'ten') |
| 1466 | call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') |
| 1467 | call assert_fails("let &thesaurusfunc = function('NonExistingFunc')", 'E700:') |
| 1468 | let g:MytsrFunc2_args = [] |
| 1469 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1470 | call assert_equal([[1, ''], [0, 'ten']], g:MytsrFunc2_args) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame^] | 1471 | bw! |
| 1472 | |
| 1473 | " Vim9 tests |
| 1474 | let lines =<< trim END |
| 1475 | vim9script |
| 1476 | |
| 1477 | # Test for using function() |
| 1478 | def Vim9tsrFunc(val: number, findstart: number, base: string): any |
| 1479 | add(g:Vim9tsrFunc_Args, [val, findstart, base]) |
| 1480 | return findstart ? 0 : [] |
| 1481 | enddef |
| 1482 | set thesaurusfunc=function('Vim9tsrFunc',\ [60]) |
| 1483 | new | only |
| 1484 | setline(1, 'one') |
| 1485 | g:Vim9tsrFunc_Args = [] |
| 1486 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1487 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args) |
| 1488 | bw! |
| 1489 | |
| 1490 | # Test for using a lambda |
| 1491 | &thesaurusfunc = (a, b) => Vim9tsrFunc(61, a, b) |
| 1492 | new | only |
| 1493 | setline(1, 'two') |
| 1494 | g:Vim9tsrFunc_Args = [] |
| 1495 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1496 | assert_equal([[61, 1, ''], [61, 0, 'two']], g:Vim9tsrFunc_Args) |
| 1497 | bw! |
| 1498 | |
| 1499 | # Test for using a string(lambda) |
| 1500 | &thesaurusfunc = '(a, b) => Vim9tsrFunc(62, a, b)' |
| 1501 | new | only |
| 1502 | setline(1, 'two') |
| 1503 | g:Vim9tsrFunc_Args = [] |
| 1504 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1505 | assert_equal([[62, 1, ''], [62, 0, 'two']], g:Vim9tsrFunc_Args) |
| 1506 | bw! |
| 1507 | |
| 1508 | # Test for using a variable with a lambda expression |
| 1509 | var Fn: func = (a, b) => Vim9tsrFunc(63, a, b) |
| 1510 | &thesaurusfunc = Fn |
| 1511 | new | only |
| 1512 | setline(1, 'three') |
| 1513 | g:Vim9tsrFunc_Args = [] |
| 1514 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1515 | assert_equal([[63, 1, ''], [63, 0, 'three']], g:Vim9tsrFunc_Args) |
| 1516 | bw! |
| 1517 | |
| 1518 | # Test for using a string(variable with a lambda expression) |
| 1519 | Fn = (a, b) => Vim9tsrFunc(64, a, b) |
| 1520 | &thesaurusfunc = string(Fn) |
| 1521 | new | only |
| 1522 | setline(1, 'three') |
| 1523 | g:Vim9tsrFunc_Args = [] |
| 1524 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1525 | assert_equal([[64, 1, ''], [64, 0, 'three']], g:Vim9tsrFunc_Args) |
| 1526 | bw! |
| 1527 | END |
| 1528 | call CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1529 | |
| 1530 | " cleanup |
| 1531 | set thesaurusfunc& |
| 1532 | delfunc MytsrFunc1 |
| 1533 | delfunc MytsrFunc2 |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1534 | %bw! |
| 1535 | endfunc |
| 1536 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 1537 | " vim: shiftwidth=2 sts=2 expandtab |