To: vim_dev@googlegroups.com Subject: Patch 8.2.2556 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.2556 Problem: Vim9: :import with "as" not fully supported. Solution: Implement "as" for more cases. Files: src/vim9script.c, src/testdir/test_vim9_script.vim *** ../vim-8.2.2555/src/vim9script.c 2021-02-20 08:16:33.823363221 +0100 --- src/vim9script.c 2021-02-27 22:27:55.536933676 +0100 *************** *** 325,406 **** { 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; garray_T names; ga_init2(&names, sizeof(char_u *), 10); if (*arg == '{') { // "import {item, item} from ..." 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; 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)); goto erret; ! } if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2])) { char_u *p; --- 325,367 ---- { char_u *arg = arg_start; char_u *cmd_end = 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; + 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 (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,426 **** --- 382,416 ---- 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,508 **** if (*arg_start == '*') { ! imported_T *imported; imported = find_imported(as_name, STRLEN(as_name), cctx); if (imported != NULL && imported->imp_sid == sid) { --- 491,500 ---- if (*arg_start == '*') { ! 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,527 **** if (imported == NULL) goto erret; imported->imp_name = as_name; ! as_name = NULL; imported->imp_sid = sid; imported->imp_flags = IMP_FLAGS_STAR; } --- 513,519 ---- if (imported == NULL) goto erret; imported->imp_name = as_name; ! ((char_u **)as_names.ga_data)[0] = NULL; imported->imp_sid = sid; imported->imp_flags = IMP_FLAGS_STAR; } *************** *** 535,540 **** --- 527,533 ---- 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,581 **** 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; imported->imp_sid = sid; if (idx >= 0) { --- 565,581 ---- if (imported == NULL) goto erret; ! 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,598 **** } erret: ga_clear_strings(&names); ! vim_free(as_name); return cmd_end; } --- 592,598 ---- } erret: ga_clear_strings(&names); ! ga_clear_strings(&as_names); return cmd_end; } *** ../vim-8.2.2555/src/testdir/test_vim9_script.vim 2021-02-27 22:36:38.946849192 +0100 --- src/testdir/test_vim9_script.vim 2021-02-27 22:31:16.748136563 +0100 *************** *** 1218,1223 **** --- 1218,1253 ---- 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'\" *** ../vim-8.2.2555/src/version.c 2021-02-27 22:36:38.946849192 +0100 --- src/version.c 2021-02-27 22:39:41.150117421 +0100 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 2556, /**/ -- bashian roulette: $ ((RANDOM%6)) || rm -rf ~ /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///