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