1
0
forked from aniani/vim

patch 9.0.1238: :runtime completion can be further improved

Problem:    :runtime completion can be further improved.
Solution:   Also complete the {where} argument values and adjust the
            completion for that. (closes #11874)
This commit is contained in:
zeertzjq
2023-01-24 12:34:03 +00:00
committed by Bram Moolenaar
parent 6ec6666047
commit 5c8771bc5a
7 changed files with 206 additions and 161 deletions

View File

@@ -3528,7 +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| runtime |:runtime| completion
scriptnames sourced script names |:scriptnames| scriptnames sourced script names |:scriptnames|
shellcmd Shell command shellcmd Shell command
sign |:sign| suboptions sign |:sign| suboptions

View File

@@ -3025,10 +3025,6 @@ ExpandFromContext(
return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches, return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
directories); directories);
} }
if (xp->xp_context == EXPAND_RUNTIME)
{
return expand_runtime_cmd(pat, numMatches, matches);
}
if (xp->xp_context == EXPAND_COMPILER) if (xp->xp_context == EXPAND_COMPILER)
{ {
char *directories[] = {"compiler", NULL}; char *directories[] = {"compiler", NULL};
@@ -3050,6 +3046,8 @@ ExpandFromContext(
#endif #endif
if (xp->xp_context == EXPAND_PACKADD) if (xp->xp_context == EXPAND_PACKADD)
return ExpandPackAddDir(pat, numMatches, matches); return ExpandPackAddDir(pat, numMatches, matches);
if (xp->xp_context == EXPAND_RUNTIME)
return expand_runtime_cmd(pat, numMatches, matches);
// When expanding a function name starting with s:, match the <SNR>nr_ // When expanding a function name starting with s:, match the <SNR>nr_
// prefix. // prefix.

View File

@@ -7,7 +7,6 @@ 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); 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);
@@ -21,6 +20,7 @@ void ex_packloadall(exarg_T *eap);
void ex_packadd(exarg_T *eap); void ex_packadd(exarg_T *eap);
void remove_duplicates(garray_T *gap); void remove_duplicates(garray_T *gap);
int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirnames[]); int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirnames[]);
int expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches);
int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
void ex_source(exarg_T *eap); void ex_source(exarg_T *eap);
void ex_options(exarg_T *eap); void ex_options(exarg_T *eap);

View File

