refactor(options): set option value for non-current context directly

Problem: Currently, we use `switch_option_context` to temporarily switch the current option context before setting an option for a different buffer / window. This is not ideal because we already support getting and setting option values for non-current contexts in the underlying implementation.

Solution: Set option value for non-current context by passing the context directly to the lower level functions. Also introduce a new `OptCtx` struct to store option context information, this will scale much better if we add more option scopes and other context information in the future.
This commit is contained in:
Famiu Haque
2024-11-08 11:57:59 +06:00
parent 98763ce4e9
commit 6257270040
12 changed files with 407 additions and 485 deletions

View File

@@ -157,8 +157,8 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
void *from = NULL;
char *filetype = NULL;
if (!validate_option_value_args(opts, name.data, &opt_idx, &opt_flags, &scope, &from,
&filetype, err)) {
if (!validate_option_value_args(opts, name.data, &opt_idx, &opt_flags, &scope, &from, &filetype,
err)) {
return (Object)OBJECT_INIT;
}
@@ -182,7 +182,7 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
from = ftbuf;
}
OptVal value = get_option_value_for(opt_idx, opt_flags, scope, from, err);
OptVal value = get_option_value_from(opt_idx, option_ctx_from(scope, from), opt_flags);
if (ftbuf != NULL) {
// restore curwin/curbuf and a few other things
@@ -257,7 +257,11 @@ 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, opt_flags, scope, to, err);
const char *errmsg
= set_option_value_for(opt_idx, optval, option_ctx_from(scope, to), opt_flags);
if (errmsg) {
api_set_error(err, kErrorTypeException, "%s", errmsg);
}
});
}