Skip to content

Commit

Permalink
fix comments formatting with variable declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Dec 23, 2019
1 parent 22e5377 commit 823a49f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/prettier-plugin-java/src/printers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _ = require("lodash");
const { line, softline, hardline } = require("prettier").doc.builders;
const {
hasLeadingLineComments,
reject,
rejectAndConcat,
rejectAndJoin,
Expand Down Expand Up @@ -188,9 +189,23 @@ class ClassesPrettierVisitor {
if (ctx.Equals) {
const variableInitializer = this.visit(ctx.variableInitializer);

if (hasLeadingLineComments(ctx.variableInitializer[0])) {
return group(
indent(
rejectAndJoin(hardline, [
rejectAndJoin(" ", [variableDeclaratorId, ctx.Equals[0]]),
variableInitializer
])
)
);
}

if (
// Array Initialisation
ctx.variableInitializer[0].children.arrayInitializer !== undefined ||
// Lambda expression
ctx.variableInitializer[0].children.expression[0].children
.lambdaExpression !== undefined ||
// Ternary Expression
(ctx.variableInitializer[0].children.expression[0].children
.ternaryExpression !== undefined &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,21 @@ describe("VariableDeclarator", () => {
"value = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne\n ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne\n : thisIsAShortInteger";
expect(formattedText).to.equal(expectedContents);
});
it("should format comment on its own line between equal and variable initializer", () => {
const snippet = "value = \n// comment2\n 1";
const entryPoint = "variableDeclarator";

const formattedText = formatJavaSnippet(snippet, entryPoint);
const expectedContents = "value =\n // comment2\n 1";
expect(formattedText).to.equal(expectedContents);
});

it("should format comments on several lines between equal and variable initializer", () => {
const snippet = "value = // comment1\n// comment2\n 1";
const entryPoint = "variableDeclarator";

const formattedText = formatJavaSnippet(snippet, entryPoint);
const expectedContents = "value = // comment1\n // comment2\n 1";
expect(formattedText).to.equal(expectedContents);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ public void breakMultipleMethods() {
boolean willDrop = predictDropResponsegetSendResultisIgnorableFailure || predictDropResponsegetSendResultisFatalError;
boolean willDrop = predictDropResponse.getSendResult().isIgnorableFailure() || predictDropResponsegetSendResultisFatalError;
}

public methodWithVariableInitializationWithComments() {
Map<String, String> map =
// there is a random comment on this line up here
// and then there is a separate comment on this line down here
new HashMap<>(
someRandomMethodThatReturnsTheInitialMapThatWeWantToMutate()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,13 @@ public void breakMultipleMethods() {
predictDropResponse.getSendResult().isIgnorableFailure() ||
predictDropResponsegetSendResultisFatalError;
}

public methodWithVariableInitializationWithComments() {
Map<String, String> map =
// there is a random comment on this line up here
// and then there is a separate comment on this line down here
new HashMap<>(
someRandomMethodThatReturnsTheInitialMapThatWeWantToMutate()
);
}
}

0 comments on commit 823a49f

Please sign in to comment.