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-11-21 12:29:07 +01:00
|
|
|
local is_registered = function(name)
|
|
|
|
local query = {
|
|
|
|
name = name,
|
|
|
|
method = require("null-ls").methods.DIAGNOSTICS,
|
|
|
|
}
|
|
|
|
return require("null-ls.sources").is_registered(query)
|
|
|
|
end
|
2021-11-16 15:55:45 +01: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
|
2021-11-16 15:55:45 +01:00
|
|
|
|
2021-11-06 13:18:59 +01:00
|
|
|
table.sort(linters)
|
2021-08-13 22:32:56 +02:00
|
|
|
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-11-16 15:55:45 +01:00
|
|
|
local name = lnt_config.exe:gsub("-", "_")
|
|
|
|
local linter = null_ls.builtins.diagnostics[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
|
2021-11-16 15:55:45 +01:00
|
|
|
elseif is_registered(lnt_config.exe) then
|
|
|
|
Log:trace "Skipping registering the source more than once"
|
2021-08-13 22:32:56 +02:00
|
|
|
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-11-16 15:55:45 +01:00
|
|
|
errors[name] = {} -- Add data here when necessary
|
2021-08-13 22:32:56 +02:00
|
|
|
else
|
2021-09-01 10:49:20 +02:00
|
|
|
Log:debug("Using linter: " .. linter_cmd)
|
2021-11-16 15:55:45 +01:00
|
|
|
table.insert(
|
|
|
|
linters,
|
|
|
|
linter.with {
|
|
|
|
command = linter_cmd,
|
|
|
|
extra_args = lnt_config.args,
|
|
|
|
filetypes = lnt_config.filetypes,
|
|
|
|
}
|
|
|
|
)
|
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
|