Skip to content

Commit cef1270

Browse files
committed
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)
1 parent 0ea0440 commit cef1270

4 files changed

Lines changed: 54 additions & 9 deletions

File tree

src/proto/userfunc.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* userfunc.c */
22
void func_init(void);
33
hashtab_T *func_tbl_get(void);
4-
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);
54
char_u *get_lambda_name(void);
65
char_u *register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state);
76
int get_lambda_tv(char_u **arg, typval_T *rettv, int types_optional, evalarg_T *evalarg);

src/testdir/test_vim9_func.vim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,42 @@ def Test_call_default_args()
241241
delfunc g:Func
242242
enddef
243243

244+
def FuncWithComment( # comment
245+
a: number, #comment
246+
b: bool, # comment
247+
c: string) #comment
248+
assert_equal(4, a)
249+
assert_equal(true, b)
250+
assert_equal('yes', c)
251+
enddef
252+
253+
def Test_func_with_comments()
254+
FuncWithComment(4, true, 'yes')
255+
256+
var lines =<< trim END
257+
def Func(# comment
258+
arg: string)
259+
enddef
260+
END
261+
CheckScriptFailure(lines, 'E125:', 1)
262+
263+
lines =<< trim END
264+
def Func(
265+
arg: string# comment
266+
)
267+
enddef
268+
END
269+
CheckScriptFailure(lines, 'E475:', 2)
270+
271+
lines =<< trim END
272+
def Func(
273+
arg: string
274+
)# comment
275+
enddef
276+
END
277+
CheckScriptFailure(lines, 'E488:', 3)
278+
enddef
279+
244280
def Test_nested_function()
245281
def Nested(arg: string): string
246282
return 'nested ' .. arg

src/userfunc.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ one_function_arg(
154154

155155
/*
156156
* Get function arguments.
157+
* "argp" should point to just after the "(", possibly to white space.
157158
* "argp" is advanced just after "endchar".
158159
*/
159-
int
160+
static int
160161
get_function_args(
161162
char_u **argp,
162163
char_u endchar,
@@ -170,12 +171,12 @@ get_function_args(
170171
char_u **line_to_free)
171172
{
172173
int mustend = FALSE;
173-
char_u *arg = *argp;
174-
char_u *p = arg;
174+
char_u *arg;
175+
char_u *p;
175176
int c;
176177
int any_default = FALSE;
177178
char_u *expr;
178-
char_u *whitep = arg;
179+
char_u *whitep = *argp;
179180

180181
if (newargs != NULL)
181182
ga_init2(newargs, (int)sizeof(char_u *), 3);
@@ -190,6 +191,8 @@ get_function_args(
190191
/*
191192
* Isolate the arguments: "arg1, arg2, ...)"
192193
*/
194+
arg = skipwhite(*argp);
195+
p = arg;
193196
while (*p != endchar)
194197
{
195198
while (eap != NULL && eap->getline != NULL
@@ -548,7 +551,7 @@ get_lambda_tv(
548551

549552
// First, check if this is really a lambda expression. "->" or "=>" must
550553
// be found after the arguments.
551-
s = skipwhite(*arg + 1);
554+
s = *arg + 1;
552555
ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
553556
types_optional ? &argtypes : NULL, types_optional,
554557
NULL, NULL, TRUE, NULL, NULL);
@@ -564,7 +567,7 @@ get_lambda_tv(
564567
pnewargs = &newargs;
565568
else
566569
pnewargs = NULL;
567-
*arg = skipwhite(*arg + 1);
570+
*arg += 1;
568571
ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
569572
types_optional ? &argtypes : NULL, types_optional,
570573
&varargs, NULL, FALSE, NULL, NULL);
@@ -2964,6 +2967,7 @@ define_function(exarg_T *eap, char_u *name_arg)
29642967
int is_global = FALSE;
29652968
char_u *p;
29662969
char_u *arg;
2970+
char_u *whitep;
29672971
char_u *line_arg = NULL;
29682972
garray_T newargs;
29692973
garray_T argtypes;
@@ -3159,7 +3163,6 @@ define_function(exarg_T *eap, char_u *name_arg)
31593163
if (vim_strchr(p, '(') != NULL)
31603164
p = vim_strchr(p, '(');
31613165
}
3162-
p = skipwhite(p + 1);
31633166

31643167
// In Vim9 script only global functions can be redefined.
31653168
if (vim9script && eap->forceit && !is_global)
@@ -3199,11 +3202,13 @@ define_function(exarg_T *eap, char_u *name_arg)
31993202

32003203
// This may get more lines and make the pointers into the first line
32013204
// invalid.
3205+
++p;
32023206
if (get_function_args(&p, ')', &newargs,
32033207
eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
32043208
&varargs, &default_args, eap->skip,
32053209
eap, &line_to_free) == FAIL)
32063210
goto errret_2;
3211+
whitep = p;
32073212

32083213
if (eap->cmdidx == CMD_def)
32093214
{
@@ -3215,6 +3220,7 @@ define_function(exarg_T *eap, char_u *name_arg)
32153220
if (p > ret_type)
32163221
{
32173222
ret_type = vim_strnsave(ret_type, p - ret_type);
3223+
whitep = p;
32183224
p = skipwhite(p);
32193225
}
32203226
else
@@ -3229,6 +3235,7 @@ define_function(exarg_T *eap, char_u *name_arg)
32293235
// find extra arguments "range", "dict", "abort" and "closure"
32303236
for (;;)
32313237
{
3238+
whitep = p;
32323239
p = skipwhite(p);
32333240
if (STRNCMP(p, "range", 5) == 0)
32343241
{
@@ -3267,7 +3274,8 @@ define_function(exarg_T *eap, char_u *name_arg)
32673274
else if (*p != NUL
32683275
&& !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
32693276
&& eap->cmdidx != CMD_def)
3270-
&& !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3277+
&& !(VIM_ISWHITE(*whitep) && *p == '#'
3278+
&& (vim9script || eap->cmdidx == CMD_def))
32713279
&& !eap->skip
32723280
&& !did_emsg)
32733281
semsg(_(e_trailing_arg), p);

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2298,
753755
/**/
754756
2297,
755757
/**/

0 commit comments

Comments
 (0)