stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.25k stars 166 forks source link

Quetsion: How to customize a formatter that has '-' in the name like "goimports-reviser" #444

Closed melmustafa closed 5 months ago

melmustafa commented 5 months ago

I was settings up my formatting workflow and added "goimports-reviser" to be my imports formatter. I wanted the "-rm-unused" flag so that it deleted the unused packages. I tried different ways and it won't work. Here is my conform setup { 'stevearc/conform.nvim', lazy = false, opts = { notify_on_error = false, format_on_save = function(bufnr) local disable_filetypes = { c = true, cpp = true } return { lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype], } end, formatters_by_ft = { go = { 'goimports-reviser', 'gofumpt', 'golines' }, }, formatters = { goimports_reviser = { prepend_args = { '-rm-unused' }, }, golines = { prepend_args = { '--max-len=80' }, }, }, }, }

stevearc commented 5 months ago

I would recommend that you familiarize yourself with the syntax of lua tables http://lua-users.org/wiki/TablesTutorial The way to insert a literal string as the key of a table is to wrap it with [""]

{
  "stevearc/conform.nvim",
  lazy = false,
  opts = {
    notify_on_error = false,
    format_on_save = function(bufnr)
      local disable_filetypes = { c = true, cpp = true }
      return { lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype] }
    end,
    formatters_by_ft = {
      go = { "goimports-reviser", "gofumpt", "golines" },
    },
    formatters = {
      ["goimports-reviser"] = { prepend_args = { "-rm-unused" } },
      golines = { prepend_args = { "--max-len=80" } },
    },
  },
}