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

EOL comment on left bracket of a block shouldn't be moved to a new line #798

Closed
a14n opened this issue Apr 3, 2019 · 5 comments
Closed

Comments

@a14n
Copy link
Contributor

a14n commented Apr 3, 2019

if (false) { // a
  var b = 1; // b
} // c

is formatted to

if (false) {
  // a
  var b = 1; // b
} // c
@a14n
Copy link
Contributor Author

a14n commented Apr 3, 2019

Same behaviour on the left square bracket of a list.

@munificent
Copy link
Member

This is working as intended. Can you show me an example where you think it makes the code look worse?

It's probably not obvious, but one reason the formatter makes changes like this is that it lets tools like code generators produce completely unformatted, throw it at dartfmt, and get something nice back. Moving the line comment to the beginning of the block looks good to me. Why is it better to leave it on the previous line?

@a14n
Copy link
Contributor Author

a14n commented Apr 5, 2019

The problem appears also in argument lists:

           actions: <Widget>[
-            IconButton( // action button
+            IconButton(
+              // action button
               icon: Icon(choices[0].icon),
               onPressed: () { _select(choices[0]); },
             ),
-            IconButton( // action button
+            IconButton(
+              // action button
               icon: Icon(choices[1].icon),
               onPressed: () { _select(choices[1]); },
             ),
-            PopupMenuButton<Choice>( // overflow menu
+            PopupMenuButton<Choice>(
+              // overflow menu
               onSelected: _select,
               itemBuilder: (BuildContext context) {

See @Hixie comment:

It makes the comments look like they're talking about the icon, icon, and onSelected arguments, instead of being about the IconButton, IconButton, and PopupMenuButton<Choice> widgets, which is what they're supposed to be about.

I agree. EOL comment should remain on the same line as its previous token (if the previous token was on the same line before formatting)

@munificent
Copy link
Member

Ah, interesting. That's a consequence of the fact that trailing comma argument lists are formatted the same way collection literals. For collection literals, it is deliberate that it moves the comma down since we assume it applies to the contents of the collection.

@munificent
Copy link
Member

I spent a lot of time working on how to best handle comments at the beginning of delimited sections in the new style. It's not unreasonable to want to leave them on the same line as the opening delimiter in some cases. But I went through a lot of code and looked at the results of leaving them there versus moving them to the next line and overall I think it looks best to move them down.

I'm going to go ahead and close this as working as intended.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants