chore: format runtime with stylua

This commit is contained in:
Christian Clason
2022-05-09 11:23:51 +02:00
parent 676e9e9334
commit aefdc6783c
37 changed files with 4198 additions and 3569 deletions

View File

@@ -5,15 +5,17 @@ local a = vim.api
local validate = vim.validate
local SET_TYPES = setmetatable({
SET = 0,
LOCAL = 1,
SET = 0,
LOCAL = 1,
GLOBAL = 2,
}, { __index = error })
local 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 end
if v.shortname ~= '' then
options_info[v.shortname] = v
end
end
local get_scoped_options = function(scope)
@@ -27,19 +29,21 @@ local get_scoped_options = function(scope)
return result
end
local buf_options = get_scoped_options("buf")
local glb_options = get_scoped_options("global")
local win_options = get_scoped_options("win")
local buf_options = get_scoped_options('buf')
local glb_options = get_scoped_options('global')
local win_options = get_scoped_options('win')
local function make_meta_accessor(get, set, del, validator)
validator = validator or function() return true end
validator = validator or function()
return true
end
validate {
get = {get, 'f'};
set = {set, 'f'};
del = {del, 'f', true};
validator = {validator, 'f'};
}
validate({
get = { get, 'f' },
set = { set, 'f' },
del = { del, 'f', true },
validator = { validator, 'f' },
})
local mt = {}
function mt:__newindex(k, v)
@@ -73,7 +77,7 @@ 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
if bufnr == nil and type(k) == 'number' then
return new_buf_opt_accessor(k)
end
@@ -103,7 +107,7 @@ end
do -- window option accessor
local function new_win_opt_accessor(winnr)
local function get(k)
if winnr == nil and type(k) == "number" then
if winnr == nil and type(k) == 'number' then
return new_win_opt_accessor(k)
end
return a.nvim_win_get_option(winnr or 0, k)
@@ -131,17 +135,19 @@ 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 = 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 `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 = 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)
---@brief [[
--- vim.opt, vim.opt_local and vim.opt_global implementation
@@ -154,7 +160,9 @@ vim.o = make_meta_accessor(
--- 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
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)
@@ -171,37 +179,41 @@ end
local key_value_options = {
fillchars = true,
listchars = true,
winhl = true,
winhl = true,
}
---@class OptionTypes
--- Option Type Enum
local OptionTypes = setmetatable({
BOOLEAN = 0,
NUMBER = 1,
STRING = 2,
ARRAY = 3,
MAP = 4,
SET = 5,
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,
__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
local get_option_type = function(name, info)
if info.type == "boolean" then
if info.type == 'boolean' then
return OptionTypes.BOOLEAN
elseif info.type == "number" then
elseif info.type == 'number' then
return OptionTypes.NUMBER
elseif info.type == "string" then
elseif info.type == 'string' then
if not info.commalist and not info.flaglist then
return OptionTypes.STRING
end
if key_value_options[name] then
assert(info.commalist, "Must be a comma list to use key:value style")
assert(info.commalist, 'Must be a comma list to use key:value style')
return OptionTypes.MAP
end
@@ -211,13 +223,12 @@ local get_option_type = function(name, info)
return OptionTypes.ARRAY
end
error("Fallthrough in OptionTypes")
error('Fallthrough in OptionTypes')
else
error("Not a known info.type:" .. info.type)
error('Not a known info.type:' .. info.type)
end
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)
@@ -228,16 +239,18 @@ local function assert_valid_value(name, value, types)
end
end
error(string.format("Invalid option type '%s' for '%s', should be %s", type_of_value, name, table.concat(types, " or ")))
error(
string.format("Invalid option type '%s' for '%s', should be %s", type_of_value, name, table.concat(types, ' or '))
)
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" },
[OptionTypes.BOOLEAN] = { 'boolean' },
[OptionTypes.NUMBER] = { 'number' },
[OptionTypes.STRING] = { 'string' },
[OptionTypes.SET] = { 'string', 'table' },
[OptionTypes.ARRAY] = { 'string', 'table' },
[OptionTypes.MAP] = { 'string', 'table' },
}
--- Convert a lua value to a vimoption_T value
@@ -245,12 +258,20 @@ 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) return value end,
[OptionTypes.NUMBER] = function(_, value) return value end,
[OptionTypes.STRING] = function(_, value) return value end,
[OptionTypes.BOOLEAN] = function(_, value)
return value
end,
[OptionTypes.NUMBER] = function(_, value)
return value
end,
[OptionTypes.STRING] = function(_, value)
return value
end,
[OptionTypes.SET] = function(info, value)
if type(value) == "string" then return value end
if type(value) == 'string' then
return value
end
if info.flaglist and info.commalist then
local keys = {}
@@ -261,7 +282,7 @@ local convert_value_to_vim = (function()
end
table.sort(keys)
return table.concat(keys, ",")
return table.concat(keys, ',')
else
local result = ''
for k, v in pairs(value) do
@@ -275,23 +296,27 @@ local convert_value_to_vim = (function()
end,
[OptionTypes.ARRAY] = function(info, value)
if type(value) == "string" then return value end
if type(value) == 'string' then
return value
end
if not info.allows_duplicates then
value = remove_duplicate_values(value)
end
return table.concat(value, ",")
return table.concat(value, ',')
end,
[OptionTypes.MAP] = function(_, value)
if type(value) == "string" then return value end
if type(value) == 'string' then
return value
end
local result = {}
for opt_key, opt_value in pairs(value) do
table.insert(result, string.format("%s:%s", opt_key, opt_value))
table.insert(result, string.format('%s:%s', opt_key, opt_value))
end
table.sort(result)
return table.concat(result, ",")
return table.concat(result, ',')
end,
}
@@ -312,12 +337,18 @@ 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) return value end,
[OptionTypes.NUMBER] = function(_, value) return value end,
[OptionTypes.STRING] = function(_, value) return value end,
[OptionTypes.BOOLEAN] = function(_, value)
return value
end,
[OptionTypes.NUMBER] = function(_, value)
return value
end,
[OptionTypes.STRING] = function(_, value)
return value
end,
[OptionTypes.ARRAY] = function(info, value)
if type(value) == "table" then
if type(value) == 'table' then
if not info.allows_duplicates then
value = remove_duplicate_values(value)
end
@@ -332,41 +363,43 @@ local convert_value_to_lua = (function()
end
-- Handles unescaped commas in a list.
if string.find(value, ",,,") then
local comma_split = vim.split(value, ",,,")
if string.find(value, ',,,') then
local comma_split = vim.split(value, ',,,')
local left = comma_split[1]
local right = comma_split[2]
local result = {}
vim.list_extend(result, vim.split(left, ","))
table.insert(result, ",")
vim.list_extend(result, vim.split(right, ","))
vim.list_extend(result, vim.split(left, ','))
table.insert(result, ',')
vim.list_extend(result, vim.split(right, ','))
table.sort(result)
return result
end
if string.find(value, ",^,,", 1, true) then
local comma_split = vim.split(value, ",^,,", true)
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 result = {}
vim.list_extend(result, vim.split(left, ","))
table.insert(result, "^,")
vim.list_extend(result, vim.split(right, ","))
vim.list_extend(result, vim.split(left, ','))
table.insert(result, '^,')
vim.list_extend(result, vim.split(right, ','))
table.sort(result)
return result
end
return vim.split(value, ",")
return vim.split(value, ',')
end,
[OptionTypes.SET] = function(info, value)
if type(value) == "table" then return value end
if type(value) == 'table' then
return value
end
-- Empty strings mean that there is nothing there,
-- so empty table should be returned.
@@ -374,10 +407,10 @@ local convert_value_to_lua = (function()
return {}
end
assert(info.flaglist, "That is the only one I know how to handle")
assert(info.flaglist, 'That is the only one I know how to handle')
if info.flaglist and info.commalist then
local split_value = vim.split(value, ",")
local split_value = vim.split(value, ',')
local result = {}
for _, v in ipairs(split_value) do
result[v] = true
@@ -395,15 +428,17 @@ local convert_value_to_lua = (function()
end,
[OptionTypes.MAP] = function(info, raw_value)
if type(raw_value) == "table" then return raw_value end
if type(raw_value) == 'table' then
return raw_value
end
assert(info.commalist, "Only commas are supported currently")
assert(info.commalist, 'Only commas are supported currently')
local result = {}
local comma_split = vim.split(raw_value, ",")
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, ":"))
local key, value = unpack(vim.split(key_value_str, ':'))
key = vim.trim(key)
result[key] = value
@@ -443,17 +478,21 @@ local prepend_value = (function()
end,
[OptionTypes.MAP] = function(left, right)
return vim.tbl_extend("force", left, right)
return vim.tbl_extend('force', left, right)
end,
[OptionTypes.SET] = function(left, right)
return vim.tbl_extend("force", left, right)
return vim.tbl_extend('force', left, right)
end,
}
return function(name, info, current, new)
return value_mutator(
name, info, convert_value_to_lua(name, info, current), convert_value_to_lua(name, info, new), methods
name,
info,
convert_value_to_lua(name, info, current),
convert_value_to_lua(name, info, new),
methods
)
end
end)()
@@ -478,17 +517,21 @@ local add_value = (function()
end,
[OptionTypes.MAP] = function(left, right)
return vim.tbl_extend("force", left, right)
return vim.tbl_extend('force', left, right)
end,
[OptionTypes.SET] = function(left, right)
return vim.tbl_extend("force", left, right)
return vim.tbl_extend('force', left, right)
end,
}
return function(name, info, current, new)
return value_mutator(
name, info, convert_value_to_lua(name, info, current), convert_value_to_lua(name, info, new), methods
name,
info,
convert_value_to_lua(name, info, current),
convert_value_to_lua(name, info, new),
methods
)
end
end)()
@@ -518,11 +561,11 @@ local remove_value = (function()
end,
[OptionTypes.STRING] = function()
error("Subtraction not supported for strings.")
error('Subtraction not supported for strings.')
end,
[OptionTypes.ARRAY] = function(left, right)
if type(right) == "string" then
if type(right) == 'string' then
remove_one_item(left, right)
else
for _, v in ipairs(right) do
@@ -534,7 +577,7 @@ local remove_value = (function()
end,
[OptionTypes.MAP] = function(left, right)
if type(right) == "string" then
if type(right) == 'string' then
left[right] = nil
else
for _, v in ipairs(right) do
@@ -546,7 +589,7 @@ local remove_value = (function()
end,
[OptionTypes.SET] = function(left, right)
if type(right) == "string" then
if type(right) == 'string' then
left[right] = nil
else
for _, v in ipairs(right) do
@@ -567,9 +610,9 @@ local create_option_metatable = function(set_type)
local set_mt, option_mt
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)
if type(value) == "table" and getmetatable(value) == option_mt then
if type(value) == 'table' and getmetatable(value) == option_mt then
assert(name == value._name, "must be the same value, otherwise that's weird.")
value = value._value
@@ -584,9 +627,9 @@ local create_option_metatable = function(set_type)
local scope
if set_type == SET_TYPES.GLOBAL then
scope = "global"
scope = 'global'
elseif set_type == SET_TYPES.LOCAL then
scope = "local"
scope = 'local'
end
option_mt = {
@@ -594,7 +637,7 @@ local create_option_metatable = function(set_type)
-- opt[my_option] = value
_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})
a.nvim_set_option_value(self._name, value, { scope = scope })
return self
end,
@@ -625,13 +668,13 @@ local create_option_metatable = function(set_type)
__sub = function(self, right)
return make_option(self._name, remove_value(self._name, self._info, self._value, right))
end
end,
}
option_mt.__index = option_mt
set_mt = {
__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,
__newindex = function(_, k, v)