Formatting
LazyVim uses conform.nvim
for formatting.
Configuring conform.nvim
:
opts.format
: extra options passed torequire("conform").format(options)
opts.formatters
: options will be merged with builtin formatters, or you can specify a new formatter.opts.formatters[NAME].prepend_args
: extra arguments passed to the formatter command. If you want to fully override theargs
, just useargs
instead ofprepend_args
.opts.formatters_by_ft
: specify which formatters to use for each filetype.
caution
Don't override plugin.config
directly, since this will break LazyVim formatting.
conform.nvim
- Options
- Full Spec
opts = function()
local plugin = require("lazy.core.config").plugins["conform.nvim"]
if plugin.config ~= M.setup then
LazyVim.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
---@type conform.setupOpts
local opts = {
default_format_opts = {
timeout_ms = 3000,
async = false, -- not recommended to change
quiet = false, -- not recommended to change
lsp_format = "fallback", -- not recommended to change
},
formatters_by_ft = {
lua = { "stylua" },
fish = { "fish_indent" },
sh = { "shfmt" },
},
-- The options you set here will be merged with the builtin formatters.
-- You can also define any custom formatters here.
---@type table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride>
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 = {
-- prepend_args = { "-i", "2", "-ci" },
-- },
},
}
return opts
end
{
"stevearc/conform.nvim",
dependencies = { "mason.nvim" },
lazy = true,
cmd = "ConformInfo",
keys = {
{
"<leader>cF",
function()
require("conform").format({ formatters = { "injected" }, timeout_ms = 3000 })
end,
mode = { "n", "v" },
desc = "Format Injected Langs",
},
},
init = function()
-- Install the conform formatter on VeryLazy
LazyVim.on_very_lazy(function()
LazyVim.format.register({
name = "conform.nvim",
priority = 100,
primary = true,
format = function(buf)
require("conform").format({ bufnr = buf })
end,
sources = function(buf)
local ret = require("conform").list_formatters(buf)
---@param v conform.FormatterInfo
return vim.tbl_map(function(v)
return v.name
end, ret)
end,
})
end)
end,
opts = function()
local plugin = require("lazy.core.config").plugins["conform.nvim"]
if plugin.config ~= M.setup then
LazyVim.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
---@type conform.setupOpts
local opts = {
default_format_opts = {
timeout_ms = 3000,
async = false, -- not recommended to change
quiet = false, -- not recommended to change
lsp_format = "fallback", -- not recommended to change
},
formatters_by_ft = {
lua = { "stylua" },
fish = { "fish_indent" },
sh = { "shfmt" },
},
-- The options you set here will be merged with the builtin formatters.
-- You can also define any custom formatters here.
---@type table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride>
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 = {
-- prepend_args = { "-i", "2", "-ci" },
-- },
},
}
return opts
end,
config = M.setup,
}
mason.nvim
- Options
- Full Spec
opts = nil
{ "mason.nvim" }