fix(lua): vim.validate message param #33675

Problem:
vim.validate does not handle `message` param.

Solution:
Add the missing logic.

(cherry picked from commit 40351bbbbe)
This commit is contained in:
Justin M. Keyes
2025-05-04 16:36:32 -07:00
committed by github-actions[bot]
parent c753e70abb
commit dc87a0d80a
4 changed files with 28 additions and 15 deletions

View File

@ -457,7 +457,8 @@ lsp.config = setmetatable({ _configs = {} }, {
--- @param cfg vim.lsp.Config --- @param cfg vim.lsp.Config
__newindex = function(self, name, cfg) __newindex = function(self, name, cfg)
validate_config_name(name) validate_config_name(name)
validate('cfg', cfg, 'table') local msg = ('table (hint: to resolve a config, use vim.lsp.config["%s"])'):format(name)
validate('cfg', cfg, 'table', msg)
invalidate_enabled_config(name) invalidate_enabled_config(name)
self._configs[name] = cfg self._configs[name] = cfg
end, end,
@ -467,7 +468,8 @@ lsp.config = setmetatable({ _configs = {} }, {
--- @param cfg vim.lsp.Config --- @param cfg vim.lsp.Config
__call = function(self, name, cfg) __call = function(self, name, cfg)
validate_config_name(name) validate_config_name(name)
validate('cfg', cfg, 'table') local msg = ('table (hint: to resolve a config, use vim.lsp.config["%s"])'):format(name)
validate('cfg', cfg, 'table', msg)
invalidate_enabled_config(name) invalidate_enabled_config(name)
self[name] = vim.tbl_deep_extend('force', self._configs[name] or {}, cfg) self[name] = vim.tbl_deep_extend('force', self._configs[name] or {}, cfg)
end, end,

View File

@ -854,7 +854,7 @@ do
--- @param param_name string --- @param param_name string
--- @param val any --- @param val any
--- @param validator vim.validate.Validator --- @param validator vim.validate.Validator
--- @param message? string --- @param message? string "Expected" message
--- @param allow_alias? boolean Allow short type names: 'n', 's', 't', 'b', 'f', 'c' --- @param allow_alias? boolean Allow short type names: 'n', 's', 't', 'b', 'f', 'c'
--- @return string? --- @return string?
local function is_valid(param_name, val, validator, message, allow_alias) local function is_valid(param_name, val, validator, message, allow_alias)
@ -866,18 +866,18 @@ do
end end
if not is_type(val, expected) then if not is_type(val, expected) then
return string.format('%s: expected %s, got %s', param_name, expected, type(val)) return ('%s: expected %s, got %s'):format(param_name, message or expected, type(val))
end end
elseif vim.is_callable(validator) then elseif vim.is_callable(validator) then
-- Check user-provided validation function -- Check user-provided validation function
local valid, opt_msg = validator(val) local valid, opt_msg = validator(val)
if not valid then if not valid then
local err_msg = local err_msg = ('%s: expected %s, got %s'):format(
string.format('%s: expected %s, got %s', param_name, message or '?', tostring(val)) param_name,
message or '?',
if opt_msg then tostring(val)
err_msg = string.format('%s. Info: %s', err_msg, opt_msg) )
end err_msg = opt_msg and ('%s. Info: %s'):format(err_msg, opt_msg) or err_msg
return err_msg return err_msg
end end

View File

@ -1528,11 +1528,16 @@ describe('lua stdlib', function()
pcall_err(exec_lua, "vim.validate('arg1', nil, {'number', 'string'})") pcall_err(exec_lua, "vim.validate('arg1', nil, {'number', 'string'})")
) )
-- Pass an additional message back. -- Validator func can return an extra "Info" message.
matches( matches(
'arg1: expected %?, got 3. Info: TEST_MSG', 'arg1: expected %?, got 3. Info: TEST_MSG',
pcall_err(exec_lua, "vim.validate('arg1', 3, function(a) return a == 1, 'TEST_MSG' end)") pcall_err(exec_lua, "vim.validate('arg1', 3, function(a) return a == 1, 'TEST_MSG' end)")
) )
-- Caller can override the "expected" message.
eq(
'arg1: expected TEST_MSG, got nil',
pcall_err(exec_lua, "vim.validate('arg1', nil, 'table', 'TEST_MSG')")
)
end) end)
it('vim.validate (spec form)', function() it('vim.validate (spec form)', function()

View File

@ -6310,7 +6310,7 @@ describe('LSP', function()
end) end)
describe('vim.lsp.config() and vim.lsp.enable()', function() describe('vim.lsp.config() and vim.lsp.enable()', function()
it('can merge settings from "*"', function() it('merges settings from "*"', function()
eq( eq(
{ {
name = 'foo', name = 'foo',
@ -6326,6 +6326,15 @@ describe('LSP', function()
) )
end) end)
it('config("bogus") shows a hint', function()
matches(
'hint%: to resolve a config',
pcall_err(exec_lua, function()
vim.print(vim.lsp.config('non-existent-config'))
end)
)
end)
it('sets up an autocmd', function() it('sets up an autocmd', function()
eq( eq(
1, 1,
@ -6678,21 +6687,18 @@ describe('LSP', function()
local _ = vim.lsp.config['foo*'] local _ = vim.lsp.config['foo*']
end) end)
) )
matches( matches(
err, err,
pcall_err(exec_lua, function() pcall_err(exec_lua, function()
vim.lsp.config['foo*'] = {} vim.lsp.config['foo*'] = {}
end) end)
) )
matches( matches(
err, err,
pcall_err(exec_lua, function() pcall_err(exec_lua, function()
vim.lsp.config('foo*', {}) vim.lsp.config('foo*', {})
end) end)
) )
-- Exception for '*' -- Exception for '*'
pcall(exec_lua, function() pcall(exec_lua, function()
vim.lsp.config('*', {}) vim.lsp.config('*', {})