mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-22 00:49:03 +02:00
perf(util): split lazyvim.util in smaller separate modules
This commit is contained in:
parent
b304db9236
commit
aa0e4f52e4
20 changed files with 442 additions and 283 deletions
|
@ -1,38 +1,54 @@
|
|||
local Util = require("lazy.core.util")
|
||||
local LazyUtil = require("lazy.core.util")
|
||||
|
||||
---@class lazyvim.util: LazyUtilCore
|
||||
---@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 toggle lazyvim.util.toggle
|
||||
---@field format lazyvim.util.format
|
||||
local M = {}
|
||||
|
||||
M.root_patterns = { ".git", "lua" }
|
||||
function M.get_clients(...)
|
||||
local fn = vim.lsp.get_clients or vim.lsp.get_active_clients
|
||||
return fn(...)
|
||||
end
|
||||
---@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 = { "toggle", "option" },
|
||||
toggle_diagnostics = { "toggle", "diagnostics" },
|
||||
toggle_number = { "toggle", "number" },
|
||||
fg = "ui",
|
||||
}
|
||||
|
||||
---@param on_attach fun(client, buffer)
|
||||
function M.on_attach(on_attach)
|
||||
vim.api.nvim_create_autocmd("LspAttach", {
|
||||
callback = function(args)
|
||||
local buffer = args.buf ---@type number
|
||||
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
||||
on_attach(client, buffer)
|
||||
end,
|
||||
})
|
||||
end
|
||||
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([[require("lazyvim.util").]] .. k, [[require("lazyvim.util").]] .. 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,
|
||||
})
|
||||
|
||||
---@param plugin string
|
||||
function M.has(plugin)
|
||||
return require("lazy.core.config").spec.plugins[plugin] ~= nil
|
||||
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)
|
||||
---@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()
|
||||
function M.on_very_lazy(fn)
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
|
@ -53,173 +69,13 @@ function M.opts(name)
|
|||
return Plugin.values(plugin, "opts", false)
|
||||
end
|
||||
|
||||
-- returns the root directory based on:
|
||||
-- * lsp workspace folders
|
||||
-- * lsp root_dir
|
||||
-- * root pattern of filename of the current buffer
|
||||
-- * root pattern of cwd
|
||||
---@return string
|
||||
function M.get_root()
|
||||
---@type string?
|
||||
local path = vim.api.nvim_buf_get_name(0)
|
||||
path = path ~= "" and vim.loop.fs_realpath(path) or nil
|
||||
---@type string[]
|
||||
local roots = {}
|
||||
if path then
|
||||
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)
|
||||
end, workspace) or client.config.root_dir and { client.config.root_dir } or {}
|
||||
for _, p in ipairs(paths) do
|
||||
local r = vim.loop.fs_realpath(p)
|
||||
if path:find(r, 1, true) then
|
||||
roots[#roots + 1] = r
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
table.sort(roots, function(a, b)
|
||||
return #a > #b
|
||||
end)
|
||||
---@type string?
|
||||
local root = roots[1]
|
||||
if not root then
|
||||
path = path and vim.fs.dirname(path) or vim.loop.cwd()
|
||||
---@type string?
|
||||
root = vim.fs.find(M.root_patterns, { path = path, upward = true })[1]
|
||||
root = root and vim.fs.dirname(root) or vim.loop.cwd()
|
||||
end
|
||||
---@cast root string
|
||||
return root
|
||||
end
|
||||
|
||||
-- this will return a function that calls telescope.
|
||||
-- cwd will default to lazyvim.util.get_root
|
||||
-- for `files`, git_files or find_files will be chosen depending on .git
|
||||
function M.telescope(builtin, opts)
|
||||
local params = { builtin = builtin, opts = opts }
|
||||
return function()
|
||||
builtin = params.builtin
|
||||
opts = params.opts
|
||||
opts = vim.tbl_deep_extend("force", { cwd = M.get_root() }, opts or {})
|
||||
if builtin == "files" then
|
||||
if vim.loop.fs_stat((opts.cwd or vim.loop.cwd()) .. "/.git") then
|
||||
opts.show_untracked = true
|
||||
builtin = "git_files"
|
||||
else
|
||||
builtin = "find_files"
|
||||
end
|
||||
end
|
||||
if opts.cwd and opts.cwd ~= vim.loop.cwd() then
|
||||
opts.attach_mappings = function(_, map)
|
||||
map("i", "<a-c>", function()
|
||||
local action_state = require("telescope.actions.state")
|
||||
local line = action_state.get_current_line()
|
||||
M.telescope(
|
||||
params.builtin,
|
||||
vim.tbl_deep_extend("force", {}, params.opts or {}, { cwd = false, default_text = line })
|
||||
)()
|
||||
end)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
require("telescope.builtin")[builtin](opts)
|
||||
end
|
||||
end
|
||||
|
||||
---@type table<string,LazyFloat>
|
||||
local terminals = {}
|
||||
|
||||
-- Opens a floating terminal (interactive by default)
|
||||
---@param cmd? string[]|string
|
||||
---@param opts? LazyCmdOptions|{interactive?:boolean, esc_esc?:false, ctrl_hjkl?:false}
|
||||
function M.float_term(cmd, opts)
|
||||
opts = vim.tbl_deep_extend("force", {
|
||||
ft = "lazyterm",
|
||||
size = { width = 0.9, height = 0.9 },
|
||||
}, opts or {}, { persistent = true })
|
||||
---@cast opts LazyCmdOptions|{interactive?:boolean, esc_esc?:false}
|
||||
|
||||
local termkey = vim.inspect({ cmd = cmd or "shell", cwd = opts.cwd, env = opts.env, count = vim.v.count1 })
|
||||
|
||||
if terminals[termkey] and terminals[termkey]:buf_valid() then
|
||||
terminals[termkey]:toggle()
|
||||
else
|
||||
terminals[termkey] = require("lazy.util").float_term(cmd, opts)
|
||||
local buf = terminals[termkey].buf
|
||||
vim.b[buf].lazyterm_cmd = cmd
|
||||
if opts.esc_esc == false then
|
||||
vim.keymap.set("t", "<esc>", "<esc>", { buffer = buf, nowait = true })
|
||||
end
|
||||
if opts.ctrl_hjkl == false then
|
||||
vim.keymap.set("t", "<c-h>", "<c-h>", { buffer = buf, nowait = true })
|
||||
vim.keymap.set("t", "<c-j>", "<c-j>", { buffer = buf, nowait = true })
|
||||
vim.keymap.set("t", "<c-k>", "<c-k>", { buffer = buf, nowait = true })
|
||||
vim.keymap.set("t", "<c-l>", "<c-l>", { buffer = buf, nowait = true })
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd("BufEnter", {
|
||||
buffer = buf,
|
||||
callback = function()
|
||||
vim.cmd.startinsert()
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
return terminals[termkey]
|
||||
end
|
||||
|
||||
---@param silent boolean?
|
||||
---@param values? {[1]:any, [2]:any}
|
||||
function M.toggle(option, silent, values)
|
||||
if values then
|
||||
if vim.opt_local[option]:get() == values[1] then
|
||||
vim.opt_local[option] = values[2]
|
||||
else
|
||||
vim.opt_local[option] = values[1]
|
||||
end
|
||||
return Util.info("Set " .. option .. " to " .. vim.opt_local[option]:get(), { title = "Option" })
|
||||
end
|
||||
vim.opt_local[option] = not vim.opt_local[option]:get()
|
||||
if not silent then
|
||||
if vim.opt_local[option]:get() then
|
||||
Util.info("Enabled " .. option, { title = "Option" })
|
||||
else
|
||||
Util.warn("Disabled " .. option, { title = "Option" })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local nu = { number = true, relativenumber = true }
|
||||
function M.toggle_number()
|
||||
if vim.opt_local.number:get() or vim.opt_local.relativenumber:get() then
|
||||
nu = { number = vim.opt_local.number:get(), relativenumber = vim.opt_local.relativenumber:get() }
|
||||
vim.opt_local.number = false
|
||||
vim.opt_local.relativenumber = false
|
||||
Util.warn("Disabled line numbers", { title = "Option" })
|
||||
else
|
||||
vim.opt_local.number = nu.number
|
||||
vim.opt_local.relativenumber = nu.relativenumber
|
||||
Util.info("Enabled line numbers", { title = "Option" })
|
||||
end
|
||||
end
|
||||
|
||||
local enabled = true
|
||||
function M.toggle_diagnostics()
|
||||
enabled = not enabled
|
||||
if enabled then
|
||||
vim.diagnostic.enable()
|
||||
Util.info("Enabled diagnostics", { title = "Diagnostics" })
|
||||
else
|
||||
vim.diagnostic.disable()
|
||||
Util.warn("Disabled diagnostics", { title = "Diagnostics" })
|
||||
end
|
||||
end
|
||||
|
||||
function M.deprecate(old, new)
|
||||
Util.warn(("`%s` is deprecated. Please use `%s` instead"):format(old, new), { title = "LazyVim" })
|
||||
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
|
||||
|
@ -259,25 +115,6 @@ 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)
|
||||
end
|
||||
|
||||
---@param server string
|
||||
---@param cond fun( root_dir, config): boolean
|
||||
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
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
---@param fn fun(name:string)
|
||||
function M.on_load(name, fn)
|
||||
|
@ -305,28 +142,6 @@ function M.changelog()
|
|||
vim.diagnostic.disable(float.buf)
|
||||
end
|
||||
|
||||
---@param from string
|
||||
---@param to string
|
||||
function M.on_rename(from, to)
|
||||
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 = {
|
||||
{
|
||||
oldUri = vim.uri_from_fname(from),
|
||||
newUri = vim.uri_from_fname(to),
|
||||
},
|
||||
},
|
||||
}, 1000, 0)
|
||||
if resp and resp.result ~= nil then
|
||||
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
|
||||
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.
|
||||
|
@ -345,6 +160,7 @@ function M.safe_keymap_set(mode, lhs, rhs, opts)
|
|||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue