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 patch = require("dressing.patch")
local M = {} local M = {}
M.setup = function(opts) M.setup = function(opts)
config.update(opts) require("dressing.config").update(opts)
patch.all() patch.all()
end end
---Patch all the vim.ui methods
M.patch = function() M.patch = function()
patch.all() patch.all()
end end
---Unpatch all the vim.ui methods
---@param names? string[] Names of vim.ui modules to unpatch
M.unpatch = function(names) M.unpatch = function(names)
if not names then if not names then
return patch.all(false) return patch.all(false)

View File

@ -1,5 +1,3 @@
local config = require("dressing.config")
local all_modules = { "input", "select" } local all_modules = { "input", "select" }
local M = {} local M = {}
@ -9,29 +7,36 @@ if not vim.ui then
vim.ui = {} vim.ui = {}
end end
local enabled_mods = {}
M.original_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) M.all = function(enabled)
for _, name in ipairs(all_modules) do for _, name in ipairs(all_modules) do
M.mod(name, enabled) M.mod(name, enabled)
end end
end end
---@param name string
---@param enabled? boolean When nil, use the default from config
M.mod = function(name, enabled) M.mod = function(name, enabled)
if enabled == nil then enabled_mods[name] = enabled
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
end end
return M return M