Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ export default {
{ value: "end", description: "" }
],
description: "Where to print operators when binary expressions wrap lines."
},
braceStyle: {
type: "choice",
category: "Java",
default: "same-line",
choices: [
{
value: "same-line",
description: "Put opening braces on same line (K&R style)"
},
{
value: "next-line",
description: "Put opening braces on new lines (Allman style)"
}
],
description: "Placement of opening braces."
}
} satisfies SupportOptions;
78 changes: 44 additions & 34 deletions src/printers/blocks-and-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const {
} = builders;

export default {
block(path, print) {
return printBlock(path, printBlockStatements(path, print));
block(path, print, options) {
return printBlock(path, printBlockStatements(path, print), options);
},

local_variable_declaration: printVariableDeclaration,
Expand All @@ -47,7 +47,7 @@ export default {
: [expression, ";"];
},

if_statement(path, print) {
if_statement(path, print, options) {
const statement = ["if ", path.call(print, "conditionNode")];

if (path.node.consequenceNode.type === ";") {
Expand All @@ -63,6 +63,8 @@ export default {
const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
statement.push(hardline, ...danglingComments, hardline);
} else if (options?.braceStyle === "next-line") {
statement.push(hardline);
} else {
const ifHasBlock = path.node.consequenceNode.type === SyntaxType.Block;
statement.push(ifHasBlock ? " " : hardline);
Expand Down Expand Up @@ -91,8 +93,8 @@ export default {
]);
},

switch_block(path, print) {
return printBlock(path, path.map(print, "namedChildren"));
switch_block(path, print, options) {
return printBlock(path, path.map(print, "namedChildren"), options);
},

switch_block_statement_group(path, print) {
Expand Down Expand Up @@ -192,15 +194,16 @@ export default {
}
},

do_statement(path, print) {
do_statement(path, print, options) {
const hasEmptyStatement = path.node.bodyNode.type === ";";
return [
"do",
hasEmptyStatement ? ";" : [" ", path.call(print, "bodyNode")],
" while ",
path.call(print, "conditionNode"),
";"
];
const isNextLine = options?.braceStyle === "next-line";
const whilePrefix = isNextLine ? [hardline, "while "] : " while ";
const body = hasEmptyStatement
? ";"
: isNextLine
? path.call(print, "bodyNode")
: [" ", path.call(print, "bodyNode")];
return ["do", body, whilePrefix, path.call(print, "conditionNode"), ";"];
},

for_statement(path, print) {
Expand Down Expand Up @@ -330,7 +333,7 @@ export default {
];
},

try_statement(path, print) {
try_statement(path, print, options) {
const parts = ["try", path.call(print, "bodyNode")];

path.each(child => {
Expand All @@ -342,23 +345,26 @@ export default {
}
}, "namedChildren");

if (options?.braceStyle === "next-line") {
return parts;
}
return join(" ", parts);
},

catch_clause(path, print) {
catch_clause(path, print, options) {
const catchFormalParameterIndex = path.node.namedChildren.findIndex(
({ type }) => type === SyntaxType.CatchFormalParameter
);
return [
"catch ",
group(
indentInParentheses(
path.call(print, "namedChildren", catchFormalParameterIndex)
)
),
" ",
path.call(print, "bodyNode")
];
const params = group(
indentInParentheses(
path.call(print, "namedChildren", catchFormalParameterIndex)
)
);
const body = path.call(print, "bodyNode");
if (options?.braceStyle === "next-line") {
return [hardline, "catch ", params, body];
}
return ["catch ", params, " ", body];
},

catch_formal_parameter(path, print) {
Expand All @@ -383,27 +389,31 @@ export default {
return join([line, "| "], path.map(print, "namedChildren"));
},

finally_clause(path, print) {
finally_clause(path, print, options) {
if (options?.braceStyle === "next-line") {
return [hardline, "finally", path.call(print, "namedChildren", 0)];
}
return ["finally ", path.call(print, "namedChildren", 0)];
},

try_with_resources_statement(path, print) {
const parts = [
"try",
path.call(print, "resourcesNode"),
path.call(print, "bodyNode")
];
try_with_resources_statement(path, print, options) {
const resources = path.call(print, "resourcesNode");
const body = path.call(print, "bodyNode");
const clauses: Doc[] = [];

path.each(child => {
if (
child.node.type === SyntaxType.CatchClause ||
child.node.type === SyntaxType.FinallyClause
) {
parts.push(print(child));
clauses.push(print(child));
}
}, "namedChildren");

return join(" ", parts);
if (options?.braceStyle === "next-line") {
return ["try ", resources, body, ...clauses];
}
return join(" ", ["try", resources, body, ...clauses]);
},

resource_specification(path, print) {
Expand Down
22 changes: 13 additions & 9 deletions src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { group, hardline, indent, indentIfBreak, join, line, softline } =
builders;

export default {
class_declaration(path, print) {
class_declaration(path, print, options) {
const parts: Doc[] = ["class ", path.call(print, "nameNode")];
const definedClauses = definedKeys(path.node, [
"superclassNode",
Expand All @@ -42,12 +42,14 @@ export default {
hasChild(path, clause) ? [separator, path.call(print, clause)] : []
);
const hasBody = path.node.bodyNode.namedChildren.length > 0;
const afterClauses =
options?.braceStyle === "next-line" ? "" : hasBody ? separator : " ";
const clauseGroup = [
hasTypeParameters && !hasMultipleClauses ? clauses : indent(clauses),
hasBody ? separator : " "
afterClauses
];
parts.push(hasMultipleClauses ? clauseGroup : group(clauseGroup));
} else {
} else if (options?.braceStyle !== "next-line") {
parts.push(" ");
}

Expand Down Expand Up @@ -89,14 +91,16 @@ export default {
return join([",", line], path.map(print, "namedChildren"));
},

class_body(path, print) {
class_body(path, print, options) {
return printBlock(
path,
printBodyDeclarations(
path,
print,
(path.parent as NamedNode | null)?.type === SyntaxType.ClassDeclaration
)
(path.parent as NamedNode | null)?.type ===
SyntaxType.ClassDeclaration && options?.braceStyle !== "next-line"
),
options
);
},

Expand Down Expand Up @@ -261,8 +265,8 @@ export default {
return [modifiers, group(declaration), " ", path.call(print, "bodyNode")];
},

constructor_body(path, print) {
return printBlock(path, printBlockStatements(path, print));
constructor_body(path, print, options) {
return printBlock(path, printBlockStatements(path, print), options);
},

explicit_constructor_invocation(path, print) {
Expand Down Expand Up @@ -379,7 +383,7 @@ export default {
if (declarations.length) {
contents.push(";", hardline, ...declarations);
}
return printBlock(path, contents.length ? [contents] : []);
return printBlock(path, contents.length ? [contents] : [], options);
},

enum_constant(path, print) {
Expand Down
71 changes: 67 additions & 4 deletions src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,22 @@ export function printArrayInitializer(
) {
if (!path.node.namedChildren.length) {
const danglingComments = printDanglingComments(path);
return danglingComments.length
? ["{", indent([hardline, ...danglingComments]), hardline, "}"]
: "{}";
if (danglingComments.length) {
if (options.braceStyle === "next-line") {
const isNested =
path.parent?.type === SyntaxType.ArrayInitializer ||
path.parent?.type === SyntaxType.ElementValueArrayInitializer;
const block = [
"{",
indent([hardline, ...danglingComments]),
hardline,
"}"
];
return isNested ? block : [hardline, ...block];
}
return ["{", indent([hardline, ...danglingComments]), hardline, "}"];
}
return "{}";
}

const list = join([",", line], path.map(print, "namedChildren"));
Expand All @@ -140,10 +153,26 @@ export function printArrayInitializer(
list.push(ifBreak(","));
}

if (options.braceStyle === "next-line") {
const isNested =
path.parent?.type === SyntaxType.ArrayInitializer ||
path.parent?.type === SyntaxType.ElementValueArrayInitializer;
const block = ["{", indent([softline, ...list]), softline, "}"];
return isNested ? block : [hardline, ...block];
}

return group(["{", indent([softline, ...list]), softline, "}"]);
}

export function printBlock(path: NamedNodePath, contents: Doc[]) {
export function printBlock(
path: NamedNodePath,
contents: Doc[],
options?: JavaParserOptions
) {
if (options?.braceStyle === "next-line") {
return printBlockNextLine(path, contents);
}

if (contents.length) {
return group([
"{",
Expand Down Expand Up @@ -195,6 +224,40 @@ export function printBlock(path: NamedNodePath, contents: Doc[]) {
: ["{", hardline, "}"];
}

function printBlockNextLine(path: NamedNodePath, contents: Doc[]) {
const parentType = path.parent?.type;
const isChildOfBodyDeclaration =
parentType != null &&
[
SyntaxType.AnnotationTypeBody,
SyntaxType.ClassBody,
SyntaxType.EnumBody,
SyntaxType.InterfaceBody
].includes(parentType);
const prefix = isChildOfBodyDeclaration ? [] : [hardline];

if (contents.length) {
return [
...prefix,
"{",
indent([hardline, ...join(hardline, contents)]),
hardline,
"}"
];
}
const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
return [
...prefix,
"{",
indent([hardline, ...danglingComments]),
hardline,
"}"
];
}
return [...prefix, "{", hardline, "}"];
}

export function printBlockStatements(
path: NamedNodePath<
| SyntaxType.Block
Expand Down
8 changes: 4 additions & 4 deletions src/printers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export default {
]);
},

interface_body(path, print) {
return printBlock(path, printBodyDeclarations(path, print));
interface_body(path, print, options) {
return printBlock(path, printBodyDeclarations(path, print), options);
},

constant_declaration: printVariableDeclaration,
Expand All @@ -91,8 +91,8 @@ export default {
return parts;
},

annotation_type_body(path, print) {
return printBlock(path, printBodyDeclarations(path, print));
annotation_type_body(path, print, options) {
return printBlock(path, printBodyDeclarations(path, print), options);
},

annotation_type_element_declaration(path, print) {
Expand Down
5 changes: 3 additions & 2 deletions src/printers/packages-and-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default {
return join(" ", parts);
},

module_body(path, print) {
module_body(path, print, options) {
return printBlock(
path,
path.map(
Expand All @@ -143,7 +143,8 @@ export default {
? [hardline, print(child)]
: print(child),
"namedChildren"
)
),
options
);
},

Expand Down
1 change: 1 addition & 0 deletions test/unit-test/braces-next-line/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "braceStyle": "next-line" }
Loading