generated from mrcjkb/nvim-lua-nix-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinit.lua
155 lines (145 loc) · 4.14 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
local M = {}
local handlers = {
cmd = require("lz.n.handler.cmd"),
event = require("lz.n.handler.event"),
ft = require("lz.n.handler.ft"),
keys = require("lz.n.handler.keys"),
colorscheme = require("lz.n.handler.colorscheme"),
lazy = require("lz.n.handler.lazy"),
}
---@param name string
---@param opts? lz.n.lookup.Opts
---@return lz.n.Plugin | nil
function M.lookup(name, opts)
---@type string | string[] | nil
local filter = opts and vim.tbl_get(opts, "filter")
if type(filter) == "string" then
filter = { filter }
end
---@type lz.n.Handler[]
local handler_list = filter
and vim.iter(filter)
:map(function(key)
return handlers[key]
end)
:totable()
or vim.tbl_values(handlers)
---@cast filter string[] | nil
---@type lz.n.Plugin | nil
local result = vim
.iter(handler_list)
---@param handler lz.n.Handler
:map(function(handler)
return handler.lookup(name)
end)
---@param result lz.n.Plugin?
:find(function(result)
return result ~= nil
end)
return result and vim.deepcopy(result)
end
---@param handler lz.n.Handler
---@return boolean success
function M.register_handler(handler)
if not handler.lookup then
vim.schedule(function()
vim.notify(
([[
lz.n: handler for %s does not have a 'lookup' function.
Ignoring.
]]):format(handler.spec_field),
vim.log.levels.WARN
)
end)
return false
end
if not handler.del then
vim.schedule(function()
vim.notify(
([[
lz.n: handler for %s does not have a 'del' function.
Ignoring.
]]):format(handler.spec_field),
vim.log.levels.WARN
)
end)
return false
end
if handlers[handler.spec_field] == nil then
handlers[handler.spec_field] = handler
return true
else
vim.notify(
"Handler already exists for " .. handler.spec_field .. ". Refusing to register new handler.",
vim.log.levels.ERROR,
{ title = "lz.n" }
)
return false
end
end
---@param plugin lz.n.Plugin
local function enable(plugin)
---@param handler lz.n.Handler
vim.iter(handlers):each(function(_, handler)
handler.add(plugin)
end)
end
---@param spec lz.n.PluginSpec
---@return boolean
local function is_lazy(spec)
---@diagnostic disable-next-line: undefined-field
if spec.lazy ~= nil then
---@diagnostic disable-next-line: undefined-field
return spec.lazy
end
return vim.iter(handlers):any(function(spec_field, _)
-- PERF: This should be simpler and more performant than
-- filtering out "lazy" spec fields. However, this also
-- assumes that 'false' means a handler is disabled.
return spec[spec_field] and spec[spec_field] ~= nil
end)
end
---Mutates the `plugin`.
---@param plugin lz.n.Plugin
---@param spec lz.n.PluginSpec
function M.parse(plugin, spec)
vim
.iter(handlers)
---@param spec_field string
---@param handler lz.n.Handler
:each(function(spec_field, handler)
if handler.parse then
handler.parse(plugin, spec[spec_field])
end
end)
plugin.lazy = is_lazy(spec)
end
---@param name string
function M.disable(name)
---@param handler lz.n.Handler
vim.iter(handlers):each(function(_, handler)
handler.del(name)
end)
end
function M.run_post_load()
---@param handler lz.n.Handler
vim.iter(handlers):each(function(_, handler)
if handler.post_load then
handler.post_load()
end
end)
end
---@param plugins table<string, lz.n.Plugin>
function M.init(plugins)
---@param plugin lz.n.Plugin
vim.iter(plugins):each(function(_, plugin)
xpcall(
enable,
vim.schedule_wrap(function(err)
vim.notify(("Failed to enable handlers for %s: %s"):format(plugin.name, err), vim.log.levels.ERROR)
end),
plugin
)
end)
end
return M