Skip to content

Suboptimal line breaks for long switch rules (with -> arrows) #880

Closed
@PhilippWendler

Description

@PhilippWendler

For the following code, I would expect line breaks to be added at the arrows (->) of the rules of the switch expression:

public class Test {

  public String f(int i) {
    return switch (i) {
      case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 -> "looooooooooooooooooooooooooooooooooooooooong expression";
      default -> "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong expression";
    };
  }
}

However, google-java-format 1.15.0 formats it like this:

public class Test {

  public String f(int i) {
    return switch (i) {
      case 0,
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9 -> "looooooooooooooooooooooooooooooooooooooooong expression";
      default -> "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"
                     + " expression";
    };
  }
}

It even prefers breaking up the string over a line break at the arrow. Please increase the priority of line breaking at the arrow. I guess it should be of higher priority than the list of cases and anything within the expression.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions