diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-03-22 10:09:28 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-22 10:09:28 +0000 |
commit | 3285cd6eccd9b7f33cc32f992c2607c3fc4ca13f (patch) | |
tree | 4357dab570945b69f7ee3c4406c45c1153222049 | |
parent | c45b5e2c5b0f02742099ebccc44462fe4203e99c (diff) | |
download | rneovim-3285cd6eccd9b7f33cc32f992c2607c3fc4ca13f.tar.gz rneovim-3285cd6eccd9b7f33cc32f992c2607c3fc4ca13f.tar.bz2 rneovim-3285cd6eccd9b7f33cc32f992c2607c3fc4ca13f.zip |
refactor: do more in TRY_WRAP
-rw-r--r-- | src/nvim/api/autocmd.c | 8 | ||||
-rw-r--r-- | src/nvim/api/command.c | 5 | ||||
-rw-r--r-- | src/nvim/api/options.c | 4 | ||||
-rw-r--r-- | src/nvim/api/private/helpers.h | 4 | ||||
-rw-r--r-- | src/nvim/api/vim.c | 8 | ||||
-rw-r--r-- | src/nvim/api/vimscript.c | 97 | ||||
-rw-r--r-- | src/nvim/lua/executor.c | 35 | ||||
-rw-r--r-- | src/nvim/lua/stdlib.c | 4 |
8 files changed, 79 insertions, 86 deletions
diff --git a/src/nvim/api/autocmd.c b/src/nvim/api/autocmd.c index aff7622dc5..620a295788 100644 --- a/src/nvim/api/autocmd.c +++ b/src/nvim/api/autocmd.c @@ -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) FUNC_API_SINCE(9) { - TRY_WRAP({ - try_start(); + TRY_WRAP(err, { char *name = augroup_name((int)id); 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) FUNC_API_SINCE(9) { - TRY_WRAP({ - try_start(); + TRY_WRAP(err, { augroup_del(name.data, false); - try_end(err); }); } diff --git a/src/nvim/api/command.c b/src/nvim/api/command.c index de766c14b9..1455f28050 100644 --- a/src/nvim/api/command.c +++ b/src/nvim/api/command.c @@ -697,8 +697,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error capture_ga = &capture_local; } - TRY_WRAP({ - try_start(); + TRY_WRAP(err, { if (output) { msg_silent++; 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. msg_col = save_msg_col; } - - try_end(err); }); if (ERROR_SET(err)) { diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 7aef6e6146..502c73d02d 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -114,10 +114,8 @@ static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err) ftbuf->b_p_ft = xstrdup(filetype); - TRY_WRAP({ - try_start(); + TRY_WRAP(err, { apply_autocmds(EVENT_FILETYPE, ftbuf->b_p_ft, ftbuf->b_fname, true, ftbuf); - try_end(err); }); return ftbuf; diff --git a/src/nvim/api/private/helpers.h b/src/nvim/api/private/helpers.h index 1b82aeac34..eef97cccb2 100644 --- a/src/nvim/api/private/helpers.h +++ b/src/nvim/api/private/helpers.h @@ -149,13 +149,15 @@ typedef struct { // which would otherwise be ignored. This pattern is from do_cmdline(). // // TODO(bfredl): prepare error-handling at "top level" (nv_event). -#define TRY_WRAP(code) \ +#define TRY_WRAP(err, code) \ do { \ msglist_T **saved_msg_list = msg_list; \ msglist_T *private_msg_list; \ msg_list = &private_msg_list; \ private_msg_list = NULL; \ + try_start(); \ code; \ + try_end(err); \ msg_list = saved_msg_list; /* Restore the exception context. */ \ } while (0) diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index a8cc7aa454..4689e6b5bf 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -538,10 +538,8 @@ ArrayOf(String) nvim_get_runtime_file(String name, Boolean all, Error *err) int flags = DIP_DIRFILE | (all ? DIP_ALL : 0); - TRY_WRAP({ - try_start(); + TRY_WRAP(err, { do_in_runtimepath((name.size ? name.data : ""), flags, find_runtime_cb, &rv); - try_end(err); }); return rv; } @@ -1238,14 +1236,12 @@ void nvim_put(ArrayOf(String) lines, String type, Boolean after, Boolean follow, finish_yankreg_from_object(reg, false); - TRY_WRAP({ - try_start(); + TRY_WRAP(err, { bool VIsual_was_active = VIsual_active; msg_silent++; // Avoid "N more lines" message. do_put(0, reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0); msg_silent--; VIsual_active = VIsual_was_active; - try_end(err); }); cleanup: diff --git a/src/nvim/api/vimscript.c b/src/nvim/api/vimscript.c index af1b23b712..32dcbdec3f 100644 --- a/src/nvim/api/vimscript.c +++ b/src/nvim/api/vimscript.c @@ -137,35 +137,37 @@ Object nvim_eval(String expr, Error *err) static int recursive = 0; // recursion depth Object rv = OBJECT_INIT; - TRY_WRAP({ - // Initialize `force_abort` and `suppress_errthrow` at the top level. - if (!recursive) { - force_abort = false; - suppress_errthrow = false; - did_throw = false; - // `did_emsg` is set by emsg(), which cancels execution. - did_emsg = false; - } - recursive++; - try_start(); + // Initialize `force_abort` and `suppress_errthrow` at the top level. + if (!recursive) { + force_abort = false; + suppress_errthrow = false; + did_throw = false; + // `did_emsg` is set by emsg(), which cancels execution. + did_emsg = false; + } - typval_T rettv; - int ok = eval0(expr.data, &rettv, NULL, true); + recursive++; - if (!try_end(err)) { - if (ok == FAIL) { - // Should never happen, try_end() should get the error. #8371 - api_set_error(err, kErrorTypeException, - "Failed to evaluate expression: '%.*s'", 256, expr.data); - } else { - rv = vim_to_object(&rettv); - } - } + typval_T rettv; + int ok; - tv_clear(&rettv); - recursive--; + TRY_WRAP(err, { + ok = eval0(expr.data, &rettv, NULL, true); }); + if (!ERROR_SET(err)) { + if (ok == FAIL) { + // Should never happen, try_end() (in TRY_WRAP) should get the error. #8371 + api_set_error(err, kErrorTypeException, + "Failed to evaluate expression: '%.*s'", 256, expr.data); + } else { + rv = vim_to_object(&rettv); + } + } + + tv_clear(&rettv); + recursive--; + return rv; } @@ -196,34 +198,37 @@ 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. - if (!recursive) { - force_abort = false; - suppress_errthrow = false; - did_throw = false; - // `did_emsg` is set by emsg(), which cancels execution. - did_emsg = false; - } - recursive++; - try_start(); - typval_T rettv; - funcexe_T funcexe = FUNCEXE_INIT; - funcexe.fe_firstline = curwin->w_cursor.lnum; - funcexe.fe_lastline = curwin->w_cursor.lnum; - funcexe.fe_evaluate = true; - funcexe.fe_selfdict = self; + // Initialize `force_abort` and `suppress_errthrow` at the top level. + if (!recursive) { + force_abort = false; + suppress_errthrow = false; + did_throw = false; + // `did_emsg` is set by emsg(), which cancels execution. + did_emsg = false; + } + recursive++; + + typval_T rettv; + funcexe_T funcexe = FUNCEXE_INIT; + funcexe.fe_firstline = curwin->w_cursor.lnum; + funcexe.fe_lastline = curwin->w_cursor.lnum; + funcexe.fe_evaluate = true; + funcexe.fe_selfdict = self; + + TRY_WRAP(err, { // call_func() retval is deceptive, ignore it. Instead we set `msg_list` // (see above) to capture abort-causing non-exception errors. (void)call_func(fn.data, (int)fn.size, &rettv, (int)args.size, vim_args, &funcexe); - if (!try_end(err)) { - rv = vim_to_object(&rettv); - } - tv_clear(&rettv); - recursive--; }); + if (!ERROR_SET(err)) { + rv = vim_to_object(&rettv); + } + + tv_clear(&rettv); + recursive--; + free_vim_args: while (i > 0) { tv_clear(&vim_args[--i]); diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 7e331a097f..dfbbfe9ab5 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1157,28 +1157,29 @@ int nlua_call(lua_State *lstate) } } - TRY_WRAP({ - // TODO(bfredl): this should be simplified in error handling refactor - force_abort = false; - suppress_errthrow = false; - did_throw = false; - did_emsg = false; - - try_start(); - typval_T rettv; - funcexe_T funcexe = FUNCEXE_INIT; - funcexe.fe_firstline = curwin->w_cursor.lnum; - funcexe.fe_lastline = curwin->w_cursor.lnum; - funcexe.fe_evaluate = true; + // TODO(bfredl): this should be simplified in error handling refactor + force_abort = false; + suppress_errthrow = false; + did_throw = false; + did_emsg = false; + + typval_T rettv; + funcexe_T funcexe = FUNCEXE_INIT; + funcexe.fe_firstline = curwin->w_cursor.lnum; + funcexe.fe_lastline = curwin->w_cursor.lnum; + funcexe.fe_evaluate = true; + + TRY_WRAP(&err, { // call_func() retval is deceptive, ignore it. Instead we set `msg_list` // (TRY_WRAP) to capture abort-causing non-exception errors. (void)call_func((char *)name, (int)name_len, &rettv, nargs, vim_args, &funcexe); - if (!try_end(&err)) { - nlua_push_typval(lstate, &rettv, false); - } - tv_clear(&rettv); }); + if (!ERROR_SET(&err)) { + nlua_push_typval(lstate, &rettv, false); + } + tv_clear(&rettv); + free_vim_args: while (i > 0) { tv_clear(&vim_args[--i]); diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 852e10c8d8..ee5676e927 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -287,10 +287,8 @@ int nlua_regex(lua_State *lstate) const char *text = luaL_checkstring(lstate, 1); regprog_T *prog = NULL; - TRY_WRAP({ - try_start(); + TRY_WRAP(&err, { prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT); - try_end(&err); }); if (ERROR_SET(&err)) { |