mirror of
https://github.com/vim/vim.git
synced 2025-10-05 05:34:07 -04:00
patch 8.1.0017: shell command completion has duplicates
Problem: Shell command completion has duplicates. (Yegappan Lakshmanan) Solution: Use a hash table to avoid duplicates. (Ozaki Kiichi, closes #539, closes #2733)
This commit is contained in:
@@ -5147,7 +5147,7 @@ expand_shellcmd(
|
|||||||
{
|
{
|
||||||
char_u *pat;
|
char_u *pat;
|
||||||
int i;
|
int i;
|
||||||
char_u *path;
|
char_u *path = NULL;
|
||||||
int mustfree = FALSE;
|
int mustfree = FALSE;
|
||||||
garray_T ga;
|
garray_T ga;
|
||||||
char_u *buf = alloc(MAXPATHL);
|
char_u *buf = alloc(MAXPATHL);
|
||||||
@@ -5156,6 +5156,9 @@ expand_shellcmd(
|
|||||||
int flags = flagsarg;
|
int flags = flagsarg;
|
||||||
int ret;
|
int ret;
|
||||||
int did_curdir = FALSE;
|
int did_curdir = FALSE;
|
||||||
|
hashtab_T found_ht;
|
||||||
|
hashitem_T *hi;
|
||||||
|
hash_T hash;
|
||||||
|
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
@@ -5169,14 +5172,13 @@ expand_shellcmd(
|
|||||||
|
|
||||||
flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
|
flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
|
||||||
|
|
||||||
/* For an absolute name we don't use $PATH. */
|
if (pat[0] == '.' && (vim_ispathsep(pat[1])
|
||||||
if (mch_isFullName(pat))
|
|| (pat[1] == '.' && vim_ispathsep(pat[2]))))
|
||||||
path = (char_u *)" ";
|
|
||||||
else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
|
|
||||||
|| (pat[1] == '.' && vim_ispathsep(pat[2])))))
|
|
||||||
path = (char_u *)".";
|
path = (char_u *)".";
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
/* For an absolute name we don't use $PATH. */
|
||||||
|
if (!mch_isFullName(pat))
|
||||||
path = vim_getenv((char_u *)"PATH", &mustfree);
|
path = vim_getenv((char_u *)"PATH", &mustfree);
|
||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
path = (char_u *)"";
|
path = (char_u *)"";
|
||||||
@@ -5188,6 +5190,7 @@ expand_shellcmd(
|
|||||||
* current directory, to find "subdir/cmd".
|
* current directory, to find "subdir/cmd".
|
||||||
*/
|
*/
|
||||||
ga_init2(&ga, (int)sizeof(char *), 10);
|
ga_init2(&ga, (int)sizeof(char *), 10);
|
||||||
|
hash_init(&found_ht);
|
||||||
for (s = path; ; s = e)
|
for (s = path; ; s = e)
|
||||||
{
|
{
|
||||||
if (*s == NUL)
|
if (*s == NUL)
|
||||||
@@ -5200,9 +5203,6 @@ expand_shellcmd(
|
|||||||
else if (*s == '.')
|
else if (*s == '.')
|
||||||
did_curdir = TRUE;
|
did_curdir = TRUE;
|
||||||
|
|
||||||
if (*s == ' ')
|
|
||||||
++s; /* Skip space used for absolute path name. */
|
|
||||||
|
|
||||||
#if defined(MSWIN)
|
#if defined(MSWIN)
|
||||||
e = vim_strchr(s, ';');
|
e = vim_strchr(s, ';');
|
||||||
#else
|
#else
|
||||||
@@ -5229,15 +5229,23 @@ expand_shellcmd(
|
|||||||
{
|
{
|
||||||
for (i = 0; i < *num_file; ++i)
|
for (i = 0; i < *num_file; ++i)
|
||||||
{
|
{
|
||||||
s = (*file)[i];
|
char_u *name = (*file)[i];
|
||||||
if (STRLEN(s) > l)
|
|
||||||
|
if (STRLEN(name) > l)
|
||||||
{
|
{
|
||||||
/* Remove the path again. */
|
// Check if this name was already found.
|
||||||
STRMOVE(s, s + l);
|
hash = hash_hash(name + l);
|
||||||
((char_u **)ga.ga_data)[ga.ga_len++] = s;
|
hi = hash_lookup(&found_ht, name + l, hash);
|
||||||
|
if (HASHITEM_EMPTY(hi))
|
||||||
|
{
|
||||||
|
// Remove the path that was prepended.
|
||||||
|
STRMOVE(name, name + l);
|
||||||
|
((char_u **)ga.ga_data)[ga.ga_len++] = name;
|
||||||
|
hash_add_item(&found_ht, hi, name, hash);
|
||||||
|
name = NULL;
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
vim_free(s);
|
vim_free(name);
|
||||||
}
|
}
|
||||||
vim_free(*file);
|
vim_free(*file);
|
||||||
}
|
}
|
||||||
@@ -5252,6 +5260,7 @@ expand_shellcmd(
|
|||||||
vim_free(pat);
|
vim_free(pat);
|
||||||
if (mustfree)
|
if (mustfree)
|
||||||
vim_free(path);
|
vim_free(path);
|
||||||
|
hash_clear(&found_ht);
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -231,6 +231,15 @@ func Test_getcompletion()
|
|||||||
let l = getcompletion('not', 'mapclear')
|
let l = getcompletion('not', 'mapclear')
|
||||||
call assert_equal([], l)
|
call assert_equal([], l)
|
||||||
|
|
||||||
|
let l = getcompletion('.', 'shellcmd')
|
||||||
|
call assert_equal(['./', '../'], l[0:1])
|
||||||
|
call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
|
||||||
|
let root = has('win32') ? 'C:\\' : '/'
|
||||||
|
let l = getcompletion(root, 'shellcmd')
|
||||||
|
let expected = map(filter(glob(root . '*', 0, 1),
|
||||||
|
\ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
|
||||||
|
call assert_equal(expected, l)
|
||||||
|
|
||||||
if has('cscope')
|
if has('cscope')
|
||||||
let l = getcompletion('', 'cscope')
|
let l = getcompletion('', 'cscope')
|
||||||
let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
|
let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
|
||||||
@@ -258,8 +267,7 @@ func Test_getcompletion()
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
" For others test if the name is recognized.
|
" For others test if the name is recognized.
|
||||||
let names = ['buffer', 'environment', 'file_in_path',
|
let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
|
||||||
\ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
|
|
||||||
if has('cmdline_hist')
|
if has('cmdline_hist')
|
||||||
call add(names, 'history')
|
call add(names, 'history')
|
||||||
endif
|
endif
|
||||||
|
@@ -761,6 +761,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 */
|
||||||
|
/**/
|
||||||
|
17,
|
||||||
/**/
|
/**/
|
||||||
16,
|
16,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user