Skip to content

Commit

Permalink
feat: config extra key validation
Browse files Browse the repository at this point in the history
don't allow unsupported keys in the configuration
  • Loading branch information
lukas-reineke committed Oct 2, 2023
1 parent af58360 commit 458298a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lua/ibl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,29 @@ local validate_config = function(config)
return
end

vim.validate {
utils.validate({
enabled = { config.enabled, "boolean", true },
viewport_buffer = { config.viewport_buffer, "table", true },
indent = { config.indent, "table", true },
whitespace = { config.whitespace, "table", true },
scope = { config.scope, "table", true },
exclude = { config.exclude, "table", true },
}
}, config, "ibl.config")

if config.viewport_buffer then
vim.validate {
utils.validate({
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
vim.validate {
utils.validate({
char = { config.indent.char, { "string", "table" }, true },
highlight = { config.indent.highlight, { "string", "table" }, true },
smart_indent_cap = { config.indent.smart_indent_cap, "boolean", true },
priority = { config.indent.priority, "number", true },
}
}, config.indent, "ibl.config.indent")
if config.indent.char then
vim.validate {
char = {
Expand Down Expand Up @@ -163,10 +163,10 @@ local validate_config = function(config)
end

if config.whitespace then
vim.validate {
utils.validate({
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 {
tab_char = {
Expand All @@ -181,7 +181,7 @@ local validate_config = function(config)
end

if config.scope then
vim.validate {
utils.validate({
enabled = { config.scope.enabled, "boolean", true },
show_start = { config.scope.show_start, "boolean", true },
show_end = { config.scope.show_end, "boolean", true },
Expand All @@ -190,7 +190,7 @@ local validate_config = function(config)
priority = { config.scope.priority, "number", true },
include = { config.scope.include, "table", true },
exclude = { config.scope.exclude, "table", true },
}
}, config.scope, "ibl.config.scope")
if config.scope.char then
vim.validate {
char = {
Expand All @@ -212,24 +212,24 @@ local validate_config = function(config)
}
end
if config.scope.exclude then
vim.validate {
utils.validate({
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
vim.validate {
utils.validate({
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
vim.validate {
utils.validate({
filetypes = { config.exclude.filetypes, "table", true },
buftypes = { config.exclude.buftypes, "table", true },
}
}, config.exclude, "ibl.config.exclude")
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lua/ibl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ M.get_whitespace = function(line)
return string.match(line, "^%s+") or ""
end

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

---@class ibl.listchars
---@field tabstop_overwrite boolean
---@field space_char string
Expand Down

0 comments on commit 458298a

Please sign in to comment.