refactor: make wildmenu code closer to Vim (#19870)

This is a small refactor that makes `compl_match_array` static and
doesn't change any behavior.
This commit is contained in:
zeertzjq 2022-08-21 11:06:07 +08:00 committed by GitHub
parent 61ff37952a
commit 506a3ec913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 27 deletions

View File

@ -57,7 +57,7 @@ static int cmd_showtail; ///< Only show path tail in lists ?
/// "compl_match_array" points the currently displayed list of entries in the
/// popup menu. It is NULL when there is no popup menu.
pumitem_T *compl_match_array = NULL; // TODO(zeertzjq): make this static in cmdexpand.c
static pumitem_T *compl_match_array = NULL;
static int compl_match_arraysize;
/// First column in cmdline of the matched item for completion.
static int compl_startcol;
@ -256,6 +256,19 @@ void cmdline_pum_display(bool changed_array)
changed_array, compl_startcol);
}
bool cmdline_pum_active(void)
{
// return p_wmnu && pum_visible() && compl_match_array != NULL;
return compl_match_array != NULL;
}
/// Remove the cmdline completion popup menu
void cmdline_pum_remove(void)
{
pum_undisplay(true);
XFREE_CLEAR(compl_match_array);
}
/// Do wildcard expansion on the string 'str'.
/// Chars that should not be expanded must be preceded with a backslash.
/// Return a pointer to allocated memory containing the new string.
@ -545,10 +558,14 @@ int showmatches(expand_T *xp, int wildmenu)
if (compl_use_pum) {
assert(num_files >= 0);
compl_match_arraysize = num_files;
compl_match_array = xcalloc((size_t)compl_match_arraysize,
sizeof(pumitem_T));
compl_match_array = xmalloc(sizeof(pumitem_T) * (size_t)compl_match_arraysize);
for (i = 0; i < num_files; i++) {
compl_match_array[i].pum_text = (char_u *)L_SHOWFILE(i);
compl_match_array[i] = (pumitem_T){
.pum_text = (char_u *)L_SHOWFILE(i),
.pum_info = NULL,
.pum_extra = NULL,
.pum_kind = NULL,
};
}
char_u *endpos = (char_u *)(showtail ? sm_gettail(xp->xp_pattern, true) : xp->xp_pattern);
if (ui_has(kUICmdline)) {
@ -2514,15 +2531,16 @@ int wildmenu_translate_key(CmdlineInfo *cclp, int key, expand_T *xp, int did_wil
{
int c = key;
if (did_wild_list && p_wmnu) {
if (did_wild_list) {
if (c == K_LEFT) {
c = Ctrl_P;
} else if (c == K_RIGHT) {
c = Ctrl_N;
}
}
// Hitting CR after "emenu Name.": complete submenu
if (xp->xp_context == EXPAND_MENUNAMES && p_wmnu
if (xp->xp_context == EXPAND_MENUNAMES
&& cclp->cmdpos > 1
&& cclp->cmdbuff[cclp->cmdpos - 1] == '.'
&& cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
@ -2548,10 +2566,6 @@ int wildmenu_process_key(CmdlineInfo *cclp, int key, expand_T *xp)
{
int c = key;
if (!p_wmnu) {
return c;
}
// Special translations for 'wildmenu'
if (xp->xp_context == EXPAND_MENUNAMES) {
// Hitting <Down> after "emenu Name.": complete submenu
@ -2696,7 +2710,7 @@ void wildmenu_cleanup(CmdlineInfo *cclp)
}
const bool skt = KeyTyped;
int old_RedrawingDisabled = RedrawingDisabled;
const int old_RedrawingDisabled = RedrawingDisabled;
if (cclp->input_fn) {
RedrawingDisabled = 0;

View File

@ -170,8 +170,6 @@ static Array cmdline_block = ARRAY_DICT_INIT;
/// user interrupting highlight function to not interrupt command-line.
static bool getln_interrupted_highlight = false;
extern pumitem_T *compl_match_array; // TODO(zeertzjq): make this static in cmdexpand.c
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_getln.c.generated.h"
#endif
@ -1023,15 +1021,14 @@ static int command_line_execute(VimState *state, int key)
s->c = Ctrl_P;
}
s->c = wildmenu_translate_key(&ccline, s->c, &s->xpc, s->did_wild_list);
if (p_wmnu) {
s->c = wildmenu_translate_key(&ccline, s->c, &s->xpc, s->did_wild_list);
}
if (compl_match_array || s->did_wild_list) {
if (s->c == Ctrl_E) {
s->res = nextwild(&s->xpc, WILD_CANCEL, WILD_NO_BEEP,
s->firstc != '@');
} else if (s->c == Ctrl_Y) {
s->res = nextwild(&s->xpc, WILD_APPLY, WILD_NO_BEEP,
s->firstc != '@');
if (cmdline_pum_active() || s->did_wild_list) {
if (s->c == Ctrl_E || s->c == Ctrl_Y) {
const int wild_type = (s->c == Ctrl_E) ? WILD_CANCEL : WILD_APPLY;
s->res = nextwild(&s->xpc, wild_type, WILD_NO_BEEP, s->firstc != '@');
s->c = Ctrl_E;
}
}
@ -1040,9 +1037,8 @@ static int command_line_execute(VimState *state, int key)
if (!(s->c == p_wc && KeyTyped) && s->c != p_wcm && s->c != Ctrl_Z
&& s->c != Ctrl_N && s->c != Ctrl_P && s->c != Ctrl_A
&& s->c != Ctrl_L) {
if (compl_match_array) {
pum_undisplay(true);
XFREE_CLEAR(compl_match_array);
if (cmdline_pum_active()) {
cmdline_pum_remove();
}
if (s->xpc.xp_numfiles != -1) {
(void)ExpandOne(&s->xpc, NULL, NULL, 0, WILD_FREE);
@ -1055,7 +1051,9 @@ static int command_line_execute(VimState *state, int key)
wildmenu_cleanup(&ccline);
}
s->c = wildmenu_process_key(&ccline, s->c, &s->xpc);
if (p_wmnu) {
s->c = wildmenu_process_key(&ccline, s->c, &s->xpc);
}
// CTRL-\ CTRL-N goes to Normal mode, CTRL-\ e prompts for an expression.
if (s->c == Ctrl_BSL) {
@ -1279,8 +1277,8 @@ static int command_line_execute(VimState *state, int key)
// <S-Tab> goes to last match, in a clumsy way
if (s->c == K_S_TAB && KeyTyped) {
if (nextwild(&s->xpc, WILD_EXPAND_KEEP, 0, s->firstc != '@') == OK) {
showmatches(&s->xpc, p_wmnu
&& ((wim_flags[s->wim_index] & WIM_LIST) == 0));
// Trigger the popup menu when wildoptions=pum
showmatches(&s->xpc, p_wmnu && ((wim_flags[s->wim_index] & WIM_LIST) == 0));
nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@');
nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@');
return command_line_changed(s);