From b0e951feb9e2b17bce8fa519fd2bf1a013b987b7 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Tue, 8 Oct 2024 01:04:22 +0200 Subject: checkpoint --- after/plugin/conform.lua | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'after/plugin/conform.lua') diff --git a/after/plugin/conform.lua b/after/plugin/conform.lua index 188b499..4560bc0 100644 --- a/after/plugin/conform.lua +++ b/after/plugin/conform.lua @@ -27,14 +27,22 @@ conform.formatters["clang-format"] = { }, } +-- ID of autocmd for CFormat +vim.b.CFormatID = nil -- Enable formatting on save for C only when using the :CFormat command vim.api.nvim_create_user_command("CFormat", function() - vim.api.nvim_create_autocmd("BufWritePre", { - buffer = 0, - callback = function() - conform.format({ formatters = { "clang-format" } }) - end, - }) - conform.format({ formatters = { "clang-format" } }) - vim.cmd.write() + if vim.b.CFormatID == nil then + vim.b.CFormatID = vim.api.nvim_create_autocmd("BufWritePre", { + buffer = 0, + callback = function() + conform.format({ formatters = { "clang-format" } }) + end, + }) + conform.format({ formatters = { "clang-format" } }) + print("Auto formatting enabled.") + else + vim.api.nvim_del_autocmd(vim.b.CFormatID) + print("Auto formatting disabled.") + vim.b.CFormatID = nil + end end, {}) -- cgit v1.2.3