mirror of
https://github.com/neovim/neovim
synced 2025-07-18 10:11:50 +00:00
perf(lua): avoid spairs in vim.validate happy path
Problem: `vim.validate` is too slow, mainly because of `vim.spairs`. Solution: Collect all errors in via `pairs`, and sort the errors via `spairs`.
This commit is contained in:
committed by
Lewis Russell
parent
ffb4b50e74
commit
cdd87222c8
@ -796,16 +796,12 @@ do
|
|||||||
return type(val) == t or (t == 'callable' and vim.is_callable(val))
|
return type(val) == t or (t == 'callable' and vim.is_callable(val))
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @param opt table<vim.validate.Type,vim.validate.Spec>
|
--- @param param_name string
|
||||||
--- @return boolean, string?
|
--- @param spec vim.validate.Spec
|
||||||
local function is_valid(opt)
|
--- @return string?
|
||||||
if type(opt) ~= 'table' then
|
local function is_param_valid(param_name, spec)
|
||||||
return false, string.format('opt: expected table, got %s', type(opt))
|
|
||||||
end
|
|
||||||
|
|
||||||
for param_name, spec in vim.spairs(opt) do
|
|
||||||
if type(spec) ~= 'table' then
|
if type(spec) ~= 'table' then
|
||||||
return false, string.format('opt[%s]: expected table, got %s', param_name, type(spec))
|
return string.format('opt[%s]: expected table, got %s', param_name, type(spec))
|
||||||
end
|
end
|
||||||
|
|
||||||
local val = spec[1] -- Argument value
|
local val = spec[1] -- Argument value
|
||||||
@ -823,17 +819,17 @@ do
|
|||||||
local error_message =
|
local error_message =
|
||||||
string.format('%s: expected %s, got %s', param_name, (spec[3] or '?'), tostring(val))
|
string.format('%s: expected %s, got %s', param_name, (spec[3] or '?'), tostring(val))
|
||||||
if optional_message ~= nil then
|
if optional_message ~= nil then
|
||||||
error_message = error_message .. string.format('. Info: %s', optional_message)
|
error_message = string.format('%s. Info: %s', error_message, optional_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
return false, error_message
|
return error_message
|
||||||
end
|
end
|
||||||
elseif type(types) == 'table' then
|
elseif type(types) == 'table' then
|
||||||
local success = false
|
local success = false
|
||||||
for i, t in ipairs(types) do
|
for i, t in ipairs(types) do
|
||||||
local t_name = type_names[t]
|
local t_name = type_names[t]
|
||||||
if not t_name then
|
if not t_name then
|
||||||
return false, string.format('invalid type name: %s', t)
|
return string.format('invalid type name: %s', t)
|
||||||
end
|
end
|
||||||
types[i] = t_name
|
types[i] = t_name
|
||||||
|
|
||||||
@ -843,8 +839,7 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not success then
|
if not success then
|
||||||
return false,
|
return string.format(
|
||||||
string.format(
|
|
||||||
'%s: expected %s, got %s',
|
'%s: expected %s, got %s',
|
||||||
param_name,
|
param_name,
|
||||||
table.concat(types, '|'),
|
table.concat(types, '|'),
|
||||||
@ -852,7 +847,30 @@ do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
return false, string.format('invalid type name: %s', tostring(types))
|
return string.format('invalid type name: %s', tostring(types))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param opt table<vim.validate.Type,vim.validate.Spec>
|
||||||
|
--- @return boolean, string?
|
||||||
|
local function is_valid(opt)
|
||||||
|
if type(opt) ~= 'table' then
|
||||||
|
return false, string.format('opt: expected table, got %s', type(opt))
|
||||||
|
end
|
||||||
|
|
||||||
|
local report --- @type table<string,string>?
|
||||||
|
|
||||||
|
for param_name, spec in pairs(opt) do
|
||||||
|
local msg = is_param_valid(param_name, spec)
|
||||||
|
if msg then
|
||||||
|
report = report or {}
|
||||||
|
report[param_name] = msg
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if report then
|
||||||
|
for _, msg in vim.spairs(report) do -- luacheck: ignore
|
||||||
|
return false, msg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user