-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api): nvim_exec2(), deprecate nvim_exec() #19032
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,4 +232,7 @@ return { | |
{ 'echo_opts', { | ||
"verbose"; | ||
}}; | ||
{ 'exec_opts', { | ||
"output"; | ||
}}; | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -45,13 +45,33 @@ | |||
/// @see |nvim_cmd()| | ||||
/// | ||||
/// @param src Vimscript code | ||||
/// @param output Capture and return all (non-error, non-shell |:!|) output | ||||
/// @param opts Optional parameters. | ||||
/// - output: (boolean, default false) Whether to capture and return | ||||
/// all (non-error, non-shell |:!|) output. | ||||
/// @param[out] err Error details (Vim error), if any | ||||
/// @return Output (non-error, non-shell |:!|) if `output` is true, | ||||
/// else empty string. | ||||
String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err) | ||||
FUNC_API_SINCE(7) | ||||
/// @return Dictionary containing information about execution, with these keys: | ||||
/// - output: (string|nil) Output if `opts.output` is true. | ||||
Dictionary nvim_exec2(uint64_t channel_id, String src, Dict(exec_opts) *opts, Error *err) | ||||
FUNC_API_SINCE(11) | ||||
{ | ||||
Dictionary result = ARRAY_DICT_INIT; | ||||
|
||||
String output = exec_impl(channel_id, src, opts, err); | ||||
if (ERROR_SET(err)) { | ||||
return result; | ||||
} | ||||
|
||||
if (HAS_KEY(opts->output) && api_object_to_bool(opts->output, "opts.output", false, err)) { | ||||
PUT(result, "output", STRING_OBJ(output)); | ||||
} | ||||
|
||||
return result; | ||||
} | ||||
|
||||
String exec_impl(uint64_t channel_id, String src, Dict(exec_opts) *opts, Error *err) | ||||
{ | ||||
Boolean output = api_object_to_bool(opts->output, "opts.output", false, err); | ||||
|
||||
const int save_msg_silent = msg_silent; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using see other functions in this file for neovim/src/nvim/api/vimscript.c Line 130 in f4518ef
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @justinmk, didn't manage to find a way to do this. More thoughts in a recent comment. |
||||
garray_T *const save_capture_ga = capture_ga; | ||||
const int save_msg_col = msg_col; | ||||
|
@@ -69,7 +89,7 @@ String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err) | |||
|
||||
const sctx_T save_current_sctx = api_set_sctx(channel_id); | ||||
|
||||
do_source_str(src.data, "nvim_exec()"); | ||||
do_source_str(src.data, "nvim_exec2()"); | ||||
if (output) { | ||||
capture_ga = save_capture_ga; | ||||
msg_silent = save_msg_silent; | ||||
|
@@ -108,8 +128,8 @@ String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err) | |||
/// | ||||
/// On execution error: fails with VimL error, updates v:errmsg. | ||||
/// | ||||
/// Prefer using |nvim_cmd()| or |nvim_exec()| over this. To evaluate multiple lines of Vim script | ||||
/// or an Ex command directly, use |nvim_exec()|. To construct an Ex command using a structured | ||||
/// Prefer using |nvim_cmd()| or |nvim_exec2()| over this. To evaluate multiple lines of Vim script | ||||
/// or an Ex command directly, use |nvim_exec2()|. To construct an Ex command using a structured | ||||
/// format and then execute it, use |nvim_cmd()|. To modify an Ex command before evaluating it, use | ||||
/// |nvim_parse_cmd()| in conjunction with |nvim_cmd()|. | ||||
/// | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
#include <string.h> | ||
|
||
#include "nvim/api/private/converter.h" | ||
#include "nvim/api/private/defs.h" | ||
#include "nvim/api/private/helpers.h" | ||
#include "nvim/api/vimscript.h" | ||
#include "nvim/context.h" | ||
|
@@ -271,8 +272,10 @@ static inline void ctx_save_funcs(Context *ctx, bool scriptonly) | |
size_t cmd_len = sizeof("func! ") + strlen(name); | ||
char *cmd = xmalloc(cmd_len); | ||
snprintf(cmd, cmd_len, "func! %s", name); | ||
String func_body = nvim_exec(VIML_INTERNAL_CALL, cstr_as_string(cmd), | ||
true, &err); | ||
Dict(exec_opts) opts = { 0 }; | ||
opts.output = BOOLEAN_OBJ(true); | ||
String func_body = exec_impl(VIML_INTERNAL_CALL, cstr_as_string(cmd), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Short answer: I tried but couldn't do it. This worked, so decided to use it. Long answer: I didn't manage to write proper C code to get Code for String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
FUNC_API_SINCE(7)
FUNC_API_DEPRECATED_SINCE(11)
{
Dict(exec_opts) opts = { 0 };
opts.output = BOOLEAN_OBJ(output);
Dictionary exec2_output = nvim_exec2(channel_id, src, &opts, err);
for (size_t i = 0; i < exec2_output.size; i++) {
if (strequal(exec2_output.items[i].key.data, "output")) {
return exec2_output.items[i].value.data.string;
}
}
return (String)STRING_INIT;
} I thought this should essentially be |
||
&opts, &err); | ||
xfree(cmd); | ||
if (!ERROR_SET(&err)) { | ||
ADD(ctx->funcs, STRING_OBJ(func_body)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
output
param be a string, is it likely that a caller would want to also capture "error" output in the future? So the current accepted values would beoutput: "yes"|"none"
and in the future it might beoutput: "yes"|"none"|"all"
? Or could be an Integer.If this is not expected then it can stay boolean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure here. To me having a one-to-one relation between
opts
and returned dictionary seems like a better design right now. So ifopts.output = true
, thenoutput
in result will represent output. Ifopts.error = true
, then errors will be safely captured and resulterror
will have those possible error captures. And so on.