Skip to content

Commit

Permalink
perf(util): split lazyvim.util in smaller separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 12, 2023
1 parent 5f5acb5 commit c8c929c
Show file tree
Hide file tree
Showing 20 changed files with 444 additions and 285 deletions.
27 changes: 13 additions & 14 deletions lua/lazyvim/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local Util = require("lazy.core.util")
local Util = require("lazyvim.util")

---@class LazyVimConfig: LazyVimOptions
local M = {}
Expand Down Expand Up @@ -155,6 +155,7 @@ function M.setup(opts)
M.load("autocmds")
end
M.load("keymaps")
Util.format.setup()
end,
})

Expand Down Expand Up @@ -240,18 +241,11 @@ end
---@param name "autocmds" | "options" | "keymaps"
function M.load(name)
local function _load(mod)
Util.try(function()
require(mod)
end, {
msg = "Failed loading " .. mod,
on_error = function(msg)
local info = require("lazy.core.cache").find(mod)
if info == nil or (type(info) == "table" and #info == 0) then
return
end
Util.error(msg)
end,
})
if require("lazy.core.cache").find(mod)[1] then
Util.try(function()
require(mod)
end, { msg = "Failed loading " .. mod })
end
end
-- always load lazyvim, then user file
if M.defaults[name] or name == "options" then
Expand All @@ -275,6 +269,11 @@ function M.init()
vim.opt.rtp:append(plugin.dir)
end

package.preload["lazyvim.plugins.lsp.format"] = function()
Util.deprecate([[require("lazyvim.plugins.lsp.format")]], [[require("lazyvim.util").format]])
return Util.format
end

M.use_lazy_file = M.use_lazy_file and vim.fn.argc(-1) > 0
---@diagnostic disable-next-line: undefined-field
M.use_lazy_file = M.use_lazy_file and require("lazy.core.handler.event").trigger_events == nil
Expand All @@ -285,7 +284,7 @@ function M.init()
-- load options here, before lazy init while sourcing plugin modules
-- this is needed to make sure options will be correctly applied
-- after installing missing plugins
require("lazyvim.config").load("options")
M.load("options")
local Plugin = require("lazy.core.plugin")
local add = Plugin.Spec.add
---@diagnostic disable-next-line: duplicate-set-field
Expand Down
12 changes: 6 additions & 6 deletions lua/lazyvim/config/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ map("n", "<leader>uf", require("lazyvim.plugins.lsp.format").toggle, { desc = "T
map("n", "<leader>us", function() Util.toggle("spell") end, { desc = "Toggle Spelling" })
map("n", "<leader>uw", function() Util.toggle("wrap") end, { desc = "Toggle Word Wrap" })
map("n", "<leader>uL", function() Util.toggle("relativenumber") end, { desc = "Toggle Relative Line Numbers" })
map("n", "<leader>ul", function() Util.toggle_number() end, { desc = "Toggle Line Numbers" })
map("n", "<leader>ud", Util.toggle_diagnostics, { desc = "Toggle Diagnostics" })
map("n", "<leader>ul", function() Util.toggle.number() end, { desc = "Toggle Line Numbers" })
map("n", "<leader>ud", function() Util.toggle.diagnostics() end, { desc = "Toggle Diagnostics" })
local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3
map("n", "<leader>uc", function() Util.toggle("conceallevel", false, {0, conceallevel}) end, { desc = "Toggle Conceal" })
if vim.lsp.inlay_hint then
map("n", "<leader>uh", function() vim.lsp.inlay_hint(0, nil) end, { desc = "Toggle Inlay Hints" })
end

-- lazygit
map("n", "<leader>gg", function() Util.float_term({ "lazygit" }, { cwd = Util.get_root(), esc_esc = false, ctrl_hjkl = false }) end, { desc = "Lazygit (root dir)" })
map("n", "<leader>gG", function() Util.float_term({ "lazygit" }, {esc_esc = false, ctrl_hjkl = false}) end, { desc = "Lazygit (cwd)" })
map("n", "<leader>gg", function() Util.terminal({ "lazygit" }, { cwd = Util.root.get(), esc_esc = false, ctrl_hjkl = false }) end, { desc = "Lazygit (root dir)" })
map("n", "<leader>gG", function() Util.terminal({ "lazygit" }, {esc_esc = false, ctrl_hjkl = false}) end, { desc = "Lazygit (cwd)" })

-- quit
map("n", "<leader>qq", "<cmd>qa<cr>", { desc = "Quit all" })
Expand All @@ -127,9 +127,9 @@ map("n", "<leader>ui", vim.show_pos, { desc = "Inspect Pos" })
map("n", "<leader>L", Util.changelog, {desc = "LazyVim Changelog"})

-- floating terminal
local lazyterm = function() Util.float_term(nil, { cwd = Util.get_root() }) end
local lazyterm = function() Util.terminal(nil, { cwd = Util.root.get() }) end
map("n", "<leader>ft", lazyterm, { desc = "Terminal (root dir)" })
map("n", "<leader>fT", function() Util.float_term() end, { desc = "Terminal (cwd)" })
map("n", "<leader>fT", function() Util.terminal() end, { desc = "Terminal (cwd)" })
map("n", "<c-/>", lazyterm, { desc = "Terminal (root dir)" })
map("n", "<c-_>", lazyterm, { desc = "which_key_ignore" })

Expand Down
4 changes: 2 additions & 2 deletions lua/lazyvim/config/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ end

-- Folding
vim.opt.foldlevel = 99
vim.opt.foldtext = "v:lua.require'lazyvim.util.ui'.foldtext()"
vim.opt.foldtext = "v:lua.require'lazyvim.util'.ui.foldtext()"

if vim.fn.has("nvim-0.9.0") == 1 then
vim.opt.statuscolumn = [[%!v:lua.require'lazyvim.util.ui'.statuscolumn()]]
vim.opt.statuscolumn = [[%!v:lua.require'lazyvim.util'.ui.statuscolumn()]]
end

-- HACK: causes freezes on <= 0.9, so only enable on >= 0.10 for now
Expand Down
6 changes: 3 additions & 3 deletions lua/lazyvim/plugins/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ return {
{
"<leader>fe",
function()
require("neo-tree.command").execute({ toggle = true, dir = require("lazyvim.util").get_root() })
require("neo-tree.command").execute({ toggle = true, dir = Util.root.get() })
end,
desc = "Explorer NeoTree (root dir)",
},
Expand Down Expand Up @@ -60,7 +60,7 @@ return {
},
config = function(_, opts)
local function on_move(data)
Util.on_rename(data.source, data.destination)
Util.lsp.on_rename(data.source, data.destination)
end

local events = require("neo-tree.events")
Expand Down Expand Up @@ -270,7 +270,7 @@ return {
"nvim-telescope/telescope.nvim",
optional = true,
opts = function(_, opts)
if not require("lazyvim.util").has("flash.nvim") then
if not Util.has("flash.nvim") then
return
end
local function flash(prompt_bufnr)
Expand Down
6 changes: 3 additions & 3 deletions lua/lazyvim/plugins/extras/coding/codeium.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ return {

local Util = require("lazyvim.util")
local colors = {
ok = Util.fg("Special"),
error = Util.fg("DiagnosticError"),
pending = Util.fg("DiagnosticWarn"),
ok = Util.ui.fg("Special"),
error = Util.ui.fg("DiagnosticError"),
pending = Util.ui.fg("DiagnosticWarn"),
}
table.insert(opts.sections.lualine_x, 2, {
function()
Expand Down
12 changes: 6 additions & 6 deletions lua/lazyvim/plugins/extras/coding/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ return {
opts = function(_, opts)
local Util = require("lazyvim.util")
local colors = {
[""] = Util.fg("Special"),
["Normal"] = Util.fg("Special"),
["Warning"] = Util.fg("DiagnosticError"),
["InProgress"] = Util.fg("DiagnosticWarn"),
[""] = Util.ui.fg("Special"),
["Normal"] = Util.ui.fg("Special"),
["Warning"] = Util.ui.fg("DiagnosticError"),
["InProgress"] = Util.ui.fg("DiagnosticWarn"),
}
table.insert(opts.sections.lualine_x, 2, {
function()
Expand All @@ -36,7 +36,7 @@ return {
if not package.loaded["copilot"] then
return
end
local ok, clients = pcall(require("lazyvim.util").get_clients, { name = "copilot", bufnr = 0 })
local ok, clients = pcall(require("lazyvim.util").lsp.get_clients, { name = "copilot", bufnr = 0 })
if not ok then
return false
end
Expand Down Expand Up @@ -66,7 +66,7 @@ return {
copilot_cmp.setup(opts)
-- attach cmp source whenever copilot attaches
-- fixes lazy-loading issues with the copilot cmp source
require("lazyvim.util").on_attach(function(client)
require("lazyvim.util").lsp.on_attach(function(client)
if client.name == "copilot" then
copilot_cmp._on_insert_enter({})
end
Expand Down
2 changes: 1 addition & 1 deletion lua/lazyvim/plugins/extras/editor/mini-files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ return {
vim.api.nvim_create_autocmd("User", {
pattern = "MiniFilesActionRename",
callback = function(event)
require("lazyvim.util").on_rename(event.data.from, event.data.to)
require("lazyvim.util").lsp.on_rename(event.data.from, event.data.to)
end,
})
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/lazyvim/plugins/extras/lang/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ return {
gopls = function(_, opts)
-- workaround for gopls not supporting semanticTokensProvider
-- https://github.com/golang/go/issues/54531#issuecomment-1464982242
require("lazyvim.util").on_attach(function(client, _)
require("lazyvim.util").lsp.on_attach(function(client, _)
if client.name == "gopls" then
if not client.server_capabilities.semanticTokensProvider then
local semantic = client.config.capabilities.textDocument.semanticTokens
Expand Down
2 changes: 1 addition & 1 deletion lua/lazyvim/plugins/extras/lang/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ return {
},
setup = {
ruff_lsp = function()
require("lazyvim.util").on_attach(function(client, _)
require("lazyvim.util").lsp.on_attach(function(client, _)
if client.name == "ruff_lsp" then
-- Disable hover in favor of Pyright
client.server_capabilities.hoverProvider = false
Expand Down
2 changes: 1 addition & 1 deletion lua/lazyvim/plugins/extras/lang/yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ return {
yamlls = function()
-- Neovim < 0.10 does not have dynamic registration for formatting
if vim.fn.has("nvim-0.10") == 0 then
require("lazyvim.util").on_attach(function(client, _)
require("lazyvim.util").lsp.on_attach(function(client, _)
if client.name == "yamlls" then
client.server_capabilities.documentFormattingProvider = true
end
Expand Down
13 changes: 8 additions & 5 deletions lua/lazyvim/plugins/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Util = require("lazyvim.util")

return {
-- lspconfig
{
Expand Down Expand Up @@ -88,6 +90,8 @@ return {
require("lazyvim.plugins.lsp.format").setup(opts)
-- setup formatting and keymaps
Util.on_attach(function(client, buffer)
-- setup keymaps
Util.lsp.on_attach(function(client, buffer)
require("lazyvim.plugins.lsp.keymaps").on_attach(client, buffer)
end)

Expand All @@ -112,7 +116,7 @@ return {
local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint

if opts.inlay_hints.enabled and inlay_hint then
Util.on_attach(function(client, buffer)
Util.lsp.on_attach(function(client, buffer)
if client.supports_method("textDocument/inlayHint") then
inlay_hint(buffer, true)
end
Expand Down Expand Up @@ -184,10 +188,10 @@ return {
mlsp.setup({ ensure_installed = ensure_installed, handlers = { setup } })
end

if Util.lsp_get_config("denols") and Util.lsp_get_config("tsserver") then
if Util.lsp.get_config("denols") and Util.lsp.get_config("tsserver") then
local is_deno = require("lspconfig.util").root_pattern("deno.json", "deno.jsonc")
Util.lsp_disable("tsserver", is_deno)
Util.lsp_disable("denols", function(root_dir)
Util.lsp.disable("tsserver", is_deno)
Util.lsp.disable("denols", function(root_dir)
return not is_deno(root_dir)
end)
end
Expand All @@ -208,7 +212,6 @@ return {
nls.builtins.diagnostics.fish,
nls.builtins.formatting.stylua,
nls.builtins.formatting.shfmt,
-- nls.builtins.diagnostics.flake8,
},
}
end,
Expand Down
4 changes: 2 additions & 2 deletions lua/lazyvim/plugins/lsp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ end
---@param method string
function M.has(buffer, method)
method = method:find("/") and method or "textDocument/" .. method
local clients = require("lazyvim.util").get_clients({ bufnr = buffer })
local clients = require("lazyvim.util").lsp.get_clients({ bufnr = buffer })
for _, client in ipairs(clients) do
if client.supports_method(method) then
return true
Expand All @@ -83,7 +83,7 @@ function M.resolve(buffer)
end
local spec = M.get()
local opts = require("lazyvim.util").opts("nvim-lspconfig")
local clients = require("lazyvim.util").get_clients({ bufnr = buffer })
local clients = require("lazyvim.util").lsp.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)
Expand Down
12 changes: 6 additions & 6 deletions lua/lazyvim/plugins/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@ return {
{
function() return require("noice").api.status.command.get() end,
cond = function() return package.loaded["noice"] and require("noice").api.status.command.has() end,
color = Util.fg("Statement"),
color = Util.ui.fg("Statement"),
},
-- stylua: ignore
{
function() return require("noice").api.status.mode.get() end,
cond = function() return package.loaded["noice"] and require("noice").api.status.mode.has() end,
color = Util.fg("Constant"),
color = Util.ui.fg("Constant"),
},
-- stylua: ignore
{
function() return "" .. require("dap").status() end,
cond = function () return package.loaded["dap"] and require("dap").status() ~= "" end,
color = Util.fg("Debug"),
color = Util.ui.fg("Debug"),
},
{ require("lazy.status").updates, cond = require("lazy.status").has_updates, color = Util.fg("Special") },
{ require("lazy.status").updates, cond = require("lazy.status").has_updates, color = Util.ui.fg("Special") },
{
"diff",
symbols = {
Expand Down Expand Up @@ -374,8 +374,8 @@ return {
lazy = true,
init = function()
vim.g.navic_silence = true
require("lazyvim.util").on_attach(function(client, buffer)
if client.server_capabilities.documentSymbolProvider then
require("lazyvim.util").lsp.on_attach(function(client, buffer)
if client.supports_method("textDocument/documentSymbol") then
require("nvim-navic").attach(client, buffer)
end
end)
Expand Down

0 comments on commit c8c929c

Please sign in to comment.