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
4 changes: 2 additions & 2 deletions packages/prettier-plugin-java/src/cst-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class CstPrettierPrinter extends BaseJavaCstVisitor {
// TODO: this methods should be defined on the prototype
// defining as instance members **after** the validations to avoid
// false positive errors on redundant methods
this.mapVisit = elements => {
this.mapVisit = (elements, params) => {
if (elements === undefined) {
// TODO: can optimize this by returning an immutable empty array singleton.
return [];
}

return elements.map(this.visit, this);
return elements.map(element => this.visit(element, params), this);
};

this.getSingle = function(ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ class BlocksAndStatementPrettierVisitor {

returnStatement(ctx) {
if (ctx.expression) {
const expression = this.visit(ctx.expression);
const expression = this.visit(ctx.expression, {
addParenthesisToWrapStatement: true,
shouldIndentBinaryOperationInExpression: false
});

return rejectAndConcat([
concat([ctx.Return[0], " "]),
Expand Down
77 changes: 72 additions & 5 deletions packages/prettier-plugin-java/src/printers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,78 @@ class ClassesPrettierVisitor {
const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
if (ctx.Equals) {
const variableInitializer = this.visit(ctx.variableInitializer);
return rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
]);

if (
// Array Initialisation
ctx.variableInitializer[0].children.arrayInitializer !== undefined ||
// Ternary Expression
(ctx.variableInitializer[0].children.expression[0].children
.ternaryExpression !== undefined &&
ctx.variableInitializer[0].children.expression[0].children
.ternaryExpression[0].children.QuestionMark !== undefined)
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
]);
}

if (
ctx.variableInitializer[0].children.expression[0].children
.ternaryExpression !== undefined
) {
const firstPrimary =
ctx.variableInitializer[0].children.expression[0].children
.ternaryExpression[0].children.binaryExpression[0].children
.unaryExpression[0].children.primary[0];

// Cast Expression
if (
firstPrimary.children.primaryPrefix[0].children.castExpression !==
undefined
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
]);
}

// New Expression
if (
firstPrimary.children.primaryPrefix[0].children.newExpression !==
undefined
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
]);
}

// Method Invocation
if (
firstPrimary.children.primarySuffix !== undefined &&
firstPrimary.children.primarySuffix[0].children
.methodInvocationSuffix !== undefined
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
]);
}
}

return group(
indent(
rejectAndJoin(line, [
rejectAndJoin(" ", [variableDeclaratorId, ctx.Equals[0]]),
variableInitializer
])
)
);
}
return variableDeclaratorId;
}
Expand Down
140 changes: 96 additions & 44 deletions packages/prettier-plugin-java/src/printers/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-unused-vars */

