0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 8.2.1399: Vim9: may find imported item in wrong script

Problem:    Vim9: may find imported item in wrong script.
Solution:   When looking up script-local function use the embedded script ID.
            (issue #6644)
This commit is contained in:
Bram Moolenaar
2020-08-08 22:16:00 +02:00
parent daa2f36573
commit efa94447e8
5 changed files with 45 additions and 9 deletions

View File

@@ -12,6 +12,7 @@ char *vartype_name(vartype_T type);
char *type_name(type_T *type, char **tofree);
int get_script_item_idx(int sid, char_u *name, int check_writable);
imported_T *find_imported(char_u *name, size_t len, cctx_T *cctx);
imported_T *find_imported_in_script(char_u *name, size_t len, int sid);
int vim9_comment_start(char_u *p);
char_u *peek_next_line_from_context(cctx_T *cctx);
char_u *next_line_from_context(cctx_T *cctx, int skip_comment);

View File

@@ -1430,6 +1430,31 @@ def Test_import_in_filetype()
&rtp = save_rtp
enddef
def Test_use_import_in_mapping()
let lines =<< trim END
vim9script
export def Funcx()
g:result = 42
enddef
END
writefile(lines, 'XsomeExport.vim')
lines =<< trim END
vim9script
import Funcx from './XsomeExport.vim'
nnoremap <C-B> :call <sid>Funcx()<cr>
END
writefile(lines, 'Xmapscript.vim')
source Xmapscript.vim
feedkeys("\<c-b>", "xt")
assert_equal(42, g:result)
unlet g:result
delete('XsomeExport.vim')
delete('Xmapscript.vim')
nunmap <C-B>
enddef
def Test_vim9script_fails()
CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')

View File

@@ -791,6 +791,7 @@ find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
{
int vim9script = in_vim9script();
char_u *after_script = NULL;
long sid = 0;
if (vim9script)
{
@@ -800,18 +801,15 @@ find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
return func;
}
if (!vim9script
&& name[0] == K_SPECIAL
if (name[0] == K_SPECIAL
&& name[1] == KS_EXTRA
&& name[2] == KE_SNR)
{
long sid;
// Caller changes s: to <SNR>99_name.
after_script = name + 3;
sid = getdigits(&after_script);
if (sid == current_sctx.sc_sid && *after_script == '_')
if (*after_script == '_')
++after_script;
else
after_script = NULL;
@@ -819,8 +817,11 @@ find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
if (vim9script || after_script != NULL)
{
// Find imported function before global one.
imported = find_imported(
after_script == NULL ? name : after_script, 0, cctx);
if (after_script != NULL && sid != current_sctx.sc_sid)
imported = find_imported_in_script(after_script, 0, sid);
else
imported = find_imported(after_script == NULL
? name : after_script, 0, cctx);
if (imported != NULL && imported->imp_funcname != NULL)
{
hi = hash_find(&func_hashtab, imported->imp_funcname);

View File

@@ -754,6 +754,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1399,
/**/
1398,
/**/

View File

@@ -2548,12 +2548,10 @@ get_script_item_idx(int sid, char_u *name, int check_writable)
imported_T *
find_imported(char_u *name, size_t len, cctx_T *cctx)
{
scriptitem_T *si;
int idx;
if (current_sctx.sc_sid <= 0)
return NULL;
si = SCRIPT_ITEM(current_sctx.sc_sid);
if (cctx != NULL)
for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
{
@@ -2566,6 +2564,15 @@ find_imported(char_u *name, size_t len, cctx_T *cctx)
return import;
}
return find_imported_in_script(name, len, current_sctx.sc_sid);
}
imported_T *
find_imported_in_script(char_u *name, size_t len, int sid)
{
scriptitem_T *si = SCRIPT_ITEM(sid);
int idx;
for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;