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>
diff --git a/src/userfunc.c b/src/userfunc.c
index 7536234..e44397d 100644
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -5503,6 +5503,47 @@
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".