perf(validate): use lighter version

- Also fix `vim.validate()` for PUC Lua when showing errors for values
  that aren't string or number.
This commit is contained in:
Lewis Russell
2024-10-16 17:03:48 +01:00
committed by Lewis Russell
parent fa6ab0d909
commit 3f3e4837d5
19 changed files with 141 additions and 253 deletions

View File

@@ -473,9 +473,7 @@ do
--- @param handle? false|integer --- @param handle? false|integer
--- @return vim.var_accessor --- @return vim.var_accessor
local function make_dict_accessor(scope, handle) local function make_dict_accessor(scope, handle)
validate({ validate('scope', scope, 'string')
scope = { scope, 's' },
})
local mt = {} local mt = {}
function mt:__newindex(k, v) function mt:__newindex(k, v)
return vim._setvar(scope, handle or 0, k, v) return vim._setvar(scope, handle or 0, k, v)
@@ -1171,12 +1169,10 @@ function vim.deprecate(name, alternative, version, plugin, backtrace)
local displayed = vim._truncated_echo_once(msg) local displayed = vim._truncated_echo_once(msg)
return displayed and msg or nil return displayed and msg or nil
else else
vim.validate { vim.validate('name', name, 'string')
name = { name, 'string' }, vim.validate('alternative', alternative, 'string', true)
alternative = { alternative, 'string', true }, vim.validate('version', version, 'string', true)
version = { version, 'string', true }, vim.validate('plugin', plugin, 'string', true)
plugin = { plugin, 'string', true },
}
local msg = ('%s is deprecated'):format(name) local msg = ('%s is deprecated'):format(name)
msg = alternative and ('%s, use %s instead.'):format(msg, alternative) or (msg .. '.') msg = alternative and ('%s, use %s instead.'):format(msg, alternative) or (msg .. '.')

View File

@@ -309,11 +309,9 @@ end
--- @param on_exit? fun(out: vim.SystemCompleted) --- @param on_exit? fun(out: vim.SystemCompleted)
--- @return vim.SystemObj --- @return vim.SystemObj
function M.run(cmd, opts, on_exit) function M.run(cmd, opts, on_exit)
vim.validate({ vim.validate('cmd', cmd, 'table')
cmd = { cmd, 'table' }, vim.validate('opts', opts, 'table', true)
opts = { opts, 'table', true }, vim.validate('on_exit', on_exit, 'function', true)
on_exit = { on_exit, 'function', true },
})
opts = opts or {} opts = opts or {}

View File

@@ -59,11 +59,9 @@ end
--- @param callback vim._watch.Callback Callback for new events --- @param callback vim._watch.Callback Callback for new events
--- @return fun() cancel Stops the watcher --- @return fun() cancel Stops the watcher
function M.watch(path, opts, callback) function M.watch(path, opts, callback)
vim.validate({ vim.validate('path', path, 'string', false)
path = { path, 'string', false }, vim.validate('opts', opts, 'table', true)
opts = { opts, 'table', true }, vim.validate('callback', callback, 'function', false)
callback = { callback, 'function', false },
})
opts = opts or {} opts = opts or {}
@@ -127,11 +125,9 @@ end
--- @param callback vim._watch.Callback Callback for new events --- @param callback vim._watch.Callback Callback for new events
--- @return fun() cancel Stops the watcher --- @return fun() cancel Stops the watcher
function M.watchdirs(path, opts, callback) function M.watchdirs(path, opts, callback)
vim.validate({ vim.validate('path', path, 'string', false)
path = { path, 'string', false }, vim.validate('opts', opts, 'table', true)
opts = { opts, 'table', true }, vim.validate('callback', callback, 'function', false)
callback = { callback, 'function', false },
})
opts = opts or {} opts = opts or {}
local debounce = opts.debounce or 500 local debounce = opts.debounce or 500

View File

