vim-patch:9.1.1109: cmdexpand.c hard to read (#32437)

Problem:  cmdexpand.c hard to read
Solution: refactor the file slightly (glepnir)

closes: vim/vim#16621

977561a719

Co-authored-by: glepnir <glephunter@gmail.com>
This commit is contained in:
zeertzjq
2025-02-14 08:26:52 +08:00
committed by GitHub
parent 6b387bde69
commit 478c71f03c

View File

@@ -645,6 +645,7 @@ static void redraw_wildmenu(expand_T *xp, int num_matches, char **matches, int m
/// in "xp->xp_selected" /// in "xp->xp_selected"
static char *get_next_or_prev_match(int mode, expand_T *xp) static char *get_next_or_prev_match(int mode, expand_T *xp)
{ {
// When no files found, return NULL
if (xp->xp_numfiles <= 0) { if (xp->xp_numfiles <= 0) {
return NULL; return NULL;
} }
@@ -652,45 +653,42 @@ static char *get_next_or_prev_match(int mode, expand_T *xp)
int findex = xp->xp_selected; int findex = xp->xp_selected;
if (mode == WILD_PREV) { if (mode == WILD_PREV) {
// Select last file if at start
if (findex == -1) { if (findex == -1) {
findex = xp->xp_numfiles; findex = xp->xp_numfiles;
} }
findex--; findex--;
} else if (mode == WILD_NEXT) { } else if (mode == WILD_NEXT) {
findex++; // Select next file
} else if (mode == WILD_PAGEUP) { findex = findex + 1;
if (findex == 0) { } else if (mode == WILD_PAGEUP || mode == WILD_PAGEDOWN) {
// at the first entry, don't select any entries // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
findex = -1; int ht = pum_get_height();
} else if (findex == -1) { if (ht > 3) {
// no entry is selected. select the last entry ht -= 2;
findex = xp->xp_numfiles - 1;
} else {
// go up by the pum height
int ht = pum_get_height();
if (ht > 3) {
ht -= 2;
}
findex -= ht;
findex = MAX(findex, 0); // few entries left, select the first entry
} }
} else if (mode == WILD_PAGEDOWN) {
if (findex == xp->xp_numfiles - 1) { if (mode == WILD_PAGEUP) {
// at the last entry, don't select any entries if (findex == 0) {
findex = -1; // at the first entry, don't select any entries
} else if (findex == -1) { findex = -1;
// no entry is selected. select the first entry } else if (findex == -1) {
findex = 0; // no entry is selected. select the last entry
} else {
// go down by the pum height
int ht = pum_get_height();
if (ht > 3) {
ht -= 2;
}
findex += ht;
if (findex >= xp->xp_numfiles) {
// few entries left, select the last entry
findex = xp->xp_numfiles - 1; findex = xp->xp_numfiles - 1;
} else {
// go up by the pum height
findex = MAX(findex - ht, 0);
}
} else { // mode == WILD_PAGEDOWN
if (findex < 0) {
// no entry is selected. select the first entry
findex = 0;
} else if (findex == xp->xp_numfiles - 1) {
// at the last entry, don't select any entries
findex = -1;
} else {
// go down by the pum height
findex = MIN(findex + ht, xp->xp_numfiles - 1);
} }
} }
} else { // mode == WILD_PUM_WANT } else { // mode == WILD_PUM_WANT
@@ -698,21 +696,27 @@ static char *get_next_or_prev_match(int mode, expand_T *xp)
findex = pum_want.item; findex = pum_want.item;
} }
// When wrapping around, return the original string, set findex to -1. // Handle wrapping around
if (findex < 0) { if (findex < 0 || findex >= xp->xp_numfiles) {
findex = xp->xp_orig == NULL ? xp->xp_numfiles - 1 : -1; // If original string exists, return to it when wrapping around
} if (xp->xp_orig != NULL) {
if (findex >= xp->xp_numfiles) { findex = -1;
findex = xp->xp_orig == NULL ? 0 : -1; } else {
// Wrap around to opposite end
findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
}
} }
// Display matches on screen
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 (p_wmnu) {
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 string or the selected match
return xstrdup(findex == -1 ? xp->xp_orig : xp->xp_files[findex]); return xstrdup(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
} }