jhipster / prettier-java

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

fix(printer): properly break and indent lambda with comments #604

Closed jtkiesel closed 7 months ago

jtkiesel commented 1 year ago

What changed with this PR:

Lambda expressions with leading/trailing comments are now broken and indented properly. Also, lambda expressions in general are formatted more similarly to Prettier JavaScript.

Example

Input

class Example {

  void example() {
    System.out.println(
      List.of(1, 2, 3).stream().map(
        // a very long comment which explains the beatifullness of multiplication by 2
        // yes this is very important
        v -> v * 2
      ).collect(Collectors.summingInt(v -> v))
    );
  }

  void example() {
    System.out.println(
      List.of(1, 2, 3).stream().map(
        v -> v * 2
        // a very long comment which explains the beatifullness of multiplication by 2
        // yes this is very important
      ).collect(Collectors.summingInt(v -> v))
    );
  }
}

Output

class Example {

  void example() {
    System.out.println(
      List.of(1, 2, 3)
        .stream()
        .map(
          // a very long comment which explains the beatifullness of multiplication by 2
          // yes this is very important
          v -> v * 2
        )
        .collect(Collectors.summingInt(v -> v))
    );
  }

  void example() {
    System.out.println(
      List.of(1, 2, 3)
        .stream()
        .map(
          v -> v * 2
          // a very long comment which explains the beatifullness of multiplication by 2
          // yes this is very important
        )
        .collect(Collectors.summingInt(v -> v))
    );
  }
}

Relative issues or prs:

Closes #581

jtkiesel commented 7 months ago

@clementdessoude This one should now be ready for review as well, if/when you have the time.