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 | |
glepnir | 1c5a120 | 2024-12-04 20:27:34 +0100 | [diff] [blame] | 280 | func Test_CompleteDone_vevent_keys() |
| 281 | func OnDone() |
| 282 | let g:complete_word = get(v:event, 'complete_word', v:null) |
| 283 | let g:complete_type = get(v:event, 'complete_type', v:null) |
| 284 | endfunction |
| 285 | |
| 286 | autocmd CompleteDone * :call OnDone() |
| 287 | |
| 288 | func CompleteFunc(findstart, base) |
| 289 | if a:findstart |
| 290 | return col(".") |
| 291 | endif |
| 292 | return [#{word: "foo"}, #{word: "bar"}] |
| 293 | endfunc |
| 294 | set omnifunc=CompleteFunc |
| 295 | set completefunc=CompleteFunc |
| 296 | set completeopt+=menuone |
| 297 | |
| 298 | new |
| 299 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'tx') |
| 300 | call assert_equal('', g:complete_word) |
| 301 | call assert_equal('omni', g:complete_type) |
| 302 | |
| 303 | call feedkeys("S\<C-X>\<C-O>\<C-Y>\<Esc>", 'tx') |
| 304 | call assert_equal('foo', g:complete_word) |
| 305 | call assert_equal('omni', g:complete_type) |
| 306 | |
| 307 | call feedkeys("S\<C-X>\<C-O>\<C-N>\<C-Y>\<Esc>0", 'tx') |
| 308 | call assert_equal('bar', g:complete_word) |
| 309 | call assert_equal('omni', g:complete_type) |
| 310 | |
| 311 | call feedkeys("Shello vim visual v\<C-X>\<C-N>\<ESC>", 'tx') |
| 312 | call assert_equal('', g:complete_word) |
| 313 | call assert_equal('keyword', g:complete_type) |
| 314 | |
| 315 | call feedkeys("Shello vim visual v\<C-X>\<C-N>\<C-Y>", 'tx') |
| 316 | call assert_equal('vim', g:complete_word) |
| 317 | call assert_equal('keyword', g:complete_type) |
| 318 | |
| 319 | call feedkeys("Shello vim visual v\<C-X>\<C-N>\<C-Y>", 'tx') |
| 320 | call assert_equal('vim', g:complete_word) |
| 321 | call assert_equal('keyword', g:complete_type) |
| 322 | |
| 323 | call feedkeys("Shello vim\<CR>completion test\<CR>\<C-X>\<C-l>\<C-Y>", 'tx') |
| 324 | call assert_equal('completion test', g:complete_word) |
| 325 | call assert_equal('whole_line', g:complete_type) |
| 326 | |
| 327 | call feedkeys("S\<C-X>\<C-U>\<C-Y>", 'tx') |
| 328 | call assert_equal('foo', g:complete_word) |
| 329 | call assert_equal('function', g:complete_type) |
| 330 | |
| 331 | inoremap <buffer> <f3> <cmd>call complete(1, ["red", "blue"])<cr> |
| 332 | call feedkeys("S\<f3>\<C-Y>", 'tx') |
| 333 | call assert_equal('red', g:complete_word) |
| 334 | call assert_equal('eval', g:complete_type) |
| 335 | |
| 336 | call feedkeys("S\<C-X>\<C-V>\<C-Y>", 'tx') |
| 337 | call assert_equal('!', g:complete_word) |
| 338 | call assert_equal('cmdline', g:complete_type) |
| 339 | |
| 340 | call writefile([''], 'foo_test', 'D') |
| 341 | call feedkeys("Sfoo\<C-X>\<C-F>\<C-Y>\<Esc>", 'tx') |
| 342 | call assert_equal('foo_test', g:complete_word) |
| 343 | call assert_equal('files', g:complete_type) |
| 344 | |
| 345 | call writefile(['hello help'], 'test_case.txt', 'D') |
| 346 | set dictionary=test_case.txt |
| 347 | call feedkeys("ggdGSh\<C-X>\<C-K>\<C-Y>\<Esc>", 'tx') |
| 348 | call assert_equal('hello', g:complete_word) |
| 349 | call assert_equal('dictionary', g:complete_type) |
| 350 | |
| 351 | set spell spelllang=en_us |
| 352 | call feedkeys("STheatre\<C-X>s\<C-Y>\<Esc>", 'tx') |
| 353 | call assert_equal('Theater', g:complete_word) |
| 354 | call assert_equal('spell', g:complete_type) |
| 355 | |
| 356 | bwipe! |
| 357 | set completeopt& omnifunc& completefunc& spell& spelllang& dictionary& |
| 358 | autocmd! CompleteDone |
| 359 | delfunc OnDone |
| 360 | delfunc CompleteFunc |
| 361 | unlet g:complete_word |
| 362 | unlet g:complete_type |
| 363 | endfunc |
| 364 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 365 | func Test_CompleteDoneDict() |
Bram Moolenaar | 3f169ce | 2020-01-26 22:43:31 +0100 | [diff] [blame] | 366 | au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1) |
| 367 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 368 | |
| 369 | set completefunc=<SID>CompleteDone_CompleteFuncDict |
| 370 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 371 | set completefunc& |
| 372 | |
zeertzjq | 4cd45f1 | 2022-12-15 13:48:30 +0000 | [diff] [blame] | 373 | call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ]) |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 374 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 375 | |
| 376 | let s:called_completedone = 0 |
| 377 | au! CompleteDone |
| 378 | endfunc |
| 379 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 380 | func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 381 | if a:findstart |
| 382 | return 0 |
| 383 | endif |
| 384 | |
| 385 | return { |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 386 | \ 'words': [ |
| 387 | \ { |
| 388 | \ 'word': 'aword', |
| 389 | \ 'abbr': 'wrd', |
| 390 | \ 'menu': 'extra text', |
| 391 | \ 'info': 'words are cool', |
| 392 | \ 'kind': 'W', |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 393 | \ } |
| 394 | \ ] |
| 395 | \ } |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 396 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 397 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 398 | func s:CompleteDone_CheckCompletedItemDictNoUserData() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 399 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 400 | call assert_equal( 'wrd', v:completed_item[ 'abbr' ] ) |
| 401 | call assert_equal( 'extra text', v:completed_item[ 'menu' ] ) |
| 402 | call assert_equal( 'words are cool', v:completed_item[ 'info' ] ) |
| 403 | call assert_equal( 'W', v:completed_item[ 'kind' ] ) |
zeertzjq | 4cd45f1 | 2022-12-15 13:48:30 +0000 | [diff] [blame] | 404 | call assert_equal( '', v:completed_item[ 'user_data' ] ) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 405 | |
| 406 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 407 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 408 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 409 | func Test_CompleteDoneDictNoUserData() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 410 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData() |
| 411 | |
| 412 | set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData |
| 413 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 414 | set completefunc& |
| 415 | |
zeertzjq | 4cd45f1 | 2022-12-15 13:48:30 +0000 | [diff] [blame] | 416 | call assert_equal('', v:completed_item[ 'user_data' ]) |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 417 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 418 | |
| 419 | let s:called_completedone = 0 |
| 420 | au! CompleteDone |
| 421 | endfunc |
| 422 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 423 | func s:CompleteDone_CompleteFuncList(findstart, base) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 424 | if a:findstart |
| 425 | return 0 |
| 426 | endif |
| 427 | |
| 428 | return [ 'aword' ] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 429 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 430 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 431 | func s:CompleteDone_CheckCompletedItemList() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 432 | call assert_equal( 'aword', v:completed_item[ 'word' ] ) |
| 433 | call assert_equal( '', v:completed_item[ 'abbr' ] ) |
| 434 | call assert_equal( '', v:completed_item[ 'menu' ] ) |
| 435 | call assert_equal( '', v:completed_item[ 'info' ] ) |
| 436 | call assert_equal( '', v:completed_item[ 'kind' ] ) |
| 437 | call assert_equal( '', v:completed_item[ 'user_data' ] ) |
| 438 | |
| 439 | let s:called_completedone = 1 |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 440 | endfunc |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 441 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 442 | func Test_CompleteDoneList() |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 443 | au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList() |
| 444 | |
| 445 | set completefunc=<SID>CompleteDone_CompleteFuncList |
| 446 | execute "normal a\<C-X>\<C-U>\<C-Y>" |
| 447 | set completefunc& |
| 448 | |
Bram Moolenaar | cee9bc2 | 2019-01-11 13:02:23 +0100 | [diff] [blame] | 449 | call assert_equal('', v:completed_item[ 'user_data' ]) |
| 450 | call assert_true(s:called_completedone) |
Bram Moolenaar | 9b56a57 | 2018-02-10 16:19:32 +0100 | [diff] [blame] | 451 | |
| 452 | let s:called_completedone = 0 |
| 453 | au! CompleteDone |
| 454 | endfunc |
| 455 | |
Bram Moolenaar | af559d2 | 2018-08-08 22:55:41 +0200 | [diff] [blame] | 456 | func Test_CompleteDone_undo() |
| 457 | au CompleteDone * call append(0, "prepend1") |
| 458 | new |
| 459 | call setline(1, ["line1", "line2"]) |
| 460 | call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx") |
| 461 | call assert_equal(["prepend1", "line1", "line2", "line1", ""], |
| 462 | \ getline(1, '$')) |
| 463 | undo |
| 464 | call assert_equal(["line1", "line2"], getline(1, '$')) |
| 465 | bwipe! |
| 466 | au! CompleteDone |
| 467 | endfunc |
| 468 | |
Shougo Matsushita | 61021aa | 2022-07-27 14:40:00 +0100 | [diff] [blame] | 469 | func Test_CompleteDone_modify() |
| 470 | let value = { |
| 471 | \ 'word': '', |
| 472 | \ 'abbr': '', |
| 473 | \ 'menu': '', |
| 474 | \ 'info': '', |
| 475 | \ 'kind': '', |
| 476 | \ 'user_data': '', |
| 477 | \ } |
| 478 | let v:completed_item = value |
zeertzjq | 7502094 | 2022-07-31 11:37:20 +0100 | [diff] [blame] | 479 | call assert_equal(value, v:completed_item) |
Shougo Matsushita | 61021aa | 2022-07-27 14:40:00 +0100 | [diff] [blame] | 480 | endfunc |
| 481 | |
Bram Moolenaar | b806aa5 | 2020-09-12 22:52:57 +0200 | [diff] [blame] | 482 | func CompleteTest(findstart, query) |
| 483 | if a:findstart |
| 484 | return col('.') |
| 485 | endif |
| 486 | return ['matched'] |
| 487 | endfunc |
| 488 | |
| 489 | func Test_completefunc_info() |
| 490 | new |
| 491 | set completeopt=menuone |
| 492 | set completefunc=CompleteTest |
| 493 | 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] | 494 | 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] | 495 | bwipe! |
| 496 | set completeopt& |
| 497 | set completefunc& |
| 498 | endfunc |
| 499 | |
Girish Palya | 12b1eb5 | 2025-02-24 21:39:42 +0100 | [diff] [blame] | 500 | func ScrollInfoWindowUserDefinedFn(findstart, query) |
| 501 | " User defined function (i_CTRL-X_CTRL-U) |
| 502 | if a:findstart |
| 503 | return col('.') |
| 504 | endif |
| 505 | let infostr = range(20)->mapnew({_, v -> string(v)})->join("\n") |
| 506 | return [{'word': 'foo', 'info': infostr}, {'word': 'bar'}] |
| 507 | endfunc |
| 508 | |
| 509 | func ScrollInfoWindowPageDown() |
| 510 | call win_execute(popup_findinfo(), "normal! \<PageDown>") |
| 511 | return '' |
| 512 | endfunc |
| 513 | |
| 514 | func ScrollInfoWindowPageUp() |
| 515 | call win_execute(popup_findinfo(), "normal! \<PageUp>") |
| 516 | return '' |
| 517 | endfunc |
| 518 | |
| 519 | func ScrollInfoWindowTest(mvmt, count, fline) |
| 520 | new |
| 521 | set completeopt=menuone,popup,noinsert,noselect |
| 522 | set completepopup=height:5 |
| 523 | set completefunc=ScrollInfoWindowUserDefinedFn |
| 524 | let keyseq = "i\<C-X>\<C-U>\<C-N>" |
| 525 | for _ in range(a:count) |
| 526 | let keyseq .= (a:mvmt == "pageup" ? "\<C-R>\<C-R>=ScrollInfoWindowPageUp()\<CR>" : |
| 527 | \ "\<C-R>\<C-R>=ScrollInfoWindowPageDown()\<CR>") |
| 528 | endfor |
| 529 | let keyseq .= "\<C-R>\<C-R>=string(popup_getpos(popup_findinfo()))\<CR>\<ESC>" |
| 530 | call feedkeys(keyseq, "tx") |
| 531 | call assert_match('''firstline'': ' . a:fline, getline(1)) |
| 532 | bwipe! |
| 533 | set completeopt& |
| 534 | set completepopup& |
| 535 | set completefunc& |
| 536 | endfunc |
| 537 | |
| 538 | func Test_scroll_info_window() |
| 539 | call ScrollInfoWindowTest("", 0, 1) |
| 540 | call ScrollInfoWindowTest("pagedown", 1, 4) |
| 541 | call ScrollInfoWindowTest("pagedown", 2, 7) |
| 542 | call ScrollInfoWindowTest("pagedown", 3, 11) |
| 543 | call ScrollInfoWindowTest("pageup", 3, 1) |
| 544 | endfunc |
| 545 | |
Girish Palya | 8950bf7 | 2024-03-20 20:07:29 +0100 | [diff] [blame] | 546 | func CompleteInfoUserDefinedFn(findstart, query) |
| 547 | " User defined function (i_CTRL-X_CTRL-U) |
| 548 | if a:findstart |
| 549 | return col('.') |
| 550 | endif |
| 551 | return [{'word': 'foo'}, {'word': 'bar'}, {'word': 'baz'}, {'word': 'qux'}] |
| 552 | endfunc |
| 553 | |
| 554 | func CompleteInfoTestUserDefinedFn(mvmt, idx, noselect) |
| 555 | new |
| 556 | if a:noselect |
| 557 | set completeopt=menuone,popup,noinsert,noselect |
| 558 | else |
| 559 | set completeopt=menu,preview |
| 560 | endif |
| 561 | set completefunc=CompleteInfoUserDefinedFn |
| 562 | call feedkeys("i\<C-X>\<C-U>" . a:mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx") |
| 563 | let completed = a:idx != -1 ? ['foo', 'bar', 'baz', 'qux']->get(a:idx) : '' |
| 564 | call assert_equal(completed. "{'pum_visible': 1, 'mode': 'function', 'selected': " . a:idx . ", 'items': [" . |
| 565 | \ "{'word': 'foo', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " . |
| 566 | \ "{'word': 'bar', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " . |
| 567 | \ "{'word': 'baz', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " . |
| 568 | \ "{'word': 'qux', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" . |
| 569 | \ "]}", getline(1)) |
| 570 | bwipe! |
| 571 | set completeopt& |
| 572 | set completefunc& |
| 573 | endfunc |
| 574 | |
| 575 | func Test_complete_info_user_defined_fn() |
| 576 | " forward |
| 577 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 1, v:true) |
| 578 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 2, v:true) |
| 579 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 2, v:false) |
| 580 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 3, v:false) |
| 581 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>", -1, v:false) |
| 582 | " backward |
| 583 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 2, v:true) |
| 584 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 1, v:true) |
| 585 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>", -1, v:true) |
| 586 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 3, v:false) |
| 587 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 2, v:false) |
| 588 | " forward backward |
| 589 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 1, v:true) |
| 590 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 0, v:true) |
| 591 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 2, v:false) |
| 592 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 3, v:false) |
| 593 | call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 1, v:false) |
| 594 | " backward forward |
| 595 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 0, v:true) |
| 596 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 2, v:true) |
| 597 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 1, v:false) |
| 598 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 3, v:false) |
| 599 | call CompleteInfoTestUserDefinedFn("\<C-P>\<C-N>\<C-N>", 1, v:false) |
| 600 | endfunc |
| 601 | |
zeertzjq | 440d4cb | 2023-03-02 17:51:32 +0000 | [diff] [blame] | 602 | " Test that mouse scrolling/movement should not interrupt completion. |
| 603 | func Test_mouse_scroll_move_during_completion() |
| 604 | new |
| 605 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 606 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 607 | call setline(1, ['', '', '', '', '']) |
| 608 | call cursor(5, 1) |
| 609 | |
| 610 | " Without completion menu scrolling can move text. |
| 611 | set completeopt-=menu wrap |
| 612 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx') |
| 613 | call assert_equal('TestCommand2', getline('.')) |
| 614 | call assert_notequal(1, winsaveview().topline) |
| 615 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx') |
| 616 | call assert_equal('TestCommand2', getline('.')) |
| 617 | call assert_equal(1, winsaveview().topline) |
| 618 | set nowrap |
| 619 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx') |
| 620 | call assert_equal('TestCommand2', getline('.')) |
| 621 | call assert_notequal(0, winsaveview().leftcol) |
| 622 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx') |
| 623 | call assert_equal('TestCommand2', getline('.')) |
| 624 | call assert_equal(0, winsaveview().leftcol) |
| 625 | call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx') |
| 626 | call assert_equal('TestCommand2', getline('.')) |
| 627 | |
| 628 | " With completion menu scrolling cannot move text. |
| 629 | set completeopt+=menu wrap |
| 630 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx') |
| 631 | call assert_equal('TestCommand2', getline('.')) |
| 632 | call assert_equal(1, winsaveview().topline) |
| 633 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx') |
| 634 | call assert_equal('TestCommand2', getline('.')) |
| 635 | call assert_equal(1, winsaveview().topline) |
| 636 | set nowrap |
| 637 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx') |
| 638 | call assert_equal('TestCommand2', getline('.')) |
| 639 | call assert_equal(0, winsaveview().leftcol) |
| 640 | call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx') |
| 641 | call assert_equal('TestCommand2', getline('.')) |
| 642 | call assert_equal(0, winsaveview().leftcol) |
| 643 | call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx') |
| 644 | call assert_equal('TestCommand2', getline('.')) |
| 645 | |
| 646 | bwipe! |
| 647 | set completeopt& wrap& |
| 648 | endfunc |
| 649 | |
Bram Moolenaar | 02ae9b4 | 2018-02-09 15:06:02 +0100 | [diff] [blame] | 650 | " Check that when using feedkeys() typeahead does not interrupt searching for |
| 651 | " completions. |
| 652 | func Test_compl_feedkeys() |
| 653 | new |
| 654 | set completeopt=menuone,noselect |
| 655 | call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx") |
| 656 | call assert_equal("jump jump", getline(1)) |
| 657 | bwipe! |
| 658 | set completeopt& |
| 659 | endfunc |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 660 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 661 | " Test for insert path completion with completeslash option |
| 662 | func Test_ins_completeslash() |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 663 | CheckMSWindows |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 664 | |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 665 | call mkdir('Xcpldir', 'R') |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 666 | let orig_shellslash = &shellslash |
| 667 | set cpt& |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 668 | new |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 669 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 670 | set noshellslash |
| 671 | |
| 672 | set completeslash= |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 673 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 674 | call assert_equal('Xcpldir\', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 675 | |
| 676 | set completeslash=backslash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 677 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 678 | call assert_equal('Xcpldir\', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 679 | |
| 680 | set completeslash=slash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 681 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 682 | call assert_equal('Xcpldir/', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 683 | |
| 684 | set shellslash |
| 685 | |
| 686 | set completeslash= |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 687 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 688 | call assert_equal('Xcpldir/', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 689 | |
| 690 | set completeslash=backslash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 691 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 692 | call assert_equal('Xcpldir\', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 693 | |
| 694 | set completeslash=slash |
Bram Moolenaar | 816736b | 2022-08-29 23:01:45 +0100 | [diff] [blame] | 695 | exe "normal oXcp\<C-X>\<C-F>" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 696 | call assert_equal('Xcpldir/', getline('.')) |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 697 | %bw! |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 698 | |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 699 | set noshellslash |
| 700 | set completeslash=slash |
| 701 | call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1) |
| 702 | |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 703 | let &shellslash = orig_shellslash |
Bram Moolenaar | 50f91d2 | 2019-08-02 19:52:15 +0200 | [diff] [blame] | 704 | set completeslash= |
Bram Moolenaar | ac3150d | 2019-07-28 16:36:39 +0200 | [diff] [blame] | 705 | endfunc |
| 706 | |
Bram Moolenaar | d0e1b71 | 2020-09-27 20:13:03 +0200 | [diff] [blame] | 707 | func Test_pum_stopped_by_timer() |
| 708 | CheckScreendump |
| 709 | |
| 710 | let lines =<< trim END |
| 711 | call setline(1, ['hello', 'hullo', 'heeee', '']) |
| 712 | func StartCompl() |
| 713 | call timer_start(100, { -> execute('stopinsert') }) |
| 714 | call feedkeys("Gah\<C-N>") |
| 715 | endfunc |
| 716 | END |
| 717 | |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 718 | call writefile(lines, 'Xpumscript', 'D') |
Bram Moolenaar | d0e1b71 | 2020-09-27 20:13:03 +0200 | [diff] [blame] | 719 | let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12}) |
| 720 | call term_sendkeys(buf, ":call StartCompl()\<CR>") |
| 721 | call TermWait(buf, 200) |
| 722 | call term_sendkeys(buf, "k") |
| 723 | call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {}) |
| 724 | |
| 725 | call StopVimInTerminal(buf) |
Bram Moolenaar | d0e1b71 | 2020-09-27 20:13:03 +0200 | [diff] [blame] | 726 | endfunc |
| 727 | |
zeertzjq | cd5dbad | 2022-05-04 17:51:50 +0100 | [diff] [blame] | 728 | func Test_complete_stopinsert_startinsert() |
| 729 | nnoremap <F2> <Cmd>startinsert<CR> |
| 730 | inoremap <F2> <Cmd>stopinsert<CR> |
| 731 | " This just checks if this causes an error |
| 732 | call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x') |
| 733 | nunmap <F2> |
| 734 | iunmap <F2> |
| 735 | endfunc |
| 736 | |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 737 | func Test_pum_with_folds_two_tabs() |
| 738 | CheckScreendump |
| 739 | |
| 740 | let lines =<< trim END |
| 741 | set fdm=marker |
| 742 | call setline(1, ['" x {{{1', '" a some text']) |
| 743 | call setline(3, range(&lines)->map({_, val -> '" a' .. val})) |
| 744 | norm! zm |
| 745 | tab sp |
| 746 | call feedkeys('2Gzv', 'xt') |
| 747 | call feedkeys("0fa", 'xt') |
| 748 | END |
| 749 | |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 750 | call writefile(lines, 'Xpumscript', 'D') |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 751 | let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 752 | call TermWait(buf, 50) |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 753 | call term_sendkeys(buf, "a\<C-N>") |
| 754 | call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {}) |
| 755 | |
| 756 | call term_sendkeys(buf, "\<Esc>") |
| 757 | call StopVimInTerminal(buf) |
Bram Moolenaar | 09dd2bb | 2019-12-14 18:42:15 +0100 | [diff] [blame] | 758 | endfunc |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 759 | |
| 760 | func Test_pum_with_preview_win() |
| 761 | CheckScreendump |
| 762 | |
| 763 | let lines =<< trim END |
glepnir | 53387c5 | 2024-05-27 15:11:01 +0200 | [diff] [blame] | 764 | func Omni_test(findstart, base) |
| 765 | if a:findstart |
| 766 | return col(".") - 1 |
| 767 | endif |
| 768 | return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}] |
| 769 | endfunc |
| 770 | set omnifunc=Omni_test |
| 771 | set completeopt+=longest |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 772 | END |
| 773 | |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 774 | call writefile(lines, 'Xpreviewscript', 'D') |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 775 | let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12}) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 776 | call term_sendkeys(buf, "Gi\<C-X>\<C-O>") |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 777 | call TermWait(buf, 200) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 778 | call term_sendkeys(buf, "\<C-N>") |
| 779 | call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {}) |
| 780 | |
| 781 | call term_sendkeys(buf, "\<Esc>") |
| 782 | call StopVimInTerminal(buf) |
Bram Moolenaar | 5e5a98d | 2019-12-15 14:55:33 +0100 | [diff] [blame] | 783 | endfunc |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 784 | |
Bram Moolenaar | 35d8c20 | 2022-03-03 11:46:00 +0000 | [diff] [blame] | 785 | func Test_scrollbar_on_wide_char() |
| 786 | CheckScreendump |
| 787 | |
| 788 | let lines =<< trim END |
| 789 | call setline(1, ['a', ' 啊啊啊', |
| 790 | \ ' 哦哦哦', |
| 791 | \ ' 呃呃呃']) |
| 792 | call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'})) |
| 793 | END |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 794 | call writefile(lines, 'Xwidescript', 'D') |
Bram Moolenaar | 35d8c20 | 2022-03-03 11:46:00 +0000 | [diff] [blame] | 795 | let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10}) |
| 796 | call term_sendkeys(buf, "A\<C-N>") |
| 797 | call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {}) |
| 798 | |
| 799 | call StopVimInTerminal(buf) |
Bram Moolenaar | 35d8c20 | 2022-03-03 11:46:00 +0000 | [diff] [blame] | 800 | endfunc |
| 801 | |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 802 | " Test for inserting the tag search pattern in insert mode |
| 803 | func Test_ins_compl_tag_sft() |
| 804 | call writefile([ |
| 805 | \ "!_TAG_FILE_ENCODING\tutf-8\t//", |
| 806 | \ "first\tXfoo\t/^int first() {}$/", |
| 807 | \ "second\tXfoo\t/^int second() {}$/", |
| 808 | \ "third\tXfoo\t/^int third() {}$/"], |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 809 | \ 'Xtags', 'D') |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 810 | set tags=Xtags |
| 811 | let code =<< trim [CODE] |
| 812 | int first() {} |
| 813 | int second() {} |
| 814 | int third() {} |
| 815 | [CODE] |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 816 | call writefile(code, 'Xfoo', 'D') |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 817 | |
| 818 | enew |
| 819 | set showfulltag |
| 820 | exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>" |
| 821 | call assert_equal('int second() {}', getline(1)) |
| 822 | set noshowfulltag |
| 823 | |
Bram Moolenaar | 830c1af | 2020-01-05 20:35:44 +0100 | [diff] [blame] | 824 | set tags& |
| 825 | %bwipe! |
| 826 | endfunc |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 827 | |
| 828 | " Test for 'completefunc' deleting text |
| 829 | func Test_completefunc_error() |
| 830 | new |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 831 | " delete text when called for the first time |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 832 | func CompleteFunc(findstart, base) |
| 833 | if a:findstart == 1 |
| 834 | normal dd |
| 835 | return col('.') - 1 |
| 836 | endif |
| 837 | return ['a', 'b'] |
| 838 | endfunc |
| 839 | set completefunc=CompleteFunc |
| 840 | call setline(1, ['', 'abcd', '']) |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 841 | call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 842 | |
| 843 | " delete text when called for the second time |
| 844 | func CompleteFunc2(findstart, base) |
| 845 | if a:findstart == 1 |
| 846 | return col('.') - 1 |
| 847 | endif |
| 848 | normal dd |
| 849 | return ['a', 'b'] |
| 850 | endfunc |
| 851 | set completefunc=CompleteFunc2 |
| 852 | call setline(1, ['', 'abcd', '']) |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 853 | call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 854 | |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 855 | " Jump to a different window from the complete function |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 856 | func CompleteFunc3(findstart, base) |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 857 | if a:findstart == 1 |
| 858 | return col('.') - 1 |
| 859 | endif |
| 860 | wincmd p |
| 861 | return ['a', 'b'] |
| 862 | endfunc |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 863 | set completefunc=CompleteFunc3 |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 864 | new |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 865 | call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:') |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 866 | close! |
| 867 | |
| 868 | set completefunc& |
| 869 | delfunc CompleteFunc |
Bram Moolenaar | 28976e2 | 2021-01-29 21:07:07 +0100 | [diff] [blame] | 870 | delfunc CompleteFunc2 |
| 871 | delfunc CompleteFunc3 |
| 872 | close! |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 873 | endfunc |
| 874 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 875 | " Test for returning non-string values from 'completefunc' |
| 876 | func Test_completefunc_invalid_data() |
| 877 | new |
| 878 | func! CompleteFunc(findstart, base) |
| 879 | if a:findstart == 1 |
| 880 | return col('.') - 1 |
| 881 | endif |
| 882 | return [{}, '', 'moon'] |
| 883 | endfunc |
| 884 | set completefunc=CompleteFunc |
| 885 | exe "normal i\<C-X>\<C-U>" |
| 886 | call assert_equal('moon', getline(1)) |
| 887 | set completefunc& |
| 888 | close! |
| 889 | endfunc |
| 890 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 891 | " Test for errors in using complete() function |
| 892 | func Test_complete_func_error() |
| 893 | call assert_fails('call complete(1, ["a"])', 'E785:') |
| 894 | func ListColors() |
| 895 | call complete(col('.'), "blue") |
| 896 | endfunc |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 897 | call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E1211:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 898 | func ListMonths() |
| 899 | call complete(col('.'), test_null_list()) |
| 900 | endfunc |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 901 | call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E1298:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 902 | delfunc ListColors |
| 903 | delfunc ListMonths |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 904 | call assert_fails('call complete_info({})', 'E1211:') |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 905 | call assert_equal([], complete_info(['items']).items) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 906 | endfunc |
| 907 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 908 | " Test for recursively starting completion mode using complete() |
| 909 | func Test_recursive_complete_func() |
| 910 | func ListColors() |
| 911 | call complete(5, ["red", "blue"]) |
| 912 | return '' |
| 913 | endfunc |
| 914 | new |
| 915 | call setline(1, ['a1', 'a2']) |
| 916 | set complete=. |
| 917 | exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>" |
| 918 | call assert_equal('a2blue', getline(3)) |
| 919 | delfunc ListColors |
| 920 | bw! |
| 921 | endfunc |
| 922 | |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 923 | " Test for using complete() with completeopt+=longest |
| 924 | func Test_complete_with_longest() |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 925 | new |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 926 | inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr> |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 927 | |
| 928 | " default: insert first match |
| 929 | set completeopt& |
| 930 | call setline(1, ['i']) |
| 931 | exe "normal Aa\<f3>\<esc>" |
| 932 | call assert_equal('iaax', getline(1)) |
| 933 | |
| 934 | " with longest: insert longest prefix |
| 935 | set completeopt+=longest |
| 936 | call setline(1, ['i']) |
| 937 | exe "normal Aa\<f3>\<esc>" |
| 938 | call assert_equal('iaa', getline(1)) |
| 939 | set completeopt& |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 940 | bwipe! |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 941 | endfunc |
| 942 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 943 | " Test for buffer-local value of 'completeopt' |
| 944 | func Test_completeopt_buffer_local() |
| 945 | set completeopt=menu |
| 946 | new |
| 947 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 948 | call assert_equal('', &l:completeopt) |
| 949 | call assert_equal('menu', &completeopt) |
| 950 | call assert_equal('menu', &g:completeopt) |
| 951 | |
| 952 | setlocal bufhidden=hide |
| 953 | enew |
| 954 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 955 | call assert_equal('', &l:completeopt) |
| 956 | call assert_equal('menu', &completeopt) |
| 957 | call assert_equal('menu', &g:completeopt) |
| 958 | |
| 959 | setlocal completeopt+=fuzzy,noinsert |
| 960 | call assert_equal('menu,fuzzy,noinsert', &l:completeopt) |
| 961 | call assert_equal('menu,fuzzy,noinsert', &completeopt) |
| 962 | call assert_equal('menu', &g:completeopt) |
| 963 | call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix') |
| 964 | call assert_equal('foobaz', getline('.')) |
| 965 | |
| 966 | setlocal completeopt= |
| 967 | call assert_equal('', &l:completeopt) |
| 968 | call assert_equal('menu', &completeopt) |
| 969 | call assert_equal('menu', &g:completeopt) |
| 970 | call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix') |
| 971 | call assert_equal('foofoo', getline('.')) |
| 972 | |
| 973 | setlocal completeopt+=longest |
| 974 | call assert_equal('menu,longest', &l:completeopt) |
| 975 | call assert_equal('menu,longest', &completeopt) |
| 976 | call assert_equal('menu', &g:completeopt) |
| 977 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 978 | call assert_equal('foo', getline('.')) |
| 979 | |
| 980 | setlocal bufhidden=hide |
| 981 | buffer # |
| 982 | call assert_equal('', &l:completeopt) |
| 983 | call assert_equal('menu', &completeopt) |
| 984 | call assert_equal('menu', &g:completeopt) |
| 985 | call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix') |
| 986 | call assert_equal('foofoo', getline('.')) |
| 987 | |
| 988 | setlocal completeopt+=fuzzy,noinsert |
| 989 | call assert_equal('menu,fuzzy,noinsert', &l:completeopt) |
| 990 | call assert_equal('menu,fuzzy,noinsert', &completeopt) |
| 991 | call assert_equal('menu', &g:completeopt) |
| 992 | call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix') |
| 993 | call assert_equal('foobaz', getline('.')) |
| 994 | |
| 995 | buffer # |
| 996 | call assert_equal('menu,longest', &l:completeopt) |
| 997 | call assert_equal('menu,longest', &completeopt) |
| 998 | call assert_equal('menu', &g:completeopt) |
| 999 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 1000 | call assert_equal('foo', getline('.')) |
| 1001 | |
| 1002 | setlocal bufhidden=wipe |
| 1003 | buffer! # |
| 1004 | bwipe! |
| 1005 | call assert_equal('', &l:completeopt) |
| 1006 | call assert_equal('menu', &completeopt) |
| 1007 | call assert_equal('menu', &g:completeopt) |
| 1008 | |
zeertzjq | 46dcd84 | 2024-11-03 09:10:50 +0100 | [diff] [blame] | 1009 | new | only |
| 1010 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 1011 | set completeopt& |
| 1012 | setlocal completeopt=menu,fuzzy,noinsert |
| 1013 | setglobal completeopt=menu,longest |
| 1014 | call assert_equal('menu,fuzzy,noinsert', &completeopt) |
| 1015 | call assert_equal('menu,fuzzy,noinsert', &l:completeopt) |
| 1016 | call assert_equal('menu,longest', &g:completeopt) |
| 1017 | call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix') |
| 1018 | call assert_equal('foobaz', getline('.')) |
| 1019 | setlocal bufhidden=wipe |
| 1020 | new | only! |
| 1021 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 1022 | call assert_equal('menu,longest', &completeopt) |
| 1023 | call assert_equal('menu,longest', &g:completeopt) |
| 1024 | call assert_equal('', &l:completeopt) |
| 1025 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 1026 | call assert_equal('foo', getline('.')) |
| 1027 | bwipe! |
| 1028 | |
| 1029 | new | only |
| 1030 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 1031 | set completeopt& |
| 1032 | setlocal completeopt=menu,fuzzy,noinsert |
| 1033 | set completeopt=menu,longest |
| 1034 | call assert_equal('menu,longest', &completeopt) |
| 1035 | call assert_equal('menu,longest', &g:completeopt) |
| 1036 | call assert_equal('', &l:completeopt) |
| 1037 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 1038 | call assert_equal('foo', getline('.')) |
| 1039 | setlocal bufhidden=wipe |
| 1040 | new | only! |
| 1041 | call setline(1, ['foofoo', 'foobar', 'foobaz', '']) |
| 1042 | call assert_equal('menu,longest', &completeopt) |
| 1043 | call assert_equal('menu,longest', &g:completeopt) |
| 1044 | call assert_equal('', &l:completeopt) |
| 1045 | call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix') |
| 1046 | call assert_equal('foo', getline('.')) |
| 1047 | bwipe! |
| 1048 | |
zeertzjq | 529b9ad | 2024-06-05 20:27:06 +0200 | [diff] [blame] | 1049 | set completeopt& |
| 1050 | endfunc |
bfredl | 87af60c | 2022-09-24 11:17:51 +0100 | [diff] [blame] | 1051 | |
Bram Moolenaar | 224a5f1 | 2020-04-28 20:29:07 +0200 | [diff] [blame] | 1052 | " Test for completing words following a completed word in a line |
| 1053 | func Test_complete_wrapscan() |
| 1054 | " complete words from another buffer |
| 1055 | new |
| 1056 | call setline(1, ['one two', 'three four']) |
| 1057 | new |
| 1058 | setlocal complete=w |
| 1059 | call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 1060 | call assert_equal('two three four', getline(1)) |
| 1061 | close! |
| 1062 | " complete words from the current buffer |
| 1063 | setlocal complete=. |
| 1064 | %d |
| 1065 | call setline(1, ['one two', '']) |
| 1066 | call cursor(2, 1) |
| 1067 | call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt') |
| 1068 | call assert_equal('one two one two', getline(2)) |
| 1069 | close! |
| 1070 | endfunc |
| 1071 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 1072 | " Test for completing special characters |
| 1073 | func Test_complete_special_chars() |
| 1074 | new |
| 1075 | call setline(1, 'int .*[-\^$ func float') |
| 1076 | call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt') |
| 1077 | call assert_equal('int .*[-\^$ func float', getline(2)) |
| 1078 | close! |
| 1079 | endfunc |
| 1080 | |
| 1081 | " Test for completion when text is wrapped across lines. |
| 1082 | func Test_complete_across_line() |
| 1083 | new |
| 1084 | call setline(1, ['red green blue', 'one two three']) |
| 1085 | setlocal textwidth=20 |
| 1086 | exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1087 | call assert_equal(['one two three red', 'green blue one'], getline(2, '$')) |
| 1088 | close! |
| 1089 | endfunc |
| 1090 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 1091 | " Test for completing words with a '.' at the end of a word. |
| 1092 | func Test_complete_joinspaces() |
| 1093 | new |
| 1094 | call setline(1, ['one two.', 'three. four']) |
| 1095 | set joinspaces |
| 1096 | exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1097 | call assert_equal("one two. three. four", getline(3)) |
| 1098 | set joinspaces& |
| 1099 | bw! |
| 1100 | endfunc |
| 1101 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 1102 | " Test for using CTRL-L to add one character when completing matching |
| 1103 | func Test_complete_add_onechar() |
| 1104 | new |
| 1105 | call setline(1, ['wool', 'woodwork']) |
| 1106 | call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt') |
| 1107 | call assert_equal('woof', getline(3)) |
| 1108 | |
| 1109 | " use 'ignorecase' and backspace to erase characters from the prefix string |
| 1110 | " and then add letters using CTRL-L |
| 1111 | %d |
| 1112 | set ignorecase backspace=2 |
| 1113 | setlocal complete=. |
| 1114 | call setline(1, ['workhorse', 'workload']) |
| 1115 | normal Go |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 1116 | 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] | 1117 | call assert_equal('workh', getline(3)) |
| 1118 | set ignorecase& backspace& |
| 1119 | close! |
| 1120 | endfunc |
| 1121 | |
Yegappan Lakshmanan | edc6f10 | 2021-12-29 17:38:46 +0000 | [diff] [blame] | 1122 | " Test for using CTRL-X CTRL-L to complete whole lines lines |
| 1123 | func Test_complete_wholeline() |
| 1124 | new |
| 1125 | " complete one-line |
| 1126 | call setline(1, ['a1', 'a2']) |
| 1127 | exe "normal ggoa\<C-X>\<C-L>" |
| 1128 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 1129 | " go to the next match (wrapping around the buffer) |
| 1130 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>" |
| 1131 | call assert_equal(['a1', 'a', 'a2'], getline(1, '$')) |
| 1132 | " go to the next match |
| 1133 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>" |
| 1134 | call assert_equal(['a1', 'a2', 'a2'], getline(1, '$')) |
| 1135 | exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>" |
| 1136 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 1137 | " repeat the test using CTRL-L |
| 1138 | " go to the next match (wrapping around the buffer) |
| 1139 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>" |
| 1140 | call assert_equal(['a1', 'a2', 'a2'], getline(1, '$')) |
| 1141 | " go to the next match |
| 1142 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>" |
| 1143 | call assert_equal(['a1', 'a', 'a2'], getline(1, '$')) |
| 1144 | exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>" |
| 1145 | call assert_equal(['a1', 'a1', 'a2'], getline(1, '$')) |
| 1146 | %d |
| 1147 | " use CTRL-X CTRL-L to add one more line |
| 1148 | call setline(1, ['a1', 'b1']) |
| 1149 | setlocal complete=. |
| 1150 | exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>" |
| 1151 | call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$')) |
| 1152 | bw! |
| 1153 | endfunc |
| 1154 | |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 1155 | " Test insert completion with 'cindent' (adjust the indent) |
| 1156 | func Test_complete_with_cindent() |
| 1157 | new |
| 1158 | setlocal cindent |
| 1159 | call setline(1, ['if (i == 1)', " j = 2;"]) |
| 1160 | exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}" |
| 1161 | call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$')) |
| 1162 | |
| 1163 | %d |
| 1164 | call setline(1, ['when while', '{', '']) |
| 1165 | setlocal cinkeys+==while |
| 1166 | exe "normal Giwh\<C-P> " |
| 1167 | call assert_equal("\twhile ", getline('$')) |
| 1168 | close! |
| 1169 | endfunc |
| 1170 | |
| 1171 | " Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions |
| 1172 | func Test_complete_cmdline() |
| 1173 | new |
| 1174 | exe "normal icaddb\<C-X>\<C-V>" |
| 1175 | call assert_equal('caddbuffer', getline(1)) |
| 1176 | exe "normal ocall getqf\<C-X>\<C-V>" |
| 1177 | call assert_equal('call getqflist(', getline(2)) |
| 1178 | exe "normal oabcxyz(\<C-X>\<C-V>" |
| 1179 | call assert_equal('abcxyz(', getline(3)) |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 1180 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 1181 | com! -buffer TestCommand2 echo 'TestCommand2' |
Jim Zhou | f7087cb | 2025-03-05 20:25:11 +0100 | [diff] [blame] | 1182 | write! TestCommand1Test |
| 1183 | write! TestCommand2Test |
zeertzjq | dca29d9 | 2021-08-31 19:12:51 +0200 | [diff] [blame] | 1184 | " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode |
| 1185 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>" |
| 1186 | call assert_equal('TestCommand2Test', getline(4)) |
| 1187 | call delete('TestCommand1Test') |
| 1188 | call delete('TestCommand2Test') |
| 1189 | delcom TestCommand1 |
| 1190 | delcom TestCommand2 |
| 1191 | close! |
| 1192 | endfunc |
| 1193 | |
| 1194 | " Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match |
| 1195 | func Test_complete_stop() |
| 1196 | new |
| 1197 | func Save_mode1() |
| 1198 | let g:mode1 = mode(1) |
| 1199 | return '' |
| 1200 | endfunc |
| 1201 | func Save_mode2() |
| 1202 | let g:mode2 = mode(1) |
| 1203 | return '' |
| 1204 | endfunc |
| 1205 | inoremap <F1> <C-R>=Save_mode1()<CR> |
| 1206 | inoremap <F2> <C-R>=Save_mode2()<CR> |
| 1207 | call setline(1, ['aaa bbb ccc ']) |
| 1208 | exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1209 | call assert_equal('ic', g:mode1) |
| 1210 | call assert_equal('i', g:mode2) |
| 1211 | call assert_equal('aaa bbb ccc ', getline(1)) |
| 1212 | exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1213 | call assert_equal('ic', g:mode1) |
| 1214 | call assert_equal('i', g:mode2) |
| 1215 | call assert_equal('aaa bbb ccc aaa', getline(1)) |
| 1216 | set completeopt+=noselect |
| 1217 | exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1218 | call assert_equal('ic', g:mode1) |
| 1219 | call assert_equal('i', g:mode2) |
| 1220 | call assert_equal('aaa bbb ccc aaa bb', getline(1)) |
| 1221 | set completeopt& |
| 1222 | exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1223 | call assert_equal('ic', g:mode1) |
| 1224 | call assert_equal('i', g:mode2) |
| 1225 | call assert_equal('aaa bbb ccc aaa bb d', getline(1)) |
| 1226 | com! -buffer TestCommand1 echo 'TestCommand1' |
| 1227 | com! -buffer TestCommand2 echo 'TestCommand2' |
| 1228 | exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" |
| 1229 | call assert_equal('ic', g:mode1) |
| 1230 | call assert_equal('i', g:mode2) |
| 1231 | call assert_equal('TestCommand2', getline(2)) |
| 1232 | delcom TestCommand1 |
| 1233 | delcom TestCommand2 |
| 1234 | unlet g:mode1 |
| 1235 | unlet g:mode2 |
| 1236 | iunmap <F1> |
| 1237 | iunmap <F2> |
| 1238 | delfunc Save_mode1 |
| 1239 | delfunc Save_mode2 |
Bram Moolenaar | f9ab52e | 2020-05-05 19:57:18 +0200 | [diff] [blame] | 1240 | close! |
| 1241 | endfunc |
| 1242 | |
Yegappan Lakshmanan | 5d2e007 | 2021-12-30 11:40:53 +0000 | [diff] [blame] | 1243 | " Test for typing CTRL-R in insert completion mode to insert a register |
| 1244 | " content. |
| 1245 | func Test_complete_reginsert() |
| 1246 | new |
| 1247 | call setline(1, ['a1', 'a12', 'a123', 'a1234']) |
| 1248 | |
| 1249 | " if a valid CTRL-X mode key is returned from <C-R>=, then it should be |
| 1250 | " processed. Otherwise, CTRL-X mode should be stopped and the key should be |
| 1251 | " inserted. |
| 1252 | exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>" |
| 1253 | call assert_equal('a123', getline(5)) |
| 1254 | let @r = "\<C-P>\<C-P>" |
| 1255 | exe "normal GCa\<C-P>\<C-R>r" |
| 1256 | call assert_equal('a12', getline(5)) |
| 1257 | exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>" |
| 1258 | call assert_equal('a1234x', getline(5)) |
| 1259 | bw! |
| 1260 | endfunc |
| 1261 | |
Bram Moolenaar | 8f187fc | 2020-09-26 18:47:11 +0200 | [diff] [blame] | 1262 | func Test_issue_7021() |
| 1263 | CheckMSWindows |
| 1264 | |
| 1265 | let orig_shellslash = &shellslash |
| 1266 | set noshellslash |
| 1267 | |
| 1268 | set completeslash=slash |
| 1269 | call assert_false(expand('~') =~ '/') |
| 1270 | |
| 1271 | let &shellslash = orig_shellslash |
| 1272 | set completeslash= |
| 1273 | endfunc |
| 1274 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1275 | " Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings |
| 1276 | func Test_complete_longest_match() |
| 1277 | for e in ['latin1', 'utf-8'] |
| 1278 | exe 'set encoding=' .. e |
| 1279 | new |
| 1280 | set complete=. |
| 1281 | set completeopt=menu,longest |
| 1282 | call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1']) |
| 1283 | exe "normal Gopfx\<C-P>" |
| 1284 | call assert_equal('pfx_', getline(5)) |
| 1285 | bw! |
| 1286 | endfor |
| 1287 | |
| 1288 | " Test for completing additional words with longest match set |
| 1289 | new |
| 1290 | call setline(1, ['abc1', 'abd2']) |
| 1291 | exe "normal Goab\<C-P>\<C-X>\<C-P>" |
| 1292 | call assert_equal('ab', getline(3)) |
| 1293 | bw! |
| 1294 | set complete& completeopt& |
| 1295 | endfunc |
| 1296 | |
| 1297 | " Test for removing the first displayed completion match and selecting the |
| 1298 | " match just before that. |
| 1299 | func Test_complete_erase_firstmatch() |
| 1300 | new |
| 1301 | call setline(1, ['a12', 'a34', 'a56']) |
| 1302 | set complete=. |
| 1303 | exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>" |
| 1304 | call assert_equal('a34', getline('$')) |
| 1305 | set complete& |
| 1306 | bw! |
| 1307 | endfunc |
| 1308 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1309 | " Test for completing words from unloaded buffers |
| 1310 | func Test_complete_from_unloadedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1311 | call writefile(['abc'], "Xfile1", 'D') |
| 1312 | call writefile(['def'], "Xfile2", 'D') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1313 | edit Xfile1 |
| 1314 | edit Xfile2 |
| 1315 | new | close |
| 1316 | enew |
| 1317 | bunload Xfile1 Xfile2 |
| 1318 | set complete=u |
| 1319 | " complete from an unloaded buffer |
| 1320 | exe "normal! ia\<C-P>" |
| 1321 | call assert_equal('abc', getline(1)) |
| 1322 | exe "normal! od\<C-P>" |
| 1323 | call assert_equal('def', getline(2)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1324 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1325 | set complete& |
| 1326 | %bw! |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1327 | endfunc |
| 1328 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1329 | " Test for completing whole lines from unloaded buffers |
| 1330 | func Test_complete_wholeline_unloadedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1331 | call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D') |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1332 | edit Xfile1 |
| 1333 | enew |
| 1334 | set complete=u |
| 1335 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1336 | call assert_equal('a line2', getline(1)) |
| 1337 | %d |
| 1338 | " completing from an unlisted buffer should fail |
| 1339 | bdel Xfile1 |
| 1340 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1341 | call assert_equal('a', getline(1)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1342 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1343 | set complete& |
| 1344 | %bw! |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1345 | endfunc |
| 1346 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1347 | " Test for completing words from unlisted buffers |
| 1348 | func Test_complete_from_unlistedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1349 | call writefile(['abc'], "Xfile1", 'D') |
| 1350 | call writefile(['def'], "Xfile2", 'D') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1351 | edit Xfile1 |
| 1352 | edit Xfile2 |
| 1353 | new | close |
| 1354 | bdel Xfile1 Xfile2 |
| 1355 | set complete=U |
| 1356 | " complete from an unlisted buffer |
| 1357 | exe "normal! ia\<C-P>" |
| 1358 | call assert_equal('abc', getline(1)) |
| 1359 | exe "normal! od\<C-P>" |
| 1360 | call assert_equal('def', getline(2)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1361 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1362 | set complete& |
| 1363 | %bw! |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1364 | endfunc |
| 1365 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1366 | " Test for completing whole lines from unlisted buffers |
| 1367 | func Test_complete_wholeline_unlistedbuf() |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1368 | call writefile(['a line1', 'a line2', 'a line3'], "Xfile1", 'D') |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1369 | edit Xfile1 |
| 1370 | enew |
| 1371 | set complete=U |
zeertzjq | c029c13 | 2024-03-28 11:37:26 +0100 | [diff] [blame] | 1372 | " completing from an unloaded buffer should fail |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1373 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1374 | call assert_equal('a', getline(1)) |
| 1375 | %d |
| 1376 | bdel Xfile1 |
| 1377 | exe "normal! ia\<C-X>\<C-L>\<C-P>" |
| 1378 | call assert_equal('a line2', getline(1)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1379 | |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1380 | set complete& |
| 1381 | %bw! |
Yegappan Lakshmanan | e982586 | 2022-01-03 11:03:48 +0000 | [diff] [blame] | 1382 | endfunc |
| 1383 | |
| 1384 | " Test for adding a multibyte character using CTRL-L in completion mode |
| 1385 | func Test_complete_mbyte_char_add() |
| 1386 | new |
| 1387 | set complete=. |
| 1388 | call setline(1, 'abė') |
| 1389 | exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>" |
| 1390 | call assert_equal('abė', getline(2)) |
| 1391 | " Test for a leader with multibyte character |
| 1392 | %d |
| 1393 | call setline(1, 'abėĕ') |
| 1394 | exe "normal! oabė\<C-P>" |
| 1395 | call assert_equal('abėĕ', getline(2)) |
| 1396 | bw! |
| 1397 | endfunc |
| 1398 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1399 | " Test for using <C-X><C-P> for local expansion even if 'complete' is set to |
| 1400 | " not to complete matches from the local buffer. Also test using multiple |
| 1401 | " <C-X> to cancel the current completion mode. |
| 1402 | func Test_complete_local_expansion() |
| 1403 | new |
| 1404 | set complete=t |
| 1405 | call setline(1, ['abc', 'def']) |
| 1406 | exe "normal! Go\<C-X>\<C-P>" |
| 1407 | call assert_equal("def", getline(3)) |
| 1408 | exe "normal! Go\<C-P>" |
| 1409 | call assert_equal("", getline(4)) |
| 1410 | exe "normal! Go\<C-X>\<C-N>" |
| 1411 | call assert_equal("abc", getline(5)) |
| 1412 | exe "normal! Go\<C-N>" |
| 1413 | call assert_equal("", getline(6)) |
| 1414 | |
| 1415 | " use multiple <C-X> to cancel the previous completion mode |
| 1416 | exe "normal! Go\<C-P>\<C-X>\<C-P>" |
| 1417 | call assert_equal("", getline(7)) |
| 1418 | exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>" |
| 1419 | call assert_equal("", getline(8)) |
| 1420 | exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>" |
| 1421 | call assert_equal("abc", getline(9)) |
| 1422 | |
| 1423 | " interrupt the current completion mode |
| 1424 | set completeopt=menu,noinsert |
| 1425 | exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>" |
| 1426 | call assert_equal("abc", getline(10)) |
| 1427 | |
| 1428 | " when only one <C-X> is used to interrupt, do normal expansion |
| 1429 | exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>" |
| 1430 | call assert_equal("", getline(11)) |
| 1431 | set completeopt& |
| 1432 | |
| 1433 | " using two <C-X> in non-completion mode and restarting the same mode |
| 1434 | exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>" |
| 1435 | call assert_equal("def", getline(12)) |
| 1436 | |
| 1437 | " test for adding a match from the original empty text |
| 1438 | %d |
| 1439 | call setline(1, 'abc def g') |
| 1440 | exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>" |
| 1441 | call assert_equal('def', getline(2)) |
| 1442 | exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>" |
| 1443 | call assert_equal('abc', getline(2)) |
| 1444 | |
| 1445 | bw! |
| 1446 | endfunc |
| 1447 | |
| 1448 | " Test for undoing changes after a insert-mode completion |
| 1449 | func Test_complete_undo() |
| 1450 | new |
| 1451 | set complete=. |
| 1452 | " undo with 'ignorecase' |
| 1453 | call setline(1, ['ABOVE', 'BELOW']) |
| 1454 | set ignorecase |
| 1455 | exe "normal! Goab\<C-G>u\<C-P>" |
| 1456 | call assert_equal("ABOVE", getline(3)) |
| 1457 | undo |
| 1458 | call assert_equal("ab", getline(3)) |
| 1459 | set ignorecase& |
| 1460 | %d |
| 1461 | " undo with longest match |
| 1462 | set completeopt=menu,longest |
| 1463 | call setline(1, ['above', 'about']) |
| 1464 | exe "normal! Goa\<C-G>u\<C-P>" |
| 1465 | call assert_equal("abo", getline(3)) |
| 1466 | undo |
| 1467 | call assert_equal("a", getline(3)) |
| 1468 | set completeopt& |
| 1469 | %d |
| 1470 | " undo for line completion |
| 1471 | call setline(1, ['above that change', 'below that change']) |
| 1472 | exe "normal! Goabove\<C-G>u\<C-X>\<C-L>" |
| 1473 | call assert_equal("above that change", getline(3)) |
| 1474 | undo |
| 1475 | call assert_equal("above", getline(3)) |
| 1476 | |
| 1477 | bw! |
| 1478 | endfunc |
| 1479 | |
| 1480 | " Test for completing a very long word |
| 1481 | func Test_complete_long_word() |
| 1482 | set complete& |
| 1483 | new |
| 1484 | call setline(1, repeat('x', 950) .. ' one two three') |
| 1485 | exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1486 | call assert_equal(repeat('x', 950) .. ' one two three', getline(2)) |
| 1487 | %d |
| 1488 | " should fail when more than 950 characters are in a word |
| 1489 | call setline(1, repeat('x', 951) .. ' one two three') |
| 1490 | exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" |
| 1491 | call assert_equal(repeat('x', 951), getline(2)) |
| 1492 | |
| 1493 | " Test for adding a very long word to an existing completion |
| 1494 | %d |
| 1495 | call setline(1, ['abc', repeat('x', 1016) .. '012345']) |
| 1496 | exe "normal! Goab\<C-P>\<C-X>\<C-P>" |
| 1497 | call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3)) |
| 1498 | bw! |
| 1499 | endfunc |
| 1500 | |
| 1501 | " Test for some fields in the complete items used by complete() |
| 1502 | func Test_complete_items() |
| 1503 | func CompleteItems(idx) |
| 1504 | let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}], |
| 1505 | \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}], |
| 1506 | \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}], |
| 1507 | \ [#{user_data: 'u9'}], |
| 1508 | \ [#{word: "", user_data: 'u10'}], |
| 1509 | \ [#{word: "", empty: 1, user_data: 'u11'}]] |
| 1510 | call complete(col('.'), items[a:idx]) |
| 1511 | return '' |
| 1512 | endfunc |
| 1513 | new |
| 1514 | exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>" |
| 1515 | call assert_equal('u2', v:completed_item.user_data) |
| 1516 | call assert_equal('one', getline(1)) |
| 1517 | exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>" |
| 1518 | call assert_equal('u3', v:completed_item.user_data) |
| 1519 | call assert_equal('one', getline(2)) |
| 1520 | exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>" |
| 1521 | call assert_equal('', getline(3)) |
| 1522 | set completeopt=menu,noinsert |
| 1523 | exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>" |
| 1524 | call assert_equal('oNE', getline(4)) |
| 1525 | call assert_equal('u8', v:completed_item.user_data) |
| 1526 | set completeopt& |
| 1527 | exe "normal! o\<C-R>=CompleteItems(3)\<CR>" |
| 1528 | call assert_equal('', getline(5)) |
| 1529 | exe "normal! o\<C-R>=CompleteItems(4)\<CR>" |
| 1530 | call assert_equal('', getline(6)) |
| 1531 | exe "normal! o\<C-R>=CompleteItems(5)\<CR>" |
| 1532 | call assert_equal('', getline(7)) |
| 1533 | call assert_equal('u11', v:completed_item.user_data) |
| 1534 | " pass invalid argument to complete() |
| 1535 | let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>" |
| 1536 | call assert_fails('exe cmd', 'E730:') |
| 1537 | bw! |
| 1538 | delfunc CompleteItems |
| 1539 | endfunc |
| 1540 | |
| 1541 | " Test for the "refresh" item in the dict returned by an insert completion |
| 1542 | " function |
| 1543 | func Test_complete_item_refresh_always() |
| 1544 | let g:CallCount = 0 |
| 1545 | func! Tcomplete(findstart, base) |
| 1546 | if a:findstart |
| 1547 | " locate the start of the word |
| 1548 | let line = getline('.') |
| 1549 | let start = col('.') - 1 |
| 1550 | while start > 0 && line[start - 1] =~ '\a' |
| 1551 | let start -= 1 |
| 1552 | endwhile |
| 1553 | return start |
| 1554 | else |
| 1555 | let g:CallCount += 1 |
| 1556 | let res = ["update1", "update12", "update123"] |
| 1557 | return #{words: res, refresh: 'always'} |
| 1558 | endif |
| 1559 | endfunc |
| 1560 | new |
| 1561 | set completeopt=menu,longest |
| 1562 | set completefunc=Tcomplete |
| 1563 | exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>" |
| 1564 | call assert_equal('up', getline(1)) |
Luca Saccarola | 959ef61 | 2024-12-01 16:25:53 +0100 | [diff] [blame] | 1565 | call assert_equal(6, g:CallCount) |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1566 | set completeopt& |
| 1567 | set completefunc& |
| 1568 | bw! |
| 1569 | delfunc Tcomplete |
| 1570 | endfunc |
| 1571 | |
| 1572 | " Test for completing from a thesaurus file without read permission |
| 1573 | func Test_complete_unreadable_thesaurus_file() |
| 1574 | CheckUnix |
| 1575 | CheckNotRoot |
| 1576 | |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1577 | call writefile(['about', 'above'], 'Xunrfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 1578 | call setfperm('Xunrfile', '---r--r--') |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1579 | new |
| 1580 | set complete=sXfile |
| 1581 | exe "normal! ia\<C-P>" |
| 1582 | call assert_equal('a', getline(1)) |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 1583 | |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1584 | bw! |
Yegappan Lakshmanan | 3707914 | 2022-01-08 10:38:48 +0000 | [diff] [blame] | 1585 | set complete& |
| 1586 | endfunc |
| 1587 | |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1588 | " Test to ensure 'Scanning...' messages are not recorded in messages history |
| 1589 | func Test_z1_complete_no_history() |
| 1590 | new |
| 1591 | messages clear |
| 1592 | let currmess = execute('messages') |
| 1593 | setlocal dictionary=README.txt |
| 1594 | exe "normal owh\<C-X>\<C-K>" |
| 1595 | exe "normal owh\<C-N>" |
| 1596 | call assert_equal(currmess, execute('messages')) |
Bram Moolenaar | d979d64 | 2022-03-04 14:51:06 +0000 | [diff] [blame] | 1597 | bwipe! |
| 1598 | endfunc |
| 1599 | |
| 1600 | " A mapping is not used for the key after CTRL-X. |
| 1601 | func Test_no_mapping_for_ctrl_x_key() |
| 1602 | new |
zeertzjq | 75f4baf | 2022-09-24 14:08:23 +0100 | [diff] [blame] | 1603 | inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR> |
Bram Moolenaar | d979d64 | 2022-03-04 14:51:06 +0000 | [diff] [blame] | 1604 | setlocal dictionary=README.txt |
| 1605 | call feedkeys("aexam\<C-X>\<C-K> ", 'xt') |
| 1606 | call assert_equal('example ', getline(1)) |
| 1607 | call assert_false(exists('was_mapped')) |
| 1608 | bwipe! |
Bram Moolenaar | cc23358 | 2020-12-12 13:32:07 +0100 | [diff] [blame] | 1609 | endfunc |
| 1610 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1611 | " Test for different ways of setting the 'completefunc' option |
| 1612 | func Test_completefunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1613 | func CompleteFunc1(callnr, findstart, base) |
| 1614 | call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base]) |
| 1615 | return a:findstart ? 0 : [] |
| 1616 | endfunc |
| 1617 | func CompleteFunc2(findstart, base) |
| 1618 | call add(g:CompleteFunc2Args, [a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1619 | return a:findstart ? 0 : [] |
| 1620 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1621 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1622 | let lines =<< trim END |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1623 | #" Test for using a global function name |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1624 | LET &completefunc = 'g:CompleteFunc2' |
| 1625 | new |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1626 | call setline(1, 'global') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1627 | LET g:CompleteFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1628 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1629 | call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1630 | bw! |
| 1631 | |
| 1632 | #" Test for using a function() |
| 1633 | set completefunc=function('g:CompleteFunc1',\ [10]) |
| 1634 | new |
| 1635 | call setline(1, 'one') |
| 1636 | LET g:CompleteFunc1Args = [] |
| 1637 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1638 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1639 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1640 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1641 | #" Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1642 | VAR Fn = function('g:CompleteFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1643 | LET &completefunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1644 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1645 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1646 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1647 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1648 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1649 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1650 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1651 | #" Using string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1652 | LET Fn = function('g:CompleteFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1653 | LET &completefunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1654 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1655 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1656 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1657 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1658 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1659 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1660 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1661 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1662 | set completefunc=funcref('g:CompleteFunc1',\ [13]) |
| 1663 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1664 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1665 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1666 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1667 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1668 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1669 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1670 | #" Using a funcref variable to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1671 | LET Fn = funcref('g:CompleteFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1672 | LET &completefunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1673 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1674 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1675 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1676 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1677 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1678 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1679 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1680 | #" Using a string(funcref_variable) to set 'completefunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1681 | LET Fn = funcref('g:CompleteFunc1', [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1682 | LET &completefunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1683 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1684 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1685 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1686 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1687 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1688 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1689 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1690 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1691 | VAR optval = "LSTART a, b LMIDDLE g:CompleteFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1692 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1693 | exe "set completefunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1694 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1695 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1696 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1697 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1698 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1699 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1700 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1701 | #" Set 'completefunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1702 | LET &completefunc = LSTART a, b LMIDDLE g:CompleteFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1703 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1704 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1705 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1706 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1707 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1708 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1709 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1710 | #" Set 'completefunc' to string(lambda_expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1711 | LET &completefunc = 'LSTART a, b LMIDDLE g:CompleteFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1712 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1713 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1714 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1715 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1716 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1717 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1718 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1719 | #" Set 'completefunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1720 | VAR Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1721 | LET &completefunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1722 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1723 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1724 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1725 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1726 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1727 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1728 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1729 | #" Set 'completefunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1730 | LET Lambda = LSTART a, b LMIDDLE g:CompleteFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1731 | LET &completefunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1732 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1733 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1734 | LET g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1735 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1736 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1737 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1738 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1739 | #" Test for using a lambda function with incorrect return value |
| 1740 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 1741 | LET &completefunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1742 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1743 | call setline(1, 'eight') |
| 1744 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1745 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1746 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1747 | #" Test for clearing the 'completefunc' option |
| 1748 | set completefunc='' |
| 1749 | set completefunc& |
| 1750 | call assert_fails("set completefunc=function('abc')", "E700:") |
| 1751 | call assert_fails("set completefunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1752 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1753 | #" set 'completefunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1754 | set completefunc=g:CompleteFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1755 | call setline(1, 'five') |
| 1756 | call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') |
| 1757 | call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1758 | LET g:CompleteFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1759 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1760 | call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1761 | bw! |
| 1762 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1763 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1764 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1765 | " Test for using a script-local function name |
| 1766 | func s:CompleteFunc3(findstart, base) |
| 1767 | call add(g:CompleteFunc3Args, [a:findstart, a:base]) |
| 1768 | return a:findstart ? 0 : [] |
| 1769 | endfunc |
| 1770 | set completefunc=s:CompleteFunc3 |
| 1771 | new |
| 1772 | call setline(1, 'script1') |
| 1773 | let g:CompleteFunc3Args = [] |
| 1774 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1775 | call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args) |
| 1776 | bw! |
| 1777 | |
| 1778 | let &completefunc = 's:CompleteFunc3' |
| 1779 | new |
| 1780 | call setline(1, 'script2') |
| 1781 | let g:CompleteFunc3Args = [] |
| 1782 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1783 | call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args) |
| 1784 | bw! |
| 1785 | delfunc s:CompleteFunc3 |
| 1786 | |
Bram Moolenaar | 1fca5f3 | 2022-02-18 17:50:47 +0000 | [diff] [blame] | 1787 | " In Vim9 script s: can be omitted |
| 1788 | let lines =<< trim END |
| 1789 | vim9script |
| 1790 | var CompleteFunc4Args = [] |
| 1791 | def CompleteFunc4(findstart: bool, base: string): any |
| 1792 | add(CompleteFunc4Args, [findstart, base]) |
| 1793 | return findstart ? 0 : [] |
| 1794 | enddef |
| 1795 | set completefunc=CompleteFunc4 |
| 1796 | new |
| 1797 | setline(1, 'script1') |
| 1798 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1799 | assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args) |
| 1800 | bw! |
| 1801 | END |
| 1802 | call v9.CheckScriptSuccess(lines) |
| 1803 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1804 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1805 | let &completefunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1806 | call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1807 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1808 | " Using Vim9 lambda expression in legacy context should fail |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1809 | set completefunc=(a,\ b)\ =>\ g:CompleteFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1810 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1811 | let g:CompleteFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1812 | 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] | 1813 | call assert_equal([], g:CompleteFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1814 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1815 | " set 'completefunc' to a partial with dict. This used to cause a crash. |
| 1816 | func SetCompleteFunc() |
| 1817 | let params = {'complete': function('g:DictCompleteFunc')} |
| 1818 | let &completefunc = params.complete |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1819 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1820 | func g:DictCompleteFunc(_) dict |
| 1821 | endfunc |
| 1822 | call SetCompleteFunc() |
| 1823 | new |
| 1824 | call SetCompleteFunc() |
| 1825 | bw |
| 1826 | call test_garbagecollect_now() |
| 1827 | new |
| 1828 | set completefunc= |
| 1829 | wincmd w |
| 1830 | set completefunc= |
| 1831 | %bw! |
| 1832 | delfunc g:DictCompleteFunc |
| 1833 | delfunc SetCompleteFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1834 | |
| 1835 | " Vim9 tests |
| 1836 | let lines =<< trim END |
| 1837 | vim9script |
| 1838 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1839 | def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any |
| 1840 | add(g:Vim9completeFuncArgs, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1841 | return findstart ? 0 : [] |
| 1842 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1843 | |
| 1844 | # Test for using a def function with completefunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1845 | set completefunc=function('Vim9CompleteFunc',\ [60]) |
| 1846 | new | only |
| 1847 | setline(1, 'one') |
| 1848 | g:Vim9completeFuncArgs = [] |
| 1849 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1850 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) |
| 1851 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1852 | |
| 1853 | # Test for using a global function name |
| 1854 | &completefunc = g:CompleteFunc2 |
| 1855 | new | only |
| 1856 | setline(1, 'two') |
| 1857 | g:CompleteFunc2Args = [] |
| 1858 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1859 | assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args) |
| 1860 | bw! |
| 1861 | |
| 1862 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1863 | def LocalCompleteFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1864 | add(g:LocalCompleteFuncArgs, [findstart, base]) |
| 1865 | return findstart ? 0 : [] |
| 1866 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 1867 | &completefunc = LocalCompleteFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 1868 | new | only |
| 1869 | setline(1, 'three') |
| 1870 | g:LocalCompleteFuncArgs = [] |
| 1871 | feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') |
| 1872 | assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs) |
| 1873 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1874 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1875 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 1876 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1877 | " cleanup |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1878 | set completefunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1879 | delfunc CompleteFunc1 |
| 1880 | delfunc CompleteFunc2 |
| 1881 | unlet g:CompleteFunc1Args g:CompleteFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1882 | %bw! |
| 1883 | endfunc |
| 1884 | |
| 1885 | " Test for different ways of setting the 'omnifunc' option |
| 1886 | func Test_omnifunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1887 | func OmniFunc1(callnr, findstart, base) |
| 1888 | call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base]) |
| 1889 | return a:findstart ? 0 : [] |
| 1890 | endfunc |
| 1891 | func OmniFunc2(findstart, base) |
| 1892 | call add(g:OmniFunc2Args, [a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1893 | return a:findstart ? 0 : [] |
| 1894 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 1895 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1896 | let lines =<< trim END |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1897 | #" Test for using a function name |
| 1898 | LET &omnifunc = 'g:OmniFunc2' |
| 1899 | new |
| 1900 | call setline(1, 'zero') |
| 1901 | LET g:OmniFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1902 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1903 | call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args) |
| 1904 | bw! |
| 1905 | |
| 1906 | #" Test for using a function() |
| 1907 | set omnifunc=function('g:OmniFunc1',\ [10]) |
| 1908 | new |
| 1909 | call setline(1, 'one') |
| 1910 | LET g:OmniFunc1Args = [] |
| 1911 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 1912 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1913 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1914 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1915 | #" Using a funcref variable to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1916 | VAR Fn = function('g:OmniFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1917 | LET &omnifunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1918 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1919 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1920 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1921 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1922 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1923 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1924 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1925 | #" Using a string(funcref_variable) to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1926 | LET Fn = function('g:OmniFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1927 | LET &omnifunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1928 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1929 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1930 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1931 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1932 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1933 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1934 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1935 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1936 | set omnifunc=funcref('g:OmniFunc1',\ [13]) |
| 1937 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1938 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1939 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1940 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1941 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1942 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1943 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1944 | #" Use let to set 'omnifunc' to a funcref |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1945 | LET Fn = funcref('g:OmniFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1946 | LET &omnifunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1947 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1948 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1949 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1950 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1951 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1952 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1953 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1954 | #" Using a string(funcref) to set 'omnifunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1955 | LET Fn = funcref("g:OmniFunc1", [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1956 | LET &omnifunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1957 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1958 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1959 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1960 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1961 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1962 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1963 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1964 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1965 | VAR optval = "LSTART a, b LMIDDLE g:OmniFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1966 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 1967 | exe "set omnifunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1968 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1969 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1970 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1971 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1972 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1973 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1974 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1975 | #" Set 'omnifunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1976 | LET &omnifunc = LSTART a, b LMIDDLE g:OmniFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1977 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1978 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1979 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1980 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1981 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1982 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 1983 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1984 | #" Set 'omnifunc' to a string(lambda_expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1985 | LET &omnifunc = 'LSTART a, b LMIDDLE g:OmniFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1986 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1987 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1988 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1989 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1990 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1991 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 1992 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1993 | #" Set 'omnifunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1994 | VAR Lambda = LSTART a, b LMIDDLE g:OmniFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1995 | LET &omnifunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1996 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1997 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 1998 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 1999 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2000 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2001 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2002 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2003 | #" Set 'omnifunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2004 | LET Lambda = LSTART a, b LMIDDLE g:OmniFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2005 | LET &omnifunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2006 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2007 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2008 | LET g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2009 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2010 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2011 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2012 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2013 | #" Test for using a lambda function with incorrect return value |
| 2014 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 2015 | LET &omnifunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2016 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2017 | call setline(1, 'eight') |
| 2018 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 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 clearing the 'omnifunc' option |
| 2022 | set omnifunc='' |
| 2023 | set omnifunc& |
| 2024 | call assert_fails("set omnifunc=function('abc')", "E700:") |
| 2025 | call assert_fails("set omnifunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2026 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2027 | #" set 'omnifunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 2028 | set omnifunc=g:OmniFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2029 | call setline(1, 'nine') |
| 2030 | call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') |
| 2031 | call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2032 | LET g:OmniFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2033 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2034 | call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2035 | bw! |
| 2036 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2037 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2038 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2039 | " Test for using a script-local function name |
| 2040 | func s:OmniFunc3(findstart, base) |
| 2041 | call add(g:OmniFunc3Args, [a:findstart, a:base]) |
| 2042 | return a:findstart ? 0 : [] |
| 2043 | endfunc |
| 2044 | set omnifunc=s:OmniFunc3 |
| 2045 | new |
| 2046 | call setline(1, 'script1') |
| 2047 | let g:OmniFunc3Args = [] |
| 2048 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 2049 | call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args) |
| 2050 | bw! |
| 2051 | |
| 2052 | let &omnifunc = 's:OmniFunc3' |
| 2053 | new |
| 2054 | call setline(1, 'script2') |
| 2055 | let g:OmniFunc3Args = [] |
| 2056 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 2057 | call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args) |
| 2058 | bw! |
| 2059 | delfunc s:OmniFunc3 |
| 2060 | |
| 2061 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2062 | let &omnifunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2063 | call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 2064 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2065 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2066 | set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2067 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2068 | let g:OmniFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2069 | 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] | 2070 | call assert_equal([], g:OmniFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2071 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2072 | " set 'omnifunc' to a partial with dict. This used to cause a crash. |
| 2073 | func SetOmniFunc() |
| 2074 | let params = {'omni': function('g:DictOmniFunc')} |
| 2075 | let &omnifunc = params.omni |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2076 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2077 | func g:DictOmniFunc(_) dict |
| 2078 | endfunc |
| 2079 | call SetOmniFunc() |
| 2080 | new |
| 2081 | call SetOmniFunc() |
| 2082 | bw |
| 2083 | call test_garbagecollect_now() |
| 2084 | new |
| 2085 | set omnifunc= |
| 2086 | wincmd w |
| 2087 | set omnifunc= |
| 2088 | %bw! |
| 2089 | delfunc g:DictOmniFunc |
| 2090 | delfunc SetOmniFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2091 | |
| 2092 | " Vim9 tests |
| 2093 | let lines =<< trim END |
| 2094 | vim9script |
| 2095 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2096 | def Vim9omniFunc(callnr: number, findstart: number, base: string): any |
| 2097 | add(g:Vim9omniFunc_Args, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2098 | return findstart ? 0 : [] |
| 2099 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2100 | |
| 2101 | # Test for using a def function with omnifunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2102 | set omnifunc=function('Vim9omniFunc',\ [60]) |
| 2103 | new | only |
| 2104 | setline(1, 'one') |
| 2105 | g:Vim9omniFunc_Args = [] |
| 2106 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 2107 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) |
| 2108 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2109 | |
| 2110 | # Test for using a global function name |
| 2111 | &omnifunc = g:OmniFunc2 |
| 2112 | new | only |
| 2113 | setline(1, 'two') |
| 2114 | g:OmniFunc2Args = [] |
| 2115 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 2116 | assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args) |
| 2117 | bw! |
| 2118 | |
| 2119 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2120 | def LocalOmniFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2121 | add(g:LocalOmniFuncArgs, [findstart, base]) |
| 2122 | return findstart ? 0 : [] |
| 2123 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2124 | &omnifunc = LocalOmniFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2125 | new | only |
| 2126 | setline(1, 'three') |
| 2127 | g:LocalOmniFuncArgs = [] |
| 2128 | feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') |
| 2129 | assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs) |
| 2130 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2131 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2132 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 4dc24eb | 2021-12-07 12:23:57 +0000 | [diff] [blame] | 2133 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2134 | " cleanup |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2135 | set omnifunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2136 | delfunc OmniFunc1 |
| 2137 | delfunc OmniFunc2 |
| 2138 | unlet g:OmniFunc1Args g:OmniFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2139 | %bw! |
| 2140 | endfunc |
| 2141 | |
| 2142 | " Test for different ways of setting the 'thesaurusfunc' option |
| 2143 | func Test_thesaurusfunc_callback() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2144 | func TsrFunc1(callnr, findstart, base) |
| 2145 | call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base]) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2146 | return a:findstart ? 0 : [] |
| 2147 | endfunc |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2148 | func TsrFunc2(findstart, base) |
| 2149 | call add(g:TsrFunc2Args, [a:findstart, a:base]) |
| 2150 | return a:findstart ? 0 : ['sunday'] |
| 2151 | endfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2152 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2153 | let lines =<< trim END |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2154 | #" Test for using a function name |
| 2155 | LET &thesaurusfunc = 'g:TsrFunc2' |
| 2156 | new |
| 2157 | call setline(1, 'zero') |
| 2158 | LET g:TsrFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2159 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2160 | call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args) |
| 2161 | bw! |
| 2162 | |
| 2163 | #" Test for using a function() |
| 2164 | set thesaurusfunc=function('g:TsrFunc1',\ [10]) |
| 2165 | new |
| 2166 | call setline(1, 'one') |
| 2167 | LET g:TsrFunc1Args = [] |
| 2168 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2169 | call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2170 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2171 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2172 | #" Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2173 | VAR Fn = function('g:TsrFunc1', [11]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2174 | LET &thesaurusfunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2175 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2176 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2177 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2178 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2179 | call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2180 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2181 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2182 | #" Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2183 | LET Fn = function('g:TsrFunc1', [12]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2184 | LET &thesaurusfunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2185 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2186 | call setline(1, 'two') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2187 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2188 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2189 | call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2190 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2191 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2192 | #" Test for using a funcref() |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2193 | set thesaurusfunc=funcref('g:TsrFunc1',\ [13]) |
| 2194 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2195 | call setline(1, 'three') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2196 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2197 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2198 | call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2199 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2200 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2201 | #" Using a funcref variable to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2202 | LET Fn = funcref('g:TsrFunc1', [14]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2203 | LET &thesaurusfunc = Fn |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2204 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2205 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2206 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2207 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2208 | call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2209 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2210 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2211 | #" Using a string(funcref_variable) to set 'thesaurusfunc' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2212 | LET Fn = funcref('g:TsrFunc1', [15]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2213 | LET &thesaurusfunc = string(Fn) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2214 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2215 | call setline(1, 'four') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2216 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2217 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2218 | call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2219 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2220 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2221 | #" Test for using a lambda function |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2222 | VAR optval = "LSTART a, b LMIDDLE g:TsrFunc1(16, a, b) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2223 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 2224 | exe "set thesaurusfunc=" .. optval |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2225 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2226 | call setline(1, 'five') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2227 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2228 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2229 | call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2230 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2231 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2232 | #" Test for using a lambda function with set |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2233 | LET &thesaurusfunc = LSTART a, b LMIDDLE g:TsrFunc1(17, a, b) LEND |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2234 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2235 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2236 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2237 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2238 | call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2239 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2240 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2241 | #" Set 'thesaurusfunc' to a string(lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2242 | LET &thesaurusfunc = 'LSTART a, b LMIDDLE g:TsrFunc1(18, a, b) LEND' |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2243 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2244 | call setline(1, 'six') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2245 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2246 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2247 | call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2248 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2249 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2250 | #" Set 'thesaurusfunc' to a variable with a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2251 | VAR Lambda = LSTART a, b LMIDDLE g:TsrFunc1(19, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2252 | LET &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2253 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2254 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2255 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2256 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2257 | call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2258 | bw! |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2259 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2260 | #" Set 'thesaurusfunc' to a string(variable with a lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2261 | LET Lambda = LSTART a, b LMIDDLE g:TsrFunc1(20, a, b) LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2262 | LET &thesaurusfunc = string(Lambda) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2263 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2264 | call setline(1, 'seven') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2265 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2266 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2267 | call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2268 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2269 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2270 | #" Test for using a lambda function with incorrect return value |
| 2271 | LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND |
| 2272 | LET &thesaurusfunc = Lambda |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2273 | new |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2274 | call setline(1, 'eight') |
| 2275 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2276 | bw! |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2277 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2278 | #" Test for clearing the 'thesaurusfunc' option |
| 2279 | set thesaurusfunc='' |
| 2280 | set thesaurusfunc& |
| 2281 | call assert_fails("set thesaurusfunc=function('abc')", "E700:") |
| 2282 | call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2283 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2284 | #" set 'thesaurusfunc' to a non-existing function |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 2285 | set thesaurusfunc=g:TsrFunc2 |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2286 | call setline(1, 'ten') |
| 2287 | call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') |
| 2288 | call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2289 | LET g:TsrFunc2Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2290 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2291 | call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2292 | bw! |
| 2293 | |
| 2294 | #" Use a buffer-local value and a global value |
| 2295 | set thesaurusfunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2296 | setlocal thesaurusfunc=function('g:TsrFunc1',\ [22]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2297 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2298 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2299 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 2300 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2301 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2302 | new |
| 2303 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2304 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2305 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 2306 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2307 | call assert_equal([], g:TsrFunc1Args) |
| 2308 | set thesaurusfunc=function('g:TsrFunc1',\ [23]) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2309 | wincmd w |
| 2310 | call setline(1, 'sun') |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2311 | LET g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2312 | call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") |
| 2313 | call assert_equal('sun', getline(1)) |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2314 | call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2315 | :%bw! |
| 2316 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2317 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2318 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2319 | " Test for using a script-local function name |
| 2320 | func s:TsrFunc3(findstart, base) |
| 2321 | call add(g:TsrFunc3Args, [a:findstart, a:base]) |
| 2322 | return a:findstart ? 0 : [] |
| 2323 | endfunc |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 2324 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2325 | set tsrfu=s:TsrFunc3 |
| 2326 | new |
| 2327 | call setline(1, 'script1') |
| 2328 | let g:TsrFunc3Args = [] |
| 2329 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2330 | call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args) |
| 2331 | bw! |
| 2332 | |
| 2333 | let &tsrfu = 's:TsrFunc3' |
| 2334 | new |
| 2335 | call setline(1, 'script2') |
| 2336 | let g:TsrFunc3Args = [] |
| 2337 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2338 | call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args) |
| 2339 | bw! |
zeertzjq | 6eda269 | 2024-11-03 09:23:33 +0100 | [diff] [blame] | 2340 | |
| 2341 | new | only |
| 2342 | set thesaurusfunc= |
| 2343 | setlocal thesaurusfunc=NoSuchFunc |
| 2344 | setglobal thesaurusfunc=s:TsrFunc3 |
| 2345 | call assert_equal('NoSuchFunc', &thesaurusfunc) |
| 2346 | call assert_equal('NoSuchFunc', &l:thesaurusfunc) |
| 2347 | call assert_equal('s:TsrFunc3', &g:thesaurusfunc) |
| 2348 | new | only |
| 2349 | call assert_equal('s:TsrFunc3', &thesaurusfunc) |
| 2350 | call assert_equal('s:TsrFunc3', &g:thesaurusfunc) |
| 2351 | call assert_equal('', &l:thesaurusfunc) |
| 2352 | call setline(1, 'script1') |
| 2353 | let g:TsrFunc3Args = [] |
| 2354 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2355 | call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args) |
| 2356 | bw! |
| 2357 | |
| 2358 | new | only |
| 2359 | set thesaurusfunc= |
| 2360 | setlocal thesaurusfunc=NoSuchFunc |
| 2361 | set thesaurusfunc=s:TsrFunc3 |
| 2362 | call assert_equal('s:TsrFunc3', &thesaurusfunc) |
| 2363 | call assert_equal('s:TsrFunc3', &g:thesaurusfunc) |
| 2364 | call assert_equal('', &l:thesaurusfunc) |
| 2365 | call setline(1, 'script1') |
| 2366 | let g:TsrFunc3Args = [] |
| 2367 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2368 | call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args) |
| 2369 | setlocal bufhidden=wipe |
| 2370 | new | only! |
| 2371 | call assert_equal('s:TsrFunc3', &thesaurusfunc) |
| 2372 | call assert_equal('s:TsrFunc3', &g:thesaurusfunc) |
| 2373 | call assert_equal('', &l:thesaurusfunc) |
| 2374 | call setline(1, 'script1') |
| 2375 | let g:TsrFunc3Args = [] |
| 2376 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2377 | call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args) |
| 2378 | bw! |
| 2379 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2380 | delfunc s:TsrFunc3 |
| 2381 | |
| 2382 | " invalid return value |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 2383 | let &thesaurusfunc = {a -> 'abc'} |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2384 | call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2385 | |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2386 | " Using Vim9 lambda expression in legacy context should fail |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2387 | set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2388 | new | only |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2389 | let g:TsrFunc1Args = [] |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2390 | 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] | 2391 | call assert_equal([], g:TsrFunc1Args) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2392 | bw! |
| 2393 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2394 | " set 'thesaurusfunc' to a partial with dict. This used to cause a crash. |
| 2395 | func SetTsrFunc() |
| 2396 | let params = {'thesaurus': function('g:DictTsrFunc')} |
| 2397 | let &thesaurusfunc = params.thesaurus |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2398 | endfunc |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2399 | func g:DictTsrFunc(_) dict |
| 2400 | endfunc |
| 2401 | call SetTsrFunc() |
| 2402 | new |
| 2403 | call SetTsrFunc() |
| 2404 | bw |
| 2405 | call test_garbagecollect_now() |
| 2406 | new |
| 2407 | set thesaurusfunc= |
| 2408 | wincmd w |
| 2409 | %bw! |
| 2410 | delfunc SetTsrFunc |
| 2411 | |
| 2412 | " set buffer-local 'thesaurusfunc' to a partial with dict. This used to |
| 2413 | " cause a crash. |
| 2414 | func SetLocalTsrFunc() |
| 2415 | let params = {'thesaurus': function('g:DictTsrFunc')} |
| 2416 | let &l:thesaurusfunc = params.thesaurus |
| 2417 | endfunc |
| 2418 | call SetLocalTsrFunc() |
| 2419 | call test_garbagecollect_now() |
| 2420 | call SetLocalTsrFunc() |
| 2421 | set thesaurusfunc= |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2422 | bw! |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 2423 | delfunc g:DictTsrFunc |
| 2424 | delfunc SetLocalTsrFunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2425 | |
| 2426 | " Vim9 tests |
| 2427 | let lines =<< trim END |
| 2428 | vim9script |
| 2429 | |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2430 | def Vim9tsrFunc(callnr: number, findstart: number, base: string): any |
| 2431 | add(g:Vim9tsrFunc_Args, [callnr, findstart, base]) |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2432 | return findstart ? 0 : [] |
| 2433 | enddef |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2434 | |
| 2435 | # Test for using a def function with thesaurusfunc |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2436 | set thesaurusfunc=function('Vim9tsrFunc',\ [60]) |
| 2437 | new | only |
| 2438 | setline(1, 'one') |
| 2439 | g:Vim9tsrFunc_Args = [] |
| 2440 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2441 | assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args) |
| 2442 | bw! |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2443 | |
| 2444 | # Test for using a global function name |
| 2445 | &thesaurusfunc = g:TsrFunc2 |
| 2446 | new | only |
| 2447 | setline(1, 'two') |
| 2448 | g:TsrFunc2Args = [] |
| 2449 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2450 | assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args) |
| 2451 | bw! |
| 2452 | |
| 2453 | # Test for using a script-local function name |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2454 | def LocalTsrFunc(findstart: number, base: string): any |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2455 | add(g:LocalTsrFuncArgs, [findstart, base]) |
| 2456 | return findstart ? 0 : [] |
| 2457 | enddef |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 2458 | &thesaurusfunc = LocalTsrFunc |
Yegappan Lakshmanan | db1a410 | 2021-12-17 16:21:20 +0000 | [diff] [blame] | 2459 | new | only |
| 2460 | setline(1, 'three') |
| 2461 | g:LocalTsrFuncArgs = [] |
| 2462 | feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') |
| 2463 | assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs) |
| 2464 | bw! |
Yegappan Lakshmanan | 2172bff | 2021-12-08 10:46:21 +0000 | [diff] [blame] | 2465 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2466 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2467 | |
| 2468 | " cleanup |
| 2469 | set thesaurusfunc& |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 2470 | delfunc TsrFunc1 |
| 2471 | delfunc TsrFunc2 |
| 2472 | unlet g:TsrFunc1Args g:TsrFunc2Args |
Yegappan Lakshmanan | 8658c75 | 2021-12-03 11:09:29 +0000 | [diff] [blame] | 2473 | %bw! |
| 2474 | endfunc |
| 2475 | |
Christian Brabandt | ac72c21 | 2022-04-07 21:00:53 +0100 | [diff] [blame] | 2476 | func FooBarComplete(findstart, base) |
| 2477 | if a:findstart |
| 2478 | return col('.') - 1 |
| 2479 | else |
| 2480 | return ["Foo", "Bar", "}"] |
| 2481 | endif |
| 2482 | endfunc |
| 2483 | |
| 2484 | func Test_complete_smartindent() |
| 2485 | new |
| 2486 | setlocal smartindent completefunc=FooBarComplete |
| 2487 | |
| 2488 | exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>" |
| 2489 | let result = getline(1,'$') |
| 2490 | call assert_equal(['', '{','}',''], result) |
| 2491 | bw! |
| 2492 | delfunction! FooBarComplete |
| 2493 | endfunc |
| 2494 | |
Bram Moolenaar | f12129f | 2022-07-01 19:58:30 +0100 | [diff] [blame] | 2495 | func Test_complete_overrun() |
| 2496 | " this was going past the end of the copied text |
| 2497 | new |
| 2498 | sil norm si0s0 |
| 2499 | bwipe! |
| 2500 | endfunc |
| 2501 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2502 | func Test_infercase_very_long_line() |
| 2503 | " this was truncating the line when inferring case |
| 2504 | new |
| 2505 | let longLine = "blah "->repeat(300) |
| 2506 | let verylongLine = "blah "->repeat(400) |
| 2507 | call setline(1, verylongLine) |
| 2508 | call setline(2, longLine) |
| 2509 | set ic infercase |
| 2510 | exe "normal 2Go\<C-X>\<C-L>\<Esc>" |
| 2511 | call assert_equal(longLine, getline(3)) |
| 2512 | |
Bram Moolenaar | b9e7173 | 2022-07-23 06:53:08 +0100 | [diff] [blame] | 2513 | " check that the too long text is NUL terminated |
| 2514 | %del |
| 2515 | norm o |
| 2516 | norm 1987ax |
| 2517 | exec "norm ox\<C-X>\<C-L>" |
| 2518 | call assert_equal(repeat('x', 1987), getline(3)) |
| 2519 | |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2520 | bwipe! |
| 2521 | set noic noinfercase |
| 2522 | endfunc |
| 2523 | |
Bram Moolenaar | baefde1 | 2022-07-07 19:59:49 +0100 | [diff] [blame] | 2524 | func Test_ins_complete_add() |
| 2525 | " this was reading past the end of allocated memory |
| 2526 | new |
| 2527 | norm o |
| 2528 | norm 7o |
| 2529 | sil! norm o |
| 2530 | |
| 2531 | bwipe! |
| 2532 | endfunc |
| 2533 | |
Bram Moolenaar | a6f9e30 | 2022-07-28 21:51:37 +0100 | [diff] [blame] | 2534 | func Test_ins_complete_end_of_line() |
| 2535 | " this was reading past the end of the line |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 2536 | new |
Bram Moolenaar | a6f9e30 | 2022-07-28 21:51:37 +0100 | [diff] [blame] | 2537 | norm 8oý |
| 2538 | sil! norm o |
| 2539 | |
| 2540 | bwipe! |
| 2541 | endfunc |
Bram Moolenaar | caea664 | 2022-07-07 19:42:04 +0100 | [diff] [blame] | 2542 | |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 2543 | func s:Tagfunc(t,f,o) |
| 2544 | bwipe! |
| 2545 | return [] |
| 2546 | endfunc |
| 2547 | |
| 2548 | " This was using freed memory, since 'complete' was in a wiped out buffer. |
| 2549 | " Also using a window that was closed. |
| 2550 | func Test_tagfunc_wipes_out_buffer() |
| 2551 | new |
| 2552 | set complete=.,t,w,b,u,i |
| 2553 | se tagfunc=s:Tagfunc |
| 2554 | sil norm i |
| 2555 | |
| 2556 | bwipe! |
| 2557 | endfunc |
| 2558 | |
Bram Moolenaar | 6ac2e43 | 2023-03-31 19:32:29 +0100 | [diff] [blame] | 2559 | func Test_ins_complete_popup_position() |
| 2560 | CheckScreendump |
| 2561 | |
| 2562 | let lines =<< trim END |
| 2563 | vim9script |
| 2564 | set nowrap |
| 2565 | setline(1, ['one', 'two', 'this is line ', 'four']) |
| 2566 | prop_type_add('test', {highlight: 'Error'}) |
| 2567 | prop_add(3, 0, { |
| 2568 | text_align: 'above', |
| 2569 | text: 'The quick brown fox jumps over the lazy dog', |
| 2570 | type: 'test' |
| 2571 | }) |
| 2572 | END |
| 2573 | call writefile(lines, 'XinsPopup', 'D') |
| 2574 | let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10}) |
| 2575 | |
| 2576 | call term_sendkeys(buf, "3GA\<C-N>") |
| 2577 | call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {}) |
| 2578 | |
| 2579 | call StopVimInTerminal(buf) |
| 2580 | endfunc |
| 2581 | |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2582 | func GetCompleteInfo() |
| 2583 | let g:compl_info = complete_info() |
| 2584 | return '' |
| 2585 | endfunc |
| 2586 | |
nwounkn | 2e3cd52 | 2023-10-17 11:05:38 +0200 | [diff] [blame] | 2587 | func Test_completion_restart() |
| 2588 | new |
| 2589 | set complete=. completeopt=menuone backspace=2 |
| 2590 | call setline(1, 'workhorse workhorse') |
| 2591 | exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>" |
| 2592 | call assert_equal(1, len(g:compl_info['items'])) |
| 2593 | call assert_equal('workhorse', g:compl_info['items'][0]['word']) |
| 2594 | set complete& completeopt& backspace& |
| 2595 | bwipe! |
| 2596 | endfunc |
| 2597 | |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2598 | func Test_complete_info_index() |
| 2599 | new |
| 2600 | call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"]) |
| 2601 | inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR> |
| 2602 | |
| 2603 | " Ensure 'index' in complete_info() is coherent with the 'items' array. |
| 2604 | |
| 2605 | set completeopt=menu,preview |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2606 | " Search forward |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2607 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2608 | 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] | 2609 | call assert_equal(6 , len(g:compl_info['items'])) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2610 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2611 | 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] | 2612 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2613 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2614 | call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2615 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2616 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2617 | call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2618 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2619 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2620 | call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2621 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2622 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2623 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2624 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2625 | " Search forward: unselected item |
| 2626 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2627 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2628 | call assert_equal(-1 , g:compl_info['selected']) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2629 | |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2630 | " Search backward |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2631 | call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2632 | 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] | 2633 | call assert_equal(6 , len(g:compl_info['items'])) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2634 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2635 | 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] | 2636 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2637 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2638 | call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2639 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2640 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2641 | call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2642 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2643 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2644 | call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2645 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2646 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2647 | call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2648 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2649 | " search backwards: unselected item |
| 2650 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2651 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2652 | call assert_equal(-1 , g:compl_info['selected']) |
| 2653 | |
| 2654 | " switch direction: forwards, then backwards |
| 2655 | call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2656 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2657 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2658 | " switch direction: forwards, then backwards, then forwards again |
| 2659 | call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2660 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2661 | call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2662 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2663 | |
| 2664 | " switch direction: backwards, then forwards |
| 2665 | call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2666 | call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2667 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2668 | " switch direction: backwards, then forwards, then backwards again |
| 2669 | call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2670 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2671 | call assert_equal(6 , len(g:compl_info['items'])) |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2672 | |
| 2673 | " Add 'noselect', check that 'selected' is -1 when nothing is selected. |
| 2674 | set completeopt+=noselect |
| 2675 | " Search forward. |
| 2676 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2677 | call assert_equal(-1, g:compl_info['selected']) |
| 2678 | |
| 2679 | " Search backward. |
| 2680 | call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx') |
| 2681 | call assert_equal(-1, g:compl_info['selected']) |
| 2682 | |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2683 | 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] | 2684 | call assert_equal(5, g:compl_info['selected']) |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2685 | call assert_equal(6 , len(g:compl_info['items'])) |
| 2686 | call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) |
| 2687 | 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] | 2688 | 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] | 2689 | call assert_equal(6 , len(g:compl_info['items'])) |
Christian Brabandt | 57f9ce1 | 2023-11-04 09:58:14 +0100 | [diff] [blame] | 2690 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') |
| 2691 | call assert_equal(-1, g:compl_info['selected']) |
| 2692 | call assert_equal(6 , len(g:compl_info['items'])) |
Christian Brabandt | daef8c7 | 2023-10-27 19:16:26 +0200 | [diff] [blame] | 2693 | |
LemonBoy | 69fb5af | 2023-10-11 21:55:56 +0200 | [diff] [blame] | 2694 | set completeopt& |
| 2695 | bwipe! |
| 2696 | endfunc |
Bram Moolenaar | 0ff0183 | 2022-09-24 19:20:30 +0100 | [diff] [blame] | 2697 | |
Christian Brabandt | fef6630 | 2024-01-29 21:46:58 +0100 | [diff] [blame] | 2698 | func Test_complete_changed_complete_info() |
| 2699 | CheckRunVimInTerminal |
| 2700 | " this used to crash vim, see #13929 |
| 2701 | let lines =<< trim END |
| 2702 | set completeopt=menuone |
| 2703 | autocmd CompleteChanged * call complete_info(['items']) |
| 2704 | call feedkeys("iii\<cr>\<c-p>") |
| 2705 | END |
| 2706 | call writefile(lines, 'Xsegfault', 'D') |
| 2707 | let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5}) |
| 2708 | call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000) |
| 2709 | call StopVimInTerminal(buf) |
| 2710 | endfunc |
| 2711 | |
zeertzjq | 0a419e0 | 2024-04-02 19:01:14 +0200 | [diff] [blame] | 2712 | func Test_completefunc_first_call_complete_add() |
| 2713 | new |
| 2714 | |
| 2715 | func Complete(findstart, base) abort |
| 2716 | if a:findstart |
| 2717 | let col = col('.') |
| 2718 | call complete_add('#') |
| 2719 | return col - 1 |
| 2720 | else |
| 2721 | return [] |
| 2722 | endif |
| 2723 | endfunc |
| 2724 | |
| 2725 | set completeopt=longest completefunc=Complete |
| 2726 | " This used to cause heap-buffer-overflow |
| 2727 | call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:') |
| 2728 | |
| 2729 | delfunc Complete |
| 2730 | set completeopt& completefunc& |
| 2731 | bwipe! |
| 2732 | endfunc |
| 2733 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2734 | func Test_complete_opt_fuzzy() |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2735 | func OnPumChange() |
| 2736 | let g:item = get(v:event, 'completed_item', {}) |
| 2737 | let g:word = get(g:item, 'word', v:null) |
glepnir | c0b7ca4 | 2025-02-13 20:27:44 +0100 | [diff] [blame] | 2738 | let g:abbr = get(g:item, 'abbr', v:null) |
| 2739 | let g:selected = get(complete_info(['selected']), 'selected') |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2740 | endfunction |
| 2741 | |
| 2742 | augroup AAAAA_Group |
| 2743 | au! |
| 2744 | autocmd CompleteChanged * :call OnPumChange() |
| 2745 | augroup END |
| 2746 | |
| 2747 | func Omni_test(findstart, base) |
| 2748 | if a:findstart |
| 2749 | return col(".") |
| 2750 | endif |
glepnir | f400a0c | 2025-01-23 19:55:14 +0100 | [diff] [blame] | 2751 | return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}, #{word: "你好吗"}, #{word: "我好"}] |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2752 | endfunc |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2753 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2754 | new |
| 2755 | set omnifunc=Omni_test |
| 2756 | set completeopt+=noinsert,fuzzy |
| 2757 | call feedkeys("Gi\<C-x>\<C-o>", 'tx') |
| 2758 | call assert_equal('foo', g:word) |
| 2759 | call feedkeys("S\<C-x>\<C-o>fb", 'tx') |
| 2760 | call assert_equal('fooBaz', g:word) |
| 2761 | call feedkeys("S\<C-x>\<C-o>fa", 'tx') |
| 2762 | call assert_equal('foobar', g:word) |
| 2763 | " select next |
| 2764 | call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx') |
| 2765 | call assert_equal('foobar', g:word) |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2766 | " can cyclically select next |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2767 | call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx') |
| 2768 | call assert_equal(v:null, g:word) |
| 2769 | " select prev |
| 2770 | call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx') |
| 2771 | call assert_equal(v:null, g:word) |
zeertzjq | 551d8c3 | 2024-06-05 19:53:32 +0200 | [diff] [blame] | 2772 | " can cyclically select prev |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2773 | call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx') |
| 2774 | call assert_equal('fooBaz', g:word) |
| 2775 | |
glepnir | f94c9c4 | 2024-06-14 21:11:56 +0200 | [diff] [blame] | 2776 | func Comp() |
| 2777 | call complete(col('.'), ["fooBaz", "foobar", "foobala"]) |
| 2778 | return '' |
| 2779 | endfunc |
| 2780 | call feedkeys("i\<C-R>=Comp()\<CR>", 'tx') |
| 2781 | call assert_equal('fooBaz', g:word) |
| 2782 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2783 | " respect noselect |
| 2784 | set completeopt+=noselect |
| 2785 | call feedkeys("S\<C-x>\<C-o>fb", 'tx') |
| 2786 | call assert_equal(v:null, g:word) |
| 2787 | call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx') |
| 2788 | call assert_equal('fooBaz', g:word) |
| 2789 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2790 | " test case for nosort option |
| 2791 | set cot=menuone,menu,noinsert,fuzzy,nosort |
| 2792 | " "fooBaz" should have a higher score when the leader is "fb". |
| 2793 | " With "nosort", "foobar" should still be shown first in the popup menu. |
| 2794 | call feedkeys("S\<C-x>\<C-o>fb", 'tx') |
| 2795 | call assert_equal('foobar', g:word) |
| 2796 | call feedkeys("S\<C-x>\<C-o>好", 'tx') |
| 2797 | call assert_equal("你好吗", g:word) |
| 2798 | |
| 2799 | set cot+=noselect |
| 2800 | call feedkeys("S\<C-x>\<C-o>好", 'tx') |
| 2801 | call assert_equal(v:null, g:word) |
| 2802 | call feedkeys("S\<C-x>\<C-o>好\<C-N>", 'tx') |
| 2803 | call assert_equal('你好吗', g:word) |
| 2804 | |
| 2805 | " "nosort" shouldn't enable fuzzy filtering when "fuzzy" isn't present. |
| 2806 | set cot=menuone,noinsert,nosort |
| 2807 | call feedkeys("S\<C-x>\<C-o>fooB\<C-Y>", 'tx') |
| 2808 | call assert_equal('fooBaz', getline('.')) |
| 2809 | |
| 2810 | set cot=menuone,fuzzy,nosort |
| 2811 | func CompAnother() |
| 2812 | call complete(col('.'), [#{word: "do" }, #{word: "echo"}, #{word: "for (${1:expr1}, ${2:expr2}, ${3:expr3}) {\n\t$0\n}", abbr: "for" }, #{word: "foo"}]) |
| 2813 | return '' |
| 2814 | endfunc |
| 2815 | call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>", 'tx') |
| 2816 | call assert_equal("for", g:abbr) |
| 2817 | call assert_equal(2, g:selected) |
| 2818 | |
| 2819 | set cot+=noinsert |
| 2820 | call feedkeys("i\<C-R>=CompAnother()\<CR>f", 'tx') |
| 2821 | call assert_equal("for", g:abbr) |
| 2822 | call assert_equal(2, g:selected) |
| 2823 | |
| 2824 | set cot=menu,menuone,noselect,fuzzy |
| 2825 | call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>\<C-N>\<C-N>", 'tx') |
| 2826 | call assert_equal("foo", g:word) |
| 2827 | call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-P>", 'tx') |
| 2828 | call assert_equal("foo", g:word) |
| 2829 | call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-P>\<C-P>", 'tx') |
| 2830 | call assert_equal("for", g:abbr) |
| 2831 | |
| 2832 | " clean up |
| 2833 | set omnifunc= |
| 2834 | bw! |
| 2835 | set complete& completeopt& |
| 2836 | autocmd! AAAAA_Group |
| 2837 | augroup! AAAAA_Group |
| 2838 | delfunc OnPumChange |
| 2839 | delfunc Omni_test |
| 2840 | delfunc Comp |
| 2841 | unlet g:item |
| 2842 | unlet g:word |
| 2843 | unlet g:abbr |
| 2844 | endfunc |
| 2845 | |
| 2846 | func Test_complete_fuzzy_collect() |
| 2847 | new |
| 2848 | set completefuzzycollect=keyword,files,whole_line |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 2849 | call setline(1, ['hello help hero h']) |
zeertzjq | acc8746 | 2024-06-16 08:44:05 +0200 | [diff] [blame] | 2850 | " Use "!" flag of feedkeys() so that ex_normal_busy is not set and |
| 2851 | " ins_compl_check_keys() is not skipped. |
| 2852 | " Add a "0" after the <Esc> to avoid waiting for an escape sequence. |
| 2853 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 2854 | call assert_equal('hello help hero hello', getline('.')) |
| 2855 | set completeopt+=noinsert |
| 2856 | call setline(1, ['hello help hero h']) |
zeertzjq | acc8746 | 2024-06-16 08:44:05 +0200 | [diff] [blame] | 2857 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
glepnir | aced8c2 | 2024-06-15 15:13:05 +0200 | [diff] [blame] | 2858 | call assert_equal('hello help hero h', getline('.')) |
| 2859 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 2860 | set completeopt-=noinsert |
| 2861 | call setline(1, ['xyz yxz x']) |
| 2862 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2863 | call assert_equal('xyz yxz xyz', getline('.')) |
| 2864 | " can fuzzy get yxz when use Ctrl-N twice |
| 2865 | call setline(1, ['xyz yxz x']) |
| 2866 | call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!') |
| 2867 | call assert_equal('xyz yxz yxz', getline('.')) |
| 2868 | |
| 2869 | call setline(1, ['你好 你']) |
| 2870 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2871 | call assert_equal('你好 你好', getline('.')) |
| 2872 | call setline(1, ['你的 我的 的']) |
| 2873 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2874 | call assert_equal('你的 我的 你的', getline('.')) |
| 2875 | " can fuzzy get multiple-byte word when use Ctrl-N twice |
| 2876 | call setline(1, ['你的 我的 的']) |
| 2877 | call feedkeys("A\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!') |
| 2878 | call assert_equal('你的 我的 我的', getline('.')) |
| 2879 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 2880 | " fuzzy on file |
| 2881 | call writefile([''], 'fobar', 'D') |
| 2882 | call writefile([''], 'foobar', 'D') |
| 2883 | call setline(1, ['fob']) |
| 2884 | call cursor(1, 1) |
| 2885 | call feedkeys("A\<C-X>\<C-f>\<Esc>0", 'tx!') |
| 2886 | call assert_equal('fobar', getline('.')) |
| 2887 | call feedkeys("Sfob\<C-X>\<C-f>\<C-N>\<Esc>0", 'tx!') |
| 2888 | call assert_equal('foobar', getline('.')) |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 2889 | call feedkeys("S../\<C-X>\<C-f>\<Esc>0", 'tx!') |
| 2890 | call assert_match('../*', getline('.')) |
| 2891 | call feedkeys("S../td\<C-X>\<C-f>\<Esc>0", 'tx!') |
| 2892 | call assert_match('../testdir', getline('.')) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 2893 | |
| 2894 | " can get completion from other buffer |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 2895 | vnew |
| 2896 | call setline(1, ["completeness,", "compatibility", "Composite", "Omnipotent"]) |
| 2897 | wincmd p |
| 2898 | call feedkeys("Somp\<C-N>\<Esc>0", 'tx!') |
| 2899 | call assert_equal('completeness', getline('.')) |
| 2900 | call feedkeys("Somp\<C-N>\<C-N>\<Esc>0", 'tx!') |
| 2901 | call assert_equal('compatibility', getline('.')) |
| 2902 | call feedkeys("Somp\<C-P>\<Esc>0", 'tx!') |
| 2903 | call assert_equal('Omnipotent', getline('.')) |
| 2904 | call feedkeys("Somp\<C-P>\<C-P>\<Esc>0", 'tx!') |
| 2905 | call assert_equal('Composite', getline('.')) |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 2906 | call feedkeys("S omp\<C-N>\<Esc>0", 'tx!') |
| 2907 | call assert_equal(' completeness', getline('.')) |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 2908 | |
| 2909 | " fuzzy on whole line completion |
| 2910 | call setline(1, ["world is on fire", "no one can save me but you", 'user can execute', '']) |
| 2911 | call cursor(4, 1) |
| 2912 | call feedkeys("Swio\<C-X>\<C-L>\<Esc>0", 'tx!') |
| 2913 | call assert_equal('world is on fire', getline('.')) |
| 2914 | call feedkeys("Su\<C-X>\<C-L>\<C-P>\<Esc>0", 'tx!') |
| 2915 | call assert_equal('no one can save me but you', getline('.')) |
| 2916 | |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 2917 | " issue #15412 |
| 2918 | call setline(1, ['alpha bravio charlie']) |
| 2919 | call feedkeys("Salpha\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2920 | call assert_equal('alpha bravio', getline('.')) |
| 2921 | call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2922 | call assert_equal('alpha', getline('.')) |
| 2923 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2924 | call assert_equal('alpha bravio', getline('.')) |
| 2925 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2926 | call assert_equal('alpha bravio charlie', getline('.')) |
| 2927 | |
| 2928 | set complete-=i |
| 2929 | call feedkeys("Salp\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2930 | call assert_equal('alpha', getline('.')) |
| 2931 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2932 | call assert_equal('alpha bravio', getline('.')) |
| 2933 | call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2934 | call assert_equal('alpha bravio charlie', getline('.')) |
| 2935 | |
| 2936 | call setline(1, ['alpha bravio charlie', 'alpha another']) |
| 2937 | call feedkeys("Salpha\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!') |
| 2938 | call assert_equal('alpha another', getline('.')) |
| 2939 | call setline(1, ['你好 我好', '你好 他好']) |
| 2940 | call feedkeys("S你好\<C-X>\<C-N>\<Esc>0", 'tx!') |
| 2941 | call assert_equal('你好 我好', getline('.')) |
| 2942 | call feedkeys("S你好\<C-X>\<C-N>\<C-N>\<Esc>0", 'tx!') |
| 2943 | call assert_equal('你好 他好', getline('.')) |
| 2944 | |
glepnir | 753794b | 2024-08-20 19:58:44 +0200 | [diff] [blame] | 2945 | " issue #15526 |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2946 | set completeopt=menuone,menu,noselect |
glepnir | 753794b | 2024-08-20 19:58:44 +0200 | [diff] [blame] | 2947 | call setline(1, ['Text', 'ToText', '']) |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 2948 | call cursor(3, 1) |
glepnir | 753794b | 2024-08-20 19:58:44 +0200 | [diff] [blame] | 2949 | call feedkeys("STe\<C-X>\<C-N>x\<CR>\<Esc>0", 'tx!') |
glepnir | 07f0dbe | 2025-02-18 20:27:30 +0100 | [diff] [blame] | 2950 | call assert_equal('Tex', getline(line('.') - 1)) |
glepnir | 753794b | 2024-08-20 19:58:44 +0200 | [diff] [blame] | 2951 | |
glepnir | 53b1457 | 2025-03-12 21:28:39 +0100 | [diff] [blame] | 2952 | call setline(1, ['fuzzy', 'fuzzycollect', 'completefuzzycollect']) |
| 2953 | call feedkeys("Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<CR>\<Esc>0", 'tx!') |
| 2954 | call assert_equal('fuzzycollect', getline(line('.') - 1)) |
| 2955 | call feedkeys("Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<CR>\<Esc>0", 'tx!') |
| 2956 | call assert_equal('completefuzzycollect', getline(line('.') - 1)) |
| 2957 | |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 2958 | " keywords in 'dictonary' |
| 2959 | call writefile(['hello', 'think'], 'test_dict.txt', 'D') |
| 2960 | set dict=test_dict.txt |
| 2961 | call feedkeys("Sh\<C-X>\<C-K>\<C-N>\<CR>\<Esc>0", 'tx!') |
| 2962 | call assert_equal('hello', getline(line('.') - 1)) |
| 2963 | call feedkeys("Sh\<C-X>\<C-K>\<C-N>\<C-N>\<CR>\<Esc>0", 'tx!') |
| 2964 | call assert_equal('think', getline(line('.') - 1)) |
| 2965 | |
glepnir | 5753084 | 2025-03-21 17:16:21 +0100 | [diff] [blame] | 2966 | call setline(1, ['foo bar fuzzy', 'completefuzzycollect']) |
| 2967 | call feedkeys("Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<C-Y>\<Esc>0", 'tx!') |
| 2968 | call assert_equal('completefuzzycollect', getline('.')) |
| 2969 | |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2970 | bw! |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 2971 | bw! |
glepnir | 5876016 | 2025-03-13 21:39:51 +0100 | [diff] [blame] | 2972 | set dict& |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2973 | set completeopt& cfc& cpt& |
glepnir | a218cc6 | 2024-06-03 19:32:39 +0200 | [diff] [blame] | 2974 | endfunc |
| 2975 | |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 2976 | func Test_cfc_with_longest() |
| 2977 | new |
| 2978 | set completefuzzycollect=keyword,files,whole_line |
| 2979 | set completeopt=menu,menuone,longest,fuzzy |
| 2980 | |
| 2981 | " keyword |
| 2982 | exe "normal ggdGShello helio think h\<C-X>\<C-N>\<ESC>" |
| 2983 | call assert_equal("hello helio think hel", getline('.')) |
| 2984 | exe "normal hello helio think h\<C-X>\<C-P>\<ESC>" |
| 2985 | call assert_equal("hello helio think hel", getline('.')) |
| 2986 | |
| 2987 | " skip non-consecutive prefixes |
| 2988 | exe "normal ggdGShello helio heo\<C-X>\<C-N>\<ESC>" |
| 2989 | call assert_equal("hello helio heo", getline('.')) |
| 2990 | |
| 2991 | " kdcit |
| 2992 | call writefile(['help'], 'test_keyword.txt', 'D') |
| 2993 | set complete=ktest_keyword.txt |
| 2994 | exe "normal ggdGSh\<C-N>\<ESC>" |
| 2995 | " auto insert help when only have one match |
| 2996 | call assert_equal("help", getline('.')) |
| 2997 | call writefile(['hello', 'help', 'think'], 'xtest_keyword.txt', 'D') |
| 2998 | set complete=kxtest_keyword.txt |
| 2999 | " auto insert hel |
| 3000 | exe "normal ggdGSh\<C-N>\<ESC>" |
| 3001 | call assert_equal("hel", getline('.')) |
| 3002 | |
| 3003 | " line start with a space |
| 3004 | call writefile([' hello'], 'test_case1.txt', 'D') |
| 3005 | set complete=ktest_case1.txt |
| 3006 | exe "normal ggdGSh\<C-N>\<ESC>" |
| 3007 | call assert_equal("hello", getline('.')) |
| 3008 | |
| 3009 | " multiple matches |
| 3010 | set complete=ktest_case2.txt |
| 3011 | call writefile([' hello help what'], 'test_case2.txt', 'D') |
| 3012 | exe "normal ggdGSh\<C-N>\<C-N>\<C-N>\<C-N>\<ESC>" |
| 3013 | call assert_equal("what", getline('.')) |
| 3014 | |
| 3015 | " multiple lines of matches |
| 3016 | set complete=ktest_case3.txt |
| 3017 | call writefile([' hello help what', 'hola', ' hey'], 'test_case3.txt', 'D') |
| 3018 | exe "normal ggdGSh\<C-N>\<C-N>\<ESC>" |
| 3019 | call assert_equal("hey", getline('.')) |
| 3020 | exe "normal ggdGSh\<C-N>\<C-N>\<C-N>\<C-N>\<ESC>" |
| 3021 | call assert_equal("hola", getline('.')) |
| 3022 | |
| 3023 | set complete=ktest_case4.txt |
| 3024 | call writefile([' auto int enum register', 'why'], 'test_case4.txt', 'D') |
| 3025 | exe "normal ggdGSe\<C-N>\<C-N>\<ESC>" |
| 3026 | call assert_equal("enum", getline('.')) |
glepnir | dd42b05 | 2025-03-08 16:52:55 +0100 | [diff] [blame] | 3027 | |
| 3028 | set complete=ktest_case5.txt |
| 3029 | call writefile(['hello friends', 'go', 'hero'], 'test_case5.txt', 'D') |
| 3030 | exe "normal ggdGSh\<C-N>\<C-N>\<ESC>" |
| 3031 | call assert_equal("hero", getline('.')) |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3032 | set complete& |
| 3033 | |
| 3034 | " file |
| 3035 | call writefile([''], 'hello', 'D') |
| 3036 | call writefile([''], 'helio', 'D') |
| 3037 | exe "normal ggdGS./h\<C-X>\<C-f>\<ESC>" |
| 3038 | call assert_equal('./hel', getline('.')) |
| 3039 | |
| 3040 | " word |
| 3041 | call setline(1, ['what do you think', 'why i have that', '']) |
| 3042 | call cursor(3,1) |
| 3043 | call feedkeys("Sw\<C-X>\<C-l>\<C-N>\<Esc>0", 'tx!') |
| 3044 | call assert_equal('wh', getline('.')) |
| 3045 | |
| 3046 | exe "normal ggdG" |
| 3047 | " auto complete when only one match |
| 3048 | exe "normal Shello\<CR>h\<C-X>\<C-N>\<esc>" |
| 3049 | call assert_equal('hello', getline('.')) |
| 3050 | exe "normal Sh\<C-N>\<C-P>\<esc>" |
| 3051 | call assert_equal('hello', getline('.')) |
| 3052 | |
| 3053 | exe "normal Shello\<CR>h\<C-X>\<C-N>\<Esc>cch\<C-X>\<C-N>\<Esc>" |
| 3054 | call assert_equal('hello', getline('.')) |
| 3055 | |
| 3056 | " continue search for new leader after insert common prefix |
| 3057 | exe "normal ohellokate\<CR>h\<C-X>\<C-N>k\<C-y>\<esc>" |
| 3058 | call assert_equal('hellokate', getline('.')) |
| 3059 | |
| 3060 | bw! |
| 3061 | set completeopt& |
| 3062 | set completefuzzycollect& |
| 3063 | endfunc |
| 3064 | |
| 3065 | func Test_completefuzzycollect_with_completeslash() |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3066 | CheckMSWindows |
| 3067 | |
| 3068 | call writefile([''], 'fobar', 'D') |
| 3069 | let orig_shellslash = &shellslash |
| 3070 | set cpt& |
| 3071 | new |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3072 | set completefuzzycollect=files |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3073 | set noshellslash |
| 3074 | |
| 3075 | " Test with completeslash unset |
| 3076 | set completeslash= |
| 3077 | call setline(1, ['.\fob']) |
| 3078 | call feedkeys("A\<C-X>\<C-F>\<Esc>0", 'tx!') |
| 3079 | call assert_equal('.\fobar', getline('.')) |
| 3080 | |
| 3081 | " Test with completeslash=backslash |
| 3082 | set completeslash=backslash |
| 3083 | call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!') |
| 3084 | call assert_equal('.\fobar', getline('.')) |
| 3085 | |
| 3086 | " Test with completeslash=slash |
| 3087 | set completeslash=slash |
| 3088 | call feedkeys("S.\\fob\<C-X>\<C-F>\<Esc>0", 'tx!') |
| 3089 | call assert_equal('./fobar', getline('.')) |
| 3090 | |
| 3091 | " Reset and clean up |
| 3092 | let &shellslash = orig_shellslash |
| 3093 | set completeslash= |
glepnir | f31cfa2 | 2025-03-06 21:59:13 +0100 | [diff] [blame] | 3094 | set completefuzzycollect& |
glepnir | b9de1a0 | 2024-08-02 19:14:38 +0200 | [diff] [blame] | 3095 | %bw! |
| 3096 | endfunc |
| 3097 | |
zeertzjq | 8e56747 | 2024-06-14 20:04:42 +0200 | [diff] [blame] | 3098 | " Check that tie breaking is stable for completeopt+=fuzzy (which should |
| 3099 | " behave the same on different platforms). |
| 3100 | func Test_complete_fuzzy_match_tie() |
| 3101 | new |
| 3102 | set completeopt+=fuzzy,noselect |
| 3103 | call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', '']) |
| 3104 | |
| 3105 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx') |
| 3106 | call assert_equal('aaabbccc', getline('.')) |
| 3107 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx') |
| 3108 | call assert_equal('aaabbCCC', getline('.')) |
| 3109 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx') |
| 3110 | call assert_equal('aaabbcccc', getline('.')) |
| 3111 | call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx') |
| 3112 | call assert_equal('aaabbCCCC', getline('.')) |
| 3113 | |
| 3114 | bwipe! |
| 3115 | set completeopt& |
| 3116 | endfunc |
| 3117 | |
Christian Brabandt | 13032a4 | 2024-07-28 21:16:48 +0200 | [diff] [blame] | 3118 | func Test_complete_backwards_default() |
| 3119 | new |
| 3120 | call append(1, ['foobar', 'foobaz']) |
| 3121 | new |
| 3122 | call feedkeys("i\<c-p>", 'tx') |
| 3123 | call assert_equal('foobaz', getline('.')) |
| 3124 | bw! |
| 3125 | bw! |
| 3126 | endfunc |
| 3127 | |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3128 | func Test_complete_info_matches() |
| 3129 | let g:what = ['matches'] |
| 3130 | func ShownInfo() |
| 3131 | let g:compl_info = complete_info(g:what) |
| 3132 | return '' |
| 3133 | endfunc |
| 3134 | set completeopt+=noinsert |
| 3135 | |
| 3136 | new |
| 3137 | call setline(1, ['aaa', 'aab', 'aba', 'abb']) |
| 3138 | inoremap <buffer><F5> <C-R>=ShownInfo()<CR> |
| 3139 | |
| 3140 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx') |
| 3141 | call assert_equal([ |
| 3142 | \ {'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, |
| 3143 | \ {'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, |
| 3144 | \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, |
| 3145 | \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, |
| 3146 | \], g:compl_info['matches']) |
| 3147 | |
| 3148 | call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx') |
| 3149 | call assert_equal([ |
| 3150 | \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, |
| 3151 | \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, |
| 3152 | \], g:compl_info['matches']) |
| 3153 | |
| 3154 | " items and matches both in what |
| 3155 | let g:what = ['items', 'matches'] |
| 3156 | call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx') |
| 3157 | call assert_equal([ |
| 3158 | \ {'word': 'aaa', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''}, |
| 3159 | \ {'word': 'aab', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''}, |
| 3160 | \ {'word': 'aba', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''}, |
| 3161 | \ {'word': 'abb', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''}, |
| 3162 | \], g:compl_info['items']) |
| 3163 | call assert_false(has_key(g:compl_info, 'matches')) |
| 3164 | |
| 3165 | bw! |
glepnir | d4088ed | 2024-12-31 10:55:22 +0100 | [diff] [blame] | 3166 | unlet g:what |
| 3167 | delfunc ShownInfo |
| 3168 | set cot& |
| 3169 | endfunc |
| 3170 | |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3171 | func Test_complete_info_completed() |
| 3172 | func ShownInfo() |
| 3173 | let g:compl_info = complete_info(['completed']) |
| 3174 | return '' |
| 3175 | endfunc |
| 3176 | set completeopt+=noinsert |
| 3177 | |
| 3178 | new |
| 3179 | call setline(1, ['aaa', 'aab', 'aba', 'abb']) |
| 3180 | inoremap <buffer><F5> <C-R>=ShownInfo()<CR> |
| 3181 | |
| 3182 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx') |
| 3183 | call assert_equal({'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed']) |
| 3184 | |
| 3185 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx') |
| 3186 | call assert_equal({'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed']) |
| 3187 | |
| 3188 | call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx') |
| 3189 | call assert_equal({'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed']) |
| 3190 | |
| 3191 | set completeopt+=noselect |
| 3192 | call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx') |
| 3193 | call assert_equal({}, g:compl_info) |
| 3194 | |
| 3195 | bw! |
glepnir | 037b028 | 2025-01-16 14:37:44 +0100 | [diff] [blame] | 3196 | delfunc ShownInfo |
| 3197 | set cot& |
| 3198 | endfunc |
| 3199 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3200 | function Test_completeopt_preinsert() |
| 3201 | func Omni_test(findstart, base) |
| 3202 | if a:findstart |
| 3203 | return col(".") |
| 3204 | endif |
| 3205 | return [#{word: "fobar"}, #{word: "foobar"}, #{word: "你的"}, #{word: "你好世界"}] |
| 3206 | endfunc |
| 3207 | set omnifunc=Omni_test |
| 3208 | set completeopt=menu,menuone,preinsert |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3209 | func GetLine() |
| 3210 | let g:line = getline('.') |
| 3211 | let g:col = col('.') |
| 3212 | endfunc |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3213 | |
| 3214 | new |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3215 | inoremap <buffer><F5> <C-R>=GetLine()<CR> |
| 3216 | call feedkeys("S\<C-X>\<C-O>f\<F5>\<ESC>", 'tx') |
| 3217 | call assert_equal("fobar", g:line) |
| 3218 | call assert_equal(2, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3219 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3220 | call feedkeys("S\<C-X>\<C-O>foo\<F5><ESC>", 'tx') |
| 3221 | call assert_equal("foobar", g:line) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3222 | |
| 3223 | call feedkeys("S\<C-X>\<C-O>foo\<BS>\<BS>\<BS>", 'tx') |
| 3224 | call assert_equal("", getline('.')) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3225 | |
| 3226 | " delete a character and input new leader |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3227 | call feedkeys("S\<C-X>\<C-O>foo\<BS>b\<F5>\<ESC>", 'tx') |
| 3228 | call assert_equal("fobar", g:line) |
| 3229 | call assert_equal(4, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3230 | |
| 3231 | " delete preinsert when prepare completion |
| 3232 | call feedkeys("S\<C-X>\<C-O>f\<Space>", 'tx') |
| 3233 | call assert_equal("f ", getline('.')) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3234 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3235 | call feedkeys("S\<C-X>\<C-O>你\<F5>\<ESC>", 'tx') |
| 3236 | call assert_equal("你的", g:line) |
| 3237 | call assert_equal(4, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3238 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3239 | call feedkeys("S\<C-X>\<C-O>你好\<F5>\<ESC>", 'tx') |
| 3240 | call assert_equal("你好世界", g:line) |
| 3241 | call assert_equal(7, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3242 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3243 | call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f\<F5>\<ESC>", 'tx') |
| 3244 | call assert_equal("hello fobar wo", g:line) |
| 3245 | call assert_equal(9, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3246 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3247 | call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f\<BS>\<F5>\<ESC>", 'tx') |
| 3248 | call assert_equal("hello wo", g:line) |
| 3249 | call assert_equal(8, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3250 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3251 | call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo\<F5>\<ESC>", 'tx') |
| 3252 | call assert_equal("hello foobar wo", g:line) |
| 3253 | call assert_equal(11, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3254 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3255 | call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo\<BS>b\<F5>\<ESC>", 'tx') |
| 3256 | call assert_equal("hello fobar wo", g:line) |
| 3257 | call assert_equal(11, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3258 | |
zeertzjq | 8297e2c | 2025-01-30 11:04:47 +0100 | [diff] [blame] | 3259 | " confirm |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3260 | call feedkeys("S\<C-X>\<C-O>f\<C-Y>", 'tx') |
| 3261 | call assert_equal("fobar", getline('.')) |
| 3262 | call assert_equal(5, col('.')) |
| 3263 | |
| 3264 | " cancel |
| 3265 | call feedkeys("S\<C-X>\<C-O>fo\<C-E>", 'tx') |
| 3266 | call assert_equal("fo", getline('.')) |
| 3267 | call assert_equal(2, col('.')) |
| 3268 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3269 | call feedkeys("S hello hero\<CR>h\<C-X>\<C-N>\<F5>\<ESC>", 'tx') |
| 3270 | call assert_equal("hello", g:line) |
| 3271 | call assert_equal(2, col('.')) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3272 | |
| 3273 | call feedkeys("Sh\<C-X>\<C-N>\<C-Y>", 'tx') |
| 3274 | call assert_equal("hello", getline('.')) |
| 3275 | call assert_equal(5, col('.')) |
| 3276 | |
| 3277 | " delete preinsert part |
| 3278 | call feedkeys("S\<C-X>\<C-O>fo ", 'tx') |
| 3279 | call assert_equal("fo ", getline('.')) |
| 3280 | call assert_equal(3, col('.')) |
| 3281 | |
glepnir | 001c26c | 2025-02-02 09:36:22 +0100 | [diff] [blame] | 3282 | call feedkeys("She\<C-X>\<C-N>\<C-U>", 'tx') |
| 3283 | call assert_equal("", getline('.')) |
| 3284 | call assert_equal(1, col('.')) |
| 3285 | |
| 3286 | call feedkeys("She\<C-X>\<C-N>\<C-W>", 'tx') |
| 3287 | call assert_equal("", getline('.')) |
| 3288 | call assert_equal(1, col('.')) |
| 3289 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3290 | " whole line |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3291 | call feedkeys("Shello hero\<CR>\<C-X>\<C-L>\<F5>\<ESC>", 'tx') |
| 3292 | call assert_equal("hello hero", g:line) |
| 3293 | call assert_equal(1, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3294 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3295 | call feedkeys("Shello hero\<CR>he\<C-X>\<C-L>\<F5>\<ESC>", 'tx') |
| 3296 | call assert_equal("hello hero", g:line) |
| 3297 | call assert_equal(3, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3298 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3299 | call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>er\<F5>\<ESC>", 'tx') |
| 3300 | call assert_equal("hero", g:line) |
| 3301 | call assert_equal(4, g:col) |
glepnir | bfb4eea | 2025-01-31 15:28:29 +0100 | [diff] [blame] | 3302 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3303 | " can not work with fuzzy |
| 3304 | set cot+=fuzzy |
| 3305 | call feedkeys("S\<C-X>\<C-O>", 'tx') |
| 3306 | call assert_equal("fobar", getline('.')) |
| 3307 | call assert_equal(5, col('.')) |
| 3308 | |
| 3309 | " test for fuzzy and noinsert |
| 3310 | set cot+=noinsert |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3311 | call feedkeys("S\<C-X>\<C-O>fb\<F5>\<ESC>", 'tx') |
| 3312 | call assert_equal("fb", g:line) |
| 3313 | call assert_equal(3, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3314 | |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3315 | call feedkeys("S\<C-X>\<C-O>你\<F5>\<ESC>", 'tx') |
| 3316 | call assert_equal("你", g:line) |
| 3317 | call assert_equal(4, g:col) |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3318 | |
| 3319 | call feedkeys("S\<C-X>\<C-O>fb\<C-Y>", 'tx') |
| 3320 | call assert_equal("fobar", getline('.')) |
| 3321 | call assert_equal(5, col('.')) |
| 3322 | |
glepnir | a2c5559 | 2025-02-28 17:43:42 +0100 | [diff] [blame] | 3323 | " When the pum is not visible, the preinsert has no effect |
glepnir | 52fd867 | 2025-02-20 22:13:24 +0100 | [diff] [blame] | 3324 | set cot=preinsert |
| 3325 | call feedkeys("Sfoo1 foo2\<CR>f\<C-X>\<C-N>bar", 'tx') |
glepnir | a2c5559 | 2025-02-28 17:43:42 +0100 | [diff] [blame] | 3326 | call assert_equal("foo1bar", getline('.')) |
| 3327 | call assert_equal(7, col('.')) |
glepnir | 52fd867 | 2025-02-20 22:13:24 +0100 | [diff] [blame] | 3328 | |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 3329 | set cot=preinsert,menuone |
glepnir | 84a7503 | 2025-03-15 09:59:22 +0100 | [diff] [blame] | 3330 | call feedkeys("Sfoo1 foo2\<CR>f\<C-X>\<C-N>\<F5>\<ESC>", 'tx') |
| 3331 | call assert_equal("foo1", g:line) |
| 3332 | call assert_equal(2, g:col) |
| 3333 | |
| 3334 | inoremap <buffer> <f3> <cmd>call complete(4, [{'word': "fobar"}, {'word': "foobar"}])<CR> |
| 3335 | call feedkeys("Swp.\<F3>\<F5>\<BS>\<ESC>", 'tx') |
| 3336 | call assert_equal("wp.fobar", g:line) |
| 3337 | call assert_equal(4, g:col) |
| 3338 | call assert_equal("wp.", getline('.')) |
glepnir | 94a045e | 2025-03-01 16:12:23 +0100 | [diff] [blame] | 3339 | |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3340 | bw! |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3341 | set cot& |
| 3342 | set omnifunc& |
| 3343 | delfunc Omni_test |
glepnir | edd4ac3 | 2025-01-29 18:53:51 +0100 | [diff] [blame] | 3344 | endfunc |
| 3345 | |
zeertzjq | 060e655 | 2025-02-21 20:06:26 +0100 | [diff] [blame] | 3346 | " Check that mark positions are correct after triggering multiline completion. |
| 3347 | func Test_complete_multiline_marks() |
| 3348 | func Omni_test(findstart, base) |
| 3349 | if a:findstart |
| 3350 | return col(".") |
| 3351 | endif |
| 3352 | return [ |
| 3353 | \ #{word: "func ()\n\t\nend"}, |
| 3354 | \ #{word: "foobar"}, |
| 3355 | \ #{word: "你好\n\t\n我好"} |
| 3356 | \ ] |
| 3357 | endfunc |
| 3358 | set omnifunc=Omni_test |
| 3359 | |
| 3360 | new |
| 3361 | let lines = mapnew(range(10), 'string(v:val)') |
| 3362 | call setline(1, lines) |
| 3363 | call setpos("'a", [0, 3, 1, 0]) |
| 3364 | |
| 3365 | call feedkeys("A \<C-X>\<C-O>\<C-E>\<BS>", 'tx') |
| 3366 | call assert_equal(lines, getline(1, '$')) |
| 3367 | call assert_equal([0, 3, 1, 0], getpos("'a")) |
| 3368 | |
| 3369 | call feedkeys("A \<C-X>\<C-O>\<C-N>\<C-E>\<BS>", 'tx') |
| 3370 | call assert_equal(lines, getline(1, '$')) |
| 3371 | call assert_equal([0, 3, 1, 0], getpos("'a")) |
| 3372 | |
| 3373 | call feedkeys("A \<C-X>\<C-O>\<C-N>\<C-N>\<C-E>\<BS>", 'tx') |
| 3374 | call assert_equal(lines, getline(1, '$')) |
| 3375 | call assert_equal([0, 3, 1, 0], getpos("'a")) |
| 3376 | |
| 3377 | call feedkeys("A \<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-E>\<BS>", 'tx') |
| 3378 | call assert_equal(lines, getline(1, '$')) |
| 3379 | call assert_equal([0, 3, 1, 0], getpos("'a")) |
| 3380 | |
| 3381 | call feedkeys("A \<C-X>\<C-O>\<C-Y>", 'tx') |
| 3382 | call assert_equal(['0 func ()', "\t", 'end'] + lines[1:], getline(1, '$')) |
| 3383 | call assert_equal([0, 5, 1, 0], getpos("'a")) |
| 3384 | |
| 3385 | bw! |
| 3386 | set omnifunc& |
| 3387 | delfunc Omni_test |
| 3388 | endfunc |
| 3389 | |
Christian Brabandt | 71f17fd | 2025-03-31 20:57:13 +0200 | [diff] [blame^] | 3390 | func Test_complete_append_selected_match_default() |
| 3391 | " when typing a normal character during completion, |
| 3392 | " completion is ended, see |
| 3393 | " :h popupmenu-completion ("There are three states:") |
| 3394 | func PrintMenuWords() |
| 3395 | let info = complete_info(["selected", "matches"]) |
| 3396 | call map(info.matches, {_, v -> v.word}) |
| 3397 | return info |
| 3398 | endfunc |
| 3399 | |
| 3400 | new |
| 3401 | call setline(1, ["fo", "foo", "foobar", "fobarbaz"]) |
| 3402 | exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" |
| 3403 | call assert_equal('fo{''matches'': [''fo'', ''foo'', ''foobar'', ''fobarbaz''], ''selected'': 0}', getline(5)) |
| 3404 | %d |
| 3405 | call setline(1, ["fo", "foo", "foobar", "fobarbaz"]) |
| 3406 | exe "normal! Gof\<c-n>o\<c-r>=PrintMenuWords()\<cr>" |
| 3407 | call assert_equal('foo{''matches'': [], ''selected'': -1}', getline(5)) |
| 3408 | %d |
| 3409 | set completeopt=menu,noselect |
| 3410 | call setline(1, ["fo", "foo", "foobar", "fobarbaz"]) |
| 3411 | exe "normal! Gof\<c-n>\<c-n>o\<c-r>=PrintMenuWords()\<cr>" |
| 3412 | call assert_equal('foo{''matches'': [], ''selected'': -1}', getline(5)) |
| 3413 | bw! |
| 3414 | |
| 3415 | set completeopt& |
| 3416 | delfunc PrintMenuWords |
| 3417 | endfunc |
| 3418 | |
Christian Brabandt | fef6630 | 2024-01-29 21:46:58 +0100 | [diff] [blame] | 3419 | " vim: shiftwidth=2 sts=2 expandtab nofoldenable |