2021-07-25 03:17:11 +02:00
|
|
|
local M = {}
|
2021-07-31 06:06:08 +02:00
|
|
|
local u = require "utils"
|
|
|
|
local null_ls = require "null-ls"
|
2021-07-25 03:17:11 +02:00
|
|
|
|
2021-07-31 06:06:08 +02:00
|
|
|
local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
|
2021-07-25 03:17:11 +02:00
|
|
|
|
2021-07-31 06:06:08 +02:00
|
|
|
M.requested_providers = {}
|
2021-07-25 03:17:11 +02:00
|
|
|
|
2021-07-31 15:04:22 +02:00
|
|
|
function M.get_registered_providers_by_filetype(ft)
|
|
|
|
local matches = {}
|
|
|
|
for _, provider in pairs(M.requested_providers) do
|
|
|
|
if vim.tbl_contains(provider.filetypes, ft) then
|
2021-07-31 17:56:25 +02:00
|
|
|
local provider_name = provider.name
|
|
|
|
-- special case: show "eslint_d" instead of eslint
|
|
|
|
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
|
|
|
|
if string.find(provider._opts.command, "eslint_d") then
|
|
|
|
provider_name = "eslint_d"
|
|
|
|
end
|
|
|
|
table.insert(matches, provider_name)
|
2021-07-31 15:04:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
|
2021-07-31 06:06:08 +02:00
|
|
|
local function is_nodejs_provider(provider)
|
|
|
|
for _, local_provider in ipairs(nodejs_local_providers) do
|
2021-07-31 17:56:25 +02:00
|
|
|
if local_provider == provider._opts.command then
|
2021-07-31 06:06:08 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
|
2021-07-31 06:06:08 +02:00
|
|
|
local function is_provider_found(provider)
|
|
|
|
local retval = { is_local = false, path = nil }
|
|
|
|
if is_nodejs_provider(provider) then
|
|
|
|
vim.cmd "let root_dir = FindRootDirectory()"
|
|
|
|
local root_dir = vim.api.nvim_get_var "root_dir"
|
|
|
|
local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
|
|
|
|
if vim.fn.executable(local_provider_command) == 1 then
|
2021-08-01 11:57:15 +02:00
|
|
|
return true, local_provider_command
|
2021-07-31 06:06:08 +02:00
|
|
|
end
|
|
|
|
end
|
2021-08-01 11:57:15 +02:00
|
|
|
if vim.fn.executable(provider._opts.command) == 1 then
|
|
|
|
return false, provider._opts.command
|
|
|
|
end
|
2021-07-31 06:06:08 +02:00
|
|
|
return retval.is_local, retval.path
|
2021-07-29 15:03:03 +02:00
|
|
|
end
|
|
|
|
|
2021-07-31 06:06:08 +02:00
|
|
|
local function validate_provider(provider)
|
|
|
|
local is_local, provider_path = is_provider_found(provider)
|
|
|
|
if not provider_path then
|
2021-07-31 17:56:25 +02:00
|
|
|
u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
|
2021-07-31 06:06:08 +02:00
|
|
|
return false
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
2021-07-31 06:06:08 +02:00
|
|
|
if is_local then
|
|
|
|
provider._opts.command = provider_path
|
|
|
|
end
|
|
|
|
return true
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
|
2021-07-25 09:10:08 +02:00
|
|
|
-- TODO: for linters and formatters with spaces and '-' replace with '_'
|
2021-07-31 06:06:08 +02:00
|
|
|
function M.setup(filetype)
|
|
|
|
for _, formatter in pairs(lvim.lang[filetype].formatters) do
|
|
|
|
local builtin_formatter = null_ls.builtins.formatting[formatter.exe]
|
|
|
|
-- FIXME: why doesn't this work?
|
|
|
|
-- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args
|
|
|
|
-- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin
|
|
|
|
table.insert(M.requested_providers, builtin_formatter)
|
|
|
|
u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
2021-07-31 06:06:08 +02:00
|
|
|
|
|
|
|
for _, linter in pairs(lvim.lang[filetype].linters) do
|
|
|
|
local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
|
|
|
|
-- FIXME: why doesn't this work?
|
|
|
|
-- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
|
|
|
|
-- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
|
2021-08-01 01:45:42 +02:00
|
|
|
-- NOTE: validate before inserting to table
|
2021-07-31 17:29:52 +02:00
|
|
|
-- special case: fallback to "eslint"
|
|
|
|
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
|
|
|
|
if linter.exe == "eslint_d" then
|
2021-08-01 11:57:15 +02:00
|
|
|
builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
|
|
|
|
end
|
|
|
|
if validate_provider(builtin_diagnoser) then
|
|
|
|
table.insert(M.requested_providers, builtin_diagnoser)
|
2021-07-31 17:29:52 +02:00
|
|
|
end
|
2021-07-31 06:06:08 +02:00
|
|
|
u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
|
2021-07-31 06:06:08 +02:00
|
|
|
null_ls.register { sources = M.requested_providers }
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|