mirror of
https://github.com/neovim/neovim.git
synced 2025-09-19 09:48:19 +00:00
refactor(vim.opt): dry up and tidy
This commit is contained in:
@@ -123,9 +123,6 @@ vim.o = setmetatable({}, {
|
|||||||
--- Preserves the order and does not mutate the original list
|
--- Preserves the order and does not mutate the original list
|
||||||
local remove_duplicate_values = function(t)
|
local remove_duplicate_values = function(t)
|
||||||
local result, seen = {}, {}
|
local result, seen = {}, {}
|
||||||
if type(t) == 'function' then
|
|
||||||
error(debug.traceback('asdf'))
|
|
||||||
end
|
|
||||||
for _, v in ipairs(t) do
|
for _, v in ipairs(t) do
|
||||||
if not seen[v] then
|
if not seen[v] then
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
@@ -157,6 +154,26 @@ local function assert_valid_value(name, value, types)
|
|||||||
)
|
)
|
||||||
end
|
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 = {
|
local valid_types = {
|
||||||
boolean = { 'boolean' },
|
boolean = { 'boolean' },
|
||||||
number = { 'number' },
|
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.
|
-- Map of functions to take a Lua style value and convert to vimoption_T style value.
|
||||||
-- Each function takes (info, lua_value) -> vim_value
|
-- Each function takes (info, lua_value) -> vim_value
|
||||||
local to_vim_value = {
|
local to_vim_value = {
|
||||||
boolean = function(_, value)
|
boolean = passthrough,
|
||||||
return value
|
number = passthrough,
|
||||||
end,
|
string = passthrough,
|
||||||
number = function(_, value)
|
|
||||||
return value
|
|
||||||
end,
|
|
||||||
string = function(_, value)
|
|
||||||
return value
|
|
||||||
end,
|
|
||||||
|
|
||||||
set = function(info, value)
|
set = function(info, value)
|
||||||
if type(value) == 'string' then
|
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.
|
-- Map of OptionType to functions that take vimoption_T values and convert to lua values.
|
||||||
-- Each function takes (info, vim_value) -> lua_value
|
-- Each function takes (info, vim_value) -> lua_value
|
||||||
local to_lua_value = {
|
local to_lua_value = {
|
||||||
boolean = function(_, value)
|
boolean = passthrough,
|
||||||
return value
|
number = passthrough,
|
||||||
end,
|
string = passthrough,
|
||||||
number = function(_, value)
|
|
||||||
return value
|
|
||||||
end,
|
|
||||||
string = function(_, value)
|
|
||||||
return value
|
|
||||||
end,
|
|
||||||
|
|
||||||
array = function(info, value)
|
array = function(info, value)
|
||||||
if type(value) == 'table' then
|
if type(value) == 'table' then
|
||||||
@@ -276,9 +281,7 @@ local convert_value_to_lua = (function()
|
|||||||
|
|
||||||
-- Handles unescaped commas in a list.
|
-- Handles unescaped commas in a list.
|
||||||
if string.find(value, ',,,') then
|
if string.find(value, ',,,') then
|
||||||
local comma_split = vim.split(value, ',,,')
|
local left, right = unpack(vim.split(value, ',,,'))
|
||||||
local left = comma_split[1]
|
|
||||||
local right = comma_split[2]
|
|
||||||
|
|
||||||
local result = {}
|
local result = {}
|
||||||
vim.list_extend(result, vim.split(left, ','))
|
vim.list_extend(result, vim.split(left, ','))
|
||||||
@@ -291,9 +294,7 @@ local convert_value_to_lua = (function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
if string.find(value, ',^,,', 1, true) then
|
if string.find(value, ',^,,', 1, true) then
|
||||||
local comma_split = vim.split(value, ',^,,', true)
|
local left, right = unpack(vim.split(value, ',^,,', true))
|
||||||
local left = comma_split[1]
|
|
||||||
local right = comma_split[2]
|
|
||||||
|
|
||||||
local result = {}
|
local result = {}
|
||||||
vim.list_extend(result, vim.split(left, ','))
|
vim.list_extend(result, vim.split(left, ','))
|
||||||
@@ -384,13 +385,8 @@ local prepend_value = (function()
|
|||||||
return left
|
return left
|
||||||
end,
|
end,
|
||||||
|
|
||||||
map = function(left, right)
|
map = tbl_merge,
|
||||||
return vim.tbl_extend('force', left, right)
|
set = tbl_merge,
|
||||||
end,
|
|
||||||
|
|
||||||
set = function(left, right)
|
|
||||||
return vim.tbl_extend('force', left, right)
|
|
||||||
end,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return function(info, current, new)
|
return function(info, current, new)
|
||||||
@@ -420,13 +416,8 @@ local add_value = (function()
|
|||||||
return left
|
return left
|
||||||
end,
|
end,
|
||||||
|
|
||||||
map = function(left, right)
|
map = tbl_merge,
|
||||||
return vim.tbl_extend('force', left, right)
|
set = tbl_merge,
|
||||||
end,
|
|
||||||
|
|
||||||
set = function(left, right)
|
|
||||||
return vim.tbl_extend('force', left, right)
|
|
||||||
end,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return function(info, current, new)
|
return function(info, current, new)
|
||||||
@@ -477,29 +468,8 @@ local remove_value = (function()
|
|||||||
return left
|
return left
|
||||||
end,
|
end,
|
||||||
|
|
||||||
map = function(left, right)
|
map = tbl_remove,
|
||||||
if type(right) == 'string' then
|
set = tbl_remove,
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return function(info, current, new)
|
return function(info, current, new)
|
||||||
@@ -507,8 +477,8 @@ local remove_value = (function()
|
|||||||
end
|
end
|
||||||
end)()
|
end)()
|
||||||
|
|
||||||
local create_option_metatable = function(scope)
|
local create_option_accessor = function(scope)
|
||||||
local set_mt, option_mt
|
local option_mt
|
||||||
|
|
||||||
local make_option = function(name, value)
|
local make_option = function(name, value)
|
||||||
local info = assert(options_info[name], 'Not a valid option name: ' .. name)
|
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
|
option_mt.__index = option_mt
|
||||||
|
|
||||||
set_mt = {
|
return setmetatable({}, {
|
||||||
__index = function(_, k)
|
__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, { scope = scope }))
|
||||||
end,
|
end,
|
||||||
|
|
||||||
__newindex = function(_, k, v)
|
__newindex = function(_, k, v)
|
||||||
local opt = make_option(k, v)
|
make_option(k, v):_set()
|
||||||
opt:_set()
|
|
||||||
end,
|
end,
|
||||||
}
|
})
|
||||||
|
|
||||||
return set_mt
|
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.opt = setmetatable({}, create_option_metatable())
|
vim.opt = create_option_accessor()
|
||||||
vim.opt_local = setmetatable({}, create_option_metatable('local'))
|
vim.opt_local = create_option_accessor('local')
|
||||||
vim.opt_global = setmetatable({}, create_option_metatable('global'))
|
vim.opt_global = create_option_accessor('global')
|
||||||
|
Reference in New Issue
Block a user