jhipster / prettier-java

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

fix: improve throws formatting #591

Open jtkiesel opened 11 months ago

jtkiesel commented 11 months ago

What changed with this PR:

Constructors/methods with throws clauses now break on their comma-separated list of exception classes rather than violating printWidth, and the left curly brace of the body is broken when this happens in order to separate the exception classes from the body (since they will be at the same indentation).

Example

Input

class Example {

  Example(String arg1, String arg2)
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  )
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example1(String arg1, String arg2)
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example2(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example3(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  )
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }
}

Output

class Example {

  Example(String arg1, String arg2) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }

  void example1(String arg1, String arg2) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }

  void example2(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example3(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }
}

Relative issues or prs:

Closes #286 Closes #429 Closes #583

jtkiesel commented 4 months ago

@clementdessoude I'm curious what your thoughts are on the formatting decisions I made here. Could you take a look, whenever you have the time?