@@ -1560,24 +1560,41 @@ get_leader_for_startcol(compl_T *match, int cached)
15601560}
15611561
15621562/*
1563- * Set fuzzy score.
1563+ * Set fuzzy score for completion matches .
15641564 */
15651565 static void
15661566set_fuzzy_score (void )
15671567{
15681568 compl_T * compl ;
1569+ char_u * pattern ;
1570+ int use_leader ;
15691571
1570- if (!compl_first_match
1571- || compl_leader .string == NULL || compl_leader .length == 0 )
1572+ if (compl_first_match == NULL )
15721573 return ;
15731574
1574- (void )get_leader_for_startcol (NULL , TRUE); // Clear the cache
1575+ /* Determine the pattern to match against */
1576+ use_leader = (compl_leader .string != NULL && compl_leader .length > 0 );
1577+ if (!use_leader )
1578+ {
1579+ if (compl_orig_text .string == NULL || compl_orig_text .length == 0 )
1580+ return ;
1581+ pattern = compl_orig_text .string ;
1582+ }
1583+ else
1584+ {
1585+ /* Clear the leader cache once before the loop */
1586+ (void )get_leader_for_startcol (NULL , TRUE);
1587+ pattern = NULL ; /* Will be computed per-completion */
1588+ }
15751589
1590+ /* Score all completion matches */
15761591 compl = compl_first_match ;
15771592 do
15781593 {
1579- compl -> cp_score = fuzzy_match_str (compl -> cp_str .string ,
1580- get_leader_for_startcol (compl , TRUE)-> string );
1594+ if (use_leader )
1595+ pattern = get_leader_for_startcol (compl , TRUE)-> string ;
1596+
1597+ compl -> cp_score = fuzzy_match_str (compl -> cp_str .string , pattern );
15811598 compl = compl -> cp_next ;
15821599 } while (compl != NULL && !is_first_match (compl ));
15831600}
@@ -2471,6 +2488,30 @@ ins_compl_has_autocomplete(void)
24712488 return curbuf -> b_p_ac >= 0 ? curbuf -> b_p_ac : p_ac ;
24722489}
24732490
2491+ /*
2492+ * Cacluate fuzzy score and sort completion matches unless sorting is disabled.
2493+ */
2494+ static void
2495+ ins_compl_fuzzy_sort (void )
2496+ {
2497+ int cur_cot_flags = get_cot_flags ();
2498+
2499+ // set the fuzzy score in cp_score
2500+ set_fuzzy_score ();
2501+ // Sort the matches linked list based on fuzzy score
2502+ if (!(cur_cot_flags & COT_NOSORT ))
2503+ {
2504+ sort_compl_match_list (cp_compare_fuzzy );
2505+ if ((cur_cot_flags & (COT_NOINSERT | COT_NOSELECT )) == COT_NOINSERT
2506+ && compl_first_match )
2507+ {
2508+ compl_shown_match = compl_first_match ;
2509+ if (compl_shows_dir_forward () && !compl_autocomplete )
2510+ compl_shown_match = compl_first_match -> cp_next ;
2511+ }
2512+ }
2513+ }
2514+
24742515/*
24752516 * Called after changing "compl_leader".
24762517 * Show the popup menu with a different set of matches.
@@ -2479,7 +2520,6 @@ ins_compl_has_autocomplete(void)
24792520 static void
24802521ins_compl_new_leader (void )
24812522{
2482- int cur_cot_flags = get_cot_flags ();
24832523 int save_w_wrow = curwin -> w_wrow ;
24842524 int save_w_leftcol = curwin -> w_leftcol ;
24852525
@@ -2499,6 +2539,8 @@ ins_compl_new_leader(void)
24992539 ins_compl_set_original_text (compl_leader .string , compl_leader .length );
25002540 if (is_cpt_func_refresh_always ())
25012541 cpt_compl_refresh ();
2542+ if (get_cot_flags () & COT_FUZZY )
2543+ ins_compl_fuzzy_sort ();
25022544 }
25032545 else
25042546 {
@@ -2529,24 +2571,6 @@ ins_compl_new_leader(void)
25292571 compl_restarting = FALSE;
25302572 }
25312573
2532- // When 'cot' contains "fuzzy" set the cp_score and maybe sort
2533- if (cur_cot_flags & COT_FUZZY )
2534- {
2535- set_fuzzy_score ();
2536- // Sort the matches linked list based on fuzzy score
2537- if (!(cur_cot_flags & COT_NOSORT ))
2538- {
2539- sort_compl_match_list (cp_compare_fuzzy );
2540- if ((cur_cot_flags & (COT_NOINSERT | COT_NOSELECT )) == COT_NOINSERT
2541- && compl_first_match )
2542- {
2543- compl_shown_match = compl_first_match ;
2544- if (compl_shows_dir_forward () && !compl_autocomplete )
2545- compl_shown_match = compl_first_match -> cp_next ;
2546- }
2547- }
2548- }
2549-
25502574 compl_enter_selects = !compl_used_match && compl_selected_item != -1 ;
25512575
25522576 // Show the popup menu with a different set of matches.
@@ -5677,6 +5701,9 @@ ins_compl_get_exp(pos_T *ini)
56775701 if (is_nearest_active () && !ins_compl_has_preinsert ())
56785702 sort_compl_match_list (cp_compare_nearest );
56795703
5704+ if ((get_cot_flags () & COT_FUZZY ) && ins_compl_leader_len () > 0 )
5705+ ins_compl_fuzzy_sort ();
5706+
56805707 return match_count ;
56815708}
56825709
@@ -6436,7 +6463,9 @@ ins_compl_check_keys(int frequency, int in_compl_func)
64366463 check_elapsed_time ();
64376464 }
64386465
6439- if (compl_pending && !got_int && !(cot_flags & COT_NOINSERT )
6466+ if (compl_pending
6467+ && !got_int
6468+ && !(cot_flags & (COT_NOINSERT | COT_FUZZY ))
64406469 && (!compl_autocomplete || ins_compl_has_preinsert ()))
64416470 {
64426471 // Insert the first match immediately and advance compl_shown_match,
0 commit comments