Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 1 | " Test for completion menu |
| 2 | |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 3 | let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] |
| 4 | let g:setting = '' |
| 5 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 6 | func! ListMonths() |
| 7 | if g:setting != '' |
| 8 | exe ":set" g:setting |
| 9 | endif |
| 10 | let mth=copy(g:months) |
| 11 | let entered = strcharpart(getline('.'),0,col('.')) |
| 12 | if !empty(entered) |
| 13 | let mth=filter(mth, 'v:val=~"^".entered') |
| 14 | endif |
| 15 | call complete(1, mth) |
| 16 | return '' |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 17 | endfunc |
| 18 | |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 19 | func! Test_popup_complete2() |
Bram Moolenaar | 9e02cfa | 2016-09-22 21:27:11 +0200 | [diff] [blame] | 20 | " Although the popupmenu is not visible, this does not mean completion mode |
| 21 | " has ended. After pressing <f5> to complete the currently typed char, Vim |
| 22 | " still stays in the first state of the completion (:h ins-completion-menu), |
| 23 | " although the popupmenu wasn't shown <c-e> will remove the inserted |
| 24 | " completed text (:h complete_CTRL-E), while the following <c-e> will behave |
| 25 | " like expected (:h i_CTRL-E) |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 26 | new |
| 27 | inoremap <f5> <c-r>=ListMonths()<cr> |
| 28 | call append(1, ["December2015"]) |
| 29 | :1 |
| 30 | 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] | 31 | call assert_equal(["Dece", "", "December2015"], getline(1,3)) |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 32 | %d |
| 33 | bw! |
| 34 | endfu |
| 35 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 36 | func! Test_popup_complete() |
| 37 | new |
| 38 | inoremap <f5> <c-r>=ListMonths()<cr> |
| 39 | |
| 40 | " <C-E> - select original typed text before the completion started |
| 41 | call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx') |
| 42 | call assert_equal(["Ju"], getline(1,2)) |
| 43 | %d |
| 44 | |
| 45 | " <C-Y> - accept current match |
| 46 | call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx') |
| 47 | call assert_equal(["August"], getline(1,2)) |
| 48 | %d |
| 49 | |
| 50 | " <BS> - Delete one character from the inserted text (state: 1) |
| 51 | " TODO: This should not end the completion, but it does. |
| 52 | " This should according to the documentation: |
| 53 | " January |
| 54 | " but instead, this does |
| 55 | " Januar |
| 56 | " (idea is, C-L inserts the match from the popup menu |
| 57 | " but if the menu is closed, it will insert the character <c-l> |
| 58 | call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx') |
| 59 | call assert_equal(["Januar"], getline(1,2)) |
| 60 | %d |
| 61 | |
| 62 | " any-non special character: Stop completion without changing the match |
| 63 | " and insert the typed character |
| 64 | call feedkeys("a\<f5>20", 'tx') |
| 65 | call assert_equal(["January20"], getline(1,2)) |
| 66 | %d |
| 67 | |
| 68 | " any-non printable, non-white character: Add this character and |
| 69 | " reduce number of matches |
| 70 | call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx') |
| 71 | call assert_equal(["Jul"], getline(1,2)) |
| 72 | %d |
| 73 | |
| 74 | " any-non printable, non-white character: Add this character and |
| 75 | " reduce number of matches |
| 76 | call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx') |
| 77 | call assert_equal(["July"], getline(1,2)) |
| 78 | %d |
| 79 | |
| 80 | " any-non printable, non-white character: Add this character and |
| 81 | " reduce number of matches |
| 82 | call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx') |
| 83 | call assert_equal(["Jul"], getline(1,2)) |
| 84 | %d |
| 85 | |
| 86 | " <BS> - Delete one character from the inserted text (state: 2) |
| 87 | call feedkeys("a\<f5>\<c-n>\<bs>", 'tx') |
| 88 | call assert_equal(["Februar"], getline(1,2)) |
| 89 | %d |
| 90 | |
| 91 | " <c-l> - Insert one character from the current match |
| 92 | call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx') |
| 93 | call assert_equal(["J"], getline(1,2)) |
| 94 | %d |
| 95 | |
| 96 | " <c-l> - Insert one character from the current match |
| 97 | call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx') |
| 98 | call assert_equal(["January"], getline(1,2)) |
| 99 | %d |
| 100 | |
| 101 | " <c-y> - Accept current selected match |
| 102 | call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx') |
| 103 | call assert_equal(["January"], getline(1,2)) |
| 104 | %d |
| 105 | |
| 106 | " <c-e> - End completion, go back to what was there before selecting a match |
| 107 | call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx') |
| 108 | call assert_equal(["Ju"], getline(1,2)) |
| 109 | %d |
| 110 | |
| 111 | " <PageUp> - Select a match several entries back |
| 112 | call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx') |
| 113 | call assert_equal([""], getline(1,2)) |
| 114 | %d |
| 115 | |
| 116 | " <PageUp><PageUp> - Select a match several entries back |
| 117 | call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx') |
| 118 | call assert_equal(["December"], getline(1,2)) |
| 119 | %d |
| 120 | |
| 121 | " <PageUp><PageUp><PageUp> - Select a match several entries back |
| 122 | call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx') |
| 123 | call assert_equal(["February"], getline(1,2)) |
| 124 | %d |
| 125 | |
| 126 | " <PageDown> - Select a match several entries further |
| 127 | call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx') |
| 128 | call assert_equal(["November"], getline(1,2)) |
| 129 | %d |
| 130 | |
| 131 | " <PageDown><PageDown> - Select a match several entries further |
| 132 | call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx') |
| 133 | call assert_equal(["December"], getline(1,2)) |
| 134 | %d |
| 135 | |
| 136 | " <PageDown><PageDown><PageDown> - Select a match several entries further |
| 137 | call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx') |
| 138 | call assert_equal([""], getline(1,2)) |
| 139 | %d |
| 140 | |
| 141 | " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further |
| 142 | call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx') |
| 143 | call assert_equal(["October"], getline(1,2)) |
| 144 | %d |
| 145 | |
| 146 | " <Up> - Select a match don't insert yet |
| 147 | call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx') |
| 148 | call assert_equal([""], getline(1,2)) |
| 149 | %d |
| 150 | |
| 151 | " <Up><Up> - Select a match don't insert yet |
| 152 | call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx') |
| 153 | call assert_equal(["December"], getline(1,2)) |
| 154 | %d |
| 155 | |
| 156 | " <Up><Up><Up> - Select a match don't insert yet |
| 157 | call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx') |
| 158 | call assert_equal(["November"], getline(1,2)) |
| 159 | %d |
| 160 | |
| 161 | " <Tab> - Stop completion and insert the match |
| 162 | call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx') |
| 163 | call assert_equal(["January "], getline(1,2)) |
| 164 | %d |
| 165 | |
| 166 | " <Space> - Stop completion and insert the match |
| 167 | call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx') |
| 168 | call assert_equal(["September "], getline(1,2)) |
| 169 | %d |
| 170 | |
| 171 | " <Enter> - Use the text and insert line break (state: 1) |
| 172 | call feedkeys("a\<f5>\<enter>\<esc>", 'tx') |
| 173 | call assert_equal(["January", ''], getline(1,2)) |
| 174 | %d |
| 175 | |
| 176 | " <Enter> - Insert the current selected text (state: 2) |
| 177 | call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx') |
| 178 | call assert_equal(["September"], getline(1,2)) |
| 179 | %d |
| 180 | |
| 181 | " Insert match immediately, if there is only one match |
| 182 | " <c-y> selects a character from the line above |
| 183 | call append(0, ["December2015"]) |
| 184 | call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx') |
| 185 | call assert_equal(["December2015", "December2015", ""], getline(1,3)) |
| 186 | %d |
| 187 | |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 188 | " use menuone for 'completeopt' |
| 189 | " Since for the first <c-y> the menu is still shown, will only select |
| 190 | " three letters from the line above |
| 191 | set completeopt&vim |
| 192 | set completeopt+=menuone |
| 193 | call append(0, ["December2015"]) |
| 194 | call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx') |
| 195 | call assert_equal(["December2015", "December201", ""], getline(1,3)) |
| 196 | %d |
| 197 | |
| 198 | " use longest for 'completeopt' |
| 199 | set completeopt&vim |
| 200 | call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx') |
| 201 | set completeopt+=longest |
| 202 | call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx') |
| 203 | call assert_equal(["M", "Ma", ""], getline(1,3)) |
| 204 | %d |
| 205 | |
| 206 | " use noselect/noinsert for 'completeopt' |
| 207 | set completeopt&vim |
| 208 | call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 209 | set completeopt+=noselect |
| 210 | call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 211 | set completeopt-=noselect completeopt+=noinsert |
| 212 | call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 213 | call assert_equal(["March", "M", "March"], getline(1,4)) |
| 214 | %d |
| 215 | endfu |
| 216 | |
| 217 | |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 218 | func! Test_popup_completion_insertmode() |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 219 | new |
| 220 | inoremap <F5> <C-R>=ListMonths()<CR> |
| 221 | |
| 222 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
| 223 | call assert_equal('February', getline(1)) |
| 224 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 225 | " Set noinsertmode |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 226 | let g:setting = 'noinsertmode' |
| 227 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
| 228 | call assert_equal('February', getline(1)) |
| 229 | call assert_false(pumvisible()) |
| 230 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 231 | " Go through all matches, until none is selected |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 232 | let g:setting = '' |
| 233 | call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx') |
| 234 | call assert_equal('', getline(1)) |
| 235 | %d |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 236 | " select previous entry |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 237 | call feedkeys("a\<f5>\<c-p>\<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 last entry |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 241 | call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx') |
| 242 | call assert_equal('December', getline(1)) |
| 243 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 244 | iunmap <F5> |
| 245 | endfunc |
| 246 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 247 | func Test_noinsert_complete() |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 248 | function! s:complTest1() abort |
| 249 | call complete(1, ['source', 'soundfold']) |
| 250 | return '' |
| 251 | endfunction |
| 252 | |
| 253 | function! s:complTest2() abort |
| 254 | call complete(1, ['source', 'soundfold']) |
| 255 | return '' |
| 256 | endfunction |
| 257 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 258 | new |
| 259 | set completeopt+=noinsert |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 260 | inoremap <F5> <C-R>=s:complTest1()<CR> |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 261 | call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx') |
| 262 | call assert_equal('soundfold', getline(1)) |
| 263 | call assert_equal('soundfold', getline(2)) |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 264 | bwipe! |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 265 | |
| 266 | new |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 267 | inoremap <F5> <C-R>=s:complTest2()<CR> |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 268 | call feedkeys("i\<F5>\<CR>\<ESC>", 'tx') |
| 269 | call assert_equal('source', getline(1)) |
| 270 | bwipe! |
| 271 | |
Bram Moolenaar | 67081e5 | 2016-07-09 21:49:03 +0200 | [diff] [blame] | 272 | set completeopt-=noinsert |
| 273 | iunmap <F5> |
Bram Moolenaar | 00672e1 | 2016-06-26 18:38:13 +0200 | [diff] [blame] | 274 | endfunc |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 275 | |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 276 | func Test_compl_vim_cmds_after_register_expr() |
| 277 | function! s:test_func() |
| 278 | return 'autocmd ' |
| 279 | endfunction |
| 280 | augroup AAAAA_Group |
| 281 | au! |
| 282 | augroup END |
Bram Moolenaar | 32b808a | 2016-07-09 21:57:20 +0200 | [diff] [blame] | 283 | |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 284 | new |
| 285 | call feedkeys("i\<c-r>=s:test_func()\<CR>\<C-x>\<C-v>\<Esc>", 'tx') |
| 286 | call assert_equal('autocmd AAAAA_Group', getline(1)) |
| 287 | autocmd! AAAAA_Group |
| 288 | augroup! AAAAA_Group |
| 289 | bwipe! |
| 290 | endfunc |
Bram Moolenaar | 4724728 | 2016-08-02 22:36:02 +0200 | [diff] [blame] | 291 | |
| 292 | " vim: shiftwidth=2 sts=2 expandtab |