0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 9.0.1053: default constructor arguments are not optional

Problem:    Default constructor arguments are not optional.
Solution:   Use "= v:none" to make constructor arguments optional.
This commit is contained in:
Bram Moolenaar
2022-12-13 18:43:22 +00:00
parent 692fe0889c
commit 65b0d16768
11 changed files with 174 additions and 35 deletions

View File

@@ -224,7 +224,6 @@ get_function_args(
char_u *p;
int c;
int any_default = FALSE;
char_u *expr;
char_u *whitep = *argp;
if (newargs != NULL)
@@ -302,6 +301,34 @@ get_function_args(
arg = p;
while (ASCII_ISALNUM(*p) || *p == '_')
++p;
char_u *argend = p;
if (*skipwhite(p) == '=')
{
char_u *defval = skipwhite(skipwhite(p) + 1);
if (STRNCMP(defval, "v:none", 6) != 0)
{
semsg(_(e_constructor_default_value_must_be_vnone_str), p);
goto err_ret;
}
any_default = TRUE;
p = defval + 6;
if (ga_grow(default_args, 1) == FAIL)
goto err_ret;
char_u *expr = vim_strsave((char_u *)"v:none");
if (expr == NULL)
goto err_ret;
((char_u **)(default_args->ga_data))
[default_args->ga_len] = expr;
default_args->ga_len++;
}
else if (any_default)
{
emsg(_(e_non_default_argument_follows_default_argument));
goto err_ret;
}
// TODO: check the argument is indeed a member
if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
@@ -309,7 +336,7 @@ get_function_args(
if (newargs != NULL)
{
((char_u **)(newargs->ga_data))[newargs->ga_len] =
vim_strnsave(arg, p - arg);
vim_strnsave(arg, argend - arg);
newargs->ga_len++;
if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
@@ -322,15 +349,22 @@ get_function_args(
if (ga_grow(newlines, 1) == OK)
{
// "this.name = name"
int len = 5 + (p - arg) + 3 + (p - arg) + 1;
int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
if (any_default)
len += 14 + 10;
char_u *assignment = alloc(len);
if (assignment != NULL)
{
c = *p;
*p = NUL;
vim_snprintf((char *)assignment, len,
c = *argend;
*argend = NUL;
if (any_default)
vim_snprintf((char *)assignment, len,
"ifargisset %d this.%s = %s",
default_args->ga_len - 1, arg, arg);
else
vim_snprintf((char *)assignment, len,
"this.%s = %s", arg, arg);
*p = c;
*argend = c;
((char_u **)(newlines->ga_data))[
newlines->ga_len++] = assignment;
}
@@ -361,7 +395,7 @@ get_function_args(
// find the end of the expression (doesn't evaluate it)
any_default = TRUE;
p = skipwhite(np + 1);
expr = p;
char_u *expr = p;
if (eval1(&p, &rettv, NULL) != FAIL)
{
if (!skip)