Skip to content

[2.10.1] Needlessly splitting comment #955

Closed

Description

@boris-petrov

Prettier-Java 2.10.1

--print-width 140

Playground link

Input:

var a = api.b(c ->
    // aaaaaaaaaaaaa
    c.d(d)
);

Output:

var a = api.b((c) ->
	c
		// aaaaaaaaaaaaa
		.d(d)
);

Expected behavior:
Unchanged because Prettier for JS does so

Activity

  1. 2is10 commented on Oct 19, 2026

    @2is10

    I was about to file this same issue, but luckily found this one.

    I noticed it also happens in 2.11.0 when a comment precedes a chain that is a call argument:

    Input:

    class A {
      Stream<Row> rows() {
        return Stream.of(
          // first
          row("a").withX(1).build(),
          // second
          row("b").withX(2).build()
        );
      }
    }

    Output:

    class A {
    
      Stream<Row> rows() {
        return Stream.of(
          // first
          row("a")
            .withX(1)
            .build(),
          // second
          row("b")
            .withX(2)
            .build()
        );
      }
    }

    ...or when the comment precedes an array element, which is common in data providers:

    Input:

    class A {
      @DataProvider
      Object[][] settings() {
        return new Object[][] {
          {
            // Different values
            Settings.builder().value("a").build(),
            Settings.builder().value("b").build(),
          },
        };
      }
    }

    Output:

    class A {
    
      @DataProvider
      Object[][] settings() {
        return new Object[][] {
          {
            // Different values
            Settings.builder()
              .value("a")
              .build(),
            Settings.builder().value("b").build(),
          },
        };
      }
    }

    Expected behavior: Unchanged. The only difference between the two elements is the comment above the first one, which shouldn't force its chain to break.


    A block comment before a chain argument is also moved onto the last call, so it ends up describing the wrong part of the expression:

    Input:

    class A {
      void f() {
        call(/* retry */ builder.withRetry(true).build());
      }
    }

    Output:

    class A {
    
      void f() {
        call(
          builder
            .withRetry(true)
            /* retry */ .build()
        );
      }
    }

    Expected behavior: Both inputs unchanged, as in Prettier JS. A comment that ends before the chain's first token should print ahead of the whole chain and shouldn't count as a comment inside it.


    We've patched prettier-java locally to print such comments ahead of the chain and leave them out of the "chain has a comment" check.

    Would it be helpful for me to open a PR? Looks like #1057 was closed without getting merged.

  2. jtkiesel commented on Oct 19, 2026

    @jtkiesel
    Contributor

    I think some/all of these might be resolved by #1109, but I will check later today.

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