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 |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 5 | import './vim9.vim' as v9 |
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. |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 12 | call mkdir('Xcpldir') |
| 13 | cd Xcpldir |
Bram Moolenaar | fb094e1 | 2017-11-05 20:59:28 +0100 | [diff] [blame] | 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 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 50 | set cpt=.,\ ,w,i |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 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 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 74 | " Test for expanding a non-existing filename |
| 75 | exe "normal oa1b2X3Y4\<C-X>\<C-F>" |
| 76 | call assert_equal('a1b2X3Y4', getline('.')) |
| 77 | normal ddk |
| 78 | |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 79 | set cpt=w |
| 80 | " checks make_cyclic in other window |
| 81 | exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>" |
| 82 | call assert_equal('STARTTEST', getline('.')) |
| 83 | |
| 84 | set cpt=u nohid |
| 85 | " checks unloaded buffer expansion |
| 86 | only |
| 87 | exe "normal oEN\<C-N>" |
| 88 | call assert_equal('ENDTEST', getline('.')) |
| 89 | " checks adding mode abortion |
| 90 | exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>" |
| 91 | call assert_equal('unless', getline('.')) |
| 92 | |
| 93 | set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch |
| 94 | " tag expansion, define add-expansion interrupted |
| 95 | 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>" |
| 96 | call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.')) |
| 97 | " t-expansion |
| 98 | exe "normal oa\<C-N>\<Esc>" |
| 99 | call assert_equal('asd', getline('.')) |
| 100 | |
| 101 | %bw! |
| 102 | call delete('Xtestfile') |
| 103 | call delete('Xtest11.one') |
| 104 | call delete('Xtest11.two') |
| 105 | call delete('Xtestdata') |
| 106 | set cpt& cot& def& tags& tagbsearch& hidden& |
Bram Moolenaar | fb094e1 | 2017-11-05 20:59:28 +0100 | [diff] [blame] | 107 | cd .. |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 108 | call delete('Xcpldir', 'rf') |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 109 | endfunc |
Bram Moolenaar | ffd99f7 | 2017-11-02 15:44:14 +0100 | [diff] [blame] | 110 | |
Bram Moolenaar | 4b28ba3 | 2021-12-27 19:28:37 +0000 | [diff] [blame] | 111 | func Test_ins_complete_invalid_byte() |
| 112 | if has('unix') && executable('base64') |
| 113 | " this weird command was causing an illegal memory access |
| 114 | call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64') |
| 115 | call system('base64 -d Xinvalid64 > Xinvalid') |
| 116 | call writefile(['qa!'], 'Xexit') |
| 117 | call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit") |
| 118 | call delete('Xinvalid64') |
| 119 | call delete('Xinvalid') |
| 120 | call delete('Xexit') |
| 121 | endif |
| 122 | endfunc |
| 123 | |
Bram Moolenaar | ffd99f7 | 2017-11-02 15:44:14 +0100 | [diff] [blame] | 124 | func Test_omni_dash() |
| 125 | func Omni(findstart, base) |
| 126 | if a:findstart |
| 127 | return 5 |
| 128 | else |
| 129 | echom a:base |
| 130 | return ['-help', '-v'] |
| 131 | endif |
| 132 | endfunc |
| 133 | set omnifunc=Omni |
| 134 | new |
| 135 | exe "normal Gofind -\<C-x>\<C-o>" |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 136 | call assert_equal("find -help", getline('$')) |
Bram Moolenaar | ffd99f7 | 2017-11-02 15:44:14 +0100 | [diff] [blame] | 137 | |
| 138 | bwipe! |
| 139 | delfunc Omni |
| 140 | set omnifunc= |
| 141 | endfunc |
Bram Moolenaar | 02ae9b4 | 2018-02-09 15:06:02 +0100 | [diff] [blame] | 142 | |
LemonBoy | 9bcb9ca | 2022-05-26 15:23:26 +0100 | [diff] [blame] | 143 | func Test_omni_throw() |
| 144 | let g:CallCount = 0 |
| 145 | func Omni(findstart, base) |
| 146 | let g:CallCount += 1 |
| 147 | if a:findstart |
| 148 | throw "he he he" |
| 149 | endif |
| 150 | endfunc |
| 151 | set omnifunc=Omni |
| 152 | new |
| 153 | try |
| 154 | exe "normal ifoo\<C-x>\<C-o>" |
| 155 | call assert_false(v:true, 'command should have failed') |
| 156 | catch |
| 157 | call assert_exception('he he he') |
| 158 | call assert_equal(1, g:CallCount) |
| 159 | endtry |
| 160 | |
| 161 | bwipe! |
| 162 | delfunc Omni |
| 163 | unlet g:CallCount |
| 164 | set omnifunc= |
| 165 | endfunc |
| 166 | |
Bram Moolenaar | ff34bee | 2021-07-25 20:27:06 +0200 | [diff] [blame] | 167 | func Test_omni_autoload() |
| 168 | let save_rtp = &rtp |
| 169 | set rtp=Xruntime/some |
| 170 | let dir = 'Xruntime/some/autoload' |
| 171 | call mkdir(dir, 'p') |
| 172 | |
| 173 | let lines =<< trim END |
| 174 | vim9script |
Bram Moolenaar | 6a05807 | 2022-01-30 18:56:35 +0000 | [diff] [blame] | 175 | export def Func(findstart: bool, base: string): any |
Bram Moolenaar | ff34bee | 2021-07-25 20:27:06 +0200 | [diff] [blame] | 176 | if findstart |
| 177 | return 1 |
| 178 | else |
| 179 | return ['match'] |
| 180 | endif |
| 181 | enddef |
| 182 | { |
| 183 | eval 1 + 2 |
| 184 | } |
| 185 | END |
| 186 | call writefile(lines, dir .. '/omni.vim') |
| 187 | |
| 188 | new |
Bram Moolenaar | 6a05807 | 2022-01-30 18:56:35 +0000 | [diff] [blame] | 189 | setlocal omnifunc=omni#Func |
Bram Moolenaar | ff34bee | 2021-07-25 20:27:06 +0200 | [diff] [blame] | 190 | call feedkeys("i\<C-X>\<C-O>\<Esc>", 'xt') |
| 191 | |
| 192 | bwipe! |
| 193 | call delete('Xruntime', 'rf') |
| 194 | set omnifunc= |
| 195 | let &rtp = save_rtp |
| 196 | endfunc |
| 197 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 198 | func Test_completefunc_args() |
| 199 | let s:args = [] |
| 200 | func! CompleteFunc(findstart, base) |
| 201 | let s:args += [[a:findstart, empty(a:base)]] |
| 202 | endfunc |
| 203 | new |
| 204 | |
| 205 | set completefunc=CompleteFunc |
| 206 | call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x') |
Bram Moolenaar | 52d3aae | 2018-06-13 21:27:24 +0200 | [diff] [blame] | 207 | call assert_equal([1, 1], s:args[0]) |
| 208 | call assert_equal(0, s:args[1][0]) |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 209 | set completefunc= |
| 210 | |
| 211 | let s:args = [] |
| 212 | set omnifunc=CompleteFunc |
| 213 | call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x') |
Bram Moolenaar | 52d3aae | 2018-06-13 21:27:24 +0200 | [diff] [blame] | 214 | call assert_equal([1, 1], s:args[0]) |
| 215 | call assert_equal(0, s:args[1][0]) |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 216 | set omnifunc= |
| 217 | |
| 218 | bwipe! |
| 219 | unlet s:args |
| 220 | delfunc CompleteFunc |
| 221 | endfunc |
| 222 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 223 | func s:CompleteDone_CompleteFuncNone( findstart, base ) |
| 224 | if a:findstart |
| 225 | return 0 |
| 226 | endif |
| 227 | |
| 228 | return v:none |
| 229 | endfunc |
| 230 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 231 | func s:CompleteDone_CompleteFuncDict( findstart, base ) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 232 | if a:findstart |
| 233 | return 0 |
| 234 | endif |
| 235 | |
| 236 | return { |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 237 | \ 'words': [ |
| 238 | \ { |
| 239 | \ 'word': 'aword', |
| 240 | \ 'abbr': 'wrd', |
| 241 | \ 'menu': 'extra text', |
| 242 | \ 'info': 'words are cool', |
| 243 | \ 'kind': 'W', |
| 244 | \ 'user_data': 'test' |
| 245 | \ } |
| 246 | \ ] |
| 247 | \ } |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 248 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 249 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 250 | func s:CompleteDone_CheckCompletedItemNone() |
| 251 | let s:called_completedone = 1 |
| 252 | endfunc |
| 253 | |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 254 | func s:CompleteDone_CheckCompletedItemDict(pre) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 255 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 256 | call assert_equal( 'wrd', v:completed_item[ 'abbr' ] ) |
| 257 | call assert_equal( 'extra text', v:completed_item[ 'menu' ] ) |
| 258 | call assert_equal( 'words are cool', v:completed_item[ 'info' ] ) |
| 259 | call assert_equal( 'W', v:completed_item[ 'kind' ] ) |
| 260 | call assert_equal( 'test', v:completed_item[ 'user_data' ] ) |
| 261 | |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 262 | if a:pre |
| 263 | call assert_equal('function', complete_info().mode) |
| 264 | endif |
Bram Moolenaar | 17e0478 | 2020-01-17 18:58:59 +0100 | [diff] [blame] | 265 | |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 266 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 267 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 268 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 269 | func Test_CompleteDoneNone() |
| 270 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone() |
Bram Moolenaar | 9845f36 | 2019-04-08 18:59:54 +0200 | [diff] [blame] | 271 | 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] | 272 | |
| 273 | set completefunc=<SID>CompleteDone_CompleteFuncNone |
| 274 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 275 | set completefunc& |
Bram Moolenaar | 9845f36 | 2019-04-08 18:59:54 +0200 | [diff] [blame] | 276 | 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] | 277 | |
| 278 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9845f36 | 2019-04-08 18:59:54 +0200 | [diff] [blame] | 279 | call assert_equal(oldline, newline) |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 280 | |
| 281 | let s:called_completedone = 0 |
| 282 | au! CompleteDone |
| 283 | endfunc |
| 284 | |
| 285 | func Test_CompleteDoneDict() |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 286 | au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1) |
| 287 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 288 | |
| 289 | set completefunc=<SID>CompleteDone_CompleteFuncDict |
| 290 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 291 | set completefunc& |
| 292 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 293 | call assert_equal('test', v:completed_item[ 'user_data' ]) |
| 294 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 295 | |
| 296 | let s:called_completedone = 0 |
| 297 | au! CompleteDone |
| 298 | endfunc |
| 299 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 300 | func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 301 | if a:findstart |
| 302 | return 0 |
| 303 | endif |
| 304 | |
| 305 | return { |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 306 | \ 'words': [ |
| 307 | \ { |
| 308 | \ 'word': 'aword', |
| 309 | \ 'abbr': 'wrd', |
| 310 | \ 'menu': 'extra text', |
| 311 | \ 'info': 'words are cool', |
| 312 | \ 'kind': 'W', |
| 313 | \ 'user_data': ['one', 'two'], |
| 314 | \ } |
| 315 | \ ] |
| 316 | \ } |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 317 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 318 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 319 | func s:CompleteDone_CheckCompletedItemDictNoUserData() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 320 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 321 | call assert_equal( 'wrd', v:completed_item[ 'abbr' ] ) |
| 322 | call assert_equal( 'extra text', v:completed_item[ 'menu' ] ) |
| 323 | call assert_equal( 'words are cool', v:completed_item[ 'info' ] ) |
| 324 | call assert_equal( 'W', v:completed_item[ 'kind' ] ) |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 325 | call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] ) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 326 | |
| 327 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 328 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 329 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 330 | func Test_CompleteDoneDictNoUserData() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 331 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData() |
| 332 | |
| 333 | set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData |
| 334 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 335 | set completefunc& |
| 336 | |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 337 | call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ]) |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 338 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 339 | |
| 340 | let s:called_completedone = 0 |
| 341 | au! CompleteDone |
| 342 | endfunc |
| 343 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 344 | func s:CompleteDone_CompleteFuncList(findstart, base) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 345 | if a:findstart |
| 346 | return 0 |
| 347 | endif |
| 348 | |
| 349 | return [ 'aword' ] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 350 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 351 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 352 | func s:CompleteDone_CheckCompletedItemList() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 353 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 354 | call assert_equal( '', v:completed_item[ 'abbr' ] ) |
| 355 | call assert_equal( '', v:completed_item[ 'menu' ] ) |
| 356 | call assert_equal( '', v:completed_item[ 'info' ] ) |
| 357 | call assert_equal( '', v:completed_item[ 'kind' ] ) |
| 358 | call assert_equal( '', v:completed_item[ 'user_data' ] ) |
| 359 | |
| 360 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 361 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 362 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 363 | func Test_CompleteDoneList() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 364 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList() |
| 365 | |
| 366 | set completefunc=<SID>CompleteDone_CompleteFuncList |
| 367 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 368 | set completefunc& |
| 369 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 370 | call assert_equal('', v:completed_item[ 'user_data' ]) |
| 371 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 372 | |
| 373 | let s:called_completedone = 0 |
| 374 | au! CompleteDone |
| 375 | endfunc |
| 376 | |
Bram Moolenaar | af559d2 | 2018-08-08 22:55:41 +0200 | [diff] [blame] | 377 | func Test_CompleteDone_undo() |
| 378 | au CompleteDone * call append(0, "prepend1") |
| 379 | new |
| 380 | call setline(1, ["line1", "line2"]) |
| 381 | call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx") |
| 382 | call assert_equal(["prepend1", "line1", "line2", "line1", ""], |
| 383 | \ getline(1, '$')) |
| 384 | undo |
| 385 | call assert_equal(["line1", "line2"], getline(1, '$')) |
| 386 | bwipe! |
| 387 | au! CompleteDone |
| 388 | endfunc |
| 389 | |
Shougo Matsushita | 61021aa | 2022-07-27 14:40:00 +0100 | [diff] [blame] | 390 | func Test_CompleteDone_modify() |
| 391 | let value = { |
| 392 | \ 'word': '', |
| 393 | \ 'abbr': '', |
| 394 | \ 'menu': '', |
| 395 | \ 'info': '', |
| 396 | \ 'kind': '', |
| 397 | \ 'user_data': '', |
| 398 | \ } |
| 399 | let v:completed_item = value |
zeertzjq | 7502094 | 2022-07-31 11:37:20 +0100 | [diff] [blame] | 400 | call assert_equal(value, v:completed_item) |
Shougo Matsushita | 61021aa | 2022-07-27 14:40:00 +0100 | [diff] [blame] | 401 | endfunc |
| 402 | |
Bram Moolenaar | b806aa5 | 2020-09-12 22:52:57 +0200 | [diff] [blame] | 403 | func CompleteTest(findstart, query) |
| 404 | if a:findstart |
| 405 | return col('.') |
| 406 | endif |
| 407 | return ['matched'] |
| 408 | endfunc |
| 409 | |
| 410 | func Test_completefunc_info() |
| 411 | new |
| 412 | set completeopt=menuone |
| 413 | set completefunc=CompleteTest |
| 414 | 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] | 415 | 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] | 416 | bwipe! |
| 417 | set completeopt& |
| 418 | set completefunc& |
| 419 | endfunc |
| 420 | |
Bram Moolenaar | 02ae9b4 | 2018-02-09 15:06:02 +0100 | [diff] [blame] | 421 | " Check that when using feedkeys() typeahead does not interrupt searching for |
| 422 | " completions. |
| 423 | func Test_compl_feedkeys() |
| 424 | new |
| 425 | set completeopt=menuone,noselect |
| 426 | call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx") |
| 427 | call assert_equal("jump jump", getline(1)) |
| 428 | bwipe! |
| 429 | set completeopt& |
| 430 | endfunc |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 431 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 432 | " Test for insert path completion with completeslash option |
| 433 | func Test_ins_completeslash() |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 434 | CheckMSWindows |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 435 | |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 436 | call mkdir('Xcpldir') |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 437 | let orig_shellslash = &shellslash |
| 438 | set cpt& |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 439 | new |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 440 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 441 | set noshellslash |
| 442 | |
| 443 | set completeslash= |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 444 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 445 | call assert_equal('Xcpldir\', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 446 | |
| 447 | set completeslash=backslash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 448 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 449 | call assert_equal('Xcpldir\', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 450 | |
| 451 | set completeslash=slash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 452 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 453 | call assert_equal('Xcpldir/', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 454 | |
| 455 | set shellslash |
| 456 | |
| 457 | set completeslash= |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 458 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 459 | call assert_equal('Xcpldir/', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 460 | |
| 461 | set completeslash=backslash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 462 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 463 | call assert_equal('Xcpldir\', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 464 | |
| 465 | set completeslash=slash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 466 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 467 | call assert_equal('Xcpldir/', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 468 | %bw! |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 469 | call delete('Xcpldir', 'rf') |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 470 | |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 471 | set noshellslash |
| 472 | set completeslash=slash |
| 473 | call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1) |
| 474 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 475 | let &shellslash = orig_shellslash |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 476 | set completeslash= |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 477 | endfunc |
| 478 | |
Bram Moolenaar | d0e1b71 | 2020-09-27 20:13:03 +0200 | [diff] [blame] | 479 | func Test_pum_stopped_by_timer() |
| 480 | CheckScreendump |
| 481 | |
| 482 | let lines =<< trim END |
| 483 | call setline(1, ['hello', 'hullo', 'heeee', '']) |
| 484 | func StartCompl() |
| 485 | call timer_start(100, { -> execute('stopinsert') }) |
| 486 | call feedkeys("Gah\<C-N>") |
| 487 | endfunc |
| 488 | END |
| 489 | |
| 490 | call writefile(lines, 'Xpumscript') |
| 491 | let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12}) |
| 492 | call term_sendkeys(buf, ":call StartCompl()\<CR>") |
| 493 | call TermWait(buf, 200) |
| 494 | call term_sendkeys(buf, "k") |
| 495 | call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {}) |
| 496 | |
| 497 | call StopVimInTerminal(buf) |
| 498 | call delete('Xpumscript') |
| 499 | endfunc |
| 500 | |
zeertzjq | cd5dbad | 2022-05-04 17:51:50 +0100 | [diff] [blame] | 501 | func Test_complete_stopinsert_startinsert() |
| 502 | nnoremap <F2> <Cmd>startinsert<CR> |
| 503 | inoremap <F2> <Cmd>stopinsert<CR> |
| 504 | " This just checks if this causes an error |
| 505 | call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x') |
| 506 | nunmap <F2> |
| 507 | iunmap <F2> |
| 508 | endfunc |
| 509 | |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 510 | func Test_pum_with_folds_two_tabs() |
| 511 | CheckScreendump |
| 512 | |
| 513 | let lines =<< trim END |
| 514 | set fdm=marker |
| 515 | call setline(1, ['" x {{{1', '" a some text']) |
| 516 | call setline(3, range(&lines)->map({_, val -> '" a' .. val})) |
| 517 | norm! zm |
| 518 | tab sp |
| 519 | call feedkeys('2Gzv', 'xt') |
| 520 | call feedkeys("0fa", 'xt') |
| 521 | END |
| 522 | |
| 523 | call writefile(lines, 'Xpumscript') |
| 524 | let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 525 | call TermWait(buf, 50) |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 526 | call term_sendkeys(buf, "a\<C-N>") |
| 527 | call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {}) |
| 528 | |
| 529 | call term_sendkeys(buf, "\<Esc>") |
| 530 | call StopVimInTerminal(buf) |
| 531 | call delete('Xpumscript') |
| 532 | endfunc |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 533 | |
| 534 | func Test_pum_with_preview_win() |
| 535 | CheckScreendump |
| 536 | |
| 537 | let lines =<< trim END |
| 538 | funct Omni_test(findstart, base) |
| 539 | if a:findstart |
| 540 | return col(".") - 1 |
| 541 | endif |
| 542 | return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}] |
| 543 | endfunc |
| 544 | set omnifunc=Omni_test |
| 545 | set completeopt+=longest |
| 546 | END |
| 547 | |
| 548 | call writefile(lines, 'Xpreviewscript') |
| 549 | let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 550 | call TermWait(buf, 50) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 551 | call term_sendkeys(buf, "Gi\<C-X>\<C-O>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 552 | call TermWait(buf, 100) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 553 | call term_sendkeys(buf, "\<C-N>") |
| 554 | call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {}) |
| 555 | |
| 556 | call term_sendkeys(buf, "\<Esc>") |
| 557 | call StopVimInTerminal(buf) |
| 558 | call delete('Xpreviewscript') |
| 559 | endfunc |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 560 | |
Bram Moolenaar | 35d8c20 | 2022-03-03 11:46:00 +0000 | [diff] [blame] | 561 | func Test_scrollbar_on_wide_char() |
| 562 | CheckScreendump |
| 563 | |
| 564 | let lines =<< trim END |
| 565 | call setline(1, ['a', ' 啊啊啊', |
| 566 | \ ' 哦哦哦', |
| 567 | \ ' 呃呃呃']) |
| 568 | call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'})) |
| 569 | END |
| 570 | call writefile(lines, 'Xwidescript') |
| 571 | let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10}) |
| 572 | call term_sendkeys(buf, "A\<C-N>") |
| 573 | call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {}) |
| 574 | |
| 575 | call StopVimInTerminal(buf) |
| 576 | call delete('Xwidescript') |
| 577 | endfunc |
| 578 | |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 579 | " Test for inserting the tag search pattern in insert mode |
| 580 | func Test_ins_compl_tag_sft() |
| 581 | call writefile([ |
| 582 | \ "!_TAG_FILE_ENCODING\tutf-8\t//", |
| 583 | \ "first\tXfoo\t/^int first() {}$/", |
| 584 | \ "second\tXfoo\t/^int second() {}$/", |
| 585 | \ "third\tXfoo\t/^int third() {}$/"], |
| 586 | \ 'Xtags') |
| 587 | set tags=Xtags |
| 588 | let code =<< trim [CODE] |
| 589 | int first() {} |
| 590 | int second() {} |
| 591 | int third() {} |
| 592 | [CODE] |
| 593 | call writefile(code, 'Xfoo') |
| 594 | |
| 595 | enew |
| 596 | set showfulltag |
| 597 | exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>" |
| 598 | call assert_equal('int second() {}', getline(1)) |
| 599 | set noshowfulltag |
| 600 | |
| 601 | call delete('Xtags') |
| 602 | call delete('Xfoo') |
| 603 | set tags& |
| 604 | %bwipe! |
| 605 | endfunc |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 606 | |
| 607 | " Test for 'completefunc' deleting text |
| 608 | func Test_completefunc_error() |
| 609 | new |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 610 | " delete text when called for the first time |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 611 | func CompleteFunc(findstart, base) |
| 612 | if a:findstart == 1 |
| 613 | normal dd |
| 614 | return col('.') - 1 |
| 615 | endif |
| 616 | return ['a', 'b'] |
| 617 | endfunc |
| 618 | set completefunc=CompleteFunc |
| 619 | call setline(1, ['', 'abcd', '']) |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 620 | call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 621 | |
| 622 | " delete text when called for the second time |
| 623 | func CompleteFunc2(findstart, base) |
| 624 | if a:findstart == 1 |
| 625 | return col('.') - 1 |
| 626 | endif |
| 627 | normal dd |
| 628 | return ['a', 'b'] |
| 629 | endfunc |
| 630 | set completefunc=CompleteFunc2 |
| 631 | call setline(1, ['', 'abcd', '']) |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 632 | call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 633 | |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 634 | " Jump to a different window from the complete function |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 635 | func CompleteFunc3(findstart, base) |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 636 | if a:findstart == 1 |
| 637 | return col('.') - 1 |
| 638 | endif |
| 639 | wincmd p |
| 640 | return ['a', 'b'] |
| 641 | endfunc |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 642 | set completefunc=CompleteFunc3 |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 643 | new |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 644 | call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 645 | close! |
| 646 | |
| 647 | set completefunc& |
| 648 | delfunc CompleteFunc |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 649 | delfunc CompleteFunc2 |
| 650 | delfunc CompleteFunc3 |
| 651 | close! |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 652 | endfunc |
| 653 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 654 | " Test for returning non-string values from 'completefunc' |
| 655 | func Test_completefunc_invalid_data() |
| 656 | new |
| 657 | func! CompleteFunc(findstart, base) |
| 658 | if a:findstart == 1 |
| 659 | return col('.') - 1 |
| 660 | endif |
| 661 | return [{}, '', 'moon'] |
| 662 | endfunc |
| 663 | set completefunc=CompleteFunc |
| 664 | exe "normal i\<C-X>\<C-U>" |
| 665 | call assert_equal('moon', getline(1)) |
| 666 | set completefunc& |
| 667 | close! |
| 668 | endfunc |
| 669 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 670 | " Test for errors in using complete() function |
| 671 | func Test_complete_func_error() |
| 672 | call assert_fails('call complete(1, ["a"])', 'E785:') |
| 673 | func ListColors() |
| 674 | call complete(col('.'), "blue") |
| 675 | endfunc |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 676 | call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 677 | func ListMonths() |
| 678 | call complete(col('.'), test_null_list()) |
| 679 | endfunc |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 680 | call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 681 | delfunc ListColors |
| 682 | delfunc ListMonths |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 683 | call assert_fails('call complete_info({})', 'E1211:') |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 684 | call assert_equal([], complete_info(['items']).items) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 685 | endfunc |
| 686 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 687 | " Test for recursively starting completion mode using complete() |
| 688 | func Test_recursive_complete_func() |
| 689 | func ListColors() |
| 690 | call complete(5, ["red", "blue"]) |
| 691 | return '' |
| 692 | endfunc |
| 693 | new |
| 694 | call setline(1, ['a1', 'a2']) |
| 695 | set complete=. |
| 696 | exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>" |
| 697 | call assert_equal('a2blue', getline(3)) |
| 698 | delfunc ListColors |
| 699 | bw! |
| 700 | endfunc |
| 701 | |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 702 | " Test for using complete() with completeopt+=longest |
| 703 | func Test_complete_with_longest() |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 704 | new |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 705 | inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr> |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 706 | |
| 707 | " default: insert first match |
| 708 | set completeopt& |
| 709 | call setline(1, ['i']) |
| 710 | exe "normal Aa\<f3>\<esc>" |
| 711 | call assert_equal('iaax', getline(1)) |
| 712 | |
| 713 | " with longest: insert longest prefix |
| 714 | set completeopt+=longest |
| 715 | call setline(1, ['i']) |
| 716 | exe "normal Aa\<f3>\<esc>" |
| 717 | call assert_equal('iaa', getline(1)) |
| 718 | set completeopt& |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 719 | bwipe! |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 720 | endfunc |
| 721 | |
| 722 | |
Bram Moolenaar | 224a5f1 | 2020-04-28 20:29:07 +0200 | [diff] [blame] | 723 | " Test for completing words following a completed word in a line |
| 724 | func Test_complete_wrapscan() |
| 725 | " complete words from another buffer |
| 726 | new |
| 727 | call setline(1, ['one two', 'three four']) |
| 728 | new |
| 729 | setlocal complete=w |
| 730 | call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 731 | call assert_equal('two three four', getline(1)) |
| 732 | close! |
| 733 | " complete words from the current buffer |
| 734 | setlocal complete=. |
| 735 | %d |
| 736 | call setline(1, ['one two', '']) |
| 737 | call cursor(2, 1) |
| 738 | call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 739 | call assert_equal('one two one two', getline(2)) |
| 740 | close! |
| 741 | endfunc |
| 742 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 743 | " Test for completing special characters |
| 744 | func Test_complete_special_chars() |
| 745 | new |
| 746 | call setline(1, 'int .*[-\^$ func float') |
| 747 | call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt') |
| 748 | call assert_equal('int .*[-\^$ func float', getline(2)) |
| 749 | close! |
| 750 | endfunc |
| 751 | |
| 752 | " Test for completion when text is wrapped across lines. |
| 753 | func Test_complete_across_line() |
| 754 | new |
| 755 | call setline(1, ['red green blue', 'one two three']) |
| 756 | setlocal textwidth=20 |
| 757 | exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 758 | call assert_equal(['one two three red', 'green blue one'], getline(2, '$')) |
| 759 | close! |
| 760 | endfunc |
| 761 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 762 | " Test for completing words with a '.' at the end of a word. |
| 763 | func Test_complete_joinspaces() |
| 764 | new |
| 765 | call setline(1, ['one two.', 'three. four']) |
| 766 | set joinspaces |
| 767 | exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 768 | call assert_equal("one two. three. four", getline(3)) |
| 769 | set joinspaces& |
| 770 | bw! |
| 771 | endfunc |
| 772 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 773 | " Test for using CTRL-L to add one character when completing matching |
| 774 | func Test_complete_add_onechar() |
| 775 | new |
| 776 | call setline(1, ['wool', 'woodwork']) |
| 777 | call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt') |
| 778 | call assert_equal('woof', getline(3)) |
| 779 | |
| 780 | " use 'ignorecase' and backspace to erase characters from the prefix string |
| 781 | " and then add letters using CTRL-L |
| 782 | %d |
| 783 | set ignorecase backspace=2 |
| 784 | setlocal complete=. |
| 785 | call setline(1, ['workhorse', 'workload']) |
| 786 | normal Go |
| 787 | exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>r\<C-L>\<C-L>" |
| 788 | call assert_equal('workh', getline(3)) |
| 789 | set ignorecase& backspace& |
| 790 | close! |
| 791 | endfunc |
| 792 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 793 | " Test for using CTRL-X CTRL-L to complete whole lines lines |
| 794 | func Test_complete_wholeline() |
| 795 | new |
| 796 | " complete one-line |
| 797 | call setline(1, ['a1', 'a2']) |
| 798 | exe "normal ggoa\<C-X>\<C-L>" |
| 799 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 800 | " go to the next match (wrapping around the buffer) |
| 801 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>" |
| 802 | call assert_equal(['a1', 'a', 'a2'], getline(1, '$')) |
| 803 | " go to the next match |
| 804 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>" |
| 805 | call assert_equal(['a1', 'a2', 'a2'], getline(1, '$')) |
| 806 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>" |
| 807 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 808 | " repeat the test using CTRL-L |
| 809 | " go to the next match (wrapping around the buffer) |
| 810 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>" |
| 811 | call assert_equal(['a1', 'a2', 'a2'], getline(1, '$')) |
| 812 | " go to the next match |
| 813 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>" |
| 814 | call assert_equal(['a1', 'a', 'a2'], getline(1, '$')) |
| 815 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>" |
| 816 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 817 | %d |
| 818 | " use CTRL-X CTRL-L to add one more line |
| 819 | call setline(1, ['a1', 'b1']) |
| 820 | setlocal complete=. |
| 821 | exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>" |
| 822 | call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$')) |
| 823 | bw! |
| 824 | endfunc |
| 825 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 826 | " Test insert completion with 'cindent' (adjust the indent) |
| 827 | func Test_complete_with_cindent() |
| 828 | new |
| 829 | setlocal cindent |
| 830 | call setline(1, ['if (i == 1)', " j = 2;"]) |
| 831 | exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}" |
| 832 | call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$')) |
| 833 | |
| 834 | %d |
| 835 | call setline(1, ['when while', '{', '']) |
| 836 | setlocal cinkeys+==while |
| 837 | exe "normal Giwh\<C-P> " |
| 838 | call assert_equal("\twhile ", getline('$')) |
| 839 | close! |
| 840 | endfunc |
| 841 | |
| 842 | " Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions |
| 843 | func Test_complete_cmdline() |
| 844 | new |
| 845 | exe "normal icaddb\<C-X>\<C-V>" |
| 846 | call assert_equal('caddbuffer', getline(1)) |
| 847 | exe "normal ocall getqf\<C-X>\<C-V>" |
| 848 | call assert_equal('call getqflist(', getline(2)) |
| 849 | exe "normal oabcxyz(\<C-X>\<C-V>" |
| 850 | call assert_equal('abcxyz(', getline(3)) |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 851 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 852 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 853 | write TestCommand1Test |
| 854 | write TestCommand2Test |
| 855 | " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode |
| 856 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>" |
| 857 | call assert_equal('TestCommand2Test', getline(4)) |
| 858 | call delete('TestCommand1Test') |
| 859 | call delete('TestCommand2Test') |
| 860 | delcom TestCommand1 |
| 861 | delcom TestCommand2 |
| 862 | close! |
| 863 | endfunc |
| 864 | |
| 865 | " Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match |
| 866 | func Test_complete_stop() |
| 867 | new |
| 868 | func Save_mode1() |
| 869 | let g:mode1 = mode(1) |
| 870 | return '' |
| 871 | endfunc |
| 872 | func Save_mode2() |
| 873 | let g:mode2 = mode(1) |
| 874 | return '' |
| 875 | endfunc |
| 876 | inoremap <F1> <C-R>=Save_mode1()<CR> |
| 877 | inoremap <F2> <C-R>=Save_mode2()<CR> |
| 878 | call setline(1, ['aaa bbb ccc ']) |
| 879 | exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 880 | call assert_equal('ic', g:mode1) |
| 881 | call assert_equal('i', g:mode2) |
| 882 | call assert_equal('aaa bbb ccc ', getline(1)) |
| 883 | exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 884 | call assert_equal('ic', g:mode1) |
| 885 | call assert_equal('i', g:mode2) |
| 886 | call assert_equal('aaa bbb ccc aaa', getline(1)) |
| 887 | set completeopt+=noselect |
| 888 | exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 889 | call assert_equal('ic', g:mode1) |
| 890 | call assert_equal('i', g:mode2) |
| 891 | call assert_equal('aaa bbb ccc aaa bb', getline(1)) |
| 892 | set completeopt& |
| 893 | exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 894 | call assert_equal('ic', g:mode1) |
| 895 | call assert_equal('i', g:mode2) |
| 896 | call assert_equal('aaa bbb ccc aaa bb d', getline(1)) |
| 897 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 898 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 899 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 900 | call assert_equal('ic', g:mode1) |
| 901 | call assert_equal('i', g:mode2) |
| 902 | call assert_equal('TestCommand2', getline(2)) |
| 903 | delcom TestCommand1 |
| 904 | delcom TestCommand2 |
| 905 | unlet g:mode1 |
| 906 | unlet g:mode2 |
| 907 | iunmap <F1> |
| 908 | iunmap <F2> |
| 909 | delfunc Save_mode1 |
| 910 | delfunc Save_mode2 |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 911 | close! |
| 912 | endfunc |
| 913 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 914 | " Test for typing CTRL-R in insert completion mode to insert a register |
| 915 | " content. |
| 916 | func Test_complete_reginsert() |
| 917 | new |
| 918 | call setline(1, ['a1', 'a12', 'a123', 'a1234']) |
| 919 | |
| 920 | " if a valid CTRL-X mode key is returned from <C-R>=, then it should be |
| 921 | " processed. Otherwise, CTRL-X mode should be stopped and the key should be |
| 922 | " inserted. |
| 923 | exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>" |
| 924 | call assert_equal('a123', getline(5)) |
| 925 | let @r = "\<C-P>\<C-P>" |
| 926 | exe "normal GCa\<C-P>\<C-R>r" |
| 927 | call assert_equal('a12', getline(5)) |
| 928 | exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>" |
| 929 | call assert_equal('a1234x', getline(5)) |
| 930 | bw! |
| 931 | endfunc |
| 932 | |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 933 | func Test_issue_7021() |
| 934 | CheckMSWindows |
| 935 | |
| 936 | let orig_shellslash = &shellslash |
| 937 | set noshellslash |
| 938 | |
| 939 | set completeslash=slash |
| 940 | call assert_false(expand('~') =~ '/') |
| 941 | |
| 942 | let &shellslash = orig_shellslash |
| 943 | set completeslash= |
| 944 | endfunc |
| 945 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 946 | " Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings |
| 947 | func Test_complete_longest_match() |
| 948 | for e in ['latin1', 'utf-8'] |
| 949 | exe 'set encoding=' .. e |
| 950 | new |
| 951 | set complete=. |
| 952 | set completeopt=menu,longest |
| 953 | call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1']) |
| 954 | exe "normal Gopfx\<C-P>" |
| 955 | call assert_equal('pfx_', getline(5)) |
| 956 | bw! |
| 957 | endfor |
| 958 | |
| 959 | " Test for completing additional words with longest match set |
| 960 | new |
| 961 | call setline(1, ['abc1', 'abd2']) |
| 962 | exe "normal Goab\<C-P>\<C-X>\<C-P>" |
| 963 | call assert_equal('ab', getline(3)) |
| 964 | bw! |
| 965 | set complete& completeopt& |
| 966 | endfunc |
| 967 | |
| 968 | " Test for removing the first displayed completion match and selecting the |
| 969 | " match just before that. |
| 970 | func Test_complete_erase_firstmatch() |
| 971 | new |
| 972 | call setline(1, ['a12', 'a34', 'a56']) |
| 973 | set complete=. |
| 974 | exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>" |
| 975 | call assert_equal('a34', getline('$')) |
| 976 | set complete& |
| 977 | bw! |
| 978 | endfunc |
| 979 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 980 | " Test for completing words from unloaded buffers |
| 981 | func Test_complete_from_unloadedbuf() |
| 982 | call writefile(['abc'], "Xfile1") |
| 983 | call writefile(['def'], "Xfile2") |
| 984 | edit Xfile1 |
| 985 | edit Xfile2 |
| 986 | new | close |
| 987 | enew |
| 988 | bunload Xfile1 Xfile2 |
| 989 | set complete=u |
| 990 | " complete from an unloaded buffer |
| 991 | exe "normal! ia\<C-P>" |
| 992 | call assert_equal('abc', getline(1)) |
| 993 | exe "normal! od\<C-P>" |
| 994 | call assert_equal('def', getline(2)) |
| 995 | set complete& |
| 996 | %bw! |
| 997 | call delete("Xfile1") |
| 998 | call delete("Xfile2") |
| 999 | endfunc |
| 1000 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1001 | " Test for completing whole lines from unloaded buffers |
| 1002 | func Test_complete_wholeline_unloadedbuf() |
| 1003 | call writefile(['a line1', 'a line2', 'a line3'], "Xfile1") |
| 1004 | edit Xfile1 |
| 1005 | enew |
| 1006 | set complete=u |
| 1007 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1008 | call assert_equal('a line2', getline(1)) |
| 1009 | %d |
| 1010 | " completing from an unlisted buffer should fail |
| 1011 | bdel Xfile1 |
| 1012 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1013 | call assert_equal('a', getline(1)) |
| 1014 | set complete& |
| 1015 | %bw! |
| 1016 | call delete("Xfile1") |
| 1017 | endfunc |
| 1018 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1019 | " Test for completing words from unlisted buffers |
| 1020 | func Test_complete_from_unlistedbuf() |
| 1021 | call writefile(['abc'], "Xfile1") |
| 1022 | call writefile(['def'], "Xfile2") |
| 1023 | edit Xfile1 |
| 1024 | edit Xfile2 |
| 1025 | new | close |
| 1026 | bdel Xfile1 Xfile2 |
| 1027 | set complete=U |
| 1028 | " complete from an unlisted buffer |
| 1029 | exe "normal! ia\<C-P>" |
| 1030 | call assert_equal('abc', getline(1)) |
| 1031 | exe "normal! od\<C-P>" |
| 1032 | call assert_equal('def', getline(2)) |
| 1033 | set complete& |
| 1034 | %bw! |
| 1035 | call delete("Xfile1") |
| 1036 | call delete("Xfile2") |
| 1037 | endfunc |
| 1038 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1039 | " Test for completing whole lines from unlisted buffers |
| 1040 | func Test_complete_wholeline_unlistedbuf() |
| 1041 | call writefile(['a line1', 'a line2', 'a line3'], "Xfile1") |
| 1042 | edit Xfile1 |
| 1043 | enew |
| 1044 | set complete=U |
| 1045 | " completing from a unloaded buffer should fail |
| 1046 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1047 | call assert_equal('a', getline(1)) |
| 1048 | %d |
| 1049 | bdel Xfile1 |
| 1050 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1051 | call assert_equal('a line2', getline(1)) |
| 1052 | set complete& |
| 1053 | %bw! |
| 1054 | call delete("Xfile1") |
| 1055 | endfunc |
| 1056 | |
| 1057 | " Test for adding a multibyte character using CTRL-L in completion mode |
| 1058 | func Test_complete_mbyte_char_add() |
| 1059 | new |
| 1060 | set complete=. |
| 1061 | call setline(1, 'abė') |
| 1062 | exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>" |
| 1063 | call assert_equal('abė', getline(2)) |
| 1064 | " Test for a leader with multibyte character |
| 1065 | %d |
| 1066 | call setline(1, 'abėĕ') |
| 1067 | exe "normal! oabė\<C-P>" |
| 1068 | call assert_equal('abėĕ', getline(2)) |
| 1069 | bw! |
| 1070 | endfunc |
| 1071 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1072 | " Test for using <C-X><C-P> for local expansion even if 'complete' is set to |
| 1073 | " not to complete matches from the local buffer. Also test using multiple |
| 1074 | " <C-X> to cancel the current completion mode. |
| 1075 | func Test_complete_local_expansion() |
| 1076 | new |
| 1077 | set complete=t |
| 1078 | call setline(1, ['abc', 'def']) |
| 1079 | exe "normal! Go\<C-X>\<C-P>" |
| 1080 | call assert_equal("def", getline(3)) |
| 1081 | exe "normal! Go\<C-P>" |
| 1082 | call assert_equal("", getline(4)) |
| 1083 | exe "normal! Go\<C-X>\<C-N>" |
| 1084 | call assert_equal("abc", getline(5)) |
| 1085 | exe "normal! Go\<C-N>" |
| 1086 | call assert_equal("", getline(6)) |
| 1087 | |
| 1088 | " use multiple <C-X> to cancel the previous completion mode |
| 1089 | exe "normal! Go\<C-P>\<C-X>\<C-P>" |
| 1090 | call assert_equal("", getline(7)) |
| 1091 | exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>" |
| 1092 | call assert_equal("", getline(8)) |
| 1093 | exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>" |
| 1094 | call assert_equal("abc", getline(9)) |
| 1095 | |
| 1096 | " interrupt the current completion mode |
| 1097 | set completeopt=menu,noinsert |
| 1098 | exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>" |
| 1099 | call assert_equal("abc", getline(10)) |
| 1100 | |
| 1101 | " when only one <C-X> is used to interrupt, do normal expansion |
| 1102 | exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>" |
| 1103 | call assert_equal("", getline(11)) |
| 1104 | set completeopt& |
| 1105 | |
| 1106 | " using two <C-X> in non-completion mode and restarting the same mode |
| 1107 | exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>" |
| 1108 | call assert_equal("def", getline(12)) |
| 1109 | |
| 1110 | " test for adding a match from the original empty text |
| 1111 | %d |
| 1112 | call setline(1, 'abc def g') |
| 1113 | exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>" |
| 1114 | call assert_equal('def', getline(2)) |
| 1115 | exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>" |
| 1116 | call assert_equal('abc', getline(2)) |
| 1117 | |
| 1118 | bw! |
| 1119 | endfunc |
| 1120 | |
| 1121 | " Test for undoing changes after a insert-mode completion |
| 1122 | func Test_complete_undo() |
| 1123 | new |
| 1124 | set complete=. |
| 1125 | " undo with 'ignorecase' |
| 1126 | call setline(1, ['ABOVE', 'BELOW']) |
| 1127 | set ignorecase |
| 1128 | exe "normal! Goab\<C-G>u\<C-P>" |
| 1129 | call assert_equal("ABOVE", getline(3)) |
| 1130 | undo |
| 1131 | call assert_equal("ab", getline(3)) |
| 1132 | set ignorecase& |
| 1133 | %d |
| 1134 | " undo with longest match |
| 1135 | set completeopt=menu,longest |
| 1136 | call setline(1, ['above', 'about']) |
| 1137 | exe "normal! Goa\<C-G>u\<C-P>" |
| 1138 | call assert_equal("abo", getline(3)) |
| 1139 | undo |
| 1140 | call assert_equal("a", getline(3)) |
| 1141 | set completeopt& |
| 1142 | %d |
| 1143 | " undo for line completion |
| 1144 | call setline(1, ['above that change', 'below that change']) |
| 1145 | exe "normal! Goabove\<C-G>u\<C-X>\<C-L>" |
| 1146 | call assert_equal("above that change", getline(3)) |
| 1147 | undo |
| 1148 | call assert_equal("above", getline(3)) |
| 1149 | |
| 1150 | bw! |
| 1151 | endfunc |
| 1152 | |
| 1153 | " Test for completing a very long word |
| 1154 | func Test_complete_long_word() |
| 1155 | set complete& |
| 1156 | new |
| 1157 | call setline(1, repeat('x', 950) .. ' one two three') |
| 1158 | exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1159 | call assert_equal(repeat('x', 950) .. ' one two three', getline(2)) |
| 1160 | %d |
| 1161 | " should fail when more than 950 characters are in a word |
| 1162 | call setline(1, repeat('x', 951) .. ' one two three') |
| 1163 | exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1164 | call assert_equal(repeat('x', 951), getline(2)) |
| 1165 | |
| 1166 | " Test for adding a very long word to an existing completion |
| 1167 | %d |
| 1168 | call setline(1, ['abc', repeat('x', 1016) .. '012345']) |
| 1169 | exe "normal! Goab\<C-P>\<C-X>\<C-P>" |
| 1170 | call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3)) |
| 1171 | bw! |
| 1172 | endfunc |
| 1173 | |
| 1174 | " Test for some fields in the complete items used by complete() |
| 1175 | func Test_complete_items() |
| 1176 | func CompleteItems(idx) |
| 1177 | let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}], |
| 1178 | \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}], |
| 1179 | \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}], |
| 1180 | \ [#{user_data: 'u9'}], |
| 1181 | \ [#{word: "", user_data: 'u10'}], |
| 1182 | \ [#{word: "", empty: 1, user_data: 'u11'}]] |
| 1183 | call complete(col('.'), items[a:idx]) |
| 1184 | return '' |
| 1185 | endfunc |
| 1186 | new |
| 1187 | exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>" |
| 1188 | call assert_equal('u2', v:completed_item.user_data) |
| 1189 | call assert_equal('one', getline(1)) |
| 1190 | exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>" |
| 1191 | call assert_equal('u3', v:completed_item.user_data) |
| 1192 | call assert_equal('one', getline(2)) |
| 1193 | exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>" |
| 1194 | call assert_equal('', getline(3)) |
| 1195 | set completeopt=menu,noinsert |
| 1196 | exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>" |
| 1197 | call assert_equal('oNE', getline(4)) |
| 1198 | call assert_equal('u8', v:completed_item.user_data) |
| 1199 | set completeopt& |
| 1200 | exe "normal! o\<C-R>=CompleteItems(3)\<CR>" |
| 1201 | call assert_equal('', getline(5)) |
| 1202 | exe "normal! o\<C-R>=CompleteItems(4)\<CR>" |
| 1203 | call assert_equal('', getline(6)) |
| 1204 | exe "normal! o\<C-R>=CompleteItems(5)\<CR>" |
| 1205 | call assert_equal('', getline(7)) |
| 1206 | call assert_equal('u11', v:completed_item.user_data) |
| 1207 | " pass invalid argument to complete() |
| 1208 | let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>" |
| 1209 | call assert_fails('exe cmd', 'E730:') |
| 1210 | bw! |
| 1211 | delfunc CompleteItems |
| 1212 | endfunc |
| 1213 | |
| 1214 | " Test for the "refresh" item in the dict returned by an insert completion |
| 1215 | " function |
| 1216 | func Test_complete_item_refresh_always() |
| 1217 | let g:CallCount = 0 |
| 1218 | func! Tcomplete(findstart, base) |
| 1219 | if a:findstart |
| 1220 | " locate the start of the word |
| 1221 | let line = getline('.') |
| 1222 | let start = col('.') - 1 |
| 1223 | while start > 0 && line[start - 1] =~ '\a' |
| 1224 | let start -= 1 |
| 1225 | endwhile |
| 1226 | return start |
| 1227 | else |
| 1228 | let g:CallCount += 1 |
| 1229 | let res = ["update1", "update12", "update123"] |
| 1230 | return #{words: res, refresh: 'always'} |
| 1231 | endif |
| 1232 | endfunc |
| 1233 | new |
| 1234 | set completeopt=menu,longest |
| 1235 | set completefunc=Tcomplete |
| 1236 | exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>" |
| 1237 | call assert_equal('up', getline(1)) |
| 1238 | call assert_equal(2, g:CallCount) |
| 1239 | set completeopt& |
| 1240 | set completefunc& |
| 1241 | bw! |
| 1242 | delfunc Tcomplete |
| 1243 | endfunc |
| 1244 | |
| 1245 | " Test for completing from a thesaurus file without read permission |
| 1246 | func Test_complete_unreadable_thesaurus_file() |
| 1247 | CheckUnix |
| 1248 | CheckNotRoot |
| 1249 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1250 | call writefile(['about', 'above'], 'Xunrfile') |
| 1251 | call setfperm('Xunrfile', '---r--r--') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1252 | new |
| 1253 | set complete=sXfile |
| 1254 | exe "normal! ia\<C-P>" |
| 1255 | call assert_equal('a', getline(1)) |
| 1256 | bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1257 | call delete('Xunrfile') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1258 | set complete& |
| 1259 | endfunc |
| 1260 | |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1261 | " Test to ensure 'Scanning...' messages are not recorded in messages history |
| 1262 | func Test_z1_complete_no_history() |
| 1263 | new |
| 1264 | messages clear |
| 1265 | let currmess = execute('messages') |
| 1266 | setlocal dictionary=README.txt |
| 1267 | exe "normal owh\<C-X>\<C-K>" |
| 1268 | exe "normal owh\<C-N>" |
| 1269 | call assert_equal(currmess, execute('messages')) |
Bram Moolenaar | d979d64 | 2022-03-04 14:51:06 +0000 | [diff] [blame] | 1270 | bwipe! |
| 1271 | endfunc |
| 1272 | |
| 1273 | " A mapping is not used for the key after CTRL-X. |
| 1274 | func Test_no_mapping_for_ctrl_x_key() |
| 1275 | new |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 1276 | inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR> |
Bram Moolenaar | d979d64 | 2022-03-04 14:51:06 +0000 | [diff] [blame] | 1277 | setlocal dictionary=README.txt |
| 1278 | call feedkeys("aexam\<C-X>\<C-K> ", 'xt') |
| 1279 | call assert_equal('example ', getline(1)) |
| 1280 | call assert_false(exists('was_mapped')) |
| 1281 | bwipe! |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1282 | endfunc |
| 1283 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1284 | " Test for different ways of setting the 'completefunc' option |
| 1285 | func Test_completefunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1286 | func CompleteFunc1(callnr, findstart, base) |
| 1287 | call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base]) |
| 1288 | return a:findstart ? 0 : [] |
| 1289 | endfunc |
| 1290 | func CompleteFunc2(findstart, base) |
| 1291 | call add(g:CompleteFunc2Args, [a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1292 | return a:findstart ? 0 : [] |
| 1293 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1294 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1295 | let lines =<< trim END |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1296 | #" Test for using a global function name |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1297 | LET &completefunc = 'g:CompleteFunc2' |
| 1298 | new |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1299 | call setline(1, 'global') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1300 | LET g:CompleteFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1301 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1302 | call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1303 | bw! |
| 1304 | |
| 1305 | #" Test for using a function() |
| 1306 | set completefunc=function('g:CompleteFunc1',\ [10]) |
| 1307 | new |
| 1308 | call setline(1, 'one') |
| 1309 | LET g:CompleteFunc1Args = [] |
| 1310 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1311 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1312 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1313 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1314 | #" Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1315 | VAR Fn = function('g:CompleteFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1316 | LET &completefunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1317 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1318 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1319 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1320 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1321 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1322 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1323 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1324 | #" Using string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1325 | LET Fn = function('g:CompleteFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1326 | LET &completefunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1327 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1328 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1329 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1330 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1331 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1332 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1333 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1334 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1335 | set completefunc=funcref('g:CompleteFunc1',\ [13]) |
| 1336 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1337 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1338 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1339 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1340 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1341 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1342 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1343 | #" Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1344 | LET Fn = funcref('g:CompleteFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1345 | LET &completefunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1346 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1347 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1348 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1349 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1350 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1351 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1352 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1353 | #" Using a string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1354 | LET Fn = funcref('g:CompleteFunc1', [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1355 | LET &completefunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1356 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1357 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1358 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1359 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1360 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1361 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1362 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1363 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1364 | VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1365 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1366 | exe "set completefunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1367 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1368 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1369 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1370 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1371 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1372 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1373 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1374 | #" Set 'completefunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1375 | LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1376 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1377 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1378 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1379 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1380 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1381 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1382 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1383 | #" Set 'completefunc' to string(lambda_expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1384 | LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1385 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1386 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1387 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1388 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1389 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1390 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1391 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1392 | #" Set 'completefunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1393 | VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1394 | LET &completefunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1395 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1396 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1397 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1398 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1399 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1400 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1401 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1402 | #" Set 'completefunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1403 | LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1404 | LET &completefunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1405 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1406 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1407 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1408 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1409 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1410 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1411 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1412 | #" Test for using a lambda function with incorrect return value |
| 1413 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 1414 | LET &completefunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1415 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1416 | call setline(1, 'eight') |
| 1417 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1418 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1419 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1420 | #" Test for clearing the 'completefunc' option |
| 1421 | set completefunc='' |
| 1422 | set completefunc& |
| 1423 | call assert_fails("set completefunc=function('abc')", "E700:") |
| 1424 | call assert_fails("set completefunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1425 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1426 | #" set 'completefunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1427 | set completefunc=g:CompleteFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1428 | call setline(1, 'five') |
| 1429 | call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') |
| 1430 | call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1431 | LET g:CompleteFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1432 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1433 | call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1434 | bw! |
| 1435 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1436 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1437 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1438 | " Test for using a script-local function name |
| 1439 | func s:CompleteFunc3(findstart, base) |
| 1440 | call add(g:CompleteFunc3Args, [a:findstart, a:base]) |
| 1441 | return a:findstart ? 0 : [] |
| 1442 | endfunc |
| 1443 | set completefunc=s:CompleteFunc3 |
| 1444 | new |
| 1445 | call setline(1, 'script1') |
| 1446 | let g:CompleteFunc3Args = [] |
| 1447 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1448 | call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args) |
| 1449 | bw! |
| 1450 | |
| 1451 | let &completefunc = 's:CompleteFunc3' |
| 1452 | new |
| 1453 | call setline(1, 'script2') |
| 1454 | let g:CompleteFunc3Args = [] |
| 1455 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1456 | call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args) |
| 1457 | bw! |
| 1458 | delfunc s:CompleteFunc3 |
| 1459 | |
Bram Moolenaar | 1fca5f3 | 2022-02-18 17:50:47 +0000 | [diff] [blame] | 1460 | " In Vim9 script s: can be omitted |
| 1461 | let lines =<< trim END |
| 1462 | vim9script |
| 1463 | var CompleteFunc4Args = [] |
| 1464 | def CompleteFunc4(findstart: bool, base: string): any |
| 1465 | add(CompleteFunc4Args, [findstart, base]) |
| 1466 | return findstart ? 0 : [] |
| 1467 | enddef |
| 1468 | set completefunc=CompleteFunc4 |
| 1469 | new |
| 1470 | setline(1, 'script1') |
| 1471 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1472 | assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args) |
| 1473 | bw! |
| 1474 | END |
| 1475 | call v9.CheckScriptSuccess(lines) |
| 1476 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1477 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1478 | let &completefunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1479 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1480 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1481 | " Using Vim9 lambda expression in legacy context should fail |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1482 | set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1483 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1484 | let g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1485 | call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1486 | call assert_equal([], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1487 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1488 | " set 'completefunc' to a partial with dict. This used to cause a crash. |
| 1489 | func SetCompleteFunc() |
| 1490 | let params = {'complete': function('g:DictCompleteFunc')} |
| 1491 | let &completefunc = params.complete |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1492 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1493 | func g:DictCompleteFunc(_) dict |
| 1494 | endfunc |
| 1495 | call SetCompleteFunc() |
| 1496 | new |
| 1497 | call SetCompleteFunc() |
| 1498 | bw |
| 1499 | call test_garbagecollect_now() |
| 1500 | new |
| 1501 | set completefunc= |
| 1502 | wincmd w |
| 1503 | set completefunc= |
| 1504 | %bw! |
| 1505 | delfunc g:DictCompleteFunc |
| 1506 | delfunc SetCompleteFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1507 | |
| 1508 | " Vim9 tests |
| 1509 | let lines =<< trim END |
| 1510 | vim9script |
| 1511 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1512 | def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any |
| 1513 | add(g:Vim9completeFuncArgs, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1514 | return findstart ? 0 : [] |
| 1515 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1516 | |
| 1517 | # Test for using a def function with completefunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1518 | set completefunc=function('Vim9CompleteFunc',\ [60]) |
| 1519 | new | only |
| 1520 | setline(1, 'one') |
| 1521 | g:Vim9completeFuncArgs = [] |
| 1522 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1523 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) |
| 1524 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1525 | |
| 1526 | # Test for using a global function name |
| 1527 | &completefunc = g:CompleteFunc2 |
| 1528 | new | only |
| 1529 | setline(1, 'two') |
| 1530 | g:CompleteFunc2Args = [] |
| 1531 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1532 | assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args) |
| 1533 | bw! |
| 1534 | |
| 1535 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1536 | def LocalCompleteFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1537 | add(g:LocalCompleteFuncArgs, [findstart, base]) |
| 1538 | return findstart ? 0 : [] |
| 1539 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1540 | &completefunc = LocalCompleteFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1541 | new | only |
| 1542 | setline(1, 'three') |
| 1543 | g:LocalCompleteFuncArgs = [] |
| 1544 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1545 | assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs) |
| 1546 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1547 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1548 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1549 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1550 | " cleanup |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1551 | set completefunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1552 | delfunc CompleteFunc1 |
| 1553 | delfunc CompleteFunc2 |
| 1554 | unlet g:CompleteFunc1Args g:CompleteFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1555 | %bw! |
| 1556 | endfunc |
| 1557 | |
| 1558 | " Test for different ways of setting the 'omnifunc' option |
| 1559 | func Test_omnifunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1560 | func OmniFunc1(callnr, findstart, base) |
| 1561 | call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base]) |
| 1562 | return a:findstart ? 0 : [] |
| 1563 | endfunc |
| 1564 | func OmniFunc2(findstart, base) |
| 1565 | call add(g:OmniFunc2Args, [a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1566 | return a:findstart ? 0 : [] |
| 1567 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1568 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1569 | let lines =<< trim END |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1570 | #" Test for using a function name |
| 1571 | LET &omnifunc = 'g:OmniFunc2' |
| 1572 | new |
| 1573 | call setline(1, 'zero') |
| 1574 | LET g:OmniFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1575 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1576 | call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args) |
| 1577 | bw! |
| 1578 | |
| 1579 | #" Test for using a function() |
| 1580 | set omnifunc=function('g:OmniFunc1',\ [10]) |
| 1581 | new |
| 1582 | call setline(1, 'one') |
| 1583 | LET g:OmniFunc1Args = [] |
| 1584 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1585 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1586 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1587 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1588 | #" Using a funcref variable to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1589 | VAR Fn = function('g:OmniFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1590 | LET &omnifunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1591 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1592 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1593 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1594 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1595 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1596 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1597 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1598 | #" Using a string(funcref_variable) to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1599 | LET Fn = function('g:OmniFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1600 | LET &omnifunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1601 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1602 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1603 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1604 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1605 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1606 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1607 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1608 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1609 | set omnifunc=funcref('g:OmniFunc1',\ [13]) |
| 1610 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1611 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1612 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1613 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1614 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1615 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1616 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1617 | #" Use let to set 'omnifunc' to a funcref |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1618 | LET Fn = funcref('g:OmniFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1619 | LET &omnifunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1620 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1621 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1622 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1623 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1624 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1625 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1626 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1627 | #" Using a string(funcref) to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1628 | LET Fn = funcref("g:OmniFunc1", [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1629 | LET &omnifunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1630 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1631 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1632 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1633 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1634 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1635 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1636 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1637 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1638 | VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1639 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1640 | exe "set omnifunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1641 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1642 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1643 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1644 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1645 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1646 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1647 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1648 | #" Set 'omnifunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1649 | LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1650 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1651 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1652 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1653 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1654 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1655 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1656 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1657 | #" Set 'omnifunc' to a string(lambda_expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1658 | LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1659 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1660 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1661 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1662 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1663 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1664 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1665 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1666 | #" Set 'omnifunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1667 | VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1668 | LET &omnifunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1669 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1670 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1671 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1672 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1673 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1674 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1675 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1676 | #" Set 'omnifunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1677 | LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1678 | LET &omnifunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1679 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1680 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1681 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1682 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1683 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1684 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1685 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1686 | #" Test for using a lambda function with incorrect return value |
| 1687 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 1688 | LET &omnifunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1689 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1690 | call setline(1, 'eight') |
| 1691 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1692 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1693 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1694 | #" Test for clearing the 'omnifunc' option |
| 1695 | set omnifunc='' |
| 1696 | set omnifunc& |
| 1697 | call assert_fails("set omnifunc=function('abc')", "E700:") |
| 1698 | call assert_fails("set omnifunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1699 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1700 | #" set 'omnifunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1701 | set omnifunc=g:OmniFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1702 | call setline(1, 'nine') |
| 1703 | call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') |
| 1704 | call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1705 | LET g:OmniFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1706 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1707 | call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1708 | bw! |
| 1709 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1710 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1711 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1712 | " Test for using a script-local function name |
| 1713 | func s:OmniFunc3(findstart, base) |
| 1714 | call add(g:OmniFunc3Args, [a:findstart, a:base]) |
| 1715 | return a:findstart ? 0 : [] |
| 1716 | endfunc |
| 1717 | set omnifunc=s:OmniFunc3 |
| 1718 | new |
| 1719 | call setline(1, 'script1') |
| 1720 | let g:OmniFunc3Args = [] |
| 1721 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1722 | call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args) |
| 1723 | bw! |
| 1724 | |
| 1725 | let &omnifunc = 's:OmniFunc3' |
| 1726 | new |
| 1727 | call setline(1, 'script2') |
| 1728 | let g:OmniFunc3Args = [] |
| 1729 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1730 | call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args) |
| 1731 | bw! |
| 1732 | delfunc s:OmniFunc3 |
| 1733 | |
| 1734 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1735 | let &omnifunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1736 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1737 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1738 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1739 | set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1740 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1741 | let g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1742 | call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1743 | call assert_equal([], g:OmniFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1744 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1745 | " set 'omnifunc' to a partial with dict. This used to cause a crash. |
| 1746 | func SetOmniFunc() |
| 1747 | let params = {'omni': function('g:DictOmniFunc')} |
| 1748 | let &omnifunc = params.omni |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1749 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1750 | func g:DictOmniFunc(_) dict |
| 1751 | endfunc |
| 1752 | call SetOmniFunc() |
| 1753 | new |
| 1754 | call SetOmniFunc() |
| 1755 | bw |
| 1756 | call test_garbagecollect_now() |
| 1757 | new |
| 1758 | set omnifunc= |
| 1759 | wincmd w |
| 1760 | set omnifunc= |
| 1761 | %bw! |
| 1762 | delfunc g:DictOmniFunc |
| 1763 | delfunc SetOmniFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1764 | |
| 1765 | " Vim9 tests |
| 1766 | let lines =<< trim END |
| 1767 | vim9script |
| 1768 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1769 | def Vim9omniFunc(callnr: number, findstart: number, base: string): any |
| 1770 | add(g:Vim9omniFunc_Args, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1771 | return findstart ? 0 : [] |
| 1772 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1773 | |
| 1774 | # Test for using a def function with omnifunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1775 | set omnifunc=function('Vim9omniFunc',\ [60]) |
| 1776 | new | only |
| 1777 | setline(1, 'one') |
| 1778 | g:Vim9omniFunc_Args = [] |
| 1779 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1780 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) |
| 1781 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1782 | |
| 1783 | # Test for using a global function name |
| 1784 | &omnifunc = g:OmniFunc2 |
| 1785 | new | only |
| 1786 | setline(1, 'two') |
| 1787 | g:OmniFunc2Args = [] |
| 1788 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1789 | assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args) |
| 1790 | bw! |
| 1791 | |
| 1792 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1793 | def LocalOmniFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1794 | add(g:LocalOmniFuncArgs, [findstart, base]) |
| 1795 | return findstart ? 0 : [] |
| 1796 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1797 | &omnifunc = LocalOmniFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1798 | new | only |
| 1799 | setline(1, 'three') |
| 1800 | g:LocalOmniFuncArgs = [] |
| 1801 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1802 | assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs) |
| 1803 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1804 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1805 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1806 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1807 | " cleanup |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1808 | set omnifunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1809 | delfunc OmniFunc1 |
| 1810 | delfunc OmniFunc2 |
| 1811 | unlet g:OmniFunc1Args g:OmniFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1812 | %bw! |
| 1813 | endfunc |
| 1814 | |
| 1815 | " Test for different ways of setting the 'thesaurusfunc' option |
| 1816 | func Test_thesaurusfunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1817 | func TsrFunc1(callnr, findstart, base) |
| 1818 | call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1819 | return a:findstart ? 0 : [] |
| 1820 | endfunc |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1821 | func TsrFunc2(findstart, base) |
| 1822 | call add(g:TsrFunc2Args, [a:findstart, a:base]) |
| 1823 | return a:findstart ? 0 : ['sunday'] |
| 1824 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1825 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1826 | let lines =<< trim END |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1827 | #" Test for using a function name |
| 1828 | LET &thesaurusfunc = 'g:TsrFunc2' |
| 1829 | new |
| 1830 | call setline(1, 'zero') |
| 1831 | LET g:TsrFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1832 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1833 | call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args) |
| 1834 | bw! |
| 1835 | |
| 1836 | #" Test for using a function() |
| 1837 | set thesaurusfunc=function('g:TsrFunc1',\ [10]) |
| 1838 | new |
| 1839 | call setline(1, 'one') |
| 1840 | LET g:TsrFunc1Args = [] |
| 1841 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1842 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1843 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1844 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1845 | #" Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1846 | VAR Fn = function('g:TsrFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1847 | LET &thesaurusfunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1848 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1849 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1850 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1851 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1852 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1853 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1854 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1855 | #" Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1856 | LET Fn = function('g:TsrFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1857 | LET &thesaurusfunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1858 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1859 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1860 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1861 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1862 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1863 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1864 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1865 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1866 | set thesaurusfunc=funcref('g:TsrFunc1',\ [13]) |
| 1867 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1868 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1869 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1870 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1871 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1872 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1873 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1874 | #" Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1875 | LET Fn = funcref('g:TsrFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1876 | LET &thesaurusfunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1877 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1878 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1879 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1880 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1881 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1882 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1883 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1884 | #" Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1885 | LET Fn = funcref('g:TsrFunc1', [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1886 | LET &thesaurusfunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1887 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1888 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1889 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1890 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1891 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1892 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1893 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1894 | #" Test for using a lambda function |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1895 | VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1896 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1897 | exe "set thesaurusfunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1898 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1899 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1900 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1901 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1902 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1903 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1904 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1905 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1906 | LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1907 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1908 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1909 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1910 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1911 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1912 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1913 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1914 | #" Set 'thesaurusfunc' to a string(lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1915 | LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1916 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1917 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1918 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1919 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1920 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1921 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1922 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1923 | #" Set 'thesaurusfunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1924 | VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1925 | LET &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1926 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1927 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1928 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1929 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1930 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1931 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1932 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1933 | #" Set 'thesaurusfunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1934 | LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1935 | LET &thesaurusfunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1936 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1937 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1938 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1939 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1940 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1941 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1942 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1943 | #" Test for using a lambda function with incorrect return value |
| 1944 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 1945 | LET &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1946 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1947 | call setline(1, 'eight') |
| 1948 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1949 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1950 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1951 | #" Test for clearing the 'thesaurusfunc' option |
| 1952 | set thesaurusfunc='' |
| 1953 | set thesaurusfunc& |
| 1954 | call assert_fails("set thesaurusfunc=function('abc')", "E700:") |
| 1955 | call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1956 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1957 | #" set 'thesaurusfunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1958 | set thesaurusfunc=g:TsrFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1959 | call setline(1, 'ten') |
| 1960 | call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') |
| 1961 | call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1962 | LET g:TsrFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1963 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1964 | call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1965 | bw! |
| 1966 | |
| 1967 | #" Use a buffer-local value and a global value |
| 1968 | set thesaurusfunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1969 | setlocal thesaurusfunc=function('g:TsrFunc1',\ [22]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1970 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1971 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1972 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 1973 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1974 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1975 | new |
| 1976 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1977 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1978 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 1979 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1980 | call assert_equal([], g:TsrFunc1Args) |
| 1981 | set thesaurusfunc=function('g:TsrFunc1',\ [23]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1982 | wincmd w |
| 1983 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1984 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1985 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 1986 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1987 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1988 | :%bw! |
| 1989 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1990 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1991 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1992 | " Test for using a script-local function name |
| 1993 | func s:TsrFunc3(findstart, base) |
| 1994 | call add(g:TsrFunc3Args, [a:findstart, a:base]) |
| 1995 | return a:findstart ? 0 : [] |
| 1996 | endfunc |
| 1997 | set tsrfu=s:TsrFunc3 |
| 1998 | new |
| 1999 | call setline(1, 'script1') |
| 2000 | let g:TsrFunc3Args = [] |
| 2001 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2002 | call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args) |
| 2003 | bw! |
| 2004 | |
| 2005 | let &tsrfu = 's:TsrFunc3' |
| 2006 | new |
| 2007 | call setline(1, 'script2') |
| 2008 | let g:TsrFunc3Args = [] |
| 2009 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2010 | call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args) |
| 2011 | bw! |
| 2012 | delfunc s:TsrFunc3 |
| 2013 | |
| 2014 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2015 | let &thesaurusfunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2016 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2017 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2018 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2019 | set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2020 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2021 | let g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2022 | call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2023 | call assert_equal([], g:TsrFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2024 | bw! |
| 2025 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2026 | " set 'thesaurusfunc' to a partial with dict. This used to cause a crash. |
| 2027 | func SetTsrFunc() |
| 2028 | let params = {'thesaurus': function('g:DictTsrFunc')} |
| 2029 | let &thesaurusfunc = params.thesaurus |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2030 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2031 | func g:DictTsrFunc(_) dict |
| 2032 | endfunc |
| 2033 | call SetTsrFunc() |
| 2034 | new |
| 2035 | call SetTsrFunc() |
| 2036 | bw |
| 2037 | call test_garbagecollect_now() |
| 2038 | new |
| 2039 | set thesaurusfunc= |
| 2040 | wincmd w |
| 2041 | %bw! |
| 2042 | delfunc SetTsrFunc |
| 2043 | |
| 2044 | " set buffer-local 'thesaurusfunc' to a partial with dict. This used to |
| 2045 | " cause a crash. |
| 2046 | func SetLocalTsrFunc() |
| 2047 | let params = {'thesaurus': function('g:DictTsrFunc')} |
| 2048 | let &l:thesaurusfunc = params.thesaurus |
| 2049 | endfunc |
| 2050 | call SetLocalTsrFunc() |
| 2051 | call test_garbagecollect_now() |
| 2052 | call SetLocalTsrFunc() |
| 2053 | set thesaurusfunc= |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2054 | bw! |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2055 | delfunc g:DictTsrFunc |
| 2056 | delfunc SetLocalTsrFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2057 | |
| 2058 | " Vim9 tests |
| 2059 | let lines =<< trim END |
| 2060 | vim9script |
| 2061 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2062 | def Vim9tsrFunc(callnr: number, findstart: number, base: string): any |
| 2063 | add(g:Vim9tsrFunc_Args, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2064 | return findstart ? 0 : [] |
| 2065 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2066 | |
| 2067 | # Test for using a def function with thesaurusfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2068 | set thesaurusfunc=function('Vim9tsrFunc',\ [60]) |
| 2069 | new | only |
| 2070 | setline(1, 'one') |
| 2071 | g:Vim9tsrFunc_Args = [] |
| 2072 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2073 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args) |
| 2074 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2075 | |
| 2076 | # Test for using a global function name |
| 2077 | &thesaurusfunc = g:TsrFunc2 |
| 2078 | new | only |
| 2079 | setline(1, 'two') |
| 2080 | g:TsrFunc2Args = [] |
| 2081 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2082 | assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args) |
| 2083 | bw! |
| 2084 | |
| 2085 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2086 | def LocalTsrFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2087 | add(g:LocalTsrFuncArgs, [findstart, base]) |
| 2088 | return findstart ? 0 : [] |
| 2089 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2090 | &thesaurusfunc = LocalTsrFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2091 | new | only |
| 2092 | setline(1, 'three') |
| 2093 | g:LocalTsrFuncArgs = [] |
| 2094 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2095 | assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs) |
| 2096 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2097 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2098 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2099 | |
| 2100 | " cleanup |
| 2101 | set thesaurusfunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2102 | delfunc TsrFunc1 |
| 2103 | delfunc TsrFunc2 |
| 2104 | unlet g:TsrFunc1Args g:TsrFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2105 | %bw! |
| 2106 | endfunc |
| 2107 | |
Christian Brabandt | ac72c21 | 2022-04-07 21:00:53 +0100 | [diff] [blame] | 2108 | func FooBarComplete(findstart, base) |
| 2109 | if a:findstart |
| 2110 | return col('.') - 1 |
| 2111 | else |
| 2112 | return ["Foo", "Bar", "}"] |
| 2113 | endif |
| 2114 | endfunc |
| 2115 | |
| 2116 | func Test_complete_smartindent() |
| 2117 | new |
| 2118 | setlocal smartindent completefunc=FooBarComplete |
| 2119 | |
| 2120 | exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>" |
| 2121 | let result = getline(1,'$') |
| 2122 | call assert_equal(['', '{','}',''], result) |
| 2123 | bw! |
| 2124 | delfunction! FooBarComplete |
| 2125 | endfunc |
| 2126 | |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2127 | func Test_complete_overrun() |
| 2128 | " this was going past the end of the copied text |
| 2129 | new |
| 2130 | sil norm si0s0 |
| 2131 | bwipe! |
| 2132 | endfunc |
| 2133 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2134 | func Test_infercase_very_long_line() |
| 2135 | " this was truncating the line when inferring case |
| 2136 | new |
| 2137 | let longLine = "blah "->repeat(300) |
| 2138 | let verylongLine = "blah "->repeat(400) |
| 2139 | call setline(1, verylongLine) |
| 2140 | call setline(2, longLine) |
| 2141 | set ic infercase |
| 2142 | exe "normal 2Go\<C-X>\<C-L>\<Esc>" |
| 2143 | call assert_equal(longLine, getline(3)) |
| 2144 | |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 2145 | " check that the too long text is NUL terminated |
| 2146 | %del |
| 2147 | norm o |
| 2148 | norm 1987ax |
| 2149 | exec "norm ox\<C-X>\<C-L>" |
| 2150 | call assert_equal(repeat('x', 1987), getline(3)) |
| 2151 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2152 | bwipe! |
| 2153 | set noic noinfercase |
| 2154 | endfunc |
| 2155 | |
Bram Moolenaar | baefde1 | 2022-07-07 19:59:49 +0100 | [diff] [blame] | 2156 | func Test_ins_complete_add() |
| 2157 | " this was reading past the end of allocated memory |
| 2158 | new |
| 2159 | norm o |
| 2160 | norm 7o |
| 2161 | sil! norm o |
| 2162 | |
| 2163 | bwipe! |
| 2164 | endfunc |
| 2165 | |
Bram Moolenaar | a6f9e30 | 2022-07-28 21:51:37 +0100 | [diff] [blame] | 2166 | func Test_ins_complete_end_of_line() |
| 2167 | " this was reading past the end of the line |
| 2168 | new |
| 2169 | norm 8oý |
| 2170 | sil! norm o |
| 2171 | |
| 2172 | bwipe! |
| 2173 | endfunc |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2174 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2175 | " vim: shiftwidth=2 sts=2 expandtab |