changeset 31798:5948cc887603 v9.0.1231

patch 9.0.1231: completion of :runtime does not handle {where} argument Commit: https://github.com/vim/vim/commit/3770f4c9cde7b5fcd10b6fa2e665cd0b69450fb2 Author: zeertzjq <zeertzjq@outlook.com> Date: Sun Jan 22 18:38:51 2023 +0000 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 https://github.com/vim/vim/issues/11863)
author Bram Moolenaar <Bram@vim.org>
date Sun, 22 Jan 2023 19:45:04 +0100
parents dd6495176845
children d4674c1fd5d9
files runtime/doc/builtin.txt src/cmdexpand.c src/filepath.c src/findfile.c src/proto/cmdexpand.pro src/proto/scriptfile.pro src/scriptfile.c src/testdir/test_cmdline.vim src/testdir/test_packadd.vim src/version.c src/vim.h
diffstat 11 files changed, 199 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -3528,6 +3528,7 @@ getcompletion({pat}, {type} [, {filtered
 		messages	|:messages| suboptions
 		option		options
 		packadd		optional package |pack-add| names
+		runtime		runtime file names |:runtime|
 		scriptnames	sourced script names |:scriptnames|
 		shellcmd	Shell command
 		sign		|:sign| suboptions
--- a/src/cmdexpand.c
+++ b/src/cmdexpand.c
@@ -2315,8 +2315,7 @@ set_context_by_cmdname(
 	    break;
 
 	case CMD_runtime:
-	    xp->xp_context = EXPAND_RUNTIME;
-	    xp->xp_pattern = arg;
+	    set_context_in_runtime_cmd(xp, arg);
 	    break;
 
 	case CMD_compiler:
@@ -3028,9 +3027,7 @@ ExpandFromContext(
     }
     if (xp->xp_context == EXPAND_RUNTIME)
     {
-	char *directories[] = {"", NULL};
-	return ExpandRTDir(pat, DIP_START + DIP_OPT + DIP_PRNEXT, numMatches,
-							 matches, directories);
+	return expand_runtime_cmd(pat, numMatches, matches);
     }
     if (xp->xp_context == EXPAND_COMPILER)
     {
@@ -3612,13 +3609,15 @@ ExpandUserList(
 /*
  * Expand "file" for all comma-separated directories in "path".
  * Adds the matches to "ga".  Caller must init "ga".
+ * If "dirs" is TRUE only expand directory names.
  */
     void
 globpath(
     char_u	*path,
     char_u	*file,
     garray_T	*ga,
-    int		expand_options)
+    int		expand_options,
+    int		dirs)
 {
     expand_T	xpc;
     char_u	*buf;
@@ -3631,7 +3630,7 @@ globpath(
 	return;
 
     ExpandInit(&xpc);
-    xpc.xp_context = EXPAND_FILES;
+    xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
 
     // Loop over all entries in {path}.
     while (*path != NUL)
@@ -4038,6 +4037,11 @@ f_getcompletion(typval_T *argvars, typva
 	    xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
 	}
 # 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))
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -1388,7 +1388,7 @@ f_globpath(typval_T *argvars, typval_T *
     if (file != NULL && !error)
     {
 	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)
 	    rettv->vval.v_string = ga_concat_strings(&ga, "\n");
 	else if (rettv_list_alloc(rettv) == OK)
--- a/src/findfile.c
+++ b/src/findfile.c
@@ -2533,7 +2533,7 @@ expand_in_path(
 	glob_flags |= WILD_ICASE;
     if (flags & EW_ADDSLASH)
 	glob_flags |= WILD_ADD_SLASH;
-    globpath(paths, pattern, gap, glob_flags);
+    globpath(paths, pattern, gap, glob_flags, FALSE);
     vim_free(paths);
 
     return gap->ga_len;
--- a/src/proto/cmdexpand.pro
+++ b/src/proto/cmdexpand.pro
@@ -14,7 +14,7 @@ char_u *addstar(char_u *fname, int len, 
 void set_expand_context(expand_T *xp);
 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);
-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_process_key(cmdline_info_T *cclp, int key, expand_T *xp);
 void wildmenu_cleanup(cmdline_info_T *cclp);
--- a/src/proto/scriptfile.pro
+++ b/src/proto/scriptfile.pro
@@ -6,6 +6,8 @@ int estack_top_is_ufunc(ufunc_T *ufunc, 
 estack_T *estack_pop(void);
 char_u *estack_sfile(estack_arg_T which);
 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 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);
--- a/src/scriptfile.c
+++ b/src/scriptfile.c
@@ -231,38 +231,78 @@ estack_sfile(estack_arg_T which UNUSED)
 }
 
 /*
+ * 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}"
  */
     void
 ex_runtime(exarg_T *eap)
 {
     char_u  *arg = eap->arg;
-    char_u  *p = skiptowhite(arg);
-    int	    len = (int)(p - arg);
     int	    flags = eap->forceit ? DIP_ALL : 0;
 
-    if (STRNCMP(arg, "START", len) == 0)
-    {
-	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);
-    }
+    flags += get_runtime_cmd_flags(&arg);
+    source_runtime(arg, flags);
+}
+
+static int runtime_expand_flags;
 
-    source_runtime(arg, 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
@@ -959,12 +999,12 @@ remove_duplicates(garray_T *gap)
 }
 
 /*
- * Expand color scheme, compiler or filetype names.
+ * Expand runtime file names.
  * Search from 'runtimepath':
  *   '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
- * 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
  * "dirnames" is an array with one or more directory names.
  */
@@ -990,116 +1030,76 @@ ExpandRTDir(
 
     for (i = 0; dirnames[i] != NULL; ++i)
     {
-	size_t buflen = STRLEN(dirnames[i]) + pat_len * 2 + 17;
-	char_u *buf = alloc(buflen);
+	size_t		buf_len = STRLEN(dirnames[i]) + pat_len + 22;
+	char		*buf = alloc(buf_len);
 	if (buf == NULL)
 	{
 	    ga_clear_strings(&ga);
 	    return FAIL;
 	}
-	if (*(dirnames[i]) == NUL)
+	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, &ga, glob_flags, expand_dirs);
+
+	if (flags & DIP_START)
 	{
-	    // 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);
+	    memcpy(tail - 15, "pack/*/start/*/", 15);
+	    globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs);
+	}
+
+	if (flags & DIP_OPT)
+	{
+	    memcpy(tail - 13, "pack/*/opt/*/", 13);
+	    globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs);
 	}
-	else
+
+	if (*(dirnames[i]) == NUL && !expand_dirs)
 	{
-	    vim_snprintf((char *)buf, buflen, "%s/%s*.vim", dirnames[i], pat);
+	    // expand dir names in another round
+	    vim_snprintf(tail, tail_buflen, "%s*", pat);
+	    glob_flags = WILD_ADD_SLASH;
+	    expand_dirs = TRUE;
+	    goto expand;
 	}
-	globpath(p_rtp, buf, &ga, 0);
+
 	vim_free(buf);
     }
 
-    if (flags & DIP_START)
-    {
-	for (i = 0; dirnames[i] != NULL; ++i)
-	{
-	    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);
-	}
-    }
+    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);
-	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)
+	if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
+					    && 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 -= 4;
 	    *e = NUL;
 	}
 
-	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;
-	}
+	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)
@@ -1143,7 +1143,7 @@ ExpandPackAddDir(
 	return FAIL;
     }
     sprintf((char *)s, "pack/*/opt/%s*", pat);
