fix: deal with deprecated util methods

This commit is contained in:
Folke Lemaitre 2024-11-07 15:33:32 +01:00
parent 93a5323ba6
commit d455a7f8ce
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
7 changed files with 123 additions and 106 deletions

View file

@ -117,8 +117,8 @@ map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning" })
-- stylua: ignore start
-- toggle options
LazyVim.toggle.format():map("<leader>uf")
LazyVim.toggle.format(true):map("<leader>uF")
LazyVim.format.snacks_toggle():map("<leader>uf")
LazyVim.format.snacks_toggle(true):map("<leader>uF")
Snacks.toggle.option("spell", { name = "Spelling"}):map("<leader>us")
Snacks.toggle.option("wrap", {name = "Wrap"}):map("<leader>uw")
Snacks.toggle.option("relativenumber", { name = "Relative Number"}):map("<leader>uL")
@ -165,7 +165,7 @@ map("n", "<leader>w", "<c-w>", { desc = "Windows", remap = true })
map("n", "<leader>-", "<C-W>s", { desc = "Split Window Below", remap = true })
map("n", "<leader>|", "<C-W>v", { desc = "Split Window Right", remap = true })
map("n", "<leader>wd", "<C-W>c", { desc = "Delete Window", remap = true })
LazyVim.toggle.maximize():map("<leader>wm")
LazyVim.ui.maximize():map("<leader>wm")
-- tabs
map("n", "<leader><tab>l", "<cmd>tablast<cr>", { desc = "Last Tab" })

View file

@ -0,0 +1,59 @@
local M = {}
M.moved = {
lsp = {
rename_file = { "Snacks.rename.rename_file" },
on_rename = { "Snacks.rename.on_rename_file" },
words = { "Snacks.words" },
},
terminal = {
open = { "Snacks.terminal" },
__call = { "Snacks.terminal" },
},
ui = {
statuscolumn = { "Snacks.statuscolumn" },
bufremove = { "Snacks.bufdelete" },
},
}
---@param name string
---@param mod table
function M.decorate(name, mod)
if not M.moved[name] then
return mod
end
setmetatable(mod, {
__call = function(_, ...)
local to = M.moved[name].__call[1]
LazyVim.deprecate("LazyVim." .. name, to)
local ret = vim.tbl_get(_G, unpack(vim.split(to, ".", { plain = true })))
return ret(...)
end,
__index = function(_, k)
if M.moved[name][k] then
local to = M.moved[name][k][1]
LazyVim.deprecate("LazyVim." .. name .. "." .. k, to)
local ret = vim.tbl_get(_G, unpack(vim.split(to, ".", { plain = true })))
return ret
end
return nil
end,
})
end
function M.lazygit()
LazyVim.deprecate("LazyVim.lazygit", "Snacks.lazygit")
return Snacks.lazygit
end
function M.toggle()
LazyVim.deprecate("LazyVim.toggle", "Snacks.toggle")
return {
map = function() end,
wrap = function()
return {}
end,
}
end
return M

View file

@ -177,4 +177,20 @@ function M.setup()
end, { desc = "Show info about the formatters for the current buffer" })
end
---@param buf? boolean
function M.snacks_toggle(buf)
return Snacks.toggle({
name = "Auto Format (" .. (buf and "Buffer" or "Global") .. ")",
get = function()
if not buf then
return vim.g.autoformat == nil or vim.g.autoformat
end
return LazyVim.format.enabled()
end,
set = function(state)
LazyVim.format.enable(state, buf)
end,
})
end
return M

View file

@ -6,7 +6,6 @@ local LazyUtil = require("lazy.core.util")
---@field lsp lazyvim.util.lsp
---@field root lazyvim.util.root
---@field terminal lazyvim.util.terminal
---@field toggle lazyvim.util.toggle
---@field format lazyvim.util.format
---@field plugin lazyvim.util.plugin
---@field extras lazyvim.util.extras
@ -19,36 +18,17 @@ local LazyUtil = require("lazy.core.util")
---@field cmp lazyvim.util.cmp
local M = {}
---@type table<string, string|string[]>
local deprecated = {
get_clients = "lsp",
on_attach = "lsp",
on_rename = "lsp",
root_patterns = { "root", "patterns" },
get_root = { "root", "get" },
float_term = { "terminal", "open" },
toggle_diagnostics = { "toggle", "diagnostics" },
toggle_number = { "toggle", "number" },
fg = "ui",
telescope = "pick",
}
setmetatable(M, {
__index = function(t, k)
if LazyUtil[k] then
return LazyUtil[k]
end
local dep = deprecated[k]
if dep then
local mod = type(dep) == "table" and dep[1] or dep
local key = type(dep) == "table" and dep[2] or k
M.deprecate([[LazyVim.]] .. k, [[LazyVim.]] .. mod .. "." .. key)
---@diagnostic disable-next-line: no-unknown
t[mod] = require("lazyvim.util." .. mod) -- load here to prevent loops
return t[mod][key]
if k == "lazygit" or k == "toggle" then -- HACK: special case for lazygit
return M.deprecated[k]()
end
---@diagnostic disable-next-line: no-unknown
t[k] = require("lazyvim.util." .. k)
M.deprecated.decorate(k, t[k])
return t[k]
end,
})

