0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.2298: Vim9: comment right after "(" of function not recognized

Problem:    Vim9: comment right after "(" of function not recognized.
Solution:   Do not skip over white space before calling get_function_args().
            (closes #7613)
This commit is contained in:
Bram Moolenaar
2021-01-04 14:09:43 +01:00
parent 0ea0440865
commit cef1270dec
4 changed files with 54 additions and 9 deletions

View File

@@ -1,7 +1,6 @@
/* userfunc.c */ /* userfunc.c */
void func_init(void); void func_init(void);
hashtab_T *func_tbl_get(void); hashtab_T *func_tbl_get(void);
int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, garray_T *argtypes, int types_optional, int *varargs, garray_T *default_args, int skip, exarg_T *eap, char_u **line_to_free);
char_u *get_lambda_name(void); char_u *get_lambda_name(void);
char_u *register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state); char_u *register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state);
int get_lambda_tv(char_u **arg, typval_T *rettv, int types_optional, evalarg_T *evalarg); int get_lambda_tv(char_u **arg, typval_T *rettv, int types_optional, evalarg_T *evalarg);

View File

@@ -241,6 +241,42 @@ def Test_call_default_args()
delfunc g:Func delfunc g:Func
enddef enddef
def FuncWithComment( # comment
a: number, #comment
b: bool, # comment
c: string) #comment
assert_equal(4, a)
assert_equal(true, b)
assert_equal('yes', c)
enddef
def Test_func_with_comments()
FuncWithComment(4, true, 'yes')
var lines =<< trim END
def Func(# comment
arg: string)
enddef
END
CheckScriptFailure(lines, 'E125:', 1)
lines =<< trim END
def Func(
arg: string# comment
)
enddef
END
CheckScriptFailure(lines, 'E475:', 2)
lines =<< trim END
def Func(
arg: string
)# comment
enddef
END
CheckScriptFailure(lines, 'E488:', 3)
enddef
def Test_nested_function() def Test_nested_function()
def Nested(arg: string): string def Nested(arg: string): string
return 'nested ' .. arg return 'nested ' .. arg

View File

@@ -154,9 +154,10 @@ one_function_arg(
/* /*
* Get function arguments. * Get function arguments.
* "argp" should point to just after the "(", possibly to white space.
* "argp" is advanced just after "endchar". * "argp" is advanced just after "endchar".
*/ */
int static int
get_function_args( get_function_args(
char_u **argp, char_u **argp,
char_u endchar, char_u endchar,
@@ -170,12 +171,12 @@ get_function_args(
char_u **line_to_free) char_u **line_to_free)
{ {
int mustend = FALSE; int mustend = FALSE;
char_u *arg = *argp; char_u *arg;
char_u *p = arg; char_u *p;
int c; int c;
int any_default = FALSE; int any_default = FALSE;
char_u *expr; char_u *expr;
char_u *whitep = arg; char_u *whitep = *argp;
if (newargs != NULL) if (newargs != NULL)
ga_init2(newargs, (int)sizeof(char_u *), 3); ga_init2(newargs, (int)sizeof(char_u *), 3);
@@ -190,6 +191,8 @@ get_function_args(
/* /*
* Isolate the arguments: "arg1, arg2, ...)" * Isolate the arguments: "arg1, arg2, ...)"
*/ */
arg = skipwhite(*argp);
p = arg;
while (*p != endchar) while (*p != endchar)
{ {
while (eap != NULL && eap->getline != NULL while (eap != NULL && eap->getline != NULL
@@ -548,7 +551,7 @@ get_lambda_tv(
// First, check if this is really a lambda expression. "->" or "=>" must // First, check if this is really a lambda expression. "->" or "=>" must
// be found after the arguments. // be found after the arguments.
s = skipwhite(*arg + 1); s = *arg + 1;
ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL, ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
types_optional ? &argtypes : NULL, types_optional, types_optional ? &argtypes : NULL, types_optional,
NULL, NULL, TRUE, NULL, NULL); NULL, NULL, TRUE, NULL, NULL);
@@ -564,7 +567,7 @@ get_lambda_tv(
pnewargs = &newargs; pnewargs = &newargs;
else else
pnewargs = NULL; pnewargs = NULL;
*arg = skipwhite(*arg + 1); *arg += 1;
ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs, ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
types_optional ? &argtypes : NULL, types_optional, types_optional ? &argtypes : NULL, types_optional,
&varargs, NULL, FALSE, NULL, NULL); &varargs, NULL, FALSE, NULL, NULL);
@@ -2964,6 +2967,7 @@ define_function(exarg_T *eap, char_u *name_arg)
int is_global = FALSE; int is_global = FALSE;
char_u *p; char_u *p;
char_u *arg; char_u *arg;
char_u *whitep;
char_u *line_arg = NULL; char_u *line_arg = NULL;
garray_T newargs; garray_T newargs;
garray_T argtypes; garray_T argtypes;
@@ -3159,7 +3163,6 @@ define_function(exarg_T *eap, char_u *name_arg)
if (vim_strchr(p, '(') != NULL) if (vim_strchr(p, '(') != NULL)
p = vim_strchr(p, '('); p = vim_strchr(p, '(');
} }
p = skipwhite(p + 1);
// In Vim9 script only global functions can be redefined. // In Vim9 script only global functions can be redefined.
if (vim9script && eap->forceit && !is_global) if (vim9script && eap->forceit && !is_global)
@@ -3199,11 +3202,13 @@ define_function(exarg_T *eap, char_u *name_arg)
// This may get more lines and make the pointers into the first line // This may get more lines and make the pointers into the first line
// invalid. // invalid.
++p;
if (get_function_args(&p, ')', &newargs, if (get_function_args(&p, ')', &newargs,
eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE, eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
&varargs, &default_args, eap->skip, &varargs, &default_args, eap->skip,
eap, &line_to_free) == FAIL) eap, &line_to_free) == FAIL)
goto errret_2; goto errret_2;
whitep = p;
if (eap->cmdidx == CMD_def) if (eap->cmdidx == CMD_def)
{ {
@@ -3215,6 +3220,7 @@ define_function(exarg_T *eap, char_u *name_arg)
if (p > ret_type) if (p > ret_type)
{ {
ret_type = vim_strnsave(ret_type, p - ret_type); ret_type = vim_strnsave(ret_type, p - ret_type);
whitep = p;
p = skipwhite(p); p = skipwhite(p);
} }
else else
@@ -3229,6 +3235,7 @@ define_function(exarg_T *eap, char_u *name_arg)
// find extra arguments "range", "dict", "abort" and "closure" // find extra arguments "range", "dict", "abort" and "closure"
for (;;) for (;;)
{ {
whitep = p;
p = skipwhite(p); p = skipwhite(p);
if (STRNCMP(p, "range", 5) == 0) if (STRNCMP(p, "range", 5) == 0)
{ {
@@ -3267,7 +3274,8 @@ define_function(exarg_T *eap, char_u *name_arg)
else if (*p != NUL else if (*p != NUL
&& !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function) && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
&& eap->cmdidx != CMD_def) && eap->cmdidx != CMD_def)
&& !(*p == '#' && (vim9script || eap->cmdidx == CMD_def)) && !(VIM_ISWHITE(*whitep) && *p == '#'
&& (vim9script || eap->cmdidx == CMD_def))
&& !eap->skip && !eap->skip
&& !did_emsg) && !did_emsg)
semsg(_(e_trailing_arg), p); semsg(_(e_trailing_arg), p);

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2298,
/**/ /**/
2297, 2297,
/**/ /**/