-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathinit.lua
137 lines (109 loc) · 3.73 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
local template = require("leetcode.config.template")
local P = require("plenary.path")
_Lc_state = {
menu = nil, ---@type lc.ui.Menu
questions = {}, ---@type lc.ui.Question[]
}
local lazy_plugs = {}
---@class lc.Config
local config = {
default = template,
user = template,
name = "leetcode.nvim",
domain = "com",
is_cn = false,
debug = false,
lang = "cpp",
version = "1.0.1",
storage = {}, ---@type lc.storage
theme = {}, ---@type lc.highlights
plugins = {},
translator = false,
langs = require("leetcode.config.langs"),
icons = require("leetcode.config.icons"),
sessions = require("leetcode.config.sessions"),
stats = require("leetcode.config.stats"),
imports = require("leetcode.config.imports"),
hooks = require("leetcode.config.hooks"),
---@type lc.UserStatus
auth = {}, ---@diagnostic disable-line
}
---Merge configurations into default configurations and set it as user configurations.
---
---@param cfg lc.UserConfig Configurations to be merged.
function config.apply(cfg)
config.user = vim.tbl_deep_extend("force", config.default, cfg or {})
config.load_plugins()
end
function config.setup()
config.validate()
-- deprecate `directory` config
if config.user.directory then
local log = require("leetcode.logger")
log.warn("leetcode.nvim config: `directory` is deprecated. Use `storage.home` instead.")
config.user.storage.home = config.user.directory
end
config.user.storage.home = vim.fn.expand(config.user.storage.home)
config.user.storage.cache = vim.fn.expand(config.user.storage.cache)
config.debug = config.user.debug or false ---@diagnostic disable-line
config.lang = config.user.lang
config.storage.home = P:new(config.user.storage.home) ---@diagnostic disable-line
config.storage.home:mkdir()
config.storage.cache = P:new(config.user.storage.cache) ---@diagnostic disable-line
config.storage.cache:mkdir()
config.storage.format = config.user.storage.format
for _, plug_load_fn in ipairs(lazy_plugs) do
plug_load_fn()
end
end
function config.validate()
local utils = require("leetcode.utils")
assert(vim.fn.has("nvim-0.9.0") == 1, "Neovim >= 0.9.0 required")
if not utils.get_lang(config.lang) then
---@type lc.lang[]
local lang_slugs = vim.tbl_map(function(lang)
return lang.slug
end, config.langs)
local matches = {}
for _, slug in ipairs(lang_slugs) do
local percent = slug:match(config.lang) or config.lang:match(slug)
if percent then
table.insert(matches, slug)
end
end
if not vim.tbl_isempty(matches) then
local log = require("leetcode.logger")
log.warn("Did you mean: { " .. table.concat(matches, ", ") .. " }?")
end
error("Unsupported Language: " .. config.lang)
end
end
function config.load_plugins()
config.plugins = {}
local plugins = {}
if config.user.cn.enabled then
table.insert(plugins, "cn")
end
for plugin, enabled in pairs(config.user.plugins) do
if enabled then
table.insert(plugins, plugin)
end
end
for _, plugin in ipairs(plugins) do
local ok, plug = pcall(require, "leetcode-plugins." .. plugin)
if ok then
if not (plug.opts or {}).lazy then
plug.load()
else
table.insert(lazy_plugs, plug.load)
end
config.plugins[plugin] = true
else
table.insert(lazy_plugs, function()
local log = require("leetcode.logger")
log.error(plug)
end)
end
end
end
return config