Merge pull request #13173 from tjdevries/tjdevries/option_info

api: add option metadata
This commit is contained in:
Björn Linse
2020-12-04 13:45:32 +01:00
committed by GitHub
6 changed files with 247 additions and 14 deletions

View File

@@ -16,6 +16,7 @@
#define BOOLEAN_OBJ(b) ((Object) { \
.type = kObjectTypeBoolean, \
.data.boolean = b })
#define BOOL(b) BOOLEAN_OBJ(b)
#define INTEGER_OBJ(i) ((Object) { \
.type = kObjectTypeInteger, \
@@ -29,6 +30,8 @@
.type = kObjectTypeString, \
.data.string = s })
#define CSTR_TO_OBJ(s) STRING_OBJ(cstr_to_string(s))
#define BUFFER_OBJ(s) ((Object) { \
.type = kObjectTypeBuffer, \
.data.integer = s })
@@ -59,6 +62,8 @@
#define PUT(dict, k, v) \
kv_push(dict, ((KeyValuePair) { .key = cstr_to_string(k), .value = v }))
#define PUT_BOOL(dict, name, condition) PUT(dict, name, BOOLEAN_OBJ(condition));
#define ADD(array, item) \
kv_push(array, item)

View File

@@ -970,6 +970,47 @@ Object nvim_get_option(String name, Error *err)
return get_option_from(NULL, SREQ_GLOBAL, name, err);
}
/// Gets the option information for all options.
///
/// The dictionary has the full option names as keys and option metadata
/// dictionaries as detailed at |nvim_get_option_info|.
///
/// @return dictionary of all options
Dictionary nvim_get_all_options_info(Error *err)
FUNC_API_SINCE(7)
{
return get_all_vimoptions();
}
/// Gets the option information for one option
///
/// Resulting dictionary has keys:
/// - name: Name of the option (like 'filetype')
/// - shortname: Shortened name of the option (like 'ft')
/// - type: type of option ("string", "integer" or "boolean")
/// - default: The default value for the option
/// - was_set: Whether the option was set.
///
/// - last_set_sid: Last set script id (if any)
/// - last_set_linenr: line number where option was set
/// - last_set_chan: Channel where option was set (0 for local)
///
/// - scope: one of "global", "win", or "buf"
/// - global_local: whether win or buf option has a global value
///
/// - commalist: List of comma separated values
/// - flaglist: List of single char flags
///
///
/// @param name Option name
/// @param[out] err Error details, if any
/// @return Option Information
Dictionary nvim_get_option_info(String name, Error *err)
FUNC_API_SINCE(7)
{
return get_vimoption(name, err);
}
/// Sets an option value.
///
/// @param channel_id

View File

