Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Zhenye-Na opened this issue Feb 15, 2024 · 2 comments
Open

Comments

@Zhenye-Na
Copy link

What happened?

Related issue: #1007

What did you want to happen?

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

@Zhenye-Na
Copy link
Author

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
Copy link
Author

Probably relates to the follow code chunk as well

public void visitRecordDeclaration(ClassTree node) {
sync(node);
List<Op> breaks = visitModifiers(
node.getModifiers(), Direction.VERTICAL, /* declarationAnnotationBreak= */ Optional.empty());
Verify.verify(node.getExtendsClause() == null);
boolean hasSuperInterfaceTypes = !node.getImplementsClause().isEmpty();
builder.addAll(breaks);
token("record");
builder.space();
visit(node.getSimpleName());
if (!node.getTypeParameters().isEmpty()) {
token("<");
}
builder.open(plusFour);
{
if (!node.getTypeParameters().isEmpty()) {
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
}
ImmutableList<JCVariableDecl> parameters = recordVariables(node);
token("(");
if (!parameters.isEmpty()) {
// Break before args.
builder.breakToFill("");
}
// record headers can't declare receiver parameters
visitFormals(/* receiver= */ Optional.empty(), parameters);
token(")");
if (hasSuperInterfaceTypes) {
builder.breakToFill(" ");
builder.open(node.getImplementsClause().size() > 1 ? plusFour : ZERO);
token("implements");
builder.space();
boolean first = true;
for (Tree superInterfaceType : node.getImplementsClause()) {
if (!first) {
token(",");
builder.breakOp(" ");
}
scan(superInterfaceType, null);
first = false;
}
builder.close();
}
}
builder.close();
if (node.getMembers() == null) {
token(";");
} else {
List<Tree> members = node.getMembers().stream()
.filter(t -> (TreeInfo.flags((JCTree) t) & Flags.GENERATED_MEMBER) == 0)
.collect(toImmutableList());
addBodyDeclarations(members, BracesOrNot.YES, FirstDeclarationsOrNot.YES);
}
dropEmptyDeclarations();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant