From 5e3db5028e75a191bf9253548546c961579123b1 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Sun, 23 Feb 2025 23:59:03 +0100 Subject: [PATCH] disable autoformat with command --- init.lua | 26 ++++++++++++++++++++++++++ lua/custom/plugins/init.lua | 3 +++ 2 files changed, 29 insertions(+) diff --git a/init.lua b/init.lua index 9bd192a..5ff3b93 100644 --- a/init.lua +++ b/init.lua @@ -145,6 +145,32 @@ vim.opt.spellsuggest = 'double,50,timeout:5000' vim.api.nvim_create_user_command('Env', function(opts) vim.cmd(string.format('r!echo $%s', opts.fargs[1])) end, { nargs = 1 }) +vim.api.nvim_create_user_command('FormatDisable', function(args) + if args.bang then + -- FormatDisable! will disable formatting globally + print 'disable autoformat globally' + vim.g.disable_autoformat = true + else + print 'disable autoformat locally' + vim.b.disable_autoformat = true + end +end, { + desc = 'Disable autoformat', + bang = true, +}) +vim.api.nvim_create_user_command('FormatEnable', function(args) + if args.bang then + -- FormatDisable! will enable formatting globally + print 'enable autoformat globally' + vim.g.disable_autoformat = false + else + print 'enable autoformat locally' + vim.b.disable_autoformat = false + end +end, { + desc = 'Enable autoformat', + bang = true, +}) -- autocommands -- === diff --git a/lua/custom/plugins/init.lua b/lua/custom/plugins/init.lua index 695d5b3..78051ed 100644 --- a/lua/custom/plugins/init.lua +++ b/lua/custom/plugins/init.lua @@ -22,6 +22,9 @@ return { opts = { notify_on_error = false, format_on_save = function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end -- Disable "format_on_save lsp_fallback" for languages that don't -- have a well standardized coding style. You can add additional -- languages here or re-enable it for the disabled ones.