feat(format): new LazyVim formatter with integrations for lsp/none-ls/conform/eslint/...

This commit is contained in:
Folke Lemaitre
2023-10-10 19:31:08 +02:00
parent c8c929c9fd
commit f1a8f24a36
7 changed files with 225 additions and 148 deletions

View File

@ -14,22 +14,37 @@ return {
},
setup = {
eslint = function()
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(event)
if not require("lazyvim.plugins.lsp.format").enabled() then
-- exit early if autoformat is not enabled
return
end
local function get_client(buf)
return require("lazyvim.util").lsp.get_clients({ name = "eslint", bufnr = buf })[1]
end
local client = require("lazyvim.util").get_clients({ bufnr = event.buf, name = "eslint" })[1]
local formatter = require("lazyvim.util").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(event.buf, { namespace = vim.lsp.diagnostic.get_namespace(client.id) })
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
end
-- register the formatter with LazyVim
require("lazyvim.util").format.register(formatter)
end,
},
},