-    globpath(p_pp, s, &ga, 0);
+    globpath(p_pp, s, &ga, 0, TRUE);
     vim_free(s);
 
     for (i = 0; i < ga.ga_len; ++i)
--- a/src/testdir/test_cmdline.vim
+++ b/src/testdir/test_cmdline.vim
@@ -553,15 +553,6 @@ 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 =~ "\\./"'))
--- a/src/testdir/test_packadd.vim
+++ b/src/testdir/test_packadd.vim
@@ -190,8 +190,10 @@ func Test_packadd_completion()
 
   call mkdir(optdir1 . '/pluginA', 'p')
   call mkdir(optdir1 . '/pluginC', 'p')
+  call writefile([], optdir1 . '/unrelated')
   call mkdir(optdir2 . '/pluginB', 'p')
   call mkdir(optdir2 . '/pluginC', 'p')
+  call writefile([], optdir2 . '/unrelated')
 
   let li = []
   call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't')
@@ -367,4 +369,66 @@ func Test_runtime()
   call assert_equal('runstartopt', g:sequence)
 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
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1231,
+/**/
     1230,
 /**/
     1229,
--- a/src/vim.h
+++ b/src/vim.h
@@ -2662,7 +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
+#define DIP_KEEPEXT  0x100	// for completion: include file extension
 
 // Lowest number used for window ID. Cannot have this many windows.
 #define LOWEST_WIN_ID 1000