@@ -230,37 +230,35 @@ estack_sfile(estack_arg_T which UNUSED)
} }
/* /*
* Get DIP_ flags from the [what] argument of a :runtime command. * Get DIP_ flags from the [where] argument of a :runtime command.
* "*argp" is advanced to after the [what] argument. * "*argp" is advanced to after the [where] argument if it is found.
*/ */
static int static int
get_runtime_cmd_flags(char_u **argp) get_runtime_cmd_flags(char_u **argp, size_t where_len)
{ {
char_u *arg = *argp; char_u *arg = *argp;
char_u *p = skiptowhite(arg);
int what_len = (int)(p - arg);
if (what_len == 0) if (where_len == 0)
return 0; return 0;
if (STRNCMP(arg, "START", what_len) == 0) if (STRNCMP(arg, "START", where_len) == 0)
{ {
*argp = skipwhite(arg + what_len); *argp = skipwhite(arg + where_len);
return DIP_START + DIP_NORTP; return DIP_START + DIP_NORTP;
} }
if (STRNCMP(arg, "OPT", what_len) == 0) if (STRNCMP(arg, "OPT", where_len) == 0)
{ {
*argp = skipwhite(arg + what_len); *argp = skipwhite(arg + where_len);
return DIP_OPT + DIP_NORTP; return DIP_OPT + DIP_NORTP;
} }
if (STRNCMP(arg, "PACK", what_len) == 0) if (STRNCMP(arg, "PACK", where_len) == 0)
{ {
*argp = skipwhite(arg + what_len); *argp = skipwhite(arg + where_len);
return DIP_START + DIP_OPT + DIP_NORTP; return DIP_START + DIP_OPT + DIP_NORTP;
} }
if (STRNCMP(arg, "ALL", what_len) == 0) if (STRNCMP(arg, "ALL", where_len) == 0)
{ {
*argp = skipwhite(arg + what_len); *argp = skipwhite(arg + where_len);
return DIP_START + DIP_OPT; return DIP_START + DIP_OPT;
} }
@@ -268,15 +266,15 @@ get_runtime_cmd_flags(char_u **argp)
} }
/* /*
* ":runtime [what] {name}" * ":runtime [where] {name}"
*/ */
void void
ex_runtime(exarg_T *eap) ex_runtime(exarg_T *eap)
{ {
char_u *arg = eap->arg; char_u *arg = eap->arg;
int flags = eap->forceit ? DIP_ALL : 0; int flags = eap->forceit ? DIP_ALL : 0;
char_u *p = skiptowhite(arg);
flags += get_runtime_cmd_flags(&arg); flags += get_runtime_cmd_flags(&arg, p - arg);
source_runtime(arg, flags); source_runtime(arg, flags);
} }
@@ -288,22 +286,13 @@ static int runtime_expand_flags;
void void
set_context_in_runtime_cmd(expand_T *xp, char_u *arg) set_context_in_runtime_cmd(expand_T *xp, char_u *arg)
{ {
runtime_expand_flags = DIP_KEEPEXT + get_runtime_cmd_flags(&arg); char_u *p = skiptowhite(arg);
runtime_expand_flags
= *p != NUL ? get_runtime_cmd_flags(&arg, p - arg) : 0;
xp->xp_context = EXPAND_RUNTIME; xp->xp_context = EXPAND_RUNTIME;
xp->xp_pattern = arg; 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)
{ {
@@ -997,6 +986,96 @@ remove_duplicates(garray_T *gap)
} }
} }
static void
ExpandRTDir_int(
char_u *pat,
size_t pat_len,
int flags,
int keep_ext,
garray_T *gap,
char *dirnames[])
{
for (int i = 0; dirnames[i] != NULL; ++i)
{
size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22;
char *buf = alloc(buf_len);
if (buf == NULL)
{
ga_clear_strings(gap);
return;
}
char *tail = buf + 15;
size_t tail_buflen = buf_len - 15;
int glob_flags = 0;
int expand_dirs = FALSE;
if (*(dirnames[i]) == NUL) // empty dir used for :runtime
vim_snprintf(tail, tail_buflen, "%s*.vim", pat);
else
vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat);
expand:
if ((flags & DIP_NORTP) == 0)
globpath(p_rtp, (char_u *)tail, gap, glob_flags, expand_dirs);
if (flags & DIP_START)
{
memcpy(tail - 15, "pack/*/start/*/", 15);
globpath(p_pp, (char_u *)tail - 15, gap, glob_flags, expand_dirs);
}
if (flags & DIP_OPT)
{
memcpy(tail - 13, "pack/*/opt/*/", 13);
globpath(p_pp, (char_u *)tail - 13, gap, 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);
}
int pat_pathsep_cnt = 0;
for (size_t i = 0; i < pat_len; ++i)
if (vim_ispathsep(pat[i]))
++pat_pathsep_cnt;
for (int i = 0; i < gap->ga_len; ++i)
{
char_u *match = ((char_u **)gap->ga_data)[i];
char_u *s = match;
char_u *e = s + STRLEN(s);
if (e - 4 > s && !keep_ext && STRNICMP(e - 4, ".vim", 4) == 0)
{
e -= 4;
*e = NUL;
}
int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
for (s = e; s > match; MB_PTR_BACK(match, s))
if (s < match || (vim_ispathsep(*s)
&& ++match_pathsep_cnt > pat_pathsep_cnt))
break;
++s;
if (s != match)
mch_memmove(match, s, e - s + 1);
}
if (gap->ga_len == 0)
return;
// Sort and remove duplicates which can happen when specifying multiple
// directories in dirnames.
remove_duplicates(gap);
}
/* /*
* Expand runtime file names. * Expand runtime file names.
* Search from 'runtimepath': * Search from 'runtimepath':
@@ -1015,101 +1094,56 @@ ExpandRTDir(
char_u ***file, char_u ***file,
char *dirnames[]) char *dirnames[])
{ {
char_u *s;
char_u *e;
char_u *match;
garray_T ga;
int i;
int pat_len;
*num_file = 0; *num_file = 0;
*file = NULL; *file = NULL;
pat_len = (int)STRLEN(pat);
garray_T ga;
ga_init2(&ga, sizeof(char *), 10); ga_init2(&ga, sizeof(char *), 10);
for (i = 0; dirnames[i] != NULL; ++i) ExpandRTDir_int(pat, STRLEN(pat), flags, FALSE, &ga, dirnames);
{
size_t buf_len = STRLEN(dirnames[i]) + pat_len + 22; if (ga.ga_len == 0)
char *buf = alloc(buf_len);
if (buf == NULL)
{
ga_clear_strings(&ga);
return FAIL; return FAIL;
*file = ga.ga_data;
*num_file = ga.ga_len;
return OK;
} }
char *tail = buf + 15;
size_t tail_buflen = buf_len - 15;
int glob_flags = 0;
int expand_dirs = FALSE;
if (*(dirnames[i]) == NUL) // empty dir used for :runtime /*
vim_snprintf(tail, tail_buflen, "%s*.vim", pat); * Handle command line completion for :runtime command.
else */
vim_snprintf(tail, tail_buflen, "%s/%s*.vim", dirnames[i], pat); int
expand_runtime_cmd(char_u *pat, int *numMatches, char_u ***matches)
expand:
if ((flags & DIP_NORTP) == 0)
globpath(p_rtp, (char_u *)tail, &ga, glob_flags, expand_dirs);
if (flags & DIP_START)
{ {
memcpy(tail - 15, "pack/*/start/*/", 15); *numMatches = 0;
globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs); *matches = NULL;
}
if (flags & DIP_OPT) garray_T ga;
ga_init2(&ga, sizeof(char *), 10);
size_t pat_len = (int)STRLEN(pat);
char *dirnames[] = {"", NULL};
ExpandRTDir_int(pat, pat_len, runtime_expand_flags, TRUE, &ga, dirnames);
// Try to complete values for [where] argument when none was found.
if (runtime_expand_flags == 0)
{ {
memcpy(tail - 13, "pack/*/opt/*/", 13); char *where_values[] = {"START", "OPT", "PACK", "ALL"};
globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs); for (size_t i = 0; i < ARRAY_LENGTH(where_values); ++i)
} if (STRNCMP(pat, where_values[i], pat_len) == 0)
if (*(dirnames[i]) == NUL && !expand_dirs)
{ {
// expand dir names in another round char_u *p = vim_strsave((char_u *)where_values[i]);
vim_snprintf(tail, tail_buflen, "%s*", pat); if (p != NULL && ga_add_string(&ga, p) == FAIL)
glob_flags = WILD_ADD_SLASH; vim_free(p);
expand_dirs = TRUE;
goto expand;
} }
vim_free(buf);
}
int pat_pathsep_cnt = 0;
for (i = 0; i < pat_len; ++i)
if (vim_ispathsep(pat[i]))
++pat_pathsep_cnt;
for (i = 0; i < ga.ga_len; ++i)
{
match = ((char_u **)ga.ga_data)[i];
s = match;
e = s + STRLEN(s);
if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
&& STRNICMP(e - 4, ".vim", 4) == 0)
{
e -= 4;
*e = NUL;
}
int match_pathsep_cnt = (e > s && e[-1] == '/') ? -1 : 0;
for (s = e; s > match; MB_PTR_BACK(match, s))
if (s < match || (vim_ispathsep(*s)
&& ++match_pathsep_cnt > pat_pathsep_cnt))
break;
++s;
*e = NUL;
mch_memmove(match, s, e - s + 1);
} }
if (ga.ga_len == 0) if (ga.ga_len == 0)
return FAIL; return FAIL;
// Sort and remove duplicates which can happen when specifying multiple *matches = ga.ga_data;
// directories in dirnames. *numMatches = ga.ga_len;
remove_duplicates(&ga);
*file = ga.ga_data;
*num_file = ga.ga_len;
return OK; return OK;
} }

