stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.01k stars 156 forks source link

question: black formatter line length #448

Closed AnshUjlayan closed 3 months ago

AnshUjlayan commented 3 months ago

I can’t specify the line length in for my black formatter. isort and black both are working, but i don’t know how to set blacks arguments to format to 79 chars instead of 88.

return {
  "stevearc/conform.nvim",
  event = { "BufReadPre", "BufNewFile" },
  config = function()
    local conform = require("conform")

    conform.setup({
      formatters_by_ft = {
        javascript = { "prettier" },
        typescript = { "prettier" },
        javascriptreact = { "prettier" },
        typescriptreact = { "prettier" },
        svelte = { "prettier" },
        css = { "prettier" },
        html = { "prettier" },
        json = { "prettier" },
        yaml = { "prettier" },
        markdown = { "prettier" },
        graphql = { "prettier" },
        liquid = { "prettier" },
        lua = { "stylua" },
        python = { { "isort" }, { "black", args = { "--line-length", "79" } } },
        cpp = { "clang-format" },
      },
      format_on_save = {
        lsp_fallback = true,
        async = false,
        timeout_ms = 1000,
      },
    })

    vim.keymap.set({ "n", "v" }, "<leader>mp", function()
      conform.format({
        lsp_fallback = true,
        async = false,
        timeout_ms = 1000,
      })
    end, { desc = "Format file or range (in visual mode)" })
  end,
}

source code for reference. https://github.com/AnshUjlayan/nvim

stevearc commented 3 months ago

Typically you would want to set this on a per-project basis using a config file, e.g. putting the options in pyproject.toml.

If you instead want to configure it globally, you can pass in the --line-length argument to the formatter. I have no idea where you got the syntax you are using, but that is not the correct way to customize the arguments for a formatter. You will want:

conform.setup({
  formatters_by_ft = {
    python = { "isort", "black" },
  },
  formatters = {
    black = {
      prepend_args = { "--line-length", "79" },
    }
  },
})
AnshUjlayan commented 3 months ago

It worked! Thank you so much :)