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
50 changes: 26 additions & 24 deletions packages/prettier-plugin-java/src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function handleLineComment(
) {
return [
handleBinaryExpressionComments,
handleConditionalExpressionComments,
handleFqnOrRefTypeComments,
handleIfStatementComments,
handleJumpStatementComments,
Expand All @@ -116,11 +117,7 @@ function handleBinaryExpressionComments(
options: JavaParserOptions
) {
const { enclosingNode, precedingNode, followingNode } = commentNode;
if (
enclosingNode &&
isNonTerminal(enclosingNode) &&
enclosingNode.name === "binaryExpression"
) {
if (enclosingNode?.name === "binaryExpression") {
if (isBinaryOperator(followingNode)) {
if (options.experimentalOperatorPosition === "start") {
util.addLeadingComment(followingNode, commentNode);
Expand All @@ -139,26 +136,37 @@ function handleBinaryExpressionComments(
return false;
}

function handleFqnOrRefTypeComments(commentNode: JavaComment) {
const { enclosingNode, followingNode } = commentNode;
function handleConditionalExpressionComments(commentNode: JavaComment) {
const { startLine, endLine, enclosingNode, precedingNode, followingNode } =
commentNode;
if (
enclosingNode &&
isNonTerminal(enclosingNode) &&
enclosingNode.name === "fqnOrRefType" &&
followingNode
enclosingNode?.name === "conditionalExpression" &&
precedingNode &&
followingNode &&
isNonTerminal(precedingNode) &&
isNonTerminal(followingNode) &&
precedingNode.location.endLine < startLine &&
endLine < followingNode.location.startLine
) {
util.addLeadingComment(followingNode, commentNode);
return true;
}
return false;
}

function handleFqnOrRefTypeComments(commentNode: JavaComment) {
const { enclosingNode, followingNode } = commentNode;
if (enclosingNode?.name === "fqnOrRefType" && followingNode) {
util.addLeadingComment(followingNode, commentNode);
return true;
}
return false;
}

function handleIfStatementComments(commentNode: JavaComment) {
const { enclosingNode, precedingNode } = commentNode;
if (
enclosingNode &&
isNonTerminal(enclosingNode) &&
enclosingNode.name === "ifStatement" &&
enclosingNode?.name === "ifStatement" &&
precedingNode &&
isNonTerminal(precedingNode) &&
precedingNode.name === "statement"
Expand All @@ -175,7 +183,6 @@ function handleJumpStatementComments(commentNode: JavaComment) {
enclosingNode &&
!precedingNode &&
!followingNode &&
isNonTerminal(enclosingNode) &&
["breakStatement", "continueStatement", "returnStatement"].includes(
enclosingNode.name
)
Expand All @@ -189,10 +196,8 @@ function handleJumpStatementComments(commentNode: JavaComment) {
function handleLabeledStatementComments(commentNode: JavaComment) {
const { enclosingNode, precedingNode } = commentNode;
if (
enclosingNode &&
enclosingNode?.name === "labeledStatement" &&
precedingNode &&
isNonTerminal(enclosingNode) &&
enclosingNode.name === "labeledStatement" &&
isTerminal(precedingNode) &&
precedingNode.tokenType.name === "Identifier"
) {
Expand All @@ -205,9 +210,7 @@ function handleLabeledStatementComments(commentNode: JavaComment) {
function handleMethodDeclaratorComments(commentNode: JavaComment) {
const { enclosingNode } = commentNode;
if (
enclosingNode &&
isNonTerminal(enclosingNode) &&
enclosingNode.name === "methodDeclarator" &&
enclosingNode?.name === "methodDeclarator" &&
!enclosingNode.children.receiverParameter &&
!enclosingNode.children.formalParameterList &&
enclosingNode.children.LBrace[0].startOffset < commentNode.startOffset &&
Expand All @@ -224,7 +227,6 @@ function handleNameComments(commentNode: JavaComment) {
if (
enclosingNode &&
precedingNode &&
isNonTerminal(enclosingNode) &&
isTerminal(precedingNode) &&
precedingNode.tokenType.name === "Identifier" &&
[
Expand Down Expand Up @@ -262,8 +264,8 @@ export type JavaComment = IToken & {
trailing: boolean;
printed: boolean;
enclosingNode?: JavaNonTerminal;
precedingNode?: JavaNonTerminal;
followingNode?: JavaNonTerminal;
precedingNode?: JavaNode;
followingNode?: JavaNode;
};

type FormatterOffOnRange = {
Expand Down
26 changes: 16 additions & 10 deletions packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from "./helpers.js";

const {
align,
breakParent,
conditionalGroup,
group,
Expand Down Expand Up @@ -114,23 +115,28 @@ export default {
conciseLambdaParameter: printSingle,
lambdaBody: printSingle,

conditionalExpression(path, print) {
conditionalExpression(path, print, options) {
const binaryExpression = call(path, print, "binaryExpression");
if (!path.node.children.QuestionMark) {
return binaryExpression;
}
const expressions = map(path, print, "expression");
const contents = indent(
join(line, [
binaryExpression,
["? ", expressions[0]],
[": ", expressions[1]]
])
);
const [consequent, alternate] = map(path, print, "expression");
const parts = [binaryExpression];
const part = [
line,
["? ", options.useTabs ? indent(consequent) : align(2, consequent)],
line,
[": ", options.useTabs ? indent(alternate) : align(2, alternate)]
];
const isNestedTernary =
(path.getNode(4) as JavaNonTerminal | null)?.name ===
"conditionalExpression";
return isNestedTernary ? contents : group(contents);
parts.push(
!isNestedTernary || options.useTabs
? part
: align(Math.max(0, options.tabWidth - 2), part)
);
return isNestedTernary ? parts : group(indent(parts));
},

binaryExpression(path, print, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ public String binaryOperationThatShouldNotBreak() {
return "This operation should" + "not break";
}

public int ternaryOperationThatShouldBreak() {
int shortInteger = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
return thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
}

public int ternaryOperationThatShouldBreak2() {
int shortInteger = thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

public int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
}

void nestedTernary() {
aaaaaaaaaa ? bbbbbbbbbb : cccccccccc ? dddddddddd : eeeeeeeeee ? ffffffffff : gggggggggg;
}

public boolean binaryOperationWithComments() {
boolean a = one || two >> 1 // one
// two
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,6 @@ public String binaryOperationThatShouldNotBreak() {
return "This operation should" + "not break";
}

public int ternaryOperationThatShouldBreak() {
int shortInteger = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
: thisIsAShortInteger;
return thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
: thisIsAShortInteger;
}

public int ternaryOperationThatShouldBreak2() {
int shortInteger = thisIsAVeryLongInteger
? thisIsAnotherVeryLongOne
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
return thisIsAVeryLongInteger
? thisIsAnotherVeryLongOne
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

public int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
}

void nestedTernary() {
aaaaaaaaaa
? bbbbbbbbbb
: cccccccccc
? dddddddddd
: eeeeeeeeee
? ffffffffff
: gggggggggg;
}

public boolean binaryOperationWithComments() {
boolean a =
one ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ public String binaryOperationThatShouldNotBreak() {
return "This operation should" + "not break";
}

public int ternaryOperationThatShouldBreak() {
int shortInteger = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
return thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
}

public int ternaryOperationThatShouldBreak2() {
int shortInteger = thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

public int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
}

void nestedTernary() {
aaaaaaaaaa ? bbbbbbbbbb : cccccccccc ? dddddddddd : eeeeeeeeee ? ffffffffff : gggggggggg;
}

public boolean binaryOperationWithComments() {
boolean a = one || two >> 1 // one
// two
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,6 @@ public String binaryOperationThatShouldNotBreak() {
return "This operation should" + "not break";
}

public int ternaryOperationThatShouldBreak() {
int shortInteger = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
: thisIsAShortInteger;
return thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne
: thisIsAShortInteger;
}

public int ternaryOperationThatShouldBreak2() {
int shortInteger = thisIsAVeryLongInteger
? thisIsAnotherVeryLongOne
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
return thisIsAVeryLongInteger
? thisIsAnotherVeryLongOne
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

public int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
}

void nestedTernary() {
aaaaaaaaaa
? bbbbbbbbbb
: cccccccccc
? dddddddddd
: eeeeeeeeee
? ffffffffff
: gggggggggg;
}

public boolean binaryOperationWithComments() {
boolean a =
one
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from "path";
import url from "url";
import { testSample } from "../../test-utils.js";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

describe("prettier-java", () => {
testSample(path.resolve(__dirname, "spaces"));
testSample(path.resolve(__dirname, "tabs"));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabWidth": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class ConditionalExpression {

int ternaryOperationThatShouldBreak() {
int shortInteger = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
return thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
}

int ternaryOperationThatShouldBreak2() {
int shortInteger = thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
}

void nestedTernary() {
aaaaaaaaaa ? bbbbbbbbbb : cccccccccc ? dddddddddd : eeeeeeeeee ? ffffffffff : gggggggggg;
}

void ternaryWithComments() {
a
? // b
b
: // c
c;
a
// b
? b
// c
: c;
a ? // b
b
: // c
c;
a
? b // b
: c; // c
}
}
Loading