From 8986c8bd609c309826ffbc761e8097ad0f725ab2 Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Sun, 11 Feb 2024 20:28:13 -0700 Subject: [PATCH] fix: consistent break after equals --- .../src/printers/printer-utils.ts | 20 ++++++++-- .../test/unit-test/variables/_input.java | 4 ++ .../test/unit-test/variables/_output.java | 40 ++++++++++++++----- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/packages/prettier-plugin-java/src/printers/printer-utils.ts b/packages/prettier-plugin-java/src/printers/printer-utils.ts index 41d1ec7d..f408935d 100644 --- a/packages/prettier-plugin-java/src/printers/printer-utils.ts +++ b/packages/prettier-plugin-java/src/printers/printer-utils.ts @@ -31,7 +31,13 @@ import { getTokenLeadingComments, printTokenWithComments } from "./comments/format-comments.js"; -import { concat, group, ifBreak, join } from "./prettier-builder.js"; +import { + concat, + group, + ifBreak, + indentIfBreak, + join +} from "./prettier-builder.js"; const { indent, hardline, line } = builders; @@ -654,8 +660,16 @@ export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc { } } level.push(nodes.shift()!); - const content = group(join(line, level)); - return levelOperator === "=" ? indent(content) : content; + const lineGroupId = Symbol("line"); + return group( + levelOperator === "=" + ? [ + level[0], + indent(group(line, { id: lineGroupId })), + indentIfBreak(level[1], { groupId: lineGroupId }) + ] + : join(line, level) + ); } function getOperator(tokens: IToken[]) { diff --git a/packages/prettier-plugin-java/test/unit-test/variables/_input.java b/packages/prettier-plugin-java/test/unit-test/variables/_input.java index 92a70fad..f315ea0f 100644 --- a/packages/prettier-plugin-java/test/unit-test/variables/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/variables/_input.java @@ -201,5 +201,9 @@ void assignment() { bbbbbbbbbbbbbbbbb ? ccccccccccccccccc : ddddddddddddddddd; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ccccccccccccccccccccccccccccccccccccccc + ddddddddddddddddddddddddddddddddd; + + foo = new Foo("aaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccc", "dddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeee", "ffffffffffffffffffff"); + + final Foo bar = new Foo("aaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccc", "dddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeee", "ffffffffffffffffffff"); } } diff --git a/packages/prettier-plugin-java/test/unit-test/variables/_output.java b/packages/prettier-plugin-java/test/unit-test/variables/_output.java index 033c473c..cc6941c3 100644 --- a/packages/prettier-plugin-java/test/unit-test/variables/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/variables/_output.java @@ -326,21 +326,39 @@ public methodWithVariableInitializationWithComments() { } void assignment() { - fileSystemDetails = - FileHandlerDetails.builder() - .fileSystemType( - EntityUtils.update( - entity.getFileSystemDetails().getFileSystemType(), - update.getFileSystemDetails().getFileSystemType() - ) - ); - - aaaaaaaaaaaaaaaaa = - bbbbbbbbbbbbbbbbb ? ccccccccccccccccc : ddddddddddddddddd; + fileSystemDetails = FileHandlerDetails.builder() + .fileSystemType( + EntityUtils.update( + entity.getFileSystemDetails().getFileSystemType(), + update.getFileSystemDetails().getFileSystemType() + ) + ); + + aaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbb + ? ccccccccccccccccc + : ddddddddddddddddd; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ccccccccccccccccccccccccccccccccccccccc + ddddddddddddddddddddddddddddddddd; + + foo = new Foo( + "aaaaaaaaaaaaaaaaaaaa", + "bbbbbbbbbbbbbbbbbbbb", + "cccccccccccccccccccc", + "dddddddddddddddddddd", + "eeeeeeeeeeeeeeeeeeee", + "ffffffffffffffffffff" + ); + + final Foo bar = new Foo( + "aaaaaaaaaaaaaaaaaaaa", + "bbbbbbbbbbbbbbbbbbbb", + "cccccccccccccccccccc", + "dddddddddddddddddddd", + "eeeeeeeeeeeeeeeeeeee", + "ffffffffffffffffffff" + ); } }