You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nvim-config/lua/plugins/conform.lua

58 lines
1.8 KiB

vim.pack.add({ "https://github.com/stevearc/conform.nvim" })
require("conform").setup({
formatters_by_ft = {
lua = { "stylua", lsp_format = "fallback" },
python = { "isort", "black" },
rust = { "rustywind", "rustfmt", lsp_format = "fallback" },
javascript = { "rustywind", "prettier" },
json = { "prettier" },
markdown = { "prettier" },
typescript = { "rustywind", "prettier" },
vue = { "rustywind", "prettier" },
css = { "prettier" },
html = { "rustywind", "prettier" },
toml = { "taplo" },
},
default_format_opts = {
lsp_format = "fallback",
},
format_on_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname:match("/node_modules/") then
return
end
return { timeout_ms = 500, lsp_format = "fallback" }
end,
})
require("conform").formatters.black = {
append_args = { "--line-length=79" },
}
-- vim.api.nvim_create_autocmd("BufWritePre", {
-- pattern = "*",
-- callback = function(args, a)
-- require("conform").format({ bufnr = args.buf })
-- end,
-- })
vim.api.nvim_create_user_command("FormatDisable", function(opts)
if opts.bang then
vim.b.disable_autoformat = true
else
vim.g.disable_autoformat = true
end
vim.notify("Autoformat disabled" .. (opts.bang and " (buffer)" or " (global)"), vim.log.levels.WARN)
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
vim.notify("Autoformat enabled", vim.log.levels.INFO)
end, { desc = "Re-enable autoformat-on-save" })