refactor(options): reduce findoption() usage

Problem: Many places in the code use `findoption()` to access an option using its name, even if the option index is available. This is very slow because it requires looping through the options array over and over.

Solution: Use option index instead of name wherever possible. Also introduce an `OptIndex` enum which contains the index for every option as enum constants, this eliminates the need to pass static option names as strings.
This commit is contained in:
Famiu Haque
2023-12-07 23:46:57 +06:00
parent 29aa4dd10a
commit 6346987601
39 changed files with 336 additions and 352 deletions

View File

@@ -1,4 +1,5 @@
local options_file = arg[1]
local options_enum_file = arg[2]
local opt_fd = assert(io.open(options_file, 'w'))
@@ -41,6 +42,12 @@ local list_flags = {
flagscomma = 'P_COMMA|P_FLAGLIST',
}
--- @param s string
--- @return string
local title_case = function(s)
return s:sub(1, 1):upper() .. s:sub(2):lower()
end
--- @param o vim.option_meta
--- @return string
local function get_flags(o)
@@ -229,4 +236,14 @@ w('')
for _, v in ipairs(defines) do
w('#define ' .. v[1] .. ' ' .. v[2])
end
-- Generate options enum file
opt_fd = assert(io.open(options_enum_file, 'w'))
w('typedef enum {')
for i, o in ipairs(options.options) do
w((' kOpt%s = %u,'):format(title_case(o.full_name), i - 1))
end
w('} OptIndex;')
opt_fd:close()