View File

@@ -370,63 +370,75 @@ func Test_runtime()
endfunc endfunc
func Test_runtime_completion() func Test_runtime_completion()
let rundir = &packpath . '/runtime/Xextra' let rundir = &packpath . '/runtime/Aextra'
let startdir = &packpath . '/pack/mine/start/foo/Xextra' let startdir = &packpath . '/pack/mine/start/foo/Aextra'
let optdir = &packpath . '/pack/mine/opt/bar/Xextra' let optdir = &packpath . '/pack/mine/opt/bar/Aextra'
call mkdir(rundir . '/Xrunbaz', 'p') call mkdir(rundir . '/Arunbaz', 'p')
call mkdir(startdir . '/Xstartbaz', 'p') call mkdir(startdir . '/Astartbaz', 'p')
call mkdir(optdir . '/Xoptbaz', 'p') call mkdir(optdir . '/Aoptbaz', 'p')
call writefile([], rundir . '/../Xrunfoo.vim') call writefile([], rundir . '/../Arunfoo.vim')
call writefile([], rundir . '/Xrunbar.vim') call writefile([], rundir . '/Arunbar.vim')
call writefile([], rundir . '/Xunrelated') call writefile([], rundir . '/Aunrelated')
call writefile([], rundir . '/../Xunrelated') call writefile([], rundir . '/../Aunrelated')
call writefile([], startdir . '/../Xstartfoo.vim') call writefile([], startdir . '/../Astartfoo.vim')
call writefile([], startdir . '/Xstartbar.vim') call writefile([], startdir . '/Astartbar.vim')
call writefile([], startdir . '/Xunrelated') call writefile([], startdir . '/Aunrelated')
call writefile([], startdir . '/../Xunrelated') call writefile([], startdir . '/../Aunrelated')
call writefile([], optdir . '/../Xoptfoo.vim') call writefile([], optdir . '/../Aoptfoo.vim')
call writefile([], optdir . '/Xoptbar.vim') call writefile([], optdir . '/Aoptbar.vim')
call writefile([], optdir . '/Xunrelated') call writefile([], optdir . '/Aunrelated')
call writefile([], optdir . '/../Xunrelated') call writefile([], optdir . '/../Aunrelated')
exe 'set rtp=' . &packpath . '/runtime' exe 'set rtp=' . &packpath . '/runtime'
func Check_runtime_completion(arg, arg1, res) func Check_runtime_completion(arg, arg1, res)
call feedkeys(':runtime ' .. a:arg .. "\<C-A>\<C-B>\"\<CR>", 'xt') call feedkeys(':runtime ' .. a:arg .. "\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"runtime ' .. a:arg1 .. join(a:res), @:) call assert_equal('"runtime ' .. a:arg1 .. join(a:res), @:)
call assert_equal(a:res, getcompletion(a:arg, 'runtime')) 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 endfunc
call Check_runtime_completion('', '', call Check_runtime_completion('', '',
\ ['Xextra/', 'Xrunfoo.vim']) \ ['Aextra/', 'Arunfoo.vim', 'START', 'OPT', 'PACK', 'ALL'])
call Check_runtime_completion('Xextra/', '', call Check_runtime_completion('S', '',
\ ['Xextra/Xrunbar.vim', 'Xextra/Xrunbaz/']) \ ['START'])
call Check_runtime_completion('O', '',
\ ['OPT'])
call Check_runtime_completion('P', '',
\ ['PACK'])
call Check_runtime_completion('A', '',
\ ['Aextra/', 'Arunfoo.vim', 'ALL'])
call Check_runtime_completion('Aextra/', '',
\ ['Aextra/Arunbar.vim', 'Aextra/Arunbaz/'])
call Check_runtime_completion('START ', 'START ', call Check_runtime_completion('START ', 'START ',
\ ['Xextra/', 'Xstartfoo.vim']) \ ['Aextra/', 'Astartfoo.vim'])
call Check_runtime_completion('START Xextra/', 'START ', call Check_runtime_completion('START A', 'START ',
\ ['Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/']) \ ['Aextra/', 'Astartfoo.vim'])
call Check_runtime_completion('START Aextra/', 'START ',
\ ['Aextra/Astartbar.vim', 'Aextra/Astartbaz/'])
call Check_runtime_completion('OPT ', 'OPT ', call Check_runtime_completion('OPT ', 'OPT ',
\ ['Xextra/', 'Xoptfoo.vim']) \ ['Aextra/', 'Aoptfoo.vim'])
call Check_runtime_completion('OPT Xextra/', 'OPT ', call Check_runtime_completion('OPT A', 'OPT ',
\ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/']) \ ['Aextra/', 'Aoptfoo.vim'])
call Check_runtime_completion('OPT Aextra/', 'OPT ',
\ ['Aextra/Aoptbar.vim', 'Aextra/Aoptbaz/'])
call Check_runtime_completion('PACK ', 'PACK ', call Check_runtime_completion('PACK ', 'PACK ',
\ ['Xextra/', 'Xoptfoo.vim', 'Xstartfoo.vim']) \ ['Aextra/', 'Aoptfoo.vim', 'Astartfoo.vim'])
call Check_runtime_completion('PACK Xextra/', 'PACK ', call Check_runtime_completion('PACK A', 'PACK ',
\ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/', \ ['Aextra/', 'Aoptfoo.vim', 'Astartfoo.vim'])
\ 'Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/']) call Check_runtime_completion('PACK Aextra/', 'PACK ',
\ ['Aextra/Aoptbar.vim', 'Aextra/Aoptbaz/',
\ 'Aextra/Astartbar.vim', 'Aextra/Astartbaz/'])
call Check_runtime_completion('ALL ', 'ALL ', call Check_runtime_completion('ALL ', 'ALL ',
\ ['Xextra/', 'Xoptfoo.vim', 'Xrunfoo.vim', 'Xstartfoo.vim']) \ ['Aextra/', 'Aoptfoo.vim', 'Arunfoo.vim', 'Astartfoo.vim'])
call Check_runtime_completion('ALL Xextra/', 'ALL ', call Check_runtime_completion('ALL A', 'ALL ',
\ ['Xextra/Xoptbar.vim', 'Xextra/Xoptbaz/', \ ['Aextra/', 'Aoptfoo.vim', 'Arunfoo.vim', 'Astartfoo.vim'])
\ 'Xextra/Xrunbar.vim', 'Xextra/Xrunbaz/', call Check_runtime_completion('ALL Aextra/', 'ALL ',
\ 'Xextra/Xstartbar.vim', 'Xextra/Xstartbaz/']) \ ['Aextra/Aoptbar.vim', 'Aextra/Aoptbaz/',
\ 'Aextra/Arunbar.vim', 'Aextra/Arunbaz/',
\ 'Aextra/Astartbar.vim', 'Aextra/Astartbaz/'])
delfunc Check_runtime_completion delfunc Check_runtime_completion
endfunc endfunc

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 */
/**/
1238,
/**/ /**/
1237, 1237,
/**/ /**/

View File

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