Files
LazyVim/lua/lazyvim/util/init.lua
Folke Lemaitre 39a908c9fa feat(pick): move pickers to extras (telescope still the default) (#3606)
## What is this PR for?

Refactoring of pickers in LazyVim:

- [x] telescope moved to extras
- [x] dressing was moved to the telescope extra. Not needed with fzf-lua
and noice
- [x] when none of the two is enabled, then telescope will be enabled
- [x] when using `:LazyExtras` to enable fzf-lua, the telescope spec
will never be parsed
- [x] when not using `:LazyExras`, the spec will be parsed, but one of
the two will be disabled.
- [x] only one picker extra can be used to prevent issues
- [ ] cleanup lsp keymaps
2024-06-13 06:07:02 +02:00

291 lines
7.5 KiB
Lua

local LazyUtil = require("lazy.core.util")
---@class lazyvim.util: LazyUtilCore
---@field config LazyVimConfig
---@field ui lazyvim.util.ui
---@field lsp lazyvim.util.lsp
---@field root lazyvim.util.root
---@field telescope lazyvim.util.telescope
---@field terminal lazyvim.util.terminal
---@field lazygit lazyvim.util.lazygit
---@field toggle lazyvim.util.toggle
---@field format lazyvim.util.format
---@field plugin lazyvim.util.plugin
---@field extras lazyvim.util.extras
---@field inject lazyvim.util.inject
---@field news lazyvim.util.news
---@field json lazyvim.util.json
---@field lualine lazyvim.util.lualine
---@field mini lazyvim.util.mini
---@field pick lazyvim.util.pick
---@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]
end
---@diagnostic disable-next-line: no-unknown
t[k] = require("lazyvim.util." .. k)
return t[k]
end,
})
function M.is_win()
return vim.uv.os_uname().sysname:find("Windows") ~= nil
end
---@param name string
function M.get_plugin(name)
return require("lazy.core.config").spec.plugins[name]
end
---@param name string
---@param path string?
function M.get_plugin_path(name, path)
local plugin = M.get_plugin(name)
path = path and "/" .. path or ""
return plugin and (plugin.dir .. path)
end
---@param plugin string
function M.has(plugin)
return M.get_plugin(plugin) ~= nil
end
---@param extra string
function M.has_extra(extra)
local Config = require("lazyvim.config")
local modname = "lazyvim.plugins.extras." .. extra
return vim.tbl_contains(require("lazy.core.config").spec.modules, modname)
or vim.tbl_contains(Config.json.data.extras, modname)
end
---@param fn fun()
function M.on_very_lazy(fn)
vim.api.nvim_create_autocmd("User", {
pattern = "VeryLazy",
callback = function()
fn()
end,
})
end
--- This extends a deeply nested list with a key in a table
--- that is a dot-separated string.
--- The nested list will be created if it does not exist.
---@generic T
---@param t T[]
---@param key string
---@param values T[]
---@return T[]?
function M.extend(t, key, values)
local keys = vim.split(key, ".", { plain = true })
for i = 1, #keys do
local k = keys[i]
t[k] = t[k] or {}
if type(t) ~= "table" then
return
end
t = t[k]
end
return vim.list_extend(t, values)
end
---@param name string
function M.opts(name)
local plugin = require("lazy.core.config").spec.plugins[name]
if not plugin then
return {}
end
local Plugin = require("lazy.core.plugin")
return Plugin.values(plugin, "opts", false)
end
function M.deprecate(old, new)
M.warn(("`%s` is deprecated. Please use `%s` instead"):format(old, new), {
title = "LazyVim",
once = true,
stacktrace = true,
stacklevel = 6,
})
end
-- delay notifications till vim.notify was replaced or after 500ms
function M.lazy_notify()
local notifs = {}
local function temp(...)
table.insert(notifs, vim.F.pack_len(...))
end
local orig = vim.notify
vim.notify = temp
local timer = vim.uv.new_timer()
local check = assert(vim.uv.new_check())
local replay = function()
timer:stop()
check:stop()
if vim.notify == temp then
vim.notify = orig -- put back the original notify if needed
end
vim.schedule(function()
---@diagnostic disable-next-line: no-unknown
for _, notif in ipairs(notifs) do
vim.notify(vim.F.unpack_len(notif))
end
end)
end
-- wait till vim.notify has been replaced
check:start(function()
if vim.notify ~= temp then
replay()
end
end)
-- or if it took more than 500ms, then something went wrong
timer:start(500, 0, replay)
end
function M.is_loaded(name)
local Config = require("lazy.core.config")
return Config.plugins[name] and Config.plugins[name]._.loaded
end
---@param name string
---@param fn fun(name:string)
function M.on_load(name, fn)
if M.is_loaded(name) then
fn(name)
else
vim.api.nvim_create_autocmd("User", {
pattern = "LazyLoad",
callback = function(event)
if event.data == name then
fn(name)
return true
end
end,
})
end
end
-- Wrapper around vim.keymap.set that will
-- not create a keymap if a lazy key handler exists.
-- It will also set `silent` to true by default.
function M.safe_keymap_set(mode, lhs, rhs, opts)
local keys = require("lazy.core.handler").handlers.keys
---@cast keys LazyKeysHandler
local modes = type(mode) == "string" and { mode } or 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
if #modes > 0 then
opts = opts or {}
opts.silent = opts.silent ~= false
if opts.remap and not vim.g.vscode then
---@diagnostic disable-next-line: no-unknown
opts.remap = nil
end
vim.keymap.set(modes, lhs, rhs, opts)
end
end
---@generic T
---@param list T[]
---@return T[]
function M.dedup(list)
local ret = {}
local seen = {}
for _, v in ipairs(list) do
if not seen[v] then
table.insert(ret, v)
seen[v] = true
end
end
return ret
end
M.CREATE_UNDO = vim.api.nvim_replace_termcodes("<c-G>u", true, true, true)
function M.create_undo()
if vim.api.nvim_get_mode().mode == "i" then
vim.api.nvim_feedkeys(M.CREATE_UNDO, "n", false)
end
end
--- Gets a path to a package in the Mason registry.
--- Prefer this to `get_package`, since the package might not always be
--- available yet and trigger errors.
---@param pkg string
---@param path? string
---@param opts? { warn?: boolean }
function M.get_pkg_path(pkg, path, opts)
pcall(require, "mason") -- make sure Mason is loaded. Will fail when generating docs
local root = vim.env.MASON or (vim.fn.stdpath("data") .. "/mason")
opts = opts or {}
opts.warn = opts.warn == nil and true or opts.warn
path = path or ""
local ret = root .. "/packages/" .. pkg .. "/" .. path
if opts.warn and not vim.loop.fs_stat(ret) and not require("lazy.core.config").headless() then
M.warn(
("Mason package path not found for **%s**:\n- `%s`\nYou may need to force update the package."):format(pkg, path)
)
end
return ret
end
--- Override the default title for notifications.
for _, level in ipairs({ "info", "warn", "error" }) do
M[level] = function(msg, opts)
opts = opts or {}
opts.title = opts.title or "LazyVim"
return LazyUtil[level](msg, opts)
end
end
local cache = {} ---@type table<(fun()), table<string, any>>
---@generic T: fun()
---@param fn T
---@return T
function M.memoize(fn)
return function(...)
local key = vim.inspect({ ... })
cache[fn] = cache[fn] or {}
if cache[fn][key] == nil then
cache[fn][key] = fn(...)
end
return cache[fn][key]
end
end
return M