blob: 708739cb229b2f078403599eba679c63874f7b62 [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
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
206endfu
207
208
Bram Moolenaar00672e12016-06-26 18:38:13 +0200209func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200210 new
211 inoremap <F5> <C-R>=ListMonths()<CR>
212
213 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
214 call assert_equal('February', getline(1))
215 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200216 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200217 let g:setting = 'noinsertmode'
218 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
219 call assert_equal('February', getline(1))
220 call assert_false(pumvisible())
221 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200222 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200223 let g:setting = ''
224 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
225 call assert_equal('', getline(1))
226 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200227 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200228 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
229 call assert_equal('', getline(1))
230 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200231 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200232 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
233 call assert_equal('December', getline(1))
234
Bram Moolenaar67081e52016-07-09 21:49:03 +0200235 iunmap <F5>
236endfunc
237
238function! ComplTest() abort
239 call complete(1, ['source', 'soundfold'])
240 return ''
241endfunction
242
243func Test_noinsert_complete()
244 new
245 set completeopt+=noinsert
246 inoremap <F5> <C-R>=ComplTest()<CR>
247 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
248 call assert_equal('soundfold', getline(1))
249 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200250 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200251
252 new
253 inoremap <F5> <C-R>=Test()<CR>
254 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
255 call assert_equal('source', getline(1))
256 bwipe!
257
Bram Moolenaar67081e52016-07-09 21:49:03 +0200258 set completeopt-=noinsert
259 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200260endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200261
262
263function! Test() abort
264 call complete(1, ['source', 'soundfold'])
265 return ''
266endfunction
Bram Moolenaar47247282016-08-02 22:36:02 +0200267
268" vim: shiftwidth=2 sts=2 expandtab