From 8d51d13b1c057c38999d7f82b1be196fe6b5f73a Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Tue, 5 Dec 2023 02:36:11 +0100 Subject: [PATCH] feat(health): warn on unrecognized configs / unsourced `vim.g.rocks_nvim` (#45) --- lua/rocks/config/check.lua | 28 ++++++++++++++++++++++++++++ lua/rocks/config/internal.lua | 18 ++++++++++++++++++ lua/rocks/health.lua | 6 ++---- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lua/rocks/config/check.lua b/lua/rocks/config/check.lua index 0fb9bae9..e701bd66 100644 --- a/lua/rocks/config/check.lua +++ b/lua/rocks/config/check.lua @@ -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 diff --git a/lua/rocks/config/internal.lua b/lua/rocks/config/internal.lua index f9aaa66d..5382d37c 100644 --- a/lua/rocks/config/internal.lua +++ b/lua/rocks/config/internal.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/rocks/health.lua b/lua/rocks/health.lua index d55d2ae4..9a7efee6 100644 --- a/lua/rocks/health.lua +++ b/lua/rocks/health.lua @@ -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