Skip to content

Commit f400a0c

Browse files
glepnirchrisbra
authored andcommitted
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 <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent df098fe commit f400a0c

7 files changed

Lines changed: 46 additions & 21 deletions

File tree

runtime/doc/options.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Jan 21
1+
*options.txt* For Vim version 9.1. Last change: 2025 Jan 23
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2164,6 +2164,10 @@ A jump table for the options with a short description can be found at |Q_op|.
21642164
characters can be skipped and matches can be found even
21652165
if the exact sequence is not typed.
21662166

2167+
nosort Disable sorting of completion candidates based on fuzzy
2168+
scores when "fuzzy" is enabled. Candidates will appear
2169+
in their original order.
2170+
21672171
*'completepopup'* *'cpp'*
21682172
'completepopup' 'cpp' string (default empty)
21692173
global

runtime/doc/version9.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2025 Jan 18
1+
*version9.txt* For Vim version 9.1. Last change: 2025 Jan 23
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41560,15 +41560,17 @@ Support for Wayland UI.
4156041560

4156141561
Support for the XDG Desktop Specification |xdg-base-dir|
4156241562

41563-
Support highlighting the matched text for insert-mode completion and
41564-
command-line completion in |ins-completion-menu|.
41565-
41566-
Support highlighting the completion kind in |ins-completion-menu|, see
41567-
|complete-items|.
41568-
4156941563
Support for translating messages in Vim script plugins using the |gettext()|
4157041564
and |bindtextdomain()| functions.
4157141565

41566+
Support highlighting the matched text and the completion kind for insert-mode
41567+
completion and command-line completion in |ins-completion-menu|, see
41568+
|complete-items|
41569+
41570+
Include the "linematch" algorithm for the 'diffopt' setting. This aligns
41571+
changes between buffers on similar lines improving the diff highlighting in
41572+
Vim
41573+
4157241574
*changed-9.2*
4157341575
Changed~
4157441576
-------
@@ -41623,9 +41625,7 @@ Changed~
4162341625
the "matches" key
4162441626
- |v:stacktrace| The stack trace of the exception most recently caught and
4162541627
not finished
41626-
- include the linematch algorithm for the 'diffopt' setting. This aligns
41627-
changes between buffers on similar lines improving the diff highlighting in
41628-
Vim
41628+
- New option value "nosort" for 'completeopt'
4162941629

4163041630
*added-9.2*
4163141631
Added ~

src/insexpand.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,8 @@ ins_compl_build_pum(void)
12641264
int max_fuzzy_score = 0;
12651265
unsigned int cur_cot_flags = get_cot_flags();
12661266
int compl_no_select = (cur_cot_flags & COT_NOSELECT) != 0;
1267-
int compl_fuzzy_match = (cur_cot_flags & COT_FUZZY) != 0;
1267+
int fuzzy_nosort = (cur_cot_flags & COT_NOSORT) != 0;
1268+
int fuzzy_filter = fuzzy_nosort || (cur_cot_flags & COT_FUZZY) != 0;
12681269
compl_T *match_head = NULL;
12691270
compl_T *match_tail = NULL;
12701271
compl_T *match_next = NULL;
@@ -1289,13 +1290,13 @@ ins_compl_build_pum(void)
12891290
compl->cp_in_match_array = FALSE;
12901291
// When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
12911292
// set the cp_score for later comparisons.
1292-
if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1293+
if (fuzzy_filter && compl_leader.string != NULL && compl_leader.length > 0)
12931294
compl->cp_score = fuzzy_match_str(compl->cp_str.string, compl_leader.string);
12941295

