dotfiles/nvim/.config/nvim/lua/plugins/formatting.lua
Marty Oehme 8b5e4dc097
nvim: Do not always run ruff_fix
Ruff fix removes unused imports etc. which is sometimes more annoying
than helpful.
2025-02-01 10:46:22 +01:00

104 lines
2.5 KiB
Lua

local formatters = {
angular = { "prettier" },
astro = { "prettier" },
bash = { "shfmt" },
bib = { "bibtex-tidy" },
css = { "prettier", "rustywind" },
go = { "gofumpt" },
graphql = { "prettier" },
html = { "prettier", "rustywind" },
javascript = { "prettier" },
javascriptreact = { "prettier" },
json = { "jq" },
liquid = { "prettier" },
lua = { "stylua" },
markdown = { "prettier", "injected" },
nim = { "nimpretty" },
python = { "ruff_format", "ruff_organize_imports" },
quarto = { "prettier", "injected" },
sh = { "shfmt" },
sql = { "sleek" },
svelte = { "prettier" },
typescript = { "prettier" },
typescriptreact = { "prettier" },
vue = { "prettier", "rustywind" },
yaml = { "prettier" },
zsh = { "shfmt" },
}
return {
-- formatting setup
{
"zapling/mason-conform.nvim",
dependencies = {
{
"stevearc/conform.nvim",
config = function()
require("conform").setup({
formatters_by_ft = formatters,
lsp_format = "fallback",
format_after_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { lsp_fallback = true }
end,
})
require("conform").formatters.prettier = {
options = {
ext_parsers = {
qmd = "markdown",
},
},
}
vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then
vim.g.disable_autoformat = true
else
-- FormatDisable! will disable formatting globally
vim.b.disable_autoformat = true
end
end, {
desc = "Disable formatting on save",
bang = true,
})
vim.api.nvim_create_user_command("FormatEnable", function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = "Enable formatting on save",
})
end,
cmd = { "ConformInfo" },
keys = {
{
"<localleader>ll",
function()
require("conform").format({ async = true, lsp_fallback = true })
end,
mode = { "n", "v" },
desc = "Format buffer",
},
{
"<localleader>lL",
function()
vim.g.disable_autoformat = not vim.g.disable_autoformat
end,
desc = "Toggle AutoFormat",
},
{
"<leader>vf",
":ConformInfo<cr>",
desc = "ConformInfo",
},
},
init = function()
-- If you want the formatexpr, here is the place to set it
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
end,
},
},
event = { "BufWritePre" },
opts = {},
},
}