Skip to content

Improve formatting for implements clause #781

Closed

Description

@boris-petrov

Prettier-Java 2.7.5

Playground link

--print-width 55

Input:

record Foo(Loooooong a, Looooooooooong b) implements Longer {
  @Override
  public String toString() {
    return "";
  }
}

Output:

record Foo(Loooooong a, Looooooooooong b) implements
	Longer {
	@Override
	public String toString() {
		return "";
	}
}

Expected behavior:

I don't like that Longer is indented the same as the code below it. And perhaps even an empty line could be added. I believe that would be better:

record Foo(Loooooong a, Looooooooooong b) implements
		Longer {

	@Override
	public String toString() {
		return "";
	}
}

This is my personal preference though. Not sure how to simulate that in Prettier for JS. My tries fail miserably - Prettier doesn't want to wrap the extends line, very strange.

Activity

  1. jtkiesel commented on Oct 4, 2025

    @jtkiesel
    Contributor

    That's interesting that it refuses to wrap the extends. It does wrap implements in TypeScript (example), and does so similarly to the method I had gone with for improving throws wrapping in #591 (in that the { is wrapped), except that the implements is always wrapped.

  2. boris-petrov commented on Oct 4, 2025

    @boris-petrov
    Author

    @jtkiesel yes, it's indeed strange that they don't wrap extends but do implements. But even more interesting is that when they do that, they move the opening { on a new line! I've never seen that. This is probably the only case when they put the opening { on a new line instead of the end of the previous one.

    But note that this does make it more readable (at least to me) - because of putting { on the next line - it's immediately obvious that the implements line is somehow "separate" from the rest of the code below. Unlike the examples you've given:

        throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
        throw new RuntimeException();

    By just quickly glancing over such code, it's not immediately clear what happens - i.e. one should move their head and see the { at the end to see where the definition finishes and the implementation begins.

    But again - this is very subjective and I just opened the issue for a discussion. I'm absolutely fine with you closing it and leave it as it is.

    Thanks!

    P.S. some clarifications.

  3. jtkiesel commented on Oct 4, 2025

    @jtkiesel
    Contributor

    @boris-petrov I apologize for the confusion, I closed the issue on accident when trying to leave my comment, so I immediately reopened it -- this is a change I want to make, at least for implements.

  4. boris-petrov commented on Oct 4, 2025

    @boris-petrov
    Author

    I think it's very strange (maybe a bug) that extends does something different (compared to implements) in Prettier for JS. I see no reason for that. So if you want to change something, maybe do it for both?

  5. jtkiesel commented on Oct 4, 2025

    @jtkiesel
    Contributor

    I'll have to think about it all for a bit. I'm guessing the reason implements is formatted differently than extends is because you can implement multiple interfaces but TypeScript (just like Java) doesn't support multiple inheritance.

    One potential complication is that in Java, we currently format classes with an empty line after {, while for interfaces we don't. i.e. This might be a bit much:

    class Foo
      extends Bar
    {
    
      void baz() {}
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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