-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathsources.lua
More file actions
335 lines (280 loc) · 8.84 KB
/
Copy pathsources.lua
File metadata and controls
335 lines (280 loc) · 8.84 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
local diagnostics = require("null-ls.diagnostics")
local log = require("null-ls.logger")
local methods = require("null-ls.methods")
local s = require("null-ls.state")
local u = require("null-ls.utils")
local validate = u.validate
local registered = {
names = {},
sources = {},
id = 0,
}
local M = {}
local matches_filetype = function(source, filetype)
if filetype:find("%.") then
local filetypes = vim.split(filetype, ".", { plain = true })
table.insert(filetypes, filetype)
local is_match = source.filetypes["_all"]
for _, ft in ipairs(filetypes) do
if source.filetypes[ft] == false then
return false
end
is_match = is_match or source.filetypes[ft]
end
return is_match
end
return source.filetypes[filetype] or source.filetypes["_all"] and source.filetypes[filetype] == nil
end
local matches_method = function(source, method)
if source.methods[method] then
return true
end
if methods.overrides[method] then
for m in pairs(methods.overrides[method]) do
if source.methods[m] then
return true
end
end
end
return false
end
local matches_query = function(source, query)
query = type(query) == "string" and { name = vim.pesc(query) } or query
local name, method, id, filetype = query.name, query.method, query.id, query.filetype
local name_matches = name == nil and true or source.name:find(name)
local method_matches = method == nil and true or matches_method(source, method)
local id_matches = id == nil and true or source.id == id
local filetype_matches = filetype == nil and true or matches_filetype(source, filetype)
return name_matches and method_matches and id_matches and filetype_matches
end
local for_each_matching = function(query, cb)
for _, source in ipairs(M.get_all()) do
if matches_query(source, query) then
cb(source)
end
end
end
local register_source = function(source)
source = M.validate_and_transform(source)
if not source then
return
end
if source.condition then
local condition = source.condition
source.try_register = function()
if condition(u.make_conditional_utils()) then
log:debug("registering conditional source " .. source.name)
M.register(source)
else
log:debug("not registering conditional source " .. source.name)
end
source.try_register = nil
end
-- prevent infinite loop
source.condition = nil
s.push_conditional_source(source)
return
end
table.insert(registered.sources, source)
registered.names[source.name] = true
end
M.is_available = function(source, filetype, method)
if source._disabled or source.generator._failed then
return false
end
return (not filetype or matches_filetype(source, filetype)) and (not method or matches_method(source, method))
end
M.get_available = function(filetype, method)
local available = {}
for _, source in ipairs(M.get_all()) do
if M.is_available(source, filetype, method) then
table.insert(available, source)
end
end
return available
end
M.get_supported = function(filetype, method)
local ft_map = require("null-ls.builtins._meta.filetype_map")
local supported = ft_map[filetype] or {}
if method then
return supported[method] or {}
end
return supported
end
M.get = function(query)
local matching = {}
for_each_matching(query, function(source)
table.insert(matching, source)
end)
return matching
end
M.enable = function(query)
for_each_matching(query, function(source)
source._disabled = false
end)
require("null-ls.client").on_source_change()
end
M.disable = function(query)
for_each_matching(query, function(source)
source._disabled = true
diagnostics.hide_source_diagnostics(source.id)
end)
end
M.toggle = function(query)
for_each_matching(query, function(source)
source._disabled = not source._disabled
if source._disabled then
diagnostics.hide_source_diagnostics(source.id)
end
end)
require("null-ls.client").on_source_change()
end
M.get_all = function()
return registered.sources
end
M.get_filetypes = function()
local filetypes = {}
for _, source in ipairs(M.get_all()) do
for ft in pairs(source.filetypes) do
if ft ~= "_all" and not vim.tbl_contains(filetypes, ft) then
table.insert(filetypes, ft)
end
end
end
return filetypes
end
M.deregister = function(query)
local all_sources = M.get_all()
-- iterate backwards to remove in loop
for i = #all_sources, 1, -1 do
if matches_query(all_sources[i], query) then
table.remove(all_sources, i)
end
end
require("null-ls.client").update_filetypes()
end
M.validate_and_transform = function(source)
if type(source) == "function" then
source = source()
if not source then
return
end
end
if source._validated then
return source
end
local generator, name, can_run = source.generator, source.name or "anonymous source", source.can_run
generator.opts = generator.opts or {}
generator.opts.name = name
local source_methods = type(source.method) == "table" and source.method or { source.method }
local condition, config, filetypes, disabled_filetypes =
source.condition, source.config, source.filetypes, source.disabled_filetypes
validate({
generator = { generator, "table" },
filetypes = { filetypes, "table" },
disabled_filetypes = { disabled_filetypes, "table", true },
condition = { condition, "function", true },
config = { config, "table", true },
name = { name, "string" },
can_run = { can_run, "function", true },
fn = { generator.fn, "function" },
opts = { generator.opts, "table" },
async = { generator.async, "boolean", true },
method = {
source_methods,
function(m)
return not vim.tbl_isempty(m), "at least one method"
end,
},
})
-- use map for filetypes and methods to simplify checks
local filetype_map, method_map = {}, {}
if vim.tbl_isempty(filetypes) then
filetype_map["_all"] = true
else
for _, ft in ipairs(filetypes) do
filetype_map[ft] = true
end
end
if disabled_filetypes then
for _, ft in ipairs(disabled_filetypes) do
filetype_map[ft] = false
end
end
for _, method in ipairs(source_methods) do
validate({
method = {
method,
function(m)
return require("null-ls.methods").internal[m] ~= nil
end,
"supported null-ls method",
},
})
method_map[method] = true
end
registered.id = registered.id + 1
local id = registered.id
generator.source_id = id
return {
id = id,
name = name,
can_run = can_run,
generator = generator,
filetypes = filetype_map,
methods = method_map,
condition = condition,
config = config,
_validated = true,
}
end
M.register = function(to_register)
if
type(to_register) == "function"
or (type(to_register) == "table" and (to_register.method or to_register._validated))
then
-- register a single source
register_source(to_register)
elseif not to_register.sources then
-- register a simple list of sources
for _, source in ipairs(to_register) do
register_source(source)
end
else
-- register multiple sources with shared configuration
for _, source in ipairs(to_register.sources) do
source.filetypes = to_register.filetypes or source.filetypes
source.name = to_register.name or source.name
register_source(source)
end
end
require("null-ls.client").on_source_change()
require("null-ls.client").update_filetypes()
end
M.reset = function()
registered.sources = {}
registered.names = {}
require("null-ls.client").update_filetypes()
end
M.is_registered = function(query)
if type(query) == "string" and registered.names[query] then
return true
end
local found
for_each_matching(query, function(source)
found = source
end)
return found ~= nil
end
M.register_name = function(name)
registered.names[name] = true
end
M._reset = function()
registered.sources = {}
registered.names = {}
registered.id = 0
end
M._set = function(sources)
registered.sources = sources
end
return M