Skip to content

Commit

Permalink
feat: use faster version of validate if available
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaSolOs committed Oct 27, 2024
1 parent 88101da commit 413abd9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
12 changes: 6 additions & 6 deletions lua/ibl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ local validate_config = function(config)
repeat_linebreak = { config.indent.repeat_linebreak, "boolean", true },
}, config.indent, "ibl.config.indent")
if config.indent.char then
vim.validate {
utils.validate {
char = {
config.indent.char,
validate_char,
Expand All @@ -146,7 +146,7 @@ local validate_config = function(config)
}
end
if config.indent.tab_char then
vim.validate {
utils.validate {
tab_char = {
config.indent.tab_char,
validate_char,
Expand All @@ -155,7 +155,7 @@ local validate_config = function(config)
}
end
if type(config.indent.highlight) == "table" then
vim.validate {
utils.validate {
tab_char = {
config.indent.highlight,
function(highlight)
Expand All @@ -173,7 +173,7 @@ local validate_config = function(config)
remove_blankline_trail = { config.whitespace.remove_blankline_trail, "boolean", true },
}, config.whitespace, "ibl.config.whitespace")
if type(config.whitespace.highlight) == "table" then
vim.validate {
utils.validate {
tab_char = {
config.whitespace.highlight,
function(highlight)
Expand All @@ -199,7 +199,7 @@ local validate_config = function(config)
exclude = { config.scope.exclude, "table", true },
}, config.scope, "ibl.config.scope")
if config.scope.char then
vim.validate {
utils.validate {
char = {
config.scope.char,
validate_char,
Expand All @@ -208,7 +208,7 @@ local validate_config = function(config)
}
end
if type(config.scope.highlight) == "table" then
vim.validate {
utils.validate {
tab_char = {
config.scope.highlight,
function(highlight)
Expand Down
6 changes: 3 additions & 3 deletions lua/ibl/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ local count = 0
---@overload fun(type: 'CLEAR', cb: ibl.hooks.cb.clear, opts: ibl.hooks.options?): string
---@overload fun(type: 'HIGHLIGHT_SETUP', cb: ibl.hooks.cb.highlight_setup, opts: ibl.hooks.options?): string
M.register = function(type, cb, opts)
vim.validate {
utils.validate {
type = {
type,
function(t)
Expand All @@ -70,7 +70,7 @@ M.register = function(type, cb, opts)
opts = { opts, "table", true },
}
opts = vim.tbl_deep_extend("keep", opts or {}, default_opts)
vim.validate {
utils.validate {
bufnr = { opts.bufnr, "number", true },
}
if opts.bufnr then
Expand Down Expand Up @@ -105,7 +105,7 @@ end
---
---@param id string
M.clear = function(id)
vim.validate { id = { id, "string" } }
utils.validate { id = { id, "string" } }
local type, hook_id = unpack(utils.split(id, "_"))
if not type or not hook_id or not utils.tbl_contains(M.type, type) then
return
Expand Down
16 changes: 15 additions & 1 deletion lua/ibl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ M.get_whitespace = function(line)
return string.match(line, "^%s+") or ""
end

--- Use the faster validate version if available.
---@param spec table<string, {[1]:any, [2]:function|string, [3]:string|true|nil}>
M.validate = function(spec)
if vim.fn.has "nvim-0.11" == 1 then
for key, key_spec in pairs(spec) do
local message = type(key_spec[3]) == "string" and key_spec[3] or nil
local optional = type(key_spec[3]) == "boolean" and key_spec[3] or nil
vim.validate(key, key_spec[1], key_spec[2], optional, message)
end
else
vim.validate(spec)
end
end

---@param opt table
---@param input table
---@param path string
M.validate_config = function(opt, input, path)
vim.validate(opt)
M.validate(opt)
for key, _ in pairs(input) do
if not opt[key] then
error(string.format("'%s' is not a valid key of %s", key, path))
Expand Down

0 comments on commit 413abd9

Please sign in to comment.