@@ -859,16 +859,26 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
859
859
* Right stores an unalignable param.
860
860
*/
861
861
private def groupParams (paramClause : ParamClause , alignParameters : Boolean )(implicit formatterState : FormatterState ): List [EitherAlignableParam ] = {
862
- val ParamClause (_, implicitOption, firstParamOption, otherParams , _) = paramClause
862
+ val ParamClause (_, implicitOption, firstParamOption, otherParamsWithComma , _) = paramClause
863
863
864
- val paramsList : List [ Param ] = otherParams .map { case (comma, param) ⇒ param }.reverse
864
+ val otherParams = otherParamsWithComma .map { case (comma, param) ⇒ param }
865
865
866
- def appendParamToGroup (paramToAppend : Param ,
867
- groupedParams : List [EitherAlignableParam ],
868
- isFirstParam : Boolean ): List [EitherAlignableParam ] = {
866
+ // This is reversed because "appendParamToGroup" works on lists, and will
867
+ // create the list in the reverse order of the list it is given.
868
+ val allParams = (firstParamOption.toList ++ otherParams).reverse
869
+
870
+ def appendParamToGroup (previousParam : Option [Param ],
871
+ paramToAppend : Param ,
872
+ nextParam : Option [Param ],
873
+ groupedParams : List [EitherAlignableParam ]
874
+ ): List [EitherAlignableParam ] = {
875
+
876
+ // This unintuitive line is dependent on the ordering of groupedParams being passed
877
+ // in. It's in reverse.
878
+ val isFirstParam = ! nextParam.isDefined
869
879
870
880
val firstParamAlignable = ! implicitOption.isDefined ||
871
- (newlineBefore(implicitOption.get) && otherParams != Nil && newlineBefore(otherParams.head._2 ))
881
+ (newlineBefore(implicitOption.get) && otherParams != Nil && newlineBefore(otherParams.head))
872
882
873
883
val paramIsAlignable = alignParameters && (! isFirstParam || firstParamAlignable)
874
884
@@ -879,7 +889,22 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
879
889
case Right (param) :: tail ⇒
880
890
Left (ConsecutiveSingleLineParams (List (paramToAppend), sectionLengths, sectionLengths)) :: groupedParams
881
891
case Left (existingParams) :: tail ⇒
882
- Left (existingParams.prepend(paramToAppend, sectionLengths)) :: tail
892
+ if (previousParam.isDefined) {
893
+ /* Group params separately if a blank line between two params:
894
+ * case class Spacing(a: Int = 1,
895
+ * bee: Int = 2,
896
+ *
897
+ * ceee: String = "",
898
+ * deeee: Any = Nothing)
899
+ */
900
+ val numNewlinesBeforeParam = hiddenPredecessors(previousParam.get.firstToken).text.count(_ == '\n ' )
901
+ if (numNewlinesBeforeParam >= 2 )
902
+ Left (ConsecutiveSingleLineParams (List (paramToAppend), sectionLengths, sectionLengths)) :: groupedParams
903
+ else
904
+ Left (existingParams.prepend(paramToAppend, sectionLengths)) :: tail
905
+ } else {
906
+ Left (existingParams.prepend(paramToAppend, sectionLengths)) :: tail
907
+ }
883
908
case Nil ⇒
884
909
Left (ConsecutiveSingleLineParams (List (paramToAppend), sectionLengths, sectionLengths)) :: Nil
885
910
}
@@ -891,11 +916,11 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
891
916
}
892
917
}
893
918
894
- var paramsGroup = paramsList.foldLeft( List [ EitherAlignableParam ]()) { (groupedParams, nextParam) ⇒
895
- appendParamToGroup(nextParam, groupedParams, isFirstParam = false )
896
- }
897
- for (firstParam ← firstParamOption) {
898
- paramsGroup = appendParamToGroup(firstParam, paramsGroup, isFirstParam = true )
919
+ val staggeredParams = Utils .withPreviousAndNext(allParams)
920
+
921
+ val paramsGroup = staggeredParams.foldLeft( List [ EitherAlignableParam ]()) { (groupedParams, prevAndNext) ⇒
922
+ val (prevParam, param, nextParam) = prevAndNext
923
+ appendParamToGroup(prevParam, param, nextParam, groupedParams )
899
924
}
900
925
901
926
paramsGroup
0 commit comments