0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

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 #11863)
This commit is contained in:
zeertzjq 2023-01-22 18:38:51 +00:00 committed by Bram Moolenaar
parent f3da4c8427
commit 3770f4c9cd
11 changed files with 200 additions and 136 deletions

View File

@ -3528,6 +3528,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

@ -2315,8 +2315,7 @@ set_context_by_cmdname(
break; break;
case CMD_runtime: case CMD_runtime:
xp->xp_context = EXPAND_RUNTIME; set_context_in_runtime_cmd(xp, arg);
xp->xp_pattern = arg;
break; break;
case CMD_compiler: case CMD_compiler:
@ -3028,9 +3027,7 @@ ExpandFromContext(
} }
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)
{ {
@ -3612,13 +3609,15 @@ ExpandUserList(
/* /*
* Expand "file" for all comma-separated directories in "path". * Expand "file" for all comma-separated directories in "path".
* Adds the matches to "ga". Caller must init "ga". * Adds the matches to "ga". Caller must init "ga".
* If "dirs" is TRUE only expand directory names.
*/ */
void void
globpath( globpath(
char_u *path, char_u *path,
char_u *file, char_u *file,
garray_T *ga, garray_T *ga,
int expand_options) int expand_options,
int dirs)
{ {
expand_T xpc; expand_T xpc;
char_u *buf; char_u *buf;
@ -3631,7 +3630,7 @@ globpath(
return; return;
ExpandInit(&xpc); ExpandInit(&xpc);
xpc.xp_context = EXPAND_FILES; xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
// Loop over all entries in {path}. // Loop over all entries in {path}.
while (*path != NUL) while (*path != NUL)
@ -4038,6 +4037,11 @@ f_getcompletion(typval_T *argvars, typval_T *rettv)
xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
} }
# endif # endif
if (xpc.xp_context == EXPAND_RUNTIME)
{
set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
}
} }
if (cmdline_fuzzy_completion_supported(&xpc)) if (cmdline_fuzzy_completion_supported(&xpc))

View File

@ -1388,7 +1388,7 @@ f_globpath(typval_T *argvars, typval_T *rettv)
if (file != NULL && !error) if (file != NULL && !error)
{ {
ga_init2(&ga, sizeof(char_u *), 10); ga_init2(&ga, sizeof(char_u *), 10);
globpath(tv_get_string(&argvars[0]), file, &ga, flags); globpath(tv_get_string(&argvars[0]), file, &ga, flags, FALSE);
if (rettv->v_type == VAR_STRING) if (rettv->v_type == VAR_STRING)
rettv->vval.v_string = ga_concat_strings(&ga, "\n"); rettv->vval.v_string = ga_concat_strings(&ga, "\n");
else if (rettv_list_alloc(rettv) == OK) else if (rettv_list_alloc(rettv) == OK)

View File

@ -2533,7 +2533,7 @@ expand_in_path(
glob_flags |= WILD_ICASE; glob_flags |= WILD_ICASE;
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);
vim_free(paths); vim_free(paths);
return gap->ga_len; return gap->ga_len;

View File

@ -14,7 +14,7 @@ char_u *addstar(char_u *fname, int len, int context);
void set_expand_context(expand_T *xp); void set_expand_context(expand_T *xp);
void set_cmd_context(expand_T *xp, char_u *str, int len, int col, int use_ccline); void set_cmd_context(expand_T *xp, char_u *str, int len, int col, int use_ccline);
int expand_cmdline(expand_T *xp, char_u *str, int col, int *matchcount, char_u ***matches); int expand_cmdline(expand_T *xp, char_u *str, int col, int *matchcount, char_u ***matches);
void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options); void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options, int dirs);
int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_wild_list); int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_wild_list);
int wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp); int wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp);
void wildmenu_cleanup(cmdline_info_T *cclp); void wildmenu_cleanup(cmdline_info_T *cclp);

View File

