Skip to content

Commit 71bfded

Browse files
authored
fix(health): backport new vim.validate behavior (#1776)
1 parent 299e174 commit 71bfded

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

lua/neo-tree/health/init.lua

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,59 @@ local function check_dependencies()
2424
end
2525
end
2626

27+
local vim_validate_new
28+
if vim.fn.has("nvim-0.11") == 1 then
29+
vim_validate_new = vim.validate
30+
else
31+
---@alias neotree.Health.Type type|"callable"
32+
---@alias neotree.Health.Types neotree.Health.Type|(neotree.Health.Type[])
33+
34+
---@param obj any
35+
---@param expected neotree.Health.Types
36+
---@return boolean matches
37+
local matches_type = function(obj, expected)
38+
if type(obj) == expected then
39+
return true
40+
end
41+
if expected == "callable" and vim.is_callable(obj) then
42+
return true
43+
end
44+
return false
45+
end
46+
47+
vim_validate_new = function(name, value, validator, optional, message)
48+
local matched, errmsg, errinfo
49+
if type(validator) == "string" then
50+
matched = matches_type(value, validator)
51+
elseif type(validator) == "table" then
52+
for _, v in ipairs(validator) do
53+
matched = matches_type(value, v)
54+
if matched then
55+
break
56+
end
57+
end
58+
elseif vim.is_callable(validator) and value ~= nil then
59+
matched, errinfo = validator(value)
60+
end
61+
matched = matched or (optional and value == nil)
62+
if not matched then
63+
local expected_types = type(validator) == "string" and { validator } or validator
64+
if optional then
65+
expected_types[#expected_types + 1] = "nil"
66+
end
67+
local expected = vim.is_callable(expected_types) and "?" or table.concat(expected_types, "|")
68+
errmsg = ("%s: %s, got %s"):format(
69+
name,
70+
message or ("expected " .. expected),
71+
message and value or type(value)
72+
)
73+
if errinfo then
74+
errmsg = errmsg .. ", Info: " .. errinfo
75+
end
76+
error(errmsg, 2)
77+
end
78+
end
79+
end
2780
---@param config neotree.Config.Base
2881
function M.check_config(config)
2982
---@type [string, string][]
@@ -55,7 +108,7 @@ function M.check_config(config)
55108
end
56109

57110
-- do regular validate
58-
local valid, errmsg = pcall(vim.validate, full_path .. name, value, validator, optional)
111+
local valid, errmsg = pcall(vim_validate_new, full_path .. name, value, validator, optional)
59112
if not valid then
60113
-- if type(validator) == "string" then
61114
-- advice = advice or ("Change this option to a %s"):format(validator)
@@ -329,14 +382,14 @@ function M.check_config(config)
329382
health.error(unpack(err))
330383
end
331384
end
385+
health.info("(Config schema checking is not comprehensive yet)")
332386
end
333387

334388
function M.check()
335389
health.start("Neo-tree")
336390
check_dependencies()
337391
local config = require("neo-tree").ensure_config()
338392
M.check_config(config)
339-
health.info("(Config schema checking is not comprehensive yet)")
340393
end
341394

342395
return M

0 commit comments

Comments
 (0)