Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(printer): properly break and indent lambda with comments #604

Merged
merged 1 commit into from
Feb 15, 2024
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
8 changes: 5 additions & 3 deletions packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import forEach from "lodash/forEach.js";
import { builders } from "prettier/doc";
import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
import { isAnnotationCstNode } from "../types/utils.js";
import { isArgumentListHuggable } from "../utils/expressions-utils.js";
import { printArgumentListWithBraces } from "../utils/index.js";
import { printTokenWithComments } from "./comments/format-comments.js";
import { handleCommentsBinaryExpression } from "./comments/handle-comments.js";
Expand Down Expand Up @@ -625,7 +624,10 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
);
}

argumentList(ctx: ArgumentListCtx, params?: { shouldBreak?: boolean }) {
argumentList(
ctx: ArgumentListCtx,
params?: { isHuggable?: boolean; shouldBreak?: boolean }
) {
const shouldBreak = params?.shouldBreak;
const expressions = this.mapVisit(ctx.expression, params);

Expand All @@ -635,7 +637,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix]));
const otherArguments = rejectAndJoinSeps(commas, expressions);

if (lastArgument && isArgumentListHuggable(ctx)) {
if (lastArgument && params?.isHuggable) {
const argumentListGroupId = Symbol("argumentList");
const separator =
shouldBreak === true ? hardline : shouldBreak === false ? "" : softline;
Expand Down
12 changes: 0 additions & 12 deletions packages/prettier-plugin-java/src/utils/expressions-utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ArgumentListCstNode, IToken } from "java-parser";
import type { ArgumentListCstNode, ArgumentListCtx, IToken } from "java-parser";
import { builders, utils } from "prettier/doc";
import { isArgumentListHuggable } from "./expressions-utils.js";
import { putIntoBraces } from "../printers/printer-utils.js";

const { breakParent, conditionalGroup, softline } = builders;
Expand All @@ -11,10 +10,17 @@ export default function printArgumentListWithBraces(
rBrace: IToken,
lBrace: IToken
) {
const argumentListCtx = argumentListNodes?.[0].children;
if (argumentListCtx && isArgumentListHuggable(argumentListCtx)) {
if (
argumentListNodes &&
!argumentListNodes[0].leadingComments &&
!rBrace.leadingComments &&
isArgumentListHuggable(argumentListNodes[0].children)
) {
const [flat, expanded] = [false, true].map(shouldBreak => {
const argumentList = this.visit(argumentListNodes, { shouldBreak });
const argumentList = this.visit(argumentListNodes, {
isHuggable: true,
shouldBreak
});
return putIntoBraces(argumentList, "", lBrace, rBrace);
});
return [
Expand All @@ -26,3 +32,13 @@ export default function printArgumentListWithBraces(
const argumentList = this.visit(argumentListNodes);
return putIntoBraces(argumentList, softline, lBrace, rBrace);
}

function isArgumentListHuggable(argumentList: ArgumentListCtx) {
const expressions = argumentList.expression;
return (
(expressions.length === 1 ||
expressions[expressions.length - 1].children.lambdaExpression?.[0]
.children.lambdaBody[0].children.block !== undefined) &&
expressions.filter(({ children }) => children.lambdaExpression).length === 1
);
}
20 changes: 20 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/lambda/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ void argumentAfterLambdaWithBlock() {
return f;
}, g);
}

void lambdaWithLeadingComments() {
System.out.println(
List.of(1, 2, 3).stream().map(
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
v -> v * 2
).collect(Collectors.summingInt(v -> v))
);
}

void lambdaWithTrailingComments() {
System.out.println(
List.of(1, 2, 3).stream().map(
v -> v * 2
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
).collect(Collectors.summingInt(v -> v))
);
}
}

class T {
Expand Down
26 changes: 26 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/lambda/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,32 @@ void argumentAfterLambdaWithBlock() {
g
);
}

void lambdaWithLeadingComments() {
System.out.println(
List.of(1, 2, 3)
.stream()
.map(
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
v -> v * 2
)
.collect(Collectors.summingInt(v -> v))
);
}

void lambdaWithTrailingComments() {
System.out.println(
List.of(1, 2, 3)
.stream()
.map(
v -> v * 2
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
)
.collect(Collectors.summingInt(v -> v))
);
}
}

class T {
Expand Down
Loading