refactor(cmdexpand.c): deduplicate code (#39582)

This commit is contained in:
zeertzjq
2026-05-04 09:32:03 +08:00
committed by GitHub
parent 0e69a38026
commit cdccd452ff
+16 -25
View File
@@ -3536,16 +3536,8 @@ static int ExpandUserDefined(const char *const pat, expand_T *xp, regmatch_T *re
return OK;
}
/// Expand names with a list returned by a function defined by the user.
static int ExpandUserList(expand_T *xp, char ***matches, int *numMatches)
static void process_user_list(list_T *retlist, char ***matches, int *numMatches)
{
*matches = NULL;
*numMatches = 0;
list_T *const retlist = call_user_expand_func(call_func_retlist, xp);
if (retlist == NULL) {
return FAIL;
}
garray_T ga;
ga_init(&ga, (int)sizeof(char *), 3);
// Loop over the items in the list.
@@ -3562,10 +3554,23 @@ static int ExpandUserList(expand_T *xp, char ***matches, int *numMatches)
*matches = ga.ga_data;
*numMatches = ga.ga_len;
}
/// Expand names with a list returned by a function defined by the user.
static int ExpandUserList(expand_T *xp, char ***matches, int *numMatches)
{
*matches = NULL;
*numMatches = 0;
list_T *const retlist = call_user_expand_func(call_func_retlist, xp);
if (retlist == NULL) {
return FAIL;
}
process_user_list(retlist, matches, numMatches);
return OK;
}
static int ExpandUserLua(expand_T *xp, int *num_file, char ***file)
static int ExpandUserLua(expand_T *xp, int *numMatches, char ***matches)
{
typval_T rettv = TV_INITIAL_VALUE;
nlua_call_user_expand_func(xp, &rettv);
@@ -3576,21 +3581,7 @@ static int ExpandUserLua(expand_T *xp, int *num_file, char ***file)
list_T *const retlist = rettv.vval.v_list;
garray_T ga;
ga_init(&ga, (int)sizeof(char *), 3);
// Loop over the items in the list.
TV_LIST_ITER_CONST(retlist, li, {
if (TV_LIST_ITEM_TV(li)->v_type != VAR_STRING
|| TV_LIST_ITEM_TV(li)->vval.v_string == NULL) {
continue; // Skip non-string items and empty strings.
}
GA_APPEND(char *, &ga, xstrdup(TV_LIST_ITEM_TV(li)->vval.v_string));
});
tv_list_unref(retlist);
*file = ga.ga_data;
*num_file = ga.ga_len;
process_user_list(retlist, matches, numMatches);
return OK;
}