0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 9.1.0547: No way to get the arity of a Vim function

Problem:  No way to get the arity of a Vim function
          (Austin Ziegler)
Solution: Enhance get() Vim script function to return the function
          argument info using get(func, "arity") (LemonBoy)

fixes: #15097
closes: #15109

Signed-off-by: LemonBoy <thatlemon@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
LemonBoy
2024-07-09 18:24:59 +02:00
committed by Christian Brabandt
parent 03acd4761b
commit 48b7d05a4f
9 changed files with 136 additions and 12 deletions

View File

@@ -5503,6 +5503,47 @@ ex_function(exarg_T *eap)
ga_clear_strings(&lines_to_free);
}
int
get_func_arity(char_u *name, int *required, int *optional, int *varargs)
{
ufunc_T *ufunc = NULL;
int argcount = 0;
int min_argcount = 0;
int idx;
idx = find_internal_func(name);
if (idx >= 0)
{
internal_func_get_argcount(idx, &argcount, &min_argcount);
*varargs = FALSE;
}
else
{
char_u fname_buf[FLEN_FIXED + 1];
char_u *tofree = NULL;
funcerror_T error = FCERR_NONE;
char_u *fname;
// May need to translate <SNR>123_ to K_SNR.
fname = fname_trans_sid(name, fname_buf, &tofree, &error);
if (error == FCERR_NONE)
ufunc = find_func(fname, FALSE);
vim_free(tofree);
if (ufunc == NULL)
return FAIL;
argcount = ufunc->uf_args.ga_len;
min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
*varargs = has_varargs(ufunc);
}
*required = min_argcount;
*optional = argcount - min_argcount;
return OK;
}
/*
* Find a function by name, including "<lambda>123".
* Check for "profile" and "debug" arguments and set"compile_type".