blob: 34a2251ab4ee14792a6456824d2c7745d20714a3 [file] [log] [blame]
Bram Moolenaar00672e12016-06-26 18:38:13 +02001" Test for completion menu
2
Bram Moolenaar00672e12016-06-26 18:38:13 +02003let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
4let g:setting = ''
5
Bram Moolenaar47247282016-08-02 22:36:02 +02006func! 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 Moolenaar00672e12016-06-26 18:38:13 +020017endfunc
18
Bram Moolenaar47247282016-08-02 22:36:02 +020019func! 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
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +0200173 " TODO: test disabled because the code change has been reverted.
174 " call append(1, ["December2015"])
175 " :1
176 " call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx')
177 " call assert_equal(["December2015", "", "December2015"], getline(1,3))
178 " %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200179
180 " use menuone for 'completeopt'
181 " Since for the first <c-y> the menu is still shown, will only select
182 " three letters from the line above
183 set completeopt&vim
184 set completeopt+=menuone
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", "December201", ""], getline(1,3))
188 %d
189
190 " use longest for 'completeopt'
191 set completeopt&vim
192 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
193 set completeopt+=longest
194 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
195 call assert_equal(["M", "Ma", ""], getline(1,3))
196 %d
197
198 " use noselect/noinsert for 'completeopt'
199 set completeopt&vim
200 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
201 set completeopt+=noselect
202 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
203 set completeopt-=noselect completeopt+=noinsert
204 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
205 call assert_equal(["March", "M", "March"], getline(1,4))
206 %d
207endfu
208
209
Bram Moolenaar00672e12016-06-26 18:38:13 +0200210func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200211 new
212 inoremap <F5> <C-R>=ListMonths()<CR>
213
214 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
215 call assert_equal('February', getline(1))
216 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200217 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200218 let g:setting = 'noinsertmode'
219 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
220 call assert_equal('February', getline(1))
221 call assert_false(pumvisible())
222 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200223 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200224 let g:setting = ''
225 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
226 call assert_equal('', getline(1))
227 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200228 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200229 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
230 call assert_equal('', getline(1))
231 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200232 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200233 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
234 call assert_equal('December', getline(1))
235
Bram Moolenaar67081e52016-07-09 21:49:03 +0200236 iunmap <F5>
237endfunc
238
239function! ComplTest() abort
240 call complete(1, ['source', 'soundfold'])
241 return ''
242endfunction
243
244func Test_noinsert_complete()
245 new
246 set completeopt+=noinsert
247 inoremap <F5> <C-R>=ComplTest()<CR>
248 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
249 call assert_equal('soundfold', getline(1))
250 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200251 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200252
253 new
254 inoremap <F5> <C-R>=Test()<CR>
255 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
256 call assert_equal('source', getline(1))
257 bwipe!
258
Bram Moolenaar67081e52016-07-09 21:49:03 +0200259 set completeopt-=noinsert
260 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200261endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200262
263
264function! Test() abort
265 call complete(1, ['source', 'soundfold'])
266 return ''
267endfunction
Bram Moolenaar47247282016-08-02 22:36:02 +0200268
269" vim: shiftwidth=2 sts=2 expandtab