vim-patch:9.1.1714: completion: wildmode=longest:full selects wrong item

Problem:  completion: wildmode=longest:full selects wrong item
          (zeertzjq)
Solution: Fix issue, refactor ex_getln.c slightly
          (Girish Palya)

fixes: vim/vim#18102
closes: vim/vim#18125

2eccb4d0be

Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
zeertzjq
2025-09-01 08:09:05 +08:00
parent 3b5337ab6c
commit 1359578abb
4 changed files with 107 additions and 74 deletions

View File

@@ -381,8 +381,7 @@ int nextwild(expand_T *xp, int type, int options, bool escape)
return OK; return OK;
} }
/// Create and display a cmdline completion popup menu with items from /// Create completion popup menu with items from 'matches'.
/// "matches".
static int cmdline_pum_create(CmdlineInfo *ccline, expand_T *xp, char **matches, int numMatches, static int cmdline_pum_create(CmdlineInfo *ccline, expand_T *xp, char **matches, int numMatches,
bool showtail, bool noselect) bool showtail, bool noselect)
{ {
@@ -402,19 +401,13 @@ static int cmdline_pum_create(CmdlineInfo *ccline, expand_T *xp, char **matches,
} }
// Compute the popup menu starting column // Compute the popup menu starting column
char *endpos = showtail ? showmatches_gettail(xp->xp_pattern, true) : xp->xp_pattern; char *endpos = showtail ? showmatches_gettail(xp->xp_pattern, noselect) : xp->xp_pattern;
if (ui_has(kUICmdline) && cmdline_win == NULL) { if (ui_has(kUICmdline) && cmdline_win == NULL) {
compl_startcol = (int)(endpos - ccline->cmdbuff); compl_startcol = (int)(endpos - ccline->cmdbuff);
} else { } else {
compl_startcol = cmd_screencol((int)(endpos - ccline->cmdbuff)); compl_startcol = cmd_screencol((int)(endpos - ccline->cmdbuff));
} }
// no default selection
compl_selected = noselect ? -1 : 0;
pum_clear();
cmdline_pum_display(true);
return EXPAND_OK; return EXPAND_OK;
} }
@@ -427,8 +420,7 @@ void cmdline_pum_display(bool changed_array)
/// Returns true if the cmdline completion popup menu is being displayed. /// Returns true if the cmdline completion popup menu is being displayed.
bool cmdline_pum_active(void) bool cmdline_pum_active(void)
{ {
// compl_match_array != NULL should already imply pum_visible() in Nvim. return pum_visible() && compl_match_array != NULL;
return compl_match_array != NULL;
} }
/// Remove the cmdline completion popup menu (if present), free the list of items. /// Remove the cmdline completion popup menu (if present), free the list of items.
@@ -752,12 +744,21 @@ static char *get_next_or_prev_match(int mode, expand_T *xp)
} }
// Display matches on screen // Display matches on screen
if (p_wmnu) {
if (compl_match_array) { if (compl_match_array) {
compl_selected = findex; compl_selected = findex;
cmdline_pum_display(false); cmdline_pum_display(false);
} else if (p_wmnu) { } else if (wop_flags & kOptWopFlagPum) {
if (cmdline_pum_create(get_cmdline_info(), xp, xp->xp_files,
xp->xp_numfiles, cmd_showtail, false) == EXPAND_OK) {
compl_selected = findex;
pum_clear();
cmdline_pum_display(true);
}
} else {
redraw_wildmenu(xp, xp->xp_numfiles, xp->xp_files, findex, cmd_showtail); redraw_wildmenu(xp, xp->xp_numfiles, xp->xp_files, findex, cmd_showtail);
} }
}
xp->xp_selected = findex; xp->xp_selected = findex;
// Return the original text or the selected match // Return the original text or the selected match
@@ -1091,10 +1092,10 @@ static void showmatches_oneline(expand_T *xp, char **matches, int numMatches, in
} }
} }
/// Show all matches for completion on the command line. /// Display completion matches.
/// Returns EXPAND_NOTHING when the character that triggered expansion should /// Returns EXPAND_NOTHING when the character that triggered expansion should be
/// be inserted like a normal character. /// inserted as a normal character.
int showmatches(expand_T *xp, bool wildmenu, bool noselect) int showmatches(expand_T *xp, bool display_wildmenu, bool display_list, bool noselect)
{ {
CmdlineInfo *const ccline = get_cmdline_info(); CmdlineInfo *const ccline = get_cmdline_info();
int numMatches; int numMatches;
@@ -1121,12 +1122,19 @@ int showmatches(expand_T *xp, bool wildmenu, bool noselect)
showtail = cmd_showtail; showtail = cmd_showtail;
} }
if (((!ui_has(kUICmdline) || cmdline_win != NULL) && wildmenu && (wop_flags & kOptWopFlagPum)) if (((!ui_has(kUICmdline) || cmdline_win != NULL) && display_wildmenu && !display_list
&& (wop_flags & kOptWopFlagPum))
|| ui_has(kUIWildmenu) || (ui_has(kUICmdline) && ui_has(kUIPopupmenu))) { || ui_has(kUIWildmenu) || (ui_has(kUICmdline) && ui_has(kUIPopupmenu))) {
return cmdline_pum_create(ccline, xp, matches, numMatches, showtail, noselect); int retval = cmdline_pum_create(ccline, xp, matches, numMatches, showtail, noselect);
if (retval == EXPAND_OK) {
compl_selected = noselect ? -1 : 0;
pum_clear();
cmdline_pum_display(true);
}
return retval;
} }
if (!wildmenu) { if (display_list) {
msg_didany = false; // lines_left will be set msg_didany = false; // lines_left will be set
msg_start(); // prepare for paging msg_start(); // prepare for paging
msg_putchar('\n'); msg_putchar('\n');
@@ -1138,10 +1146,11 @@ int showmatches(expand_T *xp, bool wildmenu, bool noselect)
} }
if (got_int) { if (got_int) {
got_int = false; // only int. the completion, not the cmd line got_int = false; // only interrupt the completion, not the cmd line
} else if (wildmenu) { } else if (display_wildmenu && !display_list) {
redraw_wildmenu(xp, numMatches, matches, noselect ? -1 : 0, showtail); redraw_wildmenu(xp, numMatches, matches, noselect ? -1 : 0,
} else { showtail); // display statusbar menu
} else if (display_list) {
// find the length of the longest file name // find the length of the longest file name
maxlen = 0; maxlen = 0;
for (int i = 0; i < numMatches; i++) { for (int i = 0; i < numMatches; i++) {
@@ -3570,7 +3579,7 @@ int wildmenu_translate_key(CmdlineInfo *cclp, int key, expand_T *xp, bool did_wi
{ {
int c = key; int c = key;
if (did_wild_list) { if (cmdline_pum_active() || did_wild_list || wild_menu_showing) {
if (c == K_LEFT) { if (c == K_LEFT) {
c = Ctrl_P; c = Ctrl_P;
} else if (c == K_RIGHT) { } else if (c == K_RIGHT) {

View File

@@ -1120,32 +1120,34 @@ static int command_line_wildchar_complete(CommandLineState *s)
{ {
int res; int res;
int options = WILD_NO_BEEP; int options = WILD_NO_BEEP;
bool noselect = p_wmnu && (wim_flags[0] & kOptWimFlagNoselect) != 0; bool escape = s->firstc != '@';
bool wim_noselect = p_wmnu && (wim_flags[0] & kOptWimFlagNoselect) != 0;
if (wim_flags[s->wim_index] & kOptWimFlagLastused) { if (wim_flags[s->wim_index] & kOptWimFlagLastused) {
options |= WILD_BUFLASTUSED; options |= WILD_BUFLASTUSED;
} }
if (s->xpc.xp_numfiles > 0) { // typed p_wc at least twice if (s->xpc.xp_numfiles > 0) { // typed p_wc at least twice
// if 'wildmode' contains "list" may still need to list // If "list" is present, list matches unless already listed
if (s->xpc.xp_numfiles > 1 if (s->xpc.xp_numfiles > 1
&& !s->did_wild_list && !s->did_wild_list
&& ((wim_flags[s->wim_index] & kOptWimFlagList) && (wim_flags[s->wim_index] & kOptWimFlagList)) {
|| (p_wmnu && (wim_flags[s->wim_index] & kOptWimFlagFull) != 0))) { showmatches(&s->xpc, false, true, wim_noselect);
showmatches(&s->xpc,
p_wmnu && ((wim_flags[s->wim_index] & kOptWimFlagList) == 0),
noselect);
redrawcmd(); redrawcmd();
s->did_wild_list = true; s->did_wild_list = true;
} }
if (wim_flags[s->wim_index] & kOptWimFlagLongest) { if (wim_flags[s->wim_index] & kOptWimFlagLongest) {
res = nextwild(&s->xpc, WILD_LONGEST, options, s->firstc != '@'); res = nextwild(&s->xpc, WILD_LONGEST, options, escape);
} else if (wim_flags[s->wim_index] & kOptWimFlagFull) { } else if (wim_flags[s->wim_index] & kOptWimFlagFull) {
res = nextwild(&s->xpc, WILD_NEXT, options, s->firstc != '@'); res = nextwild(&s->xpc, WILD_NEXT, options, escape);
} else { } else {
res = OK; // don't insert 'wildchar' now res = OK; // don't insert 'wildchar' now
} }
} else { // typed p_wc first time } else { // typed p_wc first time
bool wim_longest = (wim_flags[0] & kOptWimFlagLongest);
bool wim_list = (wim_flags[0] & kOptWimFlagList);
bool wim_full = (wim_flags[0] & kOptWimFlagFull);
s->wim_index = 0;
if (s->c == p_wc || s->c == p_wcm || s->c == K_WILD || s->c == Ctrl_Z) { if (s->c == p_wc || s->c == p_wcm || s->c == K_WILD || s->c == Ctrl_Z) {
options |= WILD_MAY_EXPAND_PATTERN; options |= WILD_MAY_EXPAND_PATTERN;
if (s->c == K_WILD) { if (s->c == K_WILD) {
@@ -1153,18 +1155,17 @@ static int command_line_wildchar_complete(CommandLineState *s)
} }
s->xpc.xp_pre_incsearch_pos = s->is_state.search_start; s->xpc.xp_pre_incsearch_pos = s->is_state.search_start;
} }
s->wim_index = 0; int cmdpos_before = ccline.cmdpos;
int j = ccline.cmdpos;
// if 'wildmode' first contains "longest", get longest // if 'wildmode' first contains "longest", get longest
// common part // common part
if (wim_flags[0] & kOptWimFlagLongest) { if (wim_longest) {
res = nextwild(&s->xpc, WILD_LONGEST, options, s->firstc != '@'); res = nextwild(&s->xpc, WILD_LONGEST, options, escape);
} else { } else {
if (noselect || (wim_flags[s->wim_index] & kOptWimFlagList)) { if (wim_noselect || wim_list) {
options |= WILD_NOSELECT; options |= WILD_NOSELECT;
} }
res = nextwild(&s->xpc, WILD_EXPAND_KEEP, options, s->firstc != '@'); res = nextwild(&s->xpc, WILD_EXPAND_KEEP, options, escape);
} }
// if interrupted while completing, behave like it failed // if interrupted while completing, behave like it failed
@@ -1176,30 +1177,29 @@ static int command_line_wildchar_complete(CommandLineState *s)
return CMDLINE_CHANGED; return CMDLINE_CHANGED;
} }
// when more than one match, and 'wildmode' first contains // Display matches
// "list", or no change and 'wildmode' contains "longest,list", if (res == OK && s->xpc.xp_numfiles > (wim_noselect ? 0 : 1)) {
// list all matches // If "longest" fails to identify the longest item, try other
if (res == OK // 'wim' values if available
&& s->xpc.xp_numfiles > (noselect ? 0 : 1)) { if (wim_longest && ccline.cmdpos == cmdpos_before) {
// a "longest" that didn't do anything is skipped (but not if (wim_full) {
// "list:longest") nextwild(&s->xpc, WILD_NEXT, options, escape);
if (wim_flags[0] == kOptWimFlagLongest && ccline.cmdpos == j) {
s->wim_index = 1;
} }
if ((wim_flags[s->wim_index] & kOptWimFlagList) if (wim_list || (p_wmnu && wim_full)) {
|| (p_wmnu && (wim_flags[s->wim_index] & (kOptWimFlagFull|kOptWimFlagNoselect)))) { showmatches(&s->xpc, p_wmnu, wim_list, false);
showmatches(&s->xpc,
p_wmnu && ((wim_flags[s->wim_index] & kOptWimFlagList) == 0),
noselect);
redrawcmd();
s->did_wild_list = true;
if (wim_flags[s->wim_index] & kOptWimFlagLongest) {
nextwild(&s->xpc, WILD_LONGEST, options, s->firstc != '@');
} }
} else if (!wim_longest) {
if (wim_list || (p_wmnu && (wim_full || wim_noselect))) {
showmatches(&s->xpc, p_wmnu, wim_list, wim_noselect);
} else { } else {
vim_beep(kOptBoFlagWildmode); vim_beep(kOptBoFlagWildmode);
} }
}
redrawcmd();
if (wim_list) {
s->did_wild_list = true;
}
} else if (s->xpc.xp_numfiles == -1) { } else if (s->xpc.xp_numfiles == -1) {
s->xpc.xp_context = EXPAND_NOTHING; s->xpc.xp_context = EXPAND_NOTHING;
} }
@@ -1339,7 +1339,7 @@ static int command_line_execute(VimState *state, int key)
int wild_type = 0; int wild_type = 0;
const bool key_is_wc = (s->c == p_wc && KeyTyped) || s->c == p_wcm; const bool key_is_wc = (s->c == p_wc && KeyTyped) || s->c == p_wcm;
if ((cmdline_pum_active() || s->did_wild_list) && !key_is_wc) { if ((cmdline_pum_active() || wild_menu_showing || s->did_wild_list) && !key_is_wc) {
// Ctrl-Y: Accept the current selection and close the popup menu. // Ctrl-Y: Accept the current selection and close the popup menu.
// Ctrl-E: cancel the cmdline popup menu and return the original text. // Ctrl-E: cancel the cmdline popup menu and return the original text.
if (s->c == Ctrl_E || s->c == Ctrl_Y) { if (s->c == Ctrl_E || s->c == Ctrl_Y) {
@@ -1464,8 +1464,7 @@ static int command_line_execute(VimState *state, int key)
if (s->xpc.xp_numfiles > 1 if (s->xpc.xp_numfiles > 1
&& ((!s->did_wild_list && (wim_flags[s->wim_index] & kOptWimFlagList)) || p_wmnu)) { && ((!s->did_wild_list && (wim_flags[s->wim_index] & kOptWimFlagList)) || p_wmnu)) {
// Trigger the popup menu when wildoptions=pum // Trigger the popup menu when wildoptions=pum
showmatches(&s->xpc, showmatches(&s->xpc, p_wmnu, wim_flags[s->wim_index] & kOptWimFlagList,
p_wmnu && ((wim_flags[s->wim_index] & kOptWimFlagList) == 0),
wim_flags[0] & kOptWimFlagNoselect); wim_flags[0] & kOptWimFlagNoselect);
} }
nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@'); nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@');
@@ -2033,7 +2032,7 @@ static int command_line_handle_key(CommandLineState *s)
} }
case Ctrl_D: case Ctrl_D:
if (showmatches(&s->xpc, false, wim_flags[0] & kOptWimFlagNoselect) if (showmatches(&s->xpc, false, true, wim_flags[0] & kOptWimFlagNoselect)
== EXPAND_NOTHING) { == EXPAND_NOTHING) {
break; // Use ^D as normal char instead break; // Use ^D as normal char instead
} }

View File

@@ -2896,11 +2896,11 @@ func Test_wildmenu_pum()
call term_sendkeys(buf, "sign xyz\<Esc>:sign \<Tab>\<C-E>\<Up>") call term_sendkeys(buf, "sign xyz\<Esc>:sign \<Tab>\<C-E>\<Up>")
call VerifyScreenDump(buf, 'Test_wildmenu_pum_29', {}) call VerifyScreenDump(buf, 'Test_wildmenu_pum_29', {})
" Check "list" still works " Check that when "longest" produces no result, "list" works
call term_sendkeys(buf, "\<C-U>set wildmode=longest,list\<CR>") call term_sendkeys(buf, "\<C-U>set wildmode=longest,list\<CR>")
call term_sendkeys(buf, ":cn\<Tab>") call term_sendkeys(buf, ":cn\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_pum_30', {}) call VerifyScreenDump(buf, 'Test_wildmenu_pum_30', {})
call term_sendkeys(buf, "s") call term_sendkeys(buf, "\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_pum_31', {}) call VerifyScreenDump(buf, 'Test_wildmenu_pum_31', {})
" Tests a directory name contained full-width characters. " Tests a directory name contained full-width characters.
@@ -3000,7 +3000,32 @@ func Test_wildmenu_pum()
call term_sendkeys(buf, "\<Esc>:set wildchazz\<Left>\<Left>\<Tab>\<C-Y>") call term_sendkeys(buf, "\<Esc>:set wildchazz\<Left>\<Left>\<Tab>\<C-Y>")
call VerifyScreenDump(buf, 'Test_wildmenu_pum_53', {}) call VerifyScreenDump(buf, 'Test_wildmenu_pum_53', {})
call term_sendkeys(buf, "\<C-U>\<CR>") call term_sendkeys(buf, "\<Esc>:set showtabline& laststatus& lazyredraw&\<CR>")
" Verify that if "longest" finds nothing, wildmenu is still shown
call term_sendkeys(buf, ":set wildmode=longest:full,full wildoptions&\<CR>")
call term_sendkeys(buf, ":cn\<Tab>")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_wildmenu_pum_54', {})
" Verify that if "longest" finds nothing, "list" is still shown
call term_sendkeys(buf, "\<Esc>:set wildmode=longest:list,full\<CR>")
call term_sendkeys(buf, ":cn\<Tab>")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_wildmenu_pum_55', {})
call term_sendkeys(buf, "\<Tab>")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_wildmenu_pum_56', {})
" Verify that if "longest" finds a candidate, wildmenu is not shown
call term_sendkeys(buf, "\<Esc>:set wildmode=longest:full,full wildoptions&\<CR>")
call term_sendkeys(buf, ":sign u\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_pum_57', {})
" Subsequent wildchar shows wildmenu
call term_sendkeys(buf, "\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_pum_58', {})
call term_sendkeys(buf, "\<C-U>\<Esc>")
call StopVimInTerminal(buf) call StopVimInTerminal(buf)
endfunc endfunc

View File

@@ -5620,7 +5620,7 @@ func Test_completetimeout_autocompletetimeout()
set completetimeout=1 set completetimeout=1
call feedkeys("Gof\<C-N>\<F2>\<Esc>0", 'xt!') call feedkeys("Gof\<C-N>\<F2>\<Esc>0", 'xt!')
let match_count = len(b:matches->mapnew('v:val.word')) let match_count = len(b:matches->mapnew('v:val.word'))
call assert_true(match_count < 2000) call assert_true(match_count < 4000)
set completetimeout=1000 set completetimeout=1000
call feedkeys("\<Esc>Sf\<C-N>\<F2>\<Esc>0", 'xt!') call feedkeys("\<Esc>Sf\<C-N>\<F2>\<Esc>0", 'xt!')