Skip to content

Commit 38f99a1

Browse files
glepnirchrisbra
authored andcommitted
patch 9.1.0690: cannot set special highlight kind in popupmenu
Problem: cannot set special highlight kind in popupmenu Solution: add kind_hlgroup item to complete function (glepnir) closes: #15561 Signed-off-by: glepnir <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 56186e4 commit 38f99a1

File tree

8 files changed

+94
-11
lines changed

8 files changed

+94
-11
lines changed

runtime/doc/insert.txt

Lines changed: 6 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 Jul 28
1+
*insert.txt* For Vim version 9.1. Last change: 2024 Aug 23
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1189,6 +1189,11 @@ items:
11891189
attributes in the popup menu to apply cterm and gui
11901190
properties (with higher priority) like strikethrough
11911191
to the completion items
1192+
kind_hlgroup an additional highlight group specifically for setting
1193+
the highlight attributes of the completion kind. When
1194+
this field is present, it will override the |hl-PmenuKind|
1195+
highlight group, allowing for the customization of
1196+
ctermfd and guifg properties for the completion kind
11921197

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

runtime/doc/version9.txt

Lines changed: 4 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 Aug 15
1+
*version9.txt* For Vim version 9.1. Last change: 2024 Aug 23
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41563,6 +41563,9 @@ Support for the XDG Desktop Specification |xdg-base-dir|
4156341563
Support highlighting the matched text for insert-mode completion and
4156441564
command-line completion in |ins-completion-menu|.
4156541565

41566+
Support highlighting the completion kind in |ins-completion-menu|, see
41567+
|complete-items|.
41568+
4156641569
Support for translating messages in Vim script plugins using the |gettext()|
4156741570
and |bindtextdomain()| functions.
4156841571

src/insexpand.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct compl_S
115115
int cp_number; // sequence number
116116
int cp_score; // fuzzy match score
117117
int cp_user_hlattr; // highlight attribute to combine with
118+
int cp_user_kind_hlattr; // highlight attribute for kind
118119
};
119120

120121
// values for cp_flags
@@ -206,7 +207,7 @@ static int compl_selected_item = -1;
206207

207208
static int *compl_fuzzy_scores;
208209

209-
static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup, int user_hlattr);
210+
static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup, int user_hlattr, int user_kind_hlattr);
210211
static void ins_compl_longest_match(compl_T *match);
211212
static void ins_compl_del_pum(void);
212213
static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
@@ -746,7 +747,7 @@ ins_compl_add_infercase(
746747
if (icase)
747748
flags |= CP_ICASE;
748749

749-
res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, -1);
750+
res = ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE, -1, -1);
750751
vim_free(tofree);
751752
return res;
752753
}
@@ -780,7 +781,8 @@ ins_compl_add(
780781
int cdir,
781782
int flags_arg,
782783
int adup, // accept duplicate match
783-
int user_hlattr)
784+
int user_hlattr,
785+
int user_kind_hlattr)
784786
{
785787
compl_T *match;
786788
int dir = (cdir == 0 ? compl_direction : cdir);
@@ -845,6 +847,7 @@ ins_compl_add(
845847
match->cp_fname = NULL;
846848
match->cp_flags = flags;
847849
match->cp_user_hlattr = user_hlattr;
850+
match->cp_user_kind_hlattr = user_kind_hlattr;
848851

849852
if (cptext != NULL)
850853
{
@@ -997,7 +1000,7 @@ ins_compl_add_matches(
9971000

9981001
for (i = 0; i < num_matches && add_r != FAIL; i++)
9991002
if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
1000-
CP_FAST | (icase ? CP_ICASE : 0), FALSE, -1)) == OK)
1003+
CP_FAST | (icase ? CP_ICASE : 0), FALSE, -1, -1)) == OK)
10011004
// if dir was BACKWARD then honor it just once
10021005
dir = FORWARD;
10031006
FreeWild(num_matches, matches);
@@ -1343,6 +1346,7 @@ ins_compl_build_pum(void)
13431346
compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
13441347
compl_match_array[i].pum_score = compl->cp_score;
13451348
compl_match_array[i].pum_user_hlattr = compl->cp_user_hlattr;
1349+
compl_match_array[i].pum_user_kind_hlattr = compl->cp_user_kind_hlattr;
13461350
if (compl->cp_text[CPT_MENU] != NULL)
13471351
compl_match_array[i++].pum_extra =
13481352
compl->cp_text[CPT_MENU];
@@ -2844,6 +2848,14 @@ expand_by_function(int type, char_u *base)
28442848
#endif // FEAT_COMPL_FUNC
28452849

