return { "stevearc/conform.nvim", event = { "BufWritePre" }, cmd = { "ConformInfo" }, opts = { -- See available formatters in: https://github.com/stevearc/conform.nvim#formatters -- Formatters can be installed by mason formatters_by_ft = { -- Conform will run multiple formatters sequentially -- Use a stop_after_first = true to run only the first available formatters -- Use the "_" filetype to run formatters on filetypes that don't -- have other formatters configured. ["_"] = { "trim_whitespace" }, blade = { "blade-formatter" }, css = { "biome" }, go = { "gofumpt", "goimports_reviser", "golines" }, html = { "djlint", "prettierd", stop_after_first = true }, kotlin = { "ktlint" }, kdl = { "kdlfmt" }, htmldjango = { "djlint", stop_after_first = true }, javascript = { "biome" }, javascriptreact = { "biome" }, json = { "biome" }, jsonc = { "biome" }, lua = { "stylua" }, markdown = { "markdownlint" }, php = { "pint" }, python = { "ruff_format", "ruff_organize_imports" }, rust = { "rustfmt" }, scss = { "biome", "prettierd", "prettier", stop_after_first = true }, sh = { "shfmt" }, sql = { "sleek" }, toml = { "taplo" }, typescript = { "biome" }, typescriptreact = { "biome" }, xml = { "lemminx" }, zsh = { "shfmt" }, }, formatters = { djlint = { prepend_args = { "--format-css", "--indent-css", "2", "--format-js", "--indent-js", "2", "--indent", "2", "--preserve-blank-lines", "--quiet", }, }, }, 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 { timeout_ms = 2000, lsp_fallback = true } end, }, config = function(_, opts) require("conform").setup(opts) vim.api.nvim_create_autocmd("FileType", { pattern = { "php" }, callback = function(bufnr) vim.b[bufnr].disable_autoformat = true end, }) local function toggleAutoFormat() -- to make this global, change b to g if vim.b.disable_autoformat == nil then vim.b.disable_autoformat = true print("Autoformat set to: " .. tostring(not vim.b.disable_autoformat)) return end vim.b.disable_autoformat = not vim.b.disable_autoformat print("Autoformat set to: " .. tostring(not vim.b.disable_autoformat)) end vim.keymap.set("n", "uf", toggleAutoFormat, { desc = "Toggle auto format", silent = true }) vim.api.nvim_create_user_command("Fmt", function(args) local range = nil if args.count ~= -1 then local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1] range = { start = { args.line1, 0 }, ["end"] = { args.line2, end_line:len() }, } end local function callback(err, did_edit) if not did_edit then vim.notify("The file was not formatted:\n" .. tostring(err), vim.log.levels.ERROR) return end if args.bang then vim.cmd("w") end end require("conform").format({ async = true, lsp_format = "fallback", range = range, formatters = args.fargs, }, callback) end, { range = true, bang = true, force = true, desc = "Format the document", nargs = "*", -- complete = function() -- local formatters = require('conform').formatters_by_ft -- -- return vim.tbl_keys(formatters) -- end }) end, }