comparison src/testdir/test_ins_complete.vim @ 33674:021e5bb88513 v9.0.2074

patch 9.0.2074: Completion menu may be wrong Commit: https://github.com/vim/vim/commit/daef8c74375141974d61b85199b383017644978c Author: Christian Brabandt <cb@256bit.org> Date: Fri Oct 27 19:16:26 2023 +0200 patch 9.0.2074: Completion menu may be wrong Problem: Completion menu may be wrong Solution: Check for the original direction of the completion menu, add more tests, make it work with 'noselect' completion: move in right direction when filling completion_info() When moving through the insert completion menu and switching directions, we need to make sure we start at the correct position in the list and move correctly forward/backwards through it, so that we do not skip entries and the selected item points to the correct entry in the list of completion entries generated by the completion_info() function. The general case is this: 1) CTRL-X CTRL-N, we will traverse the list starting from compl_first_match and then go forwards (using the cp->next pointer) through the list (skipping the very first entry, which has the CP_ORIGINAL_TEXT flag set (since that is the empty/non-selected entry 2) CTRL-X CTRL-P, we will traverse the list starting from compl_first_match (which now points to the last entry). The previous entry will have the CP_ORIGINAL_TEXT flag set, so we need to start traversing the list from the second prev pointer. There are in fact 2 special cases after starting the completion menu with CTRL-X: 3) CTRL-N and then going backwards by pressing CTRL-P again. compl_first_match will point to the same entry as in step 1 above, but since compl_dir_foward() has been switched by pressing CTRL-P to backwards we need to pretend to be in still in case 1 and still traverse the list in forward direction using the cp_next pointer 4) CTRL-P and then going forwards by pressing CTRL-N again. compl_first_match will point to the same entry as in step 2 above, but since compl_dir_foward() has been switched by pressing CTRL-N to forwards we need to pretend to be in still in case 2 and still traverse the list in backward direction using the cp_prev pointer For the 'noselect' case however, this is slightly different again. When going backwards, we only need to go one cp_prev pointer back. And resting of the direction works again slightly different. So we need to take the noselect option into account when deciding in which direction to iterate through the list of matches. related: #13402 related: #12971 closes: #13408 Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Fri, 27 Oct 2023 19:30:05 +0200
parents efe4751b93b1
children 76ff7b7b44f2
comparison
equal deleted inserted replaced
33673:26c9f7fccaeb 33674:021e5bb88513
2265 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR> 2265 inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
2266 2266
2267 " Ensure 'index' in complete_info() is coherent with the 'items' array. 2267 " Ensure 'index' in complete_info() is coherent with the 'items' array.
2268 2268
2269 set completeopt=menu,preview 2269 set completeopt=menu,preview
2270 " Search forward. 2270 " Search forward
2271 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') 2271 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2272 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word']) 2272 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2273 call assert_equal(6 , len(g:compl_info['items']))
2273 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx') 2274 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2274 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word']) 2275 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2275 2276 call assert_equal(6 , len(g:compl_info['items']))
2276 " Search backward. 2277 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2278 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2279 call assert_equal(6 , len(g:compl_info['items']))
2280 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2281 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2282 call assert_equal(6 , len(g:compl_info['items']))
2283 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2284 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2285 call assert_equal(6 , len(g:compl_info['items']))
2286 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2287 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2288 call assert_equal(6 , len(g:compl_info['items']))
2289 " Search forward: unselected item
2290 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2291 call assert_equal(6 , len(g:compl_info['items']))
2292 call assert_equal(-1 , g:compl_info['selected'])
2293
2294 " Search backward
2277 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx') 2295 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2278 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word']) 2296 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2297 call assert_equal(6 , len(g:compl_info['items']))
2279 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx') 2298 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2280 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word']) 2299 call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
2300 call assert_equal(6 , len(g:compl_info['items']))
2301 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2302 call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
2303 call assert_equal(6 , len(g:compl_info['items']))
2304 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2305 call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
2306 call assert_equal(6 , len(g:compl_info['items']))
2307 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2308 call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
2309 call assert_equal(6 , len(g:compl_info['items']))
2310 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2311 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2312 call assert_equal(6 , len(g:compl_info['items']))
2313 " search backwards: unselected item
2314 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2315 call assert_equal(6 , len(g:compl_info['items']))
2316 call assert_equal(-1 , g:compl_info['selected'])
2317
2318 " switch direction: forwards, then backwards
2319 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2320 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2321 call assert_equal(6 , len(g:compl_info['items']))
2322 " switch direction: forwards, then backwards, then forwards again
2323 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
2324 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
2325 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2326 call assert_equal(6 , len(g:compl_info['items']))
2327
2328 " switch direction: backwards, then forwards
2329 call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2330 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2331 call assert_equal(6 , len(g:compl_info['items']))
2332 " switch direction: backwards, then forwards, then backwards again
2333 call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2334 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2335 call assert_equal(6 , len(g:compl_info['items']))
2281 2336
2282 " Add 'noselect', check that 'selected' is -1 when nothing is selected. 2337 " Add 'noselect', check that 'selected' is -1 when nothing is selected.
2283 set completeopt+=noselect 2338 set completeopt+=noselect
2284 " Search forward. 2339 " Search forward.
2285 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx') 2340 call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
2287 2342
2288 " Search backward. 2343 " Search backward.
2289 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx') 2344 call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
2290 call assert_equal(-1, g:compl_info['selected']) 2345 call assert_equal(-1, g:compl_info['selected'])
2291 2346
2347 " Check if index out of range
2348 " https://github.com/vim/vim/pull/12971
2349 call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
2350 call assert_equal(0, g:compl_info['selected'])
2351 call assert_equal(6 , len(g:compl_info['items']))
2352 call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
2353 call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
2354 call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
2355 call assert_equal(6 , len(g:compl_info['items']))
2356
2292 set completeopt& 2357 set completeopt&
2293 bwipe! 2358 bwipe!
2294 endfunc 2359 endfunc
2295 2360
2296 " vim: shiftwidth=2 sts=2 expandtab 2361 " vim: shiftwidth=2 sts=2 expandtab