aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval.c
diff options
context:
space:
mode:
authorFamiu Haque <famiuhaque@proton.me>2023-12-07 00:40:48 +0600
committerLewis Russell <me@lewisr.dev>2023-12-07 19:36:34 +0000
commit9ae7d36ff5ebaf75597b442e10890bd77df01fbe (patch)
tree7211492e2fea8e9b87336c3ea69f4c5b2c0b8702 /src/nvim/eval.c
parentb2d471ab337e56f660eb7c89ae24f447f7b7a165 (diff)
downloadrneovim-9ae7d36ff5ebaf75597b442e10890bd77df01fbe.tar.gz
rneovim-9ae7d36ff5ebaf75597b442e10890bd77df01fbe.tar.bz2
rneovim-9ae7d36ff5ebaf75597b442e10890bd77df01fbe.zip
refactor(options): split `get_option_value()` into smaller functions
Problem: Currently, `get_option_value()` returns 3 separate things: The actual value of the option, whether the option is hidden, and the option flags. This makes the function difficult to refactor, modify or otherwise reason about. Solution: Split `get_option_value()` into 3 functions, each with a single purpose. This also affects `get_option_value_for()`.
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r--src/nvim/eval.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index ac1461056c..16c1231682 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -3784,37 +3784,32 @@ int eval_option(const char **const arg, typval_T *const rettv, const bool evalua
}
int ret = OK;
- bool hidden;
char c = *option_end;
*option_end = NUL;
- OptVal value = get_option_value(*arg, NULL, scope, &hidden);
- if (rettv != NULL) {
- switch (value.type) {
- case kOptValTypeNil:
+ bool is_tty_opt = is_tty_option(*arg);
+ int opt_idx = is_tty_opt ? -1 : findoption(*arg);
+
+ if (opt_idx < 0 && !is_tty_opt) {
+ // Only give error if result is going to be used.
+ if (rettv != NULL) {
semsg(_("E113: Unknown option: %s"), *arg);
- ret = FAIL;
- break;
- case kOptValTypeBoolean:
- rettv->v_type = VAR_NUMBER;
- rettv->vval.v_number = value.data.boolean;
- break;
- case kOptValTypeNumber:
- rettv->v_type = VAR_NUMBER;
- rettv->vval.v_number = value.data.number;
- break;
- case kOptValTypeString:
- rettv->v_type = VAR_STRING;
- rettv->vval.v_string = value.data.string.data;
- break;
}
- } else {
- // Value isn't being used, free it.
- optval_free(value);
- if (value.type == kOptValTypeNil || (working && hidden)) {
- ret = FAIL;
+ ret = FAIL;
+ } else if (rettv != NULL) {
+ OptVal value = is_tty_opt ? get_tty_option(*arg) : get_option_value(opt_idx, scope);
+ assert(value.type != kOptValTypeNil);
+
+ *rettv = optval_as_tv(value);
+
+ // Convert boolean option value to number for backwards compatibility.
+ if (rettv->v_type == VAR_BOOL) {
+ rettv->v_type = VAR_NUMBER;
+ rettv->vval.v_number = rettv->vval.v_bool == kBoolVarTrue ? 1 : 0;
}
+ } else if (working && !is_tty_opt && is_option_hidden(opt_idx)) {
+ ret = FAIL;
}
*option_end = c; // put back for error messages