From dc87a0d80a2b2c37f0792d88de63036996016e24 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 4 May 2025 16:36:32 -0700 Subject: [PATCH] fix(lua): vim.validate `message` param #33675 Problem: vim.validate does not handle `message` param. Solution: Add the missing logic. (cherry picked from commit 40351bbbbe7891db9bdeb0d6c3bd95e2e0309775) --- runtime/lua/vim/lsp.lua | 6 ++++-- runtime/lua/vim/shared.lua | 16 ++++++++-------- test/functional/lua/vim_spec.lua | 7 ++++++- test/functional/plugin/lsp_spec.lua | 14 ++++++++++---- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 876319030b..9df84aa9f0 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -457,7 +457,8 @@ lsp.config = setmetatable({ _configs = {} }, { --- @param cfg vim.lsp.Config __newindex = function(self, name, cfg) 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) self._configs[name] = cfg end, @@ -467,7 +468,8 @@ lsp.config = setmetatable({ _configs = {} }, { --- @param cfg vim.lsp.Config __call = function(self, name, cfg) 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) self[name] = vim.tbl_deep_extend('force', self._configs[name] or {}, cfg) end, diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 16414e2828..ce69a22e49 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -854,7 +854,7 @@ do --- @param param_name string --- @param val any --- @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' --- @return string? local function is_valid(param_name, val, validator, message, allow_alias) @@ -866,18 +866,18 @@ do end 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 elseif vim.is_callable(validator) then -- Check user-provided validation function local valid, opt_msg = validator(val) if not valid then - local err_msg = - string.format('%s: expected %s, got %s', param_name, message or '?', tostring(val)) - - if opt_msg then - err_msg = string.format('%s. Info: %s', err_msg, opt_msg) - end + local err_msg = ('%s: expected %s, got %s'):format( + param_name, + message or '?', + tostring(val) + ) + err_msg = opt_msg and ('%s. Info: %s'):format(err_msg, opt_msg) or err_msg return err_msg end diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index c31ce3c678..cbd571439d 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -1528,11 +1528,16 @@ describe('lua stdlib', function() pcall_err(exec_lua, "vim.validate('arg1', nil, {'number', 'string'})") ) - -- Pass an additional message back. + -- Validator func can return an extra "Info" message. matches( 'arg1: expected %?, got 3. Info: TEST_MSG', 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) it('vim.validate (spec form)', function() diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 29e5600368..e05d1572a1 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -6310,7 +6310,7 @@ describe('LSP', function() end) describe('vim.lsp.config() and vim.lsp.enable()', function() - it('can merge settings from "*"', function() + it('merges settings from "*"', function() eq( { name = 'foo', @@ -6326,6 +6326,15 @@ describe('LSP', function() ) 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() eq( 1, @@ -6678,21 +6687,18 @@ describe('LSP', function() local _ = vim.lsp.config['foo*'] end) ) - matches( err, pcall_err(exec_lua, function() vim.lsp.config['foo*'] = {} end) ) - matches( err, pcall_err(exec_lua, function() vim.lsp.config('foo*', {}) end) ) - -- Exception for '*' pcall(exec_lua, function() vim.lsp.config('*', {})