refactor: do more in TRY_WRAP

This commit is contained in:
Lewis Russell
2023-03-22 10:09:28 +00:00
committed by GitHub
parent c45b5e2c5b
commit 3285cd6ecc
8 changed files with 78 additions and 85 deletions

View File

@@ -683,11 +683,9 @@ Integer nvim_create_augroup(uint64_t channel_id, String name, Dict(create_augrou
void nvim_del_augroup_by_id(Integer id, Error *err) void nvim_del_augroup_by_id(Integer id, Error *err)
FUNC_API_SINCE(9) FUNC_API_SINCE(9)
{ {
TRY_WRAP({ TRY_WRAP(err, {
try_start();
char *name = augroup_name((int)id); char *name = augroup_name((int)id);
augroup_del(name, false); augroup_del(name, false);
try_end(err);
}); });
} }
@@ -700,10 +698,8 @@ void nvim_del_augroup_by_id(Integer id, Error *err)
void nvim_del_augroup_by_name(String name, Error *err) void nvim_del_augroup_by_name(String name, Error *err)
FUNC_API_SINCE(9) FUNC_API_SINCE(9)
{ {
TRY_WRAP({ TRY_WRAP(err, {
try_start();
augroup_del(name.data, false); augroup_del(name.data, false);
try_end(err);
}); });
} }

View File

@@ -697,8 +697,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
capture_ga = &capture_local; capture_ga = &capture_local;
} }
TRY_WRAP({ TRY_WRAP(err, {
try_start();
if (output) { if (output) {
msg_silent++; msg_silent++;
msg_col = 0; // prevent leading spaces msg_col = 0; // prevent leading spaces
@@ -714,8 +713,6 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
// Put msg_col back where it was, since nothing should have been written. // Put msg_col back where it was, since nothing should have been written.
msg_col = save_msg_col; msg_col = save_msg_col;
} }
try_end(err);
}); });
if (ERROR_SET(err)) { if (ERROR_SET(err)) {

View File

@@ -114,10 +114,8 @@ static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err)
ftbuf->b_p_ft = xstrdup(filetype); ftbuf->b_p_ft = xstrdup(filetype);
TRY_WRAP({ TRY_WRAP(err, {
try_start();
apply_autocmds(EVENT_FILETYPE, ftbuf->b_p_ft, ftbuf->b_fname, true, ftbuf); apply_autocmds(EVENT_FILETYPE, ftbuf->b_p_ft, ftbuf->b_fname, true, ftbuf);
try_end(err);
}); });
return ftbuf; return ftbuf;

View File

@@ -149,13 +149,15 @@ typedef struct {
// which would otherwise be ignored. This pattern is from do_cmdline(). // which would otherwise be ignored. This pattern is from do_cmdline().
// //
// TODO(bfredl): prepare error-handling at "top level" (nv_event). // TODO(bfredl): prepare error-handling at "top level" (nv_event).
#define TRY_WRAP(code) \ #define TRY_WRAP(err, code) \
do { \ do { \
msglist_T **saved_msg_list = msg_list; \ msglist_T **saved_msg_list = msg_list; \
msglist_T *private_msg_list; \ msglist_T *private_msg_list; \
msg_list = &private_msg_list; \ msg_list = &private_msg_list; \
private_msg_list = NULL; \ private_msg_list = NULL; \
try_start(); \
code; \ code; \
try_end(err); \
msg_list = saved_msg_list; /* Restore the exception context. */ \ msg_list = saved_msg_list; /* Restore the exception context. */ \
} while (0) } while (0)

View File

@@ -538,10 +538,8 @@ ArrayOf(String) nvim_get_runtime_file(String name, Boolean all, Error *err)
int flags = DIP_DIRFILE | (all ? DIP_ALL : 0); int flags = DIP_DIRFILE | (all ? DIP_ALL : 0);
TRY_WRAP({ TRY_WRAP(err, {
try_start();
do_in_runtimepath((name.size ? name.data : ""), flags, find_runtime_cb, &rv); do_in_runtimepath((name.size ? name.data : ""), flags, find_runtime_cb, &rv);
try_end(err);
}); });
return rv; return rv;
} }
@@ -1238,14 +1236,12 @@ void nvim_put(ArrayOf(String) lines, String type, Boolean after, Boolean follow,
finish_yankreg_from_object(reg, false); finish_yankreg_from_object(reg, false);
TRY_WRAP({ TRY_WRAP(err, {
try_start();
bool VIsual_was_active = VIsual_active; bool VIsual_was_active = VIsual_active;
msg_silent++; // Avoid "N more lines" message. msg_silent++; // Avoid "N more lines" message.
do_put(0, reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0); do_put(0, reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0);
msg_silent--; msg_silent--;
VIsual_active = VIsual_was_active; VIsual_active = VIsual_was_active;
try_end(err);
}); });
cleanup: cleanup:

View File

@@ -137,7 +137,6 @@ Object nvim_eval(String expr, Error *err)
static int recursive = 0; // recursion depth static int recursive = 0; // recursion depth
Object rv = OBJECT_INIT; Object rv = OBJECT_INIT;
TRY_WRAP({
// Initialize `force_abort` and `suppress_errthrow` at the top level. // Initialize `force_abort` and `suppress_errthrow` at the top level.
if (!recursive) { if (!recursive) {
force_abort = false; force_abort = false;
@@ -146,15 +145,19 @@ Object nvim_eval(String expr, Error *err)
// `did_emsg` is set by emsg(), which cancels execution. // `did_emsg` is set by emsg(), which cancels execution.
did_emsg = false; did_emsg = false;
} }
recursive++; recursive++;
try_start();
typval_T rettv; typval_T rettv;
int ok = eval0(expr.data, &rettv, NULL, true); int ok;
if (!try_end(err)) { TRY_WRAP(err, {
ok = eval0(expr.data, &rettv, NULL, true);
});
if (!ERROR_SET(err)) {
if (ok == FAIL) { if (ok == FAIL) {
// Should never happen, try_end() should get the error. #8371 // Should never happen, try_end() (in TRY_WRAP) should get the error. #8371
api_set_error(err, kErrorTypeException, api_set_error(err, kErrorTypeException,
"Failed to evaluate expression: '%.*s'", 256, expr.data); "Failed to evaluate expression: '%.*s'", 256, expr.data);
} else { } else {
@@ -164,7 +167,6 @@ Object nvim_eval(String expr, Error *err)
tv_clear(&rettv); tv_clear(&rettv);
recursive--; recursive--;
});
return rv; return rv;
} }
@@ -196,7 +198,6 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
} }
} }
TRY_WRAP({
// Initialize `force_abort` and `suppress_errthrow` at the top level. // Initialize `force_abort` and `suppress_errthrow` at the top level.
if (!recursive) { if (!recursive) {
force_abort = false; force_abort = false;
@@ -206,23 +207,27 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
did_emsg = false; did_emsg = false;
} }
recursive++; recursive++;
try_start();
typval_T rettv; typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT; funcexe_T funcexe = FUNCEXE_INIT;
funcexe.fe_firstline = curwin->w_cursor.lnum; funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum; funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = true; funcexe.fe_evaluate = true;
funcexe.fe_selfdict = self; funcexe.fe_selfdict = self;
TRY_WRAP(err, {
// call_func() retval is deceptive, ignore it. Instead we set `msg_list` // call_func() retval is deceptive, ignore it. Instead we set `msg_list`
// (see above) to capture abort-causing non-exception errors. // (see above) to capture abort-causing non-exception errors.
(void)call_func(fn.data, (int)fn.size, &rettv, (int)args.size, (void)call_func(fn.data, (int)fn.size, &rettv, (int)args.size,
vim_args, &funcexe); vim_args, &funcexe);
if (!try_end(err)) { });
if (!ERROR_SET(err)) {
rv = vim_to_object(&rettv); rv = vim_to_object(&rettv);
} }
tv_clear(&rettv); tv_clear(&rettv);
recursive--; recursive--;
});
free_vim_args: free_vim_args:
while (i > 0) { while (i > 0) {

View File

@@ -1157,27 +1157,28 @@ int nlua_call(lua_State *lstate)
} }
} }
TRY_WRAP({
// TODO(bfredl): this should be simplified in error handling refactor // TODO(bfredl): this should be simplified in error handling refactor
force_abort = false; force_abort = false;
suppress_errthrow = false; suppress_errthrow = false;
did_throw = false; did_throw = false;
did_emsg = false; did_emsg = false;
try_start();
typval_T rettv; typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT; funcexe_T funcexe = FUNCEXE_INIT;
funcexe.fe_firstline = curwin->w_cursor.lnum; funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum; funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = true; funcexe.fe_evaluate = true;
TRY_WRAP(&err, {
// call_func() retval is deceptive, ignore it. Instead we set `msg_list` // call_func() retval is deceptive, ignore it. Instead we set `msg_list`
// (TRY_WRAP) to capture abort-causing non-exception errors. // (TRY_WRAP) to capture abort-causing non-exception errors.
(void)call_func((char *)name, (int)name_len, &rettv, nargs, vim_args, &funcexe); (void)call_func((char *)name, (int)name_len, &rettv, nargs, vim_args, &funcexe);
if (!try_end(&err)) { });
if (!ERROR_SET(&err)) {
nlua_push_typval(lstate, &rettv, false); nlua_push_typval(lstate, &rettv, false);
} }
tv_clear(&rettv); tv_clear(&rettv);
});
free_vim_args: free_vim_args:
while (i > 0) { while (i > 0) {

View File

@@ -287,10 +287,8 @@ int nlua_regex(lua_State *lstate)
const char *text = luaL_checkstring(lstate, 1); const char *text = luaL_checkstring(lstate, 1);
regprog_T *prog = NULL; regprog_T *prog = NULL;
TRY_WRAP({ TRY_WRAP(&err, {
try_start();
prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT); prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT);
try_end(&err);
}); });
if (ERROR_SET(&err)) { if (ERROR_SET(&err)) {