2021-08-13 22:32:56 +02:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
local null_ls = require "null-ls"
|
2021-10-10 21:07:41 +02:00
|
|
|
local services = require "lvim.lsp.null-ls.services"
|
|
|
|
local Log = require "lvim.core.log"
|
2021-08-13 22:32:56 +02:00
|
|
|
|
2021-10-20 20:01:51 +02:00
|
|
|
function M.list_registered_providers(filetype)
|
2021-10-03 16:13:46 +02:00
|
|
|
local null_ls_methods = require "null-ls.methods"
|
|
|
|
local linter_method = null_ls_methods.internal["DIAGNOSTICS"]
|
|
|
|
local registered_providers = services.list_registered_providers_names(filetype)
|
|
|
|
return registered_providers[linter_method] or {}
|
2021-08-13 22:32:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.list_available(filetype)
|
|
|
|
local linters = {}
|
2021-10-10 21:07:41 +02:00
|
|
|
local tbl = require "lvim.utils.table"
|
2021-08-13 22:32:56 +02:00
|
|
|
for _, provider in pairs(null_ls.builtins.diagnostics) do
|
2021-10-09 13:45:34 +02:00
|
|
|
if tbl.contains(provider.filetypes or {}, function(ft)
|
|
|
|
return ft == "*" or ft == filetype
|
|
|
|
end) then
|
2021-08-13 22:32:56 +02:00
|
|
|
table.insert(linters, provider.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return linters
|
|
|
|
end
|
|
|
|
|
2021-10-09 13:39:15 +02:00
|
|
|
function M.list_configured(linter_configs)
|
2021-08-13 22:32:56 +02:00
|
|
|
local linters, errors = {}, {}
|
|
|
|
|
|
|
|
for _, lnt_config in pairs(linter_configs) do
|
2021-10-09 13:45:34 +02:00
|
|
|
local linter_name = lnt_config.exe:gsub("-", "_")
|
|
|
|
local linter = null_ls.builtins.diagnostics[linter_name]
|
2021-08-13 22:32:56 +02:00
|
|
|
|
|
|
|
if not linter then
|
2021-09-01 10:49:20 +02:00
|
|
|
Log:error("Not a valid linter: " .. lnt_config.exe)
|
2021-08-13 22:32:56 +02:00
|
|
|
errors[lnt_config.exe] = {} -- Add data here when necessary
|
|
|
|
else
|
|
|
|
local linter_cmd = services.find_command(linter._opts.command)
|
|
|
|
if not linter_cmd then
|
2021-09-01 10:49:20 +02:00
|
|
|
Log:warn("Not found: " .. linter._opts.command)
|
2021-08-13 22:32:56 +02:00
|
|
|
errors[lnt_config.exe] = {} -- Add data here when necessary
|
|
|
|
else
|
2021-09-01 10:49:20 +02:00
|
|
|
Log:debug("Using linter: " .. linter_cmd)
|
2021-10-08 08:57:11 +02:00
|
|
|
linters[lnt_config.exe] = linter.with {
|
|
|
|
command = linter_cmd,
|
|
|
|
extra_args = lnt_config.args,
|
2021-10-09 13:39:15 +02:00
|
|
|
filetypes = lnt_config.filetypes,
|
2021-10-08 08:57:11 +02:00
|
|
|
}
|
2021-08-13 22:32:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return { supported = linters, unsupported = errors }
|
|
|
|
end
|
|
|
|
|
2021-10-09 13:39:15 +02:00
|
|
|
function M.setup(linter_configs)
|
2021-10-03 16:13:46 +02:00
|
|
|
if vim.tbl_isempty(linter_configs) then
|
2021-08-13 22:32:56 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-10-09 13:39:15 +02:00
|
|
|
local linters = M.list_configured(linter_configs)
|
|
|
|
null_ls.register { sources = linters.supported }
|
2021-08-13 22:32:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|