restrict conform.nvim format on save to only specific filetypes

This commit is contained in:
Fábio André Damas 2025-08-26 10:16:22 +01:00
parent 8f912a52df
commit e4d067b26d
2 changed files with 23 additions and 39 deletions

View file

@ -4,16 +4,29 @@ return {
formatters_by_ft = {
javascript = { "prettierd", "prettier", stop_after_first = true },
kulala = { "kulala-fmt" },
lua = { lsp_format = "fallback" },
go = { lsp_format = "fallback" },
rust = { lsp_format = "fallback" },
toml = { lsp_format = "fallback" },
yaml = { lsp_format = "fallback" },
java = { lsp_format = "fallback" }
},
format_on_save = {
timeout_ms = 500,
lsp_format = "fallback",
},
},
init = function()
local FILETYPES_TO_FORMAT_ON_SAVE = {
"javascript", "javascriptreact", "typescript", "typescriptreact",
"kulala", "lua", "go", "rust", "toml", "json", "yaml", "java" }
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
callback = function(args)
local ft = vim.bo[args.buf].filetype
for _, t in ipairs(FILETYPES_TO_FORMAT_ON_SAVE) do
if ft == t then
require("conform").format({
bufnr = args.buf,
timeout_ms = 500,
lsp_format = "fallback",
})
return
end
end
end,
})
end
}

View file

@ -1,29 +1,3 @@
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
local format_on_save = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({
group = augroup,
buffer = bufnr,
})
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
-- prevents format from being included in the undo history
-- this way the cursor doesn't jump around the file when
-- undoing
vim.cmd("silent! undojoin")
vim.lsp.buf.format({
bufnr = bufnr,
async = false,
})
end,
})
end
end
vim.diagnostic.config({
virtual_lines = false,
virtual_text = false,
@ -91,9 +65,6 @@ return {
local null_ls = require("null-ls")
null_ls.setup({
on_attach = function(client, bufnr)
format_on_save(client, bufnr)
end,
sources = {
null_ls.builtins.diagnostics.dotenv_linter,
null_ls.builtins.diagnostics.editorconfig_checker,