fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
773 stars 194 forks source link

Consider documenting why certain config options exist #2979

Open brianrourkeboll opened 10 months ago

brianrourkeboll commented 10 months ago

I think it might be worth augmenting the existing documentation (e.g., here) with some of the following information, where appropriate:

  1. Why various configuration options exist.
  2. Why the user might choose one setting for a given option over another (there is some of this for, e.g., fsharp_multiline_bracket_style = stroustrup).
  3. Whether there are groups of related options that it might make sense to set consistently with each other, like is already done for G-Research settings—but also why you might do that.

I could see it argued that someone could just document such information in their own style guide instead, but there are some settings that are currently supported for which the existing style guides offer rules but not explanations, and there are some widespread styles for which there is no official, published guide.

I think adding this explanatory information to the Fantomas docs would be especially useful for people coming to F# from C-family languages. In my own experience, I have seen many newcomers perplexed as to why one might use or omit a space in various contexts, for example. Discrepancies between the tooling in Ionide, Rider, and Visual Studio, and the changes to the default formatting used by the compiler (and in documentation) have also contributed to the confusion, especially when newcomers start working in big, long-lived codebases, where significant amounts of old code were written before some of the more recent formatting styles became popular in F#.

Examples

These are some (surely incomplete) examples of the kinds of implicit motivation/historical background for some settings and grouping of settings that it might be worth documenting. They mostly represent my own poorly-phrased understanding or reasoning, so I may be wrong about some things. And there are of course surely other rationales and histories out there for many of the settings, some of which may or may not be worth mentioning. Either way, there is probably someone out there better versed in the lore here than I am—maybe there are blog posts or other writing or documentation that we could consult.

ML style

This seemed like one of the most common styles when I started learning F# a number of years ago, including in the compiler and in the VS tooling.

I think it was mostly inherited from OCaml:

fsharp_multiline_bracket_style = compact
fsharp_space_before_parameter = true
fsharp_space_before_lowercase_invocation = true
fsharp_space_before_uppercase_invocation = true
fsharp_space_before_member = true
fsharp_space_before_class_constructor = true
fsharp_space_before_colon = true

Interop/".NET"/C#/TypeScript style

fsharp_multiline_bracket_style = stroustrup
fsharp_space_before_parameter = false
fsharp_space_before_lowercase_invocation = false
fsharp_space_before_uppercase_invocation = false
fsharp_space_before_class_constructor = false
fsharp_space_before_member = false
fsharp_space_before_colon = false

Haskell style

This is probably common in OCaml and many other languages, too.

// ML style, plus these:
fsharp_space_before_semicolon = true
fsharp_space_around_delimiter = true

Diff minimization

fsharp_multiline_bracket_style = aligned
fsharp_multi_line_lambda_closing_newline = true
fsharp_bar_before_discriminated_union_declaration = true
fsharp_space_before_parameter = false
fsharp_space_before_lowercase_invocation = false
fsharp_space_before_uppercase_invocation = false
fsharp_space_before_class_constructor = false
fsharp_space_before_member = false

I'd be willing to open a PR for this.

dawedawe commented 10 months ago

Hey @brianrourkeboll , as we aren't super sure about this one yet and we don't want you to waste your time, maybe we should have a call before you get started on a potential PR. How about next week? Are you on discord or slack?

brianrourkeboll commented 10 months ago

Sure, no problem. I am on the F# Discord.

I'm also open to the possibility that the Fantomas docs aren't the best place for something like this, although it does seem like something that would be useful to have in some discoverable, well-known place.

dawedawe commented 10 months ago

Great, we'll get in touch with you.

josh-degraw commented 10 months ago

I think this would be super useful as a blog post at the very least. I actually especially like the examples with "recipes" of common styles in that context.

I also do think it's worth mentioning that having "recipes" like this technically contradicts the official recommendation in the docs to just stick with the defaults, although I personally think we may want to update at least part of that section:

To strengthen the message of unity we advice that you do not change the default settings. The out-of-the-box experience should be a result of what the brightest minds of the community came up with. If you are new to the F# language, this is what you want.

For example, there are now 3 officially recommended ways to format record types, so I don't know if it's accurate to say that the default settings are always the "best".

Regardless, ihmo having some minimal explanation as to why a given setting exists can certainly be useful even without being phrased as a recommendation.