@ -6,6 +6,8 @@ int estack_top_is_ufunc(ufunc_T *ufunc, long lnum);
estack_T *estack_pop(void); estack_T *estack_pop(void);
char_u *estack_sfile(estack_arg_T which); char_u *estack_sfile(estack_arg_T which);
void ex_runtime(exarg_T *eap); void ex_runtime(exarg_T *eap);
void set_context_in_runtime_cmd(expand_T *xp, char_u *arg);
int expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches);
int find_script_by_name(char_u *name); int find_script_by_name(char_u *name);
int get_new_scriptitem_for_fname(int *error, char_u *fname); int get_new_scriptitem_for_fname(int *error, char_u *fname);
int do_in_path(char_u *path, char_u *name, int flags, void (*callback)(char_u *fname, void *ck), void *cookie); int do_in_path(char_u *path, char_u *name, int flags, void (*callback)(char_u *fname, void *ck), void *cookie);

View File

@ -230,6 +230,44 @@ estack_sfile(estack_arg_T which UNUSED)
#endif #endif
} }
/*
* 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_u **argp)
{
char_u *arg = *argp;
char_u *p = skiptowhite(arg);
int what_len = (int)(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}"
*/ */
@ -237,34 +275,36 @@ estack_sfile(estack_arg_T which UNUSED)
ex_runtime(exarg_T *eap) ex_runtime(exarg_T *eap)
{ {
char_u *arg = eap->arg; char_u *arg = eap->arg;
char_u *p = skiptowhite(arg);
int len = (int)(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, char_u *arg)
{
runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags(&arg);
xp->xp_context = EXPAND_RUNTIME;
xp->xp_pattern = arg;
}
/*
* Handle command line completion for :runtime command.
*/
int
expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
{
char *directories[] = {"", NULL};
return ExpandRTDir(pat, runtime_expand_flags, numMatches, matches,
directories);
}
static void static void
source_callback(char_u *fname, void *cookie) source_callback(char_u *fname, void *cookie)
{ {
@ -959,12 +999,12 @@ remove_duplicates(garray_T *gap)
} }
/* /*
* Expand color scheme, compiler or filetype names. * Expand runtime file names.
* Search from 'runtimepath': * Search from 'runtimepath':
* 'runtimepath'/{dirnames}/{pat}.vim * 'runtimepath'/{dirnames}/{pat}.vim
* When "flags" has DIP_START: search also from 'start' of 'packpath': * When "flags" has DIP_START: search also from "start" of 'packpath':
* 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
* When "flags" has DIP_OPT: search also from 'opt' of 'packpath': * When "flags" has DIP_OPT: search also from "opt" of 'packpath':
* 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
* "dirnames" is an array with one or more directory names. * "dirnames" is an array with one or more directory names.
*/ */
@ -990,116 +1030,76 @@ ExpandRTDir(
for (i = 0; dirnames[i] != NULL; ++i) for (i = 0; dirnames[i] != NULL; ++i)
{ {
size_t buflen = STRLEN(dirnames[i]) + pat_len * 2 + 17; size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
char_u *buf = alloc(buflen); char *buf = alloc(buf_len);
if (buf == NULL) if (buf == NULL)
{ {
ga_clear_strings(&ga); ga_clear_strings(&ga);
return FAIL; return FAIL;
} }
if (*(dirnames[i]) == NUL) char *tail = buf + 15;
{ size_t tail_buflen = buf_len - 15;
// empty dir used for :runtime int glob_flags = 0;
if (gettail(pat) == pat) int expand_dirs = FALSE;
// no path separator, match dir names and script files
vim_snprintf((char *)buf, buflen, "\\(%s*.vim\\)\\|\\(%s*\\)", if (*(dirnames[i]) == NUL) // empty dir used for :runtime
pat, pat); vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
else
// has path separator, match script files
vim_snprintf((char *)buf, buflen, "%s*.vim", pat);
}
else else
vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
expand:
if ((flags & DIP_NORTP) == 0)
globpath(p_rtp, (char_u *)tail, &ga, glob_flags, expand_dirs);
if (flags & DIP_START)
{ {
vim_snprintf((char *)buf, buflen, "%s/%s*.vim", dirnames[i], pat); memcpy(tail - 15, "pack/*/start/*/", 15);
globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs);
} }
globpath(p_rtp, buf, &ga, 0);
if (flags & DIP_OPT)
{
memcpy(tail - 13, "pack/*/opt/*/", 13);
globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs);
}
if (*(dirnames[i]) == NUL && !expand_dirs)
{
// expand dir names in another round
vim_snprintf(tail, tail_buflen, "%s*", pat);
glob_flags = WILD_ADD_SLASH;
expand_dirs = TRUE;
goto expand;
}
vim_free(buf); vim_free(buf);
} }
if (flags & DIP_START) int pat_pathsep_cnt = 0;
{ for (i = 0; i < pat_len; ++i)
for (i = 0; dirnames[i] != NULL; ++i) if (vim_ispathsep(pat[i]))
{ ++pat_pathsep_cnt;
s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
if (s == NULL)
{
ga_clear_strings(&ga);
return FAIL;
}
sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
globpath(p_pp, s, &ga, 0);
vim_free(s);
}
}
if (flags & DIP_OPT)
{
for (i = 0; dirnames[i] != NULL; ++i)
{
s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
if (s == NULL)
{
ga_clear_strings(&ga);
return FAIL;
}
sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
globpath(p_pp, s, &ga, 0);
vim_free(s);
}
}
for (i = 0; i < ga.ga_len; ++i) for (i = 0; i < ga.ga_len; ++i)
{ {
match = ((char_u **)ga.ga_data)[i]; match = ((char_u **)ga.ga_data)[i];
s = match; s = match;
e = s + STRLEN(s); e = s + STRLEN(s);
char_u *res_start = s; if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
if ((flags & DIP_PRNEXT) != 0) && STRNICMP(e - 4, ".vim", 4) == 0)
{ {
char_u *p = (char_u *)strstr((char *)match, (char *)pat); e -= 4;
if (p != NULL)
// 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 - 4 > s && STRNICMP(e - 4, ".vim", 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;
mch_memmove(match, res_start, e - res_start + 1); for (s = e; s > match; MB_PTR_BACK(match, s))
if (s < match || (vim_ispathsep(*s)
// remove entries that look like backup files && ++match_pathsep_cnt > pat_pathsep_cnt))
if (e > s && e[-1] == '~') break;
{ ++s;
vim_free(match); *e = NUL;
char_u **fnames = (char_u **)ga.ga_data; mch_memmove(match, s, e - s + 1);
for (int j = i + 1; j < ga.ga_len; ++j)
fnames[j - 1] = fnames[j];
--ga.ga_len;
--i;
}
} }
if (ga.ga_len == 0) if (ga.ga_len == 0)
@ -1143,7 +1143,7 @@ ExpandPackAddDir(
return FAIL; return FAIL;
} }
sprintf((char *)s, "pack/*/opt/%s*", pat); sprintf((char *)s, "pack/*/opt/%s*", pat);
globpath(p_pp, s, &ga, 0); globpath(p_pp, s, &ga, 0, TRUE);
vim_free(s); vim_free(s);
for (i = 0; i < ga.ga_len; ++i) for (i = 0; i < ga.ga_len; ++i)

View File

@ -554,15 +554,6 @@ func Test_getcompletion()
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 =~ "\\./"'))
call assert_equal(-1, match(l[2:], '^\.\.\?/$')) call assert_equal(-1, match(l[2:], '^\.\.\?/$'))

View File

@ -190,8 +190,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')
@ -367,4 +369,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

View File

@ -695,6 +695,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 */
/**/
1231,
/**/ /**/
1230, 1230,
/**/ /**/

View File

@ -2662,7 +2662,7 @@ typedef enum {
#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
// Lowest number used for window ID. Cannot have this many windows. // Lowest number used for window ID. Cannot have this many windows.
#define LOWEST_WIN_ID 1000 #define LOWEST_WIN_ID 1000