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

patch 8.2.2556: Vim9: :import with "as" not fully supported

Problem:    Vim9: :import with "as" not fully supported.
Solution:   Implement "as" for more cases.
This commit is contained in:
Bram Moolenaar
2021-02-27 22:41:19 +01:00
parent 3f1e9f000f
commit 0a84284e60
3 changed files with 98 additions and 66 deletions

View File

@@ -325,82 +325,43 @@ handle_import(
{
char_u *arg = arg_start;
char_u *cmd_end = NULL;
char_u *as_name = NULL;
int ret = FAIL;
typval_T tv;
int sid = -1;
int res;
int mult = FALSE;
garray_T names;
garray_T as_names;
ga_init2(&names, sizeof(char_u *), 10);
ga_init2(&as_names, sizeof(char_u *), 10);
if (*arg == '{')
{
// "import {item, item} from ..."
mult = TRUE;
arg = skipwhite_and_linebreak(arg + 1, evalarg);
for (;;)
{
char_u *p = arg;
int had_comma = FALSE;
while (eval_isnamec(*arg))
++arg;
if (p == arg)
break;
if (ga_grow(&names, 1) == FAIL)
goto erret;
((char_u **)names.ga_data)[names.ga_len] =
vim_strnsave(p, arg - p);
++names.ga_len;
if (*arg == ',')
{
had_comma = TRUE;
++arg;
}
arg = skipwhite_and_linebreak(arg, evalarg);
if (*arg == '}')
{
arg = skipwhite_and_linebreak(arg + 1, evalarg);
break;
}
if (!had_comma)
{
emsg(_(e_missing_comma_in_import));
goto erret;
}
}
if (names.ga_len == 0)
{
emsg(_(e_syntax_error_in_import));
goto erret;
}
}
else
{
// "import Name from ..."
// "import * as Name from ..."
// "import item [as Name] from ..."
arg = skipwhite_and_linebreak(arg, evalarg);
if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
arg = skipwhite_and_linebreak(arg + 1, evalarg);
else if (eval_isnamec1(*arg))
{
char_u *p = arg;
for (;;)
{
char_u *p = arg;
int had_comma = FALSE;
char_u *as_name = NULL;
// accept "*" or "Name"
if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
++arg;
else
while (eval_isnamec(*arg))
++arg;
if (ga_grow(&names, 1) == FAIL)
goto erret;
((char_u **)names.ga_data)[names.ga_len] =
vim_strnsave(p, arg - p);
++names.ga_len;
arg = skipwhite_and_linebreak(arg, evalarg);
}
else
{
emsg(_(e_syntax_error_in_import));
if (p == arg)
break;
if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
goto erret;
}
((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
++names.ga_len;
arg = skipwhite_and_linebreak(arg, evalarg);
if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
{
char_u *p;
@@ -421,6 +382,35 @@ handle_import(
emsg(_(e_missing_as_after_star));
goto erret;
}
// without "as Name" the as_names entry is NULL
((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
++as_names.ga_len;
if (!mult)
break;
if (*arg == ',')
{
had_comma = TRUE;
++arg;
}
arg = skipwhite_and_linebreak(arg, evalarg);
if (*arg == '}')
{
++arg;
break;
}
if (!had_comma)
{
emsg(_(e_missing_comma_in_import));
goto erret;
}
}
arg = skipwhite_and_linebreak(arg, evalarg);
if (names.ga_len == 0)
{
emsg(_(e_syntax_error_in_import));
goto erret;
}
if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
@@ -501,8 +491,10 @@ handle_import(
if (*arg_start == '*')
{
imported_T *imported;
imported_T *imported;
char_u *as_name = ((char_u **)as_names.ga_data)[0];
// "import * as That"
imported = find_imported(as_name, STRLEN(as_name), cctx);
if (imported != NULL && imported->imp_sid == sid)
{
@@ -521,7 +513,7 @@ handle_import(
if (imported == NULL)
goto erret;
imported->imp_name = as_name;
as_name = NULL;
((char_u **)as_names.ga_data)[0] = NULL;
imported->imp_sid = sid;
imported->imp_flags = IMP_FLAGS_STAR;
}
@@ -535,6 +527,7 @@ handle_import(
for (i = 0; i < names.ga_len; ++i)
{
char_u *name = ((char_u **)names.ga_data)[i];
char_u *as_name = ((char_u **)as_names.ga_data)[i];
size_t len = STRLEN(name);
int idx;
imported_T *imported;
@@ -572,10 +565,17 @@ handle_import(
if (imported == NULL)
goto erret;
// TODO: check for "as" following
// imported->imp_name = vim_strsave(as_name);
imported->imp_name = name;
((char_u **)names.ga_data)[i] = NULL;
if (as_name == NULL)
{
imported->imp_name = name;
((char_u **)names.ga_data)[i] = NULL;
}
else
{
// "import This as That ..."
imported->imp_name = as_name;
((char_u **)as_names.ga_data)[i] = NULL;
}
imported->imp_sid = sid;
if (idx >= 0)
{
@@ -592,7 +592,7 @@ handle_import(
}
erret:
ga_clear_strings(&names);
vim_free(as_name);
ga_clear_strings(&as_names);
return cmd_end;
}