forked from aniani/vim
patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Problem: Vim9: the file name of an :import cannot be an expression. Solution: Accept an expression that results in a string. Do not support :import in a function.
This commit is contained in:
@@ -1354,6 +1354,9 @@ script file to avoid confusion.
|
|||||||
`:import` can also be used in legacy Vim script. The imported items still
|
`:import` can also be used in legacy Vim script. The imported items still
|
||||||
become script-local, even when the "s:" prefix is not given.
|
become script-local, even when the "s:" prefix is not given.
|
||||||
|
|
||||||
|
`:import` can not be used in a function. Imported items are intended to exist
|
||||||
|
at the script level and only imported once.
|
||||||
|
|
||||||
The script name after `import` can be:
|
The script name after `import` can be:
|
||||||
- A relative path, starting "." or "..". This finds a file relative to the
|
- A relative path, starting "." or "..". This finds a file relative to the
|
||||||
location of the script file itself. This is useful to split up a large
|
location of the script file itself. This is useful to split up a large
|
||||||
@@ -1363,6 +1366,7 @@ The script name after `import` can be:
|
|||||||
- A path not being relative or absolute. This will be found in the
|
- A path not being relative or absolute. This will be found in the
|
||||||
"import" subdirectories of 'runtimepath' entries. The name will usually be
|
"import" subdirectories of 'runtimepath' entries. The name will usually be
|
||||||
longer and unique, to avoid loading the wrong file.
|
longer and unique, to avoid loading the wrong file.
|
||||||
|
Note that "after/import" is not used.
|
||||||
|
|
||||||
Once a vim9 script file has been imported, the result is cached and used the
|
Once a vim9 script file has been imported, the result is cached and used the
|
||||||
next time the same script is imported. It will not be read again.
|
next time the same script is imported. It will not be read again.
|
||||||
|
@@ -1085,7 +1085,9 @@ def Test_vim9_import_export()
|
|||||||
enddef
|
enddef
|
||||||
g:funcref_result = GetExported()
|
g:funcref_result = GetExported()
|
||||||
|
|
||||||
import {exp_name} from './Xexport.vim'
|
var dir = './'
|
||||||
|
var ext = ".vim"
|
||||||
|
import {exp_name} from dir .. 'Xexport' .. ext
|
||||||
g:imported_name = exp_name
|
g:imported_name = exp_name
|
||||||
exp_name ..= ' Doe'
|
exp_name ..= ' Doe'
|
||||||
g:imported_name_appended = exp_name
|
g:imported_name_appended = exp_name
|
||||||
@@ -1148,26 +1150,6 @@ def Test_vim9_import_export()
|
|||||||
unlet g:imported_func
|
unlet g:imported_func
|
||||||
delete('Ximport_lbr.vim')
|
delete('Ximport_lbr.vim')
|
||||||
|
|
||||||
# import inside :def function
|
|
||||||
var import_in_def_lines =<< trim END
|
|
||||||
vim9script
|
|
||||||
def ImportInDef()
|
|
||||||
import exported from './Xexport.vim'
|
|
||||||
g:imported = exported
|
|
||||||
exported += 7
|
|
||||||
g:imported_added = exported
|
|
||||||
enddef
|
|
||||||
ImportInDef()
|
|
||||||
END
|
|
||||||
writefile(import_in_def_lines, 'Ximport2.vim')
|
|
||||||
source Ximport2.vim
|
|
||||||
# TODO: this should be 9879
|
|
||||||
assert_equal(9876, g:imported)
|
|
||||||
assert_equal(9883, g:imported_added)
|
|
||||||
unlet g:imported
|
|
||||||
unlet g:imported_added
|
|
||||||
delete('Ximport2.vim')
|
|
||||||
|
|
||||||
var import_star_as_lines =<< trim END
|
var import_star_as_lines =<< trim END
|
||||||
vim9script
|
vim9script
|
||||||
import * as Export from './Xexport.vim'
|
import * as Export from './Xexport.vim'
|
||||||
@@ -1181,8 +1163,9 @@ def Test_vim9_import_export()
|
|||||||
END
|
END
|
||||||
writefile(import_star_as_lines, 'Ximport.vim')
|
writefile(import_star_as_lines, 'Ximport.vim')
|
||||||
source Ximport.vim
|
source Ximport.vim
|
||||||
assert_equal(9883, g:imported_def)
|
# FIXME: this should be 9881
|
||||||
assert_equal(9883, g:imported_script)
|
assert_equal(9876, g:imported_def)
|
||||||
|
assert_equal(9876, g:imported_script)
|
||||||
|
|
||||||
var import_star_as_lines_no_dot =<< trim END
|
var import_star_as_lines_no_dot =<< trim END
|
||||||
vim9script
|
vim9script
|
||||||
@@ -1257,7 +1240,7 @@ def Test_vim9_import_export()
|
|||||||
END
|
END
|
||||||
writefile(import_star_as_lbr_lines, 'Ximport.vim')
|
writefile(import_star_as_lbr_lines, 'Ximport.vim')
|
||||||
source Ximport.vim
|
source Ximport.vim
|
||||||
assert_equal(9883, g:imported)
|
assert_equal(9876, g:imported)
|
||||||
|
|
||||||
var import_star_lines =<< trim END
|
var import_star_lines =<< trim END
|
||||||
vim9script
|
vim9script
|
||||||
@@ -1345,7 +1328,7 @@ def Test_vim9_import_export()
|
|||||||
import name from Xexport.vim
|
import name from Xexport.vim
|
||||||
END
|
END
|
||||||
writefile(import_invalid_string_lines, 'Ximport.vim')
|
writefile(import_invalid_string_lines, 'Ximport.vim')
|
||||||
assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
|
assert_fails('source Ximport.vim', 'E121:', '', 2, 'Ximport.vim')
|
||||||
|
|
||||||
var import_wrong_name_lines =<< trim END
|
var import_wrong_name_lines =<< trim END
|
||||||
vim9script
|
vim9script
|
||||||
@@ -1659,22 +1642,6 @@ def Test_vim9script_reload_import()
|
|||||||
source Xreload.vim
|
source Xreload.vim
|
||||||
source Xreload.vim
|
source Xreload.vim
|
||||||
|
|
||||||
var testlines =<< trim END
|
|
||||||
vim9script
|
|
||||||
def TheFunc()
|
|
||||||
import GetValtwo from './Xreload.vim'
|
|
||||||
assert_equal(222, GetValtwo())
|
|
||||||
enddef
|
|
||||||
TheFunc()
|
|
||||||
END
|
|
||||||
writefile(testlines, 'Ximport.vim')
|
|
||||||
source Ximport.vim
|
|
||||||
|
|
||||||
# Test that when not using "morelines" GetValtwo() and valtwo are still
|
|
||||||
# defined, because import doesn't reload a script.
|
|
||||||
writefile(lines, 'Xreload.vim')
|
|
||||||
source Ximport.vim
|
|
||||||
|
|
||||||
# cannot declare a var twice
|
# cannot declare a var twice
|
||||||
lines =<< trim END
|
lines =<< trim END
|
||||||
vim9script
|
vim9script
|
||||||
|
@@ -755,6 +755,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 */
|
||||||
|
/**/
|
||||||
|
3178,
|
||||||
/**/
|
/**/
|
||||||
3177,
|
3177,
|
||||||
/**/
|
/**/
|
||||||
|
@@ -4335,7 +4335,6 @@ compile_subscript(
|
|||||||
semsg(_(e_missing_paren), *arg);
|
semsg(_(e_missing_paren), *arg);
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
// TODO: base value may not be the first argument
|
|
||||||
if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
|
if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
@@ -7293,15 +7292,6 @@ compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
|
|||||||
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
|
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Compile an :import command.
|
|
||||||
*/
|
|
||||||
static char_u *
|
|
||||||
compile_import(char_u *arg, cctx_T *cctx)
|
|
||||||
{
|
|
||||||
return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
|
* generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
|
||||||
*/
|
*/
|
||||||
@@ -9638,7 +9628,8 @@ compile_def_function(
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CMD_import:
|
case CMD_import:
|
||||||
line = compile_import(p, &cctx);
|
emsg(_(e_import_can_only_be_used_in_script));
|
||||||
|
line = NULL;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CMD_if:
|
case CMD_if:
|
||||||
|
@@ -412,6 +412,7 @@ handle_import(
|
|||||||
garray_T names;
|
garray_T names;
|
||||||
garray_T as_names;
|
garray_T as_names;
|
||||||
|
|
||||||
|
tv.v_type = VAR_UNKNOWN;
|
||||||
ga_init2(&names, sizeof(char_u *), 10);
|
ga_init2(&names, sizeof(char_u *), 10);
|
||||||
ga_init2(&as_names, sizeof(char_u *), 10);
|
ga_init2(&as_names, sizeof(char_u *), 10);
|
||||||
if (*arg == '{')
|
if (*arg == '{')
|
||||||
@@ -496,14 +497,14 @@ handle_import(
|
|||||||
goto erret;
|
goto erret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The name of the file can be an expression, which must evaluate to a
|
||||||
|
// string.
|
||||||
arg = skipwhite_and_linebreak(arg + 4, evalarg);
|
arg = skipwhite_and_linebreak(arg + 4, evalarg);
|
||||||
tv.v_type = VAR_UNKNOWN;
|
ret = eval0(arg, &tv, NULL, evalarg);
|
||||||
// TODO: should we accept any expression?
|
if (ret == FAIL)
|
||||||
if (*arg == '\'')
|
goto erret;
|
||||||
ret = eval_lit_string(&arg, &tv, TRUE);
|
if (tv.v_type != VAR_STRING
|
||||||
else if (*arg == '"')
|
|| tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
|
||||||
ret = eval_string(&arg, &tv, TRUE);
|
|
||||||
if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
|
|
||||||
{
|
{
|
||||||
emsg(_(e_invalid_string_after_from));
|
emsg(_(e_invalid_string_after_from));
|
||||||
goto erret;
|
goto erret;
|
||||||
@@ -524,10 +525,7 @@ handle_import(
|
|||||||
len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
|
len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
|
||||||
from_name = alloc((int)len);
|
from_name = alloc((int)len);
|
||||||
if (from_name == NULL)
|
if (from_name == NULL)
|
||||||
{
|
|
||||||
clear_tv(&tv);
|
|
||||||
goto erret;
|
goto erret;
|
||||||
}
|
|
||||||
vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
|
vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
|
||||||
add_pathsep(from_name);
|
add_pathsep(from_name);
|
||||||
STRCAT(from_name, tv.vval.v_string);
|
STRCAT(from_name, tv.vval.v_string);
|
||||||
@@ -550,7 +548,6 @@ handle_import(
|
|||||||
from_name = alloc((int)len);
|
from_name = alloc((int)len);
|
||||||
if (from_name == NULL)
|
if (from_name == NULL)
|
||||||
{
|
{
|
||||||
clear_tv(&tv);
|
|
||||||
goto erret;
|
goto erret;
|
||||||
}
|
}
|
||||||
vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
|
vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
|
||||||
@@ -561,10 +558,8 @@ handle_import(
|
|||||||
if (res == FAIL || sid <= 0)
|
if (res == FAIL || sid <= 0)
|
||||||
{
|
{
|
||||||
semsg(_(e_could_not_import_str), tv.vval.v_string);
|
semsg(_(e_could_not_import_str), tv.vval.v_string);
|
||||||
clear_tv(&tv);
|
|
||||||
goto erret;
|
goto erret;
|
||||||
}
|
}
|
||||||
clear_tv(&tv);
|
|
||||||
|
|
||||||
if (*arg_start == '*')
|
if (*arg_start == '*')
|
||||||
{
|
{
|
||||||
@@ -669,6 +664,7 @@ handle_import(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
erret:
|
erret:
|
||||||
|
clear_tv(&tv);
|
||||||
ga_clear_strings(&names);
|
ga_clear_strings(&names);
|
||||||
ga_clear_strings(&as_names);
|
ga_clear_strings(&as_names);
|
||||||
return cmd_end;
|
return cmd_end;
|
||||||
|
Reference in New Issue
Block a user