-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
173 lines (137 loc) · 4.3 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
require('user_api.types')
---@type User
---@diagnostic disable-next-line:missing-fields
local M = {}
M.types = require('user_api.types')
M.util = require('user_api.util')
M.check = require('user_api.check')
M.maps = require('user_api.maps')
M.highlight = require('user_api.highlight')
M.opts = require('user_api.opts')
M.distro = require('user_api.distro')
M.update = require('user_api.update')
M.commands = require('user_api.commands')
M.registered_plugins = {}
---@param self User
---@param pathstr string
---@param i? integer
function M:register_plugin(pathstr, i)
local Value = self.check.value
local is_nil = Value.is_nil
local is_str = Value.is_str
local is_int = Value.is_int
local empty = Value.empty
local notify = self.util.notify.notify
i = (is_int(i) and i >= 1) and i or 0
if not is_str(pathstr) or empty(pathstr) then
error('(user_api.register_plugin): Plugin must be a non-empty string')
end
if vim.tbl_contains(self.registered_plugins, pathstr) then
return
end
---@type nil|string
local warning = nil
if i >= 1 and i <= #self.registered_plugins then
table.insert(self.registered_plugins, i, pathstr)
elseif i < 0 or i > #self.registered_plugins then
warning = 'Invalid index, appending instead'
elseif i == 0 then
table.insert(self.registered_plugins, pathstr)
end
if not is_nil(warning) then
notify(warning, 'warn', {
hide_from_history = false,
animate = false,
timeout = 1750,
title = '(user_api.register_plugin)',
})
end
end
function M:setup_keys()
local wk_avail = self.maps.wk.available
local desc = self.maps.kmap.desc
local map_dict = self.maps.map_dict
local is_nil = self.check.value.is_nil
local notify = self.util.notify.notify
local insp = inspect or vim.inspect
if wk_avail() then
map_dict({
['<leader>U'] = { group = '+User API' },
['<leader>UP'] = { group = '+Plugins' },
}, 'wk.register', false, 'n')
end
map_dict({
['<leader>UPr'] = {
function()
notify('Reloading...', 'info', {
hide_from_history = true,
title = 'User API',
timeout = 400,
})
local res = self:reload_plugins()
if not is_nil(res) then
notify(insp(res), 'error', {
hide_from_history = false,
timeout = 1000,
title = 'User API [ERROR]',
animate = true,
})
return
end
notify('Success!', 'info', {
hide_from_history = true,
timeout = 200,
title = 'User API',
})
end,
desc('Reload All Plugins'),
},
['<leader>UPl'] = {
function() self:print_loaded_plugins() end,
desc('Print Loaded Plugins'),
},
}, 'wk.register', false, 'n')
self.update:setup_maps()
self.opts:setup_maps()
end
---@param self User
---@return string[]|nil
function M:reload_plugins()
local empty = self.check.value.empty
local exists = self.check.exists.module
---@type string[]
local failed = {}
for _, plugin in next, self.registered_plugins do
if not exists(plugin) then
table.insert(failed, plugin)
goto continue
end
require(plugin)
::continue::
end
return empty(failed) and nil or failed
end
---@param self User
function M:print_loaded_plugins()
local notify = self.util.notify.notify
local empty = self.check.value.empty
local nwl = newline or string.char(10)
local msg = ''
for _, P in next, self.registered_plugins do
msg = msg .. nwl .. '- ' .. P
end
notify(msg, empty(msg) and 'error' or 'info', {
animate = true,
hide_from_history = false,
timeout = 1750,
title = 'User API',
})
end
---@param o? table
---@return User|table
function M.new(o)
o = M.check.value.is_tbl(o) and o or {}
return setmetatable(o, { __index = M })
end
return M
--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi: