mirror of
https://github.com/neovim/neovim.git
synced 2025-11-16 15:21:20 +00:00
vim-patch:9.1.0547: No way to get the arity of a Vim function (#29638)
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: vim/vim#15097
closes: vim/vim#15109
48b7d05a4f
Co-authored-by: LemonBoy <thatlemon@gmail.com>
This commit is contained in:
@@ -642,6 +642,44 @@ static char *fname_trans_sid(const char *const name, char *const fname_buf, char
|
||||
return fname;
|
||||
}
|
||||
|
||||
int get_func_arity(const char *name, int *required, int *optional, bool *varargs)
|
||||
{
|
||||
int argcount = 0;
|
||||
int min_argcount = 0;
|
||||
|
||||
const EvalFuncDef *fdef = find_internal_func(name);
|
||||
if (fdef != NULL) {
|
||||
argcount = fdef->max_argc;
|
||||
min_argcount = fdef->min_argc;
|
||||
*varargs = false;
|
||||
} else {
|
||||
char fname_buf[FLEN_FIXED + 1];
|
||||
char *tofree = NULL;
|
||||
int error = FCERR_NONE;
|
||||
|
||||
// May need to translate <SNR>123_ to K_SNR.
|
||||
char *fname = fname_trans_sid(name, fname_buf, &tofree, &error);
|
||||
ufunc_T *ufunc = NULL;
|
||||
if (error == FCERR_NONE) {
|
||||
ufunc = find_func(fname);
|
||||
}
|
||||
xfree(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 = ufunc->uf_varargs;
|
||||
}
|
||||
|
||||
*required = min_argcount;
|
||||
*optional = argcount - min_argcount;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/// Find a function by name, return pointer to it in ufuncs.
|
||||
///
|
||||
/// @return NULL for unknown function.
|
||||
|
||||
Reference in New Issue
Block a user