mirror of
https://github.com/neovim/neovim
synced 2025-07-17 01:31:48 +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))
|
||||
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
|
||||
|
||||
for param_name, spec in vim.spairs(opt) do
|
||||
--- @param param_name string
|
||||
--- @param spec vim.validate.Spec
|
||||
--- @return string?
|
||||
local function is_param_valid(param_name, spec)
|
||||
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
|
||||
|
||||
local val = spec[1] -- Argument value
|
||||
@ -823,17 +819,17 @@ do
|
||||
local error_message =
|
||||
string.format('%s: expected %s, got %s', param_name, (spec[3] or '?'), tostring(val))
|
||||
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
|
||||
|
||||
return false, error_message
|
||||
return error_message
|
||||
end
|
||||
elseif type(types) == 'table' then
|
||||
local success = false
|
||||
for i, t in ipairs(types) do
|
||||
local t_name = type_names[t]
|
||||
if not t_name then
|
||||
return false, string.format('invalid type name: %s', t)
|
||||
return string.format('invalid type name: %s', t)
|
||||
end
|
||||
types[i] = t_name
|
||||
|
||||
@ -843,8 +839,7 @@ do
|
||||
end
|
||||
end
|
||||
if not success then
|
||||
return false,
|
||||
string.format(
|
||||
return string.format(
|
||||
'%s: expected %s, got %s',
|
||||
param_name,
|
||||
table.concat(types, '|'),
|
||||
@ -852,7 +847,30 @@ do
|
||||
)
|
||||
end
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user