2021-07-25 03:17:11 +02:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
local null_ls = require "null-ls"
|
|
|
|
local sources = {}
|
|
|
|
|
|
|
|
local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
|
|
|
|
|
|
|
|
local function is_table(t)
|
|
|
|
return type(t) == "table"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_string(t)
|
|
|
|
return type(t) == "string"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function has_value(tab, val)
|
2021-07-25 13:36:46 +02:00
|
|
|
for _, value in ipairs(tab) do
|
2021-07-25 03:17:11 +02:00
|
|
|
if value == val then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local find_local_exe = function(exe)
|
|
|
|
vim.cmd "let root_dir = FindRootDirectory()"
|
|
|
|
local root_dir = vim.api.nvim_get_var "root_dir"
|
|
|
|
local local_exe = root_dir .. "/node_modules/.bin/" .. exe
|
|
|
|
return local_exe
|
|
|
|
end
|
|
|
|
|
|
|
|
local function setup_ls(exe, type)
|
|
|
|
if has_value(local_executables, exe) then
|
|
|
|
local smart_executable = null_ls.builtins[type][exe]
|
|
|
|
local local_executable = find_local_exe(exe)
|
2021-07-25 08:49:16 +02:00
|
|
|
if vim.fn.executable(local_executable) == 1 then
|
2021-07-25 03:17:11 +02:00
|
|
|
smart_executable._opts.command = local_executable
|
2021-07-25 08:49:16 +02:00
|
|
|
table.insert(sources, smart_executable)
|
|
|
|
else
|
|
|
|
if vim.fn.executable(exe) == 1 then
|
|
|
|
table.insert(sources, smart_executable)
|
|
|
|
end
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
else
|
2021-07-25 08:49:16 +02:00
|
|
|
if vim.fn.executable(exe) == 1 then
|
|
|
|
table.insert(sources, null_ls.builtins[type][exe])
|
|
|
|
end
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
null_ls.register { sources = sources }
|
|
|
|
end
|
|
|
|
|
2021-07-25 09:10:08 +02:00
|
|
|
-- TODO: for linters and formatters with spaces and '-' replace with '_'
|
2021-07-25 03:17:11 +02:00
|
|
|
local function setup(filetype, type)
|
2021-07-25 06:13:35 +02:00
|
|
|
local executables = nil
|
2021-07-25 03:17:11 +02:00
|
|
|
if type == "diagnostics" then
|
|
|
|
executables = lvim.lang[filetype].linters
|
|
|
|
end
|
|
|
|
if type == "formatting" then
|
|
|
|
executables = lvim.lang[filetype].formatter.exe
|
|
|
|
end
|
|
|
|
|
|
|
|
if is_table(executables) then
|
|
|
|
for _, exe in pairs(executables) do
|
2021-07-25 19:58:34 +02:00
|
|
|
if exe ~= "" then
|
|
|
|
setup_ls(exe, type)
|
|
|
|
end
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
end
|
2021-07-25 19:58:34 +02:00
|
|
|
if is_string(executables) and executables ~= "" then
|
2021-07-25 03:17:11 +02:00
|
|
|
setup_ls(executables, type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-25 09:10:08 +02:00
|
|
|
-- TODO: return the formatter if one was registered, then turn off the builtin formatter
|
2021-07-25 03:17:11 +02:00
|
|
|
function M.setup(filetype)
|
|
|
|
setup(filetype, "formatting")
|
|
|
|
setup(filetype, "diagnostics")
|
2021-07-25 20:45:40 +02:00
|
|
|
lvim.sources = sources
|
|
|
|
return sources
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|