From 1f8fb7c00048bc217bb9a2bf29c58630d1810d6e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 15 Jun 2023 12:36:21 +0800 Subject: [PATCH] fix(:let): fix error when applying operator to boolean option (#24030) --- src/nvim/eval/vars.c | 9 ++++----- test/functional/vimscript/let_spec.lua | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 35ae558006..9b6427fef7 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -817,12 +817,11 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const, new_n = num_modulus(cur_n, new_n); break; } - // clamp boolean values - if (newval.type == kOptValTypeBoolean && (new_n > 1 || new_n < -1)) { - new_n = (new_n > 1) ? 1 : -1; + if (curval.type == kOptValTypeNumber) { + newval = NUMBER_OPTVAL(new_n); + } else { + newval = BOOLEAN_OPTVAL(new_n == 0 ? kFalse : (new_n >= 1 ? kTrue : kNone)); } - - newval = kOptValTypeNumber ? NUMBER_OPTVAL(new_n) : BOOLEAN_OPTVAL((TriState)new_n); } else if (!hidden && is_string && curval.data.string.data != NULL && newval.data.string.data != NULL) { // string OptVal newval_old = newval; diff --git a/test/functional/vimscript/let_spec.lua b/test/functional/vimscript/let_spec.lua index 164fa86452..11417c5846 100644 --- a/test/functional/vimscript/let_spec.lua +++ b/test/functional/vimscript/let_spec.lua @@ -92,6 +92,20 @@ describe(':let', function() ]]) eq(1, eval('1')) end) + + it('can apply operator to boolean option', function() + eq(true, meths.get_option_value('equalalways', {})) + command('let &equalalways -= 1') + eq(false, meths.get_option_value('equalalways', {})) + command('let &equalalways += 1') + eq(true, meths.get_option_value('equalalways', {})) + command('let &equalalways *= 1') + eq(true, meths.get_option_value('equalalways', {})) + command('let &equalalways /= 1') + eq(true, meths.get_option_value('equalalways', {})) + command('let &equalalways %= 1') + eq(false, meths.get_option_value('equalalways', {})) + end) end) describe(':let and :const', function()