12951296
if (!match_at_original_text(compl)
12961297
&& (compl_leader.string == NULL
12971298
|| ins_compl_equal(compl, compl_leader.string, (int)compl_leader.length)
1298-
|| (compl_fuzzy_match && compl->cp_score > 0)))
1299+
|| (fuzzy_filter && compl->cp_score > 0)))
12991300
{
13001301
++compl_match_arraysize;
13011302
compl->cp_in_match_array = TRUE;
@@ -1305,7 +1306,7 @@ ins_compl_build_pum(void)
13051306
match_tail->cp_match_next = compl;
13061307
match_tail = compl;
13071308

1308-
if (!shown_match_ok && !compl_fuzzy_match)
1309+
if (!shown_match_ok && !fuzzy_filter)
13091310
{
13101311
if (compl == compl_shown_match || did_find_shown_match)
13111312
{
@@ -1321,19 +1322,21 @@ ins_compl_build_pum(void)
13211322
shown_compl = compl;
13221323
cur = i;
13231324
}
1324-
else if (compl_fuzzy_match)
1325+
else if (fuzzy_filter)
13251326
{
13261327
if (i == 0)
13271328
shown_compl = compl;
13281329
// Update the maximum fuzzy score and the shown match
13291330
// if the current item's score is higher
1330-
if (compl->cp_score > max_fuzzy_score)
1331+
if (!fuzzy_nosort && compl->cp_score > max_fuzzy_score)
13311332
{
13321333
did_find_shown_match = TRUE;
13331334
max_fuzzy_score = compl->cp_score;
13341335
if (!compl_no_select)
13351336
compl_shown_match = compl;
13361337
}
1338+
else if (fuzzy_nosort && i == 0 && !compl_no_select)
1339+
compl_shown_match = shown_compl;
13371340

13381341
if (!shown_match_ok && compl == compl_shown_match && !compl_no_select)
13391342
{
@@ -1344,7 +1347,7 @@ ins_compl_build_pum(void)
13441347
i++;
13451348
}
13461349

1347-
if (compl == compl_shown_match && !compl_fuzzy_match)
1350+
if (compl == compl_shown_match && !fuzzy_filter)
13481351
{
13491352
did_find_shown_match = TRUE;
13501353

@@ -1389,7 +1392,7 @@ ins_compl_build_pum(void)
13891392
compl = match_next;
13901393
}
13911394

1392-
if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
1395+
if (fuzzy_filter && !fuzzy_nosort && compl_leader.string != NULL && compl_leader.length > 0)
13931396
{
13941397
for (i = 0; i < compl_match_arraysize; i++)
13951398
compl_match_array[i].pum_idx = i;

src/option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ EXTERN unsigned cot_flags; // flags from 'completeopt'
530530
#define COT_NOINSERT 0x040 // FALSE: select & insert, TRUE: noinsert
531531
#define COT_NOSELECT 0x080 // FALSE: select & insert, TRUE: noselect
532532
#define COT_FUZZY 0x100 // TRUE: fuzzy match enabled
533+
#define COT_NOSORT 0x200 // TRUE: fuzzy match without qsort score
533534
#ifdef BACKSLASH_IN_FILENAME
534535
EXTERN char_u *p_csl; // 'completeslash'
535536
#endif

src/optionstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
120120
NULL};
121121
static char *(p_fcl_values[]) = {"all", NULL};
122122
#endif
123-
static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", NULL};
123+
static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", NULL};
124124
#ifdef BACKSLASH_IN_FILENAME
125125
static char *(p_csl_values[]) = {"slash", "backslash", NULL};
126126
#endif

src/testdir/test_ins_complete.vim

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,7 @@ func Test_complete_fuzzy_match()
27002700
if a:findstart
27012701
return col(".")
27022702
endif
2703-
return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}]
2703+
return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}, #{word: "你好吗"}, #{word: "我好"}]
27042704
endfunc
27052705

27062706
new
@@ -2855,6 +2855,21 @@ func Test_complete_fuzzy_match()
28552855
call feedkeys("STe\<C-X>\<C-N>x\<CR>\<Esc>0", 'tx!')
28562856
call assert_equal('Tex', getline('.'))
28572857

2858+
" test case for nosort option
2859+
set cot=menuone,menu,noinsert,fuzzy,nosort
2860+
" fooBaz" should have a higher score when the leader is "fb".
2861+
" With `nosort`, "foobar" should still be shown first in the popup menu.
2862+
call feedkeys("S\<C-x>\<C-o>fb", 'tx')
2863+
call assert_equal('foobar', g:word)
2864+
call feedkeys("S\<C-x>\<C-o>", 'tx')
2865+
call assert_equal("你好吗", g:word)
2866+
2867+
set cot+=noselect
2868+
call feedkeys("S\<C-x>\<C-o>", 'tx')
2869+
call assert_equal(v:null, g:word)
2870+
call feedkeys("S\<C-x>\<C-o>\<C-N>", 'tx')
2871+
call assert_equal('你好吗', g:word)
2872+
28582873
" clean up
28592874
set omnifunc=
28602875
bw!

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1049,
707709
/**/
708710
1048,
709711
/**/

0 commit comments

Comments
 (0)