Skip to content

Commit

Permalink
feat(conform): show error when user overwrites conform config function
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 13, 2023
1 parent 3eb91c6 commit b6e68fa
Showing 1 changed file with 71 additions and 55 deletions.
126 changes: 71 additions & 55 deletions lua/lazyvim/plugins/formatting.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
local Util = require("lazyvim.util")

local M = {}

---@type ConformOpts
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 {
{
"stevearc/conform.nvim",
Expand Down Expand Up @@ -39,62 +72,45 @@ return {
})
end)
end,
---@class ConformOpts
opts = {
-- LazyVim will use these options when formatting with the conform.nvim formatter
format = {
timeout_ms = 1000,
},
formatters_by_ft = {
lua = { "stylua" },
fish = { "fish_indent" },
sh = { "shfmt" },
},
-- LazyVim will merge the options you set here with builtin formatters.
-- You can also define any custom formatters here.
---@type table<string,table>
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" },
},
},
},
---@param opts ConformOpts
config = function(_, 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
opts = function()
local plugin = require("lazy.core.config").plugins["conform.nvim"]
if plugin.config ~= M.setup then
Util.error({
"Don't set `plugin.config` for `conform.nvim`.\n",
"This will break **LazyVim** formatting.\n",
"Please refer to the docs at https://www.lazyvim.org/plugins/formatting",
}, { title = "LazyVim" })
end
format_opts = opts.format
require("conform").setup(opts)
---@class ConformOpts
return {
-- LazyVim will use these options when formatting with the conform.nvim formatter
format = {
timeout_ms = 1000,
},
formatters_by_ft = {
lua = { "stylua" },
fish = { "fish_indent" },
sh = { "shfmt" },
},
-- LazyVim will merge the options you set here with builtin formatters.
-- You can also define any custom formatters here.
---@type table<string,table>
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,
-- },
--
-- # Example of using shfmt with extra args
-- shfmt = {
-- extra_args = { "-i", "2", "-ci" },
-- },
},
}
end,
config = M.setup,
},
}

0 comments on commit b6e68fa

Please sign in to comment.