feat(conform): show error when user overwrites conform config function

This commit is contained in:
Folke Lemaitre 2023-10-13 18:20:41 +02:00
parent 3eb91c64b5
commit b6e68fa2bf
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -1,8 +1,41 @@
local Util = require("lazyvim.util") local Util = require("lazyvim.util")
local M = {}
---@type ConformOpts ---@type ConformOpts
local format_opts = {} local format_opts = {}
---@param opts ConformOpts
function M.setup(plugin, opts)
local util = require("conform.util")
opts.formatters = opts.formatters or {}
for name, formatter in pairs(opts.formatters) do
if type(formatter) == "table" then
local ok, defaults = pcall(require, "conform.formatters." .. name)
if ok and type(defaults) == "table" then
opts.formatters[name] = vim.tbl_deep_extend("force", {}, defaults, formatter)
end
if opts.formatters[name].extra_args then
opts.formatters[name].args =
util.extend_args(opts.formatters[name].args or {}, opts.formatters[name].extra_args)
end
end
end
for _, key in ipairs({ "format_on_save", "format_after_save" }) do
if opts[key] then
Util.warn(
("Don't set `opts.%s` for `conform.nvim`.\n**LazyVim** will use the conform formatter automatically"):format(
key
)
)
opts[key] = nil
end
end
format_opts = opts.format
require("conform").setup(opts)
end
return { return {
{ {
"stevearc/conform.nvim", "stevearc/conform.nvim",
@ -39,62 +72,45 @@ return {
}) })
end) end)
end, end,
---@class ConformOpts opts = function()
opts = { local plugin = require("lazy.core.config").plugins["conform.nvim"]
-- LazyVim will use these options when formatting with the conform.nvim formatter if plugin.config ~= M.setup then
format = { Util.error({
timeout_ms = 1000, "Don't set `plugin.config` for `conform.nvim`.\n",
}, "This will break **LazyVim** formatting.\n",
formatters_by_ft = { "Please refer to the docs at https://www.lazyvim.org/plugins/formatting",
lua = { "stylua" }, }, { title = "LazyVim" })
fish = { "fish_indent" }, end
sh = { "shfmt" }, ---@class ConformOpts
}, return {
-- LazyVim will merge the options you set here with builtin formatters. -- LazyVim will use these options when formatting with the conform.nvim formatter
-- You can also define any custom formatters here. format = {
---@type table<string,table> timeout_ms = 1000,
formatters = {
injected = { options = { ignore_errors = true } },
-- -- Example of using dprint only when a dprint.json file is present
-- dprint = {
-- condition = function(ctx)
-- return vim.fs.find({ "dprint.json" }, { path = ctx.filename, upward = true })[1]
-- end,
-- },
shfmt = {
extra_args = { "-i", "2", "-ci" },
}, },
}, formatters_by_ft = {
}, lua = { "stylua" },
---@param opts ConformOpts fish = { "fish_indent" },
config = function(_, opts) sh = { "shfmt" },
local util = require("conform.util") },
opts.formatters = opts.formatters or {} -- LazyVim will merge the options you set here with builtin formatters.
for name, formatter in pairs(opts.formatters) do -- You can also define any custom formatters here.
if type(formatter) == "table" then ---@type table<string,table>
local ok, defaults = pcall(require, "conform.formatters." .. name) formatters = {
if ok and type(defaults) == "table" then injected = { options = { ignore_errors = true } },
opts.formatters[name] = vim.tbl_deep_extend("force", {}, defaults, formatter) -- # Example of using dprint only when a dprint.json file is present
end -- dprint = {
if opts.formatters[name].extra_args then -- condition = function(ctx)
opts.formatters[name].args = -- return vim.fs.find({ "dprint.json" }, { path = ctx.filename, upward = true })[1]
util.extend_args(opts.formatters[name].args or {}, opts.formatters[name].extra_args) -- end,
end -- },
end --
end -- # Example of using shfmt with extra args
-- shfmt = {
for _, key in ipairs({ "format_on_save", "format_after_save" }) do -- extra_args = { "-i", "2", "-ci" },
if opts[key] then -- },
Util.warn( },
("Don't set `opts.%s` for `conform.nvim`.\n**LazyVim** will use the conform formatter automatically"):format( }
key
)
)
opts[key] = nil
end
end
format_opts = opts.format
require("conform").setup(opts)
end, end,
config = M.setup,
}, },
} }