disable formatter for php

This commit is contained in:
Alexander Navarro 2025-09-10 15:02:59 -03:00
parent 4458a8755e
commit 158e492332

View file

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