0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -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

@@ -1218,6 +1218,36 @@ def Test_vim9_import_export()
delete('Xvim9_script') delete('Xvim9_script')
enddef enddef
def Test_import_as()
var export_lines =<< trim END
vim9script
export var one = 1
export var yes = 'yes'
END
writefile(export_lines, 'XexportAs')
var import_lines =<< trim END
vim9script
import one as thatOne from './XexportAs'
assert_equal(1, thatOne)
import yes as yesYes from './XexportAs'
assert_equal('yes', yesYes)
END
CheckScriptSuccess(import_lines)
import_lines =<< trim END
vim9script
import {one as thatOne, yes as yesYes} from './XexportAs'
assert_equal(1, thatOne)
assert_equal('yes', yesYes)
assert_fails('echo one', 'E121:')
assert_fails('echo yes', 'E121:')
END
CheckScriptSuccess(import_lines)
delete('XexportAs')
enddef
func g:Trigger() func g:Trigger()
source Ximport.vim source Ximport.vim
return "echo 'yes'\<CR>" return "echo 'yes'\<CR>"

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 */
/**/
2556,
/**/ /**/
2555, 2555,
/**/ /**/

View File

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