const _ = require("lodash");
const { line, softline } = require("prettier").doc.builders;
const { ifBreak, line, softline, hardline } = require("prettier").doc.builders;
const {
concat,
group,
Expand All @@ -19,16 +19,17 @@ const {
isExplicitLambdaParameter,
putIntoBraces,
separateTokensIntoGroups,
isShiftOperator
isShiftOperator,
isUniqueMethodInvocation
} = require("./printer-utils");

class ExpressionsPrettierVisitor {
constantExpression(ctx) {
return this.visitSingle(ctx);
}

expression(ctx) {
return this.visitSingle(ctx);
expression(ctx, params) {
return this.visitSingle(ctx, params);
}

lambdaExpression(ctx) {
Expand Down Expand Up @@ -128,8 +129,8 @@ class ExpressionsPrettierVisitor {
return this.visitSingle(ctx);
}

ternaryExpression(ctx) {
const binaryExpression = this.visit(ctx.binaryExpression);
ternaryExpression(ctx, params) {
const binaryExpression = this.visit(ctx.binaryExpression, params);
if (ctx.QuestionMark) {
const expression1 = this.visit(ctx.expression[0]);
const expression2 = this.visit(ctx.expression[1]);
Expand All @@ -153,7 +154,7 @@ class ExpressionsPrettierVisitor {
return binaryExpression;
}

binaryExpression(ctx) {
binaryExpression(ctx, params) {
const referenceType = this.mapVisit(ctx.referenceType);
const expression = this.mapVisit(ctx.expression);
const unaryExpression = this.mapVisit(ctx.unaryExpression);
Expand All @@ -165,6 +166,10 @@ class ExpressionsPrettierVisitor {
const segmentsSplittedByBinaryOperator = [];
let currentSegment = [];

if (groupsOfOperator.length === 1 && groupsOfOperator[0].length === 0) {
return unaryExpression.shift();
}

groupsOfOperator.forEach(subgroup => {
currentSegment = [unaryExpression.shift()];
for (let i = 0; i < subgroup.length; i++) {
Expand Down Expand Up @@ -198,18 +203,44 @@ class ExpressionsPrettierVisitor {
);
i += 2;
} else if (matchCategory(token, "'BinaryOperator'")) {
currentSegment.push(
indent(rejectAndJoin(line, [token, unaryExpression.shift()]))
);
const binaryOperation = rejectAndJoin(line, [
token,
unaryExpression.shift()
]);
if (
params !== undefined &&
!params.shouldIndentBinaryOperationInExpression
) {
currentSegment.push(binaryOperation);
} else {
currentSegment.push(indent(binaryOperation));
}
}
}
segmentsSplittedByBinaryOperator.push(
group(rejectAndJoin(" ", currentSegment))
);
});
if (groupsOfOperator.length === 0) {
return unaryExpression.shift();

if (params !== undefined && params.addParenthesisToWrapStatement) {
return group(
concat([
ifBreak("(", ""),
indent(
concat([
softline,
rejectAndJoinSeps(
sortedBinaryOperators.map(elt => concat([" ", elt, line])),
segmentsSplittedByBinaryOperator
)
])
),
softline,
ifBreak(")")
])
);
}

return group(
rejectAndJoinSeps(
sortedBinaryOperators.map(elt => concat([" ", elt, line])),
Expand Down Expand Up @@ -251,52 +282,56 @@ class ExpressionsPrettierVisitor {
}

primary(ctx) {
const primaryPrefix = this.visit(ctx.primaryPrefix);
const countMethodInvocation = isUniqueMethodInvocation(ctx.primarySuffix);

const primaryPrefix = this.visit(ctx.primaryPrefix, {
shouldBreakBeforeFirstMethodInvocation: countMethodInvocation > 1
});
const primarySuffixes = this.mapVisit(ctx.primarySuffix);

const suffixes = [];
let addIndent = false;
for (let i = 0; i < primarySuffixes.length; i++) {
if (ctx.primarySuffix[i].children.Dot !== undefined) {
suffixes.push(indent(softline), primarySuffixes[i]);
addIndent = true;
} else if (
ctx.primarySuffix[i].children.methodInvocationSuffix === undefined

if (ctx.primarySuffix !== undefined) {
if (
ctx.primarySuffix[0].children.Dot !== undefined &&
ctx.primaryPrefix[0].children.newExpression !== undefined
) {
suffixes.push(softline, primarySuffixes[i]);
} else {
if (addIndent) {
suffixes.push(indent(primarySuffixes[i]));
addIndent = false;
} else {
suffixes.push(primarySuffixes[i]);
suffixes.push(softline);
}
suffixes.push(primarySuffixes[0]);

for (let i = 1; i < primarySuffixes.length; i++) {
if (
ctx.primarySuffix[i].children.Dot !== undefined &&
ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined
) {
suffixes.push(softline);
}
suffixes.push(primarySuffixes[i]);
}
}

let firstSeparator = suffixes.shift();
if (
ctx.primaryPrefix[0].children.This !== undefined ||
firstSeparator === undefined
) {
firstSeparator = "";
if (countMethodInvocation === 1) {
return group(
rejectAndConcat([
primaryPrefix,
suffixes[0],
indent(rejectAndConcat(suffixes.slice(1)))
])
);
}
}

return group(
rejectAndConcat([
primaryPrefix,
firstSeparator,
rejectAndConcat(suffixes)
])
rejectAndConcat([primaryPrefix, indent(rejectAndConcat(suffixes))])
);
}

primaryPrefix(ctx) {
primaryPrefix(ctx, params) {
if (ctx.This || ctx.Void || ctx.Boolean) {
return getImageWithComments(this.getSingle(ctx));
}

return this.visitSingle(ctx);
return this.visitSingle(ctx, params);
}

primarySuffix(ctx) {
Expand All @@ -319,10 +354,25 @@ class ExpressionsPrettierVisitor {
return this.visitSingle(ctx);
}

fqnOrRefType(ctx) {
fqnOrRefType(ctx, params) {
const fqnOrRefTypePart = this.mapVisit(ctx.fqnOrRefTypePart);
const dims = this.visit(ctx.dims);
const dots = ctx.Dot ? ctx.Dot : [];

if (
params !== undefined &&
params.shouldBreakBeforeFirstMethodInvocation === true
) {
return rejectAndConcat([
indent(
rejectAndJoin(concat([softline, dots[0]]), [
fqnOrRefTypePart[0],
rejectAndJoinSeps(dots.slice(1), fqnOrRefTypePart.slice(1)),
dims
])
)
]);
}
return rejectAndConcat([rejectAndJoinSeps(dots, fqnOrRefTypePart), dims]);
}

Expand Down Expand Up @@ -375,8 +425,10 @@ class ExpressionsPrettierVisitor {
}

parenthesisExpression(ctx) {
const expression = this.visit(ctx.expression);
return rejectAndConcat([ctx.LBrace[0], expression, ctx.RBrace[0]]);
const expression = this.visit(ctx.expression, {
shouldIndentBinaryOperationInExpression: false
});
return putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]);
}

castExpression(ctx) {
Expand Down
22 changes: 21 additions & 1 deletion packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,25 @@ function compareFqn(packageOrTypeNameFirst, packageOrTypeNameSecond) {
return 0;
}

function isUniqueMethodInvocation(primarySuffixes) {
if (primarySuffixes === undefined) {
return 0;
}

let count = 0;
primarySuffixes.forEach(primarySuffix => {
if (primarySuffix.children.methodInvocationSuffix !== undefined) {
count++;

if (count > 1) {
return 2;
}
}
});

return count;
}

module.exports = {
buildFqn,
reject,
Expand All @@ -609,5 +628,6 @@ module.exports = {
buildOriginalText,
getCSTNodeStartEndToken,
isStatementEmptyStatement,
sortImports
sortImports,
isUniqueMethodInvocation
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void doSomethingLongNew2() {
}

public void doSomethingLongStatic() {
return Object.something()
return Object
.something()
.more()
.and()
.that()
Expand Down
Loading