Skip to content

Commit

Permalink
perf: Validate configuration in checkhealth
Browse files Browse the repository at this point in the history
  • Loading branch information
Isrothy committed Oct 31, 2024
1 parent 4d3e56a commit 9b21120
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
9 changes: 1 addition & 8 deletions lua/neominimap/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
local user_config = type(vim.g.neominimap) == "function" and vim.g.neominimap() or vim.g.neominimap or {}

local cfg = vim.tbl_deep_extend("force", require("neominimap.config.internal"), user_config)

local ok, err = require("neominimap.config.validator").validate_config(cfg)
if not ok then
vim.notify("neominimap: invalid config: " .. err, vim.log.levels.ERROR)
end

return cfg
return vim.tbl_deep_extend("force", require("neominimap.config.internal"), user_config)
3 changes: 2 additions & 1 deletion lua/neominimap/config/validator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ end)
local is_array_of_handlers = is_array_of(is_handler)

---@param cfg Neominimap.Internal.Config
---@return boolean
---@return boolean is_valid
---@return string|nil error_message
M.validate_config = function(cfg)
return validate_path("vim.g.neominimap", {
auto_enable = { cfg.auto_enable, "boolean" },
Expand Down
59 changes: 40 additions & 19 deletions lua/neominimap/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,35 @@ local function lualib_available(name)
return ok
end

M.check = function()
local health = vim.health or require("health")

-- Polyfill deprecated health api
if vim.fn.has("nvim-0.10") ~= 1 then
---@diagnostic disable: deprecated
health = {
start = health.report_start,
ok = health.report_ok,
info = health.report_info,
warn = health.report_warn,
error = health.report_error,
}
---@diagnostic enable: deprecated
end

-- Check Neovim version
local check_neovim_version = function(health)
if vim.fn.has("nvim-0.10") == 1 then
health.ok("Neovim version is 0.10 or higher")
else
health.error("Neovim version is lower than 0.10")
end
end

local check_lua_env = function(health)
if not _G.jit then
health.error(
"Not running on LuaJIT! Non-JIT Lua runtimes are not officially supported by the plugin. Mileage may vary."
)
end
end

local check_configuration = function(health)
local cfg = require("neominimap.config")
local ok, err = require("neominimap.config.validator").validate_config(cfg)
if ok then
health.ok("neominimap: valid config")
else
health.error("neominimap: invalid config: " .. err)
end
end

local check_treesitter = function(health)
if config.treesitter.enabled then
health.info("TreeSitter integration is enabled")
-- Check for TreeSitter
if lualib_available("nvim-treesitter") then
health.ok("TreeSitter is installed")
local parsers = require("nvim-treesitter.parsers").available_parsers()
Expand All @@ -53,10 +50,11 @@ M.check = function()
else
health.info("TreeSitter is disabled")
end
end

local check_git = function(health)
if config.git.enabled then
health.info("Gitsigns integration is enabled")
-- Check for gitsigns
if lualib_available("gitsigns") then
health.ok("Gitsigns is installed")
else
Expand All @@ -67,4 +65,27 @@ M.check = function()
end
end

M.check = function()
local health = vim.health or require("health")

-- Polyfill deprecated health api
if vim.fn.has("nvim-0.10") ~= 1 then
---@diagnostic disable: deprecated
health = {
start = health.report_start,
ok = health.report_ok,
info = health.report_info,
warn = health.report_warn,
error = health.report_error,
}
---@diagnostic enable: deprecated
end

check_neovim_version(health)
check_lua_env(health)
check_configuration(health)
check_treesitter(health)
check_git(health)
end

return M
3 changes: 3 additions & 0 deletions plugin/neominimap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ end, {
if config.auto_enable then
require("neominimap.autocmds").create_autocmds()
end

-- local logger = require("neominimap.logger")
-- logger.log(vim.inspect(package.loaded), vim.log.levels.DEBUG)

0 comments on commit 9b21120

Please sign in to comment.