forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
318 lines (288 loc) · 8.04 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
-- Rafi's Neovim config loader
-- https://github.com/rafi/vim-config
-- This is part of LazyVim's code, with my modifications.
-- See: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/init.lua
---@type RafiConfig
local M = {}
M.lazy_version = '>=9.1.0'
---@class RafiConfig
local defaults = {
-- Load the default settings
-- stylua: ignore
defaults = {
autocmds = true, -- rafi.config.autocmds
keymaps = true, -- rafi.config.keymaps
-- rafi.config.options can't be configured here since it's loaded
-- prematurely. You can disable loading options with the following line at
-- the top of your lua/config/setup.lua or init.lua:
-- `package.loaded['rafi.config.options'] = true`
},
-- String like `habamax` or a function that will load the colorscheme.
-- Disabled by default to allow theme-loader.nvim to manage the colorscheme.
---@type string|fun()
colorscheme = '',
features = {
elite_mode = false,
window_q_mapping = true,
},
-- stylua: ignore
icons = {
git = ' ',
diagnostics = {
Error = '✘', -- ✘
Warn = '', --
Info = 'ⁱ', -- ⁱ
Hint = '', --
},
status = {
git = {
added = '₊', -- ₊
modified = '∗', -- ∗
removed = '₋', -- ₋
},
diagnostics = {
error = ' ',
warn = ' ',
info = ' ',
hint = ' ',
},
filename = {
modified = '+',
readonly = '🔒',
zoomed = '🔎',
},
},
-- Default completion kind symbols.
kinds = {
Array = ' ', --
Boolean = '◩ ', -- ◩
Class = ' ', -- 𝓒
Color = ' ', --
Constant = ' ', --
Constructor = ' ', --
Copilot = ' ', --
Enum = ' ', -- ℰ
EnumMember = ' ', --
Event = ' ', --
Field = ' ', --
File = ' ', --
Folder = ' ', --
Function = ' ', --
Interface = ' ', --
Key = ' ', --
Keyword = ' ', --
Method = ' ', -- ƒ
Module = ' ', --
Namespace = ' ', --
Null = ' ', --
Number = ' ', --
Object = ' ', --
Operator = ' ', -- +
Package = ' ', --
Property = ' ', --
Reference = ' ', --
Snippet = ' ', -- ⮡
String = ' ', --
Struct = ' ', -- 𝓢
Text = ' ', -- 𝓐
TypeParameter = ' ', -- 𝙏
Unit = ' ', --
Value = ' ', --
Variable = ' ', --
},
},
}
M.renames = {}
M.did_init = false
function M.init()
if not M.did_init then
M.did_init = true
-- delay notifications till vim.notify was replaced or after 500ms
require('rafi.config').lazy_notify()
-- load options here, before lazy init while sourcing plugin modules
-- this is needed to make sure options will be correctly applied
-- after installing missing plugins
require('rafi.config').load('options')
-- carry over plugin options that their name has been changed.
local Plugin = require('lazy.core.plugin')
local add = Plugin.Spec.add
---@diagnostic disable-next-line: duplicate-set-field
Plugin.Spec.add = function(self, plugin, ...)
if type(plugin) == 'table' and M.renames[plugin[1]] then
plugin[1] = M.renames[plugin[1]]
end
return add(self, plugin, ...)
end
end
end
---@type RafiConfig
local options
-- Load rafi and user config files.
---@param user_opts table|nil
function M.setup(user_opts)
if not M.did_init then
M.init()
end
options = vim.tbl_deep_extend('force', defaults, user_opts or {})
if not M.has_version() then
require('lazy.core.util').error(
string.format(
'**lazy.nvim** version %s is required.\n Please upgrade **lazy.nvim**',
M.lazy_version
)
)
error('Exiting')
end
-- Override config with user config at lua/config/setup.lua
local ok, user_setup = pcall(require, 'config.setup')
if ok and user_setup.override then
options = vim.tbl_deep_extend('force', options, user_setup.override())
end
for feat_name, feat_val in pairs(options.features) do
vim.g['rafi_' .. feat_name] = feat_val
end
M.load('autocmds')
M.load('keymaps')
-- Set colorscheme
require('lazy.core.util').try(function()
if type(M.colorscheme) == 'function' then
M.colorscheme()
elseif #M.colorscheme > 0 then
vim.cmd.colorscheme(M.colorscheme)
end
end, {
msg = 'Could not load your colorscheme',
on_error = function(msg)
require('lazy.core.util').error(msg)
vim.cmd.colorscheme('habamax')
end,
})
end
---@return table
function M.user_lazy_opts()
local ok, user_setup = pcall(require, 'config.setup')
if ok and user_setup.lazy_opts then
return user_setup.lazy_opts()
end
return {}
end
---@param range? string
---@return boolean
function M.has_version(range)
local Semver = require('lazy.manage.semver')
return Semver.range(range or M.lazy_version)
:matches(require('lazy.core.config').version or '0.0.0')
end
---@param name "autocmds" | "options" | "keymaps"
function M.load(name)
local Util = require('lazy.core.util')
local function _load(mod)
Util.try(function()
require(mod)
end, {
msg = 'Failed loading ' .. mod,
on_error = function(msg)
local info = require('lazy.core.cache').find(mod)
if info == nil or (type(info) == 'table' and #info == 0) then
return
end
Util.error(msg)
end,
})
end
-- always load rafi's file, then user file
if M.defaults[name] or name == 'options' then
_load('rafi.config.' .. name)
end
_load('config.' .. name)
if vim.bo.filetype == 'lazy' then
vim.cmd([[do VimResized]])
end
end
-- Ensure package manager (lazy.nvim) exists.
function M.ensure_lazy()
local lazypath = M.path_join(vim.fn.stdpath('data'), 'lazy', 'lazy.nvim')
if not vim.loop.fs_stat(lazypath) then
print('Installing lazy.nvim…')
vim.fn.system({
'git',
'clone',
'--filter=blob:none',
'--branch=stable',
'https://github.com/folke/lazy.nvim.git',
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
end
-- Validate if lua/plugins/ or lua/plugins.lua exist.
---@return boolean
function M.has_user_plugins()
local user_path = M.path_join(vim.fn.stdpath('config'), 'lua')
return vim.loop.fs_stat(M.path_join(user_path, 'plugins')) ~= nil
or vim.loop.fs_stat(M.path_join(user_path, 'plugins.lua')) ~= nil
end
-- Delay notifications till vim.notify was replaced or after 500ms.
function M.lazy_notify()
local notifs = {}
local function temp(...)
table.insert(notifs, vim.F.pack_len(...))
end
local orig = vim.notify
vim.notify = temp
local timer = vim.loop.new_timer()
local check = vim.loop.new_check()
if timer == nil or check == nil then
return
end
local replay = function()
timer:stop()
check:stop()
if vim.notify == temp then
vim.notify = orig -- put back the original notify if needed
end
vim.schedule(function()
---@diagnostic disable-next-line: no-unknown
for _, notif in ipairs(notifs) do
vim.notify(vim.F.unpack_len(notif))
end
end)
end
-- wait till vim.notify has been replaced
check:start(function()
if vim.notify ~= temp then
replay()
end
end)
-- or if it took more than 500ms, then something went wrong
timer:start(500, 0, replay)
end
-- Join paths.
---@private
M.path_join = function(...)
return table.concat({ ... }, M.path_sep)
end
-- Variable holds OS directory separator.
---@private
M.path_sep = (function()
if jit then
local os = string.lower(jit.os)
if os ~= 'windows' then
return '/'
else
return '\\'
end
else
return package.config:sub(1, 1)
end
end)()
setmetatable(M, {
__index = function(_, key)
if options == nil then
return vim.deepcopy(defaults)[key]
end
---@cast options RafiConfig
return options[key]
end,
})
return M