palantir / palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Apache License 2.0
448 stars 46 forks source link

Comment after code is not correctly indented when wrapped to the next line #1152

Open jamg76 opened 4 days ago

jamg76 commented 4 days ago

What happened?

This behaviour is observed when using spotless-plugin-gradle:6.25.0. It is also observed for the develop branch (as of 17 October 2024) in this repo, when trying it as input in a unit test (FormatterTest). It occurs for style 'palantir':

This input

class ClassB extends ClassA {
  @Test
  void shouldDoThisAndThat() {
    instance.shouldDoSomething(Set.of(
        "abcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijk", // prio (yet to go live)
        "abcdefghijkabcdefghijkabcdefghijkabjkghijkabcdefghij", // prio low, not exposed yet only for manual use
        "abcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkghijkabcdefghij" // prio low, not exposed yet only for manual use
        ));
  }
}

results in the following formatted output. The second and third line of the comment are not indented correctly:

class ClassB extends ClassA {
  @Test
  void shouldDoThisAndThat() {
    instance.shouldDoSomething(Set.of(
        "abcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijk", // prio
        // (yet to
        // go live)
        "abcdefghijkabcdefghijkabcdefghijkabjkghijkabcdefghij", // prio low, not exposed yet only
        // for manual use
        "abcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkghijkabcdefghij" // prio low, not exposed yet only for manual use
        ));
  }
}

What did you want to happen?

After the line wrap, the comment should start in the same column as the comment in the first line.

This is how the indentation should look like:

class ClassB extends ClassA {
  @Test
  void shouldDoThisAndThat() {
    instance.shouldDoSomething(Set.of(
        "abcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijk", // prio
                                                                                         // (yet to
                                                                                         // go live)
        "abcdefghijkabcdefghijkabcdefghijkabjkghijkabcdefghij" // prio low, not exposed yet only for
                                                               // manual use
        ));
        ...
  }
}

Interestingly, it works correctly if I remove the last long line:

"abcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkabcdefghijkghijkabcdefghij" // prio low, not exposed yet only for manual use: