Skip to content

Commit 50da347

Browse files
author
Daniel Trinh
committed
added support for param grouping by blank lines
1 parent d1e824a commit 50da347

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

scalariform/src/main/scala/scalariform/formatter/ExprFormatter.scala

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -859,16 +859,26 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
859859
* Right stores an unalignable param.
860860
*/
861861
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
863863

864-
val paramsList: List[Param] = otherParams.map { case (comma, param) param }.reverse
864+
val otherParams = otherParamsWithComma.map { case (comma, param) param }
865865

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
869879

870880
val firstParamAlignable = !implicitOption.isDefined ||
871-
(newlineBefore(implicitOption.get) && otherParams != Nil && newlineBefore(otherParams.head._2))
881+
(newlineBefore(implicitOption.get) && otherParams != Nil && newlineBefore(otherParams.head))
872882

873883
val paramIsAlignable = alignParameters && (!isFirstParam || firstParamAlignable)
874884

@@ -879,7 +889,22 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
879889
case Right(param) :: tail
880890
Left(ConsecutiveSingleLineParams(List(paramToAppend), sectionLengths, sectionLengths)) :: groupedParams
881891
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+
}
883908
case Nil
884909
Left(ConsecutiveSingleLineParams(List(paramToAppend), sectionLengths, sectionLengths)) :: Nil
885910
}
@@ -891,11 +916,11 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
891916
}
892917
}
893918

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)
899924
}
900925

901926
paramsGroup

scalariform/src/test/scala/scalariform/formatter/TemplateFormatterTest.scala

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesW
431431
"""def a(
432432
| p1: => (SomeLongByNameParam => SomeShorterParam) = Null,
433433
| param2: SomeShorterParam = Null): A""" ==>
434-
"""def a(
434+
"""def a(
435435
| p1: => (SomeLongByNameParam => SomeShorterParam) = Null,
436436
| param2: SomeShorterParam = Null): A"""
437437

@@ -450,7 +450,7 @@ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesW
450450
| paramTwo: Int = 2,
451451
| paramThree: String = "3")"""
452452

453-
// Groups and formats consecutive single line parameters
453+
// Groups and formats consecutive single line parameters (multi line params)
454454
"""case class Spacing(param: Int = 1,
455455
|paramTwo: Int = 2,
456456
|paramThree: {
@@ -466,6 +466,20 @@ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesW
466466
| paramFour: Option[String] = Some("One"),
467467
| paramFive: Any = Nothing)"""
468468

469+
// Groups and formats consecutive single line parameters (newlines)
470+
"""case class Spacing(
471+
|param: Int = 1,
472+
|paramTwo: Int = 2,
473+
|
474+
|paramFour: Option[String] = Some("One"),
475+
|paramFive: Any = Nothing)""" ==>
476+
"""case class Spacing(
477+
| param: Int = 1,
478+
| paramTwo: Int = 2,
479+
|
480+
| paramFour: Option[String] = Some("One"),
481+
| paramFive: Any = Nothing)"""
482+
469483
// Aligns implicits and curried parameters properly
470484
"""class SomeClass(
471485
|parameterOne: Int = 1,
@@ -486,7 +500,7 @@ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesW
486500
| five: String,
487501
| six: Boolean)"""
488502

489-
// Handles annotations, modifiers, and comments
503+
// Handles annotations, modifiers, and comments
490504
"""def extraStuff(
491505
|// comment 1
492506
|@Annotated paramOne: Int = 1, // comment 2

0 commit comments

Comments
 (0)