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/gen_options.lua') 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 From bf3bc1cec9f00b9644815001a8732ecedf3ce07f Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Thu, 7 Dec 2023 23:59:30 +0600 Subject: refactor(options): convert `opt_idx` variables to `OptIndex` --- src/nvim/generators/gen_options.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/generators/gen_options.lua') diff --git a/src/nvim/generators/gen_options.lua b/src/nvim/generators/gen_options.lua index 2b17add7ba..7c5fc08129 100644 --- a/src/nvim/generators/gen_options.lua +++ b/src/nvim/generators/gen_options.lua @@ -241,6 +241,7 @@ end opt_fd = assert(io.open(options_enum_file, 'w')) w('typedef enum {') +w(' kOptInvalid = -1,') for i, o in ipairs(options.options) do w((' kOpt%s = %u,'):format(title_case(o.full_name), i - 1)) end -- cgit From a34cc1a44de75eff4c6b43f983dc983eb283119d Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Fri, 8 Dec 2023 12:36:37 +0600 Subject: refactor(options): define `kOptIndexCount` Add a macro to indicate the option count so that we can iterate through the options[] table more clearly. This also removes the need for having an option with NULL fullname at the end of `options[]`. --- src/nvim/generators/gen_options.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/nvim/generators/gen_options.lua') diff --git a/src/nvim/generators/gen_options.lua b/src/nvim/generators/gen_options.lua index 7c5fc08129..b7356a7bb1 100644 --- a/src/nvim/generators/gen_options.lua +++ b/src/nvim/generators/gen_options.lua @@ -44,8 +44,8 @@ local list_flags = { --- @param s string --- @return string -local title_case = function(s) - return s:sub(1, 1):upper() .. s:sub(2):lower() +local lowercase_to_titlecase = function(s) + return s:sub(1, 1):upper() .. s:sub(2) end --- @param o vim.option_meta @@ -229,7 +229,6 @@ static vimoption_T options[] = {]]) for i, o in ipairs(options.options) do dump_option(i, o) end -w(' [' .. ('%u'):format(#options.options) .. ']={.fullname=NULL}') w('};') w('') @@ -242,9 +241,13 @@ opt_fd = assert(io.open(options_enum_file, 'w')) w('typedef enum {') w(' kOptInvalid = -1,') + for i, o in ipairs(options.options) do - w((' kOpt%s = %u,'):format(title_case(o.full_name), i - 1)) + w((' kOpt%s = %u,'):format(lowercase_to_titlecase(o.full_name), i - 1)) end + +w(' // Option count, used when iterating through options') +w('#define kOptIndexCount ' .. tostring(#options.options)) w('} OptIndex;') opt_fd:close() -- cgit