View file

@ -1,10 +1,5 @@
---@class lazyvim.util.terminal
---@overload fun(cmd: string|string[], opts: snacks.terminal.Opts): snacks.terminal
local M = setmetatable({}, {
__call = function(m, ...)
return m.open(...)
end,
})
local M = {}
---@param shell? string
function M.setup(shell)
@ -37,12 +32,4 @@ function M.setup(shell)
end
end
-- Opens a floating terminal (interactive by default)
---@deprecated use Snacks.terminal instead
---@param cmd? string[]|string
---@param opts? snacks.terminal.Opts
function M.open(cmd, opts)
return Snacks.terminal(cmd, opts)
end
return M

View file

@ -1,61 +0,0 @@
---@class lazyvim.util.toggle
local M = {}
---@param buf? boolean
function M.format(buf)
return Snacks.toggle({
name = "Auto Format (" .. (buf and "Buffer" or "Global") .. ")",
get = function()
if not buf then
return vim.g.autoformat == nil or vim.g.autoformat
end
return LazyVim.format.enabled()
end,
set = function(state)
LazyVim.format.enable(state, buf)
end,
})
end
function M.maximize()
---@type {k:string, v:any}[]?
local maximized = nil
return Snacks.toggle({
name = "Maximize",
get = function()
return maximized ~= nil
end,
set = function(state)
if state then
maximized = {}
local function set(k, v)
table.insert(maximized, 1, { k = k, v = vim.o[k] })
vim.o[k] = v
end
set("winwidth", 999)
set("winheight", 999)
set("winminwidth", 10)
set("winminheight", 4)
vim.cmd("wincmd =")
-- `QuitPre` seems to be executed even if we quit a normal window, so we don't want that
-- `VimLeavePre` might be another consideration? Not sure about differences between the 2
vim.api.nvim_create_autocmd("ExitPre", {
once = true,
group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }),
desc = "Restore width/height when close Neovim while maximized",
callback = function()
M.maximize.set(false)
end,
})
else
for _, opt in ipairs(maximized) do
vim.o[opt.k] = opt.v
end
maximized = nil
vim.cmd("wincmd =")
end
end,
})
end
return M

View file

@ -20,11 +20,6 @@ function M.foldtext()
return ret
end
---@deprecated This is now setup by snacks.nvim
function M.statuscolumn()
return Snacks.statuscolumn()
end
---@return {fg?:string}?
function M.fg(name)
local color = M.color(name)
@ -90,4 +85,45 @@ function M.foldexpr()
return "0"
end
function M.maximize()
---@type {k:string, v:any}[]?
local maximized = nil
return Snacks.toggle({
name = "Maximize",
get = function()
return maximized ~= nil
end,
set = function(state)
if state then
maximized = {}
local function set(k, v)
table.insert(maximized, 1, { k = k, v = vim.o[k] })
vim.o[k] = v
end
set("winwidth", 999)
set("winheight", 999)
set("winminwidth", 10)
set("winminheight", 4)
vim.cmd("wincmd =")
-- `QuitPre` seems to be executed even if we quit a normal window, so we don't want that
-- `VimLeavePre` might be another consideration? Not sure about differences between the 2
vim.api.nvim_create_autocmd("ExitPre", {
once = true,
group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }),
desc = "Restore width/height when close Neovim while maximized",
callback = function()
M.maximize.set(false)
end,
})
else
for _, opt in ipairs(maximized) do
vim.o[opt.k] = opt.v
end
maximized = nil
vim.cmd("wincmd =")
end
end,
})
end
return M