-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
149 lines (135 loc) · 4.83 KB
/
Copy pathinit.lua
File metadata and controls
149 lines (135 loc) · 4.83 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
--- Color and Theme
vim.opt.termguicolors = true
vim.cmd.colorscheme("catppuccin")
--- Global options
vim.g.netrw_banner = 0
vim.g.mapleader = " " -- space for leader
vim.g.maplocalleader = " " -- space for localleader
-- Disable unused language providers (avoids checkhealth noise + startup cost)
vim.g.loaded_python3_provider = 0
vim.g.loaded_perl_provider = 0
vim.g.loaded_ruby_provider = 0
vim.g.loaded_node_provider = 0
--- Load
require("autocmds")
require("keymaps")
require("options")
require("utils.keymapdoc").setup()
--- Plugins
--- Each file in the plugins/ dir returns either a single spec table, or a
--- list of spec tables (for plugins that must be installed/configured
--- together, e.g. mason + mason-lspconfig).
---
--- return {
--- src = "https://github.com/owner/repo", -- required
--- version = "v1.2.3" or "main", -- optional (tag/branch/commit)
--- config = function() ... end, -- optional, runs after install/load
--- priority = 100, -- optional, higher runs first (default 0)
--- enabled = false, -- optional, default true; when false
--- -- the plugin is not loaded/configured
--- -- and is uninstalled if present
--- }
---
--- Configs run sorted by `priority` (desc), then file name (asc) for
--- deterministic ordering. Use `priority` for load-order dependencies.
local plugins_dir = vim.fn.stdpath("config") .. "/plugins"
---@type table Table holding compiled set of @configs
local specs = {}
---@type table Table holding individual plugin configurations.
local configs = {}
---@type table Set of plugin names (last path segment of `src`) explicitly
--- disabled via `enabled = false`, queued for uninstall.
local disabled = {}
--- Validate a single spec and queue its add-spec/config.
---@param name string Spec file name (used in error messages)
---@param plugin table Spec table returned by a plugins/*.lua file
---@return nil
local function process_spec(name, plugin)
if type(plugin) ~= "table" then
vim.notify(
("Plugin spec '%s' did not return a table (got %s). Did you forget `return { ... }`?"):format(name, type(plugin)),
vim.log.levels.ERROR
)
return
elseif not plugin.src then
vim.notify(("Plugin spec '%s' is missing the required `src` field."):format(name), vim.log.levels.ERROR)
return
end
-- Default is enabled; only `enabled = false` opts out.
if plugin.enabled == false then
disabled[plugin.src:gsub("%.git$", ""):match("([^/]+)$")] = true
return
end
table.insert(specs, { src = plugin.src, version = plugin.version })
if type(plugin.config) == "function" then
table.insert(configs, {
name = name,
src = plugin.src,
fn = plugin.config,
priority = plugin.priority or 0,
})
elseif plugin.config ~= nil then
vim.notify(
("Plugin spec '%s' has a `config` that is not a function (got %s); skipping it."):format(
name,
type(plugin.config)
),
vim.log.levels.WARN
)
end
end
for name, type_ in vim.fs.dir(plugins_dir) do
if (type_ == "file" or type_ == "link") and name:match("%.lua$") then
local ok, plugin = pcall(dofile, plugins_dir .. "/" .. name)
if not ok then
vim.notify(
("Plugin spec '%s' failed to load (error while executing the file):\n%s"):format(name, tostring(plugin)),
vim.log.levels.ERROR
)
elseif type(plugin) == "table" and plugin.src == nil and plugin[1] ~= nil then
-- A list of specs.
for _, sub in ipairs(plugin) do
process_spec(name, sub)
end
else
-- A single spec.
process_spec(name, plugin)
end
end
end
--- Deterministic order: higher priority first, then file name.
table.sort(configs, function(a, b)
if a.priority ~= b.priority then
return a.priority > b.priority
end
return a.name < b.name
end)
local ok_add, add_err = pcall(vim.pack.add, specs, { load = true })
if not ok_add then
vim.notify("vim.pack.add failed:\n" .. tostring(add_err), vim.log.levels.ERROR)
end
--- Uninstall any plugins explicitly disabled via `enabled = false`, but only
--- those actually installed (vim.pack.del errors on unknown names).
if next(disabled) then
local to_remove = {}
for _, p in ipairs(vim.pack.get()) do
if disabled[p.spec.name] then
table.insert(to_remove, p.spec.name)
end
end
if #to_remove > 0 then
local ok_del, del_err = pcall(vim.pack.del, to_remove)
if not ok_del then
vim.notify("vim.pack.del failed:\n" .. tostring(del_err), vim.log.levels.ERROR)
end
end
end
for _, config in ipairs(configs) do
local ok, err = pcall(config.fn)
if not ok then
vim.notify(
("Plugin config error in '%s' (%s):\n%s"):format(config.name, config.src, tostring(err)),
vim.log.levels.ERROR
)
end
end