diff options
author | Famiu Haque <famiuhaque@proton.me> | 2024-10-29 03:21:33 +0600 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-11-02 15:53:49 +0000 |
commit | 86e54734bf9e05605d8b7146e7b0e79025138ba2 (patch) | |
tree | 3d59b1d7266f48cf869777c72c330fdaa87fba75 | |
parent | 3688a333544251c887d78e6501eec55f0fb685f8 (diff) | |
download | rneovim-86e54734bf9e05605d8b7146e7b0e79025138ba2.tar.gz rneovim-86e54734bf9e05605d8b7146e7b0e79025138ba2.tar.bz2 rneovim-86e54734bf9e05605d8b7146e7b0e79025138ba2.zip |
refactor(options): remove `get_option_value_strict`
Problem: `get_option_value_for` can perfectly replace `get_option_value_strict`, making the latter redundant.
Solution: Remove `get_option_value_strict`
-rw-r--r-- | src/nvim/api/deprecated.c | 46 | ||||
-rw-r--r-- | src/nvim/option.c | 70 |
2 files changed, 43 insertions, 73 deletions
diff --git a/src/nvim/api/deprecated.c b/src/nvim/api/deprecated.c index 6376011106..5c0d9c0cea 100644 --- a/src/nvim/api/deprecated.c +++ b/src/nvim/api/deprecated.c @@ -633,6 +633,40 @@ void nvim_win_set_option(uint64_t channel_id, Window window, String name, Object set_option_to(channel_id, win, kOptReqWin, name, value, err); } +/// Check if option has a value in the requested scope. +/// +/// @param opt_idx Option index in options[] table. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// +/// @return true if option has a value in the requested scope, false otherwise. +static bool option_has_scope(OptIndex opt_idx, OptReqScope req_scope) +{ + if (opt_idx == kOptInvalid) { + return false; + } + + vimoption_T *opt = get_option(opt_idx); + + // Hidden option. + if (opt->var == NULL) { + return false; + } + // TTY option. + if (is_tty_option(opt->fullname)) { + return req_scope == kOptReqGlobal; + } + + switch (req_scope) { + case kOptReqGlobal: + return opt->var != VAR_WIN; + case kOptReqBuf: + return opt->indir & PV_BUF; + case kOptReqWin: + return opt->indir & PV_WIN; + } + UNREACHABLE; +} + /// Gets the value of a global or local (buffer, window) option. /// /// @param[in] from Pointer to buffer or window for local option value. @@ -647,9 +681,15 @@ static Object get_option_from(void *from, OptReqScope req_scope, String name, Er return (Object)OBJECT_INIT; }); - OptVal value = get_option_value_strict(find_option(name.data), req_scope, from, err); - if (ERROR_SET(err)) { - return (Object)OBJECT_INIT; + OptIndex opt_idx = find_option(name.data); + OptVal value = NIL_OPTVAL; + + if (option_has_scope(opt_idx, req_scope)) { + value = get_option_value_for(opt_idx, req_scope == kOptReqGlobal ? OPT_GLOBAL : OPT_LOCAL, + req_scope, from, err); + if (ERROR_SET(err)) { + return (Object)OBJECT_INIT; + } } VALIDATE_S(value.type != kOptValTypeNil, "option name", name.data, { diff --git a/src/nvim/option.c b/src/nvim/option.c index a8ec5b2919..a7e56d6d39 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -3978,76 +3978,6 @@ int get_option_attrs(OptIndex opt_idx) return attrs; } -/// Check if option has a value in the requested scope. -/// -/// @param opt_idx Option index in options[] table. -/// @param req_scope Requested option scope. See OptReqScope in option.h. -/// -/// @return true if option has a value in the requested scope, false otherwise. -static bool option_has_scope(OptIndex opt_idx, OptReqScope req_scope) -{ - if (opt_idx == kOptInvalid) { - return false; - } - - vimoption_T *opt = get_option(opt_idx); - - // Hidden option. - if (opt->var == NULL) { - return false; - } - // TTY option. - if (is_tty_option(opt->fullname)) { - return req_scope == kOptReqGlobal; - } - - switch (req_scope) { - case kOptReqGlobal: - return opt->var != VAR_WIN; - case kOptReqBuf: - return opt->indir & PV_BUF; - case kOptReqWin: - return opt->indir & PV_WIN; - } - UNREACHABLE; -} - -/// Get the option value in the requested scope. -/// -/// @param opt_idx Option index in options[] table. -/// @param req_scope Requested option scope. See OptReqScope in option.h. -/// @param[in] from Pointer to buffer or window for local option value. -/// @param[out] err Error message, if any. -/// -/// @return Option value in the requested scope. Returns a Nil option value if option is not found, -/// hidden or if it isn't present in the requested scope. (i.e. has no global, window-local or -/// buffer-local value depending on opt_scope). -OptVal get_option_value_strict(OptIndex opt_idx, OptReqScope req_scope, void *from, Error *err) -{ - if (opt_idx == kOptInvalid || !option_has_scope(opt_idx, req_scope)) { - return NIL_OPTVAL; - } - - vimoption_T *opt = get_option(opt_idx); - switchwin_T switchwin; - aco_save_T aco; - void *ctx = req_scope == kOptReqWin ? (void *)&switchwin - : (req_scope == kOptReqBuf ? (void *)&aco : NULL); - bool switched = switch_option_context(ctx, req_scope, from, err); - if (ERROR_SET(err)) { - return NIL_OPTVAL; - } - - char *varp = get_varp_scope(opt, req_scope == kOptReqGlobal ? OPT_GLOBAL : OPT_LOCAL); - OptVal retv = optval_from_varp(opt_idx, varp); - - if (switched) { - restore_option_context(ctx, req_scope); - } - - return retv; -} - /// Get option value for buffer / window. /// /// @param opt_idx Option index in options[] table. |