Skip to content

Commit 0a84284

Browse files
committed
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.
1 parent 3f1e9f0 commit 0a84284

3 files changed

Lines changed: 97 additions & 65 deletions

File tree

src/testdir/test_vim9_script.vim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,36 @@ def Test_vim9_import_export()
12181218
delete('Xvim9_script')
12191219
enddef
12201220

1221+
def Test_import_as()
1222+
var export_lines =<< trim END
1223+
vim9script
1224+
export var one = 1
1225+
export var yes = 'yes'
1226+
END
1227+
writefile(export_lines, 'XexportAs')
1228+
1229+
var import_lines =<< trim END
1230+
vim9script
1231+
import one as thatOne from './XexportAs'
1232+
assert_equal(1, thatOne)
1233+
import yes as yesYes from './XexportAs'
1234+
assert_equal('yes', yesYes)
1235+
END
1236+
CheckScriptSuccess(import_lines)
1237+
1238+
import_lines =<< trim END
1239+
vim9script
1240+
import {one as thatOne, yes as yesYes} from './XexportAs'
1241+
assert_equal(1, thatOne)
1242+
assert_equal('yes', yesYes)
1243+
assert_fails('echo one', 'E121:')
1244+
assert_fails('echo yes', 'E121:')
1245+
END
1246+
CheckScriptSuccess(import_lines)
1247+
1248+
delete('XexportAs')
1249+
enddef
1250+
12211251
func g:Trigger()
12221252
source Ximport.vim
12231253
return "echo 'yes'\<CR>"

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+
2556,
753755
/**/
754756
2555,
755757
/**/

