aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/builtin.txt4
-rw-r--r--runtime/lua/vim/_meta/vimfn.lua4
-rw-r--r--src/nvim/cmdexpand.c22
-rw-r--r--src/nvim/eval.lua4
-rw-r--r--src/nvim/ex_getln.c14
-rw-r--r--src/nvim/usercmd.c7
-rw-r--r--test/old/testdir/test_cmdline.vim38
7 files changed, 87 insertions, 6 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 0ae615ac3f..92cedbdc80 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -2354,7 +2354,9 @@ getcompletion({pat}, {type} [, {filtered}]) *getcompletion()*
color color schemes
command Ex command
compiler compilers
- diff_buffer |:diffget| and |:diffput| completion
+ custom,{func} custom completion, defined via {func}
+ customlist,{func} custom completion, defined via {func}
+ diff_buffer |:diffget| and |:diffput| completion
dir directory names
environment environment variable names
event autocommand events
diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua
index 030f268f81..300210b296 100644
--- a/runtime/lua/vim/_meta/vimfn.lua
+++ b/runtime/lua/vim/_meta/vimfn.lua
@@ -2871,7 +2871,9 @@ function vim.fn.getcmdwintype() end
--- color color schemes
--- command Ex command
--- compiler compilers
---- diff_buffer |:diffget| and |:diffput| completion
+--- custom,{func} custom completion, defined via {func}
+--- customlist,{func} custom completion, defined via {func}
+--- diff_buffer |:diffget| and |:diffput| completion
--- dir directory names
--- environment environment variable names
--- event autocommand events
diff --git a/src/nvim/cmdexpand.c b/src/nvim/cmdexpand.c
index eb9394c8ff..9470f77ab5 100644
--- a/src/nvim/cmdexpand.c
+++ b/src/nvim/cmdexpand.c
@@ -3515,12 +3515,34 @@ void f_getcompletion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
ExpandInit(&xpc);
xpc.xp_pattern = (char *)pattern;
xpc.xp_pattern_len = strlen(xpc.xp_pattern);
+ xpc.xp_line = (char *)pattern;
+
xpc.xp_context = cmdcomplete_str_to_type(type);
if (xpc.xp_context == EXPAND_NOTHING) {
semsg(_(e_invarg2), type);
return;
}
+ if (xpc.xp_context == EXPAND_USER_DEFINED) {
+ // Must be "custom,funcname" pattern
+ if (strncmp(type, "custom,", 7) != 0) {
+ semsg(_(e_invarg2), type);
+ return;
+ }
+
+ xpc.xp_arg = (char *)(type + 7);
+ }
+
+ if (xpc.xp_context == EXPAND_USER_LIST) {
+ // Must be "customlist,funcname" pattern
+ if (strncmp(type, "customlist,", 11) != 0) {
+ semsg(_(e_invarg2), type);
+ return;
+ }
+
+ xpc.xp_arg = (char *)(type + 11);
+ }
+
if (xpc.xp_context == EXPAND_MENUS) {
set_context_in_menu_cmd(&xpc, "menu", xpc.xp_pattern, false);
xpc.xp_pattern_len = strlen(xpc.xp_pattern);
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua
index 154023b25f..be87201bbf 100644
--- a/src/nvim/eval.lua
+++ b/src/nvim/eval.lua
@@ -3582,7 +3582,9 @@ M.funcs = {
color color schemes
command Ex command
compiler compilers
- diff_buffer |:diffget| and |:diffput| completion
+ custom,{func} custom completion, defined via {func}
+ customlist,{func} custom completion, defined via {func}
+ diff_buffer |:diffget| and |:diffput| completion
dir directory names
environment environment variable names
event autocommand events
diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c
index 17c17e60ce..89f5e92c33 100644
--- a/src/nvim/ex_getln.c
+++ b/src/nvim/ex_getln.c
@@ -4165,11 +4165,19 @@ static char *get_cmdline_completion(void)
}
char *cmd_compl = get_user_cmd_complete(p->xpc, p->xpc->xp_context);
- if (cmd_compl != NULL) {
- return xstrdup(cmd_compl);
+ if (cmd_compl == NULL) {
+ return NULL;
}
- return NULL;
+ if (p->xpc->xp_context == EXPAND_USER_LIST
+ || p->xpc->xp_context == EXPAND_USER_DEFINED) {
+ size_t buflen = strlen(cmd_compl) + strlen(p->xpc->xp_arg) + 2;
+ char *buffer = xmalloc(buflen);
+ snprintf(buffer, buflen, "%s,%s", cmd_compl, p->xpc->xp_arg);
+ return buffer;
+ }
+
+ return xstrdup(cmd_compl);
}
/// "getcmdcompltype()" function
diff --git a/src/nvim/usercmd.c b/src/nvim/usercmd.c
index 65720342ce..e585818e75 100644
--- a/src/nvim/usercmd.c
+++ b/src/nvim/usercmd.c
@@ -425,6 +425,13 @@ char *get_user_cmd_complete(expand_T *xp, int idx)
int cmdcomplete_str_to_type(const char *complete_str)
{
+ if (strncmp(complete_str, "custom,", 7) == 0) {
+ return EXPAND_USER_DEFINED;
+ }
+ if (strncmp(complete_str, "customlist,", 11) == 0) {
+ return EXPAND_USER_LIST;
+ }
+
for (int i = 0; i < (int)(ARRAY_SIZE(command_complete)); i++) {
char *cmd_compl = get_command_complete(i);
if (cmd_compl == NULL) {
diff --git a/test/old/testdir/test_cmdline.vim b/test/old/testdir/test_cmdline.vim
index eaea31f3eb..aab65eb8fe 100644
--- a/test/old/testdir/test_cmdline.vim
+++ b/test/old/testdir/test_cmdline.vim
@@ -3649,4 +3649,42 @@ func Test_getcompletion_usercmd()
delcom TestCompletion
endfunc
+func Test_custom_completion()
+ func CustomComplete1(lead, line, pos)
+ return "a\nb\nc"
+ endfunc
+ func CustomComplete2(lead, line, pos)
+ return ['a', 'b']->filter({ _, val -> val->stridx(a:lead) == 0 })
+ endfunc
+ func Check_custom_completion()
+ call assert_equal('custom,CustomComplete1', getcmdcompltype())
+ return ''
+ endfunc
+ func Check_customlist_completion()
+ call assert_equal('customlist,CustomComplete2', getcmdcompltype())
+ return ''
+ endfunc
+
+ command -nargs=1 -complete=custom,CustomComplete1 Test1 echo
+ command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
+
+ call feedkeys(":Test1 \<C-R>=Check_custom_completion()\<CR>\<Esc>", "xt")
+ call feedkeys(":Test2 \<C-R>=Check_customlist_completion()\<CR>\<Esc>", "xt")
+
+ call assert_fails("call getcompletion('', 'custom')", 'E475:')
+ call assert_fails("call getcompletion('', 'customlist')", 'E475:')
+
+ call assert_equal(getcompletion('', 'custom,CustomComplete1'), ['a', 'b', 'c'])
+ call assert_equal(getcompletion('', 'customlist,CustomComplete2'), ['a', 'b'])
+ call assert_equal(getcompletion('b', 'customlist,CustomComplete2'), ['b'])
+
+ delcom Test1
+ delcom Test2
+
+ delfunc CustomComplete1
+ delfunc CustomComplete2
+ delfunc Check_custom_completion
+ delfunc Check_customlist_completion
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab