]> granicus.if.org Git - vim/commitdiff
patch 8.2.2556: Vim9: :import with "as" not fully supported v8.2.2556
authorBram Moolenaar <Bram@vim.org>
Sat, 27 Feb 2021 21:41:19 +0000 (22:41 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 27 Feb 2021 21:41:19 +0000 (22:41 +0100)
Problem:    Vim9: :import with "as" not fully supported.
Solution:   Implement "as" for more cases.

src/testdir/test_vim9_script.vim
src/version.c
src/vim9script.c

index 8c9d3c1a166c588ce68c02372a53bed5790b8616..629d0b8363ed0e1c432f85988135cc271a1e6158 100644 (file)
@@ -1218,6 +1218,36 @@ def Test_vim9_import_export()
   delete('Xvim9_script')
 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()
   source Ximport.vim
   return "echo 'yes'\<CR>"
index 1dfd0d6a8f8c34e621e0246d4a29444b852e997a..d6065b7e6931a019b1d52f83fb3c1e6261f174e6 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2556,
 /**/
     2555,
 /**/
index 0994e513fc6cdc41cbc19ba15ce24ddb5b953c06..2a829f4981cfed902a26c0df819c74e507ac6a25 100644 (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
+
+    for (;;)
     {
-       // "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;
+       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;
 }