From 4dc4cf346755375e49410e16635c00a602b26c36 Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Wed, 7 Sep 2022 17:59:27 +0200 Subject: fix(options): mark `winhighlight` as list style (#19477) Also add missing fcs, lcs and winhighlight to list of key-value options for `vim.opt`. Co-authored-by: ii14 --- runtime/lua/vim/_meta.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index f1652718ee..0f45c916dc 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -198,7 +198,10 @@ end -- Can be done in a separate PR. local key_value_options = { fillchars = true, + fcs = true, listchars = true, + lcs = true, + winhighlight = true, winhl = true, } -- cgit From bc88691dbd2f5352d8793cac952bf90eea6442b2 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 6 Sep 2022 22:51:32 +0100 Subject: refactor(vim.opt): remove del arg --- runtime/lua/vim/_meta.lua | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 0f45c916dc..5dbadd3ff4 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -43,7 +43,7 @@ local function _setup() win_options = get_scoped_options('win') end -local function make_meta_accessor(get, set, del, validator) +local function make_meta_accessor(get, set, validator) validator = validator or function() return true end @@ -51,7 +51,6 @@ local function make_meta_accessor(get, set, del, validator) validate({ get = { get, 'f' }, set = { set, 'f' }, - del = { del, 'f', true }, validator = { validator, 'f' }, }) @@ -61,9 +60,6 @@ local function make_meta_accessor(get, set, del, validator) return end - if del and v == nil then - return del(k) - end return set(k, v) end function mt:__index(k) @@ -98,7 +94,7 @@ do -- buffer option accessor return a.nvim_set_option_value(k, v, { buf = bufnr or 0 }) end - return make_meta_accessor(get, set, nil, function(k) + return make_meta_accessor(get, set, function(k) if type(k) == 'string' then _setup() if win_options[k] then @@ -132,7 +128,7 @@ do -- window option accessor return a.nvim_set_option_value(k, v, { win = winnr or 0 }) end - return make_meta_accessor(get, set, nil, function(k) + return make_meta_accessor(get, set, function(k) if type(k) == 'string' then _setup() if buf_options[k] then -- cgit From f21e2a51ba43a06d929ca372bf5cfce40d4e4331 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 6 Sep 2022 22:56:55 +0100 Subject: refactor(vim.opt): remove enums --- runtime/lua/vim/_meta.lua | 124 +++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 79 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 5dbadd3ff4..fb3a1e72e9 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -4,12 +4,6 @@ local vim = assert(vim) local a = vim.api local validate = vim.validate -local SET_TYPES = setmetatable({ - SET = 0, - LOCAL = 1, - GLOBAL = 2, -}, { __index = error }) - local options_info = nil local buf_options = nil local glb_options = nil @@ -201,51 +195,30 @@ local key_value_options = { winhl = true, } ----@class OptionTypes ---- Option Type Enum -local OptionTypes = setmetatable({ - BOOLEAN = 0, - NUMBER = 1, - STRING = 2, - ARRAY = 3, - MAP = 4, - SET = 5, -}, { - __index = function(_, k) - error('Not a valid OptionType: ' .. k) - end, - __newindex = function(_, k) - error('Cannot set a new OptionType: ' .. k) - end, -}) - --- Convert a vimoption_T style dictionary to the correct OptionType associated with it. ----@return OptionType +---@return string local get_option_type = function(name, info) if info.type == 'boolean' then - return OptionTypes.BOOLEAN + return 'boolean' elseif info.type == 'number' then - return OptionTypes.NUMBER + return 'number' elseif info.type == 'string' then if not info.commalist and not info.flaglist then - return OptionTypes.STRING + return 'string' end if key_value_options[name] then assert(info.commalist, 'Must be a comma list to use key:value style') - return OptionTypes.MAP + return 'map' end if info.flaglist then - return OptionTypes.SET + return 'set' elseif info.commalist then - return OptionTypes.ARRAY + return 'array' end - - error('Fallthrough in OptionTypes') - else - error('Not a known info.type:' .. info.type) end + error('Not a known info.type:' .. info.type) end -- Check whether the OptionTypes is allowed for vim.opt @@ -269,12 +242,12 @@ local function assert_valid_value(name, value, types) end local valid_types = { - [OptionTypes.BOOLEAN] = { 'boolean' }, - [OptionTypes.NUMBER] = { 'number' }, - [OptionTypes.STRING] = { 'string' }, - [OptionTypes.SET] = { 'string', 'table' }, - [OptionTypes.ARRAY] = { 'string', 'table' }, - [OptionTypes.MAP] = { 'string', 'table' }, + boolean = { 'boolean' }, + number = { 'number' }, + string = { 'string' }, + set = { 'string', 'table' }, + array = { 'string', 'table' }, + map = { 'string', 'table' }, } --- Convert a lua value to a vimoption_T value @@ -282,17 +255,17 @@ local convert_value_to_vim = (function() -- Map of functions to take a Lua style value and convert to vimoption_T style value. -- Each function takes (info, lua_value) -> vim_value local to_vim_value = { - [OptionTypes.BOOLEAN] = function(_, value) + boolean = function(_, value) return value end, - [OptionTypes.NUMBER] = function(_, value) + number = function(_, value) return value end, - [OptionTypes.STRING] = function(_, value) + string = function(_, value) return value end, - [OptionTypes.SET] = function(info, value) + set = function(info, value) if type(value) == 'string' then return value end @@ -319,7 +292,7 @@ local convert_value_to_vim = (function() end end, - [OptionTypes.ARRAY] = function(info, value) + array = function(info, value) if type(value) == 'string' then return value end @@ -329,7 +302,7 @@ local convert_value_to_vim = (function() return table.concat(value, ',') end, - [OptionTypes.MAP] = function(_, value) + map = function(_, value) if type(value) == 'string' then return value end @@ -361,17 +334,17 @@ local convert_value_to_lua = (function() -- Map of OptionType to functions that take vimoption_T values and convert to lua values. -- Each function takes (info, vim_value) -> lua_value local to_lua_value = { - [OptionTypes.BOOLEAN] = function(_, value) + boolean = function(_, value) return value end, - [OptionTypes.NUMBER] = function(_, value) + number = function(_, value) return value end, - [OptionTypes.STRING] = function(_, value) + string = function(_, value) return value end, - [OptionTypes.ARRAY] = function(info, value) + array = function(info, value) if type(value) == 'table' then if not info.allows_duplicates then value = remove_duplicate_values(value) @@ -420,7 +393,7 @@ local convert_value_to_lua = (function() return vim.split(value, ',') end, - [OptionTypes.SET] = function(info, value) + set = function(info, value) if type(value) == 'table' then return value end @@ -451,7 +424,7 @@ local convert_value_to_lua = (function() end end, - [OptionTypes.MAP] = function(info, raw_value) + map = function(info, raw_value) if type(raw_value) == 'table' then return raw_value end @@ -485,15 +458,15 @@ end --- Handles the '^' operator local prepend_value = (function() local methods = { - [OptionTypes.NUMBER] = function() + number = function() error("The '^' operator is not currently supported for") end, - [OptionTypes.STRING] = function(left, right) + string = function(left, right) return right .. left end, - [OptionTypes.ARRAY] = function(left, right) + array = function(left, right) for i = #right, 1, -1 do table.insert(left, 1, right[i]) end @@ -501,11 +474,11 @@ local prepend_value = (function() return left end, - [OptionTypes.MAP] = function(left, right) + map = function(left, right) return vim.tbl_extend('force', left, right) end, - [OptionTypes.SET] = function(left, right) + set = function(left, right) return vim.tbl_extend('force', left, right) end, } @@ -524,15 +497,15 @@ end)() --- Handles the '+' operator local add_value = (function() local methods = { - [OptionTypes.NUMBER] = function(left, right) + number = function(left, right) return left + right end, - [OptionTypes.STRING] = function(left, right) + string = function(left, right) return left .. right end, - [OptionTypes.ARRAY] = function(left, right) + array = function(left, right) for _, v in ipairs(right) do table.insert(left, v) end @@ -540,11 +513,11 @@ local add_value = (function() return left end, - [OptionTypes.MAP] = function(left, right) + map = function(left, right) return vim.tbl_extend('force', left, right) end, - [OptionTypes.SET] = function(left, right) + set = function(left, right) return vim.tbl_extend('force', left, right) end, } @@ -580,15 +553,15 @@ local remove_value = (function() end local methods = { - [OptionTypes.NUMBER] = function(left, right) + number = function(left, right) return left - right end, - [OptionTypes.STRING] = function() + string = function() error('Subtraction not supported for strings.') end, - [OptionTypes.ARRAY] = function(left, right) + array = function(left, right) if type(right) == 'string' then remove_one_item(left, right) else @@ -600,7 +573,7 @@ local remove_value = (function() return left end, - [OptionTypes.MAP] = function(left, right) + map = function(left, right) if type(right) == 'string' then left[right] = nil else @@ -612,7 +585,7 @@ local remove_value = (function() return left end, - [OptionTypes.SET] = function(left, right) + set = function(left, right) if type(right) == 'string' then left[right] = nil else @@ -630,7 +603,7 @@ local remove_value = (function() end end)() -local create_option_metatable = function(set_type) +local create_option_metatable = function(scope) local set_mt, option_mt local make_option = function(name, value) @@ -650,13 +623,6 @@ local create_option_metatable = function(set_type) }, option_mt) end - local scope - if set_type == SET_TYPES.GLOBAL then - scope = 'global' - elseif set_type == SET_TYPES.LOCAL then - scope = 'local' - end - option_mt = { -- To set a value, instead use: -- opt[my_option] = value @@ -711,6 +677,6 @@ local create_option_metatable = function(set_type) return set_mt end -vim.opt = setmetatable({}, create_option_metatable(SET_TYPES.SET)) -vim.opt_local = setmetatable({}, create_option_metatable(SET_TYPES.LOCAL)) -vim.opt_global = setmetatable({}, create_option_metatable(SET_TYPES.GLOBAL)) +vim.opt = setmetatable({}, create_option_metatable()) +vim.opt_local = setmetatable({}, create_option_metatable('local')) +vim.opt_global = setmetatable({}, create_option_metatable('global')) -- cgit From b8de5ada80f7cc2cda3dc55defeb635ee39f3a24 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 15:42:17 +0100 Subject: refactor(vim.opt): replace _setup with lazy table --- runtime/lua/vim/_meta.lua | 154 +++++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 91 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index fb3a1e72e9..9e10e5ccf5 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -4,39 +4,52 @@ local vim = assert(vim) local a = vim.api local validate = vim.validate -local options_info = nil -local buf_options = nil -local glb_options = nil -local win_options = nil - -local function _setup() - if options_info ~= nil then - return - end - options_info = {} - for _, v in pairs(a.nvim_get_all_options_info()) do - options_info[v.name] = v - if v.shortname ~= '' then - options_info[v.shortname] = v +-- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded. +-- Can be done in a separate PR. +local key_value_options = { + fillchars = true, + fcs = true, + listchars = true, + lcs = true, + winhighlight = true, + winhl = true, +} + +--- Convert a vimoption_T style dictionary to the correct OptionType associated with it. +---@return string +local function get_option_metatype(name, info) + if info.type == 'boolean' then + return 'boolean' + elseif info.type == 'number' then + return 'number' + elseif info.type == 'string' then + if not info.commalist and not info.flaglist then + return 'string' end - end - local function get_scoped_options(scope) - local result = {} - for name, option_info in pairs(options_info) do - if option_info.scope == scope then - result[name] = true - end + if key_value_options[name] then + assert(info.commalist, 'Must be a comma list to use key:value style') + return 'map' end - return result + if info.flaglist then + return 'set' + elseif info.commalist then + return 'array' + end end - - buf_options = get_scoped_options('buf') - glb_options = get_scoped_options('global') - win_options = get_scoped_options('win') + error('Not a known info.type:' .. info.type) end +local options_info = setmetatable({}, { + __index = function(t, k) + local info = a.nvim_get_option_info(k) + info.metatype = get_option_metatype(k, info) + rawset(t, k, info) + return rawget(t, k) + end, +}) + local function make_meta_accessor(get, set, validator) validator = validator or function() return true @@ -90,12 +103,12 @@ do -- buffer option accessor return make_meta_accessor(get, set, function(k) if type(k) == 'string' then - _setup() - if win_options[k] then + local scope = options_info[k].scope + if scope == 'win' then error( string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k) ) - elseif glb_options[k] then + elseif scope == 'global' then error( string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k) ) @@ -124,12 +137,12 @@ do -- window option accessor return make_meta_accessor(get, set, function(k) if type(k) == 'string' then - _setup() - if buf_options[k] then + local scope = options_info[k].scope + if scope == 'buf' then error( string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k) ) - elseif glb_options[k] then + elseif scope == 'global' then error( string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k) ) @@ -184,43 +197,6 @@ local remove_duplicate_values = function(t) return result end --- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded. --- Can be done in a separate PR. -local key_value_options = { - fillchars = true, - fcs = true, - listchars = true, - lcs = true, - winhighlight = true, - winhl = true, -} - ---- Convert a vimoption_T style dictionary to the correct OptionType associated with it. ----@return string -local get_option_type = function(name, info) - if info.type == 'boolean' then - return 'boolean' - elseif info.type == 'number' then - return 'number' - elseif info.type == 'string' then - if not info.commalist and not info.flaglist then - return 'string' - end - - if key_value_options[name] then - assert(info.commalist, 'Must be a comma list to use key:value style') - return 'map' - end - - if info.flaglist then - return 'set' - elseif info.commalist then - return 'array' - end - end - error('Not a known info.type:' .. info.type) -end - -- Check whether the OptionTypes is allowed for vim.opt -- If it does not match, throw an error which indicates which option causes the error. local function assert_valid_value(name, value, types) @@ -322,10 +298,9 @@ local convert_value_to_vim = (function() return vim.NIL end - local option_type = get_option_type(name, info) - assert_valid_value(name, value, valid_types[option_type]) + assert_valid_value(name, value, valid_types[info.metatype]) - return to_vim_value[option_type](info, value) + return to_vim_value[info.metatype](info, value) end end)() @@ -445,14 +420,14 @@ local convert_value_to_lua = (function() end, } - return function(name, info, option_value) - return to_lua_value[get_option_type(name, info)](info, option_value) + return function(info, option_value) + return to_lua_value[info.metatype](info, option_value) end end)() --- Handles the mutation of various different values. -local value_mutator = function(name, info, current, new, mutator) - return mutator[get_option_type(name, info)](current, new) +local value_mutator = function(info, current, new, mutator) + return mutator[info.metatype](current, new) end --- Handles the '^' operator @@ -483,12 +458,11 @@ local prepend_value = (function() end, } - return function(name, info, current, new) + return function(info, current, new) return value_mutator( - name, info, - convert_value_to_lua(name, info, current), - convert_value_to_lua(name, info, new), + convert_value_to_lua(info, current), + convert_value_to_lua(info, new), methods ) end @@ -522,12 +496,11 @@ local add_value = (function() end, } - return function(name, info, current, new) + return function(info, current, new) return value_mutator( - name, info, - convert_value_to_lua(name, info, current), - convert_value_to_lua(name, info, new), + convert_value_to_lua(info, current), + convert_value_to_lua(info, new), methods ) end @@ -598,8 +571,8 @@ local remove_value = (function() end, } - return function(name, info, current, new) - return value_mutator(name, info, convert_value_to_lua(name, info, current), new, methods) + return function(info, current, new) + return value_mutator(info, convert_value_to_lua(info, current), new, methods) end end)() @@ -607,7 +580,6 @@ local create_option_metatable = function(scope) local set_mt, option_mt local make_option = function(name, value) - _setup() local info = assert(options_info[name], 'Not a valid option name: ' .. name) if type(value) == 'table' and getmetatable(value) == option_mt then @@ -634,7 +606,7 @@ local create_option_metatable = function(scope) end, get = function(self) - return convert_value_to_lua(self._name, self._info, self._value) + return convert_value_to_lua(self._info, self._value) end, append = function(self, right) @@ -642,7 +614,7 @@ local create_option_metatable = function(scope) end, __add = function(self, right) - return make_option(self._name, add_value(self._name, self._info, self._value, right)) + return make_option(self._name, add_value(self._info, self._value, right)) end, prepend = function(self, right) @@ -650,7 +622,7 @@ local create_option_metatable = function(scope) end, __pow = function(self, right) - return make_option(self._name, prepend_value(self._name, self._info, self._value, right)) + return make_option(self._name, prepend_value(self._info, self._value, right)) end, remove = function(self, right) @@ -658,7 +630,7 @@ local create_option_metatable = function(scope) end, __sub = function(self, right) - return make_option(self._name, remove_value(self._name, self._info, self._value, right)) + return make_option(self._name, remove_value(self._info, self._value, right)) end, } option_mt.__index = option_mt -- cgit From 514a1679dcd7444c7a1b66612b3313fb249d310a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 15:44:00 +0100 Subject: refactor(vim.opt): simplify get_option_metatype --- runtime/lua/vim/_meta.lua | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 9e10e5ccf5..66b55a3d47 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -18,27 +18,18 @@ local key_value_options = { --- Convert a vimoption_T style dictionary to the correct OptionType associated with it. ---@return string local function get_option_metatype(name, info) - if info.type == 'boolean' then - return 'boolean' - elseif info.type == 'number' then - return 'number' - elseif info.type == 'string' then - if not info.commalist and not info.flaglist then - return 'string' - end - - if key_value_options[name] then - assert(info.commalist, 'Must be a comma list to use key:value style') - return 'map' - end - + if info.type == 'string' then if info.flaglist then return 'set' elseif info.commalist then + if key_value_options[name] then + return 'map' + end return 'array' end + return 'string' end - error('Not a known info.type:' .. info.type) + return info.type end local options_info = setmetatable({}, { -- cgit From 164752b38074e9d71493c304dc3b8c672aac397d Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 15:52:39 +0100 Subject: refactor(vim.opt): remove make_meta_accessor() --- runtime/lua/vim/_meta.lua | 171 +++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 94 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 66b55a3d47..93077c89d6 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -2,7 +2,6 @@ local vim = assert(vim) local a = vim.api -local validate = vim.validate -- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded. -- Can be done in a separate PR. @@ -41,107 +40,85 @@ local options_info = setmetatable({}, { end, }) -local function make_meta_accessor(get, set, validator) - validator = validator or function() - return true - end - - validate({ - get = { get, 'f' }, - set = { set, 'f' }, - validator = { validator, 'f' }, - }) - - local mt = {} - function mt:__newindex(k, v) - if not validator(k) then - return - end - - return set(k, v) - end - function mt:__index(k) - if not validator(k) then - return +vim.env = setmetatable({}, { + __index = function(_, k) + local v = vim.fn.getenv(k) + if v == vim.NIL then + return nil end + return v + end, - return get(k) - end - return setmetatable({}, mt) -end + __newindex = function(_, k, v) + vim.fn.setenv(k, v) + end, +}) -vim.env = make_meta_accessor(function(k) - local v = vim.fn.getenv(k) - if v == vim.NIL then - return nil +do -- buffer option accessor + local function buf_opt_validate(k) + local scope = options_info[k].scope + if scope == 'win' then + error( + string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k) + ) + elseif scope == 'global' then + error( + string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k) + ) + end end - return v -end, vim.fn.setenv) -do -- buffer option accessor local function new_buf_opt_accessor(bufnr) - local function get(k) - if bufnr == nil and type(k) == 'number' then - return new_buf_opt_accessor(k) - end - - return a.nvim_get_option_value(k, { buf = bufnr or 0 }) - end + return setmetatable({},{ + __index = function(_, k) + if bufnr == nil and type(k) == 'number' then + return new_buf_opt_accessor(k) + end + buf_opt_validate(k) - local function set(k, v) - return a.nvim_set_option_value(k, v, { buf = bufnr or 0 }) - end + return a.nvim_get_option_value(k, { buf = bufnr or 0 }) + end, - return make_meta_accessor(get, set, function(k) - if type(k) == 'string' then - local scope = options_info[k].scope - if scope == 'win' then - error( - string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k) - ) - elseif scope == 'global' then - error( - string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k) - ) - end - end + __newindex = function(_, k, v) + buf_opt_validate(k) + return a.nvim_set_option_value(k, v, { buf = bufnr or 0 }) + end, + }) - return true - end) end vim.bo = new_buf_opt_accessor(nil) end do -- window option accessor - local function new_win_opt_accessor(winnr) - local function get(k) - if winnr == nil and type(k) == 'number' then - return new_win_opt_accessor(k) - end - return a.nvim_get_option_value(k, { win = winnr or 0 }) - end - - local function set(k, v) - return a.nvim_set_option_value(k, v, { win = winnr or 0 }) + local function win_opt_validate(k) + local scope = options_info[k].scope + if scope == 'buf' then + error( + string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k) + ) + elseif scope == 'global' then + error( + string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k) + ) end + end - return make_meta_accessor(get, set, function(k) - if type(k) == 'string' then - local scope = options_info[k].scope - if scope == 'buf' then - error( - string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k) - ) - elseif scope == 'global' then - error( - string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k) - ) + local function new_win_opt_accessor(winnr) + return setmetatable({}, { + __index = function(_, k) + if winnr == nil and type(k) == 'number' then + return new_win_opt_accessor(k) end - end - - return true - end) + win_opt_validate(k) + return a.nvim_get_option_value(k, { win = winnr or 0 }) + end, + + __newindex = function(_, k, v) + win_opt_validate(k) + return a.nvim_set_option_value(k, v, { win = winnr or 0 }) + end, + }) end vim.wo = new_win_opt_accessor(nil) @@ -149,19 +126,25 @@ end -- vim global option -- this ONLY sets the global option. like `setglobal` -vim.go = make_meta_accessor(function(k) - return a.nvim_get_option_value(k, { scope = 'global' }) -end, function(k, v) - return a.nvim_set_option_value(k, v, { scope = 'global' }) -end) +vim.go = setmetatable({}, { + __index = function(_, k) + return a.nvim_get_option_value(k, { scope = 'global' }) + end, + __newindex = function(_, k, v) + return a.nvim_set_option_value(k, v, { scope = 'global' }) + end, +}) -- vim `set` style options. -- it has no additional metamethod magic. -vim.o = make_meta_accessor(function(k) - return a.nvim_get_option_value(k, {}) -end, function(k, v) - return a.nvim_set_option_value(k, v, {}) -end) +vim.o = setmetatable({}, { + __index = function(_, k) + return a.nvim_get_option_value(k, {}) + end, + __newindex = function(_, k, v) + return a.nvim_set_option_value(k, v, {}) + end, +}) ---@brief [[ --- vim.opt, vim.opt_local and vim.opt_global implementation -- cgit From 7533ceec13a1dd9a1e46a523975bddf52f533a93 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 15:56:35 +0100 Subject: refactor(vim.opt): unify vim.bo/wo building --- runtime/lua/vim/_meta.lua | 92 +++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 63 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 93077c89d6..f78c2da581 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -54,76 +54,42 @@ vim.env = setmetatable({}, { end, }) -do -- buffer option accessor - local function buf_opt_validate(k) - local scope = options_info[k].scope - if scope == 'win' then - error( - string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k) +local function opt_validate(option_name, target_scope) + local scope = options_info[option_name].scope + if scope ~= target_scope then + local scope_to_string = { buf = 'buffer', win = 'window' } + error( + string.format( + [['%s' is a %s option, not a %s option. See ":help %s"]], + option_name, + scope_to_string[scope] or scope, + scope_to_string[target_scope] or target_scope, + option_name ) - elseif scope == 'global' then - error( - string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k) - ) - end - end - - local function new_buf_opt_accessor(bufnr) - return setmetatable({},{ - __index = function(_, k) - if bufnr == nil and type(k) == 'number' then - return new_buf_opt_accessor(k) - end - buf_opt_validate(k) - - return a.nvim_get_option_value(k, { buf = bufnr or 0 }) - end, - - __newindex = function(_, k, v) - buf_opt_validate(k) - return a.nvim_set_option_value(k, v, { buf = bufnr or 0 }) - end, - }) - + ) end - - vim.bo = new_buf_opt_accessor(nil) end -do -- window option accessor - local function win_opt_validate(k) - local scope = options_info[k].scope - if scope == 'buf' then - error( - string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k) - ) - elseif scope == 'global' then - error( - string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k) - ) - end - end - - local function new_win_opt_accessor(winnr) - return setmetatable({}, { - __index = function(_, k) - if winnr == nil and type(k) == 'number' then - return new_win_opt_accessor(k) - end - win_opt_validate(k) - return a.nvim_get_option_value(k, { win = winnr or 0 }) - end, - - __newindex = function(_, k, v) - win_opt_validate(k) - return a.nvim_set_option_value(k, v, { win = winnr or 0 }) - end, - }) - end +local function new_opt_accessor(handle, scope) + return setmetatable({}, { + __index = function(_, k) + if handle == nil and type(k) == 'number' then + return new_opt_accessor(k, scope) + end + opt_validate(k, scope) + return a.nvim_get_option_value(k, { [scope] = handle or 0 }) + end, - vim.wo = new_win_opt_accessor(nil) + __newindex = function(_, k, v) + opt_validate(k, scope) + return a.nvim_set_option_value(k, v, { [scope] = handle or 0 }) + end, + }) end +vim.bo = new_opt_accessor(nil, 'buf') +vim.wo = new_opt_accessor(nil, 'win') + -- vim global option -- this ONLY sets the global option. like `setglobal` vim.go = setmetatable({}, { -- cgit From 925a8119902ebe63343374f9edc57b4ea6038d08 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 16:44:18 +0100 Subject: refactor(vim.opt): remove value_mutator() --- runtime/lua/vim/_meta.lua | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index f78c2da581..9daef780f4 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -365,11 +365,6 @@ local convert_value_to_lua = (function() end end)() ---- Handles the mutation of various different values. -local value_mutator = function(info, current, new, mutator) - return mutator[info.metatype](current, new) -end - --- Handles the '^' operator local prepend_value = (function() local methods = { @@ -399,11 +394,9 @@ local prepend_value = (function() } return function(info, current, new) - return value_mutator( - info, + methods[info.metatype]( convert_value_to_lua(info, current), - convert_value_to_lua(info, new), - methods + convert_value_to_lua(info, new) ) end end)() @@ -437,11 +430,9 @@ local add_value = (function() } return function(info, current, new) - return value_mutator( - info, + methods[info.metatype]( convert_value_to_lua(info, current), - convert_value_to_lua(info, new), - methods + convert_value_to_lua(info, new) ) end end)() @@ -512,7 +503,7 @@ local remove_value = (function() } return function(info, current, new) - return value_mutator(info, convert_value_to_lua(info, current), new, methods) + return methods[info.metatype](convert_value_to_lua(info, current), new) end end)() -- cgit From b364bc2c340e4d56ca34960bbd73e2774e7f235c Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 16:54:34 +0100 Subject: refactor(vim.opt): dry up and tidy --- runtime/lua/vim/_meta.lua | 117 +++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 75 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 9daef780f4..cb02174e71 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -123,9 +123,6 @@ vim.o = setmetatable({}, { --- Preserves the order and does not mutate the original list local remove_duplicate_values = function(t) local result, seen = {}, {} - if type(t) == 'function' then - error(debug.traceback('asdf')) - end for _, v in ipairs(t) do if not seen[v] then table.insert(result, v) @@ -157,6 +154,26 @@ local function assert_valid_value(name, value, types) ) end +local function passthrough(_, x) + return x +end + +local function tbl_merge(left, right) + return vim.tbl_extend('force', left, right) +end + +local function tbl_remove(t, value) + if type(value) == 'string' then + t[value] = nil + else + for _, v in ipairs(value) do + t[v] = nil + end + end + + return t +end + local valid_types = { boolean = { 'boolean' }, number = { 'number' }, @@ -171,15 +188,9 @@ local convert_value_to_vim = (function() -- Map of functions to take a Lua style value and convert to vimoption_T style value. -- Each function takes (info, lua_value) -> vim_value local to_vim_value = { - boolean = function(_, value) - return value - end, - number = function(_, value) - return value - end, - string = function(_, value) - return value - end, + boolean = passthrough, + number = passthrough, + string = passthrough, set = function(info, value) if type(value) == 'string' then @@ -249,15 +260,9 @@ local convert_value_to_lua = (function() -- Map of OptionType to functions that take vimoption_T values and convert to lua values. -- Each function takes (info, vim_value) -> lua_value local to_lua_value = { - boolean = function(_, value) - return value - end, - number = function(_, value) - return value - end, - string = function(_, value) - return value - end, + boolean = passthrough, + number = passthrough, + string = passthrough, array = function(info, value) if type(value) == 'table' then @@ -276,9 +281,7 @@ local convert_value_to_lua = (function() -- Handles unescaped commas in a list. if string.find(value, ',,,') then - local comma_split = vim.split(value, ',,,') - local left = comma_split[1] - local right = comma_split[2] + local left, right = unpack(vim.split(value, ',,,')) local result = {} vim.list_extend(result, vim.split(left, ',')) @@ -291,9 +294,7 @@ local convert_value_to_lua = (function() end if string.find(value, ',^,,', 1, true) then - local comma_split = vim.split(value, ',^,,', true) - local left = comma_split[1] - local right = comma_split[2] + local left, right = unpack(vim.split(value, ',^,,', true)) local result = {} vim.list_extend(result, vim.split(left, ',')) @@ -384,13 +385,8 @@ local prepend_value = (function() return left end, - map = function(left, right) - return vim.tbl_extend('force', left, right) - end, - - set = function(left, right) - return vim.tbl_extend('force', left, right) - end, + map = tbl_merge, + set = tbl_merge, } return function(info, current, new) @@ -420,13 +416,8 @@ local add_value = (function() return left end, - map = function(left, right) - return vim.tbl_extend('force', left, right) - end, - - set = function(left, right) - return vim.tbl_extend('force', left, right) - end, + map = tbl_merge, + set = tbl_merge, } return function(info, current, new) @@ -477,29 +468,8 @@ local remove_value = (function() return left end, - map = function(left, right) - if type(right) == 'string' then - left[right] = nil - else - for _, v in ipairs(right) do - left[v] = nil - end - end - - return left - end, - - set = function(left, right) - if type(right) == 'string' then - left[right] = nil - else - for _, v in ipairs(right) do - left[v] = nil - end - end - - return left - end, + map = tbl_remove, + set = tbl_remove, } return function(info, current, new) @@ -507,8 +477,8 @@ local remove_value = (function() end end)() -local create_option_metatable = function(scope) - local set_mt, option_mt +local create_option_accessor = function(scope) + local option_mt local make_option = function(name, value) local info = assert(options_info[name], 'Not a valid option name: ' .. name) @@ -566,20 +536,17 @@ local create_option_metatable = function(scope) } option_mt.__index = option_mt - set_mt = { + return setmetatable({}, { __index = function(_, k) return make_option(k, a.nvim_get_option_value(k, { scope = scope })) end, __newindex = function(_, k, v) - local opt = make_option(k, v) - opt:_set() + make_option(k, v):_set() end, - } - - return set_mt + }) end -vim.opt = setmetatable({}, create_option_metatable()) -vim.opt_local = setmetatable({}, create_option_metatable('local')) -vim.opt_global = setmetatable({}, create_option_metatable('global')) +vim.opt = create_option_accessor() +vim.opt_local = create_option_accessor('local') +vim.opt_global = create_option_accessor('global') -- cgit From ad972990ad7ee59f0be7d267b58ca880c9ccaa7b Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 16:57:58 +0100 Subject: refactor(vim.opt): optimize append/prepend/remove --- runtime/lua/vim/_meta.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index cb02174e71..7ec132fbb5 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -502,8 +502,6 @@ local create_option_accessor = function(scope) _set = function(self) local value = convert_value_to_vim(self._name, self._info, self._value) a.nvim_set_option_value(self._name, value, { scope = scope }) - - return self end, get = function(self) @@ -511,7 +509,8 @@ local create_option_accessor = function(scope) end, append = function(self, right) - return self:__add(right):_set() + self._value = add_value(self._info, self._value, right) + self:_set() end, __add = function(self, right) @@ -519,7 +518,8 @@ local create_option_accessor = function(scope) end, prepend = function(self, right) - return self:__pow(right):_set() + self._value = prepend_value(self._info, self._value, right) + self:_set() end, __pow = function(self, right) @@ -527,7 +527,8 @@ local create_option_accessor = function(scope) end, remove = function(self, right) - return self:__sub(right):_set() + self._value = remove_value(self._info, self._value, right) + self:_set() end, __sub = function(self, right) -- cgit From 9272d20ea48ea0beb1ecefc0331344a1a3e4b05e Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 16:59:09 +0100 Subject: refactor(vim.opt): use local function syntax --- runtime/lua/vim/_meta.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 7ec132fbb5..fc5df0116d 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -121,7 +121,7 @@ vim.o = setmetatable({}, { ---@brief ]] --- Preserves the order and does not mutate the original list -local remove_duplicate_values = function(t) +local function remove_duplicate_values(t) local result, seen = {}, {} for _, v in ipairs(t) do if not seen[v] then @@ -430,7 +430,7 @@ end)() --- Handles the '-' operator local remove_value = (function() - local remove_one_item = function(t, val) + local function remove_one_item(t, val) if vim.tbl_islist(t) then local remove_index = nil for i, v in ipairs(t) do @@ -477,10 +477,10 @@ local remove_value = (function() end end)() -local create_option_accessor = function(scope) +local function create_option_accessor(scope) local option_mt - local make_option = function(name, value) + local function make_option(name, value) local info = assert(options_info[name], 'Not a valid option name: ' .. name) if type(value) == 'table' and getmetatable(value) == option_mt then -- cgit From 038c7115393c931b29c451eb1434d283c74ba55e Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 8 Sep 2022 17:09:44 +0100 Subject: refactor(vim.opt): de-nest code --- runtime/lua/vim/_meta.lua | 446 ++++++++++++++++++++++------------------------ 1 file changed, 218 insertions(+), 228 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index fc5df0116d..dada918d69 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -183,299 +183,289 @@ local valid_types = { map = { 'string', 'table' }, } ---- Convert a lua value to a vimoption_T value -local convert_value_to_vim = (function() - -- Map of functions to take a Lua style value and convert to vimoption_T style value. - -- Each function takes (info, lua_value) -> vim_value - local to_vim_value = { - boolean = passthrough, - number = passthrough, - string = passthrough, - - set = function(info, value) - if type(value) == 'string' then - return value - end - - if info.flaglist and info.commalist then - local keys = {} - for k, v in pairs(value) do - if v then - table.insert(keys, k) - end - end +-- Map of functions to take a Lua style value and convert to vimoption_T style value. +-- Each function takes (info, lua_value) -> vim_value +local to_vim_value = { + boolean = passthrough, + number = passthrough, + string = passthrough, + + set = function(info, value) + if type(value) == 'string' then + return value + end - table.sort(keys) - return table.concat(keys, ',') - else - local result = '' - for k, v in pairs(value) do - if v then - result = result .. k - end + if info.flaglist and info.commalist then + local keys = {} + for k, v in pairs(value) do + if v then + table.insert(keys, k) end - - return result end - end, - array = function(info, value) - if type(value) == 'string' then - return value - end - if not info.allows_duplicates then - value = remove_duplicate_values(value) + table.sort(keys) + return table.concat(keys, ',') + else + local result = '' + for k, v in pairs(value) do + if v then + result = result .. k + end end - return table.concat(value, ',') - end, - map = function(_, value) - if type(value) == 'string' then - return value - end + return result + end + end, - local result = {} - for opt_key, opt_value in pairs(value) do - table.insert(result, string.format('%s:%s', opt_key, opt_value)) - end + array = function(info, value) + if type(value) == 'string' then + return value + end + if not info.allows_duplicates then + value = remove_duplicate_values(value) + end + return table.concat(value, ',') + end, - table.sort(result) - return table.concat(result, ',') - end, - } + map = function(_, value) + if type(value) == 'string' then + return value + end - return function(name, info, value) - if value == nil then - return vim.NIL + local result = {} + for opt_key, opt_value in pairs(value) do + table.insert(result, string.format('%s:%s', opt_key, opt_value)) end - assert_valid_value(name, value, valid_types[info.metatype]) + table.sort(result) + return table.concat(result, ',') + end, +} - return to_vim_value[info.metatype](info, value) +--- Convert a lua value to a vimoption_T value +local function convert_value_to_vim(name, info, value) + if value == nil then + return vim.NIL end -end)() ---- Converts a vimoption_T style value to a Lua value -local convert_value_to_lua = (function() - -- Map of OptionType to functions that take vimoption_T values and convert to lua values. - -- Each function takes (info, vim_value) -> lua_value - local to_lua_value = { - boolean = passthrough, - number = passthrough, - string = passthrough, - - array = function(info, value) - if type(value) == 'table' then - if not info.allows_duplicates then - value = remove_duplicate_values(value) - end + assert_valid_value(name, value, valid_types[info.metatype]) - return value - end + return to_vim_value[info.metatype](info, value) +end + +-- Map of OptionType to functions that take vimoption_T values and convert to lua values. +-- Each function takes (info, vim_value) -> lua_value +local to_lua_value = { + boolean = passthrough, + number = passthrough, + string = passthrough, - -- Empty strings mean that there is nothing there, - -- so empty table should be returned. - if value == '' then - return {} + array = function(info, value) + if type(value) == 'table' then + if not info.allows_duplicates then + value = remove_duplicate_values(value) end - -- Handles unescaped commas in a list. - if string.find(value, ',,,') then - local left, right = unpack(vim.split(value, ',,,')) + return value + end - local result = {} - vim.list_extend(result, vim.split(left, ',')) - table.insert(result, ',') - vim.list_extend(result, vim.split(right, ',')) + -- Empty strings mean that there is nothing there, + -- so empty table should be returned. + if value == '' then + return {} + end - table.sort(result) + -- Handles unescaped commas in a list. + if string.find(value, ',,,') then + local left, right = unpack(vim.split(value, ',,,')) - return result - end + local result = {} + vim.list_extend(result, vim.split(left, ',')) + table.insert(result, ',') + vim.list_extend(result, vim.split(right, ',')) - if string.find(value, ',^,,', 1, true) then - local left, right = unpack(vim.split(value, ',^,,', true)) + table.sort(result) - local result = {} - vim.list_extend(result, vim.split(left, ',')) - table.insert(result, '^,') - vim.list_extend(result, vim.split(right, ',')) + return result + end - table.sort(result) + if string.find(value, ',^,,', 1, true) then + local left, right = unpack(vim.split(value, ',^,,', true)) - return result - end + local result = {} + vim.list_extend(result, vim.split(left, ',')) + table.insert(result, '^,') + vim.list_extend(result, vim.split(right, ',')) - return vim.split(value, ',') - end, + table.sort(result) - set = function(info, value) - if type(value) == 'table' then - return value - end + return result + end - -- Empty strings mean that there is nothing there, - -- so empty table should be returned. - if value == '' then - return {} - end + return vim.split(value, ',') + end, - assert(info.flaglist, 'That is the only one I know how to handle') + set = function(info, value) + if type(value) == 'table' then + return value + end - if info.flaglist and info.commalist then - local split_value = vim.split(value, ',') - local result = {} - for _, v in ipairs(split_value) do - result[v] = true - end + -- Empty strings mean that there is nothing there, + -- so empty table should be returned. + if value == '' then + return {} + end - return result - else - local result = {} - for i = 1, #value do - result[value:sub(i, i)] = true - end + assert(info.flaglist, 'That is the only one I know how to handle') - return result + if info.flaglist and info.commalist then + local split_value = vim.split(value, ',') + local result = {} + for _, v in ipairs(split_value) do + result[v] = true end - end, - map = function(info, raw_value) - if type(raw_value) == 'table' then - return raw_value + return result + else + local result = {} + for i = 1, #value do + result[value:sub(i, i)] = true end - assert(info.commalist, 'Only commas are supported currently') + return result + end + end, - local result = {} + map = function(info, raw_value) + if type(raw_value) == 'table' then + return raw_value + end - local comma_split = vim.split(raw_value, ',') - for _, key_value_str in ipairs(comma_split) do - local key, value = unpack(vim.split(key_value_str, ':')) - key = vim.trim(key) + assert(info.commalist, 'Only commas are supported currently') - result[key] = value - end + local result = {} - return result - end, - } + local comma_split = vim.split(raw_value, ',') + for _, key_value_str in ipairs(comma_split) do + local key, value = unpack(vim.split(key_value_str, ':')) + key = vim.trim(key) - return function(info, option_value) - return to_lua_value[info.metatype](info, option_value) - end -end)() + result[key] = value + end ---- Handles the '^' operator -local prepend_value = (function() - local methods = { - number = function() - error("The '^' operator is not currently supported for") - end, + return result + end, +} - string = function(left, right) - return right .. left - end, +--- Converts a vimoption_T style value to a Lua value +local function convert_value_to_lua(info, option_value) + return to_lua_value[info.metatype](info, option_value) +end - array = function(left, right) - for i = #right, 1, -1 do - table.insert(left, 1, right[i]) - end +local prepend_methods = { + number = function() + error("The '^' operator is not currently supported for") + end, - return left - end, + string = function(left, right) + return right .. left + end, - map = tbl_merge, - set = tbl_merge, - } + array = function(left, right) + for i = #right, 1, -1 do + table.insert(left, 1, right[i]) + end - return function(info, current, new) - methods[info.metatype]( - convert_value_to_lua(info, current), - convert_value_to_lua(info, new) - ) - end -end)() + return left + end, ---- Handles the '+' operator -local add_value = (function() - local methods = { - number = function(left, right) - return left + right - end, + map = tbl_merge, + set = tbl_merge, +} - string = function(left, right) - return left .. right - end, +--- Handles the '^' operator +local function prepend_value(info, current, new) + return prepend_methods[info.metatype]( + convert_value_to_lua(info, current), + convert_value_to_lua(info, new) + ) +end - array = function(left, right) - for _, v in ipairs(right) do - table.insert(left, v) - end +local add_methods = { + number = function(left, right) + return left + right + end, - return left - end, + string = function(left, right) + return left .. right + end, - map = tbl_merge, - set = tbl_merge, - } + array = function(left, right) + for _, v in ipairs(right) do + table.insert(left, v) + end - return function(info, current, new) - methods[info.metatype]( - convert_value_to_lua(info, current), - convert_value_to_lua(info, new) - ) - end -end)() + return left + end, ---- Handles the '-' operator -local remove_value = (function() - local function remove_one_item(t, val) - if vim.tbl_islist(t) then - local remove_index = nil - for i, v in ipairs(t) do - if v == val then - remove_index = i - end - end + map = tbl_merge, + set = tbl_merge, +} - if remove_index then - table.remove(t, remove_index) +--- Handles the '+' operator +local function add_value(info, current, new) + return add_methods[info.metatype]( + convert_value_to_lua(info, current), + convert_value_to_lua(info, new) + ) +end + +local function remove_one_item(t, val) + if vim.tbl_islist(t) then + local remove_index = nil + for i, v in ipairs(t) do + if v == val then + remove_index = i end - else - t[val] = nil end + + if remove_index then + table.remove(t, remove_index) + end + else + t[val] = nil end +end - local methods = { - number = function(left, right) - return left - right - end, +local remove_methods = { + number = function(left, right) + return left - right + end, - string = function() - error('Subtraction not supported for strings.') - end, + string = function() + error('Subtraction not supported for strings.') + end, - array = function(left, right) - if type(right) == 'string' then - remove_one_item(left, right) - else - for _, v in ipairs(right) do - remove_one_item(left, v) - end + array = function(left, right) + if type(right) == 'string' then + remove_one_item(left, right) + else + for _, v in ipairs(right) do + remove_one_item(left, v) end + end - return left - end, + return left + end, - map = tbl_remove, - set = tbl_remove, - } + map = tbl_remove, + set = tbl_remove, +} - return function(info, current, new) - return methods[info.metatype](convert_value_to_lua(info, current), new) - end -end)() +--- Handles the '-' operator +local function remove_value(info, current, new) + return remove_methods[info.metatype](convert_value_to_lua(info, current), new) +end local function create_option_accessor(scope) local option_mt -- cgit From 8c2226fc30931690186390d86f963cd43e6947ef Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 9 Oct 2022 12:40:56 +0200 Subject: fix(lua): properly configure luacheck and remove `local vim = ...` lines (#20551) --- runtime/lua/vim/_meta.lua | 3 --- 1 file changed, 3 deletions(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index dada918d69..9c7972873e 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -1,6 +1,3 @@ --- prevents luacheck from making lints for setting things on vim -local vim = assert(vim) - local a = vim.api -- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded. -- cgit From 8b9bf3e3b997f033dcf73d506cceb12231501a22 Mon Sep 17 00:00:00 2001 From: Phelipe Teles <39670535+phelipetls@users.noreply.github.com> Date: Mon, 12 Dec 2022 12:14:50 -0300 Subject: fix: vim.opt_local:append ignoring global option value (#21382) Closes https://github.com/neovim/neovim/issues/18225 --- runtime/lua/vim/_meta.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/_meta.lua') diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 9c7972873e..104f29c4c0 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -526,7 +526,7 @@ local function create_option_accessor(scope) return setmetatable({}, { __index = function(_, k) - return make_option(k, a.nvim_get_option_value(k, { scope = scope })) + return make_option(k, a.nvim_get_option_value(k, {})) end, __newindex = function(_, k, v) -- cgit