@@ -320,7 +320,7 @@ local global_diagnostic_options = {
--- @type table<string,vim.diagnostic.Handler> --- @type table<string,vim.diagnostic.Handler>
M.handlers = setmetatable({}, { M.handlers = setmetatable({}, {
__newindex = function(t, name, handler) __newindex = function(t, name, handler)
vim.validate({ handler = { handler, 't' } }) vim.validate('handler', handler, 'table')
rawset(t, name, handler) rawset(t, name, handler)
if global_diagnostic_options[name] == nil then if global_diagnostic_options[name] == nil then
global_diagnostic_options[name] = true global_diagnostic_options[name] = true
@@ -477,10 +477,8 @@ end
--- @param diagnostics vim.Diagnostic[] --- @param diagnostics vim.Diagnostic[]
--- @return vim.Diagnostic[] --- @return vim.Diagnostic[]
local function reformat_diagnostics(format, diagnostics) local function reformat_diagnostics(format, diagnostics)
vim.validate({ vim.validate('format', format, 'function')
format = { format, 'f' }, vim.validate({ diagnostics = { diagnostics, vim.islist, 'a list of diagnostics' } })
diagnostics = { diagnostics, 't' },
})
local formatted = vim.deepcopy(diagnostics, true) local formatted = vim.deepcopy(diagnostics, true)
for _, diagnostic in ipairs(formatted) do for _, diagnostic in ipairs(formatted) do
@@ -1012,10 +1010,8 @@ end
--- When omitted, update the global diagnostic options. --- When omitted, update the global diagnostic options.
---@return vim.diagnostic.Opts? : Current diagnostic config if {opts} is omitted. ---@return vim.diagnostic.Opts? : Current diagnostic config if {opts} is omitted.
function M.config(opts, namespace) function M.config(opts, namespace)
vim.validate({ vim.validate('opts', opts, 'table', true)
opts = { opts, 't', true }, vim.validate('namespace', namespace, 'number', true)
namespace = { namespace, 'n', true },
})
local t --- @type vim.diagnostic.Opts local t --- @type vim.diagnostic.Opts
if namespace then if namespace then
@@ -1058,16 +1054,10 @@ end
---@param diagnostics vim.Diagnostic[] ---@param diagnostics vim.Diagnostic[]
---@param opts? vim.diagnostic.Opts Display options to pass to |vim.diagnostic.show()| ---@param opts? vim.diagnostic.Opts Display options to pass to |vim.diagnostic.show()|
function M.set(namespace, bufnr, diagnostics, opts) function M.set(namespace, bufnr, diagnostics, opts)
vim.validate({ vim.validate('namespace', namespace, 'number')
namespace = { namespace, 'n' }, vim.validate('bufnr', bufnr, 'number')
bufnr = { bufnr, 'n' }, vim.validate({ diagnostics = { diagnostics, vim.islist, 'a list of diagnostics' } })
diagnostics = { vim.validate('opts', opts, 'table', true)
diagnostics,
vim.islist,
'a list of diagnostics',
},
opts = { opts, 't', true },
})
bufnr = get_bufnr(bufnr) bufnr = get_bufnr(bufnr)
@@ -1092,7 +1082,7 @@ end
---@param namespace integer Diagnostic namespace ---@param namespace integer Diagnostic namespace
---@return vim.diagnostic.NS : Namespace metadata ---@return vim.diagnostic.NS : Namespace metadata
function M.get_namespace(namespace) function M.get_namespace(namespace)
vim.validate({ namespace = { namespace, 'n' } }) vim.validate('namespace', namespace, 'number')
if not all_namespaces[namespace] then if not all_namespaces[namespace] then
local name --- @type string? local name --- @type string?
for k, v in pairs(api.nvim_get_namespaces()) do for k, v in pairs(api.nvim_get_namespaces()) do
@@ -1131,10 +1121,8 @@ end
---@return vim.Diagnostic[] : Fields `bufnr`, `end_lnum`, `end_col`, and `severity` ---@return vim.Diagnostic[] : Fields `bufnr`, `end_lnum`, `end_col`, and `severity`
--- are guaranteed to be present. --- are guaranteed to be present.
function M.get(bufnr, opts) function M.get(bufnr, opts)
vim.validate({ vim.validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true }, vim.validate('opts', opts, 'table', true)
opts = { opts, 't', true },
})
return vim.deepcopy(get_diagnostics(bufnr, opts, false), true) return vim.deepcopy(get_diagnostics(bufnr, opts, false), true)
end end
@@ -1147,10 +1135,8 @@ end
---@return table : Table with actually present severity values as keys ---@return table : Table with actually present severity values as keys
--- (see |diagnostic-severity|) and integer counts as values. --- (see |diagnostic-severity|) and integer counts as values.
function M.count(bufnr, opts) function M.count(bufnr, opts)
vim.validate({ vim.validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true }, vim.validate('opts', opts, 'table', true)
opts = { opts, 't', true },
})
local diagnostics = get_diagnostics(bufnr, opts, false) local diagnostics = get_diagnostics(bufnr, opts, false)
local count = {} --- @type table<integer,integer> local count = {} --- @type table<integer,integer>
@@ -1348,16 +1334,10 @@ end
M.handlers.signs = { M.handlers.signs = {
show = function(namespace, bufnr, diagnostics, opts) show = function(namespace, bufnr, diagnostics, opts)
vim.validate({ vim.validate('namespace', namespace, 'number')
namespace = { namespace, 'n' }, vim.validate('bufnr', bufnr, 'number')
bufnr = { bufnr, 'n' }, vim.validate({ diagnostics = { diagnostics, vim.islist, 'a list of diagnostics' } })
diagnostics = { vim.validate('opts', opts, 'table', true)
diagnostics,
vim.islist,
'a list of diagnostics',
},
opts = { opts, 't', true },
})
bufnr = get_bufnr(bufnr) bufnr = get_bufnr(bufnr)
opts = opts or {} opts = opts or {}
@@ -1475,16 +1455,10 @@ M.handlers.signs = {
M.handlers.underline = { M.handlers.underline = {
show = function(namespace, bufnr, diagnostics, opts) show = function(namespace, bufnr, diagnostics, opts)
vim.validate({ vim.validate('namespace', namespace, 'number')
namespace = { namespace, 'n' }, vim.validate('bufnr', bufnr, 'number')
bufnr = { bufnr, 'n' }, vim.validate({ diagnostics = { diagnostics, vim.islist, 'a list of diagnostics' } })
diagnostics = { vim.validate('opts', opts, 'table', true)
diagnostics,
vim.islist,
'a list of diagnostics',
},
opts = { opts, 't', true },
})
bufnr = get_bufnr(bufnr) bufnr = get_bufnr(bufnr)
opts = opts or {} opts = opts or {}
@@ -1548,16 +1522,10 @@ M.handlers.underline = {
M.handlers.virtual_text = { M.handlers.virtual_text = {
show = function(namespace, bufnr, diagnostics, opts) show = function(namespace, bufnr, diagnostics, opts)
vim.validate({ vim.validate('namespace', namespace, 'number')
namespace = { namespace, 'n' }, vim.validate('bufnr', bufnr, 'number')
bufnr = { bufnr, 'n' }, vim.validate({ diagnostics = { diagnostics, vim.islist, 'a list of diagnostics' } })
diagnostics = { vim.validate('opts', opts, 'table', true)
diagnostics,
vim.islist,
'a list of diagnostics',
},
opts = { opts, 't', true },
})
bufnr = get_bufnr(bufnr) bufnr = get_bufnr(bufnr)
opts = opts or {} opts = opts or {}
@@ -1681,10 +1649,8 @@ end
---@param bufnr integer? Buffer number, or 0 for current buffer. When ---@param bufnr integer? Buffer number, or 0 for current buffer. When
--- omitted, hide diagnostics in all buffers. --- omitted, hide diagnostics in all buffers.
function M.hide(namespace, bufnr) function M.hide(namespace, bufnr)
vim.validate({ vim.validate('namespace', namespace, 'number', true)
namespace = { namespace, 'n', true }, vim.validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true },
})
local buffers = bufnr and { get_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache) local buffers = bufnr and { get_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache)
for _, iter_bufnr in ipairs(buffers) do for _, iter_bufnr in ipairs(buffers) do
@@ -1741,9 +1707,9 @@ end
--- or {bufnr} is nil. --- or {bufnr} is nil.
---@param opts? vim.diagnostic.Opts Display options. ---@param opts? vim.diagnostic.Opts Display options.
function M.show(namespace, bufnr, diagnostics, opts) function M.show(namespace, bufnr, diagnostics, opts)
vim.validate('namespace', namespace, 'number', true)
vim.validate('bufnr', bufnr, 'number', true)
vim.validate({ vim.validate({
namespace = { namespace, 'n', true },
bufnr = { bufnr, 'n', true },
diagnostics = { diagnostics = {
diagnostics, diagnostics,
function(v) function(v)
@@ -1751,8 +1717,8 @@ function M.show(namespace, bufnr, diagnostics, opts)
end, end,
'a list of diagnostics', 'a list of diagnostics',
}, },
opts = { opts, 't', true },
}) })
vim.validate('opts', opts, 'table', true)
if not bufnr or not namespace then if not bufnr or not namespace then
assert(not diagnostics, 'Cannot show diagnostics without a buffer and namespace') assert(not diagnostics, 'Cannot show diagnostics without a buffer and namespace')
@@ -1825,9 +1791,7 @@ function M.open_float(opts, ...)
bufnr = opts bufnr = opts
opts = ... --- @type vim.diagnostic.Opts.Float opts = ... --- @type vim.diagnostic.Opts.Float
else else
vim.validate({ vim.validate('opts', opts, 'table', true)
opts = { opts, 't', true },
})
end end
opts = opts or {} opts = opts or {}
@@ -2038,10 +2002,8 @@ end
---@param bufnr integer? Remove diagnostics for the given buffer. When omitted, ---@param bufnr integer? Remove diagnostics for the given buffer. When omitted,
--- diagnostics are removed for all buffers. --- diagnostics are removed for all buffers.
function M.reset(namespace, bufnr) function M.reset(namespace, bufnr)
vim.validate({ vim.validate('namespace', namespace, 'number', true)
namespace = { namespace, 'n', true }, vim.validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true },
})
local buffers = bufnr and { get_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache) local buffers = bufnr and { get_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache)
for _, iter_bufnr in ipairs(buffers) do for _, iter_bufnr in ipairs(buffers) do
@@ -2144,10 +2106,8 @@ function M.enable(enable, filter)
'0.12' '0.12'
) )
vim.validate({ vim.validate('enable', enable, 'number', true) -- Legacy `bufnr` arg.
enable = { enable, 'n', true }, -- Legacy `bufnr` arg. vim.validate('filter', filter, 'number', true) -- Legacy `namespace` arg.
filter = { filter, 'n', true }, -- Legacy `namespace` arg.
})
local ns_id = type(filter) == 'number' and filter or nil local ns_id = type(filter) == 'number' and filter or nil
filter = {} filter = {}
@@ -2156,10 +2116,8 @@ function M.enable(enable, filter)
enable = true enable = true
else else
filter = filter or {} filter = filter or {}
vim.validate({ vim.validate('enable', enable, 'boolean', true)
enable = { enable, 'b', true }, vim.validate('filter', filter, 'table', true)
filter = { filter, 't', true },
})
end end
enable = enable == nil and true or enable enable = enable == nil and true or enable
@@ -2234,13 +2192,11 @@ end
--- ERROR. --- ERROR.
---@return vim.Diagnostic?: |vim.Diagnostic| structure or `nil` if {pat} fails to match {str}. ---@return vim.Diagnostic?: |vim.Diagnostic| structure or `nil` if {pat} fails to match {str}.
function M.match(str, pat, groups, severity_map, defaults) function M.match(str, pat, groups, severity_map, defaults)
vim.validate({ vim.validate('str', str, 'string')
str = { str, 's' }, vim.validate('pat', pat, 'string')
pat = { pat, 's' }, vim.validate('groups', groups, 'table')
groups = { groups, 't' }, vim.validate('severity_map', severity_map, 'table', true)
severity_map = { severity_map, 't', true }, vim.validate('defaults', defaults, 'table', true)
defaults = { defaults, 't', true },
})
--- @type table<string,vim.diagnostic.Severity> --- @type table<string,vim.diagnostic.Severity>
severity_map = severity_map or M.severity severity_map = severity_map or M.severity
@@ -2283,13 +2239,7 @@ local errlist_type_map = {
---@param diagnostics vim.Diagnostic[] ---@param diagnostics vim.Diagnostic[]
---@return table[] : Quickfix list items |setqflist-what| ---@return table[] : Quickfix list items |setqflist-what|
function M.toqflist(diagnostics) function M.toqflist(diagnostics)
vim.validate({ vim.validate({ diagnostics = { diagnostics, vim.islist, 'a list of diagnostics' } })
diagnostics = {
diagnostics,
vim.islist,
'a list of diagnostics',
},
})
local list = {} --- @type table[] local list = {} --- @type table[]
for _, v in ipairs(diagnostics) do for _, v in ipairs(diagnostics) do
@@ -2323,13 +2273,7 @@ end
---@param list table[] List of quickfix items from |getqflist()| or |getloclist()|. ---@param list table[] List of quickfix items from |getqflist()| or |getloclist()|.
---@return vim.Diagnostic[] ---@return vim.Diagnostic[]
function M.fromqflist(list) function M.fromqflist(list)
vim.validate({ vim.validate('list', list, 'table')
list = {
list,
vim.islist,
'a list of quickfix items',
},
})
local diagnostics = {} --- @type vim.Diagnostic[] local diagnostics = {} --- @type vim.Diagnostic[]
for _, item in ipairs(list) do for _, item in ipairs(list) do

View File

@@ -2736,9 +2736,7 @@ end
--- filetype specific buffer variables). The function accepts a buffer number as --- filetype specific buffer variables). The function accepts a buffer number as
--- its only argument. --- its only argument.
function M.match(args) function M.match(args)
vim.validate({ vim.validate('arg', args, 'table')
arg = { args, 't' },
})
if not (args.buf or args.filename or args.contents) then if not (args.buf or args.filename or args.contents) then
error('At least one of "buf", "filename", or "contents" must be given') error('At least one of "buf", "filename", or "contents" must be given')

View File

@@ -53,7 +53,7 @@ function M.dirname(file)
if file == nil then if file == nil then
return nil return nil
end end
vim.validate({ file = { file, 's' } }) vim.validate('file', file, 'string')
if iswin then if iswin then
file = file:gsub(os_sep, '/') --[[@as string]] file = file:gsub(os_sep, '/') --[[@as string]]
if file:match('^%w:/?$') then if file:match('^%w:/?$') then
@@ -83,7 +83,7 @@ function M.basename(file)
if file == nil then if file == nil then
return nil return nil
end end
vim.validate({ file = { file, 's' } }) vim.validate('file', file, 'string')
if iswin then if iswin then
file = file:gsub(os_sep, '/') --[[@as string]] file = file:gsub(os_sep, '/') --[[@as string]]
if file:match('^%w:/?$') then if file:match('^%w:/?$') then
@@ -123,11 +123,9 @@ end
function M.dir(path, opts) function M.dir(path, opts)
opts = opts or {} opts = opts or {}
vim.validate({ vim.validate('path', path, 'string')
path = { path, { 'string' } }, vim.validate('depth', opts.depth, 'number', true)
depth = { opts.depth, { 'number' }, true }, vim.validate('skip', opts.skip, 'function', true)
skip = { opts.skip, { 'function' }, true },
})
path = M.normalize(path) path = M.normalize(path)
if not opts.depth or opts.depth == 1 then if not opts.depth or opts.depth == 1 then
@@ -231,14 +229,12 @@ end
---@return (string[]) # Normalized paths |vim.fs.normalize()| of all matching items ---@return (string[]) # Normalized paths |vim.fs.normalize()| of all matching items
function M.find(names, opts) function M.find(names, opts)
opts = opts or {} opts = opts or {}
vim.validate({ vim.validate({ names = { names, { 'string', 'table', 'function' } } })
names = { names, { 's', 't', 'f' } }, vim.validate('path', opts.path, 'string', true)
path = { opts.path, 's', true }, vim.validate('upward', opts.upward, 'boolean', true)
upward = { opts.upward, 'b', true }, vim.validate('stop', opts.stop, 'string', true)
stop = { opts.stop, 's', true }, vim.validate('type', opts.type, 'string', true)
type = { opts.type, 's', true }, vim.validate('limit', opts.limit, 'number', true)
limit = { opts.limit, 'n', true },
})
if type(names) == 'string' then if type(names) == 'string' then
names = { names } names = { names }
@@ -547,11 +543,9 @@ function M.normalize(path, opts)
opts = opts or {} opts = opts or {}
if not opts._fast then if not opts._fast then
vim.validate({ vim.validate('path', path, 'string')
path = { path, { 'string' } }, vim.validate('expand_env', opts.expand_env, 'boolean', true)
expand_env = { opts.expand_env, { 'boolean' }, true }, vim.validate('win', opts.win, 'boolean', true)
win = { opts.win, { 'boolean' }, true },
})
end end
local win = opts.win == nil and iswin or not not opts.win local win = opts.win == nil and iswin or not not opts.win

View File

@@ -86,7 +86,7 @@ lsp._request_name_to_capability = {
---@param bufnr (integer|nil) Buffer number to resolve. Defaults to current buffer ---@param bufnr (integer|nil) Buffer number to resolve. Defaults to current buffer
---@return integer bufnr ---@return integer bufnr
local function resolve_bufnr(bufnr) local function resolve_bufnr(bufnr)
validate({ bufnr = { bufnr, 'n', true } }) validate('bufnr', bufnr, 'number', true)
if bufnr == nil or bufnr == 0 then if bufnr == nil or bufnr == 0 then
return api.nvim_get_current_buf() return api.nvim_get_current_buf()
end end
@@ -630,10 +630,8 @@ end
---@param client_id (integer) Client id ---@param client_id (integer) Client id
---@return boolean success `true` if client was attached successfully; `false` otherwise ---@return boolean success `true` if client was attached successfully; `false` otherwise
function lsp.buf_attach_client(bufnr, client_id) function lsp.buf_attach_client(bufnr, client_id)
validate({ validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true }, validate('client_id', client_id, 'number')
client_id = { client_id, 'n' },
})
bufnr = resolve_bufnr(bufnr) bufnr = resolve_bufnr(bufnr)
if not api.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr)) log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr))
@@ -669,10 +667,8 @@ end
---@param bufnr integer Buffer handle, or 0 for current ---@param bufnr integer Buffer handle, or 0 for current
---@param client_id integer Client id ---@param client_id integer Client id
function lsp.buf_detach_client(bufnr, client_id) function lsp.buf_detach_client(bufnr, client_id)
validate({ validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true }, validate('client_id', client_id, 'number')
client_id = { client_id, 'n' },
})
bufnr = resolve_bufnr(bufnr) bufnr = resolve_bufnr(bufnr)
local client = all_clients[client_id] local client = all_clients[client_id]
@@ -773,7 +769,7 @@ end
---@param filter? vim.lsp.get_clients.Filter ---@param filter? vim.lsp.get_clients.Filter
---@return vim.lsp.Client[]: List of |vim.lsp.Client| objects ---@return vim.lsp.Client[]: List of |vim.lsp.Client| objects
function lsp.get_clients(filter) function lsp.get_clients(filter)
validate({ filter = { filter, 't', true } }) validate('filter', filter, 'table', true)
filter = filter or {} filter = filter or {}
@@ -870,12 +866,10 @@ api.nvim_create_autocmd('VimLeavePre', {
---cancel all the requests. You could instead ---cancel all the requests. You could instead
---iterate all clients and call their `cancel_request()` methods. ---iterate all clients and call their `cancel_request()` methods.
function lsp.buf_request(bufnr, method, params, handler, on_unsupported) function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
validate({ validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true }, validate('method', method, 'string')
method = { method, 's' }, validate('handler', handler, 'function', true)
handler = { handler, 'f', true }, validate('on_unsupported', on_unsupported, 'function', true)
on_unsupported = { on_unsupported, 'f', true },
})
bufnr = resolve_bufnr(bufnr) bufnr = resolve_bufnr(bufnr)
local method_supported = false local method_supported = false
@@ -992,10 +986,8 @@ end
--- ---
---@return boolean success true if any client returns true; false otherwise ---@return boolean success true if any client returns true; false otherwise
function lsp.buf_notify(bufnr, method, params) function lsp.buf_notify(bufnr, method, params)
validate({ validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true }, validate('method', method, 'string')
method = { method, 's' },
})
local resp = false local resp = false
for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
if client.rpc.notify(method, params) then if client.rpc.notify(method, params) then

View File

@@ -21,10 +21,8 @@ local M = {}
--- ---
---@see |vim.lsp.buf_request()| ---@see |vim.lsp.buf_request()|
local function request(method, params, handler) local function request(method, params, handler)
validate({ validate('method', method, 'string')
method = { method, 's' }, validate('handler', handler, 'function', true)
handler = { handler, 'f', true },
})
return vim.lsp.buf_request(0, method, params, handler) return vim.lsp.buf_request(0, method, params, handler)
end end
@@ -439,7 +437,7 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
---@param opts? vim.lsp.ListOpts ---@param opts? vim.lsp.ListOpts
function M.references(context, opts) function M.references(context, opts)
validate({ context = { context, 't', true } }) validate('context', context, 'table', true)
local params = util.make_position_params() local params = util.make_position_params()
params.context = context or { params.context = context or {
includeDeclaration = true, includeDeclaration = true,
@@ -857,7 +855,7 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction
---@see vim.lsp.protocol.CodeActionTriggerKind ---@see vim.lsp.protocol.CodeActionTriggerKind
function M.code_action(opts) function M.code_action(opts)
validate({ options = { opts, 't', true } }) validate('options', opts, 'table', true)
opts = opts or {} opts = opts or {}
-- Detect old API call code_action(context) which should now be -- Detect old API call code_action(context) which should now be
-- code_action({ context = context} ) -- code_action({ context = context} )
@@ -935,10 +933,8 @@ end
--- @param command_params lsp.ExecuteCommandParams --- @param command_params lsp.ExecuteCommandParams
--- @see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand --- @see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand
function M.execute_command(command_params) function M.execute_command(command_params)
validate({ validate('command', command_params.command, 'string')
command = { command_params.command, 's' }, validate('arguments', command_params.arguments, 'table', true)
arguments = { command_params.arguments, 't', true },
})
command_params = { command_params = {
command = command_params.command, command = command_params.command,
arguments = command_params.arguments, arguments = command_params.arguments,

View File

@@ -291,7 +291,7 @@ local client_index = 0
--- @param filename (string) path to check --- @param filename (string) path to check
--- @return boolean # true if {filename} exists and is a directory, false otherwise --- @return boolean # true if {filename} exists and is a directory, false otherwise
local function is_dir(filename) local function is_dir(filename)
validate({ filename = { filename, 's' } }) validate('filename', filename, 'string')
local stat = uv.fs_stat(filename) local stat = uv.fs_stat(filename)
return stat and stat.type == 'directory' or false return stat and stat.type == 'directory' or false
end end
@@ -312,9 +312,7 @@ local valid_encodings = {
--- @param encoding string? Encoding to normalize --- @param encoding string? Encoding to normalize
--- @return string # normalized encoding name --- @return string # normalized encoding name
local function validate_encoding(encoding) local function validate_encoding(encoding)
validate({ validate('encoding', encoding, 'string', true)
encoding = { encoding, 's', true },
})
if not encoding then if not encoding then
return valid_encodings.UTF16 return valid_encodings.UTF16
end end
@@ -350,9 +348,7 @@ end
--- Validates a client configuration as given to |vim.lsp.start_client()|. --- Validates a client configuration as given to |vim.lsp.start_client()|.
--- @param config vim.lsp.ClientConfig --- @param config vim.lsp.ClientConfig
local function validate_config(config) local function validate_config(config)
validate({ validate('config', config, 'table')
config = { config, 't' },
})
validate({ validate({
handlers = { config.handlers, 't', true }, handlers = { config.handlers, 't', true },
capabilities = { config.capabilities, 't', true }, capabilities = { config.capabilities, 't', true },
@@ -640,7 +636,7 @@ end
--- @param bufnr (integer|nil) Buffer number to resolve. Defaults to current buffer --- @param bufnr (integer|nil) Buffer number to resolve. Defaults to current buffer
--- @return integer bufnr --- @return integer bufnr
local function resolve_bufnr(bufnr) local function resolve_bufnr(bufnr)
validate({ bufnr = { bufnr, 'n', true } }) validate('bufnr', bufnr, 'number', true)
if bufnr == nil or bufnr == 0 then if bufnr == nil or bufnr == 0 then
return api.nvim_get_current_buf() return api.nvim_get_current_buf()
end end
@@ -806,7 +802,7 @@ end
--- @return boolean status true if notification was successful. false otherwise --- @return boolean status true if notification was successful. false otherwise
--- @see |vim.lsp.client.notify()| --- @see |vim.lsp.client.notify()|
function Client:_cancel_request(id) function Client:_cancel_request(id)
validate({ id = { id, 'n' } }) validate('id', id, 'number')
local request = self.requests[id] local request = self.requests[id]
if request and request.type == 'pending' then if request and request.type == 'pending' then
request.type = 'cancel' request.type = 'cancel'

View File

@@ -195,7 +195,7 @@ local _client_pull_namespaces = {}
---@param client_id integer The id of the LSP client ---@param client_id integer The id of the LSP client
---@param is_pull boolean? Whether the namespace is for a pull or push client. Defaults to push ---@param is_pull boolean? Whether the namespace is for a pull or push client. Defaults to push
function M.get_namespace(client_id, is_pull) function M.get_namespace(client_id, is_pull)
vim.validate({ client_id = { client_id, 'n' } }) vim.validate('client_id', client_id, 'number')
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
if is_pull then if is_pull then

View File

@@ -145,7 +145,7 @@ end
--- @return vim.lsp.inlay_hint.get.ret[] --- @return vim.lsp.inlay_hint.get.ret[]
--- @since 12 --- @since 12
function M.get(filter) function M.get(filter)
vim.validate({ filter = { filter, 'table', true } }) vim.validate('filter', filter, 'table', true)
filter = filter or {} filter = filter or {}
local bufnr = filter.bufnr local bufnr = filter.bufnr
@@ -375,11 +375,11 @@ api.nvim_set_decoration_provider(namespace, {
--- @return boolean --- @return boolean
--- @since 12 --- @since 12
function M.is_enabled(filter) function M.is_enabled(filter)
vim.validate({ filter = { filter, 'table', true } }) vim.validate('filter', filter, 'table', true)
filter = filter or {} filter = filter or {}
local bufnr = filter.bufnr local bufnr = filter.bufnr
vim.validate({ bufnr = { bufnr, 'number', true } }) vim.validate('bufnr', bufnr, 'number', true)
if bufnr == nil then if bufnr == nil then
return globalstate.enabled return globalstate.enabled
elseif bufnr == 0 then elseif bufnr == 0 then
@@ -406,7 +406,8 @@ end
--- @param filter vim.lsp.inlay_hint.enable.Filter? --- @param filter vim.lsp.inlay_hint.enable.Filter?
--- @since 12 --- @since 12
function M.enable(enable, filter) function M.enable(enable, filter)
vim.validate({ enable = { enable, 'boolean', true }, filter = { filter, 'table', true } }) vim.validate('enable', enable, 'boolean', true)
vim.validate('filter', filter, 'table', true)
enable = enable == nil or enable enable = enable == nil or enable
filter = filter or {} filter = filter or {}

View File

@@ -152,9 +152,7 @@ end
---@param err table The error object ---@param err table The error object
---@return string error_message The formatted error message ---@return string error_message The formatted error message
function M.format_rpc_error(err) function M.format_rpc_error(err)
validate({ validate('err', err, 'table')
err = { err, 't' },
})
-- There is ErrorCodes in the LSP specification, -- There is ErrorCodes in the LSP specification,
-- but in ResponseError.code it is not used and the actual type is number. -- but in ResponseError.code it is not used and the actual type is number.
@@ -329,10 +327,8 @@ end
---@return boolean success `true` if request could be sent, `false` if not ---@return boolean success `true` if request could be sent, `false` if not
---@return integer? message_id if request could be sent, `nil` if not ---@return integer? message_id if request could be sent, `nil` if not
function Client:request(method, params, callback, notify_reply_callback) function Client:request(method, params, callback, notify_reply_callback)
validate({ validate('callback', callback, 'function')
callback = { callback, 'f' }, validate('notify_reply_callback', notify_reply_callback, 'function', true)
notify_reply_callback = { notify_reply_callback, 'f', true },
})
self.message_index = self.message_index + 1 self.message_index = self.message_index + 1
local message_id = self.message_index local message_id = self.message_index
local result = self:encode_and_send({ local result = self:encode_and_send({
@@ -465,9 +461,7 @@ function Client:handle_body(body)
local notify_reply_callbacks = self.notify_reply_callbacks local notify_reply_callbacks = self.notify_reply_callbacks
local notify_reply_callback = notify_reply_callbacks and notify_reply_callbacks[result_id] local notify_reply_callback = notify_reply_callbacks and notify_reply_callbacks[result_id]
if notify_reply_callback then if notify_reply_callback then
validate({ validate('notify_reply_callback', notify_reply_callback, 'function')
notify_reply_callback = { notify_reply_callback, 'f' },
})
notify_reply_callback(result_id) notify_reply_callback(result_id)
notify_reply_callbacks[result_id] = nil notify_reply_callbacks[result_id] = nil
end end
@@ -498,9 +492,7 @@ function Client:handle_body(body)
local callback = message_callbacks and message_callbacks[result_id] local callback = message_callbacks and message_callbacks[result_id]
if callback then if callback then
message_callbacks[result_id] = nil message_callbacks[result_id] = nil
validate({ validate('callback', callback, 'function')
callback = { callback, 'f' },
})
if decoded.error then if decoded.error then
decoded.error = setmetatable(decoded.error, { decoded.error = setmetatable(decoded.error, {
__tostring = M.format_rpc_error, __tostring = M.format_rpc_error,
@@ -734,10 +726,8 @@ end
function M.start(cmd, dispatchers, extra_spawn_params) function M.start(cmd, dispatchers, extra_spawn_params)
log.info('Starting RPC client', { cmd = cmd, extra = extra_spawn_params }) log.info('Starting RPC client', { cmd = cmd, extra = extra_spawn_params })
validate({ validate('cmd', cmd, 'table')
cmd = { cmd, 't' }, validate('dispatchers', dispatchers, 'table', true)
dispatchers = { dispatchers, 't', true },
})
extra_spawn_params = extra_spawn_params or {} extra_spawn_params = extra_spawn_params or {}

View File

@@ -565,10 +565,8 @@ local M = {}
--- - debounce (integer, default: 200): Debounce token requests --- - debounce (integer, default: 200): Debounce token requests
--- to the server by the given number in milliseconds --- to the server by the given number in milliseconds
function M.start(bufnr, client_id, opts) function M.start(bufnr, client_id, opts)
vim.validate({ vim.validate('bufnr', bufnr, 'number')
bufnr = { bufnr, 'n', false }, vim.validate('client_id', client_id, 'number')
client_id = { client_id, 'n', false },
})
if bufnr == 0 then if bufnr == 0 then
bufnr = api.nvim_get_current_buf() bufnr = api.nvim_get_current_buf()
@@ -622,10 +620,8 @@ end
---@param bufnr (integer) Buffer number, or `0` for current buffer ---@param bufnr (integer) Buffer number, or `0` for current buffer
---@param client_id (integer) The ID of the |vim.lsp.Client| ---@param client_id (integer) The ID of the |vim.lsp.Client|
function M.stop(bufnr, client_id) function M.stop(bufnr, client_id)
vim.validate({ vim.validate('bufnr', bufnr, 'number')
bufnr = { bufnr, 'n', false }, vim.validate('client_id', client_id, 'number')
client_id = { client_id, 'n', false },
})
if bufnr == 0 then if bufnr == 0 then
bufnr = api.nvim_get_current_buf() bufnr = api.nvim_get_current_buf()
@@ -708,9 +704,7 @@ end
---@param bufnr (integer|nil) filter by buffer. All buffers if nil, current ---@param bufnr (integer|nil) filter by buffer. All buffers if nil, current
--- buffer if 0 --- buffer if 0
function M.force_refresh(bufnr) function M.force_refresh(bufnr)
vim.validate({ vim.validate('bufnr', bufnr, 'number', true)
bufnr = { bufnr, 'n', true },
})
local buffers = bufnr == nil and vim.tbl_keys(STHighlighter.active) local buffers = bufnr == nil and vim.tbl_keys(STHighlighter.active)
or bufnr == 0 and { api.nvim_get_current_buf() } or bufnr == 0 and { api.nvim_get_current_buf() }

View File

@@ -26,7 +26,7 @@ end
--- ---
---@param trust table<string, string> Trust table to write ---@param trust table<string, string> Trust table to write
local function write_trust(trust) 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 f = assert(io.open(vim.fn.stdpath('state') .. '/trust', 'w'))
local t = {} ---@type string[] local t = {} ---@type string[]
@@ -49,7 +49,7 @@ end
---@return (string|nil) The contents of the given file if it exists and is ---@return (string|nil) The contents of the given file if it exists and is
--- trusted, or nil otherwise. --- trusted, or nil otherwise.
function M.read(path) function M.read(path)
vim.validate({ path = { path, 's' } }) vim.validate('path', path, 'string')
local fullpath = vim.uv.fs_realpath(vim.fs.normalize(path)) local fullpath = vim.uv.fs_realpath(vim.fs.normalize(path))
if not fullpath then if not fullpath then
return nil return nil

View File

@@ -109,7 +109,9 @@ function vim.gsplit(s, sep, opts)
if type(opts) == 'boolean' then if type(opts) == 'boolean' then
plain = opts -- For backwards compatibility. plain = opts -- For backwards compatibility.
else else
vim.validate({ s = { s, 's' }, sep = { sep, 's' }, opts = { opts, 't', true } }) vim.validate('s', s, 'string')
vim.validate('sep', sep, 'string')
vim.validate('opts', opts, 'table', true)
opts = opts or {} opts = opts or {}
plain, trimempty = opts.plain, opts.trimempty plain, trimempty = opts.plain, opts.trimempty
end end
@@ -303,7 +305,8 @@ end
---@param opts? vim.tbl_contains.Opts Keyword arguments |kwargs|: ---@param opts? vim.tbl_contains.Opts Keyword arguments |kwargs|:
---@return boolean `true` if `t` contains `value` ---@return boolean `true` if `t` contains `value`
function vim.tbl_contains(t, value, opts) function vim.tbl_contains(t, value, opts)
vim.validate({ t = { t, 't' }, opts = { opts, 't', true } }) vim.validate('t', t, 'table')
vim.validate('opts', opts, 'table', true)
--- @cast t table<any,any> --- @cast t table<any,any>
local pred --- @type fun(v: any): boolean? local pred --- @type fun(v: any): boolean?
@@ -550,12 +553,10 @@ end
---@param finish integer? Final index on src. Defaults to `#src` ---@param finish integer? Final index on src. Defaults to `#src`
---@return T dst ---@return T dst
function vim.list_extend(dst, src, start, finish) function vim.list_extend(dst, src, start, finish)
vim.validate({ vim.validate('dst', dst, 'table')
dst = { dst, 't' }, vim.validate('src', src, 'table')
src = { src, 't' }, vim.validate('start', start, 'number', true)
start = { start, 'n', true }, vim.validate('finish', finish, 'number', true)
finish = { finish, 'n', true },
})
for i = start or 1, finish or #src do for i = start or 1, finish or #src do
table.insert(dst, src[i]) table.insert(dst, src[i])
end end
@@ -987,6 +988,10 @@ do
ok = (actual == expected) or (v == nil and optional == true) ok = (actual == expected) or (v == nil and optional == true)
if not ok then if not ok then
if not jit and (actual ~= 'string' or actual ~= 'number') then
-- PUC-Lua can only handle string and number for %s in string.format()
v = vim.inspect(v)
end
err_msg = ('%s: expected %s, got %s%s'):format( err_msg = ('%s: expected %s, got %s%s'):format(
name, name,
expected, expected,

View File

@@ -133,10 +133,8 @@ end
--- ---
---@return vim.treesitter.LanguageTree object to use for parsing ---@return vim.treesitter.LanguageTree object to use for parsing
function M.get_string_parser(str, lang, opts) function M.get_string_parser(str, lang, opts)
vim.validate({ vim.validate('str', str, 'string')
str = { str, 'string' }, vim.validate('lang', lang, 'string')
lang = { lang, 'string' },
})
return LanguageTree.new(str, lang, opts) return LanguageTree.new(str, lang, opts)
end end

View File

@@ -330,9 +330,7 @@ end
--- ---
--- @param opts vim.treesitter.dev.inspect_tree.Opts? --- @param opts vim.treesitter.dev.inspect_tree.Opts?
function M.inspect_tree(opts) function M.inspect_tree(opts)
vim.validate({ vim.validate('opts', opts, 'table', true)
opts = { opts, 't', true },
})
opts = opts or {} opts = opts or {}

View File

@@ -108,11 +108,9 @@ function M.add(lang, opts)
local path = opts.path local path = opts.path
local symbol_name = opts.symbol_name local symbol_name = opts.symbol_name
vim.validate({ vim.validate('lang', lang, 'string')
lang = { lang, 'string' }, vim.validate('path', path, 'string', true)
path = { path, 'string', true }, vim.validate('symbol_name', symbol_name, 'string', true)
symbol_name = { symbol_name, 'string', true },
})
-- parser names are assumed to be lowercase (consistent behavior on case-insensitive file systems) -- parser names are assumed to be lowercase (consistent behavior on case-insensitive file systems)
lang = lang:lower() lang = lang:lower()

View File

@@ -37,10 +37,8 @@ local M = {}
--- `idx` is the 1-based index of `item` within `items`. --- `idx` is the 1-based index of `item` within `items`.
--- `nil` if the user aborted the dialog. --- `nil` if the user aborted the dialog.
function M.select(items, opts, on_choice) function M.select(items, opts, on_choice)
vim.validate({ vim.validate('items', items, 'table', false)
items = { items, 'table', false }, vim.validate('on_choice', on_choice, 'function', false)
on_choice = { on_choice, 'function', false },
})
opts = opts or {} opts = opts or {}
local choices = { opts.prompt or 'Select one of:' } local choices = { opts.prompt or 'Select one of:' }
local format_item = opts.format_item or tostring local format_item = opts.format_item or tostring
@@ -86,10 +84,8 @@ end
--- an empty string if nothing was entered), or --- an empty string if nothing was entered), or
--- `nil` if the user aborted the dialog. --- `nil` if the user aborted the dialog.
function M.input(opts, on_confirm) function M.input(opts, on_confirm)
vim.validate({ vim.validate('opts', opts, 'table', true)
opts = { opts, 'table', true }, vim.validate('on_confirm', on_confirm, 'function', false)
on_confirm = { on_confirm, 'function', false },
})
opts = (opts and not vim.tbl_isempty(opts)) and opts or vim.empty_dict() opts = (opts and not vim.tbl_isempty(opts)) and opts or vim.empty_dict()
@@ -135,9 +131,7 @@ end
--- ---
---@see |vim.system()| ---@see |vim.system()|
function M.open(path, opt) function M.open(path, opt)
vim.validate({ vim.validate('path', path, 'string')
path = { path, 'string' },
})
local is_uri = path:match('%w+:') local is_uri = path:match('%w+:')
if not is_uri then if not is_uri then
path = vim.fs.normalize(path) path = vim.fs.normalize(path)