Skip to content

Commit ee1e594

Browse files
committed
Unify breaks before expressions ending in toBuilder and stream
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=239484197
1 parent dc8f413 commit ee1e594

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17+
import static com.google.common.collect.ImmutableList.toImmutableList;
1718
import static com.google.common.collect.Iterables.getLast;
1819
import static com.google.common.collect.Iterables.getOnlyElement;
1920
import static com.google.googlejavaformat.Doc.FillMode.INDEPENDENT;
@@ -1530,7 +1531,8 @@ private boolean handleLogStatement(MethodInvocationTree node) {
15301531
return false;
15311532
}
15321533
parts.addFirst(curr);
1533-
visitDotWithPrefix(ImmutableList.copyOf(parts), false, ImmutableList.of(parts.size() - 1));
1534+
visitDotWithPrefix(
1535+
ImmutableList.copyOf(parts), false, ImmutableList.of(parts.size() - 1), INDEPENDENT);
15341536
return true;
15351537
}
15361538

@@ -1553,16 +1555,17 @@ private boolean handleLogStatement(MethodInvocationTree node) {
15531555
"withCause",
15541556
"withStackTrace");
15551557

1556-
private static Stream<Long> handleStream(List<ExpressionTree> parts) {
1558+
private static ImmutableList<Long> handleStream(List<ExpressionTree> parts) {
15571559
return indexes(
1558-
parts.stream(),
1559-
p -> {
1560-
if (!(p instanceof MethodInvocationTree)) {
1561-
return false;
1562-
}
1563-
Name name = getMethodName((MethodInvocationTree) p);
1564-
return Stream.of("stream", "toBuilder").anyMatch(name::contentEquals);
1565-
});
1560+
parts.stream(),
1561+
p -> {
1562+
if (!(p instanceof MethodInvocationTree)) {
1563+
return false;
1564+
}
1565+
Name name = getMethodName((MethodInvocationTree) p);
1566+
return Stream.of("stream", "toBuilder").anyMatch(name::contentEquals);
1567+
})
1568+
.collect(toImmutableList());
15661569
}
15671570

15681571
private static <T> Stream<Long> indexes(Stream<T> stream, Predicate<T> predicate) {
@@ -2689,10 +2692,11 @@ void visitDot(ExpressionTree node0) {
26892692
}
26902693
}
26912694

2692-
handleStream(items).forEach(x -> prefixes.add(x.intValue()));
2693-
2695+
ImmutableList<Long> streamPrefixes = handleStream(items);
2696+
streamPrefixes.forEach(x -> prefixes.add(x.intValue()));
26942697
if (!prefixes.isEmpty()) {
2695-
visitDotWithPrefix(items, needDot, prefixes);
2698+
visitDotWithPrefix(
2699+
items, needDot, prefixes, streamPrefixes.isEmpty() ? INDEPENDENT : UNIFIED);
26962700
} else {
26972701
visitRegularDot(items, needDot);
26982702
}
@@ -2788,7 +2792,10 @@ private boolean fillFirstArgument(ExpressionTree e, List<ExpressionTree> items,
27882792
* a syntactic unit
27892793
*/
27902794
private void visitDotWithPrefix(
2791-
List<ExpressionTree> items, boolean needDot, Collection<Integer> prefixes) {
2795+
List<ExpressionTree> items,
2796+
boolean needDot,
2797+
Collection<Integer> prefixes,
2798+
FillMode prefixFillMode) {
27922799
// Are there method invocations or field accesses after the prefix?
27932800
boolean trailingDereferences = !prefixes.isEmpty() && getLast(prefixes) < items.size() - 1;
27942801

@@ -2804,7 +2811,7 @@ private void visitDotWithPrefix(
28042811
if (needDot) {
28052812
FillMode fillMode;
28062813
if (!unconsumedPrefixes.isEmpty() && i <= unconsumedPrefixes.peekFirst()) {
2807-
fillMode = FillMode.INDEPENDENT;
2814+
fillMode = prefixFillMode;
28082815
} else {
28092816
fillMode = FillMode.UNIFIED;
28102817
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class B124394008 {
2+
{
3+
LocalReviewProto.Builder newReview =
4+
readProfileResponse.flatMap(authorExtractor::extract)
5+
.map(luReviewsExtractor::toLocalReviewProto)
6+
.orElse(LocalReviewProto.getDefaultInstance()).toBuilder();
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class B124394008 {
2+
{
3+
LocalReviewProto.Builder newReview =
4+
readProfileResponse
5+
.flatMap(authorExtractor::extract)
6+
.map(luReviewsExtractor::toLocalReviewProto)
7+
.orElse(LocalReviewProto.getDefaultInstance())
8+
.toBuilder();
9+
}
10+
}

0 commit comments

Comments
 (0)