blob: 10eaf3a6326758d2b8eeeb007bffed8249c71bb9 [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()
Bram Moolenaar9e02cfa2016-09-22 21:27:11 +020020 " 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 Moolenaardac19472016-09-03 22:35:40 +020026 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 Moolenaar9e02cfa2016-09-22 21:27:11 +020031 call assert_equal(["Dece", "", "December2015"], getline(1,3))
Bram Moolenaardac19472016-09-03 22:35:40 +020032 %d
33 bw!
34endfu
35
Bram Moolenaar47247282016-08-02 22:36:02 +020036func! 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 Moolenaar47247282016-08-02 22:36:02 +0200188 " 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
215endfu
216
217
Bram Moolenaar00672e12016-06-26 18:38:13 +0200218func! Test_popup_completion_insertmode()
Bram Moolenaar67081e52016-07-09 21:49:03 +0200219 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 Moolenaar47247282016-08-02 22:36:02 +0200225 " Set noinsertmode
Bram Moolenaar67081e52016-07-09 21:49:03 +0200226 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 Moolenaar47247282016-08-02 22:36:02 +0200231 " Go through all matches, until none is selected
Bram Moolenaar67081e52016-07-09 21:49:03 +0200232 let g:setting = ''
233 call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
234 call assert_equal('', getline(1))
235 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200236 " select previous entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200237 call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
238 call assert_equal('', getline(1))
239 %d
Bram Moolenaar47247282016-08-02 22:36:02 +0200240 " select last entry
Bram Moolenaar67081e52016-07-09 21:49:03 +0200241 call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
242 call assert_equal('December', getline(1))
243
Bram Moolenaar67081e52016-07-09 21:49:03 +0200244 iunmap <F5>
245endfunc
246
Bram Moolenaar67081e52016-07-09 21:49:03 +0200247func Test_noinsert_complete()
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200248 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 Moolenaar67081e52016-07-09 21:49:03 +0200258 new
259 set completeopt+=noinsert
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200260 inoremap <F5> <C-R>=s:complTest1()<CR>
Bram Moolenaar67081e52016-07-09 21:49:03 +0200261 call feedkeys("i\<F5>soun\<CR>\<CR>\<ESC>.", 'tx')
262 call assert_equal('soundfold', getline(1))
263 call assert_equal('soundfold', getline(2))
Bram Moolenaar67081e52016-07-09 21:49:03 +0200264 bwipe!
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200265
266 new
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200267 inoremap <F5> <C-R>=s:complTest2()<CR>
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200268 call feedkeys("i\<F5>\<CR>\<ESC>", 'tx')
269 call assert_equal('source', getline(1))
270 bwipe!
271
Bram Moolenaar67081e52016-07-09 21:49:03 +0200272 set completeopt-=noinsert
273 iunmap <F5>
Bram Moolenaar00672e12016-06-26 18:38:13 +0200274endfunc
Bram Moolenaar32b808a2016-07-09 21:57:20 +0200275
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200276func 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 Moolenaar32b808a2016-07-09 21:57:20 +0200283
Bram Moolenaar33a80ee2016-09-05 21:51:14 +0200284 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!
290endfunc
Bram Moolenaar47247282016-08-02 22:36:02 +0200291
292" vim: shiftwidth=2 sts=2 expandtab