Skip to content

Commit

Permalink
patch 9.1.0936: cannot highlight completed text
Browse files Browse the repository at this point in the history
Problem:  cannot highlight completed text
Solution: (optionally) highlight auto-completed text using the
          ComplMatchIns highlight group (glepnir)

closes: #16173

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
  • Loading branch information
glepnir authored and chrisbra committed Dec 16, 2024
1 parent 368ef5a commit 6a38aff
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 22 deletions.
2 changes: 1 addition & 1 deletion runtime/doc/fold.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ lines needed for the computation of a given line: For example, try to avoid the
fold levels on previous lines until an independent fold level is found.

If this proves difficult, the next best thing could be to cache all fold levels
in a buffer-local variable (b:foldlevels) that is only updated on |b:changedtick|:
in a buffer-local variable (b:foldlevels) that is only updated on |b:changedtick|:
>vim
vim9script
def MyFoldFunc(): number
Expand Down
4 changes: 3 additions & 1 deletion runtime/doc/syntax.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*syntax.txt* For Vim version 9.1. Last change: 2024 Dec 12
*syntax.txt* For Vim version 9.1. Last change: 2024 Dec 16


VIM REFERENCE MANUAL by Bram Moolenaar
Expand Down Expand Up @@ -5857,6 +5857,8 @@ PmenuThumb Popup menu: Thumb of the scrollbar.
PmenuMatch Popup menu: Matched text in normal item.
*hl-PmenuMatchSel*
PmenuMatchSel Popup menu: Matched text in selected item.
*hl-ComplMatchIns*
ComplMatchIns Matched text of the currently inserted completion.
*hl-PopupNotification*
PopupNotification
Popup window created with |popup_notification()|. If not
Expand Down
1 change: 1 addition & 0 deletions runtime/doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -8136,6 +8136,7 @@ hit-return message.txt /*hit-return*
hitest.vim syntax.txt /*hitest.vim*
hjkl usr_02.txt /*hjkl*
hl-ColorColumn syntax.txt /*hl-ColorColumn*
hl-ComplMatchIns syntax.txt /*hl-ComplMatchIns*
hl-Conceal syntax.txt /*hl-Conceal*
hl-CurSearch syntax.txt /*hl-CurSearch*
hl-Cursor syntax.txt /*hl-Cursor*
Expand Down
5 changes: 1 addition & 4 deletions runtime/doc/todo.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*todo.txt* For Vim version 9.1. Last change: 2024 Dec 04
*todo.txt* For Vim version 9.1. Last change: 2024 Dec 16


VIM REFERENCE MANUAL by Bram Moolenaar
Expand Down Expand Up @@ -1093,9 +1093,6 @@ Problem with 'delcombine'. (agguser, 2017 Nov 10, #2313)
MS-Windows: buffer completion doesn't work when using backslash (or slash)
for a path separator. (xtal8, #2201)

Would be nice for Insert mode completion to highlight the text that was added
(and may change when picking another completion).

Test more runtime files.

Window not closed when deleting buffer. (Harm te Hennepe, 2017 Aug 27, #2029)
Expand Down
1 change: 1 addition & 0 deletions runtime/doc/version9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41653,6 +41653,7 @@ Autocommands: ~

Highlighting: ~

|hl-ComplMatchIns| matched text of the currently inserted completion.
|hl-MsgArea| highlighting of the Command-line and messages area
|hl-PmenuMatch| Popup menu: highlighting of matched text
|hl-PmenuMatchSel| Popup menu: highlighting of matched text in selected
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ CClink = $(CC)
#CONF_OPT_GUI = --enable-gui=motif --with-motif-lib="-static -lXm -shared"

# Uncomment this line to run an individual test with gvim.
#GUI_TESTARG = GUI_FLAG=-g
#GUI_TESTARG = GUI_FLAG=-g

# DARWIN - detecting Mac OS X
# Uncomment this line when you want to compile a Unix version of Vim on
Expand Down
13 changes: 11 additions & 2 deletions src/drawline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ win_line(
long vcol_prev = -1; // "wlv.vcol" of previous character
char_u *line; // current line
char_u *ptr; // current position in "line"
int in_curline = wp == curwin && lnum == curwin->w_cursor.lnum;

#ifdef FEAT_PROP_POPUP
char_u *p_extra_free2 = NULL; // another p_extra to be freed
Expand Down Expand Up @@ -1172,6 +1173,7 @@ win_line(
// highlighting
int area_attr = 0; // attributes desired by highlighting
int search_attr = 0; // attributes desired by 'hlsearch'
int ins_match_attr = 0; // attributes desired by PmenuMatch
#ifdef FEAT_SYN_HL
int vcol_save_attr = 0; // saved attr for 'cursorcolumn'
int syntax_attr = 0; // attributes desired by syntax
Expand Down Expand Up @@ -1415,8 +1417,7 @@ win_line(
}

// Check if the character under the cursor should not be inverted
if (!highlight_match && lnum == curwin->w_cursor.lnum
&& wp == curwin
if (!highlight_match && in_curline
#ifdef FEAT_GUI
&& !gui.in_use
#endif
Expand Down Expand Up @@ -3939,6 +3940,14 @@ win_line(
if (wlv.draw_state == WL_LINE)
vcol_prev = wlv.vcol;

if (wlv.draw_state == WL_LINE
&& (State & MODE_INSERT) && in_curline && ins_compl_active())
{
ins_match_attr = ins_compl_col_range_attr(wlv.col);
if (ins_match_attr > 0)
wlv.char_attr = hl_combine_attr(wlv.char_attr, ins_match_attr);
}

// Store character to be displayed.
// Skip characters that are left of the screen for 'nowrap'.
if (wlv.draw_state < WL_LINE || skip_cells <= 0)
Expand Down
1 change: 1 addition & 0 deletions src/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ static char *(highlight_init_both[]) = {
"default link PmenuMatchSel PmenuSel",
"default link PmenuExtra Pmenu",
"default link PmenuExtraSel PmenuSel",
"default link ComplMatchIns Normal",

This comment has been minimized.

Copy link
@h-east

h-east Dec 17, 2024

Contributor

@chrisbra The highlight group has been added, we need to update runtime/syntax/vim.vim.
(by running runtime/syntax/generator/make)

This comment has been minimized.

Copy link
@chrisbra

chrisbra Dec 17, 2024

Member

ah, forgot. Thanks, re-generated it

CENT("Normal cterm=NONE", "Normal gui=NONE"),
NULL
};
Expand Down
53 changes: 40 additions & 13 deletions src/insexpand.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static pos_T compl_startpos;
static int compl_length = 0;
static colnr_T compl_col = 0; // column where the text starts
// that is being completed
static colnr_T compl_ins_end_col = 0;
static string_T compl_orig_text = {NULL, 0}; // text as it was before
// completion started
static int compl_cont_mode = 0;
Expand All @@ -198,6 +199,11 @@ static int compl_selected_item = -1;

static int *compl_fuzzy_scores;

// "compl_match_array" points the currently displayed list of entries in the
// popup menu. It is NULL when there is no popup menu.
static pumitem_T *compl_match_array = NULL;
static int compl_match_arraysize;

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_hl);
static void ins_compl_longest_match(compl_T *match);
static void ins_compl_del_pum(void);
Expand Down Expand Up @@ -897,6 +903,32 @@ ins_compl_equal(compl_T *match, char_u *str, int len)
return STRNCMP(match->cp_str.string, str, (size_t)len) == 0;
}

/*
* when len is -1 mean use whole length of p otherwise part of p
*/
static void
ins_compl_insert_bytes(char_u *p, int len)
{
if (len == -1)
len = (int)STRLEN(p);
ins_bytes_len(p, len);
compl_ins_end_col = curwin->w_cursor.col - 1;
}