src/vim9script.c

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -325,82 +325,43 @@ handle_import(
325325
{
326326
char_u *arg = arg_start;
327327
char_u *cmd_end = NULL;
328-
char_u *as_name = NULL;
329328
int ret = FAIL;
330329
typval_T tv;
331330
int sid = -1;
332331
int res;
332+
int mult = FALSE;
333333
garray_T names;
334+
garray_T as_names;
334335

335336
ga_init2(&names, sizeof(char_u *), 10);
337+
ga_init2(&as_names, sizeof(char_u *), 10);
336338
if (*arg == '{')
337339
{
338340
// "import {item, item} from ..."
341+
mult = TRUE;
339342
arg = skipwhite_and_linebreak(arg + 1, evalarg);
340-
for (;;)
341-
{
342-
char_u *p = arg;
343-
int had_comma = FALSE;
344-
345-
while (eval_isnamec(*arg))
346-
++arg;
347-
if (p == arg)
348-
break;
349-
if (ga_grow(&names, 1) == FAIL)
350-
goto erret;
351-
((char_u **)names.ga_data)[names.ga_len] =
352-
vim_strnsave(p, arg - p);
353-
++names.ga_len;
354-
if (*arg == ',')
355-
{
356-
had_comma = TRUE;
357-
++arg;
358-
}
359-
arg = skipwhite_and_linebreak(arg, evalarg);
360-
if (*arg == '}')
361-
{
362-
arg = skipwhite_and_linebreak(arg + 1, evalarg);
363-
break;
364-
}
365-
if (!had_comma)
366-
{
367-
emsg(_(e_missing_comma_in_import));
368-
goto erret;
369-
}
370-
}
371-
if (names.ga_len == 0)
372-
{
373-
emsg(_(e_syntax_error_in_import));
374-
goto erret;
375-
}
376343
}
377-
else
344+
345+
for (;;)
378346
{
379-
// "import Name from ..."
380-
// "import * as Name from ..."
381-
// "import item [as Name] from ..."
382-
arg = skipwhite_and_linebreak(arg, evalarg);
383-
if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
384-
arg = skipwhite_and_linebreak(arg + 1, evalarg);
385-
else if (eval_isnamec1(*arg))
386-
{
387-
char_u *p = arg;
347+
char_u *p = arg;
348+
int had_comma = FALSE;
349+
char_u *as_name = NULL;
388350

351+
// accept "*" or "Name"
352+
if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
353+
++arg;
354+
else
389355
while (eval_isnamec(*arg))
390356
++arg;
391-
if (ga_grow(&names, 1) == FAIL)
392-
goto erret;
393-
((char_u **)names.ga_data)[names.ga_len] =
394-
vim_strnsave(p, arg - p);
395-
++names.ga_len;
396-
arg = skipwhite_and_linebreak(arg, evalarg);
397-
}
398-
else
399-
{
400-
emsg(_(e_syntax_error_in_import));
357+
if (p == arg)
358+
break;
359+
if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
401360
goto erret;
402-
}
361+
((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
362+
++names.ga_len;
403363

364+
arg = skipwhite_and_linebreak(arg, evalarg);
404365
if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
405366
{
406367
char_u *p;
@@ -421,6 +382,35 @@ handle_import(
421382
emsg(_(e_missing_as_after_star));
422383
goto erret;
423384
}
385+
// without "as Name" the as_names entry is NULL
386+
((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
387+
++as_names.ga_len;
388+
389+
if (!mult)
390+
break;
391+
if (*arg == ',')
392+
{
393+
had_comma = TRUE;
394+
++arg;
395+
}
396+
arg = skipwhite_and_linebreak(arg, evalarg);
397+
if (*arg == '}')
398+
{
399+
++arg;
400+
break;
401+
}
402+
if (!had_comma)
403+
{
404+
emsg(_(e_missing_comma_in_import));
405+
goto erret;
406+
}
407+
}
408+
arg = skipwhite_and_linebreak(arg, evalarg);
409+
410+
if (names.ga_len == 0)
411+
{
412+
emsg(_(e_syntax_error_in_import));
413+
goto erret;
424414
}
425415

426416
if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
@@ -501,8 +491,10 @@ handle_import(
501491

502492
if (*arg_start == '*')
503493
{
504-
imported_T *imported;
494+
imported_T *imported;
495+
char_u *as_name = ((char_u **)as_names.ga_data)[0];
505496

497+
// "import * as That"
506498
imported = find_imported(as_name, STRLEN(as_name), cctx);
507499
if (imported != NULL && imported->imp_sid == sid)
508500
{
@@ -521,7 +513,7 @@ handle_import(
521513
if (imported == NULL)
522514
goto erret;
523515
imported->imp_name = as_name;
524-
as_name = NULL;
516+
((char_u **)as_names.ga_data)[0] = NULL;
525517
imported->imp_sid = sid;
526518
imported->imp_flags = IMP_FLAGS_STAR;
527519
}
@@ -535,6 +527,7 @@ handle_import(
535527
for (i = 0; i < names.ga_len; ++i)
536528
{
537529
char_u *name = ((char_u **)names.ga_data)[i];
530+
char_u *as_name = ((char_u **)as_names.ga_data)[i];
538531
size_t len = STRLEN(name);
539532
int idx;
540533
imported_T *imported;
@@ -572,10 +565,17 @@ handle_import(
572565
if (imported == NULL)
573566
goto erret;
574567

575-
// TODO: check for "as" following
576-
// imported->imp_name = vim_strsave(as_name);
577-
imported->imp_name = name;
578-
((char_u **)names.ga_data)[i] = NULL;
568+
if (as_name == NULL)
569+
{
570+
imported->imp_name = name;
571+
((char_u **)names.ga_data)[i] = NULL;
572+
}
573+
else
574+
{
575+
// "import This as That ..."
576+
imported->imp_name = as_name;
577+
((char_u **)as_names.ga_data)[i] = NULL;
578+
}
579579
imported->imp_sid = sid;
580580
if (idx >= 0)
581581
{
@@ -592,7 +592,7 @@ handle_import(
592592
}
593593
erret:
594594
ga_clear_strings(&names);
595-
vim_free(as_name);
595+
ga_clear_strings(&as_names);
596596
return cmd_end;
597597
}
598598

0 commit comments

Comments
 (0)