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 17, 2024
1 parent 9f2c279 commit e74f7b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
42 changes: 15 additions & 27 deletions lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ 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 },
}
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)

if config_def.default_config.deprecate then
vim.deprecate(
Expand All @@ -54,10 +52,8 @@ function configs.__newindex(t, config_name, config_def)

if config_def.commands then
for k, v in pairs(config_def.commands) do
validate {
['command.name'] = { k, 's' },
['command.fn'] = { v[1], 'f' },
}
validate('command.name', k, 'string')
validate('command.fn', v[1], 'function')
end
else
config_def.commands = {}
Expand All @@ -74,24 +70,16 @@ 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 },
}
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, 's' },
['command.fn'] = { v[1], 'f' },
}
validate('command.name', k, 'string')
validate('command.fn', v[1], 'function')
end
end

Expand Down
26 changes: 14 additions & 12 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function M.bufname_valid(bufname)
end

function M.validate_bufnr(bufnr)
validate {
bufnr = { bufnr, 'n' },
}
validate('bufnr', bufnr, 'number')
return bufnr == 0 and api.nvim_get_current_buf() or bufnr
end

Expand Down Expand Up @@ -174,7 +172,7 @@ M.path = (function()
end)()

function M.search_ancestors(startpath, func)
validate { func = { func, 'f' } }
validate('func', func, 'function')
if func(startpath) then
return startpath
end
Expand All @@ -192,14 +190,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 +316,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 e74f7b5

Please sign in to comment.