From 6b837e9165a02a334259b87fd251d617cffd9cdc Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 8 Oct 2023 10:45:38 +0200 Subject: [PATCH] style: lua annotations and handle deprecated methods --- lua/lazyvim/plugins/lsp/format.lua | 2 +- lua/lazyvim/plugins/lsp/keymaps.lua | 5 ++--- lua/lazyvim/util/init.lua | 26 +++++++++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lua/lazyvim/plugins/lsp/format.lua b/lua/lazyvim/plugins/lsp/format.lua index 7d1c9109..f507a38a 100644 --- a/lua/lazyvim/plugins/lsp/format.lua +++ b/lua/lazyvim/plugins/lsp/format.lua @@ -82,7 +82,7 @@ function M.get_formatters(bufnr) } ---@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 if M.supports_format(client) then if (#null_ls > 0 and client.name == "null-ls") or #null_ls == 0 then diff --git a/lua/lazyvim/plugins/lsp/keymaps.lua b/lua/lazyvim/plugins/lsp/keymaps.lua index dd48691a..ad05b334 100644 --- a/lua/lazyvim/plugins/lsp/keymaps.lua +++ b/lua/lazyvim/plugins/lsp/keymaps.lua @@ -66,7 +66,7 @@ end ---@param method string function M.has(buffer, 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 if client.supports_method(method) then return true @@ -83,7 +83,7 @@ function M.resolve(buffer) end local spec = M.get() 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 local maps = opts.servers[client.name] and opts.servers[client.name].keys or {} vim.list_extend(spec, maps) @@ -98,7 +98,6 @@ function M.on_attach(_, buffer) for _, keys in pairs(keymaps) do if not keys.has or M.has(buffer, keys.has) then local opts = Keys.opts(keys) - ---@diagnostic disable-next-line: no-unknown opts.has = nil opts.silent = opts.silent ~= false opts.buffer = buffer diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index cceca322..ff01a3d0 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -8,7 +8,7 @@ M.root_patterns = { ".git", "lua" } function M.on_attach(on_attach) vim.api.nvim_create_autocmd("LspAttach", { 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) on_attach(client, buffer) end, @@ -22,9 +22,11 @@ end function M.fg(name) ---@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 fg = hl and hl.fg or hl.foreground - return fg and { fg = string.format("#%06x", fg) } + ---@diagnostic disable-next-line: undefined-field + local fg = hl and (hl.fg or hl.foreground) + return fg and { fg = string.format("#%06x", fg) } or nil end ---@param fn fun() @@ -47,6 +49,8 @@ function M.opts(name) return Plugin.values(plugin, "opts", false) end +M.get_clients = vim.lsp.get_clients or vim.lsp_get_active_clients + -- returns the root directory based on: -- * lsp workspace folders -- * lsp root_dir @@ -60,7 +64,7 @@ function M.get_root() ---@type string[] local roots = {} 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 paths = workspace and vim.tbl_map(function(ws) return vim.uri_to_fname(ws.uri) @@ -227,7 +231,7 @@ function M.lazy_notify() vim.notify = temp local timer = vim.loop.new_timer() - local check = vim.loop.new_check() + local check = assert(vim.loop.new_check()) local replay = function() timer:stop() @@ -253,6 +257,7 @@ function M.lazy_notify() timer:start(500, 0, replay) end +---@return _.lspconfig.options function M.lsp_get_config(server) local configs = require("lspconfig.configs") return rawget(configs, server) @@ -263,6 +268,7 @@ end function M.lsp_disable(server, cond) local util = require("lspconfig.util") 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) if cond(root_dir, config) then config.enabled = false @@ -302,9 +308,10 @@ end ---@param from string ---@param to string function M.on_rename(from, to) - local clients = vim.lsp.get_active_clients() + local clients = M.get_clients() for _, client in ipairs(clients) do if client.supports_method("workspace/willRenameFiles") then + ---@diagnostic disable-next-line: invisible local resp = client.request_sync("workspace/willRenameFiles", { files = { { @@ -312,7 +319,7 @@ function M.on_rename(from, to) newUri = vim.uri_from_fname(to), }, }, - }, 1000) + }, 1000, 0) if resp and resp.result ~= nil then vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding) end @@ -328,8 +335,9 @@ function M.safe_keymap_set(mode, lhs, rhs, opts) ---@cast keys LazyKeysHandler local modes = type(mode) == "string" and { mode } or mode - modes = vim.tbl_filter(function(mode) - return not (keys.have and keys:have(lhs, mode)) + ---@param m string + modes = vim.tbl_filter(function(m) + return not (keys.have and keys:have(lhs, m)) end, modes) -- do not create the keymap if a lazy keys handler exists