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