Skip to content

Commit 461fab3

Browse files
committed
feat: lazy plugs
1 parent e1ab306 commit 461fab3

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

lua/leetcode-plugins/cn/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
---@class lc.plugins.cn
22
local cn = {}
33

4+
cn.opts = {
5+
lazy = true,
6+
}
7+
48
function cn.load()
59
local config = require("leetcode.config")
10+
11+
config.translator = config.user.cn.translator
612
config.domain = "cn"
713
config.is_cn = true
814

lua/leetcode-ui/renderer/menu.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local config = require("leetcode.config")
44
local utils = require("leetcode-ui.utils")
55
local Renderer = require("leetcode-ui.renderer")
66

7-
---@class lc.ui.menu : lc.ui.Renderer @field tabpage integer
7+
---@class lc.ui.menu : lc.ui.Renderer
88
---@field cursor lc-menu.cursor
99
---@field maps table
1010
local Menu = Renderer:extend("LeetMenu")

lua/leetcode.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ function leetcode.start(on_vimenter)
3737
return false
3838
end
3939

40+
config.setup()
41+
4042
vim.api.nvim_set_current_dir(config.storage.home:absolute())
4143

4244
leetcode.setup_cmds()
43-
-- config.load_plugins()
4445

4546
local utils = require("leetcode.utils")
4647
utils.exec_hooks("LeetEnter")

lua/leetcode/config/init.lua

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ _Lc_questions = {}
77
---@type lc.ui.menu
88
_Lc_Menu = {} ---@diagnostic disable-line
99

10-
---@class lc.Settings
10+
local lazy_plugs = {}
11+
12+
---@class lc.Config
1113
local config = {
1214
default = template,
1315
user = template,
@@ -33,31 +35,34 @@ local config = {
3335
---
3436
---@param cfg lc.UserConfig Configurations to be merged.
3537
function config.apply(cfg)
36-
cfg.storage = cfg.storage or {}
38+
config.user = vim.tbl_deep_extend("force", config.default, cfg or {})
39+
config.load_plugins()
40+
end
41+
42+
function config.setup()
43+
config.validate()
3744

3845
-- deprecate `directory` config
39-
if cfg.directory then
46+
if config.user.directory then
4047
local log = require("leetcode.logger")
41-
log.warn("Config: `directory` is deprecated. Use `storage.home` instead.")
42-
cfg.storage.home = cfg.directory
48+
log.warn("leetcode.nvim config: `directory` is deprecated. Use `storage.home` instead.")
49+
config.user.storage.home = config.user.directory
4350
end
4451

45-
cfg.storage = vim.tbl_map(vim.fn.expand, cfg.storage)
46-
47-
config.user = vim.tbl_deep_extend("force", config.default, cfg)
52+
config.user.storage = vim.tbl_map(vim.fn.expand, config.user.storage)
4853

4954
config.debug = config.user.debug or false ---@diagnostic disable-line
5055
config.lang = config.user.lang
5156

52-
config.validate()
53-
5457
config.storage.home = P:new(config.user.storage.home) ---@diagnostic disable-line
5558
config.storage.home:mkdir()
5659

5760
config.storage.cache = P:new(config.user.storage.cache) ---@diagnostic disable-line
5861
config.storage.cache:mkdir()
5962

60-
config.load_plugins()
63+
for _, plug_load_fn in ipairs(lazy_plugs) do
64+
plug_load_fn()
65+
end
6166
end
6267

6368
function config.validate()
@@ -87,6 +92,26 @@ end
8792
function config.load_plugins()
8893
local plugins = {}
8994

95+
if config.user.cn.enabled then table.insert(plugins, "cn") end
96+
97+
for _, plugin in ipairs(plugins) do
98+
local ok, plug = pcall(require, "leetcode-plugins." .. plugin)
99+
if ok then
100+
if not (plug.opts or {}).lazy then
101+
plug.load()
102+
else
103+
table.insert(lazy_plugs, plug.load)
104+
end
105+
else
106+
local log = require("leetcode.logger")
107+
log.error(plug)
108+
end
109+
end
110+
end
111+
112+
function config.load_high_priority_plugins()
113+
local plugins = {}
114+
90115
if config.user.cn.enabled then
91116
config.translator = config.user.cn.translator
92117
table.insert(plugins, "cn")

0 commit comments

Comments
 (0)