diff --git a/packages/prettier-plugin-java/src/printers/printer-utils.ts b/packages/prettier-plugin-java/src/printers/printer-utils.ts index 0cb30c1f..1a067074 100644 --- a/packages/prettier-plugin-java/src/printers/printer-utils.ts +++ b/packages/prettier-plugin-java/src/printers/printer-utils.ts @@ -32,7 +32,13 @@ import { 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"; import Indent = builders.Indent; import IfBreak = builders.IfBreak; @@ -659,8 +665,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 4f9459a3..fc9be874 100644 --- a/packages/prettier-plugin-java/test/unit-test/variables/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/variables/_output.java @@ -327,22 +327,40 @@ 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" + ); } }