Skip to content

Commit

Permalink
fix: consistent break after equals
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Feb 12, 2024
1 parent 983a09d commit 8e3cbe1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
20 changes: 17 additions & 3 deletions packages/prettier-plugin-java/src/printers/printer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
42 changes: 30 additions & 12 deletions packages/prettier-plugin-java/test/unit-test/variables/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}

0 comments on commit 8e3cbe1

Please sign in to comment.