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
--- @return vim.var_accessor
local function make_dict_accessor(scope, handle)
validate({
scope = { scope, 's' },
})
validate('scope', scope, 'string')
local mt = {}
function mt:__newindex(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)
return displayed and msg or nil
else
vim.validate {
name = { name, 'string' },
alternative = { alternative, 'string', true },
version = { version, 'string', true },
plugin = { plugin, 'string', true },
}
vim.validate('name', name, 'string')
vim.validate('alternative', alternative, 'string', true)
vim.validate('version', version, 'string', true)
vim.validate('plugin', plugin, 'string', true)
local msg = ('%s is deprecated'):format(name)
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)
--- @return vim.SystemObj
function M.run(cmd, opts, on_exit)
vim.validate({
cmd = { cmd, 'table' },
opts = { opts, 'table', true },
on_exit = { on_exit, 'function', true },
})
vim.validate('cmd', cmd, 'table')
vim.validate('opts', opts, 'table', true)
vim.validate('on_exit', on_exit, 'function', true)
opts = opts or {}

View File

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

View File

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

View File

@ -2736,9 +2736,7 @@ end
--- filetype specific buffer variables). The function accepts a buffer number as
--- its only argument.
function M.match(args)
vim.validate({
arg = { args, 't' },
})
vim.validate('arg', args, 'table')
if not (args.buf or args.filename or args.contents) then
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
return nil
end
vim.validate({ file = { file, 's' } })
vim.validate('file', file, 'string')
if iswin then
file = file:gsub(os_sep, '/') --[[@as string]]
if file:match('^%w:/?$') then
@ -83,7 +83,7 @@ function M.basename(file)
if file == nil then
return nil
end
vim.validate({ file = { file, 's' } })
vim.validate('file', file, 'string')
if iswin then
file = file:gsub(os_sep, '/') --[[@as string]]
if file:match('^%w:/?$') then
@ -123,11 +123,9 @@ end
function M.dir(path, opts)
opts = opts or {}
vim.validate({
path = { path, { 'string' } },
depth = { opts.depth, { 'number' }, true },
skip = { opts.skip, { 'function' }, true },
})
vim.validate('path', path, 'string')
vim.validate('depth', opts.depth, 'number', true)
vim.validate('skip', opts.skip, 'function', true)
path = M.normalize(path)
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
function M.find(names, opts)
opts = opts or {}
vim.validate({
names = { names, { 's', 't', 'f' } },
path = { opts.path, 's', true },
upward = { opts.upward, 'b', true },
stop = { opts.stop, 's', true },
type = { opts.type, 's', true },
limit = { opts.limit, 'n', true },
})
vim.validate({ names = { names, { 'string', 'table', 'function' } } })
vim.validate('path', opts.path, 'string', true)
vim.validate('upward', opts.upward, 'boolean', true)
vim.validate('stop', opts.stop, 'string', true)
vim.validate('type', opts.type, 'string', true)
vim.validate('limit', opts.limit, 'number', true)
if type(names) == 'string' then
names = { names }
@ -547,11 +543,9 @@ function M.normalize(path, opts)
opts = opts or {}
if not opts._fast then
vim.validate({
path = { path, { 'string' } },
expand_env = { opts.expand_env, { 'boolean' }, true },
win = { opts.win, { 'boolean' }, true },
})
vim.validate('path', path, 'string')
vim.validate('expand_env', opts.expand_env, 'boolean', true)
vim.validate('win', opts.win, 'boolean', true)
end
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
---@return integer bufnr
local function resolve_bufnr(bufnr)
validate({ bufnr = { bufnr, 'n', true } })
validate('bufnr', bufnr, 'number', true)
if bufnr == nil or bufnr == 0 then
return api.nvim_get_current_buf()
end
@ -630,10 +630,8 @@ end
---@param client_id (integer) Client id
---@return boolean success `true` if client was attached successfully; `false` otherwise
function lsp.buf_attach_client(bufnr, client_id)
validate({
bufnr = { bufnr, 'n', true },
client_id = { client_id, 'n' },
})
validate('bufnr', bufnr, 'number', true)
validate('client_id', client_id, 'number')
bufnr = resolve_bufnr(bufnr)
if not api.nvim_buf_is_loaded(bufnr) then
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 client_id integer Client id
function lsp.buf_detach_client(bufnr, client_id)
validate({
bufnr = { bufnr, 'n', true },
client_id = { client_id, 'n' },
})
validate('bufnr', bufnr, 'number', true)
validate('client_id', client_id, 'number')
bufnr = resolve_bufnr(bufnr)
local client = all_clients[client_id]
@ -773,7 +769,7 @@ end
---@param filter? vim.lsp.get_clients.Filter
---@return vim.lsp.Client[]: List of |vim.lsp.Client| objects
function lsp.get_clients(filter)
validate({ filter = { filter, 't', true } })
validate('filter', filter, 'table', true)
filter = filter or {}
@ -870,12 +866,10 @@ api.nvim_create_autocmd('VimLeavePre', {
---cancel all the requests. You could instead
---iterate all clients and call their `cancel_request()` methods.
function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
validate({
bufnr = { bufnr, 'n', true },
method = { method, 's' },
handler = { handler, 'f', true },
on_unsupported = { on_unsupported, 'f', true },
})
validate('bufnr', bufnr, 'number', true)
validate('method', method, 'string')
validate('handler', handler, 'function', true)
validate('on_unsupported', on_unsupported, 'function', true)
bufnr = resolve_bufnr(bufnr)
local method_supported = false
@ -992,10 +986,8 @@ end
---
---@return boolean success true if any client returns true; false otherwise
function lsp.buf_notify(bufnr, method, params)
validate({
bufnr = { bufnr, 'n', true },
method = { method, 's' },
})
validate('bufnr', bufnr, 'number', true)
validate('method', method, 'string')
local resp = false
for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
if client.rpc.notify(method, params) then

View File

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

View File

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

View File

@ -195,7 +195,7 @@ local _client_pull_namespaces = {}
---@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
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)
if is_pull then

View File

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

View File

@ -152,9 +152,7 @@ end
---@param err table The error object
---@return string error_message The formatted error message
function M.format_rpc_error(err)
validate({
err = { err, 't' },
})
validate('err', err, 'table')
-- There is ErrorCodes in the LSP specification,
-- 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 integer? message_id if request could be sent, `nil` if not
function Client:request(method, params, callback, notify_reply_callback)
validate({
callback = { callback, 'f' },
notify_reply_callback = { notify_reply_callback, 'f', true },
})
validate('callback', callback, 'function')
validate('notify_reply_callback', notify_reply_callback, 'function', true)
self.message_index = self.message_index + 1
local message_id = self.message_index
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_callback = notify_reply_callbacks and notify_reply_callbacks[result_id]
if notify_reply_callback then
validate({
notify_reply_callback = { notify_reply_callback, 'f' },
})
validate('notify_reply_callback', notify_reply_callback, 'function')
notify_reply_callback(result_id)
notify_reply_callbacks[result_id] = nil
end
@ -498,9 +492,7 @@ function Client:handle_body(body)
local callback = message_callbacks and message_callbacks[result_id]
if callback then
message_callbacks[result_id] = nil
validate({
callback = { callback, 'f' },
})
validate('callback', callback, 'function')
if decoded.error then
decoded.error = setmetatable(decoded.error, {
__tostring = M.format_rpc_error,
@ -734,10 +726,8 @@ end
function M.start(cmd, dispatchers, extra_spawn_params)
log.info('Starting RPC client', { cmd = cmd, extra = extra_spawn_params })
validate({
cmd = { cmd, 't' },
dispatchers = { dispatchers, 't', true },
})
validate('cmd', cmd, 'table')
validate('dispatchers', dispatchers, 'table', true)
extra_spawn_params = extra_spawn_params or {}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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