Skip to content

Commit

Permalink
refactor(options): generate available options values from lua
Browse files Browse the repository at this point in the history
Problem: currently the available values of options is split from options meta lua file.

Solution: generate it from lua file.
  • Loading branch information
glepnir committed May 8, 2024
1 parent b024643 commit 8920fd4
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 80 deletions.
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

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 = nil -- [[string|nil]]
local child_item = nil -- [[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 = child_item .. ' NULL };\n'
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

0 comments on commit 8920fd4

Please sign in to comment.