patch 9.1.1049: insert-completed items are always sorted
Problem: insert-completed items are always sorted, although the LSP
spec[1] standard defines sortText in the returned
completionitem list. This means that the server has sorted the
results. When fuzzy is enabled, this will break the server's
sorting results.
Solution: disable sorting of candidates when "nosort" is set in
'completeopt'
[1]
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem
closes: #16501
Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/insexpand.c b/src/insexpand.c
index e33e15d..1a71f63 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -1264,7 +1264,8 @@
int max_fuzzy_score = 0;
unsigned int cur_cot_flags = get_cot_flags();
int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
- int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
+ int fuzzy_nosort = (cur_cot_flags & COT_NOSORT) != 0;
+ int fuzzy_filter = fuzzy_nosort || (cur_cot_flags & COT_FUZZY) != 0;
compl_T *match_head = NULL;
compl_T *match_tail = NULL;
compl_T *match_next = NULL;
@@ -1289,13 +1290,13 @@
compl->cp_in_match_array = FALSE;
// When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
// set the cp_score for later comparisons.
- if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
+ if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
if (!match_at_original_text(compl)
&& (compl_leader.string == NULL
|| ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
- || (compl_fuzzy_match && compl->cp_score > 0)))
+ || (fuzzy_filter && compl->cp_score > 0)))
{
++compl_match_arraysize;
compl->cp_in_match_array = TRUE;
@@ -1305,7 +1306,7 @@
match_tail->cp_match_next = compl;
match_tail = compl;
- if (!shown_match_ok && !compl_fuzzy_match)
+ if (!shown_match_ok && !fuzzy_filter)
{
if (compl == compl_shown_match || did_find_shown_match)
{
@@ -1321,19 +1322,21 @@
shown_compl = compl;
cur = i;
}
- else if (compl_fuzzy_match)
+ else if (fuzzy_filter)
{
if (i == 0)
shown_compl = compl;
// Update the maximum fuzzy score and the shown match
// if the current item's score is higher
- if (compl->cp_score > max_fuzzy_score)
+ if (!fuzzy_nosort && compl->cp_score > max_fuzzy_score)
{
did_find_shown_match = TRUE;
max_fuzzy_score = compl->cp_score;
if (!compl_no_select)
compl_shown_match = compl;
}
+ else if (fuzzy_nosort && i == 0 && !compl_no_select)
+ compl_shown_match = shown_compl;
if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
{
@@ -1344,7 +1347,7 @@
i++;
}
- if (compl == compl_shown_match && !compl_fuzzy_match)
+ if (compl == compl_shown_match && !fuzzy_filter)
{
did_find_shown_match = TRUE;
@@ -1389,7 +1392,7 @@
compl = match_next;
}
- if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
+ if (fuzzy_filter && !fuzzy_nosort && compl_leader.string != NULL && compl_leader.length > 0)
{
for (i = 0; i < compl_match_arraysize; i++)
compl_match_array[i].pum_idx = i;