Skip to content

Commit d4088ed

Browse files
glepnirchrisbra
authored andcommitted
patch 9.1.0983: not able to get the displayed items in complete_info()
Problem: not able to get the displayed items in complete_info() (Evgeni Chasnovski) Solution: return the visible items via the "matches" key for complete_info() (glepnir) fixes: #10007 closes: #16307 Signed-off-by: glepnir <glephunter@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 34e271b commit d4088ed

6 files changed

Lines changed: 79 additions & 11 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Dec 30
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Dec 31
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1896,10 +1896,15 @@ complete_info([{what}]) *complete_info()*
18961896
See |complete_info_mode| for the values.
18971897
pum_visible |TRUE| if popup menu is visible.
18981898
See |pumvisible()|.
1899-
items List of completion matches. Each item is a
1900-
dictionary containing the entries "word",
1899+
items List of all completion candidates. Each item
1900+
is a dictionary containing the entries "word",
19011901
"abbr", "menu", "kind", "info" and "user_data".
19021902
See |complete-items|.
1903+
matches Same as "items", but only returns items that
1904+
are matching current query. If both "matches"
1905+
and "items" are in "what", the returned list
1906+
will still be named "items", but each item
1907+
will have an additional "match" field.
19031908
selected Selected item index. First index is zero.
19041909
Index is -1 if no item is selected (showing
19051910
typed text only, or the last completion after

runtime/doc/insert.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*insert.txt* For Vim version 9.1. Last change: 2024 Dec 28
1+
*insert.txt* For Vim version 9.1. Last change: 2024 Dec 31
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1195,6 +1195,7 @@ items:
11951195
|hl-PmenuKind| highlight group, allowing for the
11961196
customization of ctermfg and guifg properties for the
11971197
completion kind
1198+
match See "matches" in |complete_info()|.
11981199

11991200
All of these except "icase", "equal", "dup" and "empty" must be a string. If
12001201
an item does not meet these requirements then an error message is given and

runtime/doc/version9.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2024 Dec 30
1+
*version9.txt* For Vim version 9.1. Last change: 2024 Dec 31
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41621,6 +41621,8 @@ Changed~
4162141621
instead of the "sh" filetype
4162241622
- the default value of the 'keyprotocol' option has been updated by support
4162341623
for the ghostty terminal emulator (using kitty protocol)
41624+
- |complete_info()| returns the list of matches shown in the poppu menu via
41625+
the "matches" key
4162441626

4162541627
*added-9.2*
4162641628
Added ~

src/insexpand.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ struct compl_S
106106
int cp_flags; // CP_ values
107107
int cp_number; // sequence number
108108
int cp_score; // fuzzy match score
109+
int cp_in_match_array; // collected by compl_match_array
109110
int cp_user_abbr_hlattr; // highlight attribute for abbr
110111
int cp_user_kind_hlattr; // highlight attribute for kind
111112
};
@@ -1282,6 +1283,7 @@ ins_compl_build_pum(void)
12821283

12831284
do
12841285
{
1286+
compl->cp_in_match_array = FALSE;
12851287
// When 'completeopt' contains "fuzzy" and leader is not NULL or empty,
12861288
// set the cp_score for later comparisons.
12871289
if (compl_fuzzy_match && compl_leader.string != NULL && compl_leader.length > 0)
@@ -1293,6 +1295,7 @@ ins_compl_build_pum(void)
12931295
|| (compl_fuzzy_match && compl->cp_score > 0)))
12941296
{
12951297
++compl_match_arraysize;
1298+
compl->cp_in_match_array = TRUE;
12961299
if (match_head == NULL)
12971300
match_head = compl;
12981301
else
@@ -3259,11 +3262,12 @@ get_complete_info(list_T *what_list, dict_T *retdict)
32593262
#define CI_WHAT_ITEMS 0x04
32603263
#define CI_WHAT_SELECTED 0x08
32613264
#define CI_WHAT_INSERTED 0x10
3265+
#define CI_WHAT_MATCHES 0x20
32623266
#define CI_WHAT_ALL 0xff
32633267
int what_flag;
32643268

32653269
if (what_list == NULL)
3266-
what_flag = CI_WHAT_ALL;
3270+
what_flag = CI_WHAT_ALL & ~CI_WHAT_MATCHES;
32673271
else
32683272
{
32693273
what_flag = 0;
@@ -3282,6 +3286,8 @@ get_complete_info(list_T *what_list, dict_T *retdict)
32823286
what_flag |= CI_WHAT_SELECTED;
32833287
else if (STRCMP(what, "inserted") == 0)
32843288
what_flag |= CI_WHAT_INSERTED;
3289+
else if (STRCMP(what, "matches") == 0)
3290+
what_flag |= CI_WHAT_MATCHES;
32853291
}
32863292
}
32873293

