|
3 | 3 | let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] |
4 | 4 | let g:setting = '' |
5 | 5 |
|
6 | | -func ListMonths() |
7 | | - if g:setting != '' |
8 | | - exe ":set" g:setting |
9 | | - endif |
10 | | - call complete(col('.'), g:months) |
11 | | - return '' |
| 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 '' |
12 | 17 | endfunc |
13 | 18 |
|
| 19 | +func! Test_popup_complete() |
| 20 | + new |
| 21 | + inoremap <f5> <c-r>=ListMonths()<cr> |
| 22 | +
|
| 23 | + " <C-E> - select original typed text before the completion started |
| 24 | + call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx') |
| 25 | + call assert_equal(["Ju"], getline(1,2)) |
| 26 | + %d |
| 27 | + |
| 28 | + " <C-Y> - accept current match |
| 29 | + call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx') |
| 30 | + call assert_equal(["August"], getline(1,2)) |
| 31 | + %d |
| 32 | + |
| 33 | + " <BS> - Delete one character from the inserted text (state: 1) |
| 34 | + " TODO: This should not end the completion, but it does. |
| 35 | + " This should according to the documentation: |
| 36 | + " January |
| 37 | + " but instead, this does |
| 38 | + " Januar |
| 39 | + " (idea is, C-L inserts the match from the popup menu |
| 40 | + " but if the menu is closed, it will insert the character <c-l> |
| 41 | + call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx') |
| 42 | + call assert_equal(["Januar"], getline(1,2)) |
| 43 | + %d |
| 44 | + |
| 45 | + " any-non special character: Stop completion without changing the match |
| 46 | + " and insert the typed character |
| 47 | + call feedkeys("a\<f5>20", 'tx') |
| 48 | + call assert_equal(["January20"], getline(1,2)) |
| 49 | + %d |
| 50 | + |
| 51 | + " any-non printable, non-white character: Add this character and |
| 52 | + " reduce number of matches |
| 53 | + call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx') |
| 54 | + call assert_equal(["Jul"], getline(1,2)) |
| 55 | + %d |
| 56 | + |
| 57 | + " any-non printable, non-white character: Add this character and |
| 58 | + " reduce number of matches |
| 59 | + call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx') |
| 60 | + call assert_equal(["July"], getline(1,2)) |
| 61 | + %d |
| 62 | + |
| 63 | + " any-non printable, non-white character: Add this character and |
| 64 | + " reduce number of matches |
| 65 | + call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx') |
| 66 | + call assert_equal(["Jul"], getline(1,2)) |
| 67 | + %d |
| 68 | + |
| 69 | + " <BS> - Delete one character from the inserted text (state: 2) |
| 70 | + call feedkeys("a\<f5>\<c-n>\<bs>", 'tx') |
| 71 | + call assert_equal(["Februar"], getline(1,2)) |
| 72 | + %d |
| 73 | + |
| 74 | + " <c-l> - Insert one character from the current match |
| 75 | + call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx') |
| 76 | + call assert_equal(["J"], getline(1,2)) |
| 77 | + %d |
| 78 | + |
| 79 | + " <c-l> - Insert one character from the current match |
| 80 | + call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx') |
| 81 | + call assert_equal(["January"], getline(1,2)) |
| 82 | + %d |
| 83 | + |
| 84 | + " <c-y> - Accept current selected match |
| 85 | + call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx') |
| 86 | + call assert_equal(["January"], getline(1,2)) |
| 87 | + %d |
| 88 | + |
| 89 | + " <c-e> - End completion, go back to what was there before selecting a match |
| 90 | + call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx') |
| 91 | + call assert_equal(["Ju"], getline(1,2)) |
| 92 | + %d |
| 93 | + |
| 94 | + " <PageUp> - Select a match several entries back |
| 95 | + call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx') |
| 96 | + call assert_equal([""], getline(1,2)) |
| 97 | + %d |
| 98 | + |
| 99 | + " <PageUp><PageUp> - Select a match several entries back |
| 100 | + call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx') |
| 101 | + call assert_equal(["December"], getline(1,2)) |
| 102 | + %d |
| 103 | + |
| 104 | + " <PageUp><PageUp><PageUp> - Select a match several entries back |
| 105 | + call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx') |
| 106 | + call assert_equal(["February"], getline(1,2)) |
| 107 | + %d |
| 108 | + |
| 109 | + " <PageDown> - Select a match several entries further |
| 110 | + call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx') |
| 111 | + call assert_equal(["November"], getline(1,2)) |
| 112 | + %d |
| 113 | + |
| 114 | + " <PageDown><PageDown> - Select a match several entries further |
| 115 | + call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx') |
| 116 | + call assert_equal(["December"], getline(1,2)) |
| 117 | + %d |
| 118 | + |
| 119 | + " <PageDown><PageDown><PageDown> - Select a match several entries further |
| 120 | + call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx') |
| 121 | + call assert_equal([""], getline(1,2)) |
| 122 | + %d |
| 123 | + |
| 124 | + " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further |
| 125 | + call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx') |
| 126 | + call assert_equal(["October"], getline(1,2)) |
| 127 | + %d |
| 128 | + |
| 129 | + " <Up> - Select a match don't insert yet |
| 130 | + call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx') |
| 131 | + call assert_equal([""], getline(1,2)) |
| 132 | + %d |
| 133 | + |
| 134 | + " <Up><Up> - Select a match don't insert yet |
| 135 | + call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx') |
| 136 | + call assert_equal(["December"], getline(1,2)) |
| 137 | + %d |
| 138 | + |
| 139 | + " <Up><Up><Up> - Select a match don't insert yet |
| 140 | + call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx') |
| 141 | + call assert_equal(["November"], getline(1,2)) |
| 142 | + %d |
| 143 | + |
| 144 | + " <Tab> - Stop completion and insert the match |
| 145 | + call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx') |
| 146 | + call assert_equal(["January "], getline(1,2)) |
| 147 | + %d |
| 148 | + |
| 149 | + " <Space> - Stop completion and insert the match |
| 150 | + call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx') |
| 151 | + call assert_equal(["September "], getline(1,2)) |
| 152 | + %d |
| 153 | + |
| 154 | + " <Enter> - Use the text and insert line break (state: 1) |
| 155 | + call feedkeys("a\<f5>\<enter>\<esc>", 'tx') |
| 156 | + call assert_equal(["January", ''], getline(1,2)) |
| 157 | + %d |
| 158 | + |
| 159 | + " <Enter> - Insert the current selected text (state: 2) |
| 160 | + call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx') |
| 161 | + call assert_equal(["September"], getline(1,2)) |
| 162 | + %d |
| 163 | + |
| 164 | + " Insert match immediately, if there is only one match |
| 165 | + " <c-y> selects a character from the line above |
| 166 | + call append(0, ["December2015"]) |
| 167 | + call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx') |
| 168 | + call assert_equal(["December2015", "December2015", ""], getline(1,3)) |
| 169 | + %d |
| 170 | + |
| 171 | + " Insert match immediately, if there is only one match |
| 172 | + " <c-e> Should select a character from the line below |
| 173 | + call append(1, ["December2015"]) |
| 174 | + :1 |
| 175 | + call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx') |
| 176 | + call assert_equal(["December2015", "", "December2015"], getline(1,3)) |
| 177 | + %d |
| 178 | + |
| 179 | + " use menuone for 'completeopt' |
| 180 | + " Since for the first <c-y> the menu is still shown, will only select |
| 181 | + " three letters from the line above |
| 182 | + set completeopt&vim |
| 183 | + set completeopt+=menuone |
| 184 | + call append(0, ["December2015"]) |
| 185 | + call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx') |
| 186 | + call assert_equal(["December2015", "December201", ""], getline(1,3)) |
| 187 | + %d |
| 188 | + |
| 189 | + " use longest for 'completeopt' |
| 190 | + set completeopt&vim |
| 191 | + call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx') |
| 192 | + set completeopt+=longest |
| 193 | + call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx') |
| 194 | + call assert_equal(["M", "Ma", ""], getline(1,3)) |
| 195 | + %d |
| 196 | + |
| 197 | + " use noselect/noinsert for 'completeopt' |
| 198 | + set completeopt&vim |
| 199 | + call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 200 | + set completeopt+=noselect |
| 201 | + call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 202 | + set completeopt-=noselect completeopt+=noinsert |
| 203 | + call feedkeys("aM\<f5>\<enter>\<esc>", 'tx') |
| 204 | + call assert_equal(["March", "M", "March"], getline(1,4)) |
| 205 | + %d |
| 206 | +endfu |
| 207 | + |
| 208 | + |
14 | 209 | func! Test_popup_completion_insertmode() |
15 | 210 | new |
16 | 211 | inoremap <F5> <C-R>=ListMonths()<CR> |
17 | 212 |
|
18 | 213 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
19 | 214 | call assert_equal('February', getline(1)) |
20 | 215 | %d |
| 216 | + " Set noinsertmode |
21 | 217 | let g:setting = 'noinsertmode' |
22 | 218 | call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx') |
23 | 219 | call assert_equal('February', getline(1)) |
24 | 220 | call assert_false(pumvisible()) |
25 | 221 | %d |
| 222 | + " Go through all matches, until none is selected |
26 | 223 | let g:setting = '' |
27 | 224 | call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx') |
28 | 225 | call assert_equal('', getline(1)) |
29 | 226 | %d |
| 227 | + " select previous entry |
30 | 228 | call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx') |
31 | 229 | call assert_equal('', getline(1)) |
32 | 230 | %d |
| 231 | + " select last entry |
33 | 232 | call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx') |
34 | 233 | call assert_equal('December', getline(1)) |
35 | 234 |
|
@@ -66,3 +265,5 @@ function! Test() abort |
66 | 265 | call complete(1, ['source', 'soundfold']) |
67 | 266 | return '' |
68 | 267 | endfunction |
| 268 | + |
| 269 | +" vim: shiftwidth=2 sts=2 expandtab |
0 commit comments