tweag/topiary

Support language-specific user configuration

Closed this issue · 3 comments

Is your feature request related to a problem? Please describe.

Different users might have different style preferences. For example, rustfmt lets the user specify their preferences in a rustfmt.toml file.

Preferences like comment width, layout of imports, etc. should be configurable to the user.

Describe the solution you'd like

Each language should specify which configuration options are available for that language, perhaps in languages.toml.

I see that indent is a configuration option that can be specified in languages.toml, however that wouldn't be dynamically configurable. What I have in mind could be something like this:

# languages.toml

[[language]]
name = "bash"
extensions = ["sh", "bash"]

# ... other languages

[[language]]
name = "rust"
extensions = ["rs"]

[language.config.indent] # A subtable for each configurable option
default = "    " # The default value for the option for rust, in this case 4 spaces
accept = [ " ", "  ", "    ", "    " ] # The possible values for the option. This indicates that it accepts 1, 2, or 4 spaces, or tab

User defined config:

# Topiary.toml - this would be a user-defined file that could be checked into a project that uses topiary as its formatter.

[[rust]]
indent = "  " # The user overrides the default indentation to be 2 spaces instead of 4.

Additionally, there would need to be a way to check the configuration options in the topiary query file, since the queries would look different depending on the config.

The main thing that would be needed is support for conditionals in topiary queries.

Describe alternatives you've considered

This could all be done in languages.toml using the -c option, however it wouldn't be language-specific. There are many user preferences that would make sense for some languages but not for others.

Without a way to do conditionals in the topiary scm, the types of user style preferences we can support are limited.

Additional context

I looked through the existing issues and didn't find anything that would allow the user to configure the formatter in this way.

Hej!

I'm not sure what you mean with languages.toml not being language specific or not being dynamically configurable, but I will do my best the answer the question regardless. Let me know if I'm on the right track.

Here is the applicable section of the README that describes topiary configuration: https://github.com/tweag/topiary#configuration Notably, topiary checks 3 locations for the configuration file, with later ones overriding the earlier ones. Assuming you are on a Linux distribution:

  1. The builtin configuration file
  2. The user configuration file in the OS's configuration directory so ~/.config/topiary/languages.toml
  3. The project specific topiary configuration so .topiary/languages.toml which is searches for going up in the directory structure.

The languages.toml file is in fact language-specific. Topiary defaults to 2 spaces for indentation, and so every [[language]] block that doesn't mention indentation uses 2 spaces. Say you want to use a tab for json indentation instead of the default 2 spaces. You can create a file in ~/.config/topiary/languages.toml with the content:

[[language]]
name = "json"
indent = "\t"

Let me know if this addresses your concerns.

What I mean by language-specific is the options themselves. Currently, every language can override indent, right?

There are many formatter options that would only make sense in a certain language. For instance, merge_derives only makes sense for rust, array-callback-return only makes sense in JS/TS, etc.

If you set the indent in a language block, the indentation is only changed for that language. So above, only the indentation in json files would be changed.

Right now, there are no plans to include options for specific language due to the limited resources we have. For every such option we will likely look at the current most agreed upon (with some bias ofc) opinion among the members of that language's ecosystem, and apply that opinion.

As such, Topiary is more in spirit with ormolu than, say, rustfmt or clang-format.