fix(conform): use formatters for both custom and overriding formatters

This commit is contained in:
Folke Lemaitre 2023-09-29 15:56:20 +02:00
parent 0e5ff5c0ab
commit 6bb0d1b66f
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -1,10 +1,3 @@
-- When `conform.nvim` is enabled, it will automatically be used as the
-- formatter for all files that it supports.
-- When no conform formatter is available for a filetype, LSP format
-- will be used instead.
--
-- LazyVim adds a custom `formatter_opts` option to allow overriding the
-- default options for each formatter.
return {
{
"stevearc/conform.nvim",
@ -25,9 +18,10 @@ return {
fish = { "fish_indent" },
sh = { "shfmt" },
},
-- LazyVim extension to easily override formatter options
-- LazyVim will merge the options you set here with builtin formatters.
-- You can also define any custom formatters here.
---@type table<string,table>
formatter_opts = {
formatters = {
-- -- Example of using dprint only when a dprint.json file is present
-- dprint = {
-- condition = function(ctx)
@ -38,8 +32,9 @@ return {
},
config = function(_, opts)
opts.formatters = opts.formatters or {}
for f, o in pairs(opts.formatter_opts or {}) do
opts.formatters[f] = vim.tbl_deep_extend("force", require("conform.formatters." .. f), o)
for f, o in pairs(opts.formatters) do
local ok, formatter = pcall(require, "conform.formatters." .. f)
opts.formatters[f] = vim.tbl_deep_extend("force", {}, ok and formatter or {}, o)
end
require("conform").setup(opts)
end,