Skip to content

Commit 478c71f

Browse files
zeertzjqglepnir
andauthored
vim-patch:9.1.1109: cmdexpand.c hard to read (neovim#32437)
Problem: cmdexpand.c hard to read Solution: refactor the file slightly (glepnir) closes: vim/vim#16621 vim/vim@977561a Co-authored-by: glepnir <[email protected]>
1 parent 6b387bd commit 478c71f

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

src/nvim/cmdexpand.c

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -645,74 +645,78 @@ static void redraw_wildmenu(expand_T *xp, int num_matches, char **matches, int m
645645
/// in "xp->xp_selected"
646646
static char *get_next_or_prev_match(int mode, expand_T *xp)
647647
{
648+
// When no files found, return NULL
648649
if (xp->xp_numfiles <= 0) {
649650
return NULL;
650651
}
651652

652653
int findex = xp->xp_selected;
653654

654655
if (mode == WILD_PREV) {
656+
// Select last file if at start
655657
if (findex == -1) {
656658
findex = xp->xp_numfiles;
657659
}
658660
findex--;
659661
} else if (mode == WILD_NEXT) {
660-
findex++;
661-
} else if (mode == WILD_PAGEUP) {
662-
if (findex == 0) {
663-
// at the first entry, don't select any entries
664-
findex = -1;
665-
} else if (findex == -1) {
666-
// no entry is selected. select the last entry
667-
findex = xp->xp_numfiles - 1;
668-
} else {
669-
// go up by the pum height
670-
int ht = pum_get_height();
671-
if (ht > 3) {
672-
ht -= 2;
673-
}
674-
findex -= ht;
675-
findex = MAX(findex, 0); // few entries left, select the first entry
676-
}
677-
} else if (mode == WILD_PAGEDOWN) {
678-
if (findex == xp->xp_numfiles - 1) {
679-
// at the last entry, don't select any entries
680-
findex = -1;
681-
} else if (findex == -1) {
682-
// no entry is selected. select the first entry
683-
findex = 0;
684-
} else {
685-
// go down by the pum height
686-
int ht = pum_get_height();
687-
if (ht > 3) {
688-
ht -= 2;
689-
}
690-
findex += ht;
691-
if (findex >= xp->xp_numfiles) {
692-
// few entries left, select the last entry
662+
// Select next file
663+
findex = findex + 1;
664+
} else if (mode == WILD_PAGEUP || mode == WILD_PAGEDOWN) {
665+
// Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
666+
int ht = pum_get_height();
667+
if (ht > 3) {
668+
ht -= 2;
669+
}
670+
671+
if (mode == WILD_PAGEUP) {
672+
if (findex == 0) {
673+
// at the first entry, don't select any entries
674+
findex = -1;
675+
} else if (findex == -1) {
676+
// no entry is selected. select the last entry
693677
findex = xp->xp_numfiles - 1;
678+
} else {
679+
// go up by the pum height
680+
findex = MAX(findex - ht, 0);
681+
}
682+
} else { // mode == WILD_PAGEDOWN
683+
if (findex < 0) {
684+
// no entry is selected. select the first entry
685+
findex = 0;
686+
} else if (findex == xp->xp_numfiles - 1) {
687+
// at the last entry, don't select any entries
688+
findex = -1;
689+
} else {
690+
// go down by the pum height
691+
findex = MIN(findex + ht, xp->xp_numfiles - 1);
694692
}
695693
}
696694
} else { // mode == WILD_PUM_WANT
697695
assert(pum_want.active);
698696
findex = pum_want.item;
699697
}
700698

701-
// When wrapping around, return the original string, set findex to -1.
702-
if (findex < 0) {
703-
findex = xp->xp_orig == NULL ? xp->xp_numfiles - 1 : -1;
704-
}
705-
if (findex >= xp->xp_numfiles) {
706-
findex = xp->xp_orig == NULL ? 0 : -1;
699+
// Handle wrapping around
700+
if (findex < 0 || findex >= xp->xp_numfiles) {
701+
// If original string exists, return to it when wrapping around
702+
if (xp->xp_orig != NULL) {
703+
findex = -1;
704+
} else {
705+
// Wrap around to opposite end
706+
findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
707+
}
707708
}
709+
710+
// Display matches on screen
708711
if (compl_match_array) {
709712
compl_selected = findex;
710713
cmdline_pum_display(false);
711714
} else if (p_wmnu) {
712715
redraw_wildmenu(xp, xp->xp_numfiles, xp->xp_files, findex, cmd_showtail);
713716
}
714-
xp->xp_selected = findex;
715717

718+
xp->xp_selected = findex;
719+
// Return the original string or the selected match
716720
return xstrdup(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
717721
}
718722

0 commit comments

Comments
 (0)