Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): make nil value in nvim_set_option_value clear local value #16710

Merged
merged 3 commits into from
Dec 21, 2021

Conversation

gpanders
Copy link
Member

@gpanders gpanders commented Dec 18, 2021

Fix a bug in nvim_set_option_value that would set number and boolean options
to 0 or false respectively when passing nil as the value. A value of
nil should instead clear the local value, which has different meanings for
different option types:

String options: set the local value to the empty string ("") which has the
effect of using the global value of the option for global-local options.

Number and boolean options: set the local value to the global value.

TODO:

  • Handle global-local number options (autoread, scrolloff, undolevels)

@bfredl
Copy link
Member

bfredl commented Dec 18, 2021

Does this work for global-local bool/number options? I e autoread, undolevel and scrolloff should the sentinel value set, rather than copy from global value.

@gpanders
Copy link
Member Author

Does this work for global-local bool/number options? I e autoread, undolevel and scrolloff should the sentinel value set, rather than copy from global value.

Thanks, forgot about these. These will need to be specially handled unfortunately, as they are in do_set right now.

@gpanders gpanders marked this pull request as draft December 18, 2021 18:01
For special options such as 'undolevels' and 'scrolloff', this sets the
local value to the special "unset" value (e.g. -12345 for 'undolevels').
@gpanders gpanders marked this pull request as ready for review December 19, 2021 03:22
@gpanders gpanders requested a review from bfredl December 19, 2021 03:22
rv = BOOLEAN_OBJ(numval);
break;
default:
rv = INTEGER_OBJ(numval);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using NIL to represent unset autoread value.

Copy link
Member Author

@gpanders gpanders Dec 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 is used in do_set():

neovim/src/nvim/option.c

Lines 1211 to 1218 in abb2b1a

// For 'autoread' -1 means to use global value.
if ((int *)varp == &curbuf->b_p_ar
&& opt_flags == OPT_LOCAL) {
value = -1;
} else {
value = *(int *)get_varp_scope(&(options[opt_idx]),
OPT_GLOBAL);
}

:set autoread
:setl autoread<
:echo &l:autoread
-1

Shouldn't we try and keep the API version consistent with the Vim script version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what congruence? &l:autoread always returns an integer value, never a bool, so already using booleans breaks that rule in that case. vim script cannot change it to neither v:true / v:false nor v:null due to backwards compatibility, while I think API functions should use API types whenever possible/reasonable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok done.

@tjdevries
Copy link
Contributor

yes please, if we can make the API return bools instead of ints, that would be GREAT for the common case of:

if not vim.opt.xyz:get() then do_something() end

where xyz always returns 0 or 1.... but that's never falsey in lua.

@gpanders gpanders requested a review from bfredl December 20, 2021 14:41
@gpanders gpanders merged commit 33cd1ba into neovim:master Dec 21, 2021
@gpanders gpanders deleted the nvim_set_option_value_nil branch December 21, 2021 21:20
@github-actions
Copy link
Contributor

Backport failed for release-0.6, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git fetch origin release-0.6
git worktree add -d .worktree/backport-16710-to-release-0.6 origin/release-0.6
cd .worktree/backport-16710-to-release-0.6
git checkout -b backport-16710-to-release-0.6
ancref=$(git merge-base b42e0c40c8b8e906eccb14db0d0648a39e49c363 e5e7834e88db9fecad00565fb59ba1a6a7cc1c8b)
git cherry-pick -x $ancref..e5e7834e88db9fecad00565fb59ba1a6a7cc1c8b

@gpanders
Copy link
Member Author

Ah, this shouldn't be backported since #15996 was not backported.

@dundargoc dundargoc removed the request for review from bfredl October 29, 2022 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api libnvim, Nvim RPC API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants