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

refactor(options): generate available options values from lua #28659

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/nvim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ set(GENERATED_KEYSETS_DEFS ${GENERATED_DIR}/keysets_defs.generated.h)
set(GENERATED_OPTIONS ${GENERATED_DIR}/options.generated.h)
set(GENERATED_OPTIONS_ENUM ${GENERATED_DIR}/options_enum.generated.h)
set(GENERATED_OPTIONS_MAP ${GENERATED_DIR}/options_map.generated.h)
set(GENERATED_OPTIONS_VALUES ${GENERATED_DIR}/options_values.generated.h)
set(GENERATED_UI_EVENTS_CALL ${GENERATED_DIR}/ui_events_call.generated.h)
set(GENERATED_UI_EVENTS_CLIENT ${GENERATED_DIR}/ui_events_client.generated.h)
set(GENERATED_UI_EVENTS_REMOTE ${GENERATED_DIR}/ui_events_remote.generated.h)
Expand Down Expand Up @@ -642,6 +643,7 @@ list(APPEND NVIM_GENERATED_FOR_HEADERS
"${GENERATED_EVENTS_ENUM}"
"${GENERATED_KEYSETS_DEFS}"
"${GENERATED_OPTIONS_ENUM}"
"${GENERATED_OPTIONS_VALUES}"
)

list(APPEND NVIM_GENERATED_FOR_SOURCES
Expand Down Expand Up @@ -682,6 +684,11 @@ add_custom_command(OUTPUT ${GENERATED_OPTIONS_ENUM} ${GENERATED_OPTIONS_MAP}
DEPENDS ${LUA_GEN_DEPS} ${OPTIONS_ENUM_GENERATOR} ${CMAKE_CURRENT_LIST_DIR}/options.lua
)

add_custom_command(OUTPUT ${GENERATED_OPTIONS_VALUES}
COMMAND ${LUA_GEN} ${OPTIONS_GENERATOR} ${GENERATED_OPTIONS_VALUES}
DEPENDS ${LUA_GEN_DEPS} ${OPTIONS_GENERATOR} ${CMAKE_CURRENT_LIST_DIR}/options.lua
)

# NVIM_GENERATED_FOR_SOURCES and NVIM_GENERATED_FOR_HEADERS must be mutually exclusive.
foreach(hfile ${NVIM_GENERATED_FOR_HEADERS})
list(FIND NVIM_GENERATED_FOR_SOURCES ${hfile} hfile_idx)
Expand Down
59 changes: 54 additions & 5 deletions src/nvim/generators/gen_options.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
local options_file = arg[1]

local gen_values = options_file:find('options_values') and true or false

Comment on lines +3 to +4
Copy link
Member

@famiu famiu May 7, 2024

Choose a reason for hiding this comment

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

I don't like this pattern of using the same generator for 2 completely different purposes. Use a separate generator file for this, similar to what gen_options_enum.lua does

Copy link
Member

Choose a reason for hiding this comment

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

These are not "complete different purposes". And why is gen_options_enum.lua in a separate file?

Copy link
Member

@famiu famiu May 7, 2024

Choose a reason for hiding this comment

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

These are not "complete different purposes".

The script does two very different things depending on which file it's being called on, that seems different to me. A script's behavior depending on the filename is certainly not ideal.

And why is gen_options_enum.lua in a separate file?

Precisely because it does a completely different thing. gen_options generates the options[] array, gen_options_enum generates the option index enum (using information from the options[] array, so order matters here) and the hash table for findoption()

local opt_fd = assert(io.open(options_file, 'w'))

local w = function(s)
if s:match('^ %.') then
opt_fd:write(s .. ',\n')
else
opt_fd:write(s .. '\n')
local w = function(s, ...)
local is_values = select(1, ...) or false
if gen_values and is_values then
opt_fd:write(s)
elseif not gen_values then
opt_fd:write(s .. (s:match('^ %.') and ',\n' or '\n'))
end
end

Expand Down Expand Up @@ -148,6 +151,49 @@ local get_defaults = function(d, n)
return value_dumpers[type(d)](d)
end

---@param opt vim.option_meta
local function generate_valid_values(opt)
local item -- [[string | nil]]
local child_item -- [[string | nil]]
local abbr = opt.abbreviation
local varname = opt.varname or ('p_' .. abbr)
if abbr == 'bh' or abbr == 'bt' or abbr == 'sbo' then
local field = abbr == 'bh' and 'bufhidden' or (abbr == 'sbo' and 'scbopt' or 'buftype')
item = ('static char *(p_%s_values[]) = { '):format(field)
else
item = ('static char *(%s_values[]) = { '):format(varname)
end
if opt.enable_if then
item = '#ifdef ' .. opt.enable_if .. '\n' .. item
end
for idx, val in ipairs(opt.valid_values) do
if type(val) == 'string' then
item = item .. '"' .. val .. '", '
else
for j, k in pairs(val) do
child_item = ('static char *(p_%s_%s_values[]) = { '):format(opt.abbreviation, j)
for subidx, subval in ipairs(k) do
child_item = child_item .. '"' .. subval .. '", '
if subidx == #k then
child_item = ('%s NULL };\n'):format(child_item)
end
end
end
end

if idx == #opt.valid_values then
item = item .. ' NULL };\n'
if opt.enable_if then
item = item .. '#endif\n'
end
end
end
w(item, true)
if child_item then
w(child_item, true)
end
end

--- @type {[1]:string,[2]:string}[]
local defines = {}

Expand Down Expand Up @@ -228,6 +274,9 @@ local function dump_option(i, o)
w('#endif')
end
end
if gen_values and o.valid_values then
generate_valid_values(o)
end
w(' },')
end

Expand Down