aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-07-14 14:47:25 +0800
committerGitHub <noreply@github.com>2022-07-14 14:47:25 +0800
commitb93cb481a20972c6af330de3e8974f641bccb355 (patch)
tree3907c673ecb7c235f76dca6033101ace93e84dfc
parent778541067a2f0ec3ef4bc1b07def646323e852a9 (diff)
downloadrneovim-b93cb481a20972c6af330de3e8974f641bccb355.tar.gz
rneovim-b93cb481a20972c6af330de3e8974f641bccb355.tar.bz2
rneovim-b93cb481a20972c6af330de3e8974f641bccb355.zip
feat(eval)!: input() support any type for "cancelreturn" in a dict (#19357)
-rw-r--r--runtime/doc/vim_diff.txt3
-rw-r--r--src/nvim/eval.c26
-rw-r--r--test/functional/vimscript/input_spec.lua14
3 files changed, 24 insertions, 19 deletions
diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt
index 8e853aaf9e..d692fcfc37 100644
--- a/runtime/doc/vim_diff.txt
+++ b/runtime/doc/vim_diff.txt
@@ -350,7 +350,8 @@ Commands:
Functions:
|input()| and |inputdialog()| support for each other’s features (return on
cancel and completion respectively) via dictionary argument (replaces all
- other arguments if used).
+ other arguments if used), and "cancelreturn" can have any type if passed in
+ a dictionary.
|input()| and |inputdialog()| support user-defined cmdline highlighting.
Highlight groups:
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 4976bcc15d..096fcba981 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -6554,7 +6554,8 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const
const char *prompt = "";
const char *defstr = "";
- const char *cancelreturn = NULL;
+ typval_T *cancelreturn = NULL;
+ typval_T cancelreturn_strarg2 = TV_INITIAL_VALUE;
const char *xp_name = NULL;
Callback input_callback = { .type = kCallbackNone };
char prompt_buf[NUMBUFLEN];
@@ -6576,13 +6577,9 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const
if (defstr == NULL) {
return;
}
- cancelreturn = tv_dict_get_string_buf_chk(dict, S_LEN("cancelreturn"),
- cancelreturn_buf, def);
- if (cancelreturn == NULL) { // error
- return;
- }
- if (*cancelreturn == NUL) {
- cancelreturn = NULL;
+ dictitem_T *cancelreturn_di = tv_dict_find(dict, S_LEN("cancelreturn"));
+ if (cancelreturn_di != NULL) {
+ cancelreturn = &cancelreturn_di->di_tv;
}
xp_name = tv_dict_get_string_buf_chk(dict, S_LEN("completion"),
xp_name_buf, def);
@@ -6606,15 +6603,16 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const
return;
}
if (argvars[2].v_type != VAR_UNKNOWN) {
- const char *const arg2 = tv_get_string_buf_chk(&argvars[2],
- cancelreturn_buf);
- if (arg2 == NULL) {
+ const char *const strarg2 = tv_get_string_buf_chk(&argvars[2], cancelreturn_buf);
+ if (strarg2 == NULL) {
return;
}
if (inputdialog) {
- cancelreturn = arg2;
+ cancelreturn_strarg2.v_type = VAR_STRING;
+ cancelreturn_strarg2.vval.v_string = (char *)strarg2;
+ cancelreturn = &cancelreturn_strarg2;
} else {
- xp_name = arg2;
+ xp_name = strarg2;
}
}
}
@@ -6662,7 +6660,7 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const
callback_free(&input_callback);
if (rettv->vval.v_string == NULL && cancelreturn != NULL) {
- rettv->vval.v_string = xstrdup(cancelreturn);
+ tv_copy(cancelreturn, rettv);
}
xfree(xp_arg);
diff --git a/test/functional/vimscript/input_spec.lua b/test/functional/vimscript/input_spec.lua
index 14c02f9eb2..554d15e550 100644
--- a/test/functional/vimscript/input_spec.lua
+++ b/test/functional/vimscript/input_spec.lua
@@ -9,6 +9,7 @@ local source = helpers.source
local command = helpers.command
local exc_exec = helpers.exc_exec
local nvim_async = helpers.nvim_async
+local NIL = helpers.NIL
local screen
@@ -200,6 +201,15 @@ describe('input()', function()
feed(':let var = input({"cancelreturn": "BAR"})<CR>')
feed('<Esc>')
eq('BAR', meths.get_var('var'))
+ feed(':let var = input({"cancelreturn": []})<CR>')
+ feed('<Esc>')
+ eq({}, meths.get_var('var'))
+ feed(':let var = input({"cancelreturn": v:false})<CR>')
+ feed('<Esc>')
+ eq(false, meths.get_var('var'))
+ feed(':let var = input({"cancelreturn": v:null})<CR>')
+ feed('<Esc>')
+ eq(NIL, meths.get_var('var'))
end)
it('supports default string', function()
feed(':let var = input("", "DEF1")<CR>')
@@ -220,8 +230,6 @@ describe('input()', function()
eq('Vim(call):E730: using List as a String',
exc_exec('call input({"prompt": []})'))
eq('Vim(call):E730: using List as a String',
- exc_exec('call input({"cancelreturn": []})'))
- eq('Vim(call):E730: using List as a String',
exc_exec('call input({"default": []})'))
eq('Vim(call):E730: using List as a String',
exc_exec('call input({"completion": []})'))
@@ -418,8 +426,6 @@ describe('inputdialog()', function()
eq('Vim(call):E730: using List as a String',
exc_exec('call inputdialog({"prompt": []})'))
eq('Vim(call):E730: using List as a String',
- exc_exec('call inputdialog({"cancelreturn": []})'))
- eq('Vim(call):E730: using List as a String',
exc_exec('call inputdialog({"default": []})'))
eq('Vim(call):E730: using List as a String',
exc_exec('call inputdialog({"completion": []})'))