/*
* Checks if the column is within the currently inserted completion text
* column range. If it is, it returns a special highlight attribute.
* -1 mean normal item.
*/
int
ins_compl_col_range_attr(int col)
{
if (col >= compl_col && col < compl_ins_end_col)
return syn_name2attr((char_u *)"ComplMatchIns");

return -1;
}

/*
* Reduce the longest common string for match "match".
*/
Expand All @@ -917,7 +949,7 @@ ins_compl_longest_match(compl_T *match)
compl_leader.length = match->cp_str.length;
had_match = (curwin->w_cursor.col > compl_col);
ins_compl_delete();
ins_bytes(compl_leader.string + get_compl_len());
ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
ins_redraw(FALSE);

// When the match isn't there (to avoid matching itself) remove it
Expand Down Expand Up @@ -967,7 +999,7 @@ ins_compl_longest_match(compl_T *match)

had_match = (curwin->w_cursor.col > compl_col);
ins_compl_delete();
ins_bytes(compl_leader.string + get_compl_len());
ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
ins_redraw(FALSE);

// When the match isn't there (to avoid matching itself) remove it
Expand Down Expand Up @@ -1060,12 +1092,6 @@ get_cot_flags(void)
return curbuf->b_cot_flags != 0 ? curbuf->b_cot_flags : cot_flags;
}


// "compl_match_array" points the currently displayed list of entries in the
// popup menu. It is NULL when there is no popup menu.
static pumitem_T *compl_match_array = NULL;
static int compl_match_arraysize;

/*
* Update the screen and when there is any scrolling remove the popup menu.
*/
Expand Down Expand Up @@ -1817,6 +1843,7 @@ ins_compl_clear(void)
compl_cont_status = 0;
compl_started = FALSE;
compl_matches = 0;
compl_ins_end_col = 0;
VIM_CLEAR_STRING(compl_pattern);
VIM_CLEAR_STRING(compl_leader);
edit_submode_extra = NULL;
Expand Down Expand Up @@ -1965,7 +1992,7 @@ ins_compl_new_leader(void)
{
ins_compl_del_pum();
ins_compl_delete();
ins_bytes(compl_leader.string + get_compl_len());
ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
compl_used_match = FALSE;

if (compl_started)
Expand Down Expand Up @@ -2410,7 +2437,7 @@ ins_compl_stop(int c, int prev_mode, int retval)
int compl_len = get_compl_len();

if ((int)plen > compl_len)
ins_bytes_len(p + compl_len, (int)(plen - compl_len));
ins_compl_insert_bytes(p + compl_len, (int)(plen - compl_len));
}
retval = TRUE;
}
Expand Down Expand Up @@ -4260,7 +4287,7 @@ ins_compl_insert(int in_compl_func)
// Make sure we don't go over the end of the string, this can happen with
// illegal bytes.
if (compl_len < (int)compl_shown_match->cp_str.length)
ins_bytes(compl_shown_match->cp_str.string + compl_len);
ins_compl_insert_bytes(compl_shown_match->cp_str.string + compl_len, -1);
if (match_at_original_text(compl_shown_match))
compl_used_match = FALSE;
else
Expand Down Expand Up @@ -4537,15 +4564,15 @@ ins_compl_next(
// Insert the text of the new completion, or the compl_leader.
if (compl_no_insert && !started)
{
ins_bytes(compl_orig_text.string + get_compl_len());
ins_compl_insert_bytes(compl_orig_text.string + get_compl_len(), -1);
compl_used_match = FALSE;
}
else if (insert_match)
{
if (!compl_get_longest || compl_used_match)
ins_compl_insert(in_compl_func);
else
ins_bytes(compl_leader.string + get_compl_len());
ins_compl_insert_bytes(compl_leader.string + get_compl_len(), -1);
}
else
compl_used_match = FALSE;
Expand Down
1 change: 1 addition & 0 deletions src/proto/insexpand.pro
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ void ins_compl_delete(void);
void ins_compl_insert(int in_compl_func);
void ins_compl_check_keys(int frequency, int in_compl_func);
int ins_complete(int c, int enable_pum);
int ins_compl_col_range_attr(int col);
void free_insexpand_stuff(void);
/* vim: set ft=c : */
20 changes: 20 additions & 0 deletions src/testdir/dumps/Test_pum_matchins_01.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
|f+0#ff404010#ffffff0|o@1> +0#0000000&@71
|f+0#0000001#e0e0e08|o@1| @11| +0#4040ff13#ffffff0@59
|b+0#0000001#ffd7ff255|a|r| @11| +0#4040ff13#ffffff0@59
|你*0#0000001#ffd7ff255|好| +&@10| +0#4040ff13#ffffff0@59
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@34
20 changes: 20 additions & 0 deletions src/testdir/dumps/Test_pum_matchins_02.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
|b+0#ff404010#ffffff0|a|r> +0#0000000&@71
|f+0#0000001#ffd7ff255|o@1| @11| +0#4040ff13#ffffff0@59
|b+0#0000001#e0e0e08|a|r| @11| +0#4040ff13#ffffff0@59
|你*0#0000001#ffd7ff255|好| +&@10| +0#4040ff13#ffffff0@59
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |2| |o|f| |3| +0#0000000&@34
20 changes: 20 additions & 0 deletions src/testdir/dumps/Test_pum_matchins_03.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
|你*0#ff404010#ffffff0|好> +0#0000000&@70
|f+0#0000001#ffd7ff255|o@1| @11| +0#4040ff13#ffffff0@59
|b+0#0000001#ffd7ff255|a|r| @11| +0#4040ff13#ffffff0@59
|你*0#0000001#e0e0e08|好| +&@10| +0#4040ff13#ffffff0@59
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |3| |o|f| |3| +0#0000000&@34
20 changes: 20 additions & 0 deletions src/testdir/dumps/Test_pum_matchins_04.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
|f+0&#ffffff0|o@1> @71
|~+0#4040ff13&| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|1|,|4| @10|A|l@1|
20 changes: 20 additions & 0 deletions src/testdir/dumps/Test_pum_matchins_05.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
|f+0&#ffffff0|o@1| > @70
|~+0#4040ff13&| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|~| @73
|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|1|,|5| @10|A|l@1|
Loading

0 comments on commit 6a38aff

Please sign in to comment.