@@ -352,9 +352,6 @@ void set_init_1(bool clean_arg)
langmap_init();
// Be nocompatible
p_cp = false;
/*
* Find default value for 'shell' option.
* Don't use it if it is empty.
@@ -683,7 +680,7 @@ set_options_default(
{
for (int i = 0; options[i].fullname; i++) {
if (!(options[i].flags & P_NODEFAULT)) {
set_option_default(i, opt_flags, p_cp);
set_option_default(i, opt_flags, false);
}
}
@@ -763,7 +760,7 @@ void set_init_2(bool headless)
// which results in the actual value computed from the window height.
idx = findoption("scroll");
if (idx >= 0 && !(options[idx].flags & P_WAS_SET)) {
set_option_default(idx, OPT_LOCAL, p_cp);
set_option_default(idx, OPT_LOCAL, false);
}
comp_col();
@@ -1113,7 +1110,7 @@ int do_set(
if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL) {
arg += len;
cp_val = p_cp;
cp_val = false;
if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i') {
if (arg[3] == 'm') { // "opt&vim": set to Vim default
cp_val = false;
@@ -3617,10 +3614,14 @@ static void set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
{
int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
int indir = (int)options[opt_idx].indir;
const LastSet last_set = { .script_ctx =
{ script_ctx.sc_sid, script_ctx.sc_seq,
script_ctx.sc_lnum + sourcing_lnum },
current_channel_id };
const LastSet last_set = {
.script_ctx = {
script_ctx.sc_sid,
script_ctx.sc_seq,
script_ctx.sc_lnum + sourcing_lnum
},
current_channel_id
};
// Remember where the option was set. For local options need to do that
// in the buffer or window structure.
@@ -4950,7 +4951,7 @@ static int optval_default(vimoption_T *p, char_u *varp)
if (varp == NULL) {
return true; // hidden option is always at default
}
dvi = ((p->flags & P_VI_DEF) || p_cp) ? VI_DEFAULT : VIM_DEFAULT;
dvi = (p->flags & P_VI_DEF) ? VI_DEFAULT : VIM_DEFAULT;
if (p->flags & P_NUM) {
return *(long *)varp == (long)(intptr_t)p->def_val[dvi];
}
@@ -7174,3 +7175,75 @@ long get_sidescrolloff_value(void)
{
return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
}
Dictionary get_vimoption(String name, Error *err)
{
int opt_idx = findoption_len((const char *)name.data, name.size);
if (opt_idx < 0) {
api_set_error(err, kErrorTypeValidation, "no such option: '%s'", name.data);
return (Dictionary)ARRAY_DICT_INIT;
}
return vimoption2dict(&options[opt_idx]);
}
Dictionary get_all_vimoptions(void)
{
Dictionary retval = ARRAY_DICT_INIT;
for (size_t i = 0; i < PARAM_COUNT; i++) {
Dictionary opt_dict = vimoption2dict(&options[i]);
PUT(retval, options[i].fullname, DICTIONARY_OBJ(opt_dict));
}
return retval;
}
static Dictionary vimoption2dict(vimoption_T *opt)
{
Dictionary dict = ARRAY_DICT_INIT;
PUT(dict, "name", CSTR_TO_OBJ(opt->fullname));
PUT(dict, "shortname", CSTR_TO_OBJ(opt->shortname));
const char *scope;
if (opt->indir & PV_BUF) {
scope = "buf";
} else if (opt->indir & PV_WIN) {
scope = "win";
} else {
scope = "global";
}
PUT(dict, "scope", CSTR_TO_OBJ(scope));
// welcome to the jungle
PUT(dict, "global_local", BOOL(opt->indir & PV_BOTH));
PUT(dict, "commalist", BOOL(opt->flags & P_COMMA));
PUT(dict, "flaglist", BOOL(opt->flags & P_FLAGLIST));
PUT(dict, "was_set", BOOL(opt->flags & P_WAS_SET));
PUT(dict, "last_set_sid", INTEGER_OBJ(opt->last_set.script_ctx.sc_sid));
PUT(dict, "last_set_linenr", INTEGER_OBJ(opt->last_set.script_ctx.sc_lnum));
PUT(dict, "last_set_chan", INTEGER_OBJ((int64_t)opt->last_set.channel_id));
const char *type;
Object def;
// TODO(bfredl): do you even nocp?
char_u *def_val = opt->def_val[(opt->flags & P_VI_DEF)
? VI_DEFAULT : VIM_DEFAULT];
if (opt->flags & P_STRING) {
type = "string";
def = CSTR_TO_OBJ(def_val ? (char *)def_val : "");
} else if (opt->flags & P_NUM) {
type = "number";
def = INTEGER_OBJ((Integer)(intptr_t)def_val);
} else if (opt->flags & P_BOOL) {
type = "boolean";
def = BOOL((intptr_t)def_val);
} else {
type = ""; def = NIL;
}
PUT(dict, "type", CSTR_TO_OBJ(type));
PUT(dict, "default", def);
return dict;
}

View File

@@ -372,7 +372,6 @@ EXTERN long p_cwh; // 'cmdwinheight'
EXTERN long p_ch; // 'cmdheight'
EXTERN long p_columns; // 'columns'
EXTERN int p_confirm; // 'confirm'
EXTERN int p_cp; // 'compatible'
EXTERN char_u *p_cot; // 'completeopt'
# ifdef BACKSLASH_IN_FILENAME
EXTERN char_u *p_csl; // 'completeslash'