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
34 changes: 33 additions & 1 deletion packages/prettier-plugin-java/src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export function handleLineComment(
handleIfStatementComments,
handleJumpStatementComments,
handleLabeledStatementComments,
handleNameComments
handleNameComments,
handleTryStatementComments
].some(fn => fn(commentNode, options));
}

Expand Down Expand Up @@ -247,6 +248,37 @@ function handleNameComments(commentNode: JavaComment) {
return false;
}

function handleTryStatementComments(commentNode: JavaComment) {
const { enclosingNode, followingNode } = commentNode;
if (
enclosingNode &&
["catches", "tryStatement"].includes(enclosingNode.name) &&
followingNode &&
isNonTerminal(followingNode)
) {
const block = (
followingNode.name === "catches"
? followingNode.children.catchClause[0]
: followingNode.name === "catchClause" ||
followingNode.name === "finally"
? followingNode
: null
)?.children.block[0];
if (!block) {
return false;
}
const blockStatement =
block.children.blockStatements?.[0].children.blockStatement[0];
if (blockStatement) {
util.addLeadingComment(blockStatement, commentNode);
} else {
util.addDanglingComment(block, commentNode, undefined);
}
return true;
}
return false;
}

function isBinaryOperator(node?: JavaNode) {
return (
node !== undefined &&
Expand Down
56 changes: 45 additions & 11 deletions packages/prettier-plugin-java/src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,52 @@ export function printArrayInitializer<
}

export function printBlock(path: AstPath<JavaNonTerminal>, contents: Doc[]) {
if (!contents.length) {
const danglingComments = printDanglingComments(path);
return danglingComments.length
? ["{", indent([hardline, ...danglingComments]), hardline, "}"]
: "{}";
if (contents.length) {
return group([
"{",
indent([hardline, ...join(hardline, contents)]),
hardline,
"}"
]);
}
return group([
"{",
indent([hardline, ...join(hardline, contents)]),
hardline,
"}"
]);
const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
return ["{", indent([hardline, ...danglingComments]), hardline, "}"];
}
const parent = path.grandparent;
const grandparent = path.getNode(4);
const greatGrandparent = path.getNode(6);
return (grandparent?.name === "catches" &&
grandparent.children.catchClause.length === 1 &&
(greatGrandparent?.name === "tryStatement" ||
greatGrandparent?.name === "tryWithResourcesStatement") &&
!greatGrandparent.children.finally) ||
(greatGrandparent &&
[
"basicForStatement",
"doStatement",
"enhancedForStatement",
"whileStatement"
].includes(greatGrandparent.name)) ||
[
"annotationInterfaceBody",
"classBody",
"constructorBody",
"enumBody",
"interfaceBody",
"moduleDeclaration",
"recordBody"
].includes(path.node.name) ||
(parent &&
[
"instanceInitializer",
"lambdaBody",
"methodBody",
"staticInitializer",
"synchronizedStatement"
].includes(parent.name))
? "{}"
: ["{", hardline, "}"];
}

export function printName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ void parentheses() {
var sizeIndex = ((index - 1) >>> level) & MASK;
var from = offset > left ? 0 : (left - offset) >> level;
var to = (right - offset) >> (level + 1);
if (rawIndex < 1 << (list._level + SHIFT)) {}
if (rawIndex < 1 << (list._level + SHIFT)) {
}
var res = size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
sign = (1 - 2 * b[3]) >> 7;
exponent = ((b[3] << 1) & 0xff) | (b[2] >> (7 - 127));
Expand Down Expand Up @@ -213,12 +214,14 @@ void parentheses() {
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd &&
eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh) ||
iiiiiiiiii
) {}
) {
}

if (
(((((a * b + c) << d < e == f) & g) ^ h) | i && j) ||
(k && l | (m ^ (n & (o != p > q >> (r - s / t)))))
) {}
) {
}

if (
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd &&
Expand All @@ -227,7 +230,8 @@ void parentheses() {
mmmmmmmmmm + nnnnnnnnnn == oooooooooo + pppppppppp) ||
(qqqqqqqqqq + rrrrrrrrrr == ssssssssss + tttttttttt &&
uuuuuuuuuu + vvvvvvvvvv == wwwwwwwwww + xxxxxxxxxxx)
) {}
) {
}
}

