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(health): warn on unrecognized configs / unsourced g.rocks_nvim #45

Merged
merged 1 commit into from
Dec 5, 2023
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: 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
Loading