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
13 changes: 3 additions & 10 deletions src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
printBlockStatements,
printBodyDeclarations,
printModifiers,
printTypeParameters,
printValue,
printVariableDeclaration,
type NamedNodePrinters
} from "./helpers.ts";

const { group, hardline, indent, indentIfBreak, join, line, softline } =
builders;
const { group, hardline, indent, indentIfBreak, join, line } = builders;

export default {
class_declaration(path, print) {
Expand Down Expand Up @@ -58,14 +58,7 @@ export default {
];
},

type_parameters(path, print) {
return [
"<",
indent([softline, join([",", line], path.map(print, "namedChildren"))]),
softline,
">"
];
},
type_parameters: printTypeParameters,

superclass(path, print) {
return ["extends ", path.call(print, "namedChildren", 0)];
Expand Down
58 changes: 58 additions & 0 deletions src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,36 @@ export function printBodyDeclarations(
}, "namedChildren");
}

export function printTypeParameters(
path: NamedNodePath<SyntaxType.TypeArguments | SyntaxType.TypeParameters>,
print: PrintFunction
) {
const parameters = path.node.namedChildren;

const shouldInline =
parameters.length === 0 ||
(parameters.length === 1 &&
isSimpleType(parameters[0]) &&
!parameters.some(
({ comments }) =>
comments?.length &&
comments.some(({ type }) => type === SyntaxType.LineComment)
));

if (shouldInline) {
return ["<", join(", ", path.map(print, "namedChildren")), ">"];
}

const parts = [
"<",
indent([softline, join([",", line], path.map(print, "namedChildren"))]),
softline,
">"
];

return path.node.type === SyntaxType.TypeArguments ? group(parts) : parts;
}

export function printVariableDeclaration(
path: NamedNodePath<
| SyntaxType.ConstantDeclaration
Expand Down Expand Up @@ -364,6 +394,34 @@ export function textBlockContents(node: NamedNode<SyntaxType.StringLiteral>) {
.slice(0, -3);
}

function isSimpleType(node: NamedNode): boolean {
const { type, children, namedChildren } = node;
const lastNamedChild = namedChildren.at(-1);

return (
[
SyntaxType.BooleanType,
SyntaxType.FloatingPointType,
SyntaxType.IntegralType,
SyntaxType.TypeIdentifier,
SyntaxType.VoidType
].includes(type) ||
(type === SyntaxType.AnnotatedType &&
lastNamedChild &&
isSimpleType(lastNamedChild)) ||
(type === SyntaxType.ArrayType && isSimpleType(node.elementNode)) ||
(type === SyntaxType.ScopedTypeIdentifier &&
lastNamedChild &&
isSimpleType(namedChildren[0]) &&
isSimpleType(lastNamedChild)) ||
(type === SyntaxType.TypeParameter &&
(lastNamedChild?.type !== SyntaxType.TypeBound ||
lastNamedChild.namedChildren.length === 1)) ||
(type === SyntaxType.Wildcard &&
(children.at(-1)!.type === "?" || isSimpleType(namedChildren.at(-1)!)))
);
}

function findBaseIndent(lines: string[]) {
return Math.min(
...lines.map(line => line.search(/\S/)).filter(indent => indent >= 0)
Expand Down
21 changes: 7 additions & 14 deletions src/printers/types-values-and-variables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { builders } from "prettier/doc";
import { printValue, type NamedNodePrinters } from "./helpers.ts";
import {
printTypeParameters,
printValue,
type NamedNodePrinters
} from "./helpers.ts";

const { group, indent, join, line, softline } = builders;
const { group, indent, join, line } = builders;

export default {
boolean_type: printValue,
Expand Down Expand Up @@ -42,18 +46,7 @@ export default {
return bound;
},

type_arguments(path, print) {
const types = path.map(print, "namedChildren");

return types.length
? group([
"<",
indent([softline, join([",", line], types)]),
softline,
">"
])
: "<>";
},
type_arguments: printTypeParameters,

wildcard(path, print) {
return join(" ", path.map(print, "children"));
Expand Down
5 changes: 2 additions & 3 deletions test/unit-test/arrays/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ class Array {

boolean[] skip = new boolean[candidates.length];

Class<?> aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa<
Bbbbbbbbbbbbbbbb
>[1].getClass();
Class<?> aaaaaaaaaaaaaaaa =
new Aaaaaaaaaaaaaaaa<Bbbbbbbbbbbbbbbb>[1].getClass();
Class<?> aaaaaaaaaaaaaaaa =
new Aaaaaaaaaaaaaaaa[1111111111111111111].getClass();
Class<?> aaaaaaaaaaaaaaaa = new Aaaaaaaaaaaaaaaa[] {
Expand Down
Loading