28462850
#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
2851+
2852+
static inline int
2853+
get_user_highlight_attr(char_u *hlname)
2854+
{
2855+
if (hlname != NULL && *hlname != NUL)
2856+
return syn_name2attr(hlname);
2857+
return -1;
2858+
}
28472859
/*
28482860
* Add a match to the list of matches from a typeval_T.
28492861
* If the given string is already in the list of completions, then return
@@ -2862,7 +2874,9 @@ ins_compl_add_tv(typval_T *tv, int dir, int fast)
28622874
typval_T user_data;
28632875
int status;
28642876
char_u *user_hlname;
2877+
char_u *user_kind_hlname;
28652878
int user_hlattr = -1;
2879+
int user_kind_hlattr = -1;
28662880

28672881
user_data.v_type = VAR_UNKNOWN;
28682882
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
@@ -2872,9 +2886,12 @@ ins_compl_add_tv(typval_T *tv, int dir, int fast)
28722886
cptext[CPT_MENU] = dict_get_string(tv->vval.v_dict, "menu", FALSE);
28732887
cptext[CPT_KIND] = dict_get_string(tv->vval.v_dict, "kind", FALSE);
28742888
cptext[CPT_INFO] = dict_get_string(tv->vval.v_dict, "info", FALSE);
2889+
28752890
user_hlname = dict_get_string(tv->vval.v_dict, "hl_group", FALSE);
2876-
if (user_hlname != NULL && *user_hlname != NUL)
2877-
user_hlattr = syn_name2attr(user_hlname);
2891+
user_hlattr = get_user_highlight_attr(user_hlname);
2892+
2893+
user_kind_hlname = dict_get_string(tv->vval.v_dict, "kind_hlgroup", FALSE);
2894+
user_kind_hlattr = get_user_highlight_attr(user_kind_hlname);
28782895

28792896
dict_get_tv(tv->vval.v_dict, "user_data", &user_data);
28802897
if (dict_get_string(tv->vval.v_dict, "icase", FALSE) != NULL
@@ -2899,7 +2916,7 @@ ins_compl_add_tv(typval_T *tv, int dir, int fast)
28992916
return FAIL;
29002917
}
29012918
status = ins_compl_add(word, -1, NULL, cptext,
2902-
&user_data, dir, flags, dup, user_hlattr);
2919+
&user_data, dir, flags, dup, user_hlattr, user_kind_hlattr);
29032920
if (status != OK)
29042921
clear_tv(&user_data);
29052922
return status;
@@ -2986,7 +3003,7 @@ set_completion(colnr_T startcol, list_T *list)
29863003
flags |= CP_ICASE;
29873004
if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
29883005
-1, NULL, NULL, NULL, 0,
2989-
flags | CP_FAST, FALSE, -1) != OK)
3006+
flags | CP_FAST, FALSE, -1, -1) != OK)
29903007
return;
29913008

29923009
ctrl_x_mode = CTRL_X_EVAL;
@@ -5218,7 +5235,7 @@ ins_compl_start(void)
52185235
if (p_ic)
52195236
flags |= CP_ICASE;
52205237
if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
5221-
-1, NULL, NULL, NULL, 0, flags, FALSE, -1) != OK)
5238+
-1, NULL, NULL, NULL, 0, flags, FALSE, -1, -1) != OK)
52225239
{
52235240
VIM_CLEAR(compl_pattern);
52245241
compl_patternlen = 0;

src/popupmenu.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ pum_redraw(void)
633633
attr = highlight_attr[hlf];
634634
if (pum_array[idx].pum_user_hlattr > 0)
635635
attr = hl_combine_attr(attr, pum_array[idx].pum_user_hlattr);
636+
if (round == 1 && pum_array[idx].pum_user_kind_hlattr > 0)
637+
attr = hl_combine_attr(attr, pum_array[idx].pum_user_kind_hlattr);
636638
width = 0;
637639
s = NULL;
638640
switch (round)

src/structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,6 +4475,7 @@ typedef struct
44754475
int pum_score; // fuzzy match score
44764476
int pum_idx; // index of item before sorting by score
44774477
int pum_user_hlattr; // highlight attribute to combine with
4478+
int pum_user_kind_hlattr; // highlight attribute for kind
44784479
} pumitem_T;
44794480

44804481
/*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
|a+0&#ffffff0|w|o|r|d|1> @68
2+
|a+0#ff404010#e0e0e08|w|o|r|d|1| |v+0#ffff4012&|a|r|i|a|b|l|e| |e+0#ff404010&|x|t|r|a| |t|e|x|t| |1| | +0#4040ff13#ffffff0@45
3+
|a+0#0000001#ffd7ff255|w|o|r|d|2| |f+0#4040ff13&|u|n|c|t|i|o|n| |e+0#0000001&|x|t|r|a| |t|e|x|t| |2| | +0#4040ff13#ffffff0@45
4+
|你*0#0000001#ffd7ff255|好| +&@2|c+0#40ff4011&|l|a|s@1| @3|e+0#0000001&|x|t|r|a| |t|e|x|t| |3| | +0#4040ff13#ffffff0@45
5+
|~| @73
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|~| @73
11+
|~| @73
12+
|~| @73
13+
|~| @73
14+
|~| @73
15+
|~| @73
16+
|~| @73
17+
|~| @73
18+
|~| @73
19+
|~| @73
20+
|-+2#0000000&@1| |U|s|e|r| |d|e|f|i|n|e|d| |c|o|m|p|l|e|t|i|o|n| |(|^|U|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@26

src/testdir/test_popup.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,4 +1548,37 @@ func Test_pum_user_hl_group()
15481548
call StopVimInTerminal(buf)
15491549
endfunc
15501550

1551+
func Test_pum_user_kind_hlgroup()
1552+
CheckScreendump
1553+
let lines =<< trim END
1554+
func CompleteFunc( findstart, base )
1555+
if a:findstart
1556+
return 0
1557+
endif
1558+
return {
1559+
\ 'words': [
1560+
\ { 'word': 'aword1', 'menu': 'extra text 1', 'kind': 'variable', 'kind_hlgroup': 'KindVar', 'hl_group': 'StrikeFake' },
1561+
\ { 'word': 'aword2', 'menu': 'extra text 2', 'kind': 'function', 'kind_hlgroup': 'KindFunc' },
1562+
\ { 'word': '你好', 'menu': 'extra text 3', 'kind': 'class', 'kind_hlgroup': 'KindClass' },
1563+
\]}
1564+
endfunc
1565+
set completeopt=menu
1566+
set completefunc=CompleteFunc
1567+
1568+
hi StrikeFake ctermfg=9
1569+
hi KindVar ctermfg=yellow
1570+
hi KindFunc ctermfg=blue
1571+
hi KindClass ctermfg=green
1572+
END
1573+
call writefile(lines, 'Xscript', 'D')
1574+
let buf = RunVimInTerminal('-S Xscript', {})
1575+
1576+
call TermWait(buf)
1577+
call term_sendkeys(buf, "S\<C-X>\<C-U>")
1578+
call VerifyScreenDump(buf, 'Test_pum_highlights_16', {})
1579+
call term_sendkeys(buf, "\<C-E>\<Esc>")
1580+
1581+
call StopVimInTerminal(buf)
1582+
endfunc
1583+
15511584
" vim: shiftwidth=2 sts=2 expandtab

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+
690,
707709
/**/
708710
689,
709711
/**/

0 commit comments

Comments
 (0)