Skip to content

Commit be9caf5

Browse files
committed
nvim(plugins): init: refactor
1 parent 741e064 commit be9caf5

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

nvim/.config/nvim/lua/plugins/init.lua

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,48 @@ local function ensure_lazy_nvim_installed()
1212
end
1313

1414

15-
-- Initialize plugin_names with loaded plugins.
16-
-- Set each plugin's lazy.nvim config function to import plugins.<plugin_name>.
17-
local function build_plugin_specs()
18-
local plugins = {}
19-
local plugin_specs = {}
20-
21-
local function build_plugin_spec(module_spec, plugin_name)
22-
local plugin_spec = {}
23-
for k, v in pairs(module_spec) do
24-
if k ~= "_" then
25-
plugin_spec[k] = v
26-
end
27-
end
28-
table.insert(plugin_specs, plugin_spec)
29-
plugins[plugin_name] = plugin_spec
30-
end
31-
32-
-- Load all plugins in plugins/ directory automatically.
15+
local function load_plugin_specs()
16+
local specs = {}
17+
local loaded = {}
3318
local curr_script_dir = vim.fs.dirname(debug.getinfo(2, "S").source:sub(2))
3419
local files = vim.split(vim.fn.glob(curr_script_dir .. "/*", true), "\n")
20+
21+
-- Load all plugins in plugins/ directory automatically.
3522
for _, file in ipairs(files) do
36-
local basename = string.match(vim.fs.basename(file), "(_.*).lua$")
37-
if basename ~= nil and basename ~= "init" then
38-
local config_path = "plugins." .. basename
39-
local ok, module_spec = pcall(require, config_path)
40-
local short_url = module_spec[1]
41-
local plugin_name = string.match(short_url, "^[^/]*/([^/]*)$")
42-
if ok then
43-
build_plugin_spec(module_spec, plugin_name)
23+
local basename = vim.fs.basename(file):match("(_.*).lua$")
24+
25+
if basename and basename ~= "init" then
26+
---@type LazyPluginSpec
27+
local spec = {}
28+
for k, v in pairs(require("plugins." .. basename)) do
29+
if k ~= "_" then -- Skip private fields.
30+
spec[k] = v
31+
end
4432
end
33+
34+
table.insert(specs, spec)
35+
local short_url = spec[1]
36+
local name = short_url and short_url:match("^[^/]*/([^/]*)$")
37+
assert(name, "Could not determine name for " .. file)
38+
loaded[name] = spec.enabled ~= false
4539
end
4640
end
4741

48-
return plugins, plugin_specs
42+
function _G.plugin_loaded(name)
43+
return loaded[name] or false
44+
end
45+
46+
return specs
4947
end
5048

5149

5250
ensure_lazy_nvim_installed()
5351

54-
local plugins, plugin_specs = build_plugin_specs()
55-
56-
function _G.plugin_loaded(plugin_name)
57-
return plugins[plugin_name] ~= nil and plugins[plugin_name].enabled ~= false
58-
end
59-
60-
local opts = {
61-
defaults = {
62-
-- version = "*",
52+
require("lazy").setup {
53+
spec = load_plugin_specs(),
54+
opts = {
55+
defaults = {
56+
-- version = "*",
57+
},
6358
},
6459
}
65-
66-
require("lazy").setup(plugin_specs, opts)

0 commit comments

Comments
 (0)