diff --git a/nvim/.config/nvim/lua/lazy-plugins/lsp-conform.lua b/nvim/.config/nvim/lua/lazy-plugins/lsp-conform.lua index 2663246..c7d13c9 100644 --- a/nvim/.config/nvim/lua/lazy-plugins/lsp-conform.lua +++ b/nvim/.config/nvim/lua/lazy-plugins/lsp-conform.lua @@ -6,7 +6,35 @@ return { conform.setup(opts) - vim.keymap.set({ "n", "v" }, "cp", function() + vim.api.nvim_create_user_command("FormatDisable", function(args) + if args.bang then + vim.b.disable_autoformat = true + else + vim.g.disable_autoformat = true + end + end, { + desc = "Disable autoformat-on-save (! = current buffer only)", + bang = true, + }) + + vim.api.nvim_create_user_command("FormatEnable", function() + vim.b.disable_autoformat = false + vim.g.disable_autoformat = false + end, { desc = "Re-enable autoformat-on-save" }) + + vim.keymap.set("n", "cf", "FormatDisable!", { + desc = "Format: disable autoformat for this buffer", + }) + + vim.keymap.set("n", "cF", "FormatDisable", { + desc = "Format: disable autoformat globally (session)", + }) + + vim.keymap.set("n", "ce", "FormatEnable", { + desc = "Format: re-enable autoformat", + }) + + vim.keymap.set("n", "cw", function() conform.format({ lsp_fallback = true, async = false, @@ -32,5 +60,21 @@ return { typescript = { "prettier" }, yaml = { "prettier" }, }, + format_on_save = function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + local fname = vim.api.nvim_buf_get_name(bufnr) + if fname ~= "" then + local marker = vim.fs.find(".no-autoformat", { + upward = true, + path = vim.fs.dirname(fname), + })[1] + if marker then + return + end + end + return { timeout_ms = 2000, lsp_fallback = true } + end, }, }