diff src/testdir/test_cmdline.vim @ 29894:d8fc1effa724 v9.0.0285

patch 9.0.0285: it is not easy to change the command line from a plugin Commit: https://github.com/vim/vim/commit/07ea5f1509fe8dafe3262ed2702b4d0fc99e288b Author: Shougo Matsushita <Shougo.Matsu@gmail.com> Date: Sat Aug 27 12:22:25 2022 +0100 patch 9.0.0285: it is not easy to change the command line from a plugin Problem: It is not easy to change the command line from a plugin. Solution: Add setcmdline(). (Shougo Matsushita, closes https://github.com/vim/vim/issues/10869)
author Bram Moolenaar <Bram@vim.org>
date Sat, 27 Aug 2022 13:30:04 +0200
parents e038b8dd7cd9
children b366b19d1a3e
line wrap: on
line diff
--- a/src/testdir/test_cmdline.vim
+++ b/src/testdir/test_cmdline.vim
@@ -3262,4 +3262,44 @@ func Test_wildmenu_pum_disable_while_sho
   set wildoptions& wildmenu&
 endfunc
 
+func Test_setcmdline()
+  func SetText(text, pos)
+    call assert_equal(0, setcmdline(a:text))
+    call assert_equal(a:text, getcmdline())
+    call assert_equal(len(a:text) + 1, getcmdpos())
+
+    call assert_equal(0, setcmdline(a:text, a:pos))
+    call assert_equal(a:text, getcmdline())
+    call assert_equal(a:pos, getcmdpos())
+
+    call assert_fails('call setcmdline("' .. a:text .. '", -1)', 'E487:')
+    call assert_fails('call setcmdline({}, 0)', 'E928:')
+    call assert_fails('call setcmdline("' .. a:text .. '", {})', 'E728:')
+
+    return ''
+  endfunc
+
+  call feedkeys(":\<C-R>=SetText('set rtp?', 2)\<CR>\<CR>", 'xt')
+  call assert_equal('set rtp?', @:)
+
+  " setcmdline() returns 1 when not editing the command line.
+  call assert_equal(1, 'foo'->setcmdline())
+
+  " Called in custom function
+  func CustomComplete(A, L, P)
+    call assert_equal(0, setcmdline("DoCmd "))
+    return "January\nFebruary\nMars\n"
+  endfunc
+
+  com! -nargs=* -complete=custom,CustomComplete DoCmd :
+  call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
+  call assert_equal('"DoCmd January February Mars', @:)
+
+  " Called in <expr>
+  cnoremap <expr>a setcmdline('let foo=')
+  call feedkeys(":a\<CR>", 'tx')
+  call assert_equal('let foo=0', @:)
+  cunmap a
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab