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

Indent comment under final default in switch #1209

Open
stereotype441 opened this issue Apr 20, 2023 · 2 comments
Open

Indent comment under final default in switch #1209

stereotype441 opened this issue Apr 20, 2023 · 2 comments

Comments

@stereotype441
Copy link
Member

Formatting the following code:

void f(int i) {
  switch (i) {
    case 0:
      print('zero');
    case 1:
      print('one');
    default:
      // Don't print anything
  }
}

Produces this result:

void f(int i) {
  switch (i) {
    case 0:
      print('zero');
    case 1:
      print('one');
    default:
    // Don't print anything
  }
}

(Note that the comment // Don't print anything was shifted to the left).

IMHO the comment should be indented relative to default:, just like code would have been.

Reproduced as of 6c9bc10.

@munificent
Copy link
Member

munificent commented Apr 22, 2023

Oof, yeah. I spent a surprising amount of time working on the formatting for comments that appear in switch statements between cases but not between a case and its body. (The latter is easy and is always indented with the body.)

I tried to make it much smarter but it ended up just being worse in so many cases that I gave up and left is as the current simple but not great behavior. Some fun edge cases I ran into:

switch (o) {
  // Before any case, so obviously not part of a case body.
  case 1:
    // Between a case and a body statement, so probably indent with the body.
    body;

    // After the body before a case. Indent with the previous body or with the
    // subsequent case?
  case 2:
    body;

  // Does the presence of a blank line affect that decision?

  case 3:

    // What if the preceding body is empty?

  case 4:
    body?
    // What about the presence of a blank line before the comment?
}

In practice, I had assumed that always indenting comments to align with case bodies would be the least bad option. But that's not how the formatter has behaved for many years and no one seems to have complained. When I tried running this on a large corpus, I discovered the majority of comments between switch cases are "group headers" that separate groups of cases, or "headers" that describe the subsequent case. In both of those, it makes sense to align the comment with the cases and not the bodies.

But for the last case (which may or may not be a default), I suppose it makes sense to always indent it to body depth not case depth since there's no subsequent case it could be attached to.

@AlexWalker222
Copy link

yes I noticed this when I was working on app. I can confirm this is still happening. Comments get moved to the next line down and kind of ruins the way they were set but I'm sure the team is working hard to fix great job so far amazing work happy codding everyone and may you find peace in doing what you love. Thanks for the sick work so far comments moving is a small price to pay for such a awesome system keep up the great work.

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

3 participants