Skip to content
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

Code formatter incorrectly formats multiline lists when there is a trailing comma #6646

Closed
pragdave opened this issue Oct 9, 2017 · 17 comments

Comments

@pragdave
Copy link
Contributor

pragdave commented Oct 9, 2017

The docstring says

As mentioned in the previous section, lists, tuples, bitstrings, maps, and
structs will be broken into multiple lines if they are followed by a newline in
the opening bracket and preceded by a new line in the closing bracket lines.

However, this code:

  @deps [
    { :private, "> 0.0.0" },
    { :ex_doc,  "~> 0.14", only: :dev, runtime: false },
  ]

gets formatted as:

  @deps [{:private, "> 0.0.0"}, {:ex_doc, "~> 0.14", only: :dev, runtime: false}]

Cheers

Dave

@fishcakez fishcakez changed the title Code formatter incorrectly formats multiline lists with line breaks Code formatter incorrectly formats multiline lists when there is a trailing comma Oct 9, 2017
@fishcakez
Copy link
Member

This looks like an issue with multiline lists combined with a trailing comma. The formatter will remove the trailing commas but then not honor the multiline list. Rather it should remove trailing comma and honor the multiline list.

@pragdave
Copy link
Contributor Author

pragdave commented Oct 9, 2017 via email

@fishcakez
Copy link
Member

Fwiw, multi line lists are exactly where a trailing comma is good...

Please try to stay objective :). A topic like trailing commas can invite lots of opinions and not many facts. Elixir current style does not use trailing commas and the formatter follows the existing style. If you can show that it would be of benefit please propose it separately.

@pragdave
Copy link
Contributor Author

pragdave commented Oct 9, 2017 via email

@fishcakez
Copy link
Member

Right, it makes refactoring easier, and works nicely for history as this creates the minimal diff because a change has the smallest area of effect. I am unsure if it has a slight negative effect clarity of code for those who are not used to it. Perhaps that becomes moot, except for beginners, if the formatting is widespread. I think a similar example is piping into a case where it is not obvious until you have seen it a few times, and then it is obvious.

@pragdave
Copy link
Contributor Author

pragdave commented Oct 9, 2017 via email

@fishcakez
Copy link
Member

I think this (and #6647) bring up a point about where the line is drawn between consistent style and flexibility. The current method is quite rigid and mandates the style, which can improve productivity as there is no discussion or leg work to be had on each project or change. On other hand being more flexible allows people to express they code how they feel is clearest and make more trade offs for themselves. Personally I don't know where this line should be.

@pragdave
Copy link
Contributor Author

pragdave commented Oct 9, 2017 via email

@josevalim
Copy link
Member

Thanks for the report @pragdave! It has been fixed on 9b5af33.

Our goal with the formatter is to mandate a style, reducing the amount of discussion on each project or change and also ensuring everyone in the community follows the same guidelines.

We will likely end-up adding more flexibility but it is still too early to tell which ones. Everyone is going to have preferences on which formatting rules they would like to keep and if we adopt all of them then the formatter loses its goal of unifying the styles across the community.

@pragdave
Copy link
Contributor Author

pragdave commented Oct 9, 2017 via email

@josevalim
Copy link
Member

@pragdave I am not sure. We have linters that are flexible when it comes to rules and some that are strict. Similarly, I would expect some formatters to be more flexible than others on keeping the user input.

In any case, we are quite happy with the formatter so far exactly because it automates the rules and we don't need to explicitly act on the feedback of a linter, which is a much slower development cycle.

@hadfieldn
Copy link

Came here looking to see if there's a way to force trailing commas. For the reasons @pragdave gives I'm disappointed this isn't an option.

@dorian-marchal
Copy link

Something I rarely see mentioned in these discussions is the ability to easily work with multicursors when using trailing commas:

with.mov

vs. the awkward:

without.mov

@stefanchrobot
Copy link
Contributor

The other thing I'd like to mention is the ability to manually control the line breaks with commas like Dart does it (I think they might have borrowed it from Google's Python formatter):

  • Trailing comma forces line breaks; this
[1, 2, 3,]

is always formatted as:

[
  1,
  2,
  3,
]
  • No trailing comma collapses line breaks, unless the list does not fit into one line.

I know the same thing can be achieved manually, but the trick with commas is much, much faster - you basically add/remove the trailing comma and let the formatter do the job (which I assume for most people is just Cmd+S or similar), as opposed to manually entering line breaks or removing them.

I think this is a really useful feature and could be adopted into the Elixir formatter.

@stefanchrobot
Copy link
Contributor

And a demo:

2021-05-05 15 47 26

@wojtekmach
Copy link
Member

wojtekmach commented May 5, 2021

Trailing commas is one of the most requested addition to the formatter but as was mentioned many times (in this issue, in similar issues, on ML, etc) it is unlikely to be added.

You may want to look into https://github.com/marcandre/freedom_formatter.

@dorian-marchal
Copy link

An other use case made difficult: the formatter removes the trailing comma when commenting the last line of an array.

Screen.Recording.2021-05-25.at.20.48.25.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

8 participants