Skip to content

Commit

Permalink
feat(health): warn on unrecognized configs / unsourced `vim.g.rocks_n…
Browse files Browse the repository at this point in the history
…vim` (#45)
  • Loading branch information
mrcjkb authored Dec 5, 2023
1 parent 7f92e60 commit 8d51d13
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
28 changes: 28 additions & 0 deletions lua/rocks/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,32 @@ function check.validate(cfg)
return true
end

---Recursively check a table for unrecognized keys,
---using a default table as a reference
---@param tbl table
---@param default_tbl table
---@return string[]
function check.get_unrecognized_keys(tbl, default_tbl)
local unrecognized_keys = {}
for k, _ in pairs(tbl) do
unrecognized_keys[k] = true
end
for k, _ in pairs(default_tbl) do
unrecognized_keys[k] = false
end
local ret = {}
for k, _ in pairs(unrecognized_keys) do
if unrecognized_keys[k] then
ret[k] = k
end
if type(default_tbl[k]) == "table" and tbl[k] then
for _, subk in pairs(check.get_unrecognized_keys(tbl[k], default_tbl[k])) do
local key = k .. "." .. subk
ret[key] = key
end
end
end
return vim.tbl_keys(ret)
end

return check
18 changes: 18 additions & 0 deletions lua/rocks/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
---@field rocks_path string Local path in your filesystem to install rocks
---@field config_path string Rocks declaration file path
---@field luarocks_binary string Luarocks binary path
---@field debug_info RocksConfigDebugInfo

---@class (exact) RocksConfigDebugInfo
---@field was_g_rocks_nvim_sourced boolean
---@field unrecognized_configs string[]

--- rocks.nvim default configuration
---@type RocksConfig
Expand All @@ -28,6 +33,10 @@ local default_config = {
---@diagnostic disable-next-line: param-type-mismatch
config_path = vim.fs.joinpath(vim.fn.stdpath("config"), "rocks.toml"),
luarocks_binary = "luarocks",
debug_info = {
was_g_rocks_nvim_sourced = vim.g.rocks_nvim ~= nil,
unrecognized_configs = {},
},
}

---@type RocksOpts
Expand All @@ -41,6 +50,15 @@ if not ok then
vim.notify("Rocks: " .. err, vim.log.levels.ERROR)
end

config.debug_info.urecognized_configs = check.get_unrecognized_keys(opts, default_config)

if #config.debug_info.unrecognized_configs > 0 then
vim.notify(
"unrecognized configs found in vim.g.rocks_nvim: " .. vim.inspect(config.debug_info.unrecognized_configs),
vim.log.levels.WARN
)
end

return config

--- config.lua ends here
6 changes: 2 additions & 4 deletions lua/rocks/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ end

local function check_config()
start("Checking config")
if vim.g.rocks_nvim then
ok("vim.g.rocks_nvim is set")
else
ok("vim.g.rocks_nvim is not set")
if vim.g.rocks_nvim and not config.debug_info.was_g_rocks_nvim_sourced then
warn("unrecognized configs in vim.g.rocks_nvim: " .. vim.inspect(config.debug_info.unrecognized_configs))
end
local valid, err = require("rocks.config.check").validate(config)
if valid then
Expand Down

0 comments on commit 8d51d13

Please sign in to comment.