Files
nvim/lua/plugins/format.lua
T
2026-11-12 23:58:22 +01:00

75 lines
2.2 KiB
Lua

-- mago reads a mago.{toml,yaml,yml,json} found above the file.
local function find_mago_config(filename)
return vim.fs.find(function(name)
local ext = name:match("%.([^.]+)$")
return name:lower():find("mago", 1, true) ~= nil and vim.tbl_contains({ "toml", "yaml", "yml", "json" }, ext)
end, { path = filename, upward = true })[1]
end
return {
{
"stevearc/conform.nvim",
cond = vim.g.profile == "full",
event = "BufWritePre",
cmd = "ConformInfo",
keys = {
{
"<leader>lf",
function()
require("conform").format({ async = true, lsp_format = "fallback" })
end,
desc = "Format buffer",
},
{
"<leader>tF",
function()
vim.b.disable_autoformat = not vim.b.disable_autoformat
vim.notify("format on save: " .. (vim.b.disable_autoformat and "off" or "on"))
end,
desc = "Toggle format on save (buffer)",
},
},
opts = {
formatters_by_ft = {
nix = { "nixfmt" },
lua = { "stylua" },
sh = { "shfmt" },
bash = { "shfmt" },
yaml = { "prettierd" },
json = { "jq" },
jsonc = { "prettierd" },
markdown = { "prettierd" },
rust = { "rustfmt" },
go = { "goimports", "gofumpt" },
javascript = { "prettierd" },
javascriptreact = { "prettierd" },
typescript = { "prettierd" },
typescriptreact = { "prettierd" },
python = { "ruff_format" },
php = { "mago" },
},
formatters = {
mago = {
command = "mago",
args = function(_, ctx)
local config = find_mago_config(ctx.filename)
local args = { "format", "--stdin-input", "--stdin-filepath", "$FILENAME" }
if config then
table.insert(args, 1, config)
table.insert(args, 1, "--config")
end
return args
end,
stdin = true,
},
},
format_on_save = function(buf)
if vim.b[buf].disable_autoformat or vim.b[buf].bigfile then
return
end
return { timeout_ms = 1000, lsp_format = "fallback" }
end,
},
},
}