palantir / palantir-java-format

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

Feature Request: Indent `Record` constructor to be `4` spaces instead of `8` #1012

Open Zhenye-Na opened 5 months ago

Zhenye-Na commented 5 months ago

What happened?

Related issue: https://github.com/palantir/palantir-java-format/issues/1007

What did you want to happen?

The formatter should indent Record constructor to be 4 spaces instead of 8

Zhenye-Na commented 4 months ago

Based on my investigation, it looks like in this line https://github.com/palantir/palantir-java-format/blob/e7abbb0435ec1bbdbcc8fc9ead8ea826fefc3ecc/palantir-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java#L29C1-L29C52, we are utilizing existing tokens.

However, Record is not included there, in such case

    /**
     * {@link TokenKind}s that indicate the start of a type definition. We use this to avoid scanning the whole file,
     * since we know that imports must precede any type definition.
     */
    private static final ImmutableSet<TokenKind> CLASS_START =
            ImmutableSet.of(TokenKind.CLASS, TokenKind.INTERFACE, TokenKind.ENUM);

    /**
     * We use this set to find the first import, and again to check that there are no imports after the place we stopped
     * gathering them. An annotation definition ({@code @interface}) is two tokens, the second which is
     * {@code interface}, so we don't need a separate entry for that.
     */
    private static final ImmutableSet<String> IMPORT_OR_CLASS_START =
            ImmutableSet.of("import", "class", "interface", "enum");

The formatting for record object has inconsistent indentation comparing to the others.

I am happy to contribute to this to include at least record in to it

Zhenye-Na commented 4 months ago

Probably relates to the follow code chunk as well

https://github.com/palantir/palantir-java-format/blob/e7abbb0435ec1bbdbcc8fc9ead8ea826fefc3ecc/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java#L163-L217