style: lua annotations and handle deprecated methods

This commit is contained in:
Folke Lemaitre 2023-10-08 10:45:38 +02:00
parent 21ee35f710
commit 6b837e9165
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 20 additions and 13 deletions

View file

@ -82,7 +82,7 @@ function M.get_formatters(bufnr)
} }
---@type lsp.Client[] ---@type lsp.Client[]
local clients = vim.lsp.get_active_clients({ bufnr = bufnr }) local clients = require("lazyvim.util").get_clients({ bufnr = bufnr })
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
if M.supports_format(client) then if M.supports_format(client) then
if (#null_ls > 0 and client.name == "null-ls") or #null_ls == 0 then if (#null_ls > 0 and client.name == "null-ls") or #null_ls == 0 then

View file

@ -66,7 +66,7 @@ end
---@param method string ---@param method string
function M.has(buffer, method) function M.has(buffer, method)
method = method:find("/") and method or "textDocument/" .. method method = method:find("/") and method or "textDocument/" .. method
local clients = vim.lsp.get_active_clients({ bufnr = buffer }) local clients = require("lazyvim.util").get_clients({ bufnr = buffer })
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
if client.supports_method(method) then if client.supports_method(method) then
return true return true
@ -83,7 +83,7 @@ function M.resolve(buffer)
end end
local spec = M.get() local spec = M.get()
local opts = require("lazyvim.util").opts("nvim-lspconfig") local opts = require("lazyvim.util").opts("nvim-lspconfig")
local clients = vim.lsp.get_active_clients({ bufnr = buffer }) local clients = require("lazyvim.util").get_clients({ bufnr = buffer })
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
local maps = opts.servers[client.name] and opts.servers[client.name].keys or {} local maps = opts.servers[client.name] and opts.servers[client.name].keys or {}
vim.list_extend(spec, maps) vim.list_extend(spec, maps)
@ -98,7 +98,6 @@ function M.on_attach(_, buffer)
for _, keys in pairs(keymaps) do for _, keys in pairs(keymaps) do
if not keys.has or M.has(buffer, keys.has) then if not keys.has or M.has(buffer, keys.has) then
local opts = Keys.opts(keys) local opts = Keys.opts(keys)
---@diagnostic disable-next-line: no-unknown
opts.has = nil opts.has = nil
opts.silent = opts.silent ~= false opts.silent = opts.silent ~= false
opts.buffer = buffer opts.buffer = buffer

View file

@ -8,7 +8,7 @@ M.root_patterns = { ".git", "lua" }
function M.on_attach(on_attach) function M.on_attach(on_attach)
vim.api.nvim_create_autocmd("LspAttach", { vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args) callback = function(args)
local buffer = args.buf local buffer = args.buf ---@type number
local client = vim.lsp.get_client_by_id(args.data.client_id) local client = vim.lsp.get_client_by_id(args.data.client_id)
on_attach(client, buffer) on_attach(client, buffer)
end, end,
@ -22,9 +22,11 @@ end
function M.fg(name) function M.fg(name)
---@type {foreground?:number}? ---@type {foreground?:number}?
---@diagnostic disable-next-line: deprecated
local hl = vim.api.nvim_get_hl and vim.api.nvim_get_hl(0, { name = name }) or vim.api.nvim_get_hl_by_name(name, true) local hl = vim.api.nvim_get_hl and vim.api.nvim_get_hl(0, { name = name }) or vim.api.nvim_get_hl_by_name(name, true)
local fg = hl and hl.fg or hl.foreground ---@diagnostic disable-next-line: undefined-field
return fg and { fg = string.format("#%06x", fg) } local fg = hl and (hl.fg or hl.foreground)
return fg and { fg = string.format("#%06x", fg) } or nil
end end
---@param fn fun() ---@param fn fun()
@ -47,6 +49,8 @@ function M.opts(name)
return Plugin.values(plugin, "opts", false) return Plugin.values(plugin, "opts", false)
end end
M.get_clients = vim.lsp.get_clients or vim.lsp_get_active_clients
-- returns the root directory based on: -- returns the root directory based on:
-- * lsp workspace folders -- * lsp workspace folders
-- * lsp root_dir -- * lsp root_dir
@ -60,7 +64,7 @@ function M.get_root()
---@type string[] ---@type string[]
local roots = {} local roots = {}
if path then if path then
for _, client in pairs(vim.lsp.get_active_clients({ bufnr = 0 })) do for _, client in pairs(M.get_clients({ bufnr = 0 })) do
local workspace = client.config.workspace_folders local workspace = client.config.workspace_folders
local paths = workspace and vim.tbl_map(function(ws) local paths = workspace and vim.tbl_map(function(ws)
return vim.uri_to_fname(ws.uri) return vim.uri_to_fname(ws.uri)
@ -227,7 +231,7 @@ function M.lazy_notify()
vim.notify = temp vim.notify = temp
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
local check = vim.loop.new_check() local check = assert(vim.loop.new_check())
local replay = function() local replay = function()
timer:stop() timer:stop()
@ -253,6 +257,7 @@ function M.lazy_notify()
timer:start(500, 0, replay) timer:start(500, 0, replay)
end end
---@return _.lspconfig.options
function M.lsp_get_config(server) function M.lsp_get_config(server)
local configs = require("lspconfig.configs") local configs = require("lspconfig.configs")
return rawget(configs, server) return rawget(configs, server)
@ -263,6 +268,7 @@ end
function M.lsp_disable(server, cond) function M.lsp_disable(server, cond)
local util = require("lspconfig.util") local util = require("lspconfig.util")
local def = M.lsp_get_config(server) local def = M.lsp_get_config(server)
---@diagnostic disable-next-line: undefined-field
def.document_config.on_new_config = util.add_hook_before(def.document_config.on_new_config, function(config, root_dir) def.document_config.on_new_config = util.add_hook_before(def.document_config.on_new_config, function(config, root_dir)
if cond(root_dir, config) then if cond(root_dir, config) then
config.enabled = false config.enabled = false
@ -302,9 +308,10 @@ end
---@param from string ---@param from string
---@param to string ---@param to string
function M.on_rename(from, to) function M.on_rename(from, to)
local clients = vim.lsp.get_active_clients() local clients = M.get_clients()
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
if client.supports_method("workspace/willRenameFiles") then if client.supports_method("workspace/willRenameFiles") then
---@diagnostic disable-next-line: invisible
local resp = client.request_sync("workspace/willRenameFiles", { local resp = client.request_sync("workspace/willRenameFiles", {
files = { files = {
{ {
@ -312,7 +319,7 @@ function M.on_rename(from, to)
newUri = vim.uri_from_fname(to), newUri = vim.uri_from_fname(to),
}, },
}, },
}, 1000) }, 1000, 0)
if resp and resp.result ~= nil then if resp and resp.result ~= nil then
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding) vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
end end
@ -328,8 +335,9 @@ function M.safe_keymap_set(mode, lhs, rhs, opts)
---@cast keys LazyKeysHandler ---@cast keys LazyKeysHandler
local modes = type(mode) == "string" and { mode } or mode local modes = type(mode) == "string" and { mode } or mode
modes = vim.tbl_filter(function(mode) ---@param m string
return not (keys.have and keys:have(lhs, mode)) modes = vim.tbl_filter(function(m)
return not (keys.have and keys:have(lhs, m))
end, modes) end, modes)
-- do not create the keymap if a lazy keys handler exists -- do not create the keymap if a lazy keys handler exists