vim-patch:9.0.0269: getscriptinfo() does not include the version

Problem:    getscriptinfo() does not include the version.  Cannot select
            entries by script name.
Solution:   Add the "version" item and the "name" argument. (Yegappan
            Lakshmanan, closes vim/vim#10962)

520f6ef60a

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq 2023-04-15 19:50:23 +08:00
parent eb151a9730
commit bcc971de15
4 changed files with 63 additions and 7 deletions

View File

@ -223,7 +223,7 @@ getreg([{regname} [, 1 [, {list}]]])
String or List contents of a register
getreginfo([{regname}]) Dict information about a register
getregtype([{regname}]) String type of a register
getscriptinfo() List list of sourced scripts
getscriptinfo([{opts}]) List list of sourced scripts
gettabinfo([{expr}]) List list of tab pages
gettabvar({nr}, {varname} [, {def}])
any variable {varname} in tab {nr} or {def}
@ -3576,7 +3576,7 @@ getregtype([{regname}]) *getregtype()*
Can also be used as a |method|: >
GetRegname()->getregtype()
getscriptinfo() *getscriptinfo()*
getscriptinfo([{opts}]) *getscriptinfo()*
Returns a |List| with information about all the sourced Vim
scripts in the order they were sourced.
@ -3585,6 +3585,13 @@ getscriptinfo() *getscriptinfo()*
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.
gettabinfo([{tabnr}]) *gettabinfo()*
If {tabnr} is not specified, then information about all the

View File

@ -189,7 +189,7 @@ return {
gettabinfo={args={0, 1}, base=1},
gettabvar={args={2, 3}, base=1},
gettabwinvar={args={3, 4}, base=1},
getscriptinfo={},
getscriptinfo={args={0, 1}},
gettagstack={args={0, 1}, base=1},
gettext={args=1, base=1},
getwininfo={args={0, 1}, base=1},

View File

@ -43,6 +43,7 @@
#include "nvim/os/stdpaths_defs.h"
#include "nvim/path.h"
#include "nvim/profile.h"
#include "nvim/regexp.h"
#include "nvim/runtime.h"
#include "nvim/strings.h"
#include "nvim/usercmd.h"
@ -2361,8 +2362,25 @@ void f_getscriptinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
tv_list_alloc_ret(rettv, script_items.ga_len);
if (tv_check_for_opt_dict_arg(argvars, 0) == FAIL) {
return;
}
list_T *l = rettv->vval.v_list;
regmatch_T regmatch = {
.regprog = NULL,
.rm_ic = p_ic,
};
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);
}
}
for (int i = 1; i <= script_items.ga_len; i++) {
scriptitem_T *si = SCRIPT_ITEM(i);
@ -2370,13 +2388,22 @@ 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)) {
continue;
}
dict_T *d = tv_dict_alloc();
tv_list_append_dict(l, d);
tv_dict_add_str(d, S_LEN("name"), si->sn_name);
tv_dict_add_nr(d, S_LEN("sid"), i);
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);
}
vim_regfree(regmatch.regprog);
xfree(pat);
}
/// Get one full line from a sourced file.

View File

@ -31,12 +31,34 @@ endfunc
" Test for the getscriptinfo() function
func Test_getscriptinfo()
call writefile(['let loaded_script_id = expand("<SID>")'], 'Xscript')
source Xscript
let lines =<< trim END
let g:loaded_script_id = expand("<SID>")
let s:XscriptVar = [1, #{v: 2}]
func s:XscriptFunc()
endfunc
END
call writefile(lines, 'X22script91')
source X22script91
let l = getscriptinfo()
call assert_match('Xscript$', l[-1].name)
call assert_match('X22script91$', l[-1].name)
call assert_equal(g:loaded_script_id, $"<SNR>{l[-1].sid}_")
call delete('Xscript')
let l = getscriptinfo({'name': '22script91'})
call assert_equal(1, len(l))
call assert_match('22script91$', l[0].name)
let l = getscriptinfo({'name': 'foobar'})
call assert_equal(0, len(l))
let l = getscriptinfo({'name': ''})
call assert_true(len(l) > 1)
call assert_fails("echo getscriptinfo({'name': []})", 'E730:')
call assert_fails("echo getscriptinfo({'name': '\\@'})", 'E866:')
let l = getscriptinfo({'name': v:_null_string})
call assert_true(len(l) > 1)
call assert_fails("echo getscriptinfo('foobar')", 'E1206:')
call delete('X22script91')
endfunc
" vim: shiftwidth=2 sts=2 expandtab