Skip to content

Commit

Permalink
refactor(options): option flags enum neovim#30961
Browse files Browse the repository at this point in the history
Problem: Currently we use macros with hardcoded flag values for option flags, which is messy and requires a lot of mental math for adding / removing option flags. Using macros for option flags also means that they cannot be used inside debuggers.

Solution: Create a new `OptFlags` enum that stores all the option flags in an organized way that is easier to understand.
  • Loading branch information
famiu authored Oct 28, 2024
1 parent 0b7cc01 commit 34c44c3
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/nvim/buffer_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ struct window_S {
// transform a pointer to a "onebuf" option into a "allbuf" option
#define GLOBAL_WO(p) ((char *)(p) + sizeof(winopt_T))

// A few options have local flags for P_INSECURE.
// A few options have local flags for kOptFlagInsecure.
uint32_t w_p_stl_flags; // flags for 'statusline'
uint32_t w_p_wbr_flags; // flags for 'winbar'
uint32_t w_p_fde_flags; // flags for 'foldexpr'
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/cmdexpand.c
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ char *addstar(char *fname, size_t len, int context)
/// it.
/// EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
/// EXPAND_FILES After command with EX_XFILE set, or after setting
/// with P_EXPAND set. eg :e ^I, :w>>^I
/// with kOptFlagExpand set. eg :e ^I, :w>>^I
/// EXPAND_DIRECTORIES In some cases this is used instead of the latter
/// when we know only directories are of interest.
/// E.g. :set dir=^I and :cd ^I
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/eval/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ static OptVal tv_to_optval(typval_T *tv, OptIndex opt_idx, const char *option, b
const bool option_has_num = !is_tty_opt && option_has_type(opt_idx, kOptValTypeNumber);
const bool option_has_str = is_tty_opt || option_has_type(opt_idx, kOptValTypeString);

if (!is_tty_opt && (get_option(opt_idx)->flags & P_FUNC) && tv_is_func(*tv)) {
if (!is_tty_opt && (get_option(opt_idx)->flags & kOptFlagFunc) && tv_is_func(*tv)) {
// If the option can be set to a function reference or a lambda
// and the passed value is a function reference, then convert it to
// the name (string) of the function reference.
Expand Down
52 changes: 26 additions & 26 deletions src/nvim/generators/gen_options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ local options = require('options')
local cstr = options.cstr

local redraw_flags = {
ui_option = 'P_UI_OPTION',
tabline = 'P_RTABL',
statuslines = 'P_RSTAT',
current_window = 'P_RWIN',
current_buffer = 'P_RBUF',
all_windows = 'P_RALL',
curswant = 'P_CURSWANT',
highlight_only = 'P_HLONLY',
ui_option = 'kOptFlagUIOption',
tabline = 'kOptFlagRedrTabl',
statuslines = 'kOptFlagRedrStat',
current_window = 'kOptFlagRedrWin',
current_buffer = 'kOptFlagRedrBuf',
all_windows = 'kOptFlagRedrAll',
curswant = 'kOptFlagCurswant',
highlight_only = 'kOptFlagHLOnly',
}

local list_flags = {
comma = 'P_COMMA',
onecomma = 'P_ONECOMMA',
commacolon = 'P_COMMA|P_COLON',
onecommacolon = 'P_ONECOMMA|P_COLON',
flags = 'P_FLAGLIST',
flagscomma = 'P_COMMA|P_FLAGLIST',
comma = 'kOptFlagComma',
onecomma = 'kOptFlagOneComma',
commacolon = 'kOptFlagComma|kOptFlagColon',
onecommacolon = 'kOptFlagOneComma|kOptFlagColon',
flags = 'kOptFlagFlagList',
flagscomma = 'kOptFlagComma|kOptFlagFlagList',
}

--- @param s string
Expand Down Expand Up @@ -61,27 +61,27 @@ local function get_flags(o)
end
end
if o.expand then
add_flag('P_EXPAND')
add_flag('kOptFlagExpand')
if o.expand == 'nodefault' then
add_flag('P_NO_DEF_EXP')
add_flag('kOptFlagNoDefExp')
end
end
for _, flag_desc in ipairs({
{ 'nodefault' },
{ 'no_mkrc' },
{ 'nodefault', 'NoDefault' },
{ 'no_mkrc', 'NoMkrc' },
{ 'secure' },
{ 'gettext' },
{ 'noglob' },
{ 'normal_fname_chars', 'P_NFNAME' },
{ 'normal_dname_chars', 'P_NDNAME' },
{ 'pri_mkrc' },
{ 'deny_in_modelines', 'P_NO_ML' },
{ 'deny_duplicates', 'P_NODUP' },
{ 'modelineexpr', 'P_MLE' },
{ 'noglob', 'NoGlob' },
{ 'normal_fname_chars', 'NFname' },
{ 'normal_dname_chars', 'NDname' },
{ 'pri_mkrc', 'PriMkrc' },
{ 'deny_in_modelines', 'NoML' },
{ 'deny_duplicates', 'NoDup' },
{ 'modelineexpr', 'MLE' },
{ 'func' },
}) do
local key_name = flag_desc[1]
local def_name = flag_desc[2] or ('P_' .. key_name:upper())
local def_name = 'kOptFlag' .. (flag_desc[2] or lowercase_to_titlecase(key_name))
if o[key_name] then
add_flag(def_name)
end
Expand Down
Loading

0 comments on commit 34c44c3

Please sign in to comment.