-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the :trailing_comma
option to the formatter
#7689
Conversation
This commit adds support for the `:trailing_comma` option in `Code.format_string!/2` et al, including in `.formatter.exs`. When set `true`, multi-line list, map, and struct literals will include a trailing comma after the last item or pair in the data structure. Does not affect other data structures, or lists/maps/structs rendered on a single line.
I have been nothing but impressed with the formatter (which I honestly did not originally expect)... Except for this little detail! I love me some trailing commas for all the reasons cited. (Possibly related: I'm also a fan of the Oxford comma!) In short, I think this would be a useful feature to add. |
Hi @gamache! 👋 As you know, the goal of the formatter is exactly to unify the code style used by the community. While I understand this PR is an argument to relax this particular goal, the formatter has been publicly released for roughy 4 months. We need to get much more mileage and feedback before we can perform such changes. Note we had long discussions in the past about formatter customization, such as #6647, and the conclusion there was the same: t is too soon in the formatter adoption cycle to warrant configuration options. I also would like to note that the combination of optional parens and keyword lists make trailing commas slightly inconsistent in Elixir as the following code is ambiguous (and therefore disallowed): some_fun :foo,
key: bar,
baz This was one of the main reasons for disallowing trailing commas in the formatter, as in an attempt to standardize a developer could end-up adding comma to unwarranted places. For all of those reasons, we won't merge this PR. I understand many will disagree with this decision, but we don't plan to add new formatter options at least until we release Elixir v1.8 (Jan/2019) and the formatter gets more mileage. |
Thanks, @josevalim. In that case, I eagerly await the release of 1.7.0 so I can submit this again. 😃 |
Which is exactly why they should not be allowed in parenthesis-less function calls, but should be in multi-line parenthesis function calls, lists, maps, tuples, etc...). |
Does this mean that this can be discussed/considered before the release of v1.8, or after? |
It would be after but I would like to reaffirm my personal position on the formatter in general:
|
So....., Personally, I see no reason to not include this. The toggling reason, @josevalim mentioned, could also be applied to line_length option. There are objectively over weighing good reasons to allow a trailing comma option, such as #6646 (comment) , which shouldn't be dismissed by non productive personal reasoning. |
Not quite the same. If you have line_length set to 100 and then you set to 80, it will surely introduce line breaks in some cases. But if you roll it back to 100, everything will remain the same. Trailing commas change everything every time you toggle it. On the good news, Elixir v1.10 makes public many of the private APIs used by formatter, so now it should be simpler and more stable for third party libraries to provide their own formatting, such as the freedom_formatter from @gamache. |
Well, I expected at least an answer of compromise, like with Thx for the info anyway, probably looking for third party formatters on v1.10 release. :) |
Is it time to revisit this? From the arguments I've seen: Pros:
Cons:
|
Hi! Elixir formatter is a pleasure to work with, except for this one behavior. Could this be revisited, please? Over 4 years has passed since opening this PR and sth like trailing commas are commonly found in virtually every other programming language along with its tooling (linters and/or formatters). Amount of upsides clearly outweigh the few downsides there are. |
@Sija in case it helps: I am maintaining |
one could get very clever with custom sigil macros that treated unescaped unquoted whitespace as an item separator in lists, keyword lists, maps, etc., the way lisps do etc. (and the way sigils like ~w already work!) without needing to alter the formatter's interpretation of commas |
This commit adds support for the
:trailing_comma
option inCode.format_string!/2
et al, including in.formatter.exs
.When set
true
, multi-line list, map, and struct literals will include a trailing comma after the last item or pair in the data structure.Does not affect other data structures, or lists/maps/structs rendered on a single line.
--
The topic of trailing commas has been raised previously by @pragdave in #6646. No resolution was reached; instead, it was considered appropriate to wait for the formatter to have more time in common use before making decisions on which syntax variants to allow. It's now been a little over six months since the last discussion, and in conversations with Elixir developers, the lack of trailing commas continues to be a common source of friction.
Some possible reasons to prefer trailing commas in multi-line data structures are ease of refactoring, minimal diffs when adding or removing elements, and visual symmetry. These reasons are not worth discussing directly in this PR. It should suffice to note that there are advantages and disadvantages to either approach, and that many developers and teams feel strongly about their preference.
This PR's goal is not to dictate the style of the Elixir project or community at large. In a similar way to the existing
:line_length
option, the:trailing_comma
option provides an opt-in mechanism by which projects and organizations can benefit from standardized code formatting while retaining a style they find to be more efficient and productive.