From 653e826f1eeb39c9a0f1af6b2d81d204b3066309 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Tue, 17 Dec 2024 12:11:31 +0100 Subject: [PATCH] refactor: fix luals warnings --- lua/lspconfig/configs.lua | 55 ++++++++++++++++++--------------------- lua/lspconfig/util.lua | 30 ++++++++++++--------- plugin/lspconfig.lua | 2 +- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua index b4f9ca083a..a79f9868a0 100644 --- a/lua/lspconfig/configs.lua +++ b/lua/lspconfig/configs.lua @@ -2,6 +2,7 @@ local util = require 'lspconfig.util' local async = require 'lspconfig.async' local api, validate, lsp, fn = vim.api, vim.validate, vim.lsp, vim.fn local tbl_deep_extend = vim.tbl_deep_extend +local nvim_eleven = vim.fn.has 'nvim-0.11' == 1 local configs = {} @@ -34,13 +35,13 @@ end ---@param config_name string ---@param config_def table Config definition read from `lspconfig.configs.`. function configs.__newindex(t, config_name, config_def) - validate { - name = { config_name, 's' }, - default_config = { config_def.default_config, 't' }, - on_new_config = { config_def.on_new_config, 'f', true }, - on_attach = { config_def.on_attach, 'f', true }, - commands = { config_def.commands, 't', true }, - } + if nvim_eleven then + validate('name', config_name, 'string') + validate('default_config', config_def.default_config, 'table') + validate('on_new_config', config_def.on_new_config, 'function', true) + validate('on_attach', config_def.on_attach, 'function', true) + validate('commands', config_def.commands, 'table', true) + end if config_def.default_config.deprecate then vim.deprecate( @@ -53,11 +54,11 @@ function configs.__newindex(t, config_name, config_def) end if config_def.commands then - for k, v in pairs(config_def.commands) do - validate { - ['command.name'] = { k, 's' }, - ['command.fn'] = { v[1], 'f' }, - } + if nvim_eleven then + for k, v in pairs(config_def.commands) do + validate('command.name', k, 'string') + validate('command.fn', v[1], 'function') + end end else config_def.commands = {} @@ -74,24 +75,18 @@ function configs.__newindex(t, config_name, config_def) function M.setup(user_config) local lsp_group = api.nvim_create_augroup('lspconfig', { clear = false }) - validate { - cmd = { - user_config.cmd, - { 'f', 't' }, - true, - }, - root_dir = { user_config.root_dir, { 's', 'f' }, true }, - filetypes = { user_config.filetype, 't', true }, - on_new_config = { user_config.on_new_config, 'f', true }, - on_attach = { user_config.on_attach, 'f', true }, - commands = { user_config.commands, 't', true }, - } - if user_config.commands then - for k, v in pairs(user_config.commands) do - validate { - ['command.name'] = { k, 's' }, - ['command.fn'] = { v[1], 'f' }, - } + if nvim_eleven then + validate('cmd', user_config.cmd, { 'function', 'table' }, true) + validate('root_dir', user_config.root_dir, { 'string', 'function' }, true) + validate('filetypes', user_config.filetype, 'table', true) + validate('on_new_config', user_config.on_new_config, 'function', true) + validate('on_attach', user_config.on_attach, 'function', true) + validate('commands', user_config.commands, 'table', true) + if user_config.commands then + for k, v in pairs(user_config.commands) do + validate('command.name', k, 'string') + validate('command.fn', v[1], 'function') + end end end diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 7de9e8b856..e15d1b1ba7 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -28,9 +28,9 @@ function M.bufname_valid(bufname) end function M.validate_bufnr(bufnr) - validate { - bufnr = { bufnr, 'n' }, - } + if nvim_eleven then + validate('bufnr', bufnr, 'number') + end return bufnr == 0 and api.nvim_get_current_buf() or bufnr end @@ -174,7 +174,9 @@ M.path = (function() end)() function M.search_ancestors(startpath, func) - validate { func = { func, 'f' } } + if nvim_eleven then + validate('func', func, 'function') + end if func(startpath) then return startpath end @@ -192,14 +194,6 @@ function M.search_ancestors(startpath, func) end end -function M.tbl_flatten(t) - return nvim_eleven and vim.iter(t):flatten(math.huge):totable() or vim.tbl_flatten(t) -end - -function M.get_lsp_clients(filter) - return nvim_eleven and lsp.get_clients(filter) or lsp.get_active_clients(filter) -end - local function escape_wildcards(path) return path:gsub('([%[%]%?%*])', '\\%1') end @@ -326,6 +320,18 @@ function M.strip_archive_subpath(path) return path end +--- Functions that can be removed once minimum required neovim version is high enough + +function M.tbl_flatten(t) + --- @diagnostic disable-next-line:deprecated + return nvim_eleven and vim.iter(t):flatten(math.huge):totable() or vim.tbl_flatten(t) +end + +function M.get_lsp_clients(filter) + --- @diagnostic disable-next-line:deprecated + return nvim_eleven and lsp.get_clients(filter) or lsp.get_active_clients(filter) +end + --- Deprecated functions --- @deprecated use `vim.fn.isdirectory(path) == 1` instead diff --git a/plugin/lspconfig.lua b/plugin/lspconfig.lua index a852d967a6..0283d84732 100644 --- a/plugin/lspconfig.lua +++ b/plugin/lspconfig.lua @@ -45,7 +45,7 @@ local get_clients_from_cmd_args = function(arg) return '' end) for id in (arg or ''):gmatch '(%d+)' do - local client = lsp.get_client_by_id(tonumber(id)) + local client = lsp.get_client_by_id(assert(tonumber(id))) if client == nil then err_msg = err_msg .. ('client id "%s" not found\n'):format(id) end