--[ -- Formatter plugin for Neovim. --] return { "stevearc/conform.nvim", enabled = true, event = { "BufReadPre", "BufNewFile" }, opts = { formatters_by_ft = { javascript = { "prettier" }, typescript = { "prettier" }, svelte = { "prettier" }, css = { "prettier" }, html = { "prettier" }, json = { "prettier" }, yaml = { "prettier" }, markdown = { "prettier" }, lua = { "stylua" }, fish = { "fish_indent" }, sh = { "shfmt" }, python = function(bufnr) if require("conform").get_formatter_info("ruff_format", bufnr).available then return { "ruff_format" } else return { "isort", "black" } end end, }, format_on_save = function(bufnr) -- Disable with a global or buffer-local variable if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then return end return { lsp_fallback = true, async = false, tiemout_ms = 1000, } end, -- Conform will notify you when no formatters are available for the buffer notify_on_error = true, }, config = function(_, opts) local conform = require("conform") conform.setup(opts) vim.api.nvim_create_user_command("FormatDisable", function(args) if args.bang then -- FormatDisable! will disable formatting just for this buffer vim.b.disable_autoformat = true else vim.g.disable_autoformat = true end end, { desc = "Disable autoformat-on-save", bang = true, }) vim.api.nvim_create_user_command("FormatEnable", function() vim.b.disable_autoformat = false vim.g.disable_autoformat = false end, { desc = "Re-enable autoformat-on-save", }) vim.api.nvim_create_user_command("FormatToggle", function() if vim.b.disable_autoformat then vim.b.disable_autoformat = false vim.g.disable_autoformat = false else vim.b.disable_autoformat = true vim.g.disable_autoformat = true end end, { desc = "Toggle autoformat-on-save", }) vim.keymap.set({ "n", "v" }, "mp", function() conform.format({ lsp_fallback = true, async = false, tiemout_ms = 1000, }) end, { desc = "Format file or range (in visual mode)" }) vim.keymap.set({ "n", "v" }, "uf", "FormatToggle", { desc = "Toggle autoformat-on-save" }) end, }