Skip to content

Commit 92997dd

Browse files
Shougochrisbra
authored andcommitted
patch 9.0.1774: no support for custom cmdline completion
Problem: no support for custom cmdline completion Solution: Add new vimscript functions Add the following two functions: - getcmdcompltype() returns custom and customlist functions - getcompletion() supports both custom and customlist closes: #12228 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: Shougo Matsushita <[email protected]>
1 parent 19a3bc3 commit 92997dd

6 files changed

Lines changed: 85 additions & 3 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,6 +3551,8 @@ getcompletion({pat}, {type} [, {filtered}]) *getcompletion()*
35513551
cmdline |cmdline-completion| result
35523552
compiler compilers
35533553
cscope |:cscope| suboptions
3554+
custom,{func} custom completion, defined via {func}
3555+
customlist,{func} custom completion, defined via {func}
35543556
diff_buffer |:diffget| and |:diffput| completion
35553557
dir directory names
35563558
environment environment variable names

src/cmdexpand.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,6 +4022,7 @@ f_getcompletion(typval_T *argvars, typval_T *rettv)
40224022
{
40234023
xpc.xp_pattern = pat;
40244024
xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4025+
xpc.xp_line = pat;
40254026

40264027
xpc.xp_context = cmdcomplete_str_to_type(type);
40274028
if (xpc.xp_context == EXPAND_NOTHING)
@@ -4030,6 +4031,30 @@ f_getcompletion(typval_T *argvars, typval_T *rettv)
40304031
return;
40314032
}
40324033

4034+
if (xpc.xp_context == EXPAND_USER_DEFINED)
4035+
{
4036+
// Must be "custom,funcname" pattern
4037+
if (STRNCMP(type, "custom,", 7) != 0)
4038+
{
4039+
semsg(_(e_invalid_argument_str), type);
4040+
return;
4041+
}
4042+
4043+
xpc.xp_arg = type + 7;
4044+
}
4045+
4046+
if (xpc.xp_context == EXPAND_USER_LIST)
4047+
{
4048+
// Must be "customlist,funcname" pattern
4049+
if (STRNCMP(type, "customlist,", 11) != 0)
4050+
{
4051+
semsg(_(e_invalid_argument_str), type);
4052+
return;
4053+
}
4054+
4055+
xpc.xp_arg = type + 11;
4056+
}
4057+
40334058
# if defined(FEAT_MENU)
40344059
if (xpc.xp_context == EXPAND_MENUS)
40354060
{

src/ex_getln.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,6 +4152,7 @@ get_cmdline_str(void)
41524152
get_cmdline_completion(void)
41534153
{
41544154
cmdline_info_T *p;
4155+
char_u *buffer;
41554156

41564157
if (cmdline_star > 0)
41574158
return NULL;
@@ -4165,10 +4166,19 @@ get_cmdline_completion(void)
41654166
return NULL;
41664167

41674168
char_u *cmd_compl = cmdcomplete_type_to_str(p->xpc->xp_context);
4168-
if (cmd_compl != NULL)
4169-
return vim_strsave(cmd_compl);
4169+
if (cmd_compl == NULL)
4170+
return NULL;
41704171

4171-
return NULL;
4172+
if (p->xpc->xp_context == EXPAND_USER_LIST || p->xpc->xp_context == EXPAND_USER_DEFINED)
4173+
{
4174+
buffer = alloc(STRLEN(cmd_compl) + STRLEN(p->xpc->xp_arg) + 2);
4175+
if (buffer == NULL)
4176+
return NULL;
4177+
sprintf((char *)buffer, "%s,%s", cmd_compl, p->xpc->xp_arg);
4178+
return buffer;
4179+
}
4180+
4181+
return vim_strsave(cmd_compl);
41724182
}
41734183

41744184
/*

src/testdir/test_cmdline.vim

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,4 +3511,42 @@ func Test_getcompletion_usercmd()
35113511
delcom TestCompletion
35123512
endfunc
35133513

3514+
func Test_custom_completion()
3515+
func CustomComplete1(lead, line, pos)
3516+
return "a\nb\nc"
3517+
endfunc
3518+
func CustomComplete2(lead, line, pos)
3519+
return ['a', 'b']->filter({ _, val -> val->stridx(a:lead) == 0 })
3520+
endfunc
3521+
func Check_custom_completion()
3522+
call assert_equal('custom,CustomComplete1', getcmdcompltype())
3523+
return ''
3524+
endfunc
3525+
func Check_customlist_completion()
3526+
call assert_equal('customlist,CustomComplete2', getcmdcompltype())
3527+
return ''
3528+
endfunc
3529+
3530+
command -nargs=1 -complete=custom,CustomComplete1 Test1 echo
3531+
command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
3532+
3533+
call feedkeys(":Test1 \<C-R>=Check_custom_completion()\<CR>\<Esc>", "xt")
3534+
call feedkeys(":Test2 \<C-R>=Check_customlist_completion()\<CR>\<Esc>", "xt")
3535+
3536+
call assert_fails("call getcompletion('', 'custom')", 'E475:')
3537+
call assert_fails("call getcompletion('', 'customlist')", 'E475:')
3538+
3539+
call assert_equal(getcompletion('', 'custom,CustomComplete1'), ['a', 'b', 'c'])
3540+
call assert_equal(getcompletion('', 'customlist,CustomComplete2'), ['a', 'b'])
3541+
call assert_equal(getcompletion('b', 'customlist,CustomComplete2'), ['b'])
3542+
3543+
delcom Test1
3544+
delcom Test2
3545+
3546+
delfunc CustomComplete1
3547+
delfunc CustomComplete2
3548+
delfunc Check_custom_completion
3549+
delfunc Check_customlist_completion
3550+
endfunc
3551+
35143552
" vim: shiftwidth=2 sts=2 expandtab

src/usercmd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ cmdcomplete_str_to_type(char_u *complete_str)
481481
{
482482
int i;
483483

484+
if (STRNCMP(complete_str, "custom,", 7) == 0)
485+
return EXPAND_USER_DEFINED;
486+
if (STRNCMP(complete_str, "customlist,", 11) == 0)
487+
return EXPAND_USER_LIST;
488+
484489
for (i = 0; command_complete[i].expand != 0; ++i)
485490
if (STRCMP(complete_str, command_complete[i].name) == 0)
486491
return command_complete[i].expand;

src/version.c

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

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1774,
698700
/**/
699701
1773,
700702
/**/

0 commit comments

Comments
 (0)