Skip to content

Commit

Permalink
refactor: fix luals warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dundargoc committed Dec 18, 2024
1 parent 9f2c279 commit 653e826
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
55 changes: 25 additions & 30 deletions lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -34,13 +35,13 @@ end
---@param config_name string
---@param config_def table Config definition read from `lspconfig.configs.<name>`.
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(
Expand All @@ -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 = {}
Expand All @@ -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

Expand Down
30 changes: 18 additions & 12 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin/lspconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 653e826

Please sign in to comment.