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 |
glepnir | 53387c5 | 2024-05-27 15:11:01 +0200 | [diff] [blame] | 633 | func 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 |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 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 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 812 | " Test for buffer-local value of 'completeopt' |
| 813 | func Test_completeopt_buffer_local() |
| 814 | set completeopt=menu |
| 815 | new |
| 816 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 817 | call assert_equal('', &l:completeopt) |
| 818 | call assert_equal('menu', &completeopt) |
| 819 | call assert_equal('menu', &g:completeopt) |
| 820 | |
| 821 | setlocal bufhidden=hide |
| 822 | enew |
| 823 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 824 | call assert_equal('', &l:completeopt) |
| 825 | call assert_equal('menu', &completeopt) |
| 826 | call assert_equal('menu', &g:completeopt) |
| 827 | |
| 828 | setlocal completeopt+=fuzzy,noinsert |
| 829 | call assert_equal('menu,fuzzy,noinsert', &l:completeopt) |
| 830 | call assert_equal('menu,fuzzy,noinsert', &completeopt) |
| 831 | call assert_equal('menu', &g:completeopt) |
| 832 | call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix') |
| 833 | call assert_equal('foobaz', getline('.')) |
| 834 | |
| 835 | setlocal completeopt= |
| 836 | call assert_equal('', &l:completeopt) |
| 837 | call assert_equal('menu', &completeopt) |
| 838 | call assert_equal('menu', &g:completeopt) |
| 839 | call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix') |
| 840 | call assert_equal('foofoo', getline('.')) |
| 841 | |
| 842 | setlocal completeopt+=longest |
| 843 | call assert_equal('menu,longest', &l:completeopt) |
| 844 | call assert_equal('menu,longest', &completeopt) |
| 845 | call assert_equal('menu', &g:completeopt) |
| 846 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 847 | call assert_equal('foo', getline('.')) |
| 848 | |
| 849 | setlocal bufhidden=hide |
| 850 | buffer # |
| 851 | call assert_equal('', &l:completeopt) |
| 852 | call assert_equal('menu', &completeopt) |
| 853 | call assert_equal('menu', &g:completeopt) |
| 854 | call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix') |
| 855 | call assert_equal('foofoo', getline('.')) |
| 856 | |
| 857 | setlocal completeopt+=fuzzy,noinsert |
| 858 | call assert_equal('menu,fuzzy,noinsert', &l:completeopt) |
| 859 | call assert_equal('menu,fuzzy,noinsert', &completeopt) |
| 860 | call assert_equal('menu', &g:completeopt) |
| 861 | call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix') |
| 862 | call assert_equal('foobaz', getline('.')) |
| 863 | |
| 864 | buffer # |
| 865 | call assert_equal('menu,longest', &l:completeopt) |
| 866 | call assert_equal('menu,longest', &completeopt) |
| 867 | call assert_equal('menu', &g:completeopt) |
| 868 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 869 | call assert_equal('foo', getline('.')) |
| 870 | |
| 871 | setlocal bufhidden=wipe |
| 872 | buffer! # |
| 873 | bwipe! |
| 874 | call assert_equal('', &l:completeopt) |
| 875 | call assert_equal('menu', &completeopt) |
| 876 | call assert_equal('menu', &g:completeopt) |
| 877 | |
| 878 | set completeopt& |
| 879 | endfunc |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 880 | |
Bram Moolenaar | 224a5f1 | 2020-04-28 20:29:07 +0200 | [diff] [blame] | 881 | " Test for completing words following a completed word in a line |
| 882 | func Test_complete_wrapscan() |
| 883 | " complete words from another buffer |
| 884 | new |
| 885 | call setline(1, ['one two', 'three four']) |
| 886 | new |
| 887 | setlocal complete=w |
| 888 | call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 889 | call assert_equal('two three four', getline(1)) |
| 890 | close! |
| 891 | " complete words from the current buffer |
| 892 | setlocal complete=. |
| 893 | %d |
| 894 | call setline(1, ['one two', '']) |
| 895 | call cursor(2, 1) |
| 896 | call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 897 | call assert_equal('one two one two', getline(2)) |
| 898 | close! |
| 899 | endfunc |
| 900 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 901 | " Test for completing special characters |
| 902 | func Test_complete_special_chars() |
| 903 | new |
| 904 | call setline(1, 'int .*[-\^$ func float') |
| 905 | call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt') |
| 906 | call assert_equal('int .*[-\^$ func float', getline(2)) |
| 907 | close! |
| 908 | endfunc |
| 909 | |
| 910 | " Test for completion when text is wrapped across lines. |
| 911 | func Test_complete_across_line() |
| 912 | new |
| 913 | call setline(1, ['red green blue', 'one two three']) |
| 914 | setlocal textwidth=20 |
| 915 | exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 916 | call assert_equal(['one two three red', 'green blue one'], getline(2, '$')) |
| 917 | close! |
| 918 | endfunc |
| 919 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 920 | " Test for completing words with a '.' at the end of a word. |
| 921 | func Test_complete_joinspaces() |
| 922 | new |
| 923 | call setline(1, ['one two.', 'three. four']) |
| 924 | set joinspaces |
| 925 | exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 926 | call assert_equal("one two. three. four", getline(3)) |
| 927 | set joinspaces& |
| 928 | bw! |
| 929 | endfunc |
| 930 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 931 | " Test for using CTRL-L to add one character when completing matching |
| 932 | func Test_complete_add_onechar() |
| 933 | new |
| 934 | call setline(1, ['wool', 'woodwork']) |
| 935 | call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt') |
| 936 | call assert_equal('woof', getline(3)) |
| 937 | |
| 938 | " use 'ignorecase' and backspace to erase characters from the prefix string |
| 939 | " and then add letters using CTRL-L |
| 940 | %d |
| 941 | set ignorecase backspace=2 |
| 942 | setlocal complete=. |
| 943 | call setline(1, ['workhorse', 'workload']) |
| 944 | normal Go |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 945 | 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] | 946 | call assert_equal('workh', getline(3)) |
| 947 | set ignorecase& backspace& |
| 948 | close! |
| 949 | endfunc |
| 950 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 951 | " Test for using CTRL-X CTRL-L to complete whole lines lines |
| 952 | func Test_complete_wholeline() |
| 953 | new |
| 954 | " complete one-line |
| 955 | call setline(1, ['a1', 'a2']) |
| 956 | exe "normal ggoa\<C-X>\<C-L>" |
| 957 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 958 | " go to the next match (wrapping around the buffer) |
| 959 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>" |
| 960 | call assert_equal(['a1', 'a', 'a2'], getline(1, '$')) |
| 961 | " go to the next match |
| 962 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>" |
| 963 | call assert_equal(['a1', 'a2', 'a2'], getline(1, '$')) |
| 964 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>" |
| 965 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 966 | " repeat the test using CTRL-L |
| 967 | " go to the next match (wrapping around the buffer) |
| 968 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>" |
| 969 | call assert_equal(['a1', 'a2', 'a2'], getline(1, '$')) |
| 970 | " go to the next match |
| 971 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>" |
| 972 | call assert_equal(['a1', 'a', 'a2'], getline(1, '$')) |
| 973 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>" |
| 974 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 975 | %d |
| 976 | " use CTRL-X CTRL-L to add one more line |
| 977 | call setline(1, ['a1', 'b1']) |
| 978 | setlocal complete=. |
| 979 | exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>" |
| 980 | call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$')) |
| 981 | bw! |
| 982 | endfunc |
| 983 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 984 | " Test insert completion with 'cindent' (adjust the indent) |
| 985 | func Test_complete_with_cindent() |
| 986 | new |
| 987 | setlocal cindent |
| 988 | call setline(1, ['if (i == 1)', " j = 2;"]) |
| 989 | exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}" |
| 990 | call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$')) |
| 991 | |
| 992 | %d |
| 993 | call setline(1, ['when while', '{', '']) |
| 994 | setlocal cinkeys+==while |
| 995 | exe "normal Giwh\<C-P> " |
| 996 | call assert_equal("\twhile ", getline('$')) |
| 997 | close! |
| 998 | endfunc |
| 999 | |
| 1000 | " Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions |
| 1001 | func Test_complete_cmdline() |
| 1002 | new |
| 1003 | exe "normal icaddb\<C-X>\<C-V>" |
| 1004 | call assert_equal('caddbuffer', getline(1)) |
| 1005 | exe "normal ocall getqf\<C-X>\<C-V>" |
| 1006 | call assert_equal('call getqflist(', getline(2)) |
| 1007 | exe "normal oabcxyz(\<C-X>\<C-V>" |
| 1008 | call assert_equal('abcxyz(', getline(3)) |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 1009 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 1010 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 1011 | write TestCommand1Test |
| 1012 | write TestCommand2Test |
| 1013 | " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode |
| 1014 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>" |
| 1015 | call assert_equal('TestCommand2Test', getline(4)) |
| 1016 | call delete('TestCommand1Test') |
| 1017 | call delete('TestCommand2Test') |
| 1018 | delcom TestCommand1 |
| 1019 | delcom TestCommand2 |
| 1020 | close! |
| 1021 | endfunc |
| 1022 | |
| 1023 | " Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match |
| 1024 | func Test_complete_stop() |
| 1025 | new |
| 1026 | func Save_mode1() |
| 1027 | let g:mode1 = mode(1) |
| 1028 | return '' |
| 1029 | endfunc |
| 1030 | func Save_mode2() |
| 1031 | let g:mode2 = mode(1) |
| 1032 | return '' |
| 1033 | endfunc |
| 1034 | inoremap <F1> <C-R>=Save_mode1()<CR> |
| 1035 | inoremap <F2> <C-R>=Save_mode2()<CR> |
| 1036 | call setline(1, ['aaa bbb ccc ']) |
| 1037 | exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1038 | call assert_equal('ic', g:mode1) |
| 1039 | call assert_equal('i', g:mode2) |
| 1040 | call assert_equal('aaa bbb ccc ', getline(1)) |
| 1041 | exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1042 | call assert_equal('ic', g:mode1) |
| 1043 | call assert_equal('i', g:mode2) |
| 1044 | call assert_equal('aaa bbb ccc aaa', getline(1)) |
| 1045 | set completeopt+=noselect |
| 1046 | exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1047 | call assert_equal('ic', g:mode1) |
| 1048 | call assert_equal('i', g:mode2) |
| 1049 | call assert_equal('aaa bbb ccc aaa bb', getline(1)) |
| 1050 | set completeopt& |
| 1051 | exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1052 | call assert_equal('ic', g:mode1) |
| 1053 | call assert_equal('i', g:mode2) |
| 1054 | call assert_equal('aaa bbb ccc aaa bb d', getline(1)) |
| 1055 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 1056 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 1057 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1058 | call assert_equal('ic', g:mode1) |
| 1059 | call assert_equal('i', g:mode2) |
| 1060 | call assert_equal('TestCommand2', getline(2)) |
| 1061 | delcom TestCommand1 |
| 1062 | delcom TestCommand2 |
| 1063 | unlet g:mode1 |
| 1064 | unlet g:mode2 |
| 1065 | iunmap <F1> |
| 1066 | iunmap <F2> |
| 1067 | delfunc Save_mode1 |
| 1068 | delfunc Save_mode2 |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 1069 | close! |
| 1070 | endfunc |
| 1071 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 1072 | " Test for typing CTRL-R in insert completion mode to insert a register |
| 1073 | " content. |
| 1074 | func Test_complete_reginsert() |
| 1075 | new |
| 1076 | call setline(1, ['a1', 'a12', 'a123', 'a1234']) |
| 1077 | |
| 1078 | " if a valid CTRL-X mode key is returned from <C-R>=, then it should be |
| 1079 | " processed. Otherwise, CTRL-X mode should be stopped and the key should be |
| 1080 | " inserted. |
| 1081 | exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>" |
| 1082 | call assert_equal('a123', getline(5)) |
| 1083 | let @r = "\<C-P>\<C-P>" |
| 1084 | exe "normal GCa\<C-P>\<C-R>r" |
| 1085 | call assert_equal('a12', getline(5)) |
| 1086 | exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>" |
| 1087 | call assert_equal('a1234x', getline(5)) |
| 1088 | bw! |
| 1089 | endfunc |
| 1090 | |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 1091 | func Test_issue_7021() |
| 1092 | CheckMSWindows |
| 1093 | |
| 1094 | let orig_shellslash = &shellslash |
| 1095 | set noshellslash |
| 1096 | |
| 1097 | set completeslash=slash |
| 1098 | call assert_false(expand('~') =~ '/') |
| 1099 | |
| 1100 | let &shellslash = orig_shellslash |
| 1101 | set completeslash= |
| 1102 | endfunc |
| 1103 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1104 | " Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings |
| 1105 | func Test_complete_longest_match() |
| 1106 | for e in ['latin1', 'utf-8'] |
| 1107 | exe 'set encoding=' .. e |
| 1108 | new |
| 1109 | set complete=. |
| 1110 | set completeopt=menu,longest |
| 1111 | call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1']) |
| 1112 | exe "normal Gopfx\<C-P>" |
| 1113 | call assert_equal('pfx_', getline(5)) |
| 1114 | bw! |
| 1115 | endfor |
| 1116 | |
| 1117 | " Test for completing additional words with longest match set |
| 1118 | new |
| 1119 | call setline(1, ['abc1', 'abd2']) |
| 1120 | exe "normal Goab\<C-P>\<C-X>\<C-P>" |
| 1121 | call assert_equal('ab', getline(3)) |
| 1122 | bw! |
| 1123 | set complete& completeopt& |
| 1124 | endfunc |
| 1125 | |
| 1126 | " Test for removing the first displayed completion match and selecting the |
| 1127 | " match just before that. |
| 1128 | func Test_complete_erase_firstmatch() |
| 1129 | new |
| 1130 | call setline(1, ['a12', 'a34', 'a56']) |
| 1131 | set complete=. |
| 1132 | exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>" |
| 1133 | call assert_equal('a34', getline('$')) |
| 1134 | set complete& |
| 1135 | bw! |
| 1136 | endfunc |
| 1137 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1138 | " Test for completing words from unloaded buffers |
| 1139 | func Test_complete_from_unloadedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1140 | call writefile(['abc'], "Xfile1", 'D') |
| 1141 | call writefile(['def'], "Xfile2", 'D') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1142 | edit Xfile1 |
| 1143 | edit Xfile2 |
| 1144 | new | close |
| 1145 | enew |
| 1146 | bunload Xfile1 Xfile2 |
| 1147 | set complete=u |
| 1148 | " complete from an unloaded buffer |
| 1149 | exe "normal! ia\<C-P>" |
| 1150 | call assert_equal('abc', getline(1)) |
| 1151 | exe "normal! od\<C-P>" |
| 1152 | call assert_equal('def', getline(2)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1153 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1154 | set complete& |
| 1155 | %bw! |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1156 | endfunc |
| 1157 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1158 | " Test for completing whole lines from unloaded buffers |
| 1159 | func Test_complete_wholeline_unloadedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1160 | call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D') |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1161 | edit Xfile1 |
| 1162 | enew |
| 1163 | set complete=u |
| 1164 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1165 | call assert_equal('a line2', getline(1)) |
| 1166 | %d |
| 1167 | " completing from an unlisted buffer should fail |
| 1168 | bdel Xfile1 |
| 1169 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1170 | call assert_equal('a', getline(1)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1171 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1172 | set complete& |
| 1173 | %bw! |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1174 | endfunc |
| 1175 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1176 | " Test for completing words from unlisted buffers |
| 1177 | func Test_complete_from_unlistedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1178 | call writefile(['abc'], "Xfile1", 'D') |
| 1179 | call writefile(['def'], "Xfile2", 'D') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1180 | edit Xfile1 |
| 1181 | edit Xfile2 |
| 1182 | new | close |
| 1183 | bdel Xfile1 Xfile2 |
| 1184 | set complete=U |
| 1185 | " complete from an unlisted buffer |
| 1186 | exe "normal! ia\<C-P>" |
| 1187 | call assert_equal('abc', getline(1)) |
| 1188 | exe "normal! od\<C-P>" |
| 1189 | call assert_equal('def', getline(2)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1190 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1191 | set complete& |
| 1192 | %bw! |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1193 | endfunc |
| 1194 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1195 | " Test for completing whole lines from unlisted buffers |
| 1196 | func Test_complete_wholeline_unlistedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1197 | call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D') |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1198 | edit Xfile1 |
| 1199 | enew |
| 1200 | set complete=U |
zeertzjq | c029c13 | 2024-03-28 11:37:26 +0100 | [diff] [blame] | 1201 | " completing from an unloaded buffer should fail |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1202 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1203 | call assert_equal('a', getline(1)) |
| 1204 | %d |
| 1205 | bdel Xfile1 |
| 1206 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1207 | call assert_equal('a line2', getline(1)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1208 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1209 | set complete& |
| 1210 | %bw! |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1211 | endfunc |
| 1212 | |
| 1213 | " Test for adding a multibyte character using CTRL-L in completion mode |
| 1214 | func Test_complete_mbyte_char_add() |
| 1215 | new |
| 1216 | set complete=. |
| 1217 | call setline(1, 'abÄ—') |
| 1218 | exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>" |
| 1219 | call assert_equal('abÄ—', getline(2)) |
| 1220 | " Test for a leader with multibyte character |
| 1221 | %d |
| 1222 | call setline(1, 'abÄ—Ä•') |
| 1223 | exe "normal! oabÄ—\<C-P>" |
| 1224 | call assert_equal('abÄ—Ä•', getline(2)) |
| 1225 | bw! |
| 1226 | endfunc |
| 1227 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1228 | " Test for using <C-X><C-P> for local expansion even if 'complete' is set to |
| 1229 | " not to complete matches from the local buffer. Also test using multiple |
| 1230 | " <C-X> to cancel the current completion mode. |
| 1231 | func Test_complete_local_expansion() |
| 1232 | new |
| 1233 | set complete=t |
| 1234 | call setline(1, ['abc', 'def']) |
| 1235 | exe "normal! Go\<C-X>\<C-P>" |
| 1236 | call assert_equal("def", getline(3)) |
| 1237 | exe "normal! Go\<C-P>" |
| 1238 | call assert_equal("", getline(4)) |
| 1239 | exe "normal! Go\<C-X>\<C-N>" |
| 1240 | call assert_equal("abc", getline(5)) |
| 1241 | exe "normal! Go\<C-N>" |
| 1242 | call assert_equal("", getline(6)) |
| 1243 | |
| 1244 | " use multiple <C-X> to cancel the previous completion mode |
| 1245 | exe "normal! Go\<C-P>\<C-X>\<C-P>" |
| 1246 | call assert_equal("", getline(7)) |
| 1247 | exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>" |
| 1248 | call assert_equal("", getline(8)) |
| 1249 | exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>" |
| 1250 | call assert_equal("abc", getline(9)) |
| 1251 | |
| 1252 | " interrupt the current completion mode |
| 1253 | set completeopt=menu,noinsert |
| 1254 | exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>" |
| 1255 | call assert_equal("abc", getline(10)) |
| 1256 | |
| 1257 | " when only one <C-X> is used to interrupt, do normal expansion |
| 1258 | exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>" |
| 1259 | call assert_equal("", getline(11)) |
| 1260 | set completeopt& |
| 1261 | |
| 1262 | " using two <C-X> in non-completion mode and restarting the same mode |
| 1263 | exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>" |
| 1264 | call assert_equal("def", getline(12)) |
| 1265 | |
| 1266 | " test for adding a match from the original empty text |
| 1267 | %d |
| 1268 | call setline(1, 'abc def g') |
| 1269 | exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>" |
| 1270 | call assert_equal('def', getline(2)) |
| 1271 | exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>" |
| 1272 | call assert_equal('abc', getline(2)) |
| 1273 | |
| 1274 | bw! |
| 1275 | endfunc |
| 1276 | |
| 1277 | " Test for undoing changes after a insert-mode completion |
| 1278 | func Test_complete_undo() |
| 1279 | new |
| 1280 | set complete=. |
| 1281 | " undo with 'ignorecase' |
| 1282 | call setline(1, ['ABOVE', 'BELOW']) |
| 1283 | set ignorecase |
| 1284 | exe "normal! Goab\<C-G>u\<C-P>" |
| 1285 | call assert_equal("ABOVE", getline(3)) |
| 1286 | undo |
| 1287 | call assert_equal("ab", getline(3)) |
| 1288 | set ignorecase& |
| 1289 | %d |
| 1290 | " undo with longest match |
| 1291 | set completeopt=menu,longest |
| 1292 | call setline(1, ['above', 'about']) |
| 1293 | exe "normal! Goa\<C-G>u\<C-P>" |
| 1294 | call assert_equal("abo", getline(3)) |
| 1295 | undo |
| 1296 | call assert_equal("a", getline(3)) |
| 1297 | set completeopt& |
| 1298 | %d |
| 1299 | " undo for line completion |
| 1300 | call setline(1, ['above that change', 'below that change']) |
| 1301 | exe "normal! Goabove\<C-G>u\<C-X>\<C-L>" |
| 1302 | call assert_equal("above that change", getline(3)) |
| 1303 | undo |
| 1304 | call assert_equal("above", getline(3)) |
| 1305 | |
| 1306 | bw! |
| 1307 | endfunc |
| 1308 | |
| 1309 | " Test for completing a very long word |
| 1310 | func Test_complete_long_word() |
| 1311 | set complete& |
| 1312 | new |
| 1313 | call setline(1, repeat('x', 950) .. ' one two three') |
| 1314 | exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1315 | call assert_equal(repeat('x', 950) .. ' one two three', getline(2)) |
| 1316 | %d |
| 1317 | " should fail when more than 950 characters are in a word |
| 1318 | call setline(1, repeat('x', 951) .. ' one two three') |
| 1319 | exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1320 | call assert_equal(repeat('x', 951), getline(2)) |
| 1321 | |
| 1322 | " Test for adding a very long word to an existing completion |
| 1323 | %d |
| 1324 | call setline(1, ['abc', repeat('x', 1016) .. '012345']) |
| 1325 | exe "normal! Goab\<C-P>\<C-X>\<C-P>" |
| 1326 | call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3)) |
| 1327 | bw! |
| 1328 | endfunc |
| 1329 | |
| 1330 | " Test for some fields in the complete items used by complete() |
| 1331 | func Test_complete_items() |
| 1332 | func CompleteItems(idx) |
| 1333 | let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}], |
| 1334 | \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}], |
| 1335 | \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}], |
| 1336 | \ [#{user_data: 'u9'}], |
| 1337 | \ [#{word: "", user_data: 'u10'}], |
| 1338 | \ [#{word: "", empty: 1, user_data: 'u11'}]] |
| 1339 | call complete(col('.'), items[a:idx]) |
| 1340 | return '' |
| 1341 | endfunc |
| 1342 | new |
| 1343 | exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>" |
| 1344 | call assert_equal('u2', v:completed_item.user_data) |
| 1345 | call assert_equal('one', getline(1)) |
| 1346 | exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>" |
| 1347 | call assert_equal('u3', v:completed_item.user_data) |
| 1348 | call assert_equal('one', getline(2)) |
| 1349 | exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>" |
| 1350 | call assert_equal('', getline(3)) |
| 1351 | set completeopt=menu,noinsert |
| 1352 | exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>" |
| 1353 | call assert_equal('oNE', getline(4)) |
| 1354 | call assert_equal('u8', v:completed_item.user_data) |
| 1355 | set completeopt& |
| 1356 | exe "normal! o\<C-R>=CompleteItems(3)\<CR>" |
| 1357 | call assert_equal('', getline(5)) |
| 1358 | exe "normal! o\<C-R>=CompleteItems(4)\<CR>" |
| 1359 | call assert_equal('', getline(6)) |
| 1360 | exe "normal! o\<C-R>=CompleteItems(5)\<CR>" |
| 1361 | call assert_equal('', getline(7)) |
| 1362 | call assert_equal('u11', v:completed_item.user_data) |
| 1363 | " pass invalid argument to complete() |
| 1364 | let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>" |
| 1365 | call assert_fails('exe cmd', 'E730:') |
| 1366 | bw! |
| 1367 | delfunc CompleteItems |
| 1368 | endfunc |
| 1369 | |
| 1370 | " Test for the "refresh" item in the dict returned by an insert completion |
| 1371 | " function |
| 1372 | func Test_complete_item_refresh_always() |
| 1373 | let g:CallCount = 0 |
| 1374 | func! Tcomplete(findstart, base) |
| 1375 | if a:findstart |
| 1376 | " locate the start of the word |
| 1377 | let line = getline('.') |
| 1378 | let start = col('.') - 1 |
| 1379 | while start > 0 && line[start - 1] =~ '\a' |
| 1380 | let start -= 1 |
| 1381 | endwhile |
| 1382 | return start |
| 1383 | else |
| 1384 | let g:CallCount += 1 |
| 1385 | let res = ["update1", "update12", "update123"] |
| 1386 | return #{words: res, refresh: 'always'} |
| 1387 | endif |
| 1388 | endfunc |
| 1389 | new |
| 1390 | set completeopt=menu,longest |
| 1391 | set completefunc=Tcomplete |
| 1392 | exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>" |
| 1393 | call assert_equal('up', getline(1)) |
| 1394 | call assert_equal(2, g:CallCount) |
| 1395 | set completeopt& |
| 1396 | set completefunc& |
| 1397 | bw! |
| 1398 | delfunc Tcomplete |
| 1399 | endfunc |
| 1400 | |
| 1401 | " Test for completing from a thesaurus file without read permission |
| 1402 | func Test_complete_unreadable_thesaurus_file() |
| 1403 | CheckUnix |
| 1404 | CheckNotRoot |
| 1405 | |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1406 | call writefile(['about', 'above'], 'Xunrfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1407 | call setfperm('Xunrfile', '---r--r--') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1408 | new |
| 1409 | set complete=sXfile |
| 1410 | exe "normal! ia\<C-P>" |
| 1411 | call assert_equal('a', getline(1)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1412 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1413 | bw! |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1414 | set complete& |
| 1415 | endfunc |
| 1416 | |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1417 | " Test to ensure 'Scanning...' messages are not recorded in messages history |
| 1418 | func Test_z1_complete_no_history() |
| 1419 | new |
| 1420 | messages clear |
| 1421 | let currmess = execute('messages') |
| 1422 | setlocal dictionary=README.txt |
| 1423 | exe "normal owh\<C-X>\<C-K>" |
| 1424 | exe "normal owh\<C-N>" |
| 1425 | call assert_equal(currmess, execute('messages')) |
Bram Moolenaar | d979d64 | 2022-03-04 14:51:06 +0000 | [diff] [blame] | 1426 | bwipe! |
| 1427 | endfunc |
| 1428 | |
| 1429 | " A mapping is not used for the key after CTRL-X. |
| 1430 | func Test_no_mapping_for_ctrl_x_key() |
| 1431 | new |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 1432 | inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR> |
Bram Moolenaar | d979d64 | 2022-03-04 14:51:06 +0000 | [diff] [blame] | 1433 | setlocal dictionary=README.txt |
| 1434 | call feedkeys("aexam\<C-X>\<C-K> ", 'xt') |
| 1435 | call assert_equal('example ', getline(1)) |
| 1436 | call assert_false(exists('was_mapped')) |
| 1437 | bwipe! |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1438 | endfunc |
| 1439 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1440 | " Test for different ways of setting the 'completefunc' option |
| 1441 | func Test_completefunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1442 | func CompleteFunc1(callnr, findstart, base) |
| 1443 | call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base]) |
| 1444 | return a:findstart ? 0 : [] |
| 1445 | endfunc |
| 1446 | func CompleteFunc2(findstart, base) |
| 1447 | call add(g:CompleteFunc2Args, [a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1448 | return a:findstart ? 0 : [] |
| 1449 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1450 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1451 | let lines =<< trim END |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1452 | #" Test for using a global function name |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1453 | LET &completefunc = 'g:CompleteFunc2' |
| 1454 | new |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1455 | call setline(1, 'global') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1456 | LET g:CompleteFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1457 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1458 | call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1459 | bw! |
| 1460 | |
| 1461 | #" Test for using a function() |
| 1462 | set completefunc=function('g:CompleteFunc1',\ [10]) |
| 1463 | new |
| 1464 | call setline(1, 'one') |
| 1465 | LET g:CompleteFunc1Args = [] |
| 1466 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1467 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1468 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1469 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1470 | #" Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1471 | VAR Fn = function('g:CompleteFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1472 | LET &completefunc = Fn |
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, 'two') |
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([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1478 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1479 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1480 | #" Using string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1481 | LET Fn = function('g:CompleteFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1482 | LET &completefunc = string(Fn) |
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, 'two') |
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([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1488 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1489 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1490 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1491 | set completefunc=funcref('g:CompleteFunc1',\ [13]) |
| 1492 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1493 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1494 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1495 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1496 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1497 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1498 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1499 | #" Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1500 | LET Fn = funcref('g:CompleteFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1501 | LET &completefunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1502 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1503 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1504 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1505 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1506 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1507 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1508 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1509 | #" Using a string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1510 | LET Fn = funcref('g:CompleteFunc1', [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1511 | LET &completefunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1512 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1513 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1514 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1515 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1516 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1517 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1518 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1519 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1520 | VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1521 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1522 | exe "set completefunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1523 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1524 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1525 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1526 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1527 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1528 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1529 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1530 | #" Set 'completefunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1531 | LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1532 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1533 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1534 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1535 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1536 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1537 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1538 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1539 | #" Set 'completefunc' to string(lambda_expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1540 | LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1541 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1542 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1543 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1544 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1545 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1546 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1547 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1548 | #" Set 'completefunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1549 | VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1550 | LET &completefunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1551 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1552 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1553 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1554 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1555 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1556 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1557 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1558 | #" Set 'completefunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1559 | LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1560 | LET &completefunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1561 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1562 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1563 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1564 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1565 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1566 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1567 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1568 | #" Test for using a lambda function with incorrect return value |
| 1569 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 1570 | LET &completefunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1571 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1572 | call setline(1, 'eight') |
| 1573 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1574 | bw! |
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 | #" Test for clearing the 'completefunc' option |
| 1577 | set completefunc='' |
| 1578 | set completefunc& |
| 1579 | call assert_fails("set completefunc=function('abc')", "E700:") |
| 1580 | call assert_fails("set completefunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1581 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1582 | #" set 'completefunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1583 | set completefunc=g:CompleteFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1584 | call setline(1, 'five') |
| 1585 | call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') |
| 1586 | call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1587 | LET g:CompleteFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1588 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1589 | call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1590 | bw! |
| 1591 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1592 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1593 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1594 | " Test for using a script-local function name |
| 1595 | func s:CompleteFunc3(findstart, base) |
| 1596 | call add(g:CompleteFunc3Args, [a:findstart, a:base]) |
| 1597 | return a:findstart ? 0 : [] |
| 1598 | endfunc |
| 1599 | set completefunc=s:CompleteFunc3 |
| 1600 | new |
| 1601 | call setline(1, 'script1') |
| 1602 | let g:CompleteFunc3Args = [] |
| 1603 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1604 | call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args) |
| 1605 | bw! |
| 1606 | |
| 1607 | let &completefunc = 's:CompleteFunc3' |
| 1608 | new |
| 1609 | call setline(1, 'script2') |
| 1610 | let g:CompleteFunc3Args = [] |
| 1611 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1612 | call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args) |
| 1613 | bw! |
| 1614 | delfunc s:CompleteFunc3 |
| 1615 | |
Bram Moolenaar | 1fca5f3 | 2022-02-18 17:50:47 +0000 | [diff] [blame] | 1616 | " In Vim9 script s: can be omitted |
| 1617 | let lines =<< trim END |
| 1618 | vim9script |
| 1619 | var CompleteFunc4Args = [] |
| 1620 | def CompleteFunc4(findstart: bool, base: string): any |
| 1621 | add(CompleteFunc4Args, [findstart, base]) |
| 1622 | return findstart ? 0 : [] |
| 1623 | enddef |
| 1624 | set completefunc=CompleteFunc4 |
| 1625 | new |
| 1626 | setline(1, 'script1') |
| 1627 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1628 | assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args) |
| 1629 | bw! |
| 1630 | END |
| 1631 | call v9.CheckScriptSuccess(lines) |
| 1632 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1633 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1634 | let &completefunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1635 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1636 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1637 | " Using Vim9 lambda expression in legacy context should fail |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1638 | set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1639 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1640 | let g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1641 | 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] | 1642 | call assert_equal([], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1643 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1644 | " set 'completefunc' to a partial with dict. This used to cause a crash. |
| 1645 | func SetCompleteFunc() |
| 1646 | let params = {'complete': function('g:DictCompleteFunc')} |
| 1647 | let &completefunc = params.complete |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1648 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1649 | func g:DictCompleteFunc(_) dict |
| 1650 | endfunc |
| 1651 | call SetCompleteFunc() |
| 1652 | new |
| 1653 | call SetCompleteFunc() |
| 1654 | bw |
| 1655 | call test_garbagecollect_now() |
| 1656 | new |
| 1657 | set completefunc= |
| 1658 | wincmd w |
| 1659 | set completefunc= |
| 1660 | %bw! |
| 1661 | delfunc g:DictCompleteFunc |
| 1662 | delfunc SetCompleteFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1663 | |
| 1664 | " Vim9 tests |
| 1665 | let lines =<< trim END |
| 1666 | vim9script |
| 1667 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1668 | def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any |
| 1669 | add(g:Vim9completeFuncArgs, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1670 | return findstart ? 0 : [] |
| 1671 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1672 | |
| 1673 | # Test for using a def function with completefunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1674 | set completefunc=function('Vim9CompleteFunc',\ [60]) |
| 1675 | new | only |
| 1676 | setline(1, 'one') |
| 1677 | g:Vim9completeFuncArgs = [] |
| 1678 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1679 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) |
| 1680 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1681 | |
| 1682 | # Test for using a global function name |
| 1683 | &completefunc = g:CompleteFunc2 |
| 1684 | new | only |
| 1685 | setline(1, 'two') |
| 1686 | g:CompleteFunc2Args = [] |
| 1687 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1688 | assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args) |
| 1689 | bw! |
| 1690 | |
| 1691 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1692 | def LocalCompleteFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1693 | add(g:LocalCompleteFuncArgs, [findstart, base]) |
| 1694 | return findstart ? 0 : [] |
| 1695 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1696 | &completefunc = LocalCompleteFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1697 | new | only |
| 1698 | setline(1, 'three') |
| 1699 | g:LocalCompleteFuncArgs = [] |
| 1700 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1701 | assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs) |
| 1702 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1703 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1704 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1705 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1706 | " cleanup |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1707 | set completefunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1708 | delfunc CompleteFunc1 |
| 1709 | delfunc CompleteFunc2 |
| 1710 | unlet g:CompleteFunc1Args g:CompleteFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1711 | %bw! |
| 1712 | endfunc |
| 1713 | |
| 1714 | " Test for different ways of setting the 'omnifunc' option |
| 1715 | func Test_omnifunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1716 | func OmniFunc1(callnr, findstart, base) |
| 1717 | call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base]) |
| 1718 | return a:findstart ? 0 : [] |
| 1719 | endfunc |
| 1720 | func OmniFunc2(findstart, base) |
| 1721 | call add(g:OmniFunc2Args, [a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1722 | return a:findstart ? 0 : [] |
| 1723 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1724 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1725 | let lines =<< trim END |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1726 | #" Test for using a function name |
| 1727 | LET &omnifunc = 'g:OmniFunc2' |
| 1728 | new |
| 1729 | call setline(1, 'zero') |
| 1730 | LET g:OmniFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1731 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1732 | call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args) |
| 1733 | bw! |
| 1734 | |
| 1735 | #" Test for using a function() |
| 1736 | set omnifunc=function('g:OmniFunc1',\ [10]) |
| 1737 | new |
| 1738 | call setline(1, 'one') |
| 1739 | LET g:OmniFunc1Args = [] |
| 1740 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1741 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1742 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1743 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1744 | #" Using a funcref variable to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1745 | VAR Fn = function('g:OmniFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1746 | LET &omnifunc = Fn |
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, 'two') |
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([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1752 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1753 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1754 | #" Using a string(funcref_variable) to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1755 | LET Fn = function('g:OmniFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1756 | LET &omnifunc = string(Fn) |
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, 'two') |
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([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1762 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1763 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1764 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1765 | set omnifunc=funcref('g:OmniFunc1',\ [13]) |
| 1766 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1767 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1768 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1769 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1770 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1771 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1772 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1773 | #" Use let to set 'omnifunc' to a funcref |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1774 | LET Fn = funcref('g:OmniFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1775 | LET &omnifunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1776 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1777 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1778 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1779 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1780 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1781 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1782 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1783 | #" Using a string(funcref) to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1784 | LET Fn = funcref("g:OmniFunc1", [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1785 | LET &omnifunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1786 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1787 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1788 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1789 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1790 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1791 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1792 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1793 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1794 | VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1795 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1796 | exe "set omnifunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1797 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1798 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1799 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1800 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1801 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1802 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1803 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1804 | #" Set 'omnifunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1805 | LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1806 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1807 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1808 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1809 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1810 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1811 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1812 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1813 | #" Set 'omnifunc' to a string(lambda_expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1814 | LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1815 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1816 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1817 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1818 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1819 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1820 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1821 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1822 | #" Set 'omnifunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1823 | VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1824 | LET &omnifunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1825 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1826 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1827 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1828 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1829 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1830 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1831 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1832 | #" Set 'omnifunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1833 | LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1834 | LET &omnifunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1835 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1836 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1837 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1838 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1839 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1840 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1841 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1842 | #" Test for using a lambda function with incorrect return value |
| 1843 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 1844 | LET &omnifunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1845 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1846 | call setline(1, 'eight') |
| 1847 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1848 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1849 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1850 | #" Test for clearing the 'omnifunc' option |
| 1851 | set omnifunc='' |
| 1852 | set omnifunc& |
| 1853 | call assert_fails("set omnifunc=function('abc')", "E700:") |
| 1854 | call assert_fails("set omnifunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1855 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1856 | #" set 'omnifunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1857 | set omnifunc=g:OmniFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1858 | call setline(1, 'nine') |
| 1859 | call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') |
| 1860 | call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1861 | LET g:OmniFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1862 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1863 | call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1864 | bw! |
| 1865 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1866 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1867 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1868 | " Test for using a script-local function name |
| 1869 | func s:OmniFunc3(findstart, base) |
| 1870 | call add(g:OmniFunc3Args, [a:findstart, a:base]) |
| 1871 | return a:findstart ? 0 : [] |
| 1872 | endfunc |
| 1873 | set omnifunc=s:OmniFunc3 |
| 1874 | new |
| 1875 | call setline(1, 'script1') |
| 1876 | let g:OmniFunc3Args = [] |
| 1877 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1878 | call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args) |
| 1879 | bw! |
| 1880 | |
| 1881 | let &omnifunc = 's:OmniFunc3' |
| 1882 | new |
| 1883 | call setline(1, 'script2') |
| 1884 | let g:OmniFunc3Args = [] |
| 1885 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1886 | call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args) |
| 1887 | bw! |
| 1888 | delfunc s:OmniFunc3 |
| 1889 | |
| 1890 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1891 | let &omnifunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1892 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1893 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1894 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1895 | set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1896 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1897 | let g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1898 | 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] | 1899 | call assert_equal([], g:OmniFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1900 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1901 | " set 'omnifunc' to a partial with dict. This used to cause a crash. |
| 1902 | func SetOmniFunc() |
| 1903 | let params = {'omni': function('g:DictOmniFunc')} |
| 1904 | let &omnifunc = params.omni |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1905 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1906 | func g:DictOmniFunc(_) dict |
| 1907 | endfunc |
| 1908 | call SetOmniFunc() |
| 1909 | new |
| 1910 | call SetOmniFunc() |
| 1911 | bw |
| 1912 | call test_garbagecollect_now() |
| 1913 | new |
| 1914 | set omnifunc= |
| 1915 | wincmd w |
| 1916 | set omnifunc= |
| 1917 | %bw! |
| 1918 | delfunc g:DictOmniFunc |
| 1919 | delfunc SetOmniFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1920 | |
| 1921 | " Vim9 tests |
| 1922 | let lines =<< trim END |
| 1923 | vim9script |
| 1924 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1925 | def Vim9omniFunc(callnr: number, findstart: number, base: string): any |
| 1926 | add(g:Vim9omniFunc_Args, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1927 | return findstart ? 0 : [] |
| 1928 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1929 | |
| 1930 | # Test for using a def function with omnifunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1931 | set omnifunc=function('Vim9omniFunc',\ [60]) |
| 1932 | new | only |
| 1933 | setline(1, 'one') |
| 1934 | g:Vim9omniFunc_Args = [] |
| 1935 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1936 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) |
| 1937 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1938 | |
| 1939 | # Test for using a global function name |
| 1940 | &omnifunc = g:OmniFunc2 |
| 1941 | new | only |
| 1942 | setline(1, 'two') |
| 1943 | g:OmniFunc2Args = [] |
| 1944 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1945 | assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args) |
| 1946 | bw! |
| 1947 | |
| 1948 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1949 | def LocalOmniFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1950 | add(g:LocalOmniFuncArgs, [findstart, base]) |
| 1951 | return findstart ? 0 : [] |
| 1952 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1953 | &omnifunc = LocalOmniFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1954 | new | only |
| 1955 | setline(1, 'three') |
| 1956 | g:LocalOmniFuncArgs = [] |
| 1957 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1958 | assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs) |
| 1959 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1960 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1961 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1962 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1963 | " cleanup |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1964 | set omnifunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1965 | delfunc OmniFunc1 |
| 1966 | delfunc OmniFunc2 |
| 1967 | unlet g:OmniFunc1Args g:OmniFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1968 | %bw! |
| 1969 | endfunc |
| 1970 | |
| 1971 | " Test for different ways of setting the 'thesaurusfunc' option |
| 1972 | func Test_thesaurusfunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1973 | func TsrFunc1(callnr, findstart, base) |
| 1974 | call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1975 | return a:findstart ? 0 : [] |
| 1976 | endfunc |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1977 | func TsrFunc2(findstart, base) |
| 1978 | call add(g:TsrFunc2Args, [a:findstart, a:base]) |
| 1979 | return a:findstart ? 0 : ['sunday'] |
| 1980 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1981 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1982 | let lines =<< trim END |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1983 | #" Test for using a function name |
| 1984 | LET &thesaurusfunc = 'g:TsrFunc2' |
| 1985 | new |
| 1986 | call setline(1, 'zero') |
| 1987 | LET g:TsrFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1988 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1989 | call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args) |
| 1990 | bw! |
| 1991 | |
| 1992 | #" Test for using a function() |
| 1993 | set thesaurusfunc=function('g:TsrFunc1',\ [10]) |
| 1994 | new |
| 1995 | call setline(1, 'one') |
| 1996 | LET g:TsrFunc1Args = [] |
| 1997 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 1998 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1999 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2000 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2001 | #" Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2002 | VAR Fn = function('g:TsrFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2003 | LET &thesaurusfunc = Fn |
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, 'two') |
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([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2009 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2010 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2011 | #" Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2012 | LET Fn = function('g:TsrFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2013 | LET &thesaurusfunc = string(Fn) |
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, 'two') |
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([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2019 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2020 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2021 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2022 | set thesaurusfunc=funcref('g:TsrFunc1',\ [13]) |
| 2023 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2024 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2025 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2026 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2027 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2028 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2029 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2030 | #" Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2031 | LET Fn = funcref('g:TsrFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2032 | LET &thesaurusfunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2033 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2034 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2035 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2036 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2037 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2038 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2039 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2040 | #" Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2041 | LET Fn = funcref('g:TsrFunc1', [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2042 | LET &thesaurusfunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2043 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2044 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2045 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2046 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2047 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2048 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2049 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2050 | #" Test for using a lambda function |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2051 | VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2052 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 2053 | exe "set thesaurusfunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2054 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2055 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2056 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2057 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2058 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2059 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2060 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2061 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2062 | LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2063 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2064 | call setline(1, 'six') |
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') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2067 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2068 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2069 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2070 | #" Set 'thesaurusfunc' to a string(lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2071 | LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2072 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2073 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2074 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2075 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2076 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2077 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2078 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2079 | #" Set 'thesaurusfunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2080 | VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2081 | LET &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2082 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2083 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2084 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2085 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2086 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2087 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2088 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2089 | #" Set 'thesaurusfunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2090 | LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2091 | LET &thesaurusfunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2092 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2093 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2094 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2095 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2096 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2097 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2098 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2099 | #" Test for using a lambda function with incorrect return value |
| 2100 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 2101 | LET &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2102 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2103 | call setline(1, 'eight') |
| 2104 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2105 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2106 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2107 | #" Test for clearing the 'thesaurusfunc' option |
| 2108 | set thesaurusfunc='' |
| 2109 | set thesaurusfunc& |
| 2110 | call assert_fails("set thesaurusfunc=function('abc')", "E700:") |
| 2111 | call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2112 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2113 | #" set 'thesaurusfunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 2114 | set thesaurusfunc=g:TsrFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2115 | call setline(1, 'ten') |
| 2116 | call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') |
| 2117 | call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2118 | LET g:TsrFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2119 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2120 | call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2121 | bw! |
| 2122 | |
| 2123 | #" Use a buffer-local value and a global value |
| 2124 | set thesaurusfunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2125 | setlocal thesaurusfunc=function('g:TsrFunc1',\ [22]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2126 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2127 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2128 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 2129 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2130 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2131 | new |
| 2132 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2133 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2134 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 2135 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2136 | call assert_equal([], g:TsrFunc1Args) |
| 2137 | set thesaurusfunc=function('g:TsrFunc1',\ [23]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2138 | wincmd w |
| 2139 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2140 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2141 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 2142 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2143 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2144 | :%bw! |
| 2145 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2146 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2147 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2148 | " Test for using a script-local function name |
| 2149 | func s:TsrFunc3(findstart, base) |
| 2150 | call add(g:TsrFunc3Args, [a:findstart, a:base]) |
| 2151 | return a:findstart ? 0 : [] |
| 2152 | endfunc |
| 2153 | set tsrfu=s:TsrFunc3 |
| 2154 | new |
| 2155 | call setline(1, 'script1') |
| 2156 | let g:TsrFunc3Args = [] |
| 2157 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2158 | call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args) |
| 2159 | bw! |
| 2160 | |
| 2161 | let &tsrfu = 's:TsrFunc3' |
| 2162 | new |
| 2163 | call setline(1, 'script2') |
| 2164 | let g:TsrFunc3Args = [] |
| 2165 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2166 | call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args) |
| 2167 | bw! |
| 2168 | delfunc s:TsrFunc3 |
| 2169 | |
| 2170 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2171 | let &thesaurusfunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2172 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2173 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2174 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2175 | set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2176 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2177 | let g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2178 | 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] | 2179 | call assert_equal([], g:TsrFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2180 | bw! |
| 2181 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2182 | " set 'thesaurusfunc' to a partial with dict. This used to cause a crash. |
| 2183 | func SetTsrFunc() |
| 2184 | let params = {'thesaurus': function('g:DictTsrFunc')} |
| 2185 | let &thesaurusfunc = params.thesaurus |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2186 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2187 | func g:DictTsrFunc(_) dict |
| 2188 | endfunc |
| 2189 | call SetTsrFunc() |
| 2190 | new |
| 2191 | call SetTsrFunc() |
| 2192 | bw |
| 2193 | call test_garbagecollect_now() |
| 2194 | new |
| 2195 | set thesaurusfunc= |
| 2196 | wincmd w |
| 2197 | %bw! |
| 2198 | delfunc SetTsrFunc |
| 2199 | |
| 2200 | " set buffer-local 'thesaurusfunc' to a partial with dict. This used to |
| 2201 | " cause a crash. |
| 2202 | func SetLocalTsrFunc() |
| 2203 | let params = {'thesaurus': function('g:DictTsrFunc')} |
| 2204 | let &l:thesaurusfunc = params.thesaurus |
| 2205 | endfunc |
| 2206 | call SetLocalTsrFunc() |
| 2207 | call test_garbagecollect_now() |
| 2208 | call SetLocalTsrFunc() |
| 2209 | set thesaurusfunc= |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2210 | bw! |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2211 | delfunc g:DictTsrFunc |
| 2212 | delfunc SetLocalTsrFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2213 | |
| 2214 | " Vim9 tests |
| 2215 | let lines =<< trim END |
| 2216 | vim9script |
| 2217 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2218 | def Vim9tsrFunc(callnr: number, findstart: number, base: string): any |
| 2219 | add(g:Vim9tsrFunc_Args, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2220 | return findstart ? 0 : [] |
| 2221 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2222 | |
| 2223 | # Test for using a def function with thesaurusfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2224 | set thesaurusfunc=function('Vim9tsrFunc',\ [60]) |
| 2225 | new | only |
| 2226 | setline(1, 'one') |
| 2227 | g:Vim9tsrFunc_Args = [] |
| 2228 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2229 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args) |
| 2230 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2231 | |
| 2232 | # Test for using a global function name |
| 2233 | &thesaurusfunc = g:TsrFunc2 |
| 2234 | new | only |
| 2235 | setline(1, 'two') |
| 2236 | g:TsrFunc2Args = [] |
| 2237 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2238 | assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args) |
| 2239 | bw! |
| 2240 | |
| 2241 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2242 | def LocalTsrFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2243 | add(g:LocalTsrFuncArgs, [findstart, base]) |
| 2244 | return findstart ? 0 : [] |
| 2245 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2246 | &thesaurusfunc = LocalTsrFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2247 | new | only |
| 2248 | setline(1, 'three') |
| 2249 | g:LocalTsrFuncArgs = [] |
| 2250 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2251 | assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs) |
| 2252 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2253 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2254 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2255 | |
| 2256 | " cleanup |
| 2257 | set thesaurusfunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2258 | delfunc TsrFunc1 |
| 2259 | delfunc TsrFunc2 |
| 2260 | unlet g:TsrFunc1Args g:TsrFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2261 | %bw! |
| 2262 | endfunc |
| 2263 | |
Christian Brabandt | ac72c21 | 2022-04-07 21:00:53 +0100 | [diff] [blame] | 2264 | func FooBarComplete(findstart, base) |
| 2265 | if a:findstart |
| 2266 | return col('.') - 1 |
| 2267 | else |
| 2268 | return ["Foo", "Bar", "}"] |
| 2269 | endif |
| 2270 | endfunc |
| 2271 | |
| 2272 | func Test_complete_smartindent() |
| 2273 | new |
| 2274 | setlocal smartindent completefunc=FooBarComplete |
| 2275 | |
| 2276 | exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>" |
| 2277 | let result = getline(1,'$') |
| 2278 | call assert_equal(['', '{','}',''], result) |
| 2279 | bw! |
| 2280 | delfunction! FooBarComplete |
| 2281 | endfunc |
| 2282 | |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2283 | func Test_complete_overrun() |
| 2284 | " this was going past the end of the copied text |
| 2285 | new |
| 2286 | sil norm si”0s0 |
| 2287 | bwipe! |
| 2288 | endfunc |
| 2289 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2290 | func Test_infercase_very_long_line() |
| 2291 | " this was truncating the line when inferring case |
| 2292 | new |
| 2293 | let longLine = "blah "->repeat(300) |
| 2294 | let verylongLine = "blah "->repeat(400) |
| 2295 | call setline(1, verylongLine) |
| 2296 | call setline(2, longLine) |
| 2297 | set ic infercase |
| 2298 | exe "normal 2Go\<C-X>\<C-L>\<Esc>" |
| 2299 | call assert_equal(longLine, getline(3)) |
| 2300 | |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 2301 | " check that the too long text is NUL terminated |
| 2302 | %del |
| 2303 | norm o |
| 2304 | norm 1987ax |
| 2305 | exec "norm ox\<C-X>\<C-L>" |
| 2306 | call assert_equal(repeat('x', 1987), getline(3)) |
| 2307 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2308 | bwipe! |
| 2309 | set noic noinfercase |
| 2310 | endfunc |
| 2311 | |
Bram Moolenaar | baefde1 | 2022-07-07 19:59:49 +0100 | [diff] [blame] | 2312 | func Test_ins_complete_add() |
| 2313 | " this was reading past the end of allocated memory |
| 2314 | new |
| 2315 | norm o |
| 2316 | norm 7o€€ |
| 2317 | sil! norm o |
| 2318 | |
| 2319 | bwipe! |
| 2320 | endfunc |
| 2321 | |
Bram Moolenaar | a6f9e30 | 2022-07-28 21:51:37 +0100 | [diff] [blame] | 2322 | func Test_ins_complete_end_of_line() |
| 2323 | " this was reading past the end of the line |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 2324 | new |
Bram Moolenaar | a6f9e30 | 2022-07-28 21:51:37 +0100 | [diff] [blame] | 2325 | norm 8o€ý |
| 2326 | sil! norm o |
| 2327 | |
| 2328 | bwipe! |
| 2329 | endfunc |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2330 | |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 2331 | func s:Tagfunc(t,f,o) |
| 2332 | bwipe! |
| 2333 | return [] |
| 2334 | endfunc |
| 2335 | |
| 2336 | " This was using freed memory, since 'complete' was in a wiped out buffer. |
| 2337 | " Also using a window that was closed. |
| 2338 | func Test_tagfunc_wipes_out_buffer() |
| 2339 | new |
| 2340 | set complete=.,t,w,b,u,i |
| 2341 | se tagfunc=s:Tagfunc |
| 2342 | sil norm i |
| 2343 | |
| 2344 | bwipe! |
| 2345 | endfunc |
| 2346 | |
Bram Moolenaar | 6ac2e43 | 2023-03-31 19:32:29 +0100 | [diff] [blame] | 2347 | func Test_ins_complete_popup_position() |
| 2348 | CheckScreendump |
| 2349 | |
| 2350 | let lines =<< trim END |
| 2351 | vim9script |
| 2352 | set nowrap |
| 2353 | setline(1, ['one', 'two', 'this is line ', 'four']) |
| 2354 | prop_type_add('test', {highlight: 'Error'}) |
| 2355 | prop_add(3, 0, { |
| 2356 | text_align: 'above', |
| 2357 | text: 'The quick brown fox jumps over the lazy dog', |
| 2358 | type: 'test' |
| 2359 | }) |
| 2360 | END |
| 2361 | call writefile(lines, 'XinsPopup', 'D') |
| 2362 | let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10}) |
| 2363 | |
| 2364 | call term_sendkeys(buf, "3GA\<C-N>") |
| 2365 | call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {}) |
| 2366 | |
| 2367 | call StopVimInTerminal(buf) |
| 2368 | endfunc |
| 2369 | |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2370 | func GetCompleteInfo() |
| 2371 | let g:compl_info = complete_info() |
| 2372 | return '' |
| 2373 | endfunc |
| 2374 | |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 2375 | func Test_completion_restart() |
| 2376 | new |
| 2377 | set complete=. completeopt=menuone backspace=2 |
| 2378 | call setline(1, 'workhorse workhorse') |
| 2379 | exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>" |
| 2380 | call assert_equal(1, len(g:compl_info['items'])) |
| 2381 | call assert_equal('workhorse', g:compl_info['items'][0]['word']) |
| 2382 | set complete& completeopt& backspace& |
| 2383 | bwipe! |
| 2384 | endfunc |
| 2385 | |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2386 | func Test_complete_info_index() |
| 2387 | new |
| 2388 | call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"]) |
| 2389 | inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR> |
| 2390 | |
| 2391 | " Ensure 'index' in complete_info() is coherent with the 'items' array. |
| 2392 | |
| 2393 | set completeopt=menu,preview |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2394 | " Search forward |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2395 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2396 | 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] | 2397 | call assert_equal(6 , len(g:compl_info['items'])) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2398 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2399 | 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] | 2400 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2401 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2402 | call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2403 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2404 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2405 | call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2406 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2407 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2408 | call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2409 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2410 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2411 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2412 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2413 | " Search forward: unselected item |
| 2414 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2415 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2416 | call assert_equal(-1 , g:compl_info['selected']) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2417 | |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2418 | " Search backward |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2419 | call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2420 | 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] | 2421 | call assert_equal(6 , len(g:compl_info['items'])) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2422 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2423 | 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] | 2424 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2425 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2426 | call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2427 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2428 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2429 | call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2430 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2431 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2432 | call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2433 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2434 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2435 | call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2436 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2437 | " search backwards: unselected item |
| 2438 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2439 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2440 | call assert_equal(-1 , g:compl_info['selected']) |
| 2441 | |
| 2442 | " switch direction: forwards, then backwards |
| 2443 | call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2444 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2445 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2446 | " switch direction: forwards, then backwards, then forwards again |
| 2447 | call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2448 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2449 | call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2450 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2451 | |
| 2452 | " switch direction: backwards, then forwards |
| 2453 | call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2454 | call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2455 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2456 | " switch direction: backwards, then forwards, then backwards again |
| 2457 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2458 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2459 | call assert_equal(6 , len(g:compl_info['items'])) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2460 | |
| 2461 | " Add 'noselect', check that 'selected' is -1 when nothing is selected. |
| 2462 | set completeopt+=noselect |
| 2463 | " Search forward. |
| 2464 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2465 | call assert_equal(-1, g:compl_info['selected']) |
| 2466 | |
| 2467 | " Search backward. |
| 2468 | call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2469 | call assert_equal(-1, g:compl_info['selected']) |
| 2470 | |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2471 | 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] | 2472 | call assert_equal(5, g:compl_info['selected']) |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2473 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2474 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2475 | 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] | 2476 | 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] | 2477 | call assert_equal(6 , len(g:compl_info['items'])) |
Christian Brabandt | 57f9ce1 | 2023-11-04 09:58:14 +0100 | [diff] [blame] | 2478 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2479 | call assert_equal(-1, g:compl_info['selected']) |
| 2480 | call assert_equal(6 , len(g:compl_info['items'])) |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2481 | |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2482 | set completeopt& |
| 2483 | bwipe! |
| 2484 | endfunc |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 2485 | |
Christian Brabandt | fef6630 | 2024-01-29 21:46:58 +0100 | [diff] [blame] | 2486 | func Test_complete_changed_complete_info() |
| 2487 | CheckRunVimInTerminal |
| 2488 | " this used to crash vim, see #13929 |
| 2489 | let lines =<< trim END |
| 2490 | set completeopt=menuone |
| 2491 | autocmd CompleteChanged * call complete_info(['items']) |
| 2492 | call feedkeys("iii\<cr>\<c-p>") |
| 2493 | END |
| 2494 | call writefile(lines, 'Xsegfault', 'D') |
| 2495 | let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5}) |
| 2496 | call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000) |
| 2497 | call StopVimInTerminal(buf) |
| 2498 | endfunc |
| 2499 | |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 2500 | func Test_completefunc_first_call_complete_add() |
| 2501 | new |
| 2502 | |
| 2503 | func Complete(findstart, base) abort |
| 2504 | if a:findstart |
| 2505 | let col = col('.') |
| 2506 | call complete_add('#') |
| 2507 | return col - 1 |
| 2508 | else |
| 2509 | return [] |
| 2510 | endif |
| 2511 | endfunc |
| 2512 | |
| 2513 | set completeopt=longest completefunc=Complete |
| 2514 | " This used to cause heap-buffer-overflow |
| 2515 | call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:') |
| 2516 | |
| 2517 | delfunc Complete |
| 2518 | set completeopt& completefunc& |
| 2519 | bwipe! |
| 2520 | endfunc |
| 2521 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2522 | func Test_complete_fuzzy_match() |
| 2523 | func OnPumChange() |
| 2524 | let g:item = get(v:event, 'completed_item', {}) |
| 2525 | let g:word = get(g:item, 'word', v:null) |
| 2526 | endfunction |
| 2527 | |
| 2528 | augroup AAAAA_Group |
| 2529 | au! |
| 2530 | autocmd CompleteChanged * :call OnPumChange() |
| 2531 | augroup END |
| 2532 | |
| 2533 | func Omni_test(findstart, base) |
| 2534 | if a:findstart |
| 2535 | return col(".") |
| 2536 | endif |
| 2537 | return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}] |
| 2538 | endfunc |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2539 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2540 | new |
| 2541 | set omnifunc=Omni_test |
| 2542 | set completeopt+=noinsert,fuzzy |
| 2543 | call feedkeys("Gi\<C-x>\<C-o>", 'tx') |
| 2544 | call assert_equal('foo', g:word) |
| 2545 | call feedkeys("S\<C-x>\<C-o>fb", 'tx') |
| 2546 | call assert_equal('fooBaz', g:word) |
| 2547 | call feedkeys("S\<C-x>\<C-o>fa", 'tx') |
| 2548 | call assert_equal('foobar', g:word) |
| 2549 | " select next |
| 2550 | call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx') |
| 2551 | call assert_equal('foobar', g:word) |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2552 | " can cyclically select next |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2553 | call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx') |
| 2554 | call assert_equal(v:null, g:word) |
| 2555 | " select prev |
| 2556 | call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx') |
| 2557 | call assert_equal(v:null, g:word) |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2558 | " can cyclically select prev |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2559 | call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx') |
| 2560 | call assert_equal('fooBaz', g:word) |
| 2561 | |
glepnir | f94c9c4 | 2024-06-14 21:11:56 +0200 | [diff] [blame] | 2562 | func Comp() |
| 2563 | call complete(col('.'), ["fooBaz", "foobar", "foobala"]) |
| 2564 | return '' |
| 2565 | endfunc |
| 2566 | call feedkeys("i\<C-R>=Comp()\<CR>", 'tx') |
| 2567 | call assert_equal('fooBaz', g:word) |
| 2568 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2569 | " respect noselect |
| 2570 | set completeopt+=noselect |
| 2571 | call feedkeys("S\<C-x>\<C-o>fb", 'tx') |
| 2572 | call assert_equal(v:null, g:word) |
| 2573 | call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx') |
| 2574 | call assert_equal('fooBaz', g:word) |
| 2575 | |
zeertzjq | acc8746 | 2024-06-16 08:44:05 +0200 | [diff] [blame^] | 2576 | " avoid breaking default completion behavior |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 2577 | set completeopt=fuzzy,menu |
| 2578 | call setline(1, ['hello help hero h']) |
zeertzjq | acc8746 | 2024-06-16 08:44:05 +0200 | [diff] [blame^] | 2579 | " Use "!" flag of feedkeys() so that ex_normal_busy is not set and |
| 2580 | " ins_compl_check_keys() is not skipped. |
| 2581 | " Add a "0" after the <Esc> to avoid waiting for an escape sequence. |
| 2582 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 2583 | call assert_equal('hello help hero hello', getline('.')) |
| 2584 | set completeopt+=noinsert |
| 2585 | call setline(1, ['hello help hero h']) |
zeertzjq | acc8746 | 2024-06-16 08:44:05 +0200 | [diff] [blame^] | 2586 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 2587 | call assert_equal('hello help hero h', getline('.')) |
| 2588 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2589 | " clean up |
| 2590 | set omnifunc= |
| 2591 | bw! |
| 2592 | set complete& completeopt& |
| 2593 | autocmd! AAAAA_Group |
| 2594 | augroup! AAAAA_Group |
| 2595 | delfunc OnPumChange |
| 2596 | delfunc Omni_test |
glepnir | f94c9c4 | 2024-06-14 21:11:56 +0200 | [diff] [blame] | 2597 | delfunc Comp |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2598 | unlet g:item |
| 2599 | unlet g:word |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2600 | endfunc |
| 2601 | |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 2602 | " Check that tie breaking is stable for completeopt+=fuzzy (which should |
| 2603 | " behave the same on different platforms). |
| 2604 | func Test_complete_fuzzy_match_tie() |
| 2605 | new |
| 2606 | set completeopt+=fuzzy,noselect |
| 2607 | call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', '']) |
| 2608 | |
| 2609 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx') |
| 2610 | call assert_equal('aaabbccc', getline('.')) |
| 2611 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx') |
| 2612 | call assert_equal('aaabbCCC', getline('.')) |
| 2613 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx') |
| 2614 | call assert_equal('aaabbcccc', getline('.')) |
| 2615 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx') |
| 2616 | call assert_equal('aaabbCCCC', getline('.')) |
| 2617 | |
| 2618 | bwipe! |
| 2619 | set completeopt& |
| 2620 | endfunc |
| 2621 | |
Christian Brabandt | fef6630 | 2024-01-29 21:46:58 +0100 | [diff] [blame] | 2622 | " vim: shiftwidth=2 sts=2 expandtab nofoldenable |