1414
1515package com .google .googlejavaformat .java ;
1616
17+ import static com .google .common .collect .ImmutableList .toImmutableList ;
1718import static com .google .common .collect .Iterables .getLast ;
1819import static com .google .common .collect .Iterables .getOnlyElement ;
1920import 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 }
0 commit comments