vim-patch:9.0.0303: it is not easy to get information about a script

Problem:    It is not easy to get information about a script.
Solution:   Make getscriptinf() return the version.  When selecting a specific
            script return functions and variables. (Yegappan Lakshmanan,
            closes vim/vim#10991)

2f892d8663

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq 2023-04-15 19:57:55 +08:00
parent bcc971de15
commit e9b4f51051
3 changed files with 114 additions and 18 deletions

View File

@ -3578,21 +3578,40 @@ getregtype([{regname}]) *getregtype()*
getscriptinfo([{opts}]) *getscriptinfo()*
Returns a |List| with information about all the sourced Vim
scripts in the order they were sourced.
scripts in the order they were sourced, like what
`:scriptnames` shows.
The optional Dict argument {opts} supports the following
optional items:
name Script name match pattern. If specified,
and "sid" is not specified, information about
scripts with name that match the pattern
"name" are returned.
sid Script ID |<SID>|. If specified, only
information about the script with ID "sid" is
returned and "name" is ignored.
Each item in the returned List is a |Dict| with the following
items:
autoload always set to FALSE.
name vim script file name.
sid script ID |<SID>|.
version vimscript version, always 1
The optional Dict argument {opts} supports the following
items:
name script name match pattern. If specified,
information about scripts with name
that match the pattern "name" are returned.
autoload Always set to FALSE.
functions List of script-local function names defined in
the script. Present only when a particular
script is specified using the "sid" item in
{opts}.
name Vim script file name.
sid Script ID |<SID>|.
variables A dictionary with the script-local variables.
Present only when the a particular script is
specified using the "sid" item in {opts}.
Note that this is a copy, the value of
script-local variables cannot be changed using
this dictionary.
version Vimscript version, always 1
Examples: >
:echo getscriptinfo({'name': 'myscript'})
:echo getscriptinfo({'sid': 15}).variables
<
gettabinfo([{tabnr}]) *gettabinfo()*
If {tabnr} is not specified, then information about all the
tab pages is returned as a |List|. Each List item is a

View File

@ -22,6 +22,7 @@
#include "nvim/cmdexpand.h"
#include "nvim/debugger.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/userfunc.h"
#include "nvim/ex_cmds_defs.h"
#include "nvim/ex_docmd.h"
@ -2357,6 +2358,26 @@ linenr_T get_sourced_lnum(LineGetter fgetline, void *cookie)
: SOURCING_LNUM;
}
/// Return a List of script-local functions defined in the script with id "sid".
static list_T *get_script_local_funcs(scid_T sid)
{
hashtab_T *const functbl = func_tbl_get();
list_T *l = tv_list_alloc((ptrdiff_t)functbl->ht_used);
// Iterate through all the functions in the global function hash table
// looking for functions with script ID "sid".
HASHTAB_ITER(functbl, hi, {
const ufunc_T *const fp = HI2UF(hi);
// Add active functions with script id == "sid"
if (!(fp->uf_flags & FC_DEAD) && (fp->uf_script_ctx.sc_sid == sid)) {
const char *const name = fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
tv_list_append_string(l, name, -1);
}
});
return l;
}
/// "getscriptinfo()" function
void f_getscriptinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
@ -2372,12 +2393,20 @@ void f_getscriptinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
.regprog = NULL,
.rm_ic = p_ic,
};
bool filterpat = false;
varnumber_T sid = -1;
char *pat = NULL;
if (argvars[0].v_type == VAR_DICT) {
pat = tv_dict_get_string(argvars[0].vval.v_dict, "name", true);
if (pat != NULL) {
regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
sid = tv_dict_get_number_def(argvars[0].vval.v_dict, "sid", -1);
if (sid == -1) {
pat = tv_dict_get_string(argvars[0].vval.v_dict, "name", true);
if (pat != NULL) {
regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
}
if (regmatch.regprog != NULL) {
filterpat = true;
}
}
}
@ -2388,8 +2417,11 @@ void f_getscriptinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
continue;
}
if (pat != NULL && regmatch.regprog != NULL
&& !vim_regexec(&regmatch, si->sn_name, (colnr_T)0)) {
if (filterpat && !vim_regexec(&regmatch, si->sn_name, (colnr_T)0)) {
continue;
}
if (sid != -1 && sid != i) {
continue;
}
@ -2400,6 +2432,15 @@ void f_getscriptinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
tv_dict_add_nr(d, S_LEN("version"), 1);
// Vim9 autoload script (:h vim9-autoload), not applicable to Nvim.
tv_dict_add_bool(d, S_LEN("autoload"), false);
// When a filter pattern is specified to return information about only
// specific script(s), also add the script-local variables and
// functions.
if (sid != -1) {
dict_T *var_dict = tv_dict_copy(NULL, &si->sn_vars->sv_dict, true, get_copyID());
tv_dict_add_dict(d, S_LEN("variables"), var_dict);
tv_dict_add_list(d, S_LEN("functions"), get_script_local_funcs((scid_T)sid));
}
}
vim_regfree(regmatch.regprog);

View File

@ -32,20 +32,54 @@ endfunc
" Test for the getscriptinfo() function
func Test_getscriptinfo()
let lines =<< trim END
" scriptversion 3
let g:loaded_script_id = expand("<SID>")
let s:XscriptVar = [1, #{v: 2}]
func s:XscriptFunc()
func s:XgetScriptVar()
return s:XscriptVar
endfunc
func s:Xscript_legacy_func1()
endfunc
" def s:Xscript_def_func1()
" enddef
func Xscript_legacy_func2()
endfunc
" def Xscript_def_func2()
" enddef
END
call writefile(lines, 'X22script91')
source X22script91
let l = getscriptinfo()
call assert_match('X22script91$', l[-1].name)
call assert_equal(g:loaded_script_id, $"<SNR>{l[-1].sid}_")
" call assert_equal(3, l[-1].version)
call assert_equal(1, l[-1].version)
call assert_equal(0, has_key(l[-1], 'variables'))
call assert_equal(0, has_key(l[-1], 'functions'))
let l = getscriptinfo({'name': '22script91'})
" Get script information using script name
let l = getscriptinfo(#{name: '22script91'})
call assert_equal(1, len(l))
call assert_match('22script91$', l[0].name)
let sid = l[0].sid
" Get script information using script-ID
let l = getscriptinfo({'sid': sid})
call assert_equal(#{XscriptVar: [1, {'v': 2}]}, l[0].variables)
let funcs = ['Xscript_legacy_func2',
\ $"<SNR>{sid}_Xscript_legacy_func1",
"\ $"<SNR>{sid}_Xscript_def_func1",
"\ 'Xscript_def_func2',
\ $"<SNR>{sid}_XgetScriptVar"]
for f in funcs
call assert_true(index(l[0].functions, f) != -1)
endfor
" Verify that a script-local variable cannot be modified using the dict
" returned by getscriptinfo()
let l[0].variables.XscriptVar = ['n']
let funcname = $"<SNR>{sid}_XgetScriptVar"
call assert_equal([1, {'v': 2}], call(funcname, []))
let l = getscriptinfo({'name': 'foobar'})
call assert_equal(0, len(l))
@ -58,6 +92,8 @@ func Test_getscriptinfo()
call assert_true(len(l) > 1)
call assert_fails("echo getscriptinfo('foobar')", 'E1206:')
call assert_fails("echo getscriptinfo({'sid': []})", 'E745:')
call delete('X22script91')
endfunc