From 6346987601a28b00564295ee8be0a8b00d9ff911 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Thu, 7 Dec 2023 23:46:57 +0600 Subject: 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. --- src/nvim/generators/gen_options.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/nvim/generators') diff --git a/src/nvim/generators/gen_options.lua b/src/nvim/generators/gen_options.lua index 3a355634f3..2b17add7ba 100644 --- a/src/nvim/generators/gen_options.lua +++ b/src/nvim/generators/gen_options.lua @@ -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() -- cgit