feat: more lazy loading for faster startup

This commit is contained in:
Steven Arcangeli 2022-08-28 10:48:05 -07:00
parent 232b6b3021
commit f38eb33572
2 changed files with 25 additions and 18 deletions

View File

@ -1,17 +1,19 @@
local config = require("dressing.config")
local patch = require("dressing.patch")
local M = {}
M.setup = function(opts)
config.update(opts)
require("dressing.config").update(opts)
patch.all()
end
---Patch all the vim.ui methods
M.patch = function()
patch.all()
end
---Unpatch all the vim.ui methods
---@param names? string[] Names of vim.ui modules to unpatch
M.unpatch = function(names)
if not names then
return patch.all(false)

View File

@ -1,5 +1,3 @@
local config = require("dressing.config")
local all_modules = { "input", "select" }
local M = {}
@ -9,29 +7,36 @@ if not vim.ui then
vim.ui = {}
end
local enabled_mods = {}
M.original_mods = {}
for _, key in ipairs(all_modules) do
M.original_mods[key] = vim.ui[key]
vim.ui[key] = function(...)
local enabled = enabled_mods[key]
if enabled == nil then
enabled = require("dressing.config")[key].enabled
end
if enabled then
require(string.format("dressing.%s", key))(...)
else
return M.original_mods[key](...)
end
end
end
---Patch or unpatch all vim.ui methods
---@param enabled? boolean When nil, use the default from config
M.all = function(enabled)
for _, name in ipairs(all_modules) do
M.mod(name, enabled)
end
end
---@param name string
---@param enabled? boolean When nil, use the default from config
M.mod = function(name, enabled)
if enabled == nil then
enabled = config[name].enabled
end
if enabled then
if M.original_mods[name] == nil then
M.original_mods[name] = vim.ui[name]
end
vim.ui[name] = require(string.format("dressing.%s", name))
else
local mod = require(string.format("dressing.%s", name))
if vim.ui[name] == mod then
vim.ui[name] = M.original_mods[name]
end
end
enabled_mods[name] = enabled
end
return M