From 3f3e4837d5f7d2d9cb1c89bd3a5b2ee8a730772a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 16 Oct 2024 17:03:48 +0100 Subject: perf(validate): use lighter version - Also fix `vim.validate()` for PUC Lua when showing errors for values that aren't string or number. --- runtime/lua/vim/secure.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/secure.lua') diff --git a/runtime/lua/vim/secure.lua b/runtime/lua/vim/secure.lua index 266725cce2..0d3a271ac7 100644 --- a/runtime/lua/vim/secure.lua +++ b/runtime/lua/vim/secure.lua @@ -26,7 +26,7 @@ end --- ---@param trust table Trust table to write local function write_trust(trust) - vim.validate({ trust = { trust, 't' } }) + vim.validate('trust', trust, 'table') local f = assert(io.open(vim.fn.stdpath('state') .. '/trust', 'w')) local t = {} ---@type string[] @@ -49,7 +49,7 @@ end ---@return (string|nil) The contents of the given file if it exists and is --- trusted, or nil otherwise. function M.read(path) - vim.validate({ path = { path, 's' } }) + vim.validate('path', path, 'string') local fullpath = vim.uv.fs_realpath(vim.fs.normalize(path)) if not fullpath then return nil -- cgit From 3572319b4cb1a4163624a5fe328886f1928dbc4a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 18 Oct 2024 11:33:12 +0100 Subject: feat(vim.validate): improve fast form and deprecate spec form Problem: `vim.validate()` takes two forms when it only needs one. Solution: - Teach the fast form all the features of the spec form. - Deprecate the spec form. - General optimizations for both forms. - Add a `message` argument which can be used alongside or in place of the `optional` argument. --- runtime/lua/vim/secure.lua | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'runtime/lua/vim/secure.lua') diff --git a/runtime/lua/vim/secure.lua b/runtime/lua/vim/secure.lua index 0d3a271ac7..7b1d071270 100644 --- a/runtime/lua/vim/secure.lua +++ b/runtime/lua/vim/secure.lua @@ -132,17 +132,11 @@ end ---@return boolean success true if operation was successful ---@return string msg full path if operation was successful, else error message function M.trust(opts) - vim.validate({ - path = { opts.path, 's', true }, - bufnr = { opts.bufnr, 'n', true }, - action = { - opts.action, - function(m) - return m == 'allow' or m == 'deny' or m == 'remove' - end, - [["allow" or "deny" or "remove"]], - }, - }) + vim.validate('path', opts.path, 'string', true) + vim.validate('bufnr', opts.bufnr, 'number', true) + vim.validate('action', opts.action, function(m) + return m == 'allow' or m == 'deny' or m == 'remove' + end, [["allow" or "deny" or "remove"]]) ---@cast opts vim.trust.opts local path = opts.path -- cgit