From ed17b493db2edb522dbb9c62886a428e887253fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89douard=20Thuleau?= Date: Mon, 6 May 2024 15:03:45 +0200 Subject: [PATCH] Add command to toggle autoformatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Édouard Thuleau --- init.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 88658ef3..d32ac6e4 100644 --- a/init.lua +++ b/init.lua @@ -204,6 +204,16 @@ vim.api.nvim_create_autocmd('TextYankPost', { end, }) +-- toggle command to autoformat buffer on save +-- (can be bind to a key) +vim.g.format_is_enabled = true +vim.api.nvim_create_user_command('FormatToggle', function() + vim.g.format_is_enabled = not vim.g.format_is_enabled + print('Setting autoformatting to: ' .. tostring(vim.g.format_is_enabled)) +end, { + desc = 'Toggle autoformatting on save', +}) + -- [[ Install `lazy.nvim` plugin manager ]] -- See `:help lazy.nvim.txt` or https://github.com/folke/lazy.nvim for more info local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim' @@ -647,7 +657,7 @@ require('lazy').setup({ local disable_filetypes = { c = true, cpp = true } return { timeout_ms = 500, - lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype], + lsp_fallback = vim.g.format_is_enabled and not disable_filetypes[vim.bo[bufnr].filetype], } end, formatters_by_ft = {