]> granicus.if.org Git - vim/commitdiff
patch 9.0.1231: completion of :runtime does not handle {where} argument v9.0.1231
authorzeertzjq <zeertzjq@outlook.com>
Sun, 22 Jan 2023 18:38:51 +0000 (18:38 +0000)
committerBram Moolenaar <Bram@vim.org>
Sun, 22 Jan 2023 18:38:51 +0000 (18:38 +0000)
Problem:    Completion of :runtime does not handle {where} argument.
Solution:   Parse the {where} argument. (closes #11863)

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

index a888ae08970176dcc5946e170576642569fee53a..4fa6b34fb6e864c6340058b31ad07e3a9f69233b 100644 (file)
@@ -3528,6 +3528,7 @@ getcompletion({pat}, {type} [, {filtered}])               *getcompletion()*
                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
index 4fe9bd35bcceaecb4370e49aec3d7dc7a3f7e5e6..17f3878a0b11dc09c93add5a833ba0de1f19b067 100644 (file)
@@ -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, typval_T *rettv)
            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))
index be1a5810adcc194182bb7a7847614eca052c951b..c448edb0495fe65bfd2491ef4932039f412e32b4 100644 (file)
@@ -1388,7 +1388,7 @@ f_globpath(typval_T *argvars, typval_T *rettv)
     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)
index ea07a89ed7ed74278520af133eec0d71e06ce5aa..4e2ad774c6a1ffe1d25bcaea7fed108c1f6a3e6d 100644 (file)
@@ -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;
index 2fb543ab43dd58c8747d5ffd72264ee318b004c7..1e2b148747e7981d83785be529de325642d0d724 100644 (file)
@@ -14,7 +14,7 @@ char_u *addstar(char_u *fname, int len, int context);
 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);
index c80ed8cc046a3d302b4532f3df8e46ee633d6261..df89a6aed0d74ed30b9e15a38eafe4ddab5605d3 100644 (file)
@@ -6,6 +6,8 @@ int estack_top_is_ufunc(ufunc_T *ufunc, long lnum);
 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);
index 3404c22d1aa73e9dc5ad9030ae5610031bc5d4f4..da6bdd010751b116c46aeb1360a28a20af8d1e7e 100644 (file)
@@ -231,40 +231,80 @@ estack_sfile(estack_arg_T which UNUSED)
 }
 
 /*
- * ":runtime [what] {name}"
+ * Get DIP_ flags from the [what] argument of a :runtime command.
+ * "*argp" is advanced to after the [what] argument.
  */
-    void
-ex_runtime(exarg_T *eap)
+    static int
+get_runtime_cmd_flags(char_u **argp)
 {
-    char_u  *arg = eap->arg;
+    char_u *arg = *argp;
     char_u  *p = skiptowhite(arg);
-    int            len = (int)(p - arg);
-    int            flags = eap->forceit ? DIP_ALL : 0;
+    int            what_len = (int)(p - arg);
+
+    if (what_len == 0)
+       return 0;
 
-    if (STRNCMP(arg, "START", len) == 0)
+    if (STRNCMP(arg, "START", what_len) == 0)
     {
-       flags += DIP_START + DIP_NORTP;
-       arg = skipwhite(arg + len);
+       *argp = skipwhite(arg + what_len);
+       return DIP_START + DIP_NORTP;
     }
-    else if (STRNCMP(arg, "OPT", len) == 0)
+    if (STRNCMP(arg, "OPT", what_len) == 0)
     {
-       flags += DIP_OPT + DIP_NORTP;
-       arg = skipwhite(arg + len);
+       *argp = skipwhite(arg + what_len);
+       return DIP_OPT + DIP_NORTP;
     }
-    else if (STRNCMP(arg, "PACK", len) == 0)
+    if (STRNCMP(arg, "PACK", what_len) == 0)
     {
-       flags += DIP_START + DIP_OPT + DIP_NORTP;
-       arg = skipwhite(arg + len);
+       *argp = skipwhite(arg + what_len);
+       return DIP_START + DIP_OPT + DIP_NORTP;
     }
-    else if (STRNCMP(arg, "ALL", len) == 0)
+    if (STRNCMP(arg, "ALL", what_len) == 0)
     {
-       flags += DIP_START + DIP_OPT;
-       arg = skipwhite(arg + len);
+       *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;
+    int            flags = eap->forceit ? DIP_ALL : 0;
+
+    flags += get_runtime_cmd_flags(&arg);
     source_runtime(arg, flags);
 }
 
+static int runtime_expand_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
 source_callback(char_u *fname, void *cookie)
 {
@@ -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)
-       {
-           // 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);
-       }
+       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)
        {
-           vim_snprintf((char *)buf, buflen, "%s/%s*.vim", dirnames[i], pat);
+           memcpy(tail - 15, "pack/*/start/*/", 15);
+           globpath(p_pp, (char_u *)tail - 15, &ga, glob_flags, expand_dirs);
        }
-       globpath(p_rtp, buf, &ga, 0);
-       vim_free(buf);
-    }
 
-    if (flags & DIP_START)
-    {
-       for (i = 0; dirnames[i] != NULL; ++i)
+       if (flags & DIP_OPT)
        {
-           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);
+           memcpy(tail - 13, "pack/*/opt/*/", 13);
+           globpath(p_pp, (char_u *)tail - 13, &ga, glob_flags, expand_dirs);
        }
-    }
 
-    if (flags & DIP_OPT)
-    {
-       for (i = 0; dirnames[i] != NULL; ++i)
+       if (*(dirnames[i]) == NUL && !expand_dirs)
        {
-           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);
+           // 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 (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)
+       if (e - 4 > s && (flags & DIP_KEEPEXT) == 0
+                                           && STRNICMP(e - 4, ".vim", 4) == 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 (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)
index 8ed89c6891674b219607be125af55480ee40f0df..23d6b4c92c630a51f8207c8292c3349387ba5345 100644 (file)
@@ -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 =~ "\\./"'))
index 09fbcc00a141482a43e93c4d6a36a7a7a41ddf2e..b5583ae73ab96f8694a49952cc2deb4f7d8dec45 100644 (file)
@@ -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
index 4e66d9fe5a100265e6fde781e53922f270ae158a..0d595db8e8046af9573e99b4c4921ca34fe4d340 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1231,
 /**/
     1230,
 /**/
index 930b17b104c591d6cf640b712f5953ba058fbb06..ec08f63b71a0a34abf892362fa4b3e0654d7675a 100644 (file)
--- 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