]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0547: No way to get the arity of a Vim function v9.1.0547
authorLemonBoy <thatlemon@gmail.com>
Tue, 9 Jul 2024 16:24:59 +0000 (18:24 +0200)
committerChristian Brabandt <cb@256bit.org>
Tue, 9 Jul 2024 16:31:12 +0000 (18:31 +0200)
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>
runtime/doc/builtin.txt
runtime/doc/tags
runtime/doc/version9.txt
src/evalfunc.c
src/proto/userfunc.pro
src/testdir/test_getvar.vim
src/testdir/test_partial.vim
src/userfunc.c
src/version.c

index 238b7e43d2613beb03f2202ad47bad0ca39501ee..635b7b2218c0cd927182217e0c702ca1a3ca122f 100644 (file)
@@ -1,4 +1,4 @@
-*builtin.txt*  For Vim version 9.1.  Last change: 2024 Jun 23
+*builtin.txt*  For Vim version 9.1.  Last change: 2024 Jul 09
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -3574,7 +3574,7 @@ garbagecollect([{atexit}])                                *garbagecollect()*
                Return type: |String|
 
 
-get({list}, {idx} [, {default}])                       *get()*
+get({list}, {idx} [, {default}])                       *get()* *get()-list*
                Get item {idx} from |List| {list}.  When this item is not
                available return {default}.  Return zero when {default} is
                omitted.
@@ -3583,7 +3583,7 @@ get({list}, {idx} [, {default}])                  *get()*
 <
                Return type: any, depending on {list}
 
-get({blob}, {idx} [, {default}])
+get({blob}, {idx} [, {default}])                       *get()-blob*
                Get byte {idx} from |Blob| {blob}.  When this byte is not
                available return {default}.  Return -1 when {default} is
                omitted.
@@ -3592,7 +3592,7 @@ get({blob}, {idx} [, {default}])
 <
                Return type: |Number|
 
-get({dict}, {key} [, {default}])
+get({dict}, {key} [, {default}])                       *get()-dict*
                Get item with key {key} from |Dictionary| {dict}.  When this
                item is not available return {default}.  Return zero when
                {default} is omitted.  Useful example: >
@@ -3604,18 +3604,32 @@ get({dict}, {key} [, {default}])
 <
                Return type: any, depending on {dict}
 
-get({func}, {what})
-               Get item {what} from Funcref {func}.  Possible values for
+get({func}, {what})                                    *get()-func*
+               Get item {what} from |Funcref| {func}.  Possible values for
                {what} are:
-                       "name"  The function name
-                       "func"  The function
-                       "dict"  The dictionary
-                       "args"  The list with arguments
+                 "name"    The function name
+                 "func"    The function
+                 "dict"    The dictionary
+                 "args"    The list with arguments
+                 "arity"   A dictionary with information about the number of
+                           arguments accepted by the function (minus the
+                           {arglist}) with the following fields:
+                               required    the number of positional arguments
+                               optional    the number of optional arguments,
+                                           in addition to the required ones
+                               varargs     |TRUE| if the function accepts a
+                                           variable number of arguments |...|
+
+                               Note: There is no error, if the {arglist} of
+                               the Funcref contains more arguments than the
+                               Funcref expects, it's not validated.
+
                Returns zero on error.
+
                Preferably used as a |method|: >
                        myfunc->get(what)
 <
-               Return type: any, depending on {func}
+               Return type: any, depending on {func} and {what}
 
                                                        *getbufinfo()*
 getbufinfo([{buf}])
index e72f73927d247f93892f42089ffdb79ee9d3fee9..be71710bc46004f9736770a7908e1e41a30641cf 100644 (file)
@@ -7783,6 +7783,10 @@ gdb-version      terminal.txt    /*gdb-version*
 ge     motion.txt      /*ge*
 gender-neutral helphelp.txt    /*gender-neutral*
 get()  builtin.txt     /*get()*
+get()-blob     builtin.txt     /*get()-blob*
+get()-dict     builtin.txt     /*get()-dict*
+get()-func     builtin.txt     /*get()-func*
+get()-list     builtin.txt     /*get()-list*
 get-ms-debuggers       debug.txt       /*get-ms-debuggers*
 getbufinfo()   builtin.txt     /*getbufinfo()*
 getbufline()   builtin.txt     /*getbufline()*
index 92792b9cd5542ce900ed51ce519a04a9694fcac1..43de2692fa8a03735bce9979b338560a2058ca73 100644 (file)
@@ -1,4 +1,4 @@
-*version9.txt*  For Vim version 9.1.  Last change: 2024 Jul 06
+*version9.txt*  For Vim version 9.1.  Last change: 2024 Jul 08
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -41577,6 +41577,8 @@ Changed~
 - 'nrformat' accepts the new "blank" suboption, to determine a signed or
   unsigned number based on whitespace in front of a minus sign.
 - allow to specify a priority when defining a new sign |:sign-define|
+- provide information about function arguments using the get(func, "arity")
+  function |get()-func|
 
                                                        *added-9.2*
 Added ~
index c9480f9f64829b4d11dea65cf2a183ca472c396c..5e3122dd975ccbbc2c77d1cbb54e153225d55e34 100644 (file)
@@ -5134,6 +5134,36 @@ f_get(typval_T *argvars, typval_T *rettv)
                        list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
                }
            }
+           else if (STRCMP(what, "arity") == 0)
+           {
+               int required = 0, optional = 0, varargs = FALSE;
+               char_u *name = partial_name(pt);
+
+               get_func_arity(name, &required, &optional, &varargs);
+
+               rettv->v_type = VAR_DICT;
+               if (rettv_dict_alloc(rettv) == OK)
+               {
+                   dict_T *dict = rettv->vval.v_dict;
+
+                   // Take into account the arguments of the partial, if any.
+                   // Note that it is possible to supply more arguments than the function
+                   // accepts.
+                   if (pt->pt_argc >= required + optional)
+                       required = optional = 0;
+                   else if (pt->pt_argc > required)
+                   {
+                       optional -= pt->pt_argc - required;
+                       required = 0;
+                   }
+                   else
+                       required -= pt->pt_argc;
+
+                   dict_add_number(dict, "required", required);
+                   dict_add_number(dict, "optional", optional);
+                   dict_add_bool(dict, "varargs", varargs);
+               }
+           }
            else
                semsg(_(e_invalid_argument_str), what);
 
index 9bb461663e41c064bec5e93b1830f70d3e3ad165..ce5d257caf2a2911e0f17fccb8acd64a1478b0e9 100644 (file)
@@ -95,4 +95,5 @@ int set_ref_in_call_stack(int copyID);
 int set_ref_in_functions(int copyID);
 int set_ref_in_func_args(int copyID);
 int set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID);
+int get_func_arity(char_u *name, int *required, int *optional, int *varargs);
 /* vim: set ft=c : */
