Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use faster version of vim.validate if available #934

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lua/ibl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ local validate_config = function(config)
return
end

utils.validate({
utils.validate_config({
enabled = { config.enabled, "boolean", true },
debounce = { config.debounce, "number", true },
viewport_buffer = { config.viewport_buffer, "table", true },
Expand All @@ -121,14 +121,14 @@ local validate_config = function(config)
}, config, "ibl.config")

if config.viewport_buffer then
utils.validate({
utils.validate_config({
min = { config.viewport_buffer.min, "number", true },
max = { config.viewport_buffer.max, "number", true },
}, config.viewport_buffer, "ibl.config.viewport_buffer")
end

if config.indent then
utils.validate({
utils.validate_config({
char = { config.indent.char, { "string", "table" }, true },
tab_char = { config.indent.char, { "string", "table" }, true },
highlight = { config.indent.highlight, { "string", "table" }, true },
Expand All @@ -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 @@ -168,12 +168,12 @@ local validate_config = function(config)
end

if config.whitespace then
utils.validate({
utils.validate_config({
highlight = { config.whitespace.highlight, { "string", "table" }, true },
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 @@ -186,7 +186,7 @@ local validate_config = function(config)
end

if config.scope then
utils.validate({
utils.validate_config({
enabled = { config.scope.enabled, "boolean", true },
char = { config.scope.char, { "string", "table" }, true },
show_start = { config.scope.show_start, "boolean", true },
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 All @@ -219,21 +219,21 @@ local validate_config = function(config)
}
end
if config.scope.exclude then
utils.validate({
utils.validate_config({
language = { config.scope.exclude.language, "table", true },
node_type = { config.scope.exclude.node_type, "table", true },
}, config.scope.exclude, "ibl.config.scope.exclude")
end
if config.scope.include then
utils.validate({
utils.validate_config({
node_type = { config.scope.include.node_type, "table", true },
}, config.scope.include, "ibl.config.scope.include")
end
end

if config.exclude then
if config.exclude then
utils.validate({
utils.validate_config({
filetypes = { config.exclude.filetypes, "table", true },
buftypes = { config.exclude.buftypes, "table", true },
}, config.exclude, "ibl.config.exclude")
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
22 changes: 20 additions & 2 deletions lua/ibl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@ M.get_whitespace = function(line)
return string.match(line, "^%s+") or ""
end

--- Use the faster validate version if available.
--- NOTE: We disable some Lua diagnostics here since lua_ls isn't smart enough to
--- realize that we're using an overloaded function.
---@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 --[[@as string?]]
local optional = type(key_spec[3]) == "boolean" and key_spec[3] or nil --[[@as boolean?]]
---@diagnostic disable-next-line:param-type-mismatch, redundant-parameter
vim.validate(key, key_spec[1], key_spec[2], optional, message)
end
else
---@diagnostic disable-next-line:param-type-mismatch
vim.validate(spec)
MariaSolOs marked this conversation as resolved.
Show resolved Hide resolved
end
end

---@param opt table
---@param input table
---@param path string
M.validate = function(opt, input, path)
vim.validate(opt)
M.validate_config = function(opt, input, path)
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
Loading