2021-07-25 03:17:11 +02:00
|
|
|
local M = {}
|
|
|
|
|
2021-07-29 08:01:15 +02:00
|
|
|
local _, null_ls = pcall(require, "null-ls")
|
2021-07-28 23:24:55 +02:00
|
|
|
local utils = require "utils"
|
2021-07-25 03:17:11 +02:00
|
|
|
local sources = {}
|
|
|
|
|
|
|
|
local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
|
|
|
|
|
|
|
|
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)
|
2021-07-28 23:24:55 +02:00
|
|
|
if utils.has_value(local_executables, exe) then
|
2021-07-25 03:17:11 +02:00
|
|
|
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-28 17:03:49 +02:00
|
|
|
if null_ls.builtins[type][exe] and vim.fn.executable(null_ls.builtins[type][exe]._opts.command) then
|
2021-07-25 08:49:16 +02:00
|
|
|
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
|
|
|
|
|
2021-07-28 23:24:55 +02:00
|
|
|
if utils.is_table(executables) then
|
2021-07-25 03:17:11 +02:00
|
|
|
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-28 23:24:55 +02:00
|
|
|
if utils.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
|