-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Command to disable formatting #192
Comments
I don't think this makes sense to add to the core plugin, but it's easy enough to add this logic yourself! https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#command-to-toggle-format-on-save |
Wanting to write a buffer without formatting is a valid use-case. Specially when you're working on a code base in a team and you don't want your formatter to change the entire file because a line change you had to make. |
Here is my auto-format toggle config:
return { -- Autoformat
'stevearc/conform.nvim',
lazy = false,
keys = {
{
'<leader>tf',
function()
-- If autoformat is currently disabled for this buffer,
-- then enable it, otherwise disable it
if vim.b.disable_autoformat then
vim.cmd 'FormatEnable'
vim.notify 'Enabled autoformat for current buffer'
else
vim.cmd 'FormatDisable!'
vim.notify 'Disabled autoformat for current buffer'
end
end,
desc = 'Toggle autoformat for current buffer',
},
{
'<leader>tF',
function()
-- If autoformat is currently disabled globally,
-- then enable it globally, otherwise disable it globally
if vim.g.disable_autoformat then
vim.cmd 'FormatEnable'
vim.notify 'Enabled autoformat globally'
else
vim.cmd 'FormatDisable'
vim.notify 'Disabled autoformat globally'
end
end,
desc = 'Toggle autoformat globally',
},
},
opts = {
notify_on_error = false,
format_on_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
local disable_filetypes = { c = false, cpp = false }
return {
timeout_ms = 500,
lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
}
end,
formatters_by_ft = {
lua = { 'stylua' },
python = { 'black' },
javascript = { { 'prettierd', 'prettier' } },
typescript = { { 'prettierd', 'prettier' } },
typescriptreact = { { 'prettierd', 'prettier' } },
javascriptreact = { { 'prettierd', 'prettier' } },
css = { { 'prettierd', 'prettier' } },
cs = { 'csharpier' },
},
},
config = function(_, opts)
require('conform').setup(opts)
vim.api.nvim_create_user_command('FormatDisable', function(args)
if args.bang then
-- :FormatDisable! disables autoformat for this buffer only
vim.b.disable_autoformat = true
else
-- :FormatDisable disables autoformat globally
vim.g.disable_autoformat = true
end
end, {
desc = 'Disable autoformat-on-save',
bang = true, -- allows the ! variant
})
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',
})
end,
}
|
Hello @stevearc and all maintainers, thanks for the amazing work, not only on conform.nvim but also on oil.nvim which I heavily use them.
Would it be possible to add a command to conform to disable the format on save? I used that a lot with null-ls, sometimes my formatting rules are different than the repo rules and sometimes I get a diff on git logs just for formatting changes and what I was doing in this cases on null-ls were disabling the formatting, save the file and then enabling the formatting again.
Would it be possible to do with conform as well?
Thanks in advance :D
The text was updated successfully, but these errors were encountered: