jhipster / prettier-java

Prettier Java Plugin
http://www.jhipster.tech/prettier-java/
Apache License 2.0
1.1k stars 104 forks source link

Support JDK 22 multiple patterns in switch case label #612

Closed NolwennD closed 11 months ago

NolwennD commented 1 year ago

In JEP 456 a little enhancement will come for case label:

var x = 42;
switch (box) {
  case Box(RedBall _), Box(BlueBall _) when x == 42 -> processMatchAllBox(box); // multiple patterns introduce in JDK 22
  case Box(RedBall _) when x == 21, Box(BlueBall _) -> processBox(box);
  case Box(GreenBall _)                -> stopProcessing();
  case Box(_)                          -> pickAnotherBox();
}
jtkiesel commented 1 year ago

611 happens to add support for multiple case patterns in a switch label, though the way it prints them when wrapped is less than ideal:

class Example {

  int example() {
    return switch ("") {
      case LongTypeName longVariableName, LongTypeName longVariableName -> 0;
      case LongTypeName longVariableName,
        LongTypeName longVariableName,
        LongTypeName longVariableName -> 0;
      case MyRecord(A a), MyRecord(B b) -> 0;
      case MyRecord(A a), MyRecord(B b) when true -> 0;
      case MyRecord(
        LongTypeName longVariableName,
        LongTypeName longVariableName
      ),
        MyRecord(
        LongTypeName longVariableName,
        LongTypeName longVariableName
      ) -> 0;
    };
  }
}

It also doesn't support unnamed patterns/variables (_). I've begun working on those two improvements.

clementdessoude commented 1 year ago

@jtkiesel do you want to take this one (as it seems you already started working on this), or I can take it ?

jtkiesel commented 1 year ago

@clementdessoude I've got it nearly finished, I'll put up a PR shortly.