blob: dd949334c8db8d67a374a638fffa5deefe22b6cd [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 Moolenaardac19472016-09-03 22:35:40 +020019func! Test_popup_complete2()
20 " Insert match immediately, if there is only one match
21 " <c-e> Should select a character from the line below
22 " TODO: test disabled because the code change has been reverted.
23 throw "Skipped: Bug with <c-e> and popupmenu not fixed yet"
24 new
25 inoremap <f5> <c-r>=ListMonths()<cr>
26 call append(1, ["December2015"])
27 :1
28 call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx')
29 call assert_equal(["December2015", "", "December2015"], getline(1,3))
30 %d
31 bw!
32endfu
33
Bram Moolenaar47247282016-08-02 22:36:02 +020034func! Test_popup_complete()
35 new
36 inoremap <f5> <c-r>=ListMonths()<cr>
37
38 " <C-E> - select original typed text before the completion started
39 call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx')
40 call assert_equal(["Ju"], getline(1,2))
41 %d
42
43 " <C-Y> - accept current match
44 call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx')
45 call assert_equal(["August"], getline(1,2))
46 %d
47
48 " <BS> - Delete one character from the inserted text (state: 1)
49 " TODO: This should not end the completion, but it does.
50 " This should according to the documentation:
51 " January
52 " but instead, this does
53 " Januar
54 " (idea is, C-L inserts the match from the popup menu
55 " but if the menu is closed, it will insert the character <c-l>
56 call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx')
57 call assert_equal(["Januar "], getline(1,2))
58 %d
59
60 " any-non special character: Stop completion without changing the match
61 " and insert the typed character
62 call feedkeys("a\<f5>20", 'tx')
63 call assert_equal(["January20"], getline(1,2))
64 %d
65
66 " any-non printable, non-white character: Add this character and
67 " reduce number of matches
68 call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx')
69 call assert_equal(["Jul"], getline(1,2))
70 %d
71
72 " any-non printable, non-white character: Add this character and
73 " reduce number of matches
74 call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx')
75 call assert_equal(["July"], getline(1,2))
76 %d
77
78 " any-non printable, non-white character: Add this character and
79 " reduce number of matches
80 call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx')
81 call assert_equal(["Jul"], getline(1,2))
82 %d
83
84 " <BS> - Delete one character from the inserted text (state: 2)
85 call feedkeys("a\<f5>\<c-n>\<bs>", 'tx')
86 call assert_equal(["Februar"], getline(1,2))
87 %d
88
89 " <c-l> - Insert one character from the current match
90 call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx')
91 call assert_equal(["J "], getline(1,2))
92 %d
93
94 " <c-l> - Insert one character from the current match
95 call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx')
96 call assert_equal(["January "], getline(1,2))
97 %d
98
99 " <c-y> - Accept current selected match
100 call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx')
101 call assert_equal(["January"], getline(1,2))
102 %d
103
104 " <c-e> - End completion, go back to what was there before selecting a match
105 call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx')
106 call assert_equal(["Ju"], getline(1,2))
107 %d
108
109 " <PageUp> - Select a match several entries back
110 call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx')
111 call assert_equal([""], getline(1,2))
112 %d
113
114 " <PageUp><PageUp> - Select a match several entries back
115 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
116 call assert_equal(["December"], getline(1,2))
117 %d
118
119 " <PageUp><PageUp><PageUp> - Select a match several entries back
120 call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
121 call assert_equal(["February"], getline(1,2))
122 %d
123
124 " <PageDown> - Select a match several entries further
125 call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx')
126 call assert_equal(["November"], getline(1,2))
127 %d
128
129 " <PageDown><PageDown> - Select a match several entries further
130 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
131 call assert_equal(["December"], getline(1,2))
132 %d
133
134 " <PageDown><PageDown><PageDown> - Select a match several entries further
135 call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
136 call assert_equal([""], getline(1,2))
137 %d
138
139 " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further
140 call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx')
141 call assert_equal(["October"], getline(1,2))
142 %d
143
144 " <Up> - Select a match don't insert yet
145 call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx')
146 call assert_equal([""], getline(1,2))
147 %d
148
149 " <Up><Up> - Select a match don't insert yet
150 call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx')
151 call assert_equal(["December"], getline(1,2))
152 %d
153
154 " <Up><Up><Up> - Select a match don't insert yet
155 call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx')
156 call assert_equal(["November"], getline(1,2))
157 %d
158
159 " <Tab> - Stop completion and insert the match
160 call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx')
161 call assert_equal(["January "], getline(1,2))
162 %d
163
164 " <Space> - Stop completion and insert the match
165 call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx')
166 call assert_equal(["September "], getline(1,2))
167 %d
168
169 " <Enter> - Use the text and insert line break (state: 1)
170 call feedkeys("a\<f5>\<enter>\<esc>", 'tx')
171 call assert_equal(["January", ''], getline(1,2))
172 %d
173
174 " <Enter> - Insert the current selected text (state: 2)
175 call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx')
176 call assert_equal(["September"], getline(1,2))
177 %d
178
179 " Insert match immediately, if there is only one match
180 " <c-y> selects a character from the line above
181 call append(0, ["December2015"])
182 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
183 call assert_equal(["December2015", "December2015", ""], getline(1,3))
184 %d
185
Bram Moolenaar47247282016-08-02 22:36:02 +0200186 " use menuone for 'completeopt'
187 " Since for the first <c-y> the menu is still shown, will only select
188 " three letters from the line above
189 set completeopt&vim
190 set completeopt+=menuone
191 call append(0, ["December2015"])
192 call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
193 call assert_equal(["December2015", "December201", ""], getline(1,3))
194 %d
195
196 " use longest for 'completeopt'
197 set completeopt&vim
198 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
199 set completeopt+=longest
200 call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
201 call assert_equal(["M", "Ma", ""], getline(1,3))
202 %d
203
204 " use noselect/noinsert for 'completeopt'
205 set completeopt&vim
206 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
207 set completeopt+=noselect
208 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
209 set completeopt-=noselect completeopt+=noinsert
210 call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
211 call assert_equal(["March", "M", "March"], getline(1,4))
212 %d
213endfu
214
215
Bram Moolenaar00672e12016-06-26 18:38:13 +0200216func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200217 new
218 inoremap <F5> <C-R>=ListMonths()<CR>
219
220 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
221 call assert_equal('February', getline(1))
222 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200223 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200224 let g:setting = 'noinsertmode'
225 call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
226 call assert_equal('February', getline(1))
227 call assert_false(pumvisible())
228 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200229 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200230 let g:setting = ''
231 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
232 call assert_equal('', getline(1))
233 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200234 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200235 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
236 call assert_equal('', getline(1))
237 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200238 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200239 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
240 call assert_equal('December', getline(1))
241
Bram Moolenaar67081e52016-07-09 21:49:03 +0200242 iunmap <F5>
243endfunc
244
245function! ComplTest() abort
246 call complete(1, ['source', 'soundfold'])
247 return ''
248endfunction
249
250func Test_noinsert_complete()
251 new
252 set completeopt+=noinsert
253 inoremap <F5> <C-R>=ComplTest()<CR>
254 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
255 call assert_equal('soundfold', getline(1))
256 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200257 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200258
259 new
260 inoremap <F5> <C-R>=Test()<CR>
261 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
262 call assert_equal('source', getline(1))
263 bwipe!
264
Bram Moolenaar67081e52016-07-09 21:49:03 +0200265 set completeopt-=noinsert
266 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200267endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200268
269
270function! Test() abort
271 call complete(1, ['source', 'soundfold'])
272 return ''
273endfunction
Bram Moolenaar47247282016-08-02 22:36:02 +0200274
275" vim: shiftwidth=2 sts=2 expandtab