Skip to content

Commit 62fe66f

Browse files
committed
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)
1 parent d45aa55 commit 62fe66f

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/ex_getln.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5147,7 +5147,7 @@ expand_shellcmd(
51475147
{
51485148
char_u *pat;
51495149
int i;
5150-
char_u *path;
5150+
char_u *path = NULL;
51515151
int mustfree = FALSE;
51525152
garray_T ga;
51535153
char_u *buf = alloc(MAXPATHL);
@@ -5156,6 +5156,9 @@ expand_shellcmd(
51565156
int flags = flagsarg;
51575157
int ret;
51585158
int did_curdir = FALSE;
5159+
hashtab_T found_ht;
5160+
hashitem_T *hi;
5161+
hash_T hash;
51595162

51605163
if (buf == NULL)
51615164
return FAIL;
@@ -5169,15 +5172,14 @@ expand_shellcmd(
51695172

51705173
flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
51715174

5172-
/* For an absolute name we don't use $PATH. */
5173-
if (mch_isFullName(pat))
5174-
path = (char_u *)" ";
5175-
else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
5176-
|| (pat[1] == '.' && vim_ispathsep(pat[2])))))
5175+
if (pat[0] == '.' && (vim_ispathsep(pat[1])
5176+
|| (pat[1] == '.' && vim_ispathsep(pat[2]))))
51775177
path = (char_u *)".";
51785178
else
51795179
{
5180-
path = vim_getenv((char_u *)"PATH", &mustfree);
5180+
/* For an absolute name we don't use $PATH. */
5181+
if (!mch_isFullName(pat))
5182+
path = vim_getenv((char_u *)"PATH", &mustfree);
51815183
if (path == NULL)
51825184
path = (char_u *)"";
51835185
}
@@ -5188,6 +5190,7 @@ expand_shellcmd(
51885190
* current directory, to find "subdir/cmd".
51895191
*/
51905192
ga_init2(&ga, (int)sizeof(char *), 10);
5193+
hash_init(&found_ht);
51915194
for (s = path; ; s = e)
51925195
{
51935196
if (*s == NUL)
@@ -5200,9 +5203,6 @@ expand_shellcmd(
52005203
else if (*s == '.')
52015204
did_curdir = TRUE;
52025205

5203-
if (*s == ' ')
5204-
++s; /* Skip space used for absolute path name. */
5205-
52065206
#if defined(MSWIN)
52075207
e = vim_strchr(s, ';');
52085208
#else
@@ -5229,15 +5229,23 @@ expand_shellcmd(
52295229
{
52305230
for (i = 0; i < *num_file; ++i)
52315231
{
5232-
s = (*file)[i];
5233-
if (STRLEN(s) > l)
5232+
char_u *name = (*file)[i];
5233+
5234+
if (STRLEN(name) > l)
52345235
{
5235-
/* Remove the path again. */
5236-
STRMOVE(s, s + l);
5237-
((char_u **)ga.ga_data)[ga.ga_len++] = s;
5236+
// Check if this name was already found.
5237+
hash = hash_hash(name + l);
5238+
hi = hash_lookup(&found_ht, name + l, hash);
5239+
if (HASHITEM_EMPTY(hi))
5240+
{
5241+
// Remove the path that was prepended.
5242+
STRMOVE(name, name + l);
5243+
((char_u **)ga.ga_data)[ga.ga_len++] = name;
5244+
hash_add_item(&found_ht, hi, name, hash);
5245+
name = NULL;
5246+
}
52385247
}
5239-
else
5240-
vim_free(s);
5248+
vim_free(name);
52415249
}
52425250
vim_free(*file);
52435251
}
@@ -5252,6 +5260,7 @@ expand_shellcmd(
52525260
vim_free(pat);
52535261
if (mustfree)
52545262
vim_free(path);
5263+
hash_clear(&found_ht);
52555264
return OK;
52565265
}
52575266

src/testdir/test_cmdline.vim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ func Test_getcompletion()
231231
let l = getcompletion('not', 'mapclear')
232232
call assert_equal([], l)
233233

234+
let l = getcompletion('.', 'shellcmd')
235+
call assert_equal(['./', '../'], l[0:1])
236+
call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
237+
let root = has('win32') ? 'C:\\' : '/'
238+
let l = getcompletion(root, 'shellcmd')
239+
let expected = map(filter(glob(root . '*', 0, 1),
240+
\ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
241+
call assert_equal(expected, l)
242+
234243
if has('cscope')
235244
let l = getcompletion('', 'cscope')
236245
let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
@@ -258,8 +267,7 @@ func Test_getcompletion()
258267
endif
259268

260269
" For others test if the name is recognized.
261-
let names = ['buffer', 'environment', 'file_in_path',
262-
\ 'mapping', 'shellcmd', 'tag', 'tag_listfiles', 'user']
270+
let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
263271
if has('cmdline_hist')
264272
call add(names, 'history')
265273
endif

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
17,
764766
/**/
765767
16,
766768
/**/

0 commit comments

Comments
 (0)