stevearc / conform.nvim

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

How to override default configuration for formatters? #403

Closed KikeSenpai closed 4 months ago

KikeSenpai commented 4 months ago

it's not a bug report, it's a question

Conform help don't say anything about overriding the default configuration or rules of any formatter. How can this be achieved? Does conform pickup config files inside a project? is there a way to add global configuration to where the formatters are installed? I am using Mason to install the formatters.

C0deBr3ak3r commented 4 months ago

https://github.com/stevearc/conform.nvim?tab=readme-ov-file#customizing-formatters tells what to do, you basically just need to set inherit = false in the formatter to override the config and write your custom options. Here is an example:

require('conform').formatters.taplo = {
    inherit = false,
    command = 'echo',
    args = { 'hello', 'world' },
}
stevearc commented 4 months ago

@C0deBr3ak3r is correct, the formatters table is the correct place to override. Note that setting inherit = false means that we will use the exact configuration you supply. If you instead want to merge your configuration with the built-in config, set inherit = true or just leave it out altogether (true is the default).

farzadmf commented 1 month ago

Hey guys, question about this:

I have my formatters_by_ft explicitly set up to use formatters. But seems like taplo as an LSP also does formatting (even if I don't have toml = { 'taplo' } in my formatters_by_ft, my buffer is still auto-formatted).

Now, if I do add toml = { 'taplo' } in my conform config, I don't seem to be able to configure it properly.

Let's say I want to pass --option align_entries=true; I tried doing this:

formatters_by_ft = {
  toml = { 'taplo' },
},
formatters = {
  taplo = {
    prepend_args = { '--option', 'align_entries=true' }
  }
}

This complains saying Found argument '--option' which wasn't expected, which makes sense looking at taplo.lua in the formatters.

So, I tried this:

formatters = {
  taplo = {
    args = { 'format', '--option', 'align_entries=true' },
  }
}

And, with this, whenever I save, I get the message: WARNING: The file has been changed since reading it

Next, I tried to completely override it (needed to install the taplo binary separately):

formatters = {
  taplo = {
    inherit = false,
    command = 'taplo',
    args = { 'format', '--option', 'align_entries=true' },
  }
}

With this also I'm getting the "file has been changed" warning message.

Any help is appreciated. Thank you!

stevearc commented 1 month ago

My guess is you need

args = { 'format', '--option', 'align_entries=true', '-' },

As that final - in the arguments tells the command to use stdin/stdout for formatting. In general, I will direct you to the docs on debugging which should be helpful for figuring out what could be going wrong.

farzadmf commented 1 month ago

Thank you so much @stevearc for the quick answer; as you pointed out, the missing part was the - arg at the end.

I did some troubleshooting using ConformInfo (that's where I got those error messages), but didn't know about that debugging doc; will definitely take a look at that!