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> |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 41 | set belloff=all |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 218 | set belloff& |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 219 | endfu |
| 220 | |
| 221 | |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 222 | func! Test_popup_completion_insertmode() |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 223 | new |
| 224 | inoremap <F5> <C-R>=ListMonths()<CR> |
| 225 | |
| 226 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
| 227 | call assert_equal('February', getline(1)) |
| 228 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 229 | " Set noinsertmode |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 230 | let g:setting = 'noinsertmode' |
| 231 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
| 232 | call assert_equal('February', getline(1)) |
| 233 | call assert_false(pumvisible()) |
| 234 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 235 | " Go through all matches, until none is selected |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 236 | let g:setting = '' |
| 237 | call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx') |
| 238 | call assert_equal('', getline(1)) |
| 239 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 240 | " select previous entry |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 241 | call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx') |
| 242 | call assert_equal('', getline(1)) |
| 243 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 244 | " select last entry |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 245 | call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx') |
| 246 | call assert_equal('December', getline(1)) |
| 247 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 248 | iunmap <F5> |
| 249 | endfunc |
| 250 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 251 | func Test_noinsert_complete() |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 252 | function! s:complTest1() abort |
| 253 | call complete(1, ['source', 'soundfold']) |
| 254 | return '' |
| 255 | endfunction |
| 256 | |
| 257 | function! s:complTest2() abort |
| 258 | call complete(1, ['source', 'soundfold']) |
| 259 | return '' |
| 260 | endfunction |
| 261 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 262 | new |
| 263 | set completeopt+=noinsert |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 264 | inoremap <F5> <C-R>=s:complTest1()<CR> |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 265 | call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx') |
| 266 | call assert_equal('soundfold', getline(1)) |
| 267 | call assert_equal('soundfold', getline(2)) |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 268 | bwipe! |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 269 | |
| 270 | new |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 271 | inoremap <F5> <C-R>=s:complTest2()<CR> |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 272 | call feedkeys("i\<F5>\<CR>\<ESC>", 'tx') |
| 273 | call assert_equal('source', getline(1)) |
| 274 | bwipe! |
| 275 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 276 | set completeopt-=noinsert |
| 277 | iunmap <F5> |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 278 | endfunc |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 279 | |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 280 | func Test_compl_vim_cmds_after_register_expr() |
| 281 | function! s:test_func() |
| 282 | return 'autocmd ' |
| 283 | endfunction |
| 284 | augroup AAAAA_Group |
| 285 | au! |
| 286 | augroup END |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 287 | |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 288 | new |
| 289 | call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx') |
| 290 | call assert_equal('autocmd AAAAA_Group', getline(1)) |
| 291 | autocmd! AAAAA_Group |
| 292 | augroup! AAAAA_Group |
| 293 | bwipe! |
| 294 | endfunc |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 295 | |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 296 | func DummyCompleteOne(findstart, base) |
| 297 | if a:findstart |
| 298 | return 0 |
| 299 | else |
| 300 | wincmd n |
| 301 | return ['onedef', 'oneDEF'] |
| 302 | endif |
| 303 | endfunc |
| 304 | |
| 305 | " Test that nothing happens if the 'completefunc' opens |
| 306 | " a new window (no completion, no crash) |
| 307 | func Test_completefunc_opens_new_window_one() |
| 308 | new |
| 309 | let winid = win_getid() |
| 310 | setlocal completefunc=DummyCompleteOne |
| 311 | call setline(1, 'one') |
| 312 | /^one |
| 313 | call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E839:') |
| 314 | call assert_notequal(winid, win_getid()) |
| 315 | q! |
| 316 | call assert_equal(winid, win_getid()) |
| 317 | call assert_equal('', getline(1)) |
| 318 | q! |
| 319 | endfunc |
| 320 | |
| 321 | " Test that nothing happens if the 'completefunc' opens |
| 322 | " a new window (no completion, no crash) |
| 323 | func DummyCompleteTwo(findstart, base) |
| 324 | if a:findstart |
| 325 | wincmd n |
| 326 | return 0 |
| 327 | else |
| 328 | return ['twodef', 'twoDEF'] |
| 329 | endif |
| 330 | endfunction |
| 331 | |
| 332 | " Test that nothing happens if the 'completefunc' opens |
| 333 | " a new window (no completion, no crash) |
| 334 | func Test_completefunc_opens_new_window_two() |
| 335 | new |
| 336 | let winid = win_getid() |
| 337 | setlocal completefunc=DummyCompleteTwo |
| 338 | call setline(1, 'two') |
| 339 | /^two |
| 340 | call assert_fails('call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x")', 'E764:') |
| 341 | call assert_notequal(winid, win_getid()) |
| 342 | q! |
| 343 | call assert_equal(winid, win_getid()) |
| 344 | call assert_equal('two', getline(1)) |
| 345 | q! |
| 346 | endfunc |
| 347 | |
| 348 | func DummyCompleteThree(findstart, base) |
| 349 | if a:findstart |
| 350 | return 0 |
| 351 | else |
| 352 | return ['threedef', 'threeDEF'] |
| 353 | endif |
| 354 | endfunc |
| 355 | |
| 356 | :"Test that 'completefunc' works when it's OK. |
| 357 | func Test_completefunc_works() |
| 358 | new |
| 359 | let winid = win_getid() |
| 360 | setlocal completefunc=DummyCompleteThree |
| 361 | call setline(1, 'three') |
| 362 | /^three |
| 363 | call feedkeys("A\<C-X>\<C-U>\<C-N>\<Esc>", "x") |
| 364 | call assert_equal(winid, win_getid()) |
| 365 | call assert_equal('threeDEF', getline(1)) |
| 366 | q! |
| 367 | endfunc |
| 368 | |
| 369 | func DummyCompleteFour(findstart, base) |
| 370 | if a:findstart |
| 371 | return 0 |
| 372 | else |
| 373 | call complete_add('four1') |
| 374 | call complete_add('four2') |
| 375 | call complete_check() |
| 376 | call complete_add('four3') |
| 377 | call complete_add('four4') |
| 378 | call complete_check() |
| 379 | call complete_add('four5') |
| 380 | call complete_add('four6') |
| 381 | return [] |
| 382 | endif |
| 383 | endfunc |
| 384 | |
Bram Moolenaar | 60ef3e8 | 2016-10-29 14:37:56 +0200 | [diff] [blame] | 385 | " Test that 'omnifunc' works when it's OK. |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 386 | func Test_omnifunc_with_check() |
| 387 | new |
| 388 | setlocal omnifunc=DummyCompleteFour |
| 389 | call setline(1, 'four') |
| 390 | /^four |
| 391 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<Esc>", "x") |
| 392 | call assert_equal('four2', getline(1)) |
| 393 | |
| 394 | call setline(1, 'four') |
| 395 | /^four |
| 396 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<Esc>", "x") |
| 397 | call assert_equal('four3', getline(1)) |
| 398 | |
| 399 | call setline(1, 'four') |
| 400 | /^four |
| 401 | call feedkeys("A\<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-N>\<Esc>", "x") |
| 402 | call assert_equal('four5', getline(1)) |
| 403 | |
| 404 | q! |
| 405 | endfunc |
| 406 | |
Bram Moolenaar | 869e352 | 2016-10-16 15:35:47 +0200 | [diff] [blame] | 407 | function UndoComplete() |
| 408 | call complete(1, ['January', 'February', 'March', |
| 409 | \ 'April', 'May', 'June', 'July', 'August', 'September', |
| 410 | \ 'October', 'November', 'December']) |
| 411 | return '' |
| 412 | endfunc |
| 413 | |
| 414 | " Test that no undo item is created when no completion is inserted |
| 415 | func Test_complete_no_undo() |
| 416 | set completeopt=menu,preview,noinsert,noselect |
| 417 | inoremap <Right> <C-R>=UndoComplete()<CR> |
| 418 | new |
| 419 | call feedkeys("ixxx\<CR>\<CR>yyy\<Esc>k", 'xt') |
| 420 | call feedkeys("iaaa\<Esc>0", 'xt') |
| 421 | call assert_equal('aaa', getline(2)) |
| 422 | call feedkeys("i\<Right>\<Esc>", 'xt') |
| 423 | call assert_equal('aaa', getline(2)) |
| 424 | call feedkeys("u", 'xt') |
| 425 | call assert_equal('', getline(2)) |
| 426 | |
Bram Moolenaar | cbd3bd6 | 2016-10-17 20:47:02 +0200 | [diff] [blame] | 427 | call feedkeys("ibbb\<Esc>0", 'xt') |
| 428 | call assert_equal('bbb', getline(2)) |
| 429 | call feedkeys("A\<Right>\<Down>\<CR>\<Esc>", 'xt') |
| 430 | call assert_equal('January', getline(2)) |
| 431 | call feedkeys("u", 'xt') |
| 432 | call assert_equal('bbb', getline(2)) |
| 433 | |
Bram Moolenaar | 9ec7fa8 | 2016-10-18 13:06:41 +0200 | [diff] [blame] | 434 | call feedkeys("A\<Right>\<C-N>\<Esc>", 'xt') |
| 435 | call assert_equal('January', getline(2)) |
| 436 | call feedkeys("u", 'xt') |
| 437 | call assert_equal('bbb', getline(2)) |
| 438 | |
Bram Moolenaar | 869e352 | 2016-10-16 15:35:47 +0200 | [diff] [blame] | 439 | iunmap <Right> |
| 440 | set completeopt& |
| 441 | q! |
| 442 | endfunc |
| 443 | |
Bram Moolenaar | 60ef3e8 | 2016-10-29 14:37:56 +0200 | [diff] [blame] | 444 | function! DummyCompleteFive(findstart, base) |
| 445 | if a:findstart |
| 446 | return 0 |
| 447 | else |
| 448 | return [ |
| 449 | \ { 'word': 'January', 'info': "info1-1\n1-2\n1-3" }, |
| 450 | \ { 'word': 'February', 'info': "info2-1\n2-2\n2-3" }, |
| 451 | \ { 'word': 'March', 'info': "info3-1\n3-2\n3-3" }, |
| 452 | \ { 'word': 'April', 'info': "info4-1\n4-2\n4-3" }, |
| 453 | \ { 'word': 'May', 'info': "info5-1\n5-2\n5-3" }, |
| 454 | \ ] |
| 455 | endif |
| 456 | endfunc |
| 457 | |
| 458 | " Test that 'completefunc' on Scratch buffer with preview window works when |
| 459 | " it's OK. |
| 460 | func Test_completefunc_with_scratch_buffer() |
| 461 | new +setlocal\ buftype=nofile\ bufhidden=wipe\ noswapfile |
| 462 | set completeopt+=preview |
| 463 | setlocal completefunc=DummyCompleteFive |
| 464 | call feedkeys("A\<C-X>\<C-U>\<C-N>\<C-N>\<C-N>\<Esc>", "x") |
| 465 | call assert_equal(['April'], getline(1, '$')) |
| 466 | pclose |
| 467 | q! |
| 468 | set completeopt& |
| 469 | endfunc |
Bram Moolenaar | 869e352 | 2016-10-16 15:35:47 +0200 | [diff] [blame] | 470 | |
Bram Moolenaar | 73fd498 | 2016-12-09 19:36:56 +0100 | [diff] [blame] | 471 | " <C-E> - select original typed text before the completion started without |
| 472 | " auto-wrap text. |
| 473 | func Test_completion_ctrl_e_without_autowrap() |
| 474 | new |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 475 | let tw_save = &tw |
Bram Moolenaar | 73fd498 | 2016-12-09 19:36:56 +0100 | [diff] [blame] | 476 | set tw=78 |
| 477 | let li = [ |
| 478 | \ '" zzz', |
| 479 | \ '" zzzyyyyyyyyyyyyyyyyyyy'] |
| 480 | call setline(1, li) |
| 481 | 0 |
| 482 | call feedkeys("A\<C-X>\<C-N>\<C-E>\<Esc>", "tx") |
| 483 | call assert_equal(li, getline(1, '$')) |
| 484 | |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 485 | let &tw = tw_save |
Bram Moolenaar | 73fd498 | 2016-12-09 19:36:56 +0100 | [diff] [blame] | 486 | q! |
| 487 | endfunc |
| 488 | |
Bram Moolenaar | aed6d0b | 2017-01-27 21:48:54 +0100 | [diff] [blame] | 489 | function! DummyCompleteSix() |
| 490 | call complete(1, ['Hello', 'World']) |
| 491 | return '' |
| 492 | endfunction |
| 493 | |
| 494 | " complete() correctly clears the list of autocomplete candidates |
| 495 | " See #1411 |
| 496 | func Test_completion_clear_candidate_list() |
| 497 | new |
| 498 | %d |
| 499 | " select first entry from the completion popup |
| 500 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>", "tx") |
| 501 | call assert_equal('Hello', getline(1)) |
| 502 | %d |
| 503 | " select second entry from the completion popup |
| 504 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>", "tx") |
| 505 | call assert_equal('World', getline(1)) |
| 506 | %d |
| 507 | " select original text |
| 508 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>", "tx") |
| 509 | call assert_equal(' xxx', getline(1)) |
| 510 | %d |
| 511 | " back at first entry from completion list |
| 512 | call feedkeys("a xxx\<C-N>\<C-R>=DummyCompleteSix()\<CR>\<C-N>\<C-N>\<C-N>", "tx") |
| 513 | call assert_equal('Hello', getline(1)) |
| 514 | |
| 515 | bw! |
| 516 | endfunc |
| 517 | |
Bram Moolenaar | 190b04c | 2017-02-09 17:37:03 +0100 | [diff] [blame] | 518 | func Test_completion_respect_bs_option() |
| 519 | new |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 520 | set belloff=all |
Bram Moolenaar | 190b04c | 2017-02-09 17:37:03 +0100 | [diff] [blame] | 521 | let li = ["aaa", "aaa12345", "aaaabcdef", "aaaABC"] |
| 522 | |
| 523 | set bs=indent,eol |
| 524 | call setline(1, li) |
| 525 | 1 |
| 526 | call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx") |
| 527 | call assert_equal('aaa', getline(1)) |
| 528 | |
| 529 | %d |
| 530 | set bs=indent,eol,start |
| 531 | call setline(1, li) |
| 532 | 1 |
| 533 | call feedkeys("A\<C-X>\<C-N>\<C-P>\<BS>\<BS>\<BS>\<Esc>", "tx") |
| 534 | call assert_equal('', getline(1)) |
| 535 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 536 | set belloff& |
Bram Moolenaar | 190b04c | 2017-02-09 17:37:03 +0100 | [diff] [blame] | 537 | bw! |
| 538 | endfunc |
| 539 | |
Bram Moolenaar | d56a79d | 2017-02-19 15:26:18 +0100 | [diff] [blame] | 540 | func CompleteUndo() abort |
| 541 | call complete(1, g:months) |
| 542 | return '' |
| 543 | endfunc |
| 544 | |
| 545 | func Test_completion_can_undo() |
| 546 | inoremap <Right> <c-r>=CompleteUndo()<cr> |
| 547 | set completeopt+=noinsert,noselect |
| 548 | |
| 549 | new |
| 550 | call feedkeys("a\<Right>a\<Esc>", 'xt') |
| 551 | call assert_equal('a', getline(1)) |
| 552 | undo |
| 553 | call assert_equal('', getline(1)) |
| 554 | |
| 555 | bwipe! |
| 556 | set completeopt& |
| 557 | iunmap <Right> |
| 558 | endfunc |
| 559 | |
Bram Moolenaar | d099e03 | 2017-02-21 23:00:36 +0100 | [diff] [blame] | 560 | func Test_completion_comment_formatting() |
| 561 | new |
| 562 | setl formatoptions=tcqro |
| 563 | call feedkeys("o/*\<cr>\<cr>/\<esc>", 'tx') |
| 564 | call assert_equal(['', '/*', ' *', ' */'], getline(1,4)) |
| 565 | %d |
| 566 | call feedkeys("o/*\<cr>foobar\<cr>/\<esc>", 'tx') |
| 567 | call assert_equal(['', '/*', ' * foobar', ' */'], getline(1,4)) |
| 568 | %d |
| 569 | try |
| 570 | call feedkeys("o/*\<cr>\<cr>\<c-x>\<c-u>/\<esc>", 'tx') |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 571 | call assert_report('completefunc not set, should have failed') |
Bram Moolenaar | d099e03 | 2017-02-21 23:00:36 +0100 | [diff] [blame] | 572 | catch |
| 573 | call assert_exception('E764:') |
| 574 | endtry |
| 575 | call assert_equal(['', '/*', ' *', ' */'], getline(1,4)) |
| 576 | bwipe! |
| 577 | endfunc |
| 578 | |
Bram Moolenaar | 4475b62 | 2017-05-01 20:46:52 +0200 | [diff] [blame] | 579 | fun MessCompleteMonths() |
| 580 | for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep") |
| 581 | call complete_add(m) |
| 582 | if complete_check() |
| 583 | break |
| 584 | endif |
| 585 | endfor |
| 586 | return [] |
| 587 | endfun |
| 588 | |
| 589 | fun MessCompleteMore() |
| 590 | call complete(1, split("Oct Nov Dec")) |
| 591 | return [] |
| 592 | endfun |
| 593 | |
| 594 | fun MessComplete(findstart, base) |
| 595 | if a:findstart |
| 596 | let line = getline('.') |
| 597 | let start = col('.') - 1 |
| 598 | while start > 0 && line[start - 1] =~ '\a' |
| 599 | let start -= 1 |
| 600 | endwhile |
| 601 | return start |
| 602 | else |
| 603 | call MessCompleteMonths() |
| 604 | call MessCompleteMore() |
| 605 | return [] |
| 606 | endif |
| 607 | endf |
| 608 | |
| 609 | func Test_complete_func_mess() |
| 610 | " Calling complete() after complete_add() in 'completefunc' is wrong, but it |
| 611 | " should not crash. |
| 612 | set completefunc=MessComplete |
| 613 | new |
| 614 | call setline(1, 'Ju') |
| 615 | call feedkeys("A\<c-x>\<c-u>/\<esc>", 'tx') |
| 616 | call assert_equal('Oct/Oct', getline(1)) |
| 617 | bwipe! |
| 618 | set completefunc= |
| 619 | endfunc |
| 620 | |
Bram Moolenaar | 24a9e34 | 2017-06-24 15:39:07 +0200 | [diff] [blame] | 621 | func Test_complete_CTRLN_startofbuffer() |
| 622 | new |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 623 | set belloff=all |
Bram Moolenaar | 24a9e34 | 2017-06-24 15:39:07 +0200 | [diff] [blame] | 624 | call setline(1, [ 'organize(cupboard, 3, 2);', |
| 625 | \ 'prioritize(bureau, 8, 7);', |
| 626 | \ 'realize(bannister, 4, 4);', |
| 627 | \ 'moralize(railing, 3,9);']) |
| 628 | let expected=['cupboard.organize(3, 2);', |
| 629 | \ 'bureau.prioritize(8, 7);', |
| 630 | \ 'bannister.realize(4, 4);', |
| 631 | \ 'railing.moralize(3,9);'] |
| 632 | call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx') |
| 633 | call assert_equal(expected, getline(1,'$')) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 634 | set belloff& |
| 635 | bwipe! |
| 636 | endfunc |
| 637 | |
| 638 | func Test_popup_and_window_resize() |
| 639 | if !has('terminal') || has('gui_running') |
| 640 | return |
| 641 | endif |
| 642 | let h = winheight(0) |
| 643 | if h < 15 |
| 644 | return |
| 645 | endif |
| 646 | let g:buf = term_start([$VIMPROG, '--clean', '-c', 'set noswapfile'], {'term_rows': h / 3}) |
| 647 | call term_sendkeys(g:buf, (h / 3 - 1)."o\<esc>G") |
| 648 | call term_sendkeys(g:buf, "i\<c-x>") |
| 649 | call term_wait(g:buf, 100) |
| 650 | call term_sendkeys(g:buf, "\<c-v>") |
| 651 | call term_wait(g:buf, 100) |
Bram Moolenaar | c79977a | 2017-09-30 14:39:27 +0200 | [diff] [blame] | 652 | call WaitFor('term_getline(g:buf, 1) =~ "^!"') |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 653 | call assert_match('^!\s*$', term_getline(g:buf, 1)) |
| 654 | exe 'resize +' . (h - 1) |
| 655 | call term_wait(g:buf, 100) |
| 656 | redraw! |
Bram Moolenaar | c79977a | 2017-09-30 14:39:27 +0200 | [diff] [blame] | 657 | call WaitFor('term_getline(g:buf, 1) == ""') |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 658 | call assert_equal('', term_getline(g:buf, 1)) |
| 659 | sleep 100m |
Bram Moolenaar | c79977a | 2017-09-30 14:39:27 +0200 | [diff] [blame] | 660 | call WaitFor('term_getline(g:buf, term_getcursor(g:buf)[0] + 1) =~ "^!"') |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 661 | call assert_match('^!\s*$', term_getline(g:buf, term_getcursor(g:buf)[0] + 1)) |
Bram Moolenaar | 24a9e34 | 2017-06-24 15:39:07 +0200 | [diff] [blame] | 662 | bwipe! |
| 663 | endfunc |
Bram Moolenaar | 4475b62 | 2017-05-01 20:46:52 +0200 | [diff] [blame] | 664 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 665 | " vim: shiftwidth=2 sts=2 expandtab |