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
42 changes: 34 additions & 8 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 @@

const _ = require("lodash");
const { ifBreak, line, softline } = require("prettier").doc.builders;
const { concat, group, indent } = require("./prettier-builder");
const { concat, group, indent, dedent } = require("./prettier-builder");
const { printTokenWithComments } = require("./comments/format-comments");
const {
handleCommentsBinaryExpression
Expand Down Expand Up @@ -288,7 +288,6 @@ class ExpressionsPrettierVisitor {
const primaryPrefix = this.visit(ctx.primaryPrefix, {
shouldBreakBeforeFirstMethodInvocation: countMethodInvocation > 1
});
const primarySuffixes = this.mapVisit(ctx.primarySuffix);

const suffixes = [];

Expand All @@ -299,16 +298,38 @@ class ExpressionsPrettierVisitor {
) {
suffixes.push(softline);
}
suffixes.push(primarySuffixes[0]);
suffixes.push(
this.visit(ctx.primarySuffix[0], {
shouldDedent:
// dedent when simple method invocation
countMethodInvocation !== 1 &&
// dedent when (chain) method invocation
ctx.primaryPrefix[0] &&
ctx.primaryPrefix[0].children.fqnOrRefType &&
!(
ctx.primaryPrefix[0].children.fqnOrRefType[0].children.Dot !==
undefined
) &&
// indent when lambdaExpression
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent or dedent ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent, we should not dedent when we have a lambdaExpression.
The results are not good.

ctx.primarySuffix[0].children.methodInvocationSuffix &&
ctx.primarySuffix[0].children.methodInvocationSuffix[0].children
.argumentList &&
ctx.primarySuffix[0].children.methodInvocationSuffix[0].children
.argumentList[0].children.expression &&
ctx.primarySuffix[0].children.methodInvocationSuffix[0].children
.argumentList[0].children.expression[0].children
.lambdaExpression === undefined
})
);

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

if (countMethodInvocation === 1) {
Expand All @@ -335,7 +356,7 @@ class ExpressionsPrettierVisitor {
return this.visitSingle(ctx, params);
}

primarySuffix(ctx) {
primarySuffix(ctx, params) {
if (ctx.Dot) {
if (ctx.This) {
return rejectAndConcat([ctx.Dot[0], ctx.This[0]]);
Expand All @@ -352,7 +373,7 @@ class ExpressionsPrettierVisitor {
unqualifiedClassInstanceCreationExpression
]);
}
return this.visitSingle(ctx);
return this.visitSingle(ctx, params);
}

fqnOrRefType(ctx, params) {
Expand Down Expand Up @@ -532,12 +553,17 @@ class ExpressionsPrettierVisitor {
return concat([ctx.Less[0], ctx.Greater[0]]);
}

methodInvocationSuffix(ctx) {
methodInvocationSuffix(ctx, params) {
if (ctx.argumentList === undefined) {
return rejectAndConcat([ctx.LBrace[0], ctx.RBrace[0]]);
}

const argumentList = this.visit(ctx.argumentList);
if (params && params.shouldDedent) {
return dedent(
putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0])
);
}
return putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]);
}

Expand Down
37 changes: 37 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/indent/_input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Indent {

void indetMethod() {
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);

assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();

assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo()
.anotherInvocation(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);

myinstanceobject
.assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();


}
}
35 changes: 35 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/indent/_output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Indent {

void indetMethod() {
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);

assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();

assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo()
.anotherInvocation(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);

myinstanceobject
.assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe("prettier-java", () => {
require("../../test-utils").testSample(__dirname);
});