Skip to content
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

Closed
BitInByte opened this issue Nov 10, 2023 · 3 comments
Closed

feat: Command to disable formatting #192

BitInByte opened this issue Nov 10, 2023 · 3 comments

Comments

@BitInByte
Copy link

BitInByte commented Nov 10, 2023

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

@BitInByte BitInByte changed the title feat: feat: Command to disable formatting Nov 10, 2023
@stevearc
Copy link
Owner

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

@andradei
Copy link

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.

@breakingram
Copy link

Here is my auto-format toggle config:

<leader>tf - will toggle per‑buffer autoformat
<leader>tF - will toggle global autoformat

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,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants