Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 1 | " Test for completion menu |
| 2 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 3 | source shared.vim |
| 4 | |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 5 | let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] |
| 6 | let g:setting = '' |
| 7 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 8 | func! ListMonths() |
| 9 | if g:setting != '' |
| 10 | exe ":set" g:setting |
| 11 | endif |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 12 | let mth = copy(g:months) |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 13 | let entered = strcharpart(getline('.'),0,col('.')) |
| 14 | if !empty(entered) |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 15 | let mth = filter(mth, 'v:val=~"^".entered') |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 16 | endif |
| 17 | call complete(1, mth) |
| 18 | return '' |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 19 | endfunc |
| 20 | |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 21 | func! Test_popup_complete2() |
Bram Moolenaar | 9e02cfa | 2016-09-22 21:27:11 +0200 | [diff] [blame] | 22 | " Although the popupmenu is not visible, this does not mean completion mode |
| 23 | " has ended. After pressing <f5> to complete the currently typed char, Vim |
| 24 | " still stays in the first state of the completion (:h ins-completion-menu), |
| 25 | " although the popupmenu wasn't shown <c-e> will remove the inserted |
| 26 | " completed text (:h complete_CTRL-E), while the following <c-e> will behave |
| 27 | " like expected (:h i_CTRL-E) |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 28 | new |
| 29 | inoremap <f5> <c-r>=ListMonths()<cr> |
| 30 | call append(1, ["December2015"]) |
| 31 | :1 |
| 32 | call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx') |
Bram Moolenaar | 9e02cfa | 2016-09-22 21:27:11 +0200 | [diff] [blame] | 33 | call assert_equal(["Dece", "", "December2015"], getline(1,3)) |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 34 | %d |
| 35 | bw! |
| 36 | endfu |
| 37 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 38 | func! Test_popup_complete() |
| 39 | new |
| 40 | inoremap <f5> <c-r>=ListMonths()<cr> |
| 41 | |
| 42 | " <C-E> - select original typed text before the completion started |
| 43 | call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx') |
| 44 | call assert_equal(["Ju"], getline(1,2)) |
| 45 | %d |
| 46 | |
| 47 | " <C-Y> - accept current match |
| 48 | call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx') |
| 49 | call assert_equal(["August"], getline(1,2)) |
| 50 | %d |
| 51 | |
| 52 | " <BS> - Delete one character from the inserted text (state: 1) |
| 53 | " TODO: This should not end the completion, but it does. |
| 54 | " This should according to the documentation: |
| 55 | " January |
| 56 | " but instead, this does |
| 57 | " Januar |
| 58 | " (idea is, C-L inserts the match from the popup menu |
| 59 | " but if the menu is closed, it will insert the character <c-l> |
| 60 | call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx') |
| 61 | call assert_equal(["Januar"], getline(1,2)) |
| 62 | %d |
| 63 | |
| 64 | " any-non special character: Stop completion without changing the match |
| 65 | " and insert the typed character |
| 66 | call feedkeys("a\<f5>20", 'tx') |
| 67 | call assert_equal(["January20"], getline(1,2)) |
| 68 | %d |
| 69 | |
| 70 | " any-non printable, non-white character: Add this character and |
| 71 | " reduce number of matches |
| 72 | call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx') |
| 73 | call assert_equal(["Jul"], getline(1,2)) |
| 74 | %d |
| 75 | |
| 76 | " any-non printable, non-white character: Add this character and |
| 77 | " reduce number of matches |
| 78 | call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx') |
| 79 | call assert_equal(["July"], getline(1,2)) |
| 80 | %d |
| 81 | |
| 82 | " any-non printable, non-white character: Add this character and |
| 83 | " reduce number of matches |
| 84 | call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx') |
| 85 | call assert_equal(["Jul"], getline(1,2)) |
| 86 | %d |
| 87 | |
| 88 | " <BS> - Delete one character from the inserted text (state: 2) |
| 89 | call feedkeys("a\<f5>\<c-n>\<bs>", 'tx') |
| 90 | call assert_equal(["Februar"], getline(1,2)) |
| 91 | %d |
| 92 | |
| 93 | " <c-l> - Insert one character from the current match |
| 94 | call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx') |
| 95 | call assert_equal(["J"], getline(1,2)) |
| 96 | %d |
| 97 | |
| 98 | " <c-l> - Insert one character from the current match |
| 99 | call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx') |
| 100 | call assert_equal(["January"], getline(1,2)) |
| 101 | %d |
| 102 | |
| 103 | " <c-y> - Accept current selected match |
| 104 | call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx') |
| 105 | call assert_equal(["January"], getline(1,2)) |
| 106 | %d |
| 107 | |
| 108 | " <c-e> - End completion, go back to what was there before selecting a match |
| 109 | call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx') |
| 110 | call assert_equal(["Ju"], getline(1,2)) |
| 111 | %d |
| 112 | |
| 113 | " <PageUp> - Select a match several entries back |
| 114 | call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx') |
| 115 | call assert_equal([""], getline(1,2)) |
| 116 | %d |
| 117 | |
| 118 | " <PageUp><PageUp> - Select a match several entries back |
| 119 | call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx') |
| 120 | call assert_equal(["December"], getline(1,2)) |
| 121 | %d |
| 122 | |
| 123 | " <PageUp><PageUp><PageUp> - Select a match several entries back |
| 124 | call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx') |
| 125 | call assert_equal(["February"], getline(1,2)) |
| 126 | %d |
| 127 | |
| 128 | " <PageDown> - Select a match several entries further |
| 129 | call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx') |
| 130 | call assert_equal(["November"], getline(1,2)) |
| 131 | %d |
| 132 | |
| 133 | " <PageDown><PageDown> - Select a match several entries further |
| 134 | call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx') |
| 135 | call assert_equal(["December"], getline(1,2)) |
| 136 | %d |
| 137 | |
| 138 | " <PageDown><PageDown><PageDown> - Select a match several entries further |
| 139 | call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx') |
| 140 | call assert_equal([""], getline(1,2)) |
| 141 | %d |
| 142 | |
| 143 | " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further |
| 144 | call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx') |
| 145 | call assert_equal(["October"], getline(1,2)) |
| 146 | %d |
| 147 | |
| 148 | " <Up> - Select a match don't insert yet |
| 149 | call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx') |
| 150 | call assert_equal([""], getline(1,2)) |
| 151 | %d |
| 152 | |
| 153 | " <Up><Up> - Select a match don't insert yet |
| 154 | call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx') |
| 155 | call assert_equal(["December"], getline(1,2)) |
| 156 | %d |
| 157 | |
| 158 | " <Up><Up><Up> - Select a match don't insert yet |
| 159 | call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx') |
| 160 | call assert_equal(["November"], getline(1,2)) |
| 161 | %d |
| 162 | |
| 163 | " <Tab> - Stop completion and insert the match |
| 164 | call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx') |
| 165 | call assert_equal(["January "], getline(1,2)) |
| 166 | %d |
| 167 | |
| 168 | " <Space> - Stop completion and insert the match |
| 169 | call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx') |
| 170 | call assert_equal(["September "], getline(1,2)) |
| 171 | %d |
| 172 | |
| 173 | " <Enter> - Use the text and insert line break (state: 1) |
| 174 | call feedkeys("a\<f5>\<enter>\<esc>", 'tx') |
| 175 | call assert_equal(["January", ''], getline(1,2)) |
| 176 | %d |
| 177 | |
| 178 | " <Enter> - Insert the current selected text (state: 2) |
| 179 | call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx') |
| 180 | call assert_equal(["September"], getline(1,2)) |
| 181 | %d |
| 182 | |
| 183 | " Insert match immediately, if there is only one match |
| 184 | " <c-y> selects a character from the line above |
| 185 | call append(0, ["December2015"]) |
| 186 | call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx') |
| 187 | call assert_equal(["December2015", "December2015", ""], getline(1,3)) |
| 188 | %d |
| 189 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 190 | " use menuone for 'completeopt' |
| 191 | " Since for the first <c-y> the menu is still shown, will only select |
| 192 | " three letters from the line above |
| 193 | set completeopt&vim |
| 194 | set completeopt+=menuone |
| 195 | call append(0, ["December2015"]) |
| 196 | call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx') |
| 197 | call assert_equal(["December2015", "December201", ""], getline(1,3)) |
| 198 | %d |
| 199 | |
| 200 | " use longest for 'completeopt' |
| 201 | set completeopt&vim |
| 202 | call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx') |
| 203 | set completeopt+=longest |
| 204 | call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx') |
| 205 | call assert_equal(["M", "Ma", ""], getline(1,3)) |
| 206 | %d |
| 207 | |
| 208 | " use noselect/noinsert for 'completeopt' |
| 209 | set completeopt&vim |
| 210 | call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 211 | set completeopt+=noselect |
| 212 | call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 213 | set completeopt-=noselect completeopt+=noinsert |
| 214 | call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 215 | call assert_equal(["March", "M", "March"], getline(1,4)) |
| 216 | %d |
| 217 | endfu |
| 218 | |
| 219 | |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 220 | func! Test_popup_completion_insertmode() |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 221 | new |
| 222 | inoremap <F5> <C-R>=ListMonths()<CR> |
| 223 | |
| 224 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
| 225 | call assert_equal('February', getline(1)) |
| 226 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 227 | " Set noinsertmode |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 228 | let g:setting = 'noinsertmode' |
| 229 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
| 230 | call assert_equal('February', getline(1)) |
| 231 | call assert_false(pumvisible()) |
| 232 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 233 | " Go through all matches, until none is selected |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 234 | let g:setting = '' |
| 235 | call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx') |
| 236 | call assert_equal('', getline(1)) |
| 237 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 238 | " select previous entry |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 239 | call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx') |
| 240 | call assert_equal('', getline(1)) |
| 241 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 242 | " select last entry |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 243 | call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx') |
| 244 | call assert_equal('December', getline(1)) |
| 245 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 246 | iunmap <F5> |
| 247 | endfunc |
| 248 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 249 | func Test_noinsert_complete() |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 250 | function! s:complTest1() abort |
| 251 | call complete(1, ['source', 'soundfold']) |
| 252 | return '' |
| 253 | endfunction |
| 254 | |
| 255 | function! s:complTest2() abort |
| 256 | call complete(1, ['source', 'soundfold']) |
| 257 | return '' |
| 258 | endfunction |
| 259 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 260 | new |
| 261 | set completeopt+=noinsert |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 262 | inoremap <F5> <C-R>=s:complTest1()<CR> |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 263 | call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx') |
| 264 | call assert_equal('soundfold', getline(1)) |
| 265 | call assert_equal('soundfold', getline(2)) |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 266 | bwipe! |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 267 | |
| 268 | new |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 269 | inoremap <F5> <C-R>=s:complTest2()<CR> |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 270 | call feedkeys("i\<F5>\<CR>\<ESC>", 'tx') |
| 271 | call assert_equal('source', getline(1)) |
| 272 | bwipe! |
| 273 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 274 | set completeopt-=noinsert |
| 275 | iunmap <F5> |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 276 | endfunc |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 277 | |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 278 | func Test_compl_vim_cmds_after_register_expr() |
| 279 | function! s:test_func() |
| 280 | return 'autocmd ' |
| 281 | endfunction |
| 282 | augroup AAAAA_Group |
| 283 | au! |
| 284 | augroup END |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 285 | |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 286 | new |
| 287 | call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx') |
| 288 | call assert_equal('autocmd AAAAA_Group', getline(1)) |
| 289 | autocmd! AAAAA_Group |
| 290 | augroup! AAAAA_Group |
| 291 | bwipe! |
| 292 | endfunc |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 293 | |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 294 | func DummyCompleteOne(findstart, base) |
| 295 | if a:findstart |
| 296 | return 0 |
| 297 | else |
| 298 | wincmd n |
| 299 | return ['onedef', 'oneDEF'] |
| 300 | endif |
| 301 | endfunc |
| 302 | |
| 303 | " Test that nothing happens if the 'completefunc' opens |
| 304 | " a new window (no completion, no crash) |
| 305 | func Test_completefunc_opens_new_window_one() |
| 306 | new |
| 307 | let winid = win_getid() |
| 308 | setlocal completefunc=DummyCompleteOne |
| 309 | call setline(1, 'one') |
| 310 | /^one |
| 311 | call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:') |
| 312 | call assert_notequal(winid, win_getid()) |
| 313 | q! |
| 314 | call assert_equal(winid, win_getid()) |
| 315 | call assert_equal('', getline(1)) |
| 316 | q! |
| 317 | endfunc |
| 318 | |
| 319 | " Test that nothing happens if the 'completefunc' opens |
| 320 | " a new window (no completion, no crash) |
| 321 | func DummyCompleteTwo(findstart, base) |
| 322 | if a:findstart |
| 323 | wincmd n |
| 324 | return 0 |
| 325 | else |
| 326 | return ['twodef', 'twoDEF'] |
| 327 | endif |
| 328 | endfunction |
| 329 | |
| 330 | " Test that nothing happens if the 'completefunc' opens |
| 331 | " a new window (no completion, no crash) |
| 332 | func Test_completefunc_opens_new_window_two() |
| 333 | new |
| 334 | let winid = win_getid() |
| 335 | setlocal completefunc=DummyCompleteTwo |
| 336 | call setline(1, 'two') |
| 337 | /^two |
| 338 | call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:') |
| 339 | call assert_notequal(winid, win_getid()) |
| 340 | q! |
| 341 | call assert_equal(winid, win_getid()) |
| 342 | call assert_equal('two', getline(1)) |
| 343 | q! |
| 344 | endfunc |
| 345 | |
| 346 | func DummyCompleteThree(findstart, base) |
| 347 | if a:findstart |
| 348 | return 0 |
| 349 | else |
| 350 | return ['threedef', 'threeDEF'] |
| 351 | endif |
| 352 | endfunc |
| 353 | |
| 354 | :"Test that 'completefunc' works when it's OK. |
| 355 | func Test_completefunc_works() |
| 356 | new |
| 357 | let winid = win_getid() |
| 358 | setlocal completefunc=DummyCompleteThree |
| 359 | call setline(1, 'three') |
| 360 | /^three |
| 361 | call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x") |
| 362 | call assert_equal(winid, win_getid()) |
| 363 | call assert_equal('threeDEF', getline(1)) |
| 364 | q! |
| 365 | endfunc |
| 366 | |
| 367 | func DummyCompleteFour(findstart, base) |
| 368 | if a:findstart |
| 369 | return 0 |
| 370 | else |
| 371 | call complete_add('four1') |
| 372 | call complete_add('four2') |
| 373 | call complete_check() |
| 374 | call complete_add('four3') |
| 375 | call complete_add('four4') |
| 376 | call complete_check() |
| 377 | call complete_add('four5') |
| 378 | call complete_add('four6') |
| 379 | return [] |
| 380 | endif |
| 381 | endfunc |
| 382 | |
Bram Moolenaar | 60ef3e8 | 2016-10-29 14:37:56 +0200 | [diff] [blame] | 383 | " Test that 'omnifunc' works when it's OK. |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 384 | func Test_omnifunc_with_check() |
| 385 | new |
| 386 | setlocal omnifunc=DummyCompleteFour |
| 387 | call setline(1, 'four') |
| 388 | /^four |
| 389 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x") |
| 390 | call assert_equal('four2', getline(1)) |
| 391 | |
| 392 | call setline(1, 'four') |
| 393 | /^four |
| 394 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x") |
| 395 | call assert_equal('four3', getline(1)) |
| 396 | |
| 397 | call setline(1, 'four') |
| 398 | /^four |
| 399 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x") |
| 400 | call assert_equal('four5', getline(1)) |
| 401 | |
| 402 | q! |
| 403 | endfunc |
| 404 | |
Bram Moolenaar | 869e352 | 2016-10-16 15:35:47 +0200 | [diff] [blame] | 405 | function UndoComplete() |
| 406 | call complete(1, ['January', 'February', 'March', |
| 407 | \ 'April', 'May', 'June', 'July', 'August', 'September', |
| 408 | \ 'October', 'November', 'December']) |
| 409 | return '' |
| 410 | endfunc |
| 411 | |
| 412 | " Test that no undo item is created when no completion is inserted |
| 413 | func Test_complete_no_undo() |
| 414 | set completeopt=menu,preview,noinsert,noselect |
| 415 | inoremap <Right> <C-R>=UndoComplete()<CR> |
| 416 | new |
| 417 | call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt') |
| 418 | call feedkeys("iaaa\<Esc>0", 'xt') |
| 419 | call assert_equal('aaa', getline(2)) |
| 420 | call feedkeys("i\<Right>\<Esc>", 'xt') |
| 421 | call assert_equal('aaa', getline(2)) |
| 422 | call feedkeys("u", 'xt') |
| 423 | call assert_equal('', getline(2)) |
| 424 | |
Bram Moolenaar | cbd3bd6 | 2016-10-17 20:47:02 +0200 | [diff] [blame] | 425 | call feedkeys("ibbb\<Esc>0", 'xt') |
| 426 | call assert_equal('bbb', getline(2)) |
| 427 | call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt') |
| 428 | call assert_equal('January', getline(2)) |
| 429 | call feedkeys("u", 'xt') |
| 430 | call assert_equal('bbb', getline(2)) |
| 431 | |
Bram Moolenaar | 9ec7fa8 | 2016-10-18 13:06:41 +0200 | [diff] [blame] | 432 | call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt') |
| 433 | call assert_equal('January', getline(2)) |
| 434 | call feedkeys("u", 'xt') |
| 435 | call assert_equal('bbb', getline(2)) |
| 436 | |
Bram Moolenaar | 869e352 | 2016-10-16 15:35:47 +0200 | [diff] [blame] | 437 | iunmap <Right> |
| 438 | set completeopt& |
| 439 | q! |
| 440 | endfunc |
| 441 | |
Bram Moolenaar | 60ef3e8 | 2016-10-29 14:37:56 +0200 | [diff] [blame] | 442 | function! DummyCompleteFive(findstart, base) |
| 443 | if a:findstart |
| 444 | return 0 |
| 445 | else |
| 446 | return [ |
| 447 | \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" }, |
| 448 | \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" }, |
| 449 | \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" }, |
| 450 | \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" }, |
| 451 | \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" }, |
| 452 | \ ] |
| 453 | endif |
| 454 | endfunc |
| 455 | |
| 456 | " Test that 'completefunc' on Scratch buffer with preview window works when |
| 457 | " it's OK. |
| 458 | func Test_completefunc_with_scratch_buffer() |
| 459 | new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile |
| 460 | set completeopt+=preview |
| 461 | setlocal completefunc=DummyCompleteFive |
| 462 | call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x") |
| 463 | call assert_equal(['April'], getline(1, '$')) |
| 464 | pclose |
| 465 | q! |
| 466 | set completeopt& |
| 467 | endfunc |
Bram Moolenaar | 869e352 | 2016-10-16 15:35:47 +0200 | [diff] [blame] | 468 | |
Bram Moolenaar | 73fd498 | 2016-12-09 19:36:56 +0100 | [diff] [blame] | 469 | " <C-E> - select original typed text before the completion started without |
| 470 | " auto-wrap text. |
| 471 | func Test_completion_ctrl_e_without_autowrap() |
| 472 | new |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 473 | let tw_save = &tw |
Bram Moolenaar | 73fd498 | 2016-12-09 19:36:56 +0100 | [diff] [blame] | 474 | set tw=78 |
| 475 | let li = [ |
| 476 | \ '" zzz', |
| 477 | \ '" zzzyyyyyyyyyyyyyyyyyyy'] |
| 478 | call setline(1, li) |
| 479 | 0 |
| 480 | call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx") |
| 481 | call assert_equal(li, getline(1, '$')) |
| 482 | |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 483 | let &tw = tw_save |
Bram Moolenaar | 73fd498 | 2016-12-09 19:36:56 +0100 | [diff] [blame] | 484 | q! |
| 485 | endfunc |
| 486 | |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 487 | function! DummyCompleteSix() |
| 488 | call complete(1, ['Hello', 'World']) |
| 489 | return '' |
| 490 | endfunction |
| 491 | |
| 492 | " complete() correctly clears the list of autocomplete candidates |
| 493 | " See #1411 |
| 494 | func Test_completion_clear_candidate_list() |
| 495 | new |
| 496 | %d |
| 497 | " select first entry from the completion popup |
| 498 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx") |
| 499 | call assert_equal('Hello', getline(1)) |
| 500 | %d |
| 501 | " select second entry from the completion popup |
| 502 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx") |
| 503 | call assert_equal('World', getline(1)) |
| 504 | %d |
| 505 | " select original text |
| 506 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx") |
| 507 | call assert_equal(' xxx', getline(1)) |
| 508 | %d |
| 509 | " back at first entry from completion list |
| 510 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx") |
| 511 | call assert_equal('Hello', getline(1)) |
| 512 | |
| 513 | bw! |
| 514 | endfunc |
| 515 | |
Bram Moolenaar | 190b04c | 2017-02-09 17:37:03 +0100 | [diff] [blame] | 516 | func Test_completion_respect_bs_option() |
| 517 | new |
| 518 | let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"] |
| 519 | |
| 520 | set bs=indent,eol |
| 521 | call setline(1, li) |
| 522 | 1 |
| 523 | call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx") |
| 524 | call assert_equal('aaa', getline(1)) |
| 525 | |
| 526 | %d |
| 527 | set bs=indent,eol,start |
| 528 | call setline(1, li) |
| 529 | 1 |
| 530 | call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx") |
| 531 | call assert_equal('', getline(1)) |
| 532 | |
| 533 | bw! |
| 534 | endfunc |
| 535 | |
Bram Moolenaar | d56a79d | 2017-02-19 15:26:18 +0100 | [diff] [blame] | 536 | func CompleteUndo() abort |
| 537 | call complete(1, g:months) |
| 538 | return '' |
| 539 | endfunc |
| 540 | |
| 541 | func Test_completion_can_undo() |
| 542 | inoremap <Right> <c-r>=CompleteUndo()<cr> |
| 543 | set completeopt+=noinsert,noselect |
| 544 | |
| 545 | new |
| 546 | call feedkeys("a\<Right>a\<Esc>", 'xt') |
| 547 | call assert_equal('a', getline(1)) |
| 548 | undo |
| 549 | call assert_equal('', getline(1)) |
| 550 | |
| 551 | bwipe! |
| 552 | set completeopt& |
| 553 | iunmap <Right> |
| 554 | endfunc |
| 555 | |
Bram Moolenaar | d099e03 | 2017-02-21 23:00:36 +0100 | [diff] [blame] | 556 | func Test_completion_comment_formatting() |
| 557 | new |
| 558 | setl formatoptions=tcqro |
| 559 | call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx') |
| 560 | call assert_equal(['', '/*', ' *', ' */'], getline(1,4)) |
| 561 | %d |
| 562 | call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx') |
| 563 | call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4)) |
| 564 | %d |
| 565 | try |
| 566 | call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx') |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 567 | call assert_report('completefunc not set, should have failed') |
Bram Moolenaar | d099e03 | 2017-02-21 23:00:36 +0100 | [diff] [blame] | 568 | catch |
| 569 | call assert_exception('E764:') |
| 570 | endtry |
| 571 | call assert_equal(['', '/*', ' *', ' */'], getline(1,4)) |
| 572 | bwipe! |
| 573 | endfunc |
| 574 | |
Bram Moolenaar | 4475b62 | 2017-05-01 20:46:52 +0200 | [diff] [blame] | 575 | fun MessCompleteMonths() |
| 576 | for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep") |
| 577 | call complete_add(m) |
| 578 | if complete_check() |
| 579 | break |
| 580 | endif |
| 581 | endfor |
| 582 | return [] |
| 583 | endfun |
| 584 | |
| 585 | fun MessCompleteMore() |
| 586 | call complete(1, split("Oct Nov Dec")) |
| 587 | return [] |
| 588 | endfun |
| 589 | |
| 590 | fun MessComplete(findstart, base) |
| 591 | if a:findstart |
| 592 | let line = getline('.') |
| 593 | let start = col('.') - 1 |
| 594 | while start > 0 && line[start - 1] =~ '\a' |
| 595 | let start -= 1 |
| 596 | endwhile |
| 597 | return start |
| 598 | else |
| 599 | call MessCompleteMonths() |
| 600 | call MessCompleteMore() |
| 601 | return [] |
| 602 | endif |
| 603 | endf |
| 604 | |
| 605 | func Test_complete_func_mess() |
| 606 | " Calling complete() after complete_add() in 'completefunc' is wrong, but it |
| 607 | " should not crash. |
| 608 | set completefunc=MessComplete |
| 609 | new |
| 610 | call setline(1, 'Ju') |
| 611 | call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx') |
| 612 | call assert_equal('Oct/Oct', getline(1)) |
| 613 | bwipe! |
| 614 | set completefunc= |
| 615 | endfunc |
| 616 | |
Bram Moolenaar | 24a9e34 | 2017-06-24 15:39:07 +0200 | [diff] [blame] | 617 | func Test_complete_CTRLN_startofbuffer() |
| 618 | new |
| 619 | call setline(1, [ 'organize(cupboard, 3, 2);', |
| 620 | \ 'prioritize(bureau, 8, 7);', |
| 621 | \ 'realize(bannister, 4, 4);', |
| 622 | \ 'moralize(railing, 3,9);']) |
| 623 | let expected=['cupboard.organize(3, 2);', |
| 624 | \ 'bureau.prioritize(8, 7);', |
| 625 | \ 'bannister.realize(4, 4);', |
| 626 | \ 'railing.moralize(3,9);'] |
| 627 | call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx') |
| 628 | call assert_equal(expected, getline(1,'$')) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 629 | bwipe! |
| 630 | endfunc |
| 631 | |
| 632 | func Test_popup_and_window_resize() |
| 633 | if !has('terminal') || has('gui_running') |
| 634 | return |
| 635 | endif |
| 636 | let h = winheight(0) |
| 637 | if h < 15 |
| 638 | return |
| 639 | endif |
Bram Moolenaar | b315876 | 2017-11-02 17:50:14 +0100 | [diff] [blame] | 640 | let rows = h / 3 |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 641 | let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': rows}) |
| 642 | call term_sendkeys(buf, (h / 3 - 1) . "o\<esc>") |
Bram Moolenaar | b315876 | 2017-11-02 17:50:14 +0100 | [diff] [blame] | 643 | " Wait for the nested Vim to exit insert mode, where it will show the ruler. |
| 644 | " Need to trigger a redraw. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 645 | call WaitFor({-> execute("redraw") == "" && term_getline(buf, rows) =~ '\<' . rows . ',.*Bot'}) |
Bram Moolenaar | b315876 | 2017-11-02 17:50:14 +0100 | [diff] [blame] | 646 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 647 | call term_sendkeys(buf, "Gi\<c-x>") |
| 648 | call term_sendkeys(buf, "\<c-v>") |
| 649 | call term_wait(buf, 100) |
Bram Moolenaar | f52c383 | 2017-09-30 16:49:19 +0200 | [diff] [blame] | 650 | " popup first entry "!" must be at the top |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 651 | call WaitFor({-> term_getline(buf, 1) =~ "^!"}) |
| 652 | call assert_match('^!\s*$', term_getline(buf, 1)) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 653 | exe 'resize +' . (h - 1) |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 654 | call term_wait(buf, 100) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 655 | redraw! |
Bram Moolenaar | f52c383 | 2017-09-30 16:49:19 +0200 | [diff] [blame] | 656 | " popup shifted down, first line is now empty |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 657 | call WaitFor({-> term_getline(buf, 1) == ""}) |
| 658 | call assert_equal('', term_getline(buf, 1)) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 659 | sleep 100m |
Bram Moolenaar | f52c383 | 2017-09-30 16:49:19 +0200 | [diff] [blame] | 660 | " popup is below cursor line and shows first match "!" |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 661 | call WaitFor({-> term_getline(buf, term_getcursor(buf)[0] + 1) =~ "^!"}) |
| 662 | call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1)) |
Bram Moolenaar | f52c383 | 2017-09-30 16:49:19 +0200 | [diff] [blame] | 663 | " cursor line also shows ! |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 664 | call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0])) |
Bram Moolenaar | 24a9e34 | 2017-06-24 15:39:07 +0200 | [diff] [blame] | 665 | bwipe! |
| 666 | endfunc |
Bram Moolenaar | 4475b62 | 2017-05-01 20:46:52 +0200 | [diff] [blame] | 667 | |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 668 | func Test_popup_and_preview_autocommand() |
| 669 | " This used to crash Vim |
| 670 | if !has('python') |
| 671 | return |
| 672 | endif |
| 673 | let h = winheight(0) |
| 674 | if h < 15 |
| 675 | return |
| 676 | endif |
| 677 | new |
| 678 | augroup MyBufAdd |
| 679 | au! |
| 680 | au BufAdd * nested tab sball |
| 681 | augroup END |
| 682 | set omnifunc=pythoncomplete#Complete |
| 683 | call setline(1, 'import os') |
| 684 | " make the line long |
| 685 | call setline(2, ' os.') |
| 686 | $ |
| 687 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<enter>\<esc>", 'tx') |
Bram Moolenaar | 2a45d64 | 2017-10-27 01:35:00 +0200 | [diff] [blame] | 688 | call assert_equal("import os", getline(1)) |
| 689 | call assert_match(' os.\(EX_IOERR\|O_CREAT\)$', getline(2)) |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 690 | call assert_equal(1, winnr('$')) |
| 691 | " previewwindow option is not set |
| 692 | call assert_equal(0, &previewwindow) |
| 693 | norm! gt |
| 694 | call assert_equal(0, &previewwindow) |
| 695 | norm! gT |
Bram Moolenaar | 02ae9b4 | 2018-02-09 15:06:02 +0100 | [diff] [blame] | 696 | call assert_equal(10, tabpagenr('$')) |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 697 | tabonly |
| 698 | pclose |
| 699 | augroup MyBufAdd |
| 700 | au! |
| 701 | augroup END |
| 702 | augroup! MyBufAdd |
| 703 | bw! |
| 704 | endfunc |
| 705 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 706 | func Test_balloon_split() |
Bram Moolenaar | 7221fce | 2017-11-19 20:32:49 +0100 | [diff] [blame] | 707 | if !exists('*balloon_split') |
| 708 | return |
| 709 | endif |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 710 | call assert_equal([ |
Bram Moolenaar | a3571eb | 2017-11-26 16:53:16 +0100 | [diff] [blame] | 711 | \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"', |
| 712 | \ ], balloon_split( |
| 713 | \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"')) |
| 714 | call assert_equal([ |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 715 | \ 'one two three four one two three four one two thre', |
| 716 | \ 'e four', |
| 717 | \ ], balloon_split( |
| 718 | \ 'one two three four one two three four one two three four')) |
| 719 | |
| 720 | call assert_equal([ |
| 721 | \ 'struct = {', |
| 722 | \ ' one = 1,', |
| 723 | \ ' two = 2,', |
| 724 | \ ' three = 3}', |
| 725 | \ ], balloon_split( |
| 726 | \ 'struct = {one = 1, two = 2, three = 3}')) |
| 727 | |
| 728 | call assert_equal([ |
| 729 | \ 'struct = {', |
| 730 | \ ' one = 1,', |
| 731 | \ ' nested = {', |
| 732 | \ ' n1 = "yes",', |
| 733 | \ ' n2 = "no"}', |
| 734 | \ ' two = 2}', |
| 735 | \ ], balloon_split( |
| 736 | \ 'struct = {one = 1, nested = {n1 = "yes", n2 = "no"} two = 2}')) |
| 737 | call assert_equal([ |
| 738 | \ 'struct = 0x234 {', |
| 739 | \ ' long = 2343 "\\"some long string that will be wr', |
| 740 | \ 'apped in two\\"",', |
| 741 | \ ' next = 123}', |
| 742 | \ ], balloon_split( |
| 743 | \ 'struct = 0x234 {long = 2343 "\\"some long string that will be wrapped in two\\"", next = 123}')) |
| 744 | endfunc |
| 745 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 746 | " vim: shiftwidth=2 sts=2 expandtab |