index 2065186a5ad52ddce3ce0d7f15dffcde0cf8b35d..6efb192ebc6ad22b566cde4955a750909e26e060 100644 (file)
@@ -142,20 +142,28 @@ func Test_get_func()
   let l:F = function('tr')
   call assert_equal('tr', get(l:F, 'name'))
   call assert_equal(l:F, get(l:F, 'func'))
+  call assert_equal({'required': 3, 'optional': 0, 'varargs': v:false},
+      \ get(l:F, 'arity'))
 
   let Fb_func = function('s:FooBar')
   call assert_match('<SNR>\d\+_FooBar', get(Fb_func, 'name'))
+  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false},
+      \ get(Fb_func, 'arity'))
   let Fb_ref = funcref('s:FooBar')
   call assert_match('<SNR>\d\+_FooBar', get(Fb_ref, 'name'))
+  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false},
+      \ get(Fb_ref, 'arity'))
 
   call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'}))
   call assert_equal(0, get(l:F, 'dict'))
   call assert_equal([], get(l:F, 'args'))
+
   let NF = test_null_function()
   call assert_equal('', get(NF, 'name'))
   call assert_equal(NF, get(NF, 'func'))
   call assert_equal(0, get(NF, 'dict'))
   call assert_equal([], get(NF, 'args'))
+  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false}, get(NF, 'arity'))
 endfunc
 
 " get({partial}, {what} [, {default}]) - in test_partial.vim
index b5a58f6e59fc93080eac71885decd6df4527425e..acc8b73c815e7b9bd561ec0ced88825668b2b019 100644 (file)
@@ -311,6 +311,11 @@ func Test_auto_partial_rebind()
 endfunc
 
 func Test_get_partial_items()
+  func s:Qux(x, y, z=3, w=1, ...)
+  endfunc
+  func s:Qux1(x, y)
+  endfunc
+
   let dict = {'name': 'hello'}
   let args = ["foo", "bar"]
   let Func = function('MyDictFunc')
@@ -331,6 +336,23 @@ func Test_get_partial_items()
   let dict = {'partial has': 'no dict'}
   call assert_equal(dict, get(P, 'dict', dict))
   call assert_equal(0, get(l:P, 'dict'))
+
+  call assert_equal({'required': 2, 'optional': 2, 'varargs': v:true},
+      \ get(funcref('s:Qux', []), 'arity'))
+  call assert_equal({'required': 1, 'optional': 2, 'varargs': v:true},
+      \ get(funcref('s:Qux', [1]), 'arity'))
+  call assert_equal({'required': 0, 'optional': 2, 'varargs': v:true},
+      \ get(funcref('s:Qux', [1, 2]), 'arity'))
+  call assert_equal({'required': 0, 'optional': 1, 'varargs': v:true},
+      \ get(funcref('s:Qux', [1, 2, 3]), 'arity'))
+  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:true},
+      \ get(funcref('s:Qux', [1, 2, 3, 4]), 'arity'))
+  " More args than expected is not an error
+  call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false},
+      \ get(funcref('s:Qux1', [1, 2, 3, 4]), 'arity'))
+
+  delfunc s:Qux
+  delfunc s:Qux1
 endfunc
 
 func Test_compare_partials()
index 7536234b829f4f87dc2d1860d55b415dc4209eab..e44397d81b7c2ca6c5d3f68dc5ccf28e6ae2c91a 100644 (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".
index 53bafd37d51cef9320f17311846eeb19ae0b872a..ce6b8d60ab5a5219e4fa0d2813a98eaed5f931b4 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    547,
 /**/
     546,
 /**/