From 6f1601a1b94e6ea724d8436600c64760525d1d2b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 11 Oct 2024 06:48:45 +0800 Subject: [PATCH] vim-patch:9.1.0774: "shellcmdline" doesn't work with getcompletion() (#30750) Problem: "shellcmdline" doesn't work with getcompletion(). Solution: Use set_context_for_wildcard_arg() (zeertzjq). closes: vim/vim#15834 https://github.com/vim/vim/commit/85f36d61e09b12d6f1c60201129823371daa4a84 --- runtime/doc/builtin.txt | 1 + runtime/lua/vim/_meta/vimfn.lua | 1 + src/nvim/cmdexpand.c | 8 +++++++- src/nvim/eval.lua | 1 + test/old/testdir/test_cmdline.vim | 10 ++++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 617fd87553..aa31c59ab2 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -3362,6 +3362,7 @@ getcompletion({pat}, {type} [, {filtered}]) *getcompletion()* runtime |:runtime| completion scriptnames sourced script names |:scriptnames| shellcmd Shell command + shellcmdline Shell command line with filename arguments sign |:sign| suboptions syntax syntax file names |'syntax'| syntime |:syntime| suboptions diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 1e1e87fca8..81e7070ac0 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -3008,6 +3008,7 @@ function vim.fn.getcmdwintype() end --- runtime |:runtime| completion --- scriptnames sourced script names |:scriptnames| --- shellcmd Shell command +--- shellcmdline Shell command line with filename arguments --- sign |:sign| suboptions --- syntax syntax file names |'syntax'| --- syntime |:syntime| suboptions diff --git a/src/nvim/cmdexpand.c b/src/nvim/cmdexpand.c index 45648fc15f..250d705ee6 100644 --- a/src/nvim/cmdexpand.c +++ b/src/nvim/cmdexpand.c @@ -1529,7 +1529,8 @@ static void set_context_for_wildcard_arg(exarg_T *eap, const char *arg, bool use xp->xp_context = EXPAND_FILES; // For a shell command more chars need to be escaped. - if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal + if (usefilter + || (eap != NULL && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)) || *complp == EXPAND_SHELLCMDLINE) { #ifndef BACKSLASH_IN_FILENAME xp->xp_shell = true; @@ -3604,6 +3605,11 @@ void f_getcompletion(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) set_context_in_runtime_cmd(&xpc, xpc.xp_pattern); xpc.xp_pattern_len = strlen(xpc.xp_pattern); } + if (xpc.xp_context == EXPAND_SHELLCMDLINE) { + int context = EXPAND_SHELLCMDLINE; + set_context_for_wildcard_arg(NULL, xpc.xp_pattern, false, &xpc, &context); + xpc.xp_pattern_len = strlen(xpc.xp_pattern); + } theend: if (xpc.xp_context == EXPAND_LUA) { diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 24f986ef4e..a440450b9d 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -3775,6 +3775,7 @@ M.funcs = { runtime |:runtime| completion scriptnames sourced script names |:scriptnames| shellcmd Shell command + shellcmdline Shell command line with filename arguments sign |:sign| suboptions syntax syntax file names |'syntax'| syntime |:syntime| suboptions diff --git a/test/old/testdir/test_cmdline.vim b/test/old/testdir/test_cmdline.vim index a5df637cc2..0ef2c33c03 100644 --- a/test/old/testdir/test_cmdline.vim +++ b/test/old/testdir/test_cmdline.vim @@ -1032,6 +1032,8 @@ func Test_cmdline_complete_shellcmdline() call feedkeys(":MyCmd whoam\\\"\", 'tx') call assert_match('^".*\', @:) + let l = getcompletion('whoam', 'shellcmdline') + call assert_match('\', join(l, ' ')) delcommand MyCmd endfunc @@ -3818,9 +3820,13 @@ func Test_cmdline_complete_shellcmdline_argument() call feedkeys(":MyCmd vim test_cmdline.\\\"\", 'xt') call assert_equal('"MyCmd vim test_cmdline.vim', @:) + call assert_equal(['test_cmdline.vim'], + \ getcompletion('vim test_cmdline.', 'shellcmdline')) call feedkeys(":MyCmd vim nonexistentfile\\\"\", 'xt') call assert_equal('"MyCmd vim nonexistentfile', @:) + call assert_equal([], + \ getcompletion('vim nonexistentfile', 'shellcmdline')) let compl1 = getcompletion('', 'file')[0] let compl2 = getcompletion('', 'file')[1] @@ -3837,9 +3843,13 @@ func Test_cmdline_complete_shellcmdline_argument() set wildoptions& call feedkeys(":MyCmd vim test_cmdline.\\\"\", 'xt') call assert_equal('"MyCmd vim test_cmdline.vim', @:) + call assert_equal(['test_cmdline.vim'], + \ getcompletion('vim test_cmdline.', 'shellcmdline')) call feedkeys(":MyCmd vim nonexistentfile\\\"\", 'xt') call assert_equal('"MyCmd vim nonexistentfile', @:) + call assert_equal([], + \ getcompletion('vim nonexistentfile', 'shellcmdline')) let compl1 = getcompletion('', 'file')[0] let compl2 = getcompletion('', 'file')[1]