patch 9.0.1227: no cmdline completion for :runtime

Problem:    No cmdline completion for :runtime.
Solution:   Add completion for :runtime. (closes #11853, closes #11447)
            Improve the resulting matches.
This commit is contained in:
root
2023-01-21 21:56:06 +00:00
committed by Bram Moolenaar
parent 51b2fc2ef5
commit a6759381a5
6 changed files with 94 additions and 13 deletions

View File

@ -56,6 +56,7 @@ cmdline_fuzzy_completion_supported(expand_T *xp)
&& xp->xp_context != EXPAND_OLD_SETTING
&& xp->xp_context != EXPAND_OWNSYNTAX
&& xp->xp_context != EXPAND_PACKADD
&& xp->xp_context != EXPAND_RUNTIME
&& xp->xp_context != EXPAND_SHELLCMD
&& xp->xp_context != EXPAND_TAGS
&& xp->xp_context != EXPAND_TAGS_LISTFILES
@ -1362,6 +1363,7 @@ addstar(
// For a tag pattern starting with "/" no translation is needed.
if (context == EXPAND_HELP
|| context == EXPAND_COLORS
|| context == EXPAND_RUNTIME
|| context == EXPAND_COMPILER
|| context == EXPAND_OWNSYNTAX
|| context == EXPAND_FILETYPE
@ -2312,6 +2314,11 @@ set_context_by_cmdname(
xp->xp_pattern = arg;
break;
case CMD_runtime:
xp->xp_context = EXPAND_RUNTIME;
xp->xp_pattern = arg;
break;
case CMD_compiler:
xp->xp_context = EXPAND_COMPILER;
xp->xp_pattern = arg;
@ -3019,6 +3026,12 @@ ExpandFromContext(
return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
directories);
}
if (xp->xp_context == EXPAND_RUNTIME)
{
char *directories[] = {"", NULL};
return ExpandRTDir(pat, DIP_START + DIP_OPT + DIP_PRNEXT, numMatches,
matches, directories);
}
if (xp->xp_context == EXPAND_COMPILER)
{
char *directories[] = {"compiler", NULL};

View File

@ -990,18 +990,34 @@ ExpandRTDir(
for (i = 0; dirnames[i] != NULL; ++i)
{
s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
if (s == NULL)
size_t buflen = STRLEN(dirnames[i]) + pat_len * 2 + 17;
char_u *buf = alloc(buflen);
if (buf == NULL)
{
ga_clear_strings(&ga);
return FAIL;
}
sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
globpath(p_rtp, s, &ga, 0);
vim_free(s);
if (*(dirnames[i]) == NUL)
{
// empty dir used for :runtime
if (gettail(pat) == pat)
// no path separator, match dir names and script files
vim_snprintf((char *)buf, buflen, "\\(%s*.vim\\)\\|\\(%s*\\)",
pat, pat);
else
// has path separator, match script files
vim_snprintf((char *)buf, buflen, "%s*.vim", pat);
}
else
{
vim_snprintf((char *)buf, buflen, "%s/%s*.vim", dirnames[i], pat);
}
globpath(p_rtp, buf, &ga, 0);
vim_free(buf);
}
if (flags & DIP_START) {
if (flags & DIP_START)
{
for (i = 0; dirnames[i] != NULL; ++i)
{
s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
@ -1016,7 +1032,8 @@ ExpandRTDir(
}
}
if (flags & DIP_OPT) {
if (flags & DIP_OPT)
{
for (i = 0; dirnames[i] != NULL; ++i)
{
s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
@ -1036,15 +1053,52 @@ ExpandRTDir(
match = ((char_u **)ga.ga_data)[i];
s = match;
e = s + STRLEN(s);
char_u *res_start = s;
if ((flags & DIP_PRNEXT) != 0)
{
char_u *p = (char_u *)strstr((char *)match, (char *)pat);
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)
{
e -= 4;
for (s = e; s > match; MB_PTR_BACK(match, s))
if (s < match || vim_ispathsep(*s))
break;
++s;
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;
mch_memmove(match, s, e - s + 1);
}
if (res_start > match)
mch_memmove(match, res_start, e - res_start + 1);
// remove entries that look like backup files
if (e > s && e[-1] == '~')
{
vim_free(match);
char_u **fnames = (char_u **)ga.ga_data;
for (int j = i + 1; j < ga.ga_len; ++j)
fnames[j - 1] = fnames[j];
--ga.ga_len;
--i;
}
}

View File

@ -552,6 +552,15 @@ func Test_getcompletion()
call assert_true(index(l, '<buffer>') >= 0)
let l = getcompletion('not', 'mapclear')
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')
call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))

View File

@ -86,6 +86,7 @@ static struct
#endif
{EXPAND_SETTINGS, "option"},
{EXPAND_PACKADD, "packadd"},
{EXPAND_RUNTIME, "runtime"},
{EXPAND_SHELLCMD, "shellcmd"},
#if defined(FEAT_SIGNS)
{EXPAND_SIGN, "sign"},

View File

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

View File

@ -811,6 +811,7 @@ extern int (*dyn_libintl_wputenv)(const wchar_t *envstring);
#define EXPAND_DISASSEMBLE 50
#define EXPAND_BREAKPOINT 51
#define EXPAND_SCRIPTNAMES 52
#define EXPAND_RUNTIME 53
// Values for exmode_active (0 is no exmode)
#define EXMODE_NORMAL 1
@ -2661,6 +2662,7 @@ typedef enum {
#define DIP_NORTP 0x20 // do not use 'runtimepath'
#define DIP_NOAFTER 0x40 // skip "after" directories
#define DIP_AFTER 0x80 // only use "after" directories
#define DIP_PRNEXT 0x100 // for print also file extension
// Lowest number used for window ID. Cannot have this many windows.
#define LOWEST_WIN_ID 1000