marleyvim/nvim/lua/plugins/formatting/conform-nvim.lua
2025-01-04 21:36:09 -08:00

96 lines
2.6 KiB
Lua

return {
'conform.nvim',
event = { 'BufWritePre' },
cmd = { 'ConformInfo' },
keys = {
{
'<LEADER>cf',
function()
require('conform').format()
end,
desc = 'format',
},
{
'<LEADER>cF',
function()
require('conform').format({
formatters = { 'injected' },
timeout_ms = 3000,
})
end,
mode = { 'n', 'v' },
desc = 'format injected langs',
},
},
after = function()
require('conform').setup({
format_on_save = function(buf)
if (vim.g.autoformat == false) or (vim.b[buf].autoformat == false) then
return
end
return { timeout_ms = 3000, lsp_format = 'fallback' }
end,
-- Prettier is last but only activated if there is a config available,
-- thereby giving it priority only when it is wanted.
formatters_by_ft = {
blade = { 'blade-formatter', 'rustywind' },
css = { 'stylelint', 'biome', 'prettier' },
fish = { 'fish_indent' },
graphql = { 'biome', 'prettier' },
handlebars = { 'prettier' },
html = { 'prettier' },
javascript = { 'biome', 'prettier' },
javascriptreact = { 'biome', 'prettier' },
json = { 'biome', 'prettier' },
jsonc = { 'biome', 'prettier' },
liquid = { 'prettier' },
lua = { 'stylua' },
less = { 'prettier' },
markdown = { 'prettier' },
['markdown.mdx'] = { 'prettier' },
nix = { 'alejandra' },
php = { 'pint', 'php_cs_fixer', stop_after_first = true },
ruby = { 'prettier' },
scss = { 'stylelint', 'prettier' },
sh = { 'shfmt' },
toml = { 'taplo' },
typescript = { 'biome', 'prettier' },
typescriptreact = { 'biome', 'prettier' },
vue = { 'biome', 'prettier' },
yaml = { 'prettier' },
},
formatters = {
biome = {
require_cwd = true,
},
pint = {
meta = { url = 'https://github.com/laravel/pint' },
command = require('conform.util').find_executable({
'vendor/bin/pint',
}, 'pint'),
args = { '$FILENAME' },
stdin = false,
},
prettier = {
condition = function(_, ctx)
return MarleyVim.prettier.has_parser(ctx)
and MarleyVim.prettier.has_config(ctx)
end,
},
shfmt = {
prepend_args = {
'--indent=2',
'--case-indent',
'--binary-next-line',
'--func-next-line',
},
},
},
})
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
end,
}