palantir / palantir/palantir-java-format

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

Open
#1,152 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
907
Forks
101
Avg merge
1d 3h
Merged PRs (30d)
19

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FormatterTest unit test using the reported input and the palantir style, then trace the formatter code responsible for wrapping trailing comments. Reproduce the incorrect indentation and add coverage for the shown cases; done means wrapped comment lines begin in the same column as the original comment.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.