void instanceOf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ void parentheses() {
var sizeIndex = ((index - 1) >>> level) & MASK;
var from = offset > left ? 0 : (left - offset) >> level;
var to = (right - offset) >> (level + 1);
if (rawIndex < 1 << (list._level + SHIFT)) {}
if (rawIndex < 1 << (list._level + SHIFT)) {
}
var res = size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
sign = (1 - 2 * b[3]) >> 7;
exponent = ((b[3] << 1) & 0xff) | (b[2] >> (7 - 127));
Expand Down Expand Up @@ -206,12 +207,14 @@ void parentheses() {
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd
&& eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh)
|| iiiiiiiiii
) {}
) {
}

if (
(((((a * b + c) << d < e == f) & g) ^ h) | i && j)
|| (k && l | (m ^ (n & (o != p > q >> (r - s / t)))))
) {}
) {
}

if (
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd
Expand All @@ -220,7 +223,8 @@ void parentheses() {
&& mmmmmmmmmm + nnnnnnnnnn == oooooooooo + pppppppppp)
|| (qqqqqqqqqq + rrrrrrrrrr == ssssssssss + tttttttttt
&& uuuuuuuuuu + vvvvvvvvvv == wwwwwwwwww + xxxxxxxxxxx)
) {}
) {
}
}

void instanceOf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ void commentsIfLineComment() {
if (
// test
t
) {}
) {
}

if (
t // test
) {}
) {
}

if (t) {} // test
if (t) {
} // test

if (
// test
t
) {}
) {
}

if (
true // comment
Expand All @@ -25,26 +29,38 @@ void commentsIfLineComment() {
}

void commentsIfBlockComment() {
if (/* test */ t) {}
if (/* test */ t) {
}

if (t /* test */) {}
if (t /* test */) {
}

if (t) /* test */ {}
if (t) /* test */ {
}

if (/* test */ t) {}
if (/* test */ t) {
}
}

void commentsElseLineComment() {
if (t) {}
if (t) {
}
// test
else {}
else {
}

if (t) {} else {} // test
if (t) {
} else {
} // test
}

void commentsElseBlockComment() {
if (t) {} /* test */ else {}
if (t) {
} /* test */ else {
}

if (t) {} else /* test */ {}
if (t) {
} else /* test */ {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public void instanceOf() {
}

public void printSimple() {
if (myValue == 42) {}
if (myValue == 42) {
}

if (myValue != 42) {
System.out.println("Why not 42 !");
Expand All @@ -92,33 +93,40 @@ public void printIf() {
myValue == 42 ||
(myValue == 42 && myValue == 42 && myValue == 42) ||
(myValue == 42 && myValue == 42)
) {}
) {
}

if (
(myValue != 42 && 42 / 42) ||
(myValue & 42 && myValue > 42) ||
(myValue < 42 && myValue == 42)
) {}
) {
}

if (myValue != 42 && myValue == 42) {}
if (myValue != 42 && myValue == 42) {
}
}

public void printSwitch() {
switch (
myValue == 42 ||
(myValue == 42 && myValue == 42 && myValue == 42) ||
(myValue == 42 && myValue == 42)
) {}
) {
}

switch (
(myValue != 42 && 42 / 42) ||
(myValue & 42 && myValue > 42) ||
(myValue < 42 && myValue == 42)
) {}
) {
}

switch (myValue != 42) {}
switch (myValue != 42) {
}

switch (myValue != 42 && myValue == 42) {}
switch (myValue != 42 && myValue == 42) {
}
}

public void printWhile() {
Expand Down
10 changes: 10 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/switch/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,14 @@ void switchRulesWithComments() {
throw new RuntimeException();
}
}

void emptyBlocks() {
switch (a) {}
switch (a) {
case 1: {}
}
switch (a) {
case 1 -> {}
}
}
}
19 changes: 17 additions & 2 deletions packages/prettier-plugin-java/test/unit-test/switch/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public String shouldWrapEvenForSmallSwitchCases() {

void switchCaseWithBlock1() {
switch (a) {
case 0: {}
default: {}
case 0: {
}
default: {
}
}
}

Expand Down Expand Up @@ -233,4 +235,17 @@ void switchRulesWithComments() {
throw new RuntimeException();
}
}

void emptyBlocks() {
switch (a) {
}
switch (a) {
case 1: {
}
}
switch (a) {
case 1 -> {
}
}
}
}
Loading