]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1931: completion: wrong item selected with fuzzy and noinsert v9.1.1931
authorGirish Palya <girishji@gmail.com>
Thu, 27 Nov 2025 20:20:38 +0000 (20:20 +0000)
committerChristian Brabandt <cb@256bit.org>
Thu, 27 Nov 2025 20:20:38 +0000 (20:20 +0000)
Problem:  completion: wrong item selected with fuzzy and noinsert
          (Evgeni Chasnovski)
Solution: Reset selected item after fuzzy sort
          (Girish Palya)

fixes: #18802
closes: #18816

Signed-off-by: Girish Palya <girishji@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/insexpand.c
src/testdir/test_ins_complete.vim
src/version.c

index ea4ce495bf8e17f612add5bc97fb486038b975d9..1bcadadf83911d85378f52bd24f9a2afec4e6658 100644 (file)
@@ -2480,18 +2480,20 @@ ins_compl_fuzzy_sort(void)
 {
     int            cur_cot_flags = get_cot_flags();
 
-    // set the fuzzy score in cp_score
+    // Set the fuzzy score in cp_score and sort
     set_fuzzy_score();
-    // Sort the matches linked list based on fuzzy score
     if (!(cur_cot_flags & COT_NOSORT))
     {
        sort_compl_match_list(cp_compare_fuzzy);
-       if ((cur_cot_flags & (COT_NOINSERT | COT_NOSELECT)) == COT_NOINSERT
-               && compl_first_match)
-       {
-           compl_shown_match = compl_first_match;
-           if (compl_shows_dir_forward() && !compl_autocomplete)
-               compl_shown_match = compl_first_match->cp_next;
+       // Reset the shown item since sorting reorders items
+       if ((cur_cot_flags & (COT_NOINSERT | COT_NOSELECT)) == COT_NOINSERT)
+       {
+           int none_selected = compl_shown_match == (compl_shows_dir_forward()
+                   ? compl_first_match : compl_first_match->cp_prev);
+           if (!none_selected)
+               compl_shown_match
+                   = (!compl_autocomplete && compl_shows_dir_forward())
+                   ? compl_first_match->cp_next : compl_first_match;
        }
     }
 }
index c2268682fdfdb8266ea268b521a580da8a1cdf71..194422b34ac2c276e84b11a455cbd5f07f4a36b0 100644 (file)
@@ -3593,6 +3593,15 @@ func Test_complete_opt_fuzzy()
   call feedkeys("Gof\<C-N>\<C-R>=PrintMenuWords()\<CR>\<Esc>0", 'tx')
   call assert_equal('f{''items'': [''func1'', ''func2'', ''xfunc'']}', getline('.'))
 
+  " Issue #18802: Reset selected item after fuzzy sort
+  %d
+  call setline(1, ['aa', 'aaa', 'aaaa'])
+  set completeopt=menuone,noinsert,fuzzy
+  call feedkeys("Goa\<C-N>\<C-Y>\<Esc>", 'tx')
+  call assert_equal('aa', getline('.'))
+  call feedkeys("Goa\<C-P>\<C-Y>\<Esc>", 'tx')
+  call assert_equal('aaaa', getline('.'))
+
   " clean up
   set omnifunc=
   bw!
index e2619a3397d3fe5e64241b5708f83ba057c484ac..75095d126de4755ee4d05477880d4ead863882af 100644 (file)
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1931,
 /**/
     1930,
 /**/