prettier-java icon indicating copy to clipboard operation
prettier-java copied to clipboard

fix: improve throws formatting

Open jtkiesel opened this issue 1 year ago • 1 comments

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

jtkiesel avatar Jul 29 '23 06:07 jtkiesel

@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?

jtkiesel avatar Feb 28 '24 18:02 jtkiesel