From c7ffe451a6e399beee0c1cae53ae9025ebed0e4d Mon Sep 17 00:00:00 2001 From: Girish Palya Date: Thu, 27 Nov 2025 07:50:12 +0100 Subject: [PATCH] Fix issue #18802: Reset selected item after fuzzy sort --- src/insexpand.c | 18 ++++++++++-------- src/testdir/test_ins_complete.vim | 9 +++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/insexpand.c b/src/insexpand.c index 8ec549ae020290..a5f7716d3245dd 100644 --- a/src/insexpand.c +++ b/src/insexpand.c @@ -2496,18 +2496,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; } } } diff --git a/src/testdir/test_ins_complete.vim b/src/testdir/test_ins_complete.vim index 77be5b42e7ac1d..3146f01e342545 100644 --- a/src/testdir/test_ins_complete.vim +++ b/src/testdir/test_ins_complete.vim @@ -3575,6 +3575,15 @@ func Test_complete_opt_fuzzy() call feedkeys("Gof\\=PrintMenuWords()\\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\\\", 'tx') + call assert_equal('aa', getline('.')) + call feedkeys("Goa\\\", 'tx') + call assert_equal('aaaa', getline('.')) + " clean up set omnifunc= bw!