vim-patch:9.0.1231: completion of :runtime does not handle {where} argument

Problem:    Completion of :runtime does not handle {where} argument.
Solution:   Parse the {where} argument. (closes vim/vim#11863)

3770f4c9cd
This commit is contained in:
zeertzjq 2023-01-26 09:58:27 +08:00
parent 6644786db0
commit 6320c91c50
8 changed files with 183 additions and 127 deletions

View File

@ -2991,6 +2991,7 @@ getcompletion({pat}, {type} [, {filtered}]) *getcompletion()*
messages |:messages| suboptions messages |:messages| suboptions
option options option options
packadd optional package |pack-add| names packadd optional package |pack-add| names
runtime runtime file names |:runtime|
scriptnames sourced script names |:scriptnames| scriptnames sourced script names |:scriptnames|
shellcmd Shell command shellcmd Shell command
sign |:sign| suboptions sign |:sign| suboptions

View File

@ -2075,8 +2075,7 @@ static const char *set_context_by_cmdname(const char *cmd, cmdidx_T cmdidx, expa
break; break;
case CMD_runtime: case CMD_runtime:
xp->xp_context = EXPAND_RUNTIME; set_context_in_runtime_cmd(xp, arg);
xp->xp_pattern = (char *)arg;
break; break;
case CMD_compiler: case CMD_compiler:
@ -2720,9 +2719,7 @@ static int ExpandFromContext(expand_T *xp, char *pat, char ***matches, int *numM
return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches, directories); return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches, directories);
} }
if (xp->xp_context == EXPAND_RUNTIME) { if (xp->xp_context == EXPAND_RUNTIME) {
char *directories[] = { "", NULL }; return expand_runtime_cmd(pat, numMatches, matches);
return ExpandRTDir(pat, DIP_START + DIP_OPT + DIP_PRNEXT, numMatches,
matches, directories);
} }
if (xp->xp_context == EXPAND_COMPILER) { if (xp->xp_context == EXPAND_COMPILER) {
char *directories[] = { "compiler", NULL }; char *directories[] = { "compiler", NULL };
@ -3213,11 +3210,12 @@ static int ExpandUserLua(expand_T *xp, int *num_file, char ***file)
/// Expand `file` for all comma-separated directories in `path`. /// Expand `file` for all comma-separated directories in `path`.
/// Adds matches to `ga`. /// Adds matches to `ga`.
void globpath(char *path, char *file, garray_T *ga, int expand_options) /// If "dirs" is true only expand directory names.
void globpath(char *path, char *file, garray_T *ga, int expand_options, bool dirs)
{ {
expand_T xpc; expand_T xpc;
ExpandInit(&xpc); ExpandInit(&xpc);
xpc.xp_context = EXPAND_FILES; xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
char *buf = xmalloc(MAXPATHL); char *buf = xmalloc(MAXPATHL);
@ -3536,11 +3534,14 @@ void f_getcompletion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
set_context_in_menu_cmd(&xpc, "menu", xpc.xp_pattern, false); set_context_in_menu_cmd(&xpc, "menu", xpc.xp_pattern, false);
xpc.xp_pattern_len = strlen(xpc.xp_pattern); xpc.xp_pattern_len = strlen(xpc.xp_pattern);
} }
if (xpc.xp_context == EXPAND_SIGN) { if (xpc.xp_context == EXPAND_SIGN) {
set_context_in_sign_cmd(&xpc, xpc.xp_pattern); set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
xpc.xp_pattern_len = strlen(xpc.xp_pattern); xpc.xp_pattern_len = strlen(xpc.xp_pattern);
} }
if (xpc.xp_context == EXPAND_RUNTIME) {
set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
xpc.xp_pattern_len = strlen(xpc.xp_pattern);
}
theend: theend:
if (cmdline_fuzzy_completion_supported(&xpc)) { if (cmdline_fuzzy_completion_supported(&xpc)) {

View File

@ -2983,7 +2983,7 @@ static void f_globpath(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
if (file != NULL && !error) { if (file != NULL && !error) {
garray_T ga; garray_T ga;
ga_init(&ga, (int)sizeof(char *), 10); ga_init(&ga, (int)sizeof(char *), 10);
globpath((char *)tv_get_string(&argvars[0]), (char *)file, &ga, flags); globpath((char *)tv_get_string(&argvars[0]), (char *)file, &ga, flags, false);
if (rettv->v_type == VAR_STRING) { if (rettv->v_type == VAR_STRING) {
rettv->vval.v_string = ga_concat_strings_sep(&ga, "\n"); rettv->vval.v_string = ga_concat_strings_sep(&ga, "\n");

View File

@ -1138,7 +1138,7 @@ static int expand_in_path(garray_T *const gap, char *const pattern, const int fl
if (flags & EW_ADDSLASH) { if (flags & EW_ADDSLASH) {
glob_flags |= WILD_ADD_SLASH; glob_flags |= WILD_ADD_SLASH;
} }
globpath(paths, pattern, gap, glob_flags); globpath(paths, pattern, gap, glob_flags, false);
xfree(paths); xfree(paths);
return gap->ga_len; return gap->ga_len;

View File

@ -208,31 +208,65 @@ void runtime_init(void)
uv_mutex_init(&runtime_search_path_mutex); uv_mutex_init(&runtime_search_path_mutex);
} }
/// Get DIP_ flags from the [what] argument of a :runtime command.
/// "*argp" is advanced to after the [what] argument.
static int get_runtime_cmd_flags(char **argp)
{
char *arg = *argp;
char *p = skiptowhite(arg);
size_t what_len = (size_t)(p - arg);
if (what_len == 0) {
return 0;
}
if (strncmp(arg, "START", what_len) == 0) {
*argp = skipwhite(arg + what_len);
return DIP_START + DIP_NORTP;
}
if (strncmp(arg, "OPT", what_len) == 0) {
*argp = skipwhite(arg + what_len);
return DIP_OPT + DIP_NORTP;
}
if (strncmp(arg, "PACK", what_len) == 0) {
*argp = skipwhite(arg + what_len);
return DIP_START + DIP_OPT + DIP_NORTP;
}
if (strncmp(arg, "ALL", what_len) == 0) {
*argp = skipwhite(arg + what_len);
return DIP_START + DIP_OPT;
}
return 0;
}
/// ":runtime [what] {name}" /// ":runtime [what] {name}"
void ex_runtime(exarg_T *eap) void ex_runtime(exarg_T *eap)
{ {
char *arg = eap->arg; char *arg = eap->arg;
char *p = skiptowhite(arg);
size_t len = (size_t)(p - arg);
int flags = eap->forceit ? DIP_ALL : 0; int flags = eap->forceit ? DIP_ALL : 0;
if (strncmp(arg, "START", len) == 0) { flags += get_runtime_cmd_flags(&arg);
flags += DIP_START + DIP_NORTP;
arg = skipwhite(arg + len);
} else if (strncmp(arg, "OPT", len) == 0) {
flags += DIP_OPT + DIP_NORTP;
arg = skipwhite(arg + len);
} else if (strncmp(arg, "PACK", len) == 0) {
flags += DIP_START + DIP_OPT + DIP_NORTP;
arg = skipwhite(arg + len);
} else if (strncmp(arg, "ALL", len) == 0) {
flags += DIP_START + DIP_OPT;
arg = skipwhite(arg + len);
}
source_runtime(arg, flags); source_runtime(arg, flags);
} }
static int runtime_expand_flags;
/// Set the completion context for the :runtime command.
void set_context_in_runtime_cmd(expand_T *xp, const char *arg)
{
runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags((char **)&arg);
xp->xp_context = EXPAND_RUNTIME;
xp->xp_pattern = (char *)arg;
}
/// Handle command line completion for :runtime command.
int expand_runtime_cmd(char *pat, int *numMatches, char ***matches)
{
char *directories[] = {"", NULL};
return ExpandRTDir(pat, runtime_expand_flags, numMatches, matches, directories);
}
static void source_callback(char *fname, void *cookie) static void source_callback(char *fname, void *cookie)
{ {
(void)do_source(fname, false, DOSO_NONE); (void)do_source(fname, false, DOSO_NONE);
@ -1194,57 +1228,53 @@ int ExpandRTDir(char *pat, int flags, int *num_file, char ***file, char *dirname
// TODO(bfredl): this is bullshit, expandpath should not reinvent path logic. // TODO(bfredl): this is bullshit, expandpath should not reinvent path logic.
for (int i = 0; dirnames[i] != NULL; i++) { for (int i = 0; dirnames[i] != NULL; i++) {
size_t size = strlen(dirnames[i]) + pat_len * 2 + 26; const size_t buf_len = strlen(dirnames[i]) + pat_len + 31;
char *buf = xmalloc(size); char *const buf = xmalloc(buf_len);
if (*dirnames[i] == NUL) { char *const tail = buf + 15;
// empty dir used for :runtime const size_t tail_buflen = buf_len - 15;
if (path_tail(pat) == pat) { int glob_flags = 0;
// no path separator, match dir names and script files bool expand_dirs = false;
snprintf(buf, size, "\\(%s*.\\(vim\\|lua\\)\\)\\|\\(%s*\\)", pat, pat);
} else { if (*dirnames[i] == NUL) { // empty dir used for :runtime
// has path separator, match script files snprintf(tail, tail_buflen, "%s*.\\(vim\\|lua\\)", pat);
snprintf(buf, size, "%s*.vim", pat);
}
} else { } else {
snprintf(buf, size, "%s/%s*.\\(vim\\|lua\\)", dirnames[i], pat); snprintf(tail, tail_buflen, "%s/%s*.\\(vim\\|lua\\)", dirnames[i], pat);
} }
globpath(p_rtp, buf, &ga, 0);
expand:
if ((flags & DIP_NORTP) == 0) {
globpath(p_rtp, tail, &ga, glob_flags, expand_dirs);
}
if (flags & DIP_START) {
memcpy(tail - 15, "pack/*/start/*/", 15); // NOLINT
globpath(p_pp, tail - 15, &ga, glob_flags, expand_dirs);
memcpy(tail - 8, "start/*/", 8); // NOLINT
globpath(p_pp, tail - 8, &ga, glob_flags, expand_dirs);
}
if (flags & DIP_OPT) {
memcpy(tail - 13, "pack/*/opt/*/", 13); // NOLINT
globpath(p_pp, tail - 13, &ga, glob_flags, expand_dirs);
memcpy(tail - 6, "opt/*/", 6); // NOLINT
globpath(p_pp, tail - 6, &ga, glob_flags, expand_dirs);
}
if (*dirnames[i] == NUL && !expand_dirs) {
// expand dir names in another round
snprintf(tail, tail_buflen, "%s*", pat);
glob_flags = WILD_ADD_SLASH;
expand_dirs = true;
goto expand;
}
xfree(buf); xfree(buf);
} }
if (flags & DIP_START) { int pat_pathsep_cnt = 0;
for (int i = 0; dirnames[i] != NULL; i++) { for (size_t i = 0; i < pat_len; i++) {
size_t size = strlen(dirnames[i]) + pat_len + 31; if (vim_ispathsep(pat[i])) {
char *s = xmalloc(size); pat_pathsep_cnt++;
snprintf(s, size, "pack/*/start/*/%s/%s*.\\(vim\\|lua\\)", dirnames[i], pat); // NOLINT
globpath(p_pp, s, &ga, 0);
xfree(s);
}
for (int i = 0; dirnames[i] != NULL; i++) {
size_t size = strlen(dirnames[i]) + pat_len + 31;
char *s = xmalloc(size);
snprintf(s, size, "start/*/%s/%s*.\\(vim\\|lua\\)", dirnames[i], pat); // NOLINT
globpath(p_pp, s, &ga, 0);
xfree(s);
}
}
if (flags & DIP_OPT) {
for (int i = 0; dirnames[i] != NULL; i++) {
size_t size = strlen(dirnames[i]) + pat_len + 29;
char *s = xmalloc(size);
snprintf(s, size, "pack/*/opt/*/%s/%s*.\\(vim\\|lua\\)", dirnames[i], pat); // NOLINT
globpath(p_pp, s, &ga, 0);
xfree(s);
}
for (int i = 0; dirnames[i] != NULL; i++) {
size_t size = strlen(dirnames[i]) + pat_len + 29;
char *s = xmalloc(size);
snprintf(s, size, "opt/*/%s/%s*.\\(vim\\|lua\\)", dirnames[i], pat); // NOLINT
globpath(p_pp, s, &ga, 0);
xfree(s);
} }
} }
@ -1252,54 +1282,23 @@ int ExpandRTDir(char *pat, int flags, int *num_file, char ***file, char *dirname
char *match = ((char **)ga.ga_data)[i]; char *match = ((char **)ga.ga_data)[i];
char *s = match; char *s = match;
char *e = s + strlen(s); char *e = s + strlen(s);
char *res_start = s; if (e - s > 4 && (flags & DIP_KEEPEXT) == 0
if ((flags & DIP_PRNEXT) != 0) { && (STRNICMP(e - 4, ".vim", 4) == 0
char *p = strstr(match, pat); || STRNICMP(e - 4, ".lua", 4) == 0)) {
if (p != NULL) { e -= 4;
// Drop what comes before "pat" in the match, so that for
// match "/long/path/syntax/cpp.vim" with pattern
// "syntax/cp" we only keep "syntax/cpp.vim".
res_start = p;
}
}
if (e - s > 4 && (STRNICMP(e - 4, ".vim", 4) == 0
|| STRNICMP(e - 4, ".lua", 4) == 0)) {
if (res_start == s) {
// Only keep the file name.
// Remove file ext only if flag DIP_PRNEXT is not present.
if ((flags & DIP_PRNEXT) == 0) {
e -= 4;
}
for (s = e; s > match; MB_PTR_BACK(match, s)) {
if (s < match) {
break;
}
if (vim_ispathsep(*s)) {
res_start = s + 1;
break;
}
}
}
*e = NUL; *e = NUL;
} }
if (res_start > match) { int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
assert((e - res_start) + 1 >= 0); for (s = e; s > match; MB_PTR_BACK(match, s)) {
memmove(match, res_start, (size_t)(e - res_start) + 1); if (vim_ispathsep(*s) && ++match_pathsep_cnt > pat_pathsep_cnt) {
} break;
// remove entries that look like backup files
if (e > s && e[-1] == '~') {
xfree(match);
char **fnames = (char **)ga.ga_data;
for (int j = i + 1; j < ga.ga_len; ++j) {
fnames[j - 1] = fnames[j];
} }
ga.ga_len--;
i--;
} }
s++;
*e = NUL;
assert((e - s) + 1 >= 0);
memmove(match, s, (size_t)(e - s) + 1);
} }
if (GA_EMPTY(&ga)) { if (GA_EMPTY(&ga)) {
@ -1329,9 +1328,9 @@ int ExpandPackAddDir(char *pat, int *num_file, char ***file)
size_t buflen = pat_len + 26; size_t buflen = pat_len + 26;
char *s = xmalloc(buflen); char *s = xmalloc(buflen);
snprintf(s, buflen, "pack/*/opt/%s*", pat); // NOLINT snprintf(s, buflen, "pack/*/opt/%s*", pat); // NOLINT
globpath(p_pp, s, &ga, 0); globpath(p_pp, s, &ga, 0, true);
snprintf(s, buflen, "opt/%s*", pat); // NOLINT snprintf(s, buflen, "opt/%s*", pat); // NOLINT
globpath(p_pp, s, &ga, 0); globpath(p_pp, s, &ga, 0, true);
xfree(s); xfree(s);
for (int i = 0; i < ga.ga_len; i++) { for (int i = 0; i < ga.ga_len; i++) {

View File

@ -105,7 +105,7 @@ typedef kvec_t(char *) CharVec;
#define DIP_NORTP 0x20 // do not use 'runtimepath' #define DIP_NORTP 0x20 // do not use 'runtimepath'
#define DIP_NOAFTER 0x40 // skip "after" directories #define DIP_NOAFTER 0x40 // skip "after" directories
#define DIP_AFTER 0x80 // only use "after" directories #define DIP_AFTER 0x80 // only use "after" directories
#define DIP_PRNEXT 0x100 // for print also file extension #define DIP_KEEPEXT 0x100 // for completion: include file extension
#define DIP_DIRFILE 0x200 // find both files and directories #define DIP_DIRFILE 0x200 // find both files and directories
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@ -542,15 +542,6 @@ func Test_getcompletion()
call assert_true(index(l, '<buffer>') >= 0) call assert_true(index(l, '<buffer>') >= 0)
let l = getcompletion('not', 'mapclear') let l = getcompletion('not', 'mapclear')
call assert_equal([], l) call assert_equal([], l)
let l = getcompletion('', 'runtime')
call assert_true(index(l, 'defaults.vim') >= 0)
let l = getcompletion('synt', 'runtime')
call assert_true(index(l, 'syntax') >= 0)
let l = getcompletion('syntax/vi', 'runtime')
call assert_true(index(l, 'syntax/vim.vim') >= 0)
let l = getcompletion('notexitsts', 'runtime')
call assert_equal([], l)
let l = getcompletion('.', 'shellcmd') let l = getcompletion('.', 'shellcmd')
call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"')) call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))

View File

@ -193,8 +193,10 @@ func Test_packadd_completion()
call mkdir(optdir1 . '/pluginA', 'p') call mkdir(optdir1 . '/pluginA', 'p')
call mkdir(optdir1 . '/pluginC', 'p') call mkdir(optdir1 . '/pluginC', 'p')
call writefile([], optdir1 . '/unrelated')
call mkdir(optdir2 . '/pluginB', 'p') call mkdir(optdir2 . '/pluginB', 'p')
call mkdir(optdir2 . '/pluginC', 'p') call mkdir(optdir2 . '/pluginC', 'p')
call writefile([], optdir2 . '/unrelated')
let li = [] let li = []
call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't') call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't')
@ -358,4 +360,66 @@ func Test_runtime()
call assert_equal('runstartopt', g:sequence) call assert_equal('runstartopt', g:sequence)
endfunc endfunc
func Test_runtime_completion()
let rundir = &packpath . '/runtime/Xextra'
let startdir = &packpath . '/pack/mine/start/foo/Xextra'
let optdir = &packpath . '/pack/mine/opt/bar/Xextra'
call mkdir(rundir . '/Xrunbaz', 'p')
call mkdir(startdir . '/Xstartbaz', 'p')
call mkdir(optdir . '/Xoptbaz', 'p')
call writefile([], rundir . '/../Xrunfoo.vim')
call writefile([], rundir . '/Xrunbar.vim')
call writefile([], rundir . '/Xunrelated')
call writefile([], rundir . '/../Xunrelated')
call writefile([], startdir . '/../Xstartfoo.vim')
call writefile([], startdir . '/Xstartbar.vim')
call writefile([], startdir . '/Xunrelated')
call writefile([], startdir . '/../Xunrelated')
call writefile([], optdir . '/../Xoptfoo.vim')
call writefile([], optdir . '/Xoptbar.vim')
call writefile([], optdir . '/Xunrelated')
call writefile([], optdir . '/../Xunrelated')
exe 'set rtp=' . &packpath . '/runtime'
func Check_runtime_completion(arg, arg1, res)
call feedkeys(':runtime ' .. a:arg .. "\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"runtime ' .. a:arg1 .. join(a:res), @:)
call assert_equal(a:res, getcompletion(a:arg, 'runtime'))
call feedkeys(':runtime ' .. a:arg .. "X\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"runtime ' .. a:arg1 .. join(a:res), @:)
call assert_equal(a:res, getcompletion(a:arg .. 'X', 'runtime'))
endfunc
call Check_runtime_completion('', '',
\ ['Xextra/', 'Xrunfoo.vim'])
call Check_runtime_completion('Xextra/', '',
\ ['Xextra/Xrunbar.vim', 'Xextra/Xrunbaz/'])
call Check_runtime_completion('START ', 'START ',
\ ['Xextra/', 'Xstartfoo.vim'])
call Check_runtime_completion('START Xextra/', 'START ',
\ ['Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/'])
call Check_runtime_completion('OPT ', 'OPT ',
\ ['Xextra/', 'Xoptfoo.vim'])
call Check_runtime_completion('OPT Xextra/', 'OPT ',
\ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/'])
call Check_runtime_completion('PACK ', 'PACK ',
\ ['Xextra/', 'Xoptfoo.vim', 'Xstartfoo.vim'])
call Check_runtime_completion('PACK Xextra/', 'PACK ',
\ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/',
\ 'Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/'])
call Check_runtime_completion('ALL ', 'ALL ',
\ ['Xextra/', 'Xoptfoo.vim', 'Xrunfoo.vim', 'Xstartfoo.vim'])
call Check_runtime_completion('ALL Xextra/', 'ALL ',
\ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/',
\ 'Xextra/Xrunbar.vim', 'Xextra/Xrunbaz/',
\ 'Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/'])
delfunc Check_runtime_completion
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab