refactor(options): fix confusing naming of scope and req_scope (#31317)

Problem: The name `scope` is often used to refer to option flags because
`OPT_LOCAL` and `OPT_GLOBAL` are often used to determine the option
scope. This leads to the name `req_scope` being used for actual option
scopes instead.

Solution: Since the end-goal is to remove `OPT_LOCAL` and `OPT_GLOBAL`
entirely and replace them with `OptScope`, rename `OptScope` variables
to `scope` and the old scope flag variables to `opt_flags`.
This commit is contained in:
Famiu Haque
2024-11-25 15:07:56 +06:00
committed by GitHub
parent 9e7b0bcf51
commit beec377e90
5 changed files with 111 additions and 110 deletions

View File

@@ -636,12 +636,12 @@ void nvim_win_set_option(uint64_t channel_id, Window window, String name, Object
/// Gets the value of a global or local (buffer, window) option.
///
/// @param[in] from Pointer to buffer or window for local option value.
/// @param req_scope Requested option scope. See OptScope in option.h.
/// @param scope Option scope. See OptScope in option.h.
/// @param name The option name.
/// @param[out] err Details of an error that may have occurred.
///
/// @return the option value.
static Object get_option_from(void *from, OptScope req_scope, String name, Error *err)
static Object get_option_from(void *from, OptScope scope, String name, Error *err)
{
VALIDATE_S(name.size > 0, "option name", "<empty>", {
return (Object)OBJECT_INIT;
@@ -650,9 +650,9 @@ static Object get_option_from(void *from, OptScope req_scope, String name, Error
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 == kOptScopeGlobal ? OPT_GLOBAL : OPT_LOCAL,
req_scope, from, err);
if (option_has_scope(opt_idx, scope)) {
value = get_option_value_for(opt_idx, scope == kOptScopeGlobal ? OPT_GLOBAL : OPT_LOCAL,
scope, from, err);
if (ERROR_SET(err)) {
return (Object)OBJECT_INIT;
}
@@ -668,12 +668,12 @@ static Object get_option_from(void *from, OptScope req_scope, String name, Error
/// Sets the value of a global or local (buffer, window) option.
///
/// @param[in] to Pointer to buffer or window for local option value.
/// @param req_scope Requested option scope. See OptScope in option.h.
/// @param scope Option scope. See OptScope in option.h.
/// @param name The option name.
/// @param value New option value.
/// @param[out] err Details of an error that may have occurred.
static void set_option_to(uint64_t channel_id, void *to, OptScope req_scope, String name,
Object value, Error *err)
static void set_option_to(uint64_t channel_id, void *to, OptScope scope, String name, Object value,
Error *err)
{
VALIDATE_S(name.size > 0, "option name", "<empty>", {
return;
@@ -698,12 +698,12 @@ static void set_option_to(uint64_t channel_id, void *to, OptScope req_scope, Str
// For global-win-local options -> setlocal
// For win-local options -> setglobal and setlocal (opt_flags == 0)
const int opt_flags
= (req_scope == kOptScopeWin && !option_has_scope(opt_idx, kOptScopeGlobal))
= (scope == kOptScopeWin && !option_has_scope(opt_idx, kOptScopeGlobal))
? 0
: ((req_scope == kOptScopeGlobal) ? OPT_GLOBAL : OPT_LOCAL);
: ((scope == kOptScopeGlobal) ? OPT_GLOBAL : OPT_LOCAL);
WITH_SCRIPT_CONTEXT(channel_id, {
set_option_value_for(name.data, opt_idx, optval, opt_flags, req_scope, to, err);
set_option_value_for(name.data, opt_idx, optval, opt_flags, scope, to, err);
});
}

View File

@@ -23,15 +23,15 @@
#endif
static int validate_option_value_args(Dict(option) *opts, char *name, OptIndex *opt_idxp,
int *scope, OptScope *req_scope, void **from, char **filetype,
int *opt_flags, OptScope *scope, void **from, char **filetype,
Error *err)
{
#define HAS_KEY_X(d, v) HAS_KEY(d, option, v)
if (HAS_KEY_X(opts, scope)) {
if (!strcmp(opts->scope.data, "local")) {
*scope = OPT_LOCAL;
*opt_flags = OPT_LOCAL;
} else if (!strcmp(opts->scope.data, "global")) {
*scope = OPT_GLOBAL;
*opt_flags = OPT_GLOBAL;
} else {
VALIDATE_EXP(false, "scope", "'local' or 'global'", NULL, {
return FAIL;
@@ -39,14 +39,14 @@ static int validate_option_value_args(Dict(option) *opts, char *name, OptIndex *
}
}
*req_scope = kOptScopeGlobal;
*scope = kOptScopeGlobal;
if (filetype != NULL && HAS_KEY_X(opts, filetype)) {
*filetype = opts->filetype.data;
}
if (HAS_KEY_X(opts, win)) {
*req_scope = kOptScopeWin;
*scope = kOptScopeWin;
*from = find_window_by_handle(opts->win, err);
if (ERROR_SET(err)) {
return FAIL;
@@ -54,12 +54,12 @@ static int validate_option_value_args(Dict(option) *opts, char *name, OptIndex *
}
if (HAS_KEY_X(opts, buf)) {
VALIDATE(!(HAS_KEY_X(opts, scope) && *scope == OPT_GLOBAL), "%s",
VALIDATE(!(HAS_KEY_X(opts, scope) && *opt_flags == OPT_GLOBAL), "%s",
"cannot use both global 'scope' and 'buf'", {
return FAIL;
});
*scope = OPT_LOCAL;
*req_scope = kOptScopeBuf;
*opt_flags = OPT_LOCAL;
*scope = kOptScopeBuf;
*from = find_buffer_by_handle(opts->buf, err);
if (ERROR_SET(err)) {
return FAIL;
@@ -81,10 +81,10 @@ static int validate_option_value_args(Dict(option) *opts, char *name, OptIndex *
if (*opt_idxp == kOptInvalid) {
// unknown option
api_set_error(err, kErrorTypeValidation, "Unknown option '%s'", name);
} else if (*req_scope == kOptScopeBuf || *req_scope == kOptScopeWin) {
} else if (*scope == kOptScopeBuf || *scope == kOptScopeWin) {
// if 'buf' or 'win' is passed, make sure the option supports it
if (!option_has_scope(*opt_idxp, *req_scope)) {
char *tgt = *req_scope == kOptScopeBuf ? "buf" : "win";
if (!option_has_scope(*opt_idxp, *scope)) {
char *tgt = *scope == kOptScopeBuf ? "buf" : "win";
char *global = option_has_scope(*opt_idxp, kOptScopeGlobal) ? "global " : "";
char *req = option_has_scope(*opt_idxp, kOptScopeBuf)
? "buffer-local "
@@ -151,13 +151,13 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
FUNC_API_SINCE(9) FUNC_API_RET_ALLOC
{
OptIndex opt_idx = 0;
int scope = 0;
OptScope req_scope = kOptScopeGlobal;
int opt_flags = 0;
OptScope scope = kOptScopeGlobal;
void *from = NULL;
char *filetype = NULL;
if (!validate_option_value_args(opts, name.data, &opt_idx, &scope, &req_scope, &from, &filetype,
err)) {
if (!validate_option_value_args(opts, name.data, &opt_idx, &opt_flags, &scope, &from,
&filetype, err)) {
return (Object)OBJECT_INIT;
}
@@ -181,7 +181,7 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
from = ftbuf;
}
OptVal value = get_option_value_for(opt_idx, scope, req_scope, from, err);
OptVal value = get_option_value_for(opt_idx, opt_flags, scope, from, err);
if (ftbuf != NULL) {
// restore curwin/curbuf and a few other things
@@ -224,10 +224,11 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
FUNC_API_SINCE(9)
{
OptIndex opt_idx = 0;
int scope = 0;
OptScope req_scope = kOptScopeGlobal;
int opt_flags = 0;
OptScope scope = kOptScopeGlobal;
void *to = NULL;
if (!validate_option_value_args(opts, name.data, &opt_idx, &scope, &req_scope, &to, NULL, err)) {
if (!validate_option_value_args(opts, name.data, &opt_idx, &opt_flags, &scope, &to, NULL,
err)) {
return;
}
@@ -237,9 +238,9 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
// - option is global or local to window (global-local)
//
// Then force scope to local since we don't want to change the global option
if (req_scope == kOptScopeWin && scope == 0) {
if (scope == kOptScopeWin && opt_flags == 0) {
if (option_has_scope(opt_idx, kOptScopeGlobal)) {
scope = OPT_LOCAL;
opt_flags = OPT_LOCAL;
}
}
@@ -255,7 +256,7 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
});
WITH_SCRIPT_CONTEXT(channel_id, {
set_option_value_for(name.data, opt_idx, optval, scope, req_scope, to, err);
set_option_value_for(name.data, opt_idx, optval, opt_flags, scope, to, err);
});
}
@@ -310,16 +311,16 @@ Dict nvim_get_option_info2(String name, Dict(option) *opts, Arena *arena, Error
FUNC_API_SINCE(11)
{
OptIndex opt_idx = 0;
int scope = 0;
OptScope req_scope = kOptScopeGlobal;
int opt_flags = 0;
OptScope scope = kOptScopeGlobal;
void *from = NULL;
if (!validate_option_value_args(opts, name.data, &opt_idx, &scope, &req_scope, &from, NULL,
if (!validate_option_value_args(opts, name.data, &opt_idx, &opt_flags, &scope, &from, NULL,
err)) {
return (Dict)ARRAY_DICT_INIT;
}
buf_T *buf = (req_scope == kOptScopeBuf) ? (buf_T *)from : curbuf;
win_T *win = (req_scope == kOptScopeWin) ? (win_T *)from : curwin;
buf_T *buf = (scope == kOptScopeBuf) ? (buf_T *)from : curbuf;
win_T *win = (scope == kOptScopeWin) ? (win_T *)from : curwin;
return get_vimoption(name, scope, buf, win, arena, err);
return get_vimoption(name, opt_flags, buf, win, arena, err);
}