Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 76 additions & 29 deletions packages/prettier-plugin-java/src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { AstPath, Doc } from "prettier";
import { builders } from "prettier/doc";
import {
call,
definedKeys,
each,
hasDeclarationAnnotations,
hasLeadingComments,
Expand Down Expand Up @@ -42,33 +43,49 @@ export default {
},

normalClassDeclaration(path, print) {
const { classExtends, classImplements, classPermits, typeParameters } =
path.node.children;
const header = ["class ", call(path, print, "typeIdentifier")];
if (typeParameters) {
header.push(call(path, print, "typeParameters"));
}
if (classExtends) {
header.push(indent([line, call(path, print, "classExtends")]));
}
if (classImplements) {
header.push(indent([line, call(path, print, "classImplements")]));
const { children } = path.node;
const definedClauses = definedKeys(children, [
"classExtends",
"classImplements",
"classPermits"
]);
const hasMultipleClauses = definedClauses.length > 1;
const hasTypeParameters = children.typeParameters !== undefined;
const parts = ["class ", call(path, print, "typeIdentifier")];
if (hasTypeParameters) {
const typeParameters = call(path, print, "typeParameters");
parts.push(
hasMultipleClauses ? group(indent(typeParameters)) : typeParameters
);
}
if (classPermits) {
header.push(indent([line, call(path, print, "classPermits")]));
if (definedClauses.length) {
const separator = hasTypeParameters && !hasMultipleClauses ? " " : line;
const clauses = definedClauses.flatMap(clause => [
separator,
call(path, print, clause)
]);
const hasBody =
children.classBody[0].children.classBodyDeclaration !== undefined;
const clauseGroup = [
hasTypeParameters && !hasMultipleClauses ? clauses : indent(clauses),
hasBody ? separator : " "
];
parts.push(hasMultipleClauses ? clauseGroup : group(clauseGroup));
} else {
parts.push(" ");
}
return [group(header), " ", call(path, print, "classBody")];
return [group(parts), call(path, print, "classBody")];
},

classModifier: printSingle,

typeParameters(path, print) {
return group([
return [
"<",
indent([softline, call(path, print, "typeParameterList")]),
softline,
">"
]);
];
},

typeParameterList(path, print) {
Expand All @@ -89,7 +106,7 @@ export default {
classPermits: printClassPermits,

interfaceTypeList(path, print) {
return group(printList(path, print, "interfaceType"));
return printList(path, print, "interfaceType");
},

classBody(path, print) {
Expand Down Expand Up @@ -199,7 +216,7 @@ export default {
const { typeParameters, annotation, throws } = path.node.children;
const header: Doc[] = [];
if (typeParameters) {
header.push(call(path, print, "typeParameters"));
header.push(group(call(path, print, "typeParameters")));
}
if (annotation) {
header.push(join(line, map(path, print, "annotation")));
Expand Down Expand Up @@ -322,7 +339,7 @@ export default {
: "()"
);
return children.typeParameters
? [call(path, print, "typeParameters"), " ", ...header]
? [group(call(path, print, "typeParameters")), " ", ...header]
: header;
},

Expand Down Expand Up @@ -379,11 +396,21 @@ export default {
},

enumDeclaration(path, print) {
const header = ["enum", call(path, print, "typeIdentifier")];
if (path.node.children.classImplements) {
header.push(call(path, print, "classImplements"));
const { children } = path.node;
const parts = ["enum ", call(path, print, "typeIdentifier")];
if (children.classImplements) {
const body = children.enumBody[0].children;
const hasBody =
body.enumBodyDeclarations !== undefined ||
body.enumConstantList !== undefined;
parts.push(
indent([line, call(path, print, "classImplements")]),
hasBody ? line : " "
);
} else {
parts.push(" ");
}
return join(" ", [...header, call(path, print, "enumBody")]);
return [group(parts), call(path, print, "enumBody")];
},

enumBody(path, print, options) {
Expand Down Expand Up @@ -445,20 +472,40 @@ export default {

recordDeclaration(path, print) {
const { children } = path.node;
const header = ["record ", call(path, print, "typeIdentifier")];
const parts = ["record ", call(path, print, "typeIdentifier")];
if (children.typeParameters) {
header.push(call(path, print, "typeParameters"));
parts.push(group(call(path, print, "typeParameters")));
}
header.push(call(path, print, "recordHeader"));
parts.push(call(path, print, "recordHeader"));
if (children.classImplements) {
header.push(" ", call(path, print, "classImplements"));
const hasComponents =
children.recordHeader[0].children.recordComponentList !== undefined;
const hasBody =
children.recordBody[0].children.recordBodyDeclaration !== undefined;
const classImplements = [
hasComponents ? " " : line,
call(path, print, "classImplements")
];
parts.push(
group([
hasComponents ? classImplements : indent(classImplements),
hasBody ? line : " "
])
);
} else {
parts.push(" ");
}
return [group(header), " ", call(path, print, "recordBody")];
return [group(parts), call(path, print, "recordBody")];
},

recordHeader(path, print) {
return path.node.children.recordComponentList
? indentInParentheses(call(path, print, "recordComponentList"))
? [
"(",
indent([softline, call(path, print, "recordComponentList")]),
softline,
")"
]
: indentInParentheses(printDanglingComments(path), { shouldBreak: true });
},

Expand Down
5 changes: 1 addition & 4 deletions packages/prettier-plugin-java/src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,7 @@ export function printClassPermits(
path: AstPath<ClassPermitsCstNode | InterfacePermitsCstNode>,
print: JavaPrintFn
) {
return group([
"permits",
indent([line, group(printList(path, print, "typeName"))])
]);
return group(["permits", indent([line, printList(path, print, "typeName")])]);
}

export function printClassType(
Expand Down
42 changes: 31 additions & 11 deletions packages/prettier-plugin-java/src/printers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Doc } from "prettier";
import { builders } from "prettier/doc";
import {
call,
definedKeys,
each,
hasDeclarationAnnotations,
indentInParentheses,
Expand Down Expand Up @@ -35,19 +36,38 @@ export default {
},

normalInterfaceDeclaration(path, print) {
const { interfaceExtends, interfacePermits, typeParameters } =
path.node.children;
const header = ["interface ", call(path, print, "typeIdentifier")];
if (typeParameters) {
header.push(call(path, print, "typeParameters"));
}
if (interfaceExtends) {
header.push(indent([line, call(path, print, "interfaceExtends")]));
const { children } = path.node;
const definedClauses = definedKeys(children, [
"interfaceExtends",
"interfacePermits"
]);
const hasMultipleClauses = definedClauses.length > 1;
const hasTypeParameters = children.typeParameters !== undefined;
const parts = ["interface ", call(path, print, "typeIdentifier")];
if (hasTypeParameters) {
const typeParameters = call(path, print, "typeParameters");
parts.push(
hasMultipleClauses ? group(indent(typeParameters)) : typeParameters
);
}
if (interfacePermits) {
header.push(indent([line, call(path, print, "interfacePermits")]));
if (definedClauses.length) {
const separator = hasTypeParameters && !hasMultipleClauses ? " " : line;
const clauses = definedClauses.flatMap(clause => [
separator,
call(path, print, clause)
]);
const hasBody =
children.interfaceBody[0].children.interfaceMemberDeclaration !==
undefined;
const clauseGroup = [
hasTypeParameters && !hasMultipleClauses ? clauses : indent(clauses),
hasBody ? separator : " "
];
parts.push(hasMultipleClauses ? clauseGroup : group(clauseGroup));
} else {
parts.push(" ");
}
return [group(header), " ", call(path, print, "interfaceBody")];
return [group(parts), call(path, print, "interfaceBody")];
},

interfaceModifier: printSingle,
Expand Down
43 changes: 43 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/classes/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,46 @@ class ClassWithSemicolon {
;
private FieldOneClass fieldOne;
}

class Aaaaaaaaaa<Bbbbbbbbbb> extends Cccccccccc implements Dddddddddd {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc> extends Dddddddddd implements Eeeeeeeeee {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc> extends Dddddddddd implements Eeeeeeeeee {}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc> extends Dddddddddd<Eeeeeeeeee, Ffffffffff> {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc> extends Dddddddddd<Eeeeeeeeee, Ffffffffff, Gggggggggg, Hhhhhhhhhh, Iiiiiiiiii> {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc> extends Dddddddddd<Eeeeeeeeee, Ffffffffff> implements Gggggggggg, Hhhhhhhhhh {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc> extends Dddddddddd<Eeeeeeeeee, Ffffffffff> implements Gggggggggg, Hhhhhhhhhh {}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc, Dddddddddd, Eeeeeeeeee, Ffffffffff, Gggggggggg> extends Hhhhhhhhhh<Iiiiiiiiii, Jjjjjjjjjj> implements Kkkkkkkkkk, Llllllllll {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc, Dddddddddd, Eeeeeeeeee, Ffffffffff, Gggggggggg> extends Hhhhhhhhhh<Iiiiiiiiii, Jjjjjjjjjj> implements Kkkkkkkkkk, Llllllllll {}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc, Dddddddddd, Eeeeeeeeee, Ffffffffff, Gggggggggg> extends Hhhhhhhhhh<Iiiiiiiiii, Jjjjjjjjjj, Kkkkkkkkkk, Llllllllll, Mmmmmmmmmm, Nnnnnnnnnn> implements Oooooooooo, Pppppppppp, Qqqqqqqqqq, Rrrrrrrrrr, Ssssssssss, Tttttttttt, Uuuuuuuuuu {

void a() {}
}

class Aaaaaaaaaa<Bbbbbbbbbb, Cccccccccc, Dddddddddd, Eeeeeeeeee, Ffffffffff, Gggggggggg> extends Hhhhhhhhhh<Iiiiiiiiii, Jjjjjjjjjj, Kkkkkkkkkk, Llllllllll, Mmmmmmmmmm, Nnnnnnnnnn> implements Oooooooooo, Pppppppppp, Qqqqqqqqqq, Rrrrrrrrrr, Ssssssssss, Tttttttttt, Uuuuuuuuuu {}
Loading