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