2024-11-09 00:05:00 +11:00
|
|
|
if lazyvim_docs then
|
|
|
|
-- Set to false to disable auto format
|
|
|
|
vim.g.lazyvim_eslint_auto_format = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local auto_format = vim.g.lazyvim_eslint_auto_format == nil or vim.g.lazyvim_eslint_auto_format
|
|
|
|
|
2023-03-10 08:44:47 +01:00
|
|
|
return {
|
|
|
|
{
|
|
|
|
"neovim/nvim-lspconfig",
|
|
|
|
-- other settings removed for brevity
|
|
|
|
opts = {
|
2023-05-19 22:49:19 +02:00
|
|
|
---@type lspconfig.options
|
2023-03-10 08:44:47 +01:00
|
|
|
servers = {
|
|
|
|
eslint = {
|
|
|
|
settings = {
|
|
|
|
-- helps eslint find the eslintrc when it's placed in a subfolder instead of the cwd root
|
2024-01-21 10:53:09 -08:00
|
|
|
workingDirectories = { mode = "auto" },
|
2024-11-09 00:05:00 +11:00
|
|
|
format = auto_format,
|
2023-03-10 08:44:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setup = {
|
|
|
|
eslint = function()
|
2024-11-09 00:05:00 +11:00
|
|
|
if not auto_format then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-10-10 19:31:08 +02:00
|
|
|
local function get_client(buf)
|
2024-03-22 09:15:09 +01:00
|
|
|
return LazyVim.lsp.get_clients({ name = "eslint", bufnr = buf })[1]
|
2023-10-10 19:31:08 +02:00
|
|
|
end
|
|
|
|
|
2024-03-22 09:15:09 +01:00
|
|
|
local formatter = LazyVim.lsp.formatter({
|
2023-10-10 19:31:08 +02:00
|
|
|
name = "eslint: lsp",
|
|
|
|
primary = false,
|
|
|
|
priority = 200,
|
|
|
|
filter = "eslint",
|
|
|
|
})
|
2023-06-08 07:48:34 +02:00
|
|
|
|
2023-10-10 19:31:08 +02:00
|
|
|
-- Use EslintFixAll on Neovim < 0.10.0
|
|
|
|
if not pcall(require, "vim.lsp._dynamic") then
|
|
|
|
formatter.name = "eslint: EslintFixAll"
|
|
|
|
formatter.sources = function(buf)
|
|
|
|
local client = get_client(buf)
|
|
|
|
return client and { "eslint" } or {}
|
|
|
|
end
|
|
|
|
formatter.format = function(buf)
|
|
|
|
local client = get_client(buf)
|
2023-05-19 22:49:19 +02:00
|
|
|
if client then
|
2023-10-10 19:31:08 +02:00
|
|
|
local diag = vim.diagnostic.get(buf, { namespace = vim.lsp.diagnostic.get_namespace(client.id) })
|
2023-05-19 22:49:19 +02:00
|
|
|
if #diag > 0 then
|
|
|
|
vim.cmd("EslintFixAll")
|
|
|
|
end
|
2023-03-10 08:54:56 +01:00
|
|
|
end
|
2023-10-10 19:31:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- register the formatter with LazyVim
|
2024-03-22 09:15:09 +01:00
|
|
|
LazyVim.format.register(formatter)
|
2023-03-10 08:44:47 +01:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|