mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-21 16:39:06 +02:00
## Description This adds an option to disable eslint's autoformatting. The reasons for not wanting an auto format by eslint are: 1. It is generally not recommended to use a linter as a formatter as highlighted by https://typescript-eslint.io/users/what-about-formatting/#:~:text=We%20recommend%20against%20using%20ESLint,dprint%2C%20or%20an%20equivalent%20instead 2. A personal preference not to have a linter change my code which I'm sure other people share. 3. As highlighted in the link above, eslint auto format can be quite slow. I would even argue that this should be the default, but I've left it as is in order not to break anyone's config or ruffle anyone's feathers. ## Checklist - [x] I've read the [CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md) guidelines.
64 lines
1.8 KiB
Lua
64 lines
1.8 KiB
Lua
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
|
|
|
|
return {
|
|
{
|
|
"neovim/nvim-lspconfig",
|
|
-- other settings removed for brevity
|
|
opts = {
|
|
---@type lspconfig.options
|
|
servers = {
|
|
eslint = {
|
|
settings = {
|
|
-- helps eslint find the eslintrc when it's placed in a subfolder instead of the cwd root
|
|
workingDirectories = { mode = "auto" },
|
|
format = auto_format,
|
|
},
|
|
},
|
|
},
|
|
setup = {
|
|
eslint = function()
|
|
if not auto_format then
|
|
return
|
|
end
|
|
|
|
local function get_client(buf)
|
|
return LazyVim.lsp.get_clients({ name = "eslint", bufnr = buf })[1]
|
|
end
|
|
|
|
local formatter = LazyVim.lsp.formatter({
|
|
name = "eslint: lsp",
|
|
primary = false,
|
|
priority = 200,
|
|
filter = "eslint",
|
|
})
|
|
|
|
-- 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)
|
|
if client then
|
|
local diag = vim.diagnostic.get(buf, { namespace = vim.lsp.diagnostic.get_namespace(client.id) })
|
|
if #diag > 0 then
|
|
vim.cmd("EslintFixAll")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- register the formatter with LazyVim
|
|
LazyVim.format.register(formatter)
|
|
end,
|
|
},
|
|
},
|
|
},
|
|
}
|