perf(util): split lazyvim.util in smaller separate modules
This commit is contained in:
File diff suppressed because it is too large
Load Diff
112
lua/lazyvim/util/lsp.lua
Normal file
112
lua/lazyvim/util/lsp.lua
Normal file
@ -0,0 +1,112 @@
|
||||
---@class lazyvim.util.lsp
|
||||
local M = {}
|
||||
|
||||
function M.get_clients(...)
|
||||
---@diagnostic disable-next-line: deprecated
|
||||
local fn = vim.lsp.get_clients or vim.lsp.get_active_clients
|
||||
return fn(...)
|
||||
end
|
||||
|
||||
---@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
|
||||
|
||||
---@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
|
||||
|
||||
---@return _.lspconfig.options
|
||||
function M.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.disable(server, cond)
|
||||
local util = require("lspconfig.util")
|
||||
local def = M.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
|
||||
|
||||
---@alias lsp.Client.filter fun(client: lsp.Client): boolean
|
||||
|
||||
---@param name string
|
||||
---@return lsp.Client.filter
|
||||
function M.filter(name)
|
||||
return function(client)
|
||||
return client.name == name
|
||||
end
|
||||
end
|
||||
|
||||
---@param opts? LazyFormatter| {filter?: (string|lsp.Client.filter), bufnr?: number}
|
||||
function M.formatter(opts)
|
||||
opts = opts or {}
|
||||
local filter = opts.filter
|
||||
filter = type(filter) == "string" and M.filter(filter) or filter
|
||||
---@cast filter lsp.Client.filter?
|
||||
---@type LazyFormatter
|
||||
local ret = {
|
||||
name = "LSP",
|
||||
primary = true,
|
||||
priority = 1,
|
||||
format = function(buf)
|
||||
M.format({ bufnr = buf, filter = filter })
|
||||
end,
|
||||
sources = function(buf)
|
||||
local clients = M.get_clients({ bufnr = buf })
|
||||
---@param client lsp.Client
|
||||
local ret = vim.tbl_filter(function(client)
|
||||
return (not filter or filter(client))
|
||||
and (
|
||||
client.supports_method("textDocument/formatting")
|
||||
or client.supports_method("textDocument/rangeFormatting")
|
||||
)
|
||||
end, clients)
|
||||
---@param client lsp.Client
|
||||
return vim.tbl_map(function(client)
|
||||
return client.name
|
||||
end, ret)
|
||||
end,
|
||||
}
|
||||
return vim.tbl_deep_extend("force", ret, opts) --[[@as LazyFormatter]]
|
||||
end
|
||||
|
||||
---@param opts? {filter?: lsp.Client.filter, bufnr?: number}
|
||||
function M.format(opts)
|
||||
vim.lsp.buf.format(
|
||||
vim.tbl_deep_extend("force", opts or {}, require("lazyvim.util").opts("nvim-lspconfig").format or {})
|
||||
)
|
||||
end
|
||||
|
||||
return M
|
49
lua/lazyvim/util/root.lua
Normal file
49
lua/lazyvim/util/root.lua
Normal file
@ -0,0 +1,49 @@
|
||||
local Util = require("lazyvim.util")
|
||||
|
||||
---@class lazyvim.util.root
|
||||
local M = {}
|
||||
|
||||
M.patterns = { ".git", "lua" }
|
||||
|
||||
-- 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()
|
||||
---@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(Util.lsp.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.patterns, { path = path, upward = true })[1]
|
||||
root = root and vim.fs.dirname(root) or vim.loop.cwd()
|
||||
end
|
||||
---@cast root string
|
||||
return root
|
||||
end
|
||||
|
||||
return M
|
53
lua/lazyvim/util/telescope.lua
Normal file
53
lua/lazyvim/util/telescope.lua
Normal file
@ -0,0 +1,53 @@
|
||||
local Util = require("lazyvim.util")
|
||||
|
||||
---@class lazyvim.util.telescope.opts
|
||||
---@field cwd? string|boolean
|
||||
---@field show_untracked? boolean
|
||||
|
||||
---@class lazyvim.util.telescope
|
||||
---@overload fun(builtin:string, opts?:lazyvim.util.telescope.opts)
|
||||
local M = setmetatable({}, {
|
||||
__call = function(m, ...)
|
||||
return m.telescope(...)
|
||||
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
|
||||
---@param builtin string
|
||||
---@param opts? lazyvim.util.telescope.opts
|
||||
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 = Util.root.get() }, opts or {}) --[[@as lazyvim.util.telescope.opts]]
|
||||
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
|
||||
---@diagnostic disable-next-line: inject-field
|
||||
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
|
||||
|
||||
return M
|
55
lua/lazyvim/util/terminal.lua
Normal file
55
lua/lazyvim/util/terminal.lua
Normal file
@ -0,0 +1,55 @@
|
||||
---@class lazyvim.util.terminal
|
||||
---@overload fun(cmd: string|string[], opts: LazyTermOpts): LazyFloat
|
||||
local M = setmetatable({}, {
|
||||
__call = function(m, ...)
|
||||
return m.open(...)
|
||||
end,
|
||||
})
|
||||
|
||||
---@type table<string,LazyFloat>
|
||||
local terminals = {}
|
||||
|
||||
---@class LazyTermOpts: LazyCmdOptions
|
||||
---@field interactive? boolean
|
||||
---@field esc_esc? boolean
|
||||
---@field ctrl_hjkl? boolean
|
||||
|
||||
-- Opens a floating terminal (interactive by default)
|
||||
---@param cmd? string[]|string
|
||||
---@param opts? LazyTermOpts
|
||||
function M.open(cmd, opts)
|
||||
opts = vim.tbl_deep_extend("force", {
|
||||
ft = "lazyterm",
|
||||
size = { width = 0.9, height = 0.9 },
|
||||
}, opts or {}, { persistent = true }) --[[@as LazyTermOpts]]
|
||||
|
||||
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
|
||||
|
||||
return M
|
62
lua/lazyvim/util/toggle.lua
Normal file
62
lua/lazyvim/util/toggle.lua
Normal file
@ -0,0 +1,62 @@
|
||||
local Util = require("lazyvim.util")
|
||||
|
||||
---@class lazyvim.util.toggle
|
||||
local M = {}
|
||||
|
||||
---@param silent boolean?
|
||||
---@param values? {[1]:any, [2]:any}
|
||||
function M.option(option, silent, values)
|
||||
if values then
|
||||
if vim.opt_local[option]:get() == values[1] then
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
vim.opt_local[option] = values[2]
|
||||
else
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
vim.opt_local[option] = values[1]
|
||||
end
|
||||
return Util.info("Set " .. option .. " to " .. vim.opt_local[option]:get(), { title = "Option" })
|
||||
end
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
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.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.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
|
||||
|
||||
setmetatable(M, {
|
||||
__call = function(m, ...)
|
||||
return m.option(...)
|
||||
end,
|
||||
})
|
||||
|
||||
return M
|
@ -1,3 +1,4 @@
|
||||
---@class lazyvim.util.ui
|
||||
local M = {}
|
||||
|
||||
---@alias Sign {name:string, text:string, texthl:string, priority:number}
|
||||
@ -119,4 +120,13 @@ function M.statuscolumn()
|
||||
}, "")
|
||||
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
|
||||
|
||||
return M
|
||||
|
Reference in New Issue
Block a user