@@ -3291,19 +3297,23 @@ get_complete_info(list_T *what_list, dict_T *retdict)
32913297
if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE))
32923298
ret = dict_add_number(retdict, "pum_visible", pum_visible());
32933299

3294-
if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED))
3300+
if (ret == OK && (what_flag & CI_WHAT_ITEMS || what_flag & CI_WHAT_SELECTED
3301+
|| what_flag & CI_WHAT_MATCHES))
32953302
{
32963303
list_T *li = NULL;
32973304
dict_T *di;
32983305
compl_T *match;
32993306
int selected_idx = -1;
3307+
int has_items = what_flag & CI_WHAT_ITEMS;
3308+
int has_matches = what_flag & CI_WHAT_MATCHES;
33003309

3301-
if (what_flag & CI_WHAT_ITEMS)
3310+
if (has_items || has_matches)
33023311
{
33033312
li = list_alloc();
33043313
if (li == NULL)
33053314
return;
3306-
ret = dict_add_list(retdict, "items", li);
3315+
ret = dict_add_list(retdict, (has_matches && !has_items)
3316+
? "matches" : "items", li);
33073317
}
33083318
if (ret == OK && what_flag & CI_WHAT_SELECTED)
33093319
if (compl_curr_match != NULL && compl_curr_match->cp_number == -1)
@@ -3316,7 +3326,8 @@ get_complete_info(list_T *what_list, dict_T *retdict)
33163326
{
33173327
if (!match_at_original_text(match))
33183328
{
3319-
if (what_flag & CI_WHAT_ITEMS)
3329+
if (has_items
3330+
|| (has_matches && match->cp_in_match_array))
33203331
{
33213332
di = dict_alloc();
33223333
if (di == NULL)
@@ -3329,13 +3340,16 @@ get_complete_info(list_T *what_list, dict_T *retdict)
33293340
dict_add_string(di, "menu", match->cp_text[CPT_MENU]);
33303341
dict_add_string(di, "kind", match->cp_text[CPT_KIND]);
33313342
dict_add_string(di, "info", match->cp_text[CPT_INFO]);
3343+
if (has_matches && has_items)
3344+
dict_add_bool(di, "match", match->cp_in_match_array);
33323345
if (match->cp_user_data.v_type == VAR_UNKNOWN)
33333346
// Add an empty string for backwards compatibility
33343347
dict_add_string(di, "user_data", (char_u *)"");
33353348
else
33363349
dict_add_tv(di, "user_data", &match->cp_user_data);
33373350
}
3338-
if (compl_curr_match != NULL && compl_curr_match->cp_number == match->cp_number)
3351+
if (compl_curr_match != NULL
3352+
&& compl_curr_match->cp_number == match->cp_number)
33393353
selected_idx = list_idx;
33403354
list_idx += 1;
33413355
}

src/testdir/test_ins_complete.vim

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,4 +2931,48 @@ func Test_complete_backwards_default()
29312931
bw!
29322932
endfunc
29332933

2934+
func Test_complete_info_matches()
2935+
let g:what = ['matches']
2936+
func ShownInfo()
2937+
let g:compl_info = complete_info(g:what)
2938+
return ''
2939+
endfunc
2940+
set completeopt+=noinsert
2941+
2942+
new
2943+
call setline(1, ['aaa', 'aab', 'aba', 'abb'])
2944+
inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
2945+
2946+
call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
2947+
call assert_equal([
2948+
\ {'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2949+
\ {'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2950+
\ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2951+
\ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2952+
\], g:compl_info['matches'])
2953+
2954+
call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
2955+
call assert_equal([
2956+
\ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2957+
\ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
2958+
\], g:compl_info['matches'])
2959+
2960+
" items and matches both in what
2961+
let g:what = ['items', 'matches']
2962+
call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
2963+
call assert_equal([
2964+
\ {'word': 'aaa', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
2965+
\ {'word': 'aab', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
2966+
\ {'word': 'aba', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
2967+
\ {'word': 'abb', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
2968+
\], g:compl_info['items'])
2969+
call assert_false(has_key(g:compl_info, 'matches'))
2970+
2971+
bw!
2972+
bw!
2973+
unlet g:what
2974+
delfunc ShownInfo
2975+
set cot&
2976+
endfunc
2977+
29342978
" vim: shiftwidth=2 sts=2 expandtab nofoldenable

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+
983,
707709
/**/
708710
982,
709711
/**/

0 commit comments

Comments
 (0)