refactor(completion): use an inline function to free cptext (#26380)

This commit is contained in:
Raphael 2023-12-04 15:49:31 +08:00 committed by GitHub
parent 5651c1ff27
commit 589f4761ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -747,6 +747,16 @@ int ins_compl_add_infercase(char *str_arg, int len, bool icase, char *fname, Dir
return res;
}
/// free cptext
static inline void free_cptext(char *const *const cptext)
{
if (cptext != NULL) {
for (size_t i = 0; i < CPT_COUNT; i++) {
xfree(cptext[i]);
}
}
}
/// Add a match to the list of matches
///
/// @param[in] str text of the match to add
@ -784,16 +794,10 @@ static int ins_compl_add(char *const str, int len, char *const fname, char *cons
} else {
os_breakcheck();
}
#define FREE_CPTEXT(cptext, cptext_allocated) \
do { \
if ((cptext) != NULL && (cptext_allocated)) { \
for (size_t i = 0; i < CPT_COUNT; i++) { \
xfree((cptext)[i]); \
} \
} \
} while (0)
if (got_int) {
FREE_CPTEXT(cptext, cptext_allocated);
if (cptext_allocated) {
free_cptext(cptext);
}
return FAIL;
}
if (len < 0) {
@ -807,7 +811,9 @@ static int ins_compl_add(char *const str, int len, char *const fname, char *cons
if (!match_at_original_text(match)
&& strncmp(match->cp_str, str, (size_t)len) == 0
&& ((int)strlen(match->cp_str) <= len || match->cp_str[len] == NUL)) {
FREE_CPTEXT(cptext, cptext_allocated);
if (cptext_allocated) {
free_cptext(cptext);
}
return NOTDONE;
}
match = match->cp_next;
@ -1552,9 +1558,7 @@ static void ins_compl_free(void)
if (match->cp_flags & CP_FREE_FNAME) {
xfree(match->cp_fname);
}
for (int i = 0; i < CPT_COUNT; i++) {
xfree(match->cp_text[i]);
}
free_cptext(match->cp_text);
tv_clear(&match->cp_user_data);
xfree(match);
} while (compl_curr_match != NULL && !is_first_match(compl_curr_match));
@ -2459,9 +2463,7 @@ static int ins_compl_add_tv(typval_T *const tv, const Direction dir, bool fast)
CLEAR_FIELD(cptext);
}
if (word == NULL || (!empty && *word == NUL)) {
for (size_t i = 0; i < CPT_COUNT; i++) {
xfree(cptext[i]);
}
free_cptext(cptext);
tv_clear(&user_data);
return FAIL;
}