From e4d067b26d21abe8b0004ebab6ef2e29d88d4d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Andr=C3=A9=20Damas?= Date: Tue, 26 Aug 2025 10:16:22 +0100 Subject: [PATCH] restrict conform.nvim format on save to only specific filetypes --- lua/plugins/conform.nvim.lua | 33 +++++++++++++++++++++++---------- lua/plugins/lsp.lua | 29 ----------------------------- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/lua/plugins/conform.nvim.lua b/lua/plugins/conform.nvim.lua index 9254bc6..f1e5561 100644 --- a/lua/plugins/conform.nvim.lua +++ b/lua/plugins/conform.nvim.lua @@ -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 } diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 6e9dbb2..3ca6d0f 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -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,