Skip to content

Commit

Permalink
feat: more lazy loading for faster startup
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Aug 28, 2022
1 parent 232b6b3 commit f38eb33
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lua/dressing/init.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
37 changes: 21 additions & 16 deletions lua/dressing/patch.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local config = require("dressing.config")

local all_modules = { "input", "select" }

local M = {}
Expand All @@ -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

0 comments on commit f38eb33

Please sign in to comment.