From cdccd452ff686990afebbabb25e419090f12e715 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 4 May 2026 09:32:03 +0800 Subject: [PATCH] refactor(cmdexpand.c): deduplicate code (#39582) --- src/nvim/cmdexpand.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/nvim/cmdexpand.c b/src/nvim/cmdexpand.c index 1cd6408746..acd5d05fd3 100644 --- a/src/nvim/cmdexpand.c +++ b/src/nvim/cmdexpand.c @@ -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; }