mirror of
https://github.com/neovim/neovim.git
synced 2025-09-05 19:08:15 +00:00
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`
This commit is contained in:

committed by
Lewis Russell

parent
3688a33354
commit
86e54734bf
@@ -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, {
|
||||
|
@@ -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.
|
||||
|